New API lxpanel_apply_hotkey() as a convenient interface to libkeybinder.
[lxde/lxpanel.git] / plugins / menu.c
1 /**
2 * Copyright (c) 2006-2014 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include <menu-cache.h>
31 #include <libfm/fm-gtk.h>
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37
38 #include "misc.h"
39 #include "plugin.h"
40 #include "menu-policy.h"
41
42 #include "dbg.h"
43 #include "gtk-compat.h"
44
45 /* support for libmenu-cache 0.4.x */
46 #ifndef MENU_CACHE_CHECK_VERSION
47 # ifdef HAVE_MENU_CACHE_DIR_LIST_CHILDREN
48 # define MENU_CACHE_CHECK_VERSION(_a,_b,_c) (_a == 0 && _b < 5) /* < 0.5.0 */
49 # else
50 # define MENU_CACHE_CHECK_VERSION(_a,_b,_c) 0 /* not even 0.4.0 */
51 # endif
52 #endif
53
54 #define DEFAULT_MENU_ICON PACKAGE_DATA_DIR "/images/my-computer.png"
55 /*
56 * SuxPanel version 0.1
57 * Copyright (c) 2003 Leandro Pereira <leandro@linuxmag.com.br>
58 */
59
60 /*
61 * menu style code was taken from suxpanel
62 */
63
64 typedef struct {
65 GtkWidget *menu, *box, *img, *label;
66 char *fname, *caption;
67 int iconsize;
68 gboolean has_system_menu;
69 guint show_system_menu_idle;
70 LXPanel *panel;
71 config_setting_t *settings;
72
73 MenuCache* menu_cache;
74 guint visibility_flags;
75 gpointer reload_notify;
76 FmDndSrc *ds;
77 } menup;
78
79 static guint idle_loader = 0;
80
81 GQuark SYS_MENU_ITEM_ID = 0;
82
83 /* FIXME: those are defined on panel main code */
84 void restart(void);
85 void gtk_run(void);
86 void logout(void);
87
88 static void on_data_get(FmDndSrc *ds, GtkWidget *mi)
89 {
90 FmFileInfo *fi = g_object_get_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID);
91
92 /* g_debug("on_data_get(...)"); */
93 fm_dnd_src_set_file(ds, fi);
94 }
95
96 static void
97 menu_destructor(gpointer user_data)
98 {
99 menup *m = (menup *)user_data;
100
101 if( G_UNLIKELY( idle_loader ) )
102 {
103 g_source_remove( idle_loader );
104 idle_loader = 0;
105 }
106
107 if (m->show_system_menu_idle)
108 g_source_remove(m->show_system_menu_idle);
109
110 g_signal_handlers_disconnect_matched(m->ds, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
111 on_data_get, NULL);
112 g_object_unref(G_OBJECT(m->ds));
113 gtk_widget_destroy(m->menu);
114
115 if( m->menu_cache )
116 {
117 menu_cache_remove_reload_notify(m->menu_cache, m->reload_notify);
118 menu_cache_unref( m->menu_cache );
119 }
120
121 g_free(m->fname);
122 g_free(m->caption);
123 g_free(m);
124 RET();
125 }
126
127 static void
128 spawn_app(GtkWidget *widget, gpointer data)
129 {
130 ENTER;
131 if (data) {
132 fm_launch_command_simple(NULL, NULL, 0, data, NULL);
133 }
134 RET();
135 }
136
137
138 static void
139 run_command(GtkWidget *widget, void (*cmd)(void))
140 {
141 ENTER;
142 cmd();
143 RET();
144 }
145
146 static void
147 menu_pos(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, GtkWidget *widget)
148 {
149 int ox, oy, w, h;
150 menup *m;
151 GtkAllocation allocation;
152
153 gtk_widget_get_allocation(GTK_WIDGET(widget), &allocation);
154 ENTER;
155 m = lxpanel_plugin_get_data(widget);
156 gdk_window_get_origin(gtk_widget_get_window(widget), &ox, &oy);
157 #if GTK_CHECK_VERSION(2,20,0)
158 GtkRequisition requisition;
159 gtk_widget_get_requisition(GTK_WIDGET(menu), &requisition);
160 w = requisition.width;
161 h = requisition.height;
162
163 #else
164 w = GTK_WIDGET(menu)->requisition.width;
165 h = GTK_WIDGET(menu)->requisition.height;
166 #endif
167 if (panel_get_orientation(m->panel) == GTK_ORIENTATION_HORIZONTAL) {
168 *x = ox;
169 if (*x + w > gdk_screen_width())
170 *x = ox + allocation.width - w;
171 *y = oy - h;
172 if (*y < 0)
173 *y = oy + allocation.height;
174 } else {
175 *x = ox + allocation.width;
176 if (*x > gdk_screen_width())
177 *x = ox - w;
178 *y = oy;
179 if (*y + h > gdk_screen_height())
180 *y = oy + allocation.height - h;
181 }
182 DBG("widget: x,y=%d,%d w,h=%d,%d\n", ox, oy,
183 allocation.width, allocation.height );
184 DBG("w-h %d %d\n", w, h);
185 *push_in = TRUE;
186 RET();
187 }
188
189 static void on_menu_item( GtkMenuItem* mi, menup* m )
190 {
191 FmFileInfo *fi = g_object_get_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID);
192
193 lxpanel_launch_path(m->panel, fm_file_info_get_path(fi));
194 }
195
196 /* load icon when mapping the menu item to speed up */
197 static void on_menu_item_map(GtkWidget *mi, menup *m)
198 {
199 GtkImage* img = GTK_IMAGE(gtk_image_menu_item_get_image(GTK_IMAGE_MENU_ITEM(mi)));
200 if( img )
201 {
202 FmFileInfo *fi;
203 if (gtk_image_get_storage_type(img) == GTK_IMAGE_EMPTY &&
204 (fi = g_object_get_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID)) != NULL &&
205 fi != (gpointer)1 /* ignore placeholder or separator */)
206 {
207 FmIcon *fm_icon = fm_file_info_get_icon(fi);
208 FmIcon *_fm_icon = NULL;
209 GdkPixbuf *icon = NULL;
210
211 if (fm_icon == NULL)
212 fm_icon = _fm_icon = fm_icon_from_name("application-x-executable");
213 icon = fm_pixbuf_from_icon_with_fallback(fm_icon, m->iconsize,
214 "application-x-executable");
215 if (_fm_icon)
216 g_object_unref(_fm_icon);
217 if (icon)
218 {
219 gtk_image_set_from_pixbuf(img, icon);
220 g_object_unref(icon);
221 }
222 }
223 }
224 }
225
226 static void on_menu_item_style_set(GtkWidget* mi, GtkStyle* prev, menup *m)
227 {
228 /* reload icon */
229 on_menu_item_map(mi, m);
230 }
231
232 /* FIXME: this is very dirty, especially if desktop is set not to ~/Desktop
233 therefore it should be removed and drag & drop gestures used instead */
234 static void on_add_menu_item_to_desktop(GtkMenuItem* item, GtkWidget* mi)
235 {
236 FmFileInfo *fi = g_object_get_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID);
237 FmPathList *files = fm_path_list_new();
238
239 fm_path_list_push_tail(files, fm_file_info_get_path(fi));
240 fm_link_files(NULL, files, fm_path_get_desktop());
241 fm_path_list_unref(files);
242 }
243
244 /* TODO: add menu item to panel */
245 #if 0
246 static void on_add_menu_item_to_panel(GtkMenuItem* item, MenuCacheApp* app)
247 {
248 /* Find a penel containing launchbar applet.
249 * The launchbar with most buttons will be choosen if
250 * there are several launchbar applets loaded.
251 */
252 GSList* l;
253 Plugin* lb = NULL;
254 int n_btns = -1;
255
256 for(l = all_panels; !lb && l; l = l->next)
257 {
258 Panel* panel = (Panel*)l->data;
259 GList* pl;
260 for(pl=panel->plugins; pl; pl = pl->next)
261 {
262 Plugin* plugin = (Plugin*)pl;
263 if( strcmp(plugin->class->type, "launchbar") == 0 )
264 {
265 /* FIXME: should we let the users choose which launcherbar to add the btn? */
266 break;
267 #if 0
268 int n = launchbar_get_n_btns(plugin);
269 if( n > n_btns )
270 {
271 lb = plugin;
272 n_btns = n;
273 }
274 #endif
275 }
276 }
277 }
278
279 if( ! lb ) /* launchbar is not currently in use */
280 {
281 /* FIXME: add a launchbar plugin to the panel which has a menu, too. */
282 }
283
284 if( lb )
285 {
286
287 }
288 }
289 #endif
290
291 static void on_menu_item_properties(GtkMenuItem* item, GtkWidget* mi)
292 {
293 FmFileInfo *fi = g_object_get_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID);
294 FmFileInfoList *files = fm_file_info_list_new();
295
296 fm_file_info_list_push_tail(files, fi);
297 fm_show_file_properties(NULL, files);
298 fm_file_info_list_unref(files);
299 }
300
301 #if 0
302 /* This following function restore_grabs is taken from menu.c of
303 * gnome-panel.
304 */
305 /*most of this function stolen from the real gtk_menu_popup*/
306 static void restore_grabs(GtkWidget *w, gpointer data)
307 {
308 GtkWidget *menu_item = data;
309 GtkMenu *menu = GTK_MENU(gtk_widget_get_parent(menu_item));
310 GtkWidget *xgrab_shell;
311 GtkWidget *parent;
312
313 /* Find the last viewable ancestor, and make an X grab on it
314 */
315 parent = GTK_WIDGET (menu);
316 xgrab_shell = NULL;
317 while (parent)
318 {
319 gboolean viewable = TRUE;
320 GtkWidget *tmp = parent;
321
322 while (tmp)
323 {
324 if (!gtk_widget_get_mapped(tmp))
325 {
326 viewable = FALSE;
327 break;
328 }
329 tmp = gtk_widget_get_parent(tmp);
330 }
331
332 if (viewable)
333 xgrab_shell = parent;
334
335 parent = gtk_widget_get_parent(parent);
336 }
337
338 /*only grab if this HAD a grab before*/
339 if (xgrab_shell && (gtk_widget_has_focus(xgrab_shell)))
340 {
341 if (gdk_pointer_grab (gtk_widget_get_window(xgrab_shell), TRUE,
342 GDK_BUTTON_PRESS_MASK |
343 GDK_BUTTON_RELEASE_MASK |
344 GDK_ENTER_NOTIFY_MASK |
345 GDK_LEAVE_NOTIFY_MASK,
346 NULL, NULL, 0) == 0)
347 {
348 if (gdk_keyboard_grab (gtk_widget_get_window(xgrab_shell), TRUE,
349 GDK_CURRENT_TIME) == 0)
350 gtk_widget_grab_focus (xgrab_shell);
351 else
352 gdk_pointer_ungrab (GDK_CURRENT_TIME);
353 }
354 }
355 gtk_grab_add (GTK_WIDGET (menu));
356 }
357 #endif
358
359 static void restore_submenu(GtkMenuItem *mi, GtkWidget *submenu)
360 {
361 g_signal_handlers_disconnect_by_func(mi, restore_submenu, submenu);
362 gtk_menu_item_set_submenu(mi, submenu);
363 g_object_set_data(G_OBJECT(mi), "PanelMenuItemSubmenu", NULL);
364 }
365
366 static gboolean on_menu_button_press(GtkWidget* mi, GdkEventButton* evt, menup* m)
367 {
368 if( evt->button == 3 ) /* right */
369 {
370 GtkWidget* item;
371 GtkMenu* p;
372
373 /* don't make duplicates */
374 if (g_signal_handler_find(mi, G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
375 restore_submenu, NULL))
376 {
377 return FALSE;
378 }
379
380 p = GTK_MENU(gtk_menu_new());
381
382 item = gtk_menu_item_new_with_label(_("Add to desktop"));
383 g_signal_connect(item, "activate", G_CALLBACK(on_add_menu_item_to_desktop), mi);
384 gtk_menu_shell_append(GTK_MENU_SHELL(p), item);
385
386 item = gtk_separator_menu_item_new();
387 gtk_menu_shell_append(GTK_MENU_SHELL(p), item);
388
389 item = gtk_menu_item_new_with_label(_("Properties"));
390 g_signal_connect(item, "activate", G_CALLBACK(on_menu_item_properties), mi);
391 gtk_menu_shell_append(GTK_MENU_SHELL(p), item);
392
393 item = gtk_menu_item_get_submenu(GTK_MENU_ITEM(mi)); /* reuse it */
394 if (item)
395 {
396 /* set object data to keep reference on the submenu we preserve */
397 g_object_set_data_full(G_OBJECT(mi), "PanelMenuItemSubmenu",
398 g_object_ref(item), g_object_unref);
399 gtk_menu_popdown(GTK_MENU(item));
400 }
401 gtk_menu_item_set_submenu(GTK_MENU_ITEM(mi), GTK_WIDGET(p));
402 g_signal_connect(mi, "deselect", G_CALLBACK(restore_submenu), item);
403 gtk_widget_show_all(GTK_WIDGET(p));
404 }
405 else if (evt->button == 1) /* allow drag on clicked item */
406 {
407 /* disconnect previous menu item */
408 g_signal_handlers_disconnect_matched(m->ds, G_SIGNAL_MATCH_FUNC, 0, 0,
409 NULL, on_data_get, NULL);
410 /* remap FmDndSrc onto current item */
411 fm_dnd_src_set_widget(m->ds, mi);
412 g_signal_connect(m->ds, "data-get", G_CALLBACK(on_data_get), mi);
413 }
414 return FALSE;
415 }
416
417 static GtkWidget* create_item(MenuCacheItem *item, menup *m)
418 {
419 GtkWidget* mi;
420 if( menu_cache_item_get_type(item) == MENU_CACHE_TYPE_SEP )
421 {
422 mi = gtk_separator_menu_item_new();
423 g_object_set_qdata(G_OBJECT(mi), SYS_MENU_ITEM_ID, (gpointer)1);
424 }
425 else
426 {
427 GtkWidget* img;
428 /* create FmFileInfo for the item, it will be used in callbacks */
429 char *mpath = menu_cache_dir_make_path(MENU_CACHE_DIR(item));
430 FmPath *path = fm_path_new_relative(fm_path_get_apps_menu(), mpath+13);
431 /* skip "/Applications" */
432 FmFileInfo *fi = fm_file_info_new_from_menu_cache_item(path, item);
433
434 g_free(mpath);
435 fm_path_unref(path);
436 mi = gtk_image_menu_item_new_with_mnemonic( menu_cache_item_get_name(item) );
437 g_object_set_qdata_full(G_OBJECT(mi), SYS_MENU_ITEM_ID, fi,
438 (GDestroyNotify)fm_file_info_unref);
439 img = gtk_image_new();
440 gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(mi), img );
441 if( menu_cache_item_get_type(item) == MENU_CACHE_TYPE_APP )
442 {
443 const char *comment = menu_cache_item_get_comment(item);
444 if (comment != NULL)
445 gtk_widget_set_tooltip_text(mi, comment);
446 g_signal_connect(mi, "activate", G_CALLBACK(on_menu_item), m);
447 }
448 g_signal_connect(mi, "map", G_CALLBACK(on_menu_item_map), m);
449 g_signal_connect(mi, "style-set", G_CALLBACK(on_menu_item_style_set), m);
450 g_signal_connect(mi, "button-press-event", G_CALLBACK(on_menu_button_press), m);
451 /* allow drag and add empty set for now to allow dragging the item
452 the rest will be done by FmDndSrc after drag begins */
453 gtk_drag_source_set(mi, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
454 }
455 gtk_widget_show( mi );
456 return mi;
457 }
458
459 static int load_menu(menup* m, MenuCacheDir* dir, GtkWidget* menu, int pos )
460 {
461 GSList * l;
462 /* number of visible entries */
463 gint count = 0;
464 #if MENU_CACHE_CHECK_VERSION(0, 4, 0)
465 GSList *children;
466 # if MENU_CACHE_CHECK_VERSION(0, 5, 0)
467 # if !MENU_CACHE_CHECK_VERSION(1, 0, 0)
468 char *kfpath;
469 GKeyFile *kf;
470 # endif
471 if (!menu_cache_dir_is_visible(dir)) /* directory is hidden, ignore children */
472 return 0;
473 # if !MENU_CACHE_CHECK_VERSION(1, 0, 0)
474 /* version 1.0.0 has NoDisplay checked internally */
475 kfpath = menu_cache_item_get_file_path(MENU_CACHE_ITEM(dir));
476 kf = g_key_file_new();
477 /* for version 0.5.0 we enable hidden so should test NoDisplay flag */
478 if (kfpath && g_key_file_load_from_file(kf, kfpath, 0, NULL) &&
479 g_key_file_get_boolean(kf, "Desktop Entry", "NoDisplay", NULL))
480 count = -1;
481 g_free(kfpath);
482 g_key_file_free(kf);
483 if (count < 0) /* directory is hidden, ignore children */
484 return 0;
485 # endif /* < 1.0.0 */
486 # endif /* < 0.5.0 */
487 children = menu_cache_dir_list_children(dir);
488 for (l = children; l; l = l->next)
489 #else /* < 0.4.0 */
490 for( l = menu_cache_dir_get_children(dir); l; l = l->next )
491 #endif
492 {
493 MenuCacheItem* item = MENU_CACHE_ITEM(l->data);
494
495 gboolean is_visible = ((menu_cache_item_get_type(item) != MENU_CACHE_TYPE_APP) ||
496 (panel_menu_item_evaluate_visibility(item, m->visibility_flags)));
497
498 if (is_visible)
499 {
500 GtkWidget * mi = create_item(item, m);
501 count++;
502 if (mi != NULL)
503 gtk_menu_shell_insert( (GtkMenuShell*)menu, mi, pos );
504 if( pos >= 0 )
505 ++pos;
506 /* process subentries */
507 if (menu_cache_item_get_type(item) == MENU_CACHE_TYPE_DIR)
508 {
509 GtkWidget* sub = gtk_menu_new();
510 /* always pass -1 for position */
511 gint s_count = load_menu( m, MENU_CACHE_DIR(item), sub, -1 );
512 if (s_count)
513 gtk_menu_item_set_submenu( GTK_MENU_ITEM(mi), sub );
514 else
515 {
516 /* don't keep empty submenus */
517 gtk_widget_destroy( sub );
518 gtk_widget_destroy( mi );
519 if (pos > 0)
520 pos--;
521 }
522 }
523 }
524 }
525 #if MENU_CACHE_CHECK_VERSION(0, 4, 0)
526 g_slist_foreach(children, (GFunc)menu_cache_item_unref, NULL);
527 g_slist_free(children);
528 #endif
529 return count;
530 }
531
532
533
534 static gboolean sys_menu_item_has_data( GtkMenuItem* item )
535 {
536 return (g_object_get_qdata( G_OBJECT(item), SYS_MENU_ITEM_ID ) != NULL);
537 }
538
539 static void _unload_old_icons(GtkMenu* menu, GtkIconTheme* theme, menup* m)
540 {
541 GList *children, *child;
542 GtkMenuItem* item;
543 GtkWidget* sub_menu=NULL;
544
545 children = gtk_container_get_children( GTK_CONTAINER(menu) );
546 for( child = children; child; child = child->next )
547 {
548 item = GTK_MENU_ITEM( child->data );
549 if( sys_menu_item_has_data( item ) )
550 {
551 GtkImage* img;
552 item = GTK_MENU_ITEM( child->data );
553 if( GTK_IS_IMAGE_MENU_ITEM(item) )
554 {
555 img = GTK_IMAGE(gtk_image_menu_item_get_image(GTK_IMAGE_MENU_ITEM(item)));
556 gtk_image_clear(img);
557 if (gtk_widget_get_mapped(GTK_WIDGET(img)))
558 on_menu_item_map(GTK_WIDGET(item), m);
559 }
560 }
561 else if( ( sub_menu = gtk_menu_item_get_submenu( item ) ) )
562 {
563 _unload_old_icons(GTK_MENU(sub_menu), theme, m);
564 }
565 }
566 g_list_free( children );
567 }
568
569 static void unload_old_icons(GtkIconTheme* theme, menup* m)
570 {
571 _unload_old_icons(GTK_MENU(m->menu), theme, m);
572 }
573
574 static void remove_change_handler(gpointer id, GObject* menu)
575 {
576 g_signal_handler_disconnect(gtk_icon_theme_get_default(), GPOINTER_TO_INT(id));
577 }
578
579 /*
580 * Insert application menus into specified menu
581 * menu: The parent menu to which the items should be inserted
582 * pisition: Position to insert items.
583 Passing -1 in this parameter means append all items
584 at the end of menu.
585 */
586 static void sys_menu_insert_items( menup* m, GtkMenu* menu, int position )
587 {
588 MenuCacheDir* dir;
589 guint change_handler;
590
591 if( G_UNLIKELY( SYS_MENU_ITEM_ID == 0 ) )
592 SYS_MENU_ITEM_ID = g_quark_from_static_string( "SysMenuItem" );
593
594 #if MENU_CACHE_CHECK_VERSION(0, 4, 0)
595 dir = menu_cache_dup_root_dir(m->menu_cache);
596 #else
597 dir = menu_cache_get_root_dir( m->menu_cache );
598 #endif
599 if(dir)
600 {
601 load_menu( m, dir, GTK_WIDGET(menu), position );
602 #if MENU_CACHE_CHECK_VERSION(0, 4, 0)
603 menu_cache_item_unref(MENU_CACHE_ITEM(dir));
604 #endif
605 }
606 else /* menu content is empty */
607 {
608 /* add a place holder */
609 GtkWidget* mi = gtk_menu_item_new();
610 g_object_set_qdata( G_OBJECT(mi), SYS_MENU_ITEM_ID, GINT_TO_POINTER(1) );
611 gtk_menu_shell_insert(GTK_MENU_SHELL(menu), mi, position);
612 }
613
614 change_handler = g_signal_connect(gtk_icon_theme_get_default(), "changed", G_CALLBACK(unload_old_icons), m);
615 g_object_weak_ref( G_OBJECT(menu), remove_change_handler, GINT_TO_POINTER(change_handler) );
616 }
617
618
619 static void
620 reload_system_menu( menup* m, GtkMenu* menu )
621 {
622 GList *children, *child;
623 GtkMenuItem* item;
624 GtkWidget* sub_menu;
625 gint idx;
626
627 children = gtk_container_get_children( GTK_CONTAINER(menu) );
628 for( child = children, idx = 0; child; child = child->next, ++idx )
629 {
630 item = GTK_MENU_ITEM( child->data );
631 if( sys_menu_item_has_data( item ) )
632 {
633 do
634 {
635 item = GTK_MENU_ITEM( child->data );
636 child = child->next;
637 gtk_widget_destroy( GTK_WIDGET(item) );
638 }while( child && sys_menu_item_has_data( child->data ) );
639 sys_menu_insert_items( m, menu, idx );
640 if( ! child )
641 break;
642 }
643 else if( ( sub_menu = gtk_menu_item_get_submenu( item ) ) )
644 {
645 reload_system_menu( m, GTK_MENU(sub_menu) );
646 }
647 }
648 g_list_free( children );
649 }
650
651 static void show_menu( GtkWidget* widget, menup* m, int btn, guint32 time )
652 {
653 gtk_menu_popup(GTK_MENU(m->menu),
654 NULL, NULL,
655 (GtkMenuPositionFunc)menu_pos, widget,
656 btn, time);
657 }
658
659 static gboolean
660 menu_button_press_event(GtkWidget * widget, GdkEventButton * event, LXPanel * panel)
661 {
662 ENTER;
663
664 if (event->button == 1)
665 {
666 show_menu( widget, lxpanel_plugin_get_data(widget), event->button, event->time );
667 RET(TRUE);
668 }
669 RET(FALSE);
670 }
671
672 static gboolean show_system_menu_idle(gpointer user_data)
673 {
674 menup* m = (menup*)user_data;
675 if (g_source_is_destroyed(g_main_current_source()))
676 return FALSE;
677 show_menu( m->box, m, 0, GDK_CURRENT_TIME );
678 m->show_system_menu_idle = 0;
679 return FALSE;
680 }
681
682 static void show_system_menu(GtkWidget *p)
683 {
684 menup *m = lxpanel_plugin_get_data(p);
685
686 if (m->has_system_menu && m->show_system_menu_idle == 0)
687 /* FIXME: I've no idea why this doesn't work without timeout
688 under some WMs, like icewm. */
689 m->show_system_menu_idle = g_timeout_add(200, show_system_menu_idle, m);
690 }
691
692 static GtkWidget *
693 make_button(menup *m, const gchar *fname, const gchar *name, GdkColor* tint, GtkWidget *menu)
694 {
695 char* title = NULL;
696
697 ENTER;
698 m->menu = menu;
699
700 if( name )
701 {
702 /* load the name from *.directory file if needed */
703 if( g_str_has_suffix( name, ".directory" ) )
704 {
705 GKeyFile* kf = g_key_file_new();
706 char* dir_file = g_build_filename( "desktop-directories", name, NULL );
707 if( g_key_file_load_from_data_dirs( kf, dir_file, NULL, 0, NULL ) )
708 {
709 title = g_key_file_get_locale_string( kf, "Desktop Entry", "Name", NULL, NULL );
710 }
711 g_free( dir_file );
712 g_key_file_free( kf );
713 }
714
715 m->img = lxpanel_button_new_for_icon(m->panel, fname, tint, title ? title : name);
716
717 g_free( title );
718 }
719 else
720 {
721 m->img = lxpanel_button_new_for_icon(m->panel, fname, tint, NULL);
722 }
723
724 gtk_widget_show(m->img);
725 gtk_container_add(GTK_CONTAINER(m->box), m->img);
726
727 m->ds = fm_dnd_src_new(NULL);
728
729 RET(m->img);
730 }
731
732 /* those were in configurator.c initially but it's safer to have those here */
733 typedef struct {
734 char *name;
735 char *disp_name;
736 void (*cmd)(void);
737 } Command;
738
739 static Command commands[] = {
740 //{ "configure", N_("Preferences"), configure },
741 { "run", N_("Run"), gtk_run },
742 { "restart", N_("Restart"), restart },
743 { "logout", N_("Logout"), logout },
744 { NULL, NULL },
745 };
746
747 static GtkWidget *
748 read_item(menup *m, config_setting_t *s)
749 {
750 const gchar *name, *fname, *action, *str;
751 GtkWidget *item;
752 Command *cmd_entry = NULL;
753 char *tmp;
754
755 ENTER;
756 name = fname = action = NULL;
757
758 config_setting_lookup_string(s, "name", &name);
759 config_setting_lookup_string(s, "image", &fname);
760 config_setting_lookup_string(s, "action", &action);
761 if (config_setting_lookup_string(s, "command", &str))
762 {
763 Command *tmp;
764
765 for (tmp = commands; tmp->name; tmp++) {
766 if (!g_ascii_strcasecmp(str, tmp->name)) {
767 cmd_entry = tmp;
768 break;
769 }
770 }
771 }
772 /* menu button */
773 if( cmd_entry ) /* built-in commands */
774 {
775 item = gtk_image_menu_item_new_with_label( _(cmd_entry->disp_name) );
776 g_signal_connect(G_OBJECT(item), "activate", (GCallback)run_command, cmd_entry->cmd);
777 }
778 else if (action)
779 {
780 item = gtk_image_menu_item_new_with_label(name ? name : "");
781 tmp = g_strdup(action);
782 g_object_weak_ref(G_OBJECT(item), (GWeakNotify)g_free, tmp);
783 g_signal_connect(G_OBJECT(item), "activate", (GCallback)spawn_app, tmp);
784 }
785 else
786 goto error;
787 gtk_container_set_border_width(GTK_CONTAINER(item), 0);
788 if (fname) {
789 GtkWidget *img;
790
791 tmp = expand_tilda(fname);
792 img = lxpanel_image_new_for_icon(m->panel, tmp, m->iconsize, NULL);
793 gtk_widget_show(img);
794 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), img);
795 g_free(tmp);
796 }
797 RET(item);
798
799 error:
800 RET(NULL);
801 }
802
803 static GtkWidget *
804 read_separator(menup *m, config_setting_t *s)
805 {
806 ENTER;
807 RET(gtk_separator_menu_item_new());
808 }
809
810 static void on_reload_menu(MenuCache* cache, gpointer menu_pointer)
811 {
812 menup *m = menu_pointer;
813 /* g_debug("reload system menu!!"); */
814 reload_system_menu( m, GTK_MENU(m->menu) );
815 }
816
817 static void
818 read_system_menu(GtkMenu *menu, menup *m, config_setting_t *s)
819 {
820 if (m->menu_cache == NULL)
821 {
822 guint32 flags;
823 m->menu_cache = panel_menu_cache_new(&flags);
824 if (m->menu_cache == NULL)
825 {
826 g_warning("error loading applications menu");
827 return;
828 }
829 m->visibility_flags = flags;
830 m->reload_notify = menu_cache_add_reload_notify(m->menu_cache, on_reload_menu, m);
831 sys_menu_insert_items( m, menu, -1 );
832 }
833
834 m->has_system_menu = TRUE;
835 }
836
837 #if 0
838 static void
839 read_include(Plugin *p, char **fp)
840 {
841 ENTER;
842 gchar *name;
843 line s;
844 menup *m = (menup *)p->priv;
845 /* FIXME: this is disabled */
846 ENTER;
847 s.len = 256;
848 name = NULL;
849 if( fp )
850 {
851 while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END) {
852 if (s.type == LINE_VAR) {
853 if (!g_ascii_strcasecmp(s.t[0], "name"))
854 name = expand_tilda(s.t[1]);
855 else {
856 ERR( "menu/include: unknown var %s\n", s.t[0]);
857 RET();
858 }
859 }
860 }
861 }
862 if ((fp = fopen(name, "r"))) {
863 LOG(LOG_INFO, "Including %s\n", name);
864 m->files = g_slist_prepend(m->files, fp);
865 p->fp = fp;
866 } else {
867 ERR("Can't include %s\n", name);
868 }
869 if (name) g_free(name);
870 RET();
871 }
872 #endif
873
874 static GtkWidget *
875 read_submenu(menup *m, config_setting_t *s, gboolean as_item)
876 {
877 GtkWidget *mi, *menu;
878 const gchar *name, *fname, *str;
879 config_setting_t *list = config_setting_add(s, "", PANEL_CONF_TYPE_LIST);
880 GdkColor color={0, 0, 36 * 0xffff / 0xff, 96 * 0xffff / 0xff};
881 guint i;
882
883 ENTER;
884
885 menu = gtk_menu_new ();
886 gtk_container_set_border_width(GTK_CONTAINER(menu), 0);
887
888 fname = NULL;
889 name = NULL;
890 config_setting_lookup_string(s, "name", &name);
891 config_setting_lookup_string(s, "image", &fname);
892 if (config_setting_lookup_string(s, "tintcolor", &str))
893 gdk_color_parse(str, &color);
894
895 for (i = 0; (s = config_setting_get_elem(list, i)) != NULL; i++)
896 {
897 str = config_setting_get_name(s);
898 if (!g_ascii_strcasecmp(str, "item")) {
899 mi = read_item(m, s);
900 } else if (!g_ascii_strcasecmp(str, "separator")) {
901 mi = read_separator(m, s);
902 } else if (!g_ascii_strcasecmp(str, "system")) {
903 read_system_menu(GTK_MENU(menu), m, s); /* add system menu items */
904 continue;
905 } else if (!g_ascii_strcasecmp(str, "menu")) {
906 mi = read_submenu(m, s, TRUE);
907 #if 0
908 } else if (!g_ascii_strcasecmp(str, "include")) {
909 read_include(p, fp);
910 continue;
911 #endif
912 } else {
913 g_warning("menu: unknown block %s", str);
914 goto error;
915 }
916 if (!mi) {
917 g_warning("menu: can't create menu item");
918 goto error;
919 }
920 gtk_widget_show(mi);
921 gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
922 }
923 if (as_item) {
924 mi = gtk_image_menu_item_new_with_label(name);
925 if (fname) {
926 GtkWidget *img;
927 char *expanded = expand_tilda(fname);
928 img = lxpanel_image_new_for_icon(m->panel, expanded, m->iconsize, NULL);
929 gtk_widget_show(img);
930 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi), img);
931 g_free(expanded);
932 }
933 gtk_menu_item_set_submenu (GTK_MENU_ITEM (mi), menu);
934 } else {
935 m->fname = fname ? expand_tilda(fname) : g_strdup(DEFAULT_MENU_ICON);
936 m->caption = g_strdup(name);
937 mi = make_button(m, m->fname, name, &color, menu);
938 }
939
940 RET(mi);
941
942 error:
943 // FIXME: we need to recursivly destroy all child menus and their items
944 gtk_widget_destroy(menu);
945 RET(NULL);
946 }
947
948 static GtkWidget *
949 menu_constructor(LXPanel *panel, config_setting_t *settings)
950 {
951 menup *m;
952 config_setting_t *s;
953 int iw, ih;
954
955 m = g_new0(menup, 1);
956 g_return_val_if_fail(m != NULL, 0);
957
958 gtk_icon_size_lookup( GTK_ICON_SIZE_MENU, &iw, &ih );
959 m->iconsize = MAX(iw, ih);
960
961 m->box = gtk_event_box_new();
962 gtk_widget_set_has_window(m->box, FALSE);
963 lxpanel_plugin_set_data(m->box, m, menu_destructor);
964 gtk_container_set_border_width(GTK_CONTAINER(m->box), 0);
965
966 /* Save construction pointers */
967 m->panel = panel;
968 m->settings = settings;
969
970 /* Check if configuration exists */
971 settings = config_setting_add(settings, "", PANEL_CONF_TYPE_LIST);
972 if (config_setting_get_elem(settings, 0) == NULL)
973 {
974 /* create default menu */
975 config_setting_add(settings, "system", PANEL_CONF_TYPE_GROUP);
976 config_setting_add(settings, "separator", PANEL_CONF_TYPE_GROUP);
977 s = config_setting_add(settings, "item", PANEL_CONF_TYPE_GROUP);
978 config_group_set_string(s, "command", "run");
979 config_setting_add(settings, "separator", PANEL_CONF_TYPE_GROUP);
980 s = config_setting_add(settings, "item", PANEL_CONF_TYPE_GROUP);
981 config_group_set_string(s, "command", "logout");
982 config_group_set_string(s, "image", "gnome-logout");
983 config_group_set_string(m->settings, "image", DEFAULT_MENU_ICON);
984 }
985
986 if (!read_submenu(m, m->settings, FALSE)) {
987 g_warning("menu: plugin init failed");
988 gtk_widget_destroy(m->box);
989 return NULL;
990 }
991
992 /* FIXME: allow bind a global key to toggle menu using libkeybinder */
993 return m->box;
994 }
995
996 static gboolean apply_config(gpointer user_data)
997 {
998 GtkWidget *p = user_data;
999 menup* m = lxpanel_plugin_get_data(p);
1000
1001 if( m->fname ) {
1002 lxpanel_button_set_icon(m->img, m->fname, -1);
1003 }
1004 config_group_set_string(m->settings, "image", m->fname);
1005 /* config_group_set_int(m->settings, "panelSize", m->match_panel); */
1006 config_group_set_string(m->settings, "name", m->caption);
1007 return FALSE;
1008 }
1009
1010 static GtkWidget *menu_config(LXPanel *panel, GtkWidget *p)
1011 {
1012 menup* menu = lxpanel_plugin_get_data(p);
1013 return lxpanel_generic_config_dlg(_("Menu"), panel, apply_config, p,
1014 _("Icon"), &menu->fname, CONF_TYPE_FILE_ENTRY,
1015 /* _("Use panel size as icon size"), &menu->match_panel, CONF_TYPE_INT, */
1016 /* _("Caption"), &menu->caption, CONF_TYPE_STR, */
1017 NULL);
1018 }
1019
1020 /* Callback when panel configuration changes. */
1021 static void menu_panel_configuration_changed(LXPanel *panel, GtkWidget *p)
1022 {
1023 apply_config(p);
1024 }
1025
1026 LXPanelPluginInit lxpanel_static_plugin_menu = {
1027 .name = N_("Menu"),
1028 .description = N_("Application Menu"),
1029
1030 .new_instance = menu_constructor,
1031 .config = menu_config,
1032 .reconfigure = menu_panel_configuration_changed,
1033 .button_press_event = menu_button_press_event,
1034 .show_system_menu = show_system_menu
1035 };
1036
1037 /* vim: set sw=4 et sts=4 : */