4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* appmenu.c - handles application-specific menus read from XMLwrapper.xml */
32 #include <libxml/parser.h>
37 #include "gui_support.h"
47 /* Static prototypes */
48 static void apprun_menu(GtkWidget
*item
, gpointer data
);
49 static GtkWidget
*create_menu_item(xmlNode
*node
);
51 /* There can only be one menu open at a time... we store: */
52 static GtkWidget
*current_menu
= NULL
; /* The GtkMenu */
53 static guchar
*current_app_path
= NULL
; /* The path of the application */
54 static GList
*current_items
= NULL
; /* The GtkMenuItems we added directly
55 * to it --- not submenu items.
58 /****************************************************************
59 * EXTERNAL INTERFACE *
60 ****************************************************************/
62 /* Removes all appmenu menu items */
63 void appmenu_remove(void)
70 for (next
= current_items
; next
; next
= next
->next
)
71 gtk_widget_destroy((GtkWidget
*) next
->data
);
73 null_g_free(¤t_app_path
);
76 g_list_free(current_items
);
80 /* Add AppMenu entries to 'menu', if appropriate.
81 * This function modifies the menu stored in "menu".
82 * 'app_dir' is the pathname of the application directory, and 'item'
83 * is the corresponding DirItem.
84 * Call appmenu_remove() to undo the effect.
86 void appmenu_add(const gchar
*app_dir
, DirItem
*item
, GtkWidget
*menu
)
88 XMLwrapper
*ai
= NULL
;
93 g_return_if_fail(menu
!= NULL
);
95 /* Should have called appmenu_remove() already... */
96 g_return_if_fail(current_menu
== NULL
);
98 ai
= appinfo_get(app_dir
, item
);
100 return; /* No appmenu (or invalid XML) */
102 /* OK, we've got a valid info file and parsed it.
103 * Now build the GtkMenuItem widgets and add them to the menu
104 * (if there are any).
107 node
= xml_get_section(ai
, NULL
, "AppMenu");
111 g_return_if_fail(current_items
== NULL
);
113 /* Add the menu entries */
114 for (node
= node
->xmlChildrenNode
; node
; node
= node
->next
)
118 item
= create_menu_item(node
);
121 current_items
= g_list_prepend(current_items
, item
);
124 sep
= gtk_menu_item_new();
125 current_items
= g_list_prepend(current_items
, sep
);
126 gtk_widget_show(sep
);
128 for (next
= current_items
; next
; next
= next
->next
)
129 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu
),
130 GTK_WIDGET(next
->data
));
133 current_app_path
= g_strdup(app_dir
);
140 /****************************************************************
141 * INTERNAL FUNCTIONS *
142 ****************************************************************/
144 /* Create a new menu and return it */
145 static GtkWidget
*appmenu_add_submenu(xmlNode
*subm_node
)
150 /* Create the new submenu */
151 sub_menu
= gtk_menu_new();
153 /* Add the menu entries */
154 for (node
= subm_node
->xmlChildrenNode
; node
; node
= node
->next
)
158 item
= create_menu_item(node
);
160 gtk_menu_shell_append(GTK_MENU_SHELL(sub_menu
), item
);
166 /* Create and return a menu item */
167 static GtkWidget
*create_menu_item(xmlNode
*node
)
171 guchar
*label
, *option
= NULL
;
174 if (node
->type
!= XML_ELEMENT_NODE
)
177 if (strcmp(node
->name
, "Item") == 0)
180 option
= xmlGetProp(node
, "option");
182 else if (strcmp(node
->name
, "AppMenu") == 0)
187 /* Create the item */
188 label_node
= get_subnode(node
, NULL
, "Label");
191 label
= xmlNodeListGetString(label_node
->doc
,
192 label_node
->xmlChildrenNode
, 1);
196 label
= xmlGetProp(node
, "label");
198 label
= g_strdup(_("<missing label>"));
200 item
= gtk_menu_item_new_with_label(label
);
202 gtk_widget_set_accel_path(item
, NULL
, NULL
); /* XXX */
208 /* Add submenu items */
210 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
),
211 appmenu_add_submenu(node
));
215 /* Set up callback */
219 g_object_set_data_full(G_OBJECT(item
), "option",
225 g_signal_connect(item
, "activate", G_CALLBACK(apprun_menu
),
229 gtk_widget_show(item
);
234 /* Function called to execute an AppMenu item */
235 static void apprun_menu(GtkWidget
*item
, gpointer data
)
240 g_return_if_fail(current_app_path
!= NULL
);
242 option
= g_object_get_data(G_OBJECT(item
), "option");
244 argv
[0] = g_strconcat(current_app_path
, "/AppRun", NULL
);
245 argv
[1] = option
; /* (may be NULL) */
248 rox_spawn(NULL
, (const gchar
**) argv
);