pipe-menu helper library v2
Mike
logan at dct.com
Mon Nov 10 18:18:39 EST 2003
Heya,
I made some quick changes to allow for menus and submenus and yadda.
The attached file has a better test, basicly a smaller version of my menu. If you try this, be sure not to click Exit or such, because they actually work. :P
I thought skipping the menu-id arg for ob3_pipe_menu_menu_add(). Not sure if there's any reason to set it manually.. Perhaps test to see if it's NULL, then generate one.
Feedback would be appreciated.
Thanks,
Mike
-------------- next part --------------
/*
* gcc -Wall -O2 -g `pkg-config --cflags obparser-3.0` openbox3-pipe_menu.c \
* -o openbox3-pipe_menu `pkg-config --libs obparser-3.0`
*/
#include <openbox/parse.h>
/* -- openbox pipe-menu helper functions ------------------------------------ */
gboolean ob3_pipe_menu_start = FALSE;
xmlDocPtr ob3_pipe_menu_doc;
xmlNodePtr ob3_pipe_menu_node;
/*
* ob3_pipe_menu_startup:
* initialize pipe-menu helper functions, setup xmlDocPtr, xmlNodePtr.
*/
void ob3_pipe_menu_startup(void)
{
g_return_if_fail(ob3_pipe_menu_start != TRUE);
ob3_pipe_menu_start = TRUE;
ob3_pipe_menu_doc = xmlNewDoc("1.0");
ob3_pipe_menu_node = xmlNewDocNode(ob3_pipe_menu_doc, NULL,
"openbox_pipe_menu", NULL);
xmlDocSetRootElement(ob3_pipe_menu_doc, ob3_pipe_menu_node);
}
/*
* ob3_pipe_menu_shutdown:
* print xml data if child nodes exist, free xml memory.
*/
void ob3_pipe_menu_shutdown(void)
{
g_return_if_fail(ob3_pipe_menu_start != FALSE);
if(ob3_pipe_menu_node->children != NULL)
xmlSaveFormatFileEnc("-", ob3_pipe_menu_doc, "UTF-8", 1);
xmlFreeDoc(ob3_pipe_menu_doc);
}
/*
* ob3_pipe_menu_node_add:
* @node: xmlNodePtr to use, if NULL toplevel is used.
* @name: string to use for node name.
* @value: string to use for node value, NULL can be used for no node value.
*
* adds a property to the specified node.
*
* <@name>@value</@name>
*/
xmlNodePtr ob3_pipe_menu_node_add(xmlNodePtr node, const gchar *name,
const gchar *value)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
g_return_val_if_fail(ob3_pipe_menu_start != FALSE, NULL);
g_return_val_if_fail(name != NULL && name[0] != '\0', NULL);
return(xmlNewTextChild(n, NULL, name, value));
}
/*
* ob3_pipe_menu_node_set_prop:
* @node: xmlNodePtr to use, if NULL toplevel is used.
* @name: string to use for property name.
* @value: string to use for property.
*
* adds a property to the specified node.
*
* <@node @name="@value" />
*/
void ob3_pipe_menu_node_set_prop(xmlNodePtr node, const gchar *name,
const gchar *value)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
g_return_if_fail(ob3_pipe_menu_start != FALSE);
g_return_if_fail(name != NULL && name[0] != '\0');
xmlSetProp(n, name, value);
}
/*
* ob3_pipe_menu_separator_add:
* @node: xmlNodePtr to use, if NULL toplevel is used.
*
* adds a separator to the openbox pipe-menu.
*
* <separator />
*/
void ob3_pipe_menu_separator_add(xmlNodePtr node)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
g_return_if_fail(ob3_pipe_menu_start != FALSE);
ob3_pipe_menu_node_add(n, "separator", NULL);
}
/*
* ob3_pipe_menu_menu_add:
* @node: xmlNodePtr to use, if NULL toplevel is used.
* @label: string to use for menu label.
* @id: string to use for menu-id.
*
* adds a menu to the openbox pipe-menu, returns xmlNodePtr.
*
* <menu id="@id" label="@label">
* </menu>
*/
xmlNodePtr ob3_pipe_menu_menu_add(xmlNodePtr node, const gchar *label,
const gchar *id)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
xmlNodePtr p;
g_return_val_if_fail(ob3_pipe_menu_start != FALSE, NULL);
g_return_val_if_fail(id != NULL && id[0] != '\0', NULL);
g_return_val_if_fail(label != NULL && label[0] != '\0', NULL);
p = ob3_pipe_menu_node_add(n, "menu", NULL);
ob3_pipe_menu_node_set_prop(p, "id", id);
ob3_pipe_menu_node_set_prop(p, "label", label);
return(p);
}
/*
* ob3_pipe_menu_action_add:
* @node: xmlNodePtr to use, if NULL toplevel is used.
* @action: string to use for action name.
* @execute: string to use for execute action. if NULL execute is excluded.
*
* adds an action to a node item, returns xmlNodePtr.
*
* <action name="@action">
* <execute>@execute</execute>
* </action>
*/
xmlNodePtr ob3_pipe_menu_action_add(xmlNodePtr node, const gchar *action,
const gchar *execute)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
xmlNodePtr p;
g_return_val_if_fail(ob3_pipe_menu_start != FALSE, NULL);
g_return_val_if_fail(action != NULL && action[0] != '\0', NULL);
p = ob3_pipe_menu_node_add(n, "action", NULL);
ob3_pipe_menu_node_set_prop(p, "name", action);
if(execute != NULL)
ob3_pipe_menu_node_add(p, "execute", execute);
return(p);
}
/*
* ob3_pipe_menu_item_add:
* @node: xmlNodePtr to use, if NULL toplevel is used.
* @label: string to use for item label.
* @action: string to use for execute action. if NULL action is excluded.
*
* adds a menu item to the openbox pipe-menu, returns xmlNodePtr.
*
* <item label="@label">
* <action name="Execute">
* <execute>@action</execute>
* </action>
* </item>
*/
xmlNodePtr ob3_pipe_menu_item_add(xmlNodePtr node, const gchar *label,
const gchar *action)
{
xmlNodePtr n = (node != NULL) ? node : ob3_pipe_menu_node;
xmlNodePtr p;
g_return_val_if_fail(ob3_pipe_menu_start != FALSE, NULL);
g_return_val_if_fail(label != NULL && label[0] != '\0', NULL);
p = ob3_pipe_menu_node_add(n, "item", NULL);
ob3_pipe_menu_node_set_prop(p, "label", label);
if(action != NULL && action[0] != '\0')
ob3_pipe_menu_action_add(p, "Execute", action);
return(p);
}
/* -- openbox pipe-menu helper functions ------------------------------------ */
int main(void)
{
xmlNodePtr n;
ob3_pipe_menu_startup();
ob3_pipe_menu_item_add(NULL, "rxvt", "rxvt");
ob3_pipe_menu_item_add(NULL, "lock", "xlock -mode blank");
ob3_pipe_menu_separator_add(NULL);
if((n = ob3_pipe_menu_menu_add(NULL, "Applications", "menu-apps"))) {
ob3_pipe_menu_item_add(n, "Open Office", "ooffice");
ob3_pipe_menu_item_add(n, "gDeskCal", "gdeskcal");
ob3_pipe_menu_item_add(n, "Qball's Inventory", "inventory");
}
if((n = ob3_pipe_menu_menu_add(NULL, "Internet", "menu-inet"))) {
ob3_pipe_menu_item_add(n, "Mozilla", "mozilla");
ob3_pipe_menu_item_add(n, "Mozilla Firebird", "MozillaFirebird");
ob3_pipe_menu_separator_add(n);
ob3_pipe_menu_item_add(n, "Xchat", "xchat");
}
if((n = ob3_pipe_menu_menu_add(NULL, "Openbox", "menu-openbox"))) {
xmlNodePtr p;
if((p = ob3_pipe_menu_menu_add(n, "Themes", "menu-themes"))) {
ob3_pipe_menu_item_add(p, "Artwiz", "ob3_theme ...");
ob3_pipe_menu_item_add(p, "I-merry+lgn_mods", "ob3_theme ...");
}
if((p = ob3_pipe_menu_menu_add(n, "Wallpaper", "menu-wall"))) {
ob3_pipe_menu_item_add(p, "capricorn.jpg", "ob3_wall ...");
ob3_pipe_menu_item_add(p, "galleon.jpg", "ob3_wall ...");
}
ob3_pipe_menu_separator_add(n);
if((p = ob3_pipe_menu_menu_add(n, "Desktops", "menu-desktops"))) {
ob3_pipe_menu_item_add(p, "One", NULL);
ob3_pipe_menu_item_add(p, "Two", NULL);
ob3_pipe_menu_item_add(p, "Three", NULL);
ob3_pipe_menu_item_add(p, "Four", NULL);
}
ob3_pipe_menu_separator_add(n);
if((p = ob3_pipe_menu_item_add(n, "Reconfigure", NULL)))
ob3_pipe_menu_action_add(p, "Reconfigure", NULL);
}
if((n = ob3_pipe_menu_menu_add(NULL, "Exit", "menu-exit"))) {
xmlNodePtr p;
if((p = ob3_pipe_menu_item_add(n, "Restart", NULL)))
ob3_pipe_menu_action_add(p, "Restart", NULL);
if((p = ob3_pipe_menu_item_add(n, "Exit", NULL)))
ob3_pipe_menu_action_add(p, "Exit", NULL);
}
ob3_pipe_menu_shutdown();
exit(0);
}
More information about the openbox
mailing list