r2525: Use stock icons in pinboard and panel menus.
[rox-filer.git] / ROX-Filer / src / icon.c
blob36f446393d957ddf3e53aa86dde193305d417908
1 /*
2 * $Id$
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)
10 * any later version.
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
15 * more details.
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 /* icon.c - abstract base class for pinboard and panel icons.
24 * An Icon contains the full pathname of its file and the DirItem for that
25 * file. Icons emit the following signals:
27 * redraw - the image, details or selection state have changed.
28 * update - the name or path has changed.
29 * destroy - someone wishes to remove the icon.
31 * Note that an icon may be removed without emitting 'destroy'.
34 #include "config.h"
36 #include <string.h>
37 #include <gtk/gtk.h>
38 #include <X11/keysym.h>
39 #include <gdk/gdkx.h>
41 #include "global.h"
43 #include "main.h"
44 #include "gui_support.h"
45 #include "support.h"
46 #include "icon.h"
47 #include "diritem.h"
48 #include "menu.h"
49 #include "appmenu.h"
50 #include "dnd.h"
51 #include "run.h"
52 #include "infobox.h"
53 #include "pixmaps.h"
54 #include "mount.h"
55 #include "type.h"
56 #include "usericons.h"
57 #include "pinboard.h" /* For pinboard_set_backdrop */
59 static gboolean have_primary = FALSE; /* We own the PRIMARY selection? */
61 GtkWidget *icon_menu; /* The popup icon menu */
62 static GtkWidget *icon_file_menu; /* The file submenu */
63 static GtkWidget *icon_file_item; /* 'File' label */
64 static GtkWidget *file_shift_item; /* 'Shift Open' label */
66 /* A list of selected Icons. Every icon in the list is from the same group
67 * (eg, you can't have icons from two different panels selected at the
68 * same time).
70 GList *icon_selection = NULL;
72 /* A list of Icons which have grabs in effect. The same combo may be
73 * listed more than once, but has only one X grab.
75 GList *icon_shortcuts = NULL;
77 #define CLICK_TO_SET _("(click to set)")
79 static unsigned int AltMask;
80 static unsigned int MetaMask;
81 static unsigned int NumLockMask;
82 static unsigned int ScrollLockMask;
83 static unsigned int CapsLockMask;
84 static unsigned int SuperMask;
85 static unsigned int HyperMask;
87 /* {MyKey -> Number of grabs} */
88 static GHashTable *grab_counter = NULL;
90 /* Each entry is a GList of Icons which have the given pathname.
91 * This allows us to update all necessary icons when something changes.
93 static GHashTable *icons_hash = NULL; /* path -> [Icon] */
95 static Icon *menu_icon = NULL; /* Item clicked if there is no selection */
97 /* Static prototypes */
98 static void rename_activate(GtkWidget *dialog);
99 static void menu_closed(GtkWidget *widget);
100 static void lose_selection(GtkClipboard *primary, gpointer data);
101 static void selection_get(GtkClipboard *primary,
102 GtkSelectionData *selection_data,
103 guint info,
104 gpointer data);
105 static void remove_items(gpointer data, guint action, GtkWidget *widget);
106 static void set_backdrop(gpointer data, guint action, GtkWidget *widget);
107 static void file_op(gpointer data, guint action, GtkWidget *widget);
108 static void show_rename_box(Icon *icon);
109 static void icon_set_selected_int(Icon *icon, gboolean selected);
110 static void icon_class_init(gpointer gclass, gpointer data);
111 static void icon_init(GTypeInstance *object, gpointer gclass);
112 static void icon_hash_path(Icon *icon);
113 static void icon_unhash_path(Icon *icon);
114 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n);
115 static void ungrab_key(Icon *icon);
116 static void grab_key(Icon *icon);
117 static void parseKeyString(MyKey *key, const char *str);
118 static void icon_wink(Icon *icon);
119 static void initModifiers(void);
121 enum {
122 ACTION_SHIFT,
123 ACTION_HELP,
124 ACTION_INFO,
125 ACTION_RUN_ACTION,
126 ACTION_SET_ICON,
127 ACTION_EDIT,
128 ACTION_LOCATION,
131 #undef N_
132 #define N_(x) x
133 static GtkItemFactoryEntry menu_def[] = {
134 {N_("ROX-Filer"), NULL, NULL, 0, "<Branch>"},
135 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
136 {">" N_("Show Help Files"), NULL, menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
137 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
138 {">", NULL, NULL, 0, "<Separator>"},
139 {">" N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
140 {">" N_("Home Directory"), NULL, open_home, 0, "<StockItem>", GTK_STOCK_HOME},
141 {N_("File"), NULL, NULL, 0, "<Branch>"},
142 {">" N_("Shift Open"), NULL, file_op, ACTION_SHIFT, NULL},
143 {">" N_("Help"), NULL, file_op, ACTION_HELP, "<StockItem>", GTK_STOCK_HELP},
144 {">" N_("Info"), NULL, file_op, ACTION_INFO, "<StockItem>", GTK_STOCK_DIALOG_INFO},
145 {">" N_("Set Run Action..."), NULL, file_op, ACTION_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
146 {">" N_("Set Icon..."), NULL, file_op, ACTION_SET_ICON, NULL},
147 {N_("Edit Item"), NULL, file_op, ACTION_EDIT, NULL},
148 {N_("Show Location"), NULL, file_op, ACTION_LOCATION, NULL},
149 {N_("Remove Item(s)"), NULL, remove_items, 0, NULL},
150 {"", NULL, NULL, 0, "<Separator>"},
151 {N_("Backdrop..."), NULL, set_backdrop, 0, NULL},
154 /****************************************************************
155 * EXTERNAL INTERFACE *
156 ****************************************************************/
158 /* Called when the pointer moves over the icon */
159 void icon_may_update(Icon *icon)
161 MaskedPixmap *image;
162 int flags;
164 g_return_if_fail(icon != NULL);
166 image = icon->item->image;
167 flags = icon->item->flags;
169 if (image)
170 g_object_ref(image);
171 mount_update(FALSE);
172 diritem_restat(icon->path, icon->item, NULL);
174 if (icon->item->image != image || icon->item->flags != flags)
176 /* Appearance changed; need to redraw */
177 g_signal_emit_by_name(icon, "update");
180 if (image)
181 g_object_unref(image);
184 /* If path is on an icon then it may have changed... check! */
185 void icons_may_update(const gchar *path)
187 GList *affected;
189 if (icons_hash)
191 affected = g_hash_table_lookup(icons_hash, path);
193 for (; affected; affected = affected->next)
194 icon_may_update((Icon *) affected->data);
198 typedef struct _CheckData CheckData;
199 struct _CheckData {
200 const gchar *path;
201 gboolean found;
204 static void check_has(gpointer key, GList *icons, CheckData *check)
206 Icon *icon;
208 g_return_if_fail(icons != NULL);
210 icon = icons->data;
212 if (is_sub_dir(icon->path, check->path))
213 check->found = TRUE;
216 /* Returns TRUE if any icon links to this file (or any file inside
217 * this directory). Used to check that it's OK to delete 'path'.
219 gboolean icons_require(const gchar *path)
221 CheckData check;
223 if (!icons_hash)
224 return FALSE;
226 check.path = path;
227 check.found = FALSE;
228 g_hash_table_foreach(icons_hash, (GHFunc) check_has, &check);
230 return check.found;
233 /* Menu was clicked over this icon. Set things up correctly (shade items,
234 * add app menu stuff, etc).
235 * You should show icon_menu after calling this...
237 void icon_prepare_menu(Icon *icon, gboolean pinboard)
239 appmenu_remove();
241 menu_icon = icon;
243 menu_set_hidden(icon_menu, !pinboard, 5, 2);
245 /* Shade Remove Item(s) unless there is a selection */
246 menu_set_items_shaded(icon_menu,
247 (icon_selection || menu_icon) ? FALSE : TRUE,
248 4, 1);
250 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
251 FALSE);
253 /* Shade the File/Edit/Show items unless an item was clicked */
254 if (icon)
256 guchar *tmp;
258 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
259 menu_set_items_shaded(icon_file_menu, FALSE, 0, 6);
260 if (!can_set_run_action(icon->item))
261 menu_set_items_shaded(icon_file_menu, TRUE, 3, 1);
263 tmp = g_strdup_printf("%s '%s'",
264 basetype_name(icon->item),
265 icon->item->leafname);
266 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
267 g_free(tmp);
269 /* Check for app-specific menu */
270 appmenu_add(icon->path, icon->item, icon_menu);
272 else
274 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
275 menu_set_items_shaded(icon_file_menu, TRUE, 0, 6);
276 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
280 /* Set whether this icon is selected. Will automatically clear the selection
281 * if it contains icons from a different group.
283 void icon_set_selected(Icon *icon, gboolean selected)
285 if (selected && icon_selection)
287 IconClass *iclass;
288 Icon *other = (Icon *) icon_selection->data;
290 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
291 if (!iclass->same_group(icon, other))
293 icon_select_only(icon);
294 return;
298 icon_set_selected_int(icon, selected);
301 /* Clear everything, except 'select', which is selected.
302 * If select is NULL, unselects everything.
304 void icon_select_only(Icon *select)
306 GList *to_clear, *next;
308 if (select)
309 icon_set_selected_int(select, TRUE);
311 to_clear = g_list_copy(icon_selection);
313 if (select)
314 to_clear = g_list_remove(to_clear, select);
316 for (next = to_clear; next; next = next->next)
317 icon_set_selected_int((Icon *) next->data, FALSE);
319 g_list_free(to_clear);
322 /* Destroys this icon's widget, causing it to be removed from the screen */
323 void icon_destroy(Icon *icon)
325 g_return_if_fail(icon != NULL);
327 icon_set_selected_int(icon, FALSE);
329 g_signal_emit_by_name(icon, "destroy");
332 /* Return a text/uri-list of all the icons in the selection */
333 gchar *icon_create_uri_list(void)
335 GString *tmp;
336 guchar *retval;
337 guchar *leader;
338 GList *next;
340 tmp = g_string_new(NULL);
341 leader = g_strdup_printf("file://%s", our_host_name_for_dnd());
343 for (next = icon_selection; next; next = next->next)
345 Icon *icon = (Icon *) next->data;
347 g_string_append(tmp, leader);
348 g_string_append(tmp, icon->path);
349 g_string_append(tmp, "\r\n");
352 g_free(leader);
353 retval = tmp->str;
354 g_string_free(tmp, FALSE);
356 return retval;
359 GType icon_get_type(void)
361 static GType type = 0;
363 if (!type)
365 static const GTypeInfo info =
367 sizeof (IconClass),
368 NULL, /* base_init */
369 NULL, /* base_finalise */
370 icon_class_init,
371 NULL, /* class_finalise */
372 NULL, /* class_data */
373 sizeof(Icon),
374 0, /* n_preallocs */
375 icon_init
378 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
379 &info, 0);
382 return type;
385 /* Sets, unsets or changes the pathname and name for an icon.
386 * Updates icons_hash and (re)stats the item.
387 * If name is NULL then gets the leafname from pathname.
388 * If pathname is NULL, frees everything.
390 void icon_set_path(Icon *icon, const char *pathname, const char *name)
392 if (icon->path)
394 icon_unhash_path(icon);
395 icon->src_path = NULL;
396 icon->path = NULL;
398 diritem_free(icon->item);
399 icon->item = NULL;
402 if (pathname)
404 icon->src_path = g_strdup(pathname);
405 icon->path = expand_path(pathname);
407 icon_hash_path(icon);
409 if (!name)
410 name = g_basename(pathname);
412 icon->item = diritem_new(name);
413 diritem_restat(icon->path, icon->item, NULL);
417 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
419 g_return_if_fail(icon != NULL);
421 if (shortcut && !*shortcut)
422 shortcut = NULL;
423 if (icon->shortcut == shortcut)
424 return;
425 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
426 return;
428 initModifiers();
430 ungrab_key(icon);
432 g_free(icon->shortcut);
433 icon->shortcut = g_strdup(shortcut);
434 parseKeyString(&icon->shortcut_key, shortcut);
436 grab_key(icon);
439 /****************************************************************
440 * INTERNAL FUNCTIONS *
441 ****************************************************************/
443 /* The icons_hash table allows us to convert from a path to a list
444 * of icons that use that path.
445 * Add this icon to the list for its path.
447 static void icon_hash_path(Icon *icon)
449 GList *list;
451 g_return_if_fail(icon != NULL);
453 /* g_print("[ hashing '%s' ]\n", icon->path); */
455 list = g_hash_table_lookup(icons_hash, icon->path);
456 list = g_list_prepend(list, icon);
457 g_hash_table_insert(icons_hash, icon->path, list);
460 /* Remove this icon from the icons_hash table */
461 static void icon_unhash_path(Icon *icon)
463 GList *list;
465 g_return_if_fail(icon != NULL);
467 /* g_print("[ unhashing '%s' ]\n", icon->path); */
469 list = g_hash_table_lookup(icons_hash, icon->path);
470 g_return_if_fail(list != NULL);
472 list = g_list_remove(list, icon);
474 /* Remove it first; the hash key may have changed address */
475 g_hash_table_remove(icons_hash, icon->path);
476 if (list)
477 g_hash_table_insert(icons_hash,
478 ((Icon *) list->data)->path, list);
481 static void rename_activate(GtkWidget *dialog)
483 GtkWidget *entry, *src, *shortcut;
484 Icon *icon;
485 const guchar *new_name, *new_src, *new_shortcut;
487 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
488 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
489 src = g_object_get_data(G_OBJECT(dialog), "new_path");
490 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
492 g_return_if_fail(entry != NULL &&
493 src != NULL &&
494 icon != NULL &&
495 shortcut != NULL);
497 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
498 new_src = gtk_entry_get_text(GTK_ENTRY(src));
499 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
500 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
501 new_shortcut = NULL;
503 if (*new_src == '\0')
504 report_error(
505 _("The location must contain at least one character!"));
506 else
508 icon_set_path(icon, new_src, new_name);
509 icon_set_shortcut(icon, new_shortcut);
510 g_signal_emit_by_name(icon, "update");
511 gtk_widget_destroy(dialog);
515 static void menu_closed(GtkWidget *widget)
517 appmenu_remove();
518 menu_icon = NULL;
521 /* Called when another application takes the selection away from us */
522 static void lose_selection(GtkClipboard *primary, gpointer data)
524 have_primary = FALSE;
525 icon_select_only(NULL);
528 /* Called when another application wants the contents of our selection */
529 static void selection_get(GtkClipboard *primary,
530 GtkSelectionData *selection_data,
531 guint info,
532 gpointer data)
534 gchar *text;
536 if (info == TARGET_URI_LIST)
537 text = icon_create_uri_list();
538 else
540 GList *next;
541 GString *str;
543 str = g_string_new(NULL);
545 for (next = icon_selection; next; next = next->next)
547 Icon *icon = (Icon *) next->data;
549 g_string_append(str, icon->path);
550 g_string_append_c(str, ' ');
553 text = str->str;
554 g_string_free(str, FALSE);
557 gtk_selection_data_set(selection_data,
558 gdk_atom_intern("STRING", FALSE),
559 8, text, strlen(text));
562 static void set_backdrop(gpointer data, guint action, GtkWidget *widget)
564 pinboard_set_backdrop();
567 static void remove_items(gpointer data, guint action, GtkWidget *widget)
569 IconClass *iclass;
571 if (menu_icon)
572 icon_set_selected(menu_icon, TRUE);
574 if (!icon_selection)
576 delayed_error(
577 _("You must first select some items to remove"));
578 return;
581 iclass = (IconClass *)
582 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
584 iclass->remove_items();
587 static void file_op(gpointer data, guint action, GtkWidget *widget)
589 if (!menu_icon)
591 delayed_error(_("You must open the menu over an item"));
592 return;
595 switch (action)
597 case ACTION_SHIFT:
598 run_diritem(menu_icon->path, menu_icon->item,
599 NULL, NULL, TRUE);
600 break;
601 case ACTION_EDIT:
602 show_rename_box(menu_icon);
603 break;
604 case ACTION_LOCATION:
605 open_to_show(menu_icon->path);
606 break;
607 case ACTION_HELP:
608 show_item_help(menu_icon->path, menu_icon->item);
609 break;
610 case ACTION_INFO:
611 infobox_new(menu_icon->path);
612 break;
613 case ACTION_RUN_ACTION:
614 if (can_set_run_action(menu_icon->item))
615 type_set_handler_dialog(
616 menu_icon->item->mime_type);
617 else
618 report_error(
619 _("You can only set the run action for a "
620 "regular file"));
621 break;
622 case ACTION_SET_ICON:
623 icon_set_handler_dialog(menu_icon->item,
624 menu_icon->path);
625 break;
629 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
631 if (response == GTK_RESPONSE_OK)
632 rename_activate(dialog);
633 else if (response == GTK_RESPONSE_CANCEL)
634 gtk_widget_destroy(dialog);
637 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
638 GdkEvent *event,
639 gpointer data)
641 XKeyEvent *kev = (XKeyEvent *) xevent;
642 GtkWidget *popup = (GtkWidget *) data;
643 Display *dpy = GDK_DISPLAY();
645 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
646 return GDK_FILTER_CONTINUE;
648 initModifiers();
650 if (kev->type == KeyRelease)
652 gchar *str;
653 KeySym sym;
654 unsigned int m = kev->state;
656 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
657 if (!sym)
658 return GDK_FILTER_CONTINUE;
660 str = g_strdup_printf("%s%s%s%s%s%s%s",
661 m & ControlMask ? "Control+" : "",
662 m & ShiftMask ? "Shift+" : "",
663 m & AltMask ? "Alt+" : "",
664 m & MetaMask ? "Meta+" : "",
665 m & SuperMask ? "Super+" : "",
666 m & HyperMask ? "Hyper+" : "",
667 XKeysymToString(sym));
669 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
672 gdk_window_remove_filter(popup->window, filter_get_key, data);
673 gtk_widget_destroy(popup);
675 return GDK_FILTER_REMOVE;
678 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
680 gchar *str;
682 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
683 if (str)
685 gtk_label_set_text(GTK_LABEL(label), str);
686 g_free(str);
687 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
691 static void get_shortcut(GtkWidget *button, GtkWidget *label)
693 GtkWidget *popup, *frame, *msg;
694 Window xid;
695 Display *dpy = GDK_DISPLAY();
697 popup = gtk_window_new(GTK_WINDOW_POPUP);
699 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
701 frame = gtk_frame_new(NULL);
702 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
703 gtk_container_add(GTK_CONTAINER(popup), frame);
705 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
707 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
708 gtk_container_add(GTK_CONTAINER(frame), msg);
710 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
712 gtk_widget_add_events(popup,
713 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
715 g_signal_connect(popup, "destroy",
716 G_CALLBACK(may_set_shortcut), label);
718 gtk_widget_show_all(popup);
720 gdk_window_add_filter(popup->window, filter_get_key, popup);
722 xid = gdk_x11_drawable_get_xid(popup->window);
724 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
725 gtk_get_current_event_time()) != Success)
727 delayed_error(_("Failed to get keyboard grab!"));
728 gtk_widget_destroy(popup);
731 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
732 GrabModeAsync, xid, None,
733 gtk_get_current_event_time()) != Success)
735 g_warning("Failed to get mouse grab");
739 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
741 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
744 /* Opens a box allowing the user to change the name of a pinned icon.
745 * If the icon is destroyed then the box will close.
746 * If the user chooses OK then the callback is called once the icon's
747 * name, src_path and path fields have been updated and the item field
748 * restatted.
750 static void show_rename_box(Icon *icon)
752 GtkDialog *dialog;
753 GtkWidget *label, *entry, *button, *button2, *hbox, *spacer;
754 GtkBox *vbox;
756 if (icon->dialog)
758 gtk_window_present(GTK_WINDOW(icon->dialog));
759 return;
762 icon->dialog = gtk_dialog_new();
763 g_signal_connect(icon->dialog, "destroy",
764 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
766 dialog = GTK_DIALOG(icon->dialog);
768 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
769 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
770 TRUE, TRUE, 0);
771 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
773 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
774 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
776 label = gtk_label_new(_("Clicking the icon opens:"));
777 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
779 entry = gtk_entry_new();
780 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
781 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
782 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
783 g_signal_connect_swapped(entry, "activate",
784 G_CALLBACK(rename_activate), dialog);
786 spacer = gtk_drawing_area_new();
787 gtk_widget_set_size_request(spacer, 4, 4);
788 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
790 label = gtk_label_new(_("The text displayed under the icon is:"));
791 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
792 entry = gtk_entry_new();
793 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
794 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
795 gtk_widget_grab_focus(entry);
796 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
797 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
799 spacer = gtk_drawing_area_new();
800 gtk_widget_set_size_request(spacer, 4, 4);
801 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
803 label = gtk_label_new(_("The keyboard shortcut is:"));
804 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
806 hbox = gtk_hbox_new(FALSE, 2);
807 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
808 button = gtk_button_new_with_label(icon->shortcut
809 ? icon->shortcut
810 : CLICK_TO_SET);
811 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
812 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
813 GTK_BIN(button)->child);
814 g_signal_connect(button, "clicked",
815 G_CALLBACK(get_shortcut),
816 GTK_BIN(button)->child);
817 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
818 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
819 g_signal_connect(button2, "clicked",
820 G_CALLBACK(clear_shortcut),
821 GTK_BIN(button)->child);
823 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
825 gtk_dialog_add_buttons(dialog,
826 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
827 GTK_STOCK_OK, GTK_RESPONSE_OK,
828 NULL);
829 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
831 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
833 gtk_widget_show_all(GTK_WIDGET(dialog));
836 static gpointer parent_class = NULL;
838 static void icon_finialize(GObject *object)
840 Icon *icon = (Icon *) object;
842 g_return_if_fail(!icon->selected);
844 if (icon->dialog)
845 gtk_widget_destroy(icon->dialog);
847 if (icon == menu_icon)
848 menu_icon = NULL;
850 icon_set_path(icon, NULL, NULL);
851 icon_set_shortcut(icon, NULL);
853 G_OBJECT_CLASS(parent_class)->finalize(object);
856 static void icon_class_init(gpointer gclass, gpointer data)
858 guchar *tmp;
859 GList *items;
860 GtkItemFactory *item_factory;
861 GObjectClass *object = (GObjectClass *) gclass;
862 IconClass *icon = (IconClass *) gclass;
864 parent_class = g_type_class_peek_parent(gclass);
866 object->finalize = icon_finialize;
867 icon->destroy = NULL;
868 icon->redraw = NULL;
869 icon->update = NULL;
870 icon->same_group = NULL;
871 icon->wink = NULL;
873 g_signal_new("update",
874 G_TYPE_FROM_CLASS(gclass),
875 G_SIGNAL_RUN_LAST,
876 G_STRUCT_OFFSET(IconClass, update),
877 NULL, NULL,
878 g_cclosure_marshal_VOID__VOID,
879 G_TYPE_NONE, 0);
881 g_signal_new("destroy",
882 G_TYPE_FROM_CLASS(gclass),
883 G_SIGNAL_RUN_LAST,
884 G_STRUCT_OFFSET(IconClass, destroy),
885 NULL, NULL,
886 g_cclosure_marshal_VOID__VOID,
887 G_TYPE_NONE, 0);
889 g_signal_new("redraw",
890 G_TYPE_FROM_CLASS(gclass),
891 G_SIGNAL_RUN_LAST,
892 G_STRUCT_OFFSET(IconClass, redraw),
893 NULL, NULL,
894 g_cclosure_marshal_VOID__VOID,
895 G_TYPE_NONE, 0);
897 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
899 item_factory = menu_create(menu_def,
900 sizeof(menu_def) / sizeof(*menu_def),
901 "<icon>", NULL);
903 tmp = g_strconcat("<icon>/", _("File"), NULL);
904 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
905 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
906 g_free(tmp);
908 /* File '' label... */
909 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
910 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
911 g_list_free(items);
913 /* Shift Open... label */
914 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
915 file_shift_item = GTK_BIN(items->data)->child;
916 g_list_free(items);
918 g_signal_connect(icon_menu, "unmap_event",
919 G_CALLBACK(menu_closed), NULL);
922 static void icon_init(GTypeInstance *object, gpointer gclass)
924 Icon *icon = (Icon *) object;
926 icon->selected = FALSE;
927 icon->src_path = NULL;
928 icon->path = NULL;
929 icon->item = NULL;
930 icon->dialog = NULL;
931 icon->shortcut = NULL;
932 icon->shortcut_key.keycode = 0;
933 icon->shortcut_key.modifier = 0;
936 /* As icon_set_selected(), but doesn't automatically unselect incompatible
937 * icons.
939 static void icon_set_selected_int(Icon *icon, gboolean selected)
941 static GtkClipboard *primary;
943 g_return_if_fail(icon != NULL);
945 if (icon->selected == selected)
946 return;
948 if (!primary)
949 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
951 if (selected)
953 icon_selection = g_list_prepend(icon_selection, icon);
954 if (!have_primary)
956 GtkTargetEntry target_table[] =
958 {"text/uri-list", 0, TARGET_URI_LIST},
959 {"UTF8", 0, TARGET_STRING},
960 {"COMPOUND_TEXT", 0, TARGET_STRING},
961 {"STRING", 0, TARGET_STRING},
964 /* Grab selection */
965 have_primary = gtk_clipboard_set_with_data(primary,
966 target_table,
967 sizeof(target_table) / sizeof(*target_table),
968 selection_get, lose_selection, NULL);
971 else
973 icon_selection = g_list_remove(icon_selection, icon);
974 if (have_primary && !icon_selection)
976 have_primary = FALSE;
977 gtk_clipboard_clear(primary);
981 icon->selected = selected;
982 g_signal_emit_by_name(icon, "redraw");
985 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n)
987 GList *items, *item;
989 items = gtk_container_get_children(GTK_CONTAINER(menu));
991 item = g_list_nth(items, from);
992 while (item && n--)
994 if (hidden)
995 gtk_widget_hide(GTK_WIDGET(item->data));
996 else
997 gtk_widget_show(GTK_WIDGET(item->data));
998 item = item->next;
1000 g_list_free(items);
1003 /* Stolen from xfwm4 */
1004 static void initModifiers(void)
1006 static gboolean need_init = TRUE;
1007 Display *dpy = GDK_DISPLAY();
1008 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1009 int m, k;
1011 if (!need_init)
1012 return;
1013 need_init = FALSE;
1015 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1016 SuperMask = HyperMask = 0;
1018 /* Work out which mask to use for each modifier group */
1019 if (xmk)
1021 KeyCode *c = xmk->modifiermap;
1022 KeyCode numLockKeyCode;
1023 KeyCode scrollLockKeyCode;
1024 KeyCode capsLockKeyCode;
1025 KeyCode altKeyCode;
1026 KeyCode metaKeyCode;
1027 KeyCode superKeyCode;
1028 KeyCode hyperKeyCode;
1030 /* Find the codes to search for... */
1031 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1032 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1033 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1034 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1035 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1036 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1037 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1039 /* If some are missing, try alternatives... */
1040 if (!altKeyCode)
1041 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1042 if (!metaKeyCode)
1043 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1044 if (!superKeyCode)
1045 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1046 if (!hyperKeyCode)
1047 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1049 /* Check each of the eight modifier lists.
1050 * The idea (I think) is that we name the modifier group which
1051 * includes the Alt key as the 'Alt group', and so on for
1052 * the other modifiers.
1054 for (m = 0; m < 8; m++)
1056 for (k = 0; k < xmk->max_keypermod; k++, c++)
1058 if (*c == NoSymbol)
1059 continue;
1060 if (*c == numLockKeyCode)
1061 NumLockMask = (1 << m);
1062 if (*c == scrollLockKeyCode)
1063 ScrollLockMask = (1 << m);
1064 if (*c == capsLockKeyCode)
1065 CapsLockMask = (1 << m);
1066 if (*c == altKeyCode)
1067 AltMask = (1 << m);
1068 if (*c == metaKeyCode)
1069 MetaMask = (1 << m);
1070 if (*c == superKeyCode)
1071 SuperMask = (1 << m);
1072 if (*c == hyperKeyCode)
1073 HyperMask = (1 << m);
1076 XFreeModifiermap(xmk);
1079 if(MetaMask == AltMask)
1080 MetaMask = 0;
1082 if (AltMask != 0 && MetaMask == Mod1Mask)
1084 MetaMask = AltMask;
1085 AltMask = Mod1Mask;
1088 if (AltMask == 0 && MetaMask != 0)
1090 if (MetaMask != Mod1Mask)
1091 AltMask = Mod1Mask;
1092 else
1094 AltMask = MetaMask;
1095 MetaMask = 0;
1099 if (AltMask == 0)
1100 AltMask = Mod1Mask;
1104 /* Fill in key from str. Sets keycode to zero if str is NULL.
1105 * Stolen from xfwm4 and modified. Call initModifiers before this.
1107 static void parseKeyString(MyKey *key, const char *str)
1109 char *k;
1110 Display *dpy = GDK_DISPLAY();
1112 key->keycode = 0;
1113 key->modifier = 0;
1115 if (!str)
1116 return;
1118 k = strrchr(str, '+');
1119 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1120 if (k)
1122 gchar *tmp;
1124 tmp = g_ascii_strdown(str, -1);
1126 if (strstr(tmp, "shift"))
1127 key->modifier = key->modifier | ShiftMask;
1128 if (strstr(tmp, "control"))
1129 key->modifier = key->modifier | ControlMask;
1130 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1131 key->modifier = key->modifier | AltMask;
1132 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1133 key->modifier = key->modifier | MetaMask;
1134 if (strstr(tmp, "hyper"))
1135 key->modifier = key->modifier | HyperMask;
1136 if (strstr(tmp, "super"))
1137 key->modifier = key->modifier | SuperMask;
1139 g_free(tmp);
1142 if (!key->keycode)
1143 g_warning("Can't parse key '%s'\n", str);
1146 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1147 GdkEvent *event,
1148 gpointer data)
1150 GList *next;
1151 XKeyEvent *kev = (XKeyEvent *) xevent;
1152 guint state;
1154 if (kev->type != KeyPress)
1155 return GDK_FILTER_CONTINUE;
1157 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask |
1158 HyperMask | SuperMask);
1160 for (next = icon_shortcuts; next; next = next->next)
1162 Icon *icon = (Icon *) next->data;
1164 if (icon->shortcut_key.keycode == kev->keycode &&
1165 icon->shortcut_key.modifier == state)
1167 icon_wink(icon);
1168 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
1172 return GDK_FILTER_CONTINUE;
1175 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1176 root, False, GrabModeAsync, GrabModeAsync)
1177 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1178 root)
1180 static guint mykey_hash(gconstpointer key)
1182 MyKey *k = (MyKey *) key;
1184 return (k->keycode << 8) + k->modifier;
1187 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1189 MyKey *ka = (MyKey *) a;
1190 MyKey *kb = (MyKey *) b;
1192 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1195 /* Stolen from xfwm4 and modified.
1196 * FALSE on error. Call initModifiers before this.
1198 static gboolean grabKey(MyKey *key)
1200 Window root;
1201 Display *dpy = GDK_DISPLAY();
1202 static gboolean need_init = TRUE;
1204 if (need_init)
1206 need_init = FALSE;
1207 gdk_window_add_filter(gdk_get_default_root_window(),
1208 filter_keys, NULL);
1211 gdk_error_trap_push();
1213 root = GDK_ROOT_WINDOW();
1215 GRAB(key, 0);
1217 if (key->modifier != AnyModifier && key->modifier != 0)
1219 /* Here we grab all combinations of well known
1220 * modifiers.
1222 GRAB(key, ScrollLockMask);
1223 GRAB(key, NumLockMask);
1224 GRAB(key, CapsLockMask);
1225 GRAB(key, ScrollLockMask | NumLockMask);
1226 GRAB(key, ScrollLockMask | CapsLockMask);
1227 GRAB(key, CapsLockMask | NumLockMask);
1228 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1231 gdk_flush();
1232 return gdk_error_trap_pop() == Success;
1235 static gboolean ungrabKey(MyKey *key)
1237 Window root = GDK_ROOT_WINDOW();
1238 Display *dpy = GDK_DISPLAY();
1240 gdk_error_trap_push();
1242 UNGRAB(key, 0);
1244 if (key->modifier != AnyModifier && key->modifier != 0)
1246 UNGRAB(key, ScrollLockMask);
1247 UNGRAB(key, NumLockMask);
1248 UNGRAB(key, CapsLockMask);
1249 UNGRAB(key, ScrollLockMask | NumLockMask);
1250 UNGRAB(key, ScrollLockMask | CapsLockMask);
1251 UNGRAB(key, CapsLockMask | NumLockMask);
1252 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1255 gdk_flush();
1256 return gdk_error_trap_pop() == Success;
1259 static void ungrab_key(Icon *icon)
1261 int *count;
1263 g_return_if_fail(icon != NULL);
1265 if (!icon->shortcut_key.keycode)
1266 return;
1268 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1270 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1271 g_return_if_fail(count != NULL);
1273 (*count)--;
1275 if (*count > 0)
1276 return;
1278 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1280 if (!ungrabKey(&icon->shortcut_key))
1281 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1282 icon->shortcut, icon->item->leafname);
1285 static void grab_key(Icon *icon)
1287 MyKey *hash_key;
1288 int *count;
1290 g_return_if_fail(icon != NULL);
1292 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1294 if (!icon->shortcut_key.keycode)
1295 return;
1297 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1299 if (!grab_counter)
1300 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1301 g_free, NULL);
1303 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1304 if (count)
1306 (*count)++;
1307 return; /* Already grabbed */
1310 hash_key = g_new(MyKey, 1);
1311 *hash_key = icon->shortcut_key;
1312 count = g_new(int, 1);
1313 *count = 1;
1314 g_hash_table_insert(grab_counter, hash_key, count);
1316 if (!grabKey(&icon->shortcut_key))
1317 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1318 "Some other application may be already using it!\n",
1319 icon->shortcut, icon->item->leafname);
1323 static void icon_wink(Icon *icon)
1325 IconClass *iclass;
1327 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1328 g_return_if_fail(iclass->wink != NULL);
1330 iclass->wink(icon);