r1996: Cope slightly better with invalid filenames in various places (reported by
[rox-filer.git] / ROX-Filer / src / icon.c
blob95d41cb2aca052cf1277a277ed02914ff5da06ab
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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);
120 enum {
121 ACTION_SHIFT,
122 ACTION_HELP,
123 ACTION_INFO,
124 ACTION_RUN_ACTION,
125 ACTION_SET_ICON,
126 ACTION_EDIT,
127 ACTION_LOCATION,
130 #undef N_
131 #define N_(x) x
132 static GtkItemFactoryEntry menu_def[] = {
133 {N_("ROX-Filer"), NULL, NULL, 0, "<Branch>"},
134 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
135 {">" N_("Show Help Files"), NULL, menu_rox_help, HELP_DIR, NULL},
136 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
137 {">", NULL, NULL, 0, "<Separator>"},
138 {">" N_("Options..."), NULL, menu_show_options, 0, NULL},
139 {">" N_("Home Directory"), NULL, open_home, 0, NULL},
140 {N_("File"), NULL, NULL, 0, "<Branch>"},
141 {">" N_("Shift Open"), NULL, file_op, ACTION_SHIFT, NULL},
142 {">" N_("Help"), NULL, file_op, ACTION_HELP, NULL},
143 {">" N_("Info"), NULL, file_op, ACTION_INFO, NULL},
144 {">" N_("Set Run Action..."), NULL, file_op, ACTION_RUN_ACTION, NULL},
145 {">" N_("Set Icon..."), NULL, file_op, ACTION_SET_ICON, NULL},
146 {N_("Edit Item"), NULL, file_op, ACTION_EDIT, NULL},
147 {N_("Show Location"), NULL, file_op, ACTION_LOCATION, NULL},
148 {N_("Remove Item(s)"), NULL, remove_items, 0, NULL},
149 {"", NULL, NULL, 0, "<Separator>"},
150 {N_("Backdrop..."), NULL, set_backdrop, 0, NULL},
153 /****************************************************************
154 * EXTERNAL INTERFACE *
155 ****************************************************************/
157 /* Called when the pointer moves over the icon */
158 void icon_may_update(Icon *icon)
160 MaskedPixmap *image;
161 int flags;
163 g_return_if_fail(icon != NULL);
165 image = icon->item->image;
166 flags = icon->item->flags;
168 if (image)
169 g_object_ref(image);
170 mount_update(FALSE);
171 diritem_restat(icon->path, icon->item, NULL);
173 if (icon->item->image != image || icon->item->flags != flags)
175 /* Appearance changed; need to redraw */
176 g_signal_emit_by_name(icon, "redraw");
179 if (image)
180 g_object_unref(image);
183 /* If path is on an icon then it may have changed... check! */
184 void icons_may_update(const gchar *path)
186 GList *affected;
188 if (icons_hash)
190 affected = g_hash_table_lookup(icons_hash, path);
192 for (; affected; affected = affected->next)
193 icon_may_update((Icon *) affected->data);
197 typedef struct _CheckData CheckData;
198 struct _CheckData {
199 const gchar *path;
200 gboolean found;
203 static void check_has(gpointer key, GList *icons, CheckData *check)
205 Icon *icon;
207 g_return_if_fail(icons != NULL);
209 icon = icons->data;
211 if (is_sub_dir(icon->path, check->path))
212 check->found = TRUE;
215 /* Returns TRUE if any icon links to this file (or any file inside
216 * this directory). Used to check that it's OK to delete 'path'.
218 gboolean icons_require(const gchar *path)
220 CheckData check;
222 if (!icons_hash)
223 return FALSE;
225 check.path = path;
226 check.found = FALSE;
227 g_hash_table_foreach(icons_hash, (GHFunc) check_has, &check);
229 return check.found;
232 /* Menu was clicked over this icon. Set things up correctly (shade items,
233 * add app menu stuff, etc).
234 * You should show icon_menu after calling this...
236 void icon_prepare_menu(Icon *icon, gboolean pinboard)
238 appmenu_remove();
240 menu_icon = icon;
242 menu_set_hidden(icon_menu, !pinboard, 5, 2);
244 /* Shade Remove Item(s) unless there is a selection */
245 menu_set_items_shaded(icon_menu,
246 (icon_selection || menu_icon) ? FALSE : TRUE,
247 4, 1);
249 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
250 FALSE);
252 /* Shade the File/Edit/Show items unless an item was clicked */
253 if (icon)
255 guchar *tmp;
257 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
258 menu_set_items_shaded(icon_file_menu, FALSE, 0, 6);
259 if (!can_set_run_action(icon->item))
260 menu_set_items_shaded(icon_file_menu, TRUE, 3, 1);
262 tmp = g_strdup_printf("%s '%s'",
263 basetype_name(icon->item),
264 icon->item->leafname);
265 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
266 g_free(tmp);
268 /* Check for app-specific menu */
269 appmenu_add(icon->path, icon->item, icon_menu);
271 else
273 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
274 menu_set_items_shaded(icon_file_menu, TRUE, 0, 6);
275 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
279 /* Set whether this icon is selected. Will automatically clear the selection
280 * if it contains icons from a different group.
282 void icon_set_selected(Icon *icon, gboolean selected)
284 if (selected && icon_selection)
286 IconClass *iclass;
287 Icon *other = (Icon *) icon_selection->data;
289 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
290 if (!iclass->same_group(icon, other))
292 icon_select_only(icon);
293 return;
297 icon_set_selected_int(icon, selected);
300 /* Clear everything, except 'select', which is selected.
301 * If select is NULL, unselects everything.
303 void icon_select_only(Icon *select)
305 GList *to_clear, *next;
307 if (select)
308 icon_set_selected_int(select, TRUE);
310 to_clear = g_list_copy(icon_selection);
312 if (select)
313 to_clear = g_list_remove(to_clear, select);
315 for (next = to_clear; next; next = next->next)
316 icon_set_selected_int((Icon *) next->data, FALSE);
318 g_list_free(to_clear);
321 /* Destroys this icon's widget, causing it to be removed from the screen */
322 void icon_destroy(Icon *icon)
324 g_return_if_fail(icon != NULL);
326 icon_set_selected_int(icon, FALSE);
328 g_signal_emit_by_name(icon, "destroy");
331 /* Return a text/uri-list of all the icons in the selection */
332 gchar *icon_create_uri_list(void)
334 GString *tmp;
335 guchar *retval;
336 guchar *leader;
337 GList *next;
339 tmp = g_string_new(NULL);
340 leader = g_strdup_printf("file://%s", our_host_name_for_dnd());
342 for (next = icon_selection; next; next = next->next)
344 Icon *icon = (Icon *) next->data;
346 g_string_append(tmp, leader);
347 g_string_append(tmp, icon->path);
348 g_string_append(tmp, "\r\n");
351 g_free(leader);
352 retval = tmp->str;
353 g_string_free(tmp, FALSE);
355 return retval;
358 GType icon_get_type(void)
360 static GType type = 0;
362 if (!type)
364 static const GTypeInfo info =
366 sizeof (IconClass),
367 NULL, /* base_init */
368 NULL, /* base_finalise */
369 icon_class_init,
370 NULL, /* class_finalise */
371 NULL, /* class_data */
372 sizeof(Icon),
373 0, /* n_preallocs */
374 icon_init
377 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
378 &info, 0);
381 return type;
384 /* Sets, unsets or changes the pathname and name for an icon.
385 * Updates icons_hash and (re)stats the item.
386 * If name is NULL then gets the leafname from pathname.
387 * If pathname is NULL, frees everything.
389 void icon_set_path(Icon *icon, const char *pathname, const char *name)
391 if (icon->path)
393 icon_unhash_path(icon);
394 icon->src_path = NULL;
395 icon->path = NULL;
397 diritem_free(icon->item);
398 icon->item = NULL;
401 if (pathname)
403 icon->src_path = g_strdup(pathname);
404 icon->path = expand_path(pathname);
406 icon_hash_path(icon);
408 if (!name)
409 name = g_basename(pathname);
411 icon->item = diritem_new(name);
412 diritem_restat(icon->path, icon->item, NULL);
416 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
418 g_return_if_fail(icon != NULL);
420 if (shortcut && !*shortcut)
421 shortcut = NULL;
422 if (icon->shortcut == shortcut)
423 return;
424 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
425 return;
427 ungrab_key(icon);
429 g_free(icon->shortcut);
430 icon->shortcut = g_strdup(shortcut);
431 parseKeyString(&icon->shortcut_key, shortcut);
433 grab_key(icon);
436 /****************************************************************
437 * INTERNAL FUNCTIONS *
438 ****************************************************************/
440 /* The icons_hash table allows us to convert from a path to a list
441 * of icons that use that path.
442 * Add this icon to the list for its path.
444 static void icon_hash_path(Icon *icon)
446 GList *list;
448 g_return_if_fail(icon != NULL);
450 /* g_print("[ hashing '%s' ]\n", icon->path); */
452 list = g_hash_table_lookup(icons_hash, icon->path);
453 list = g_list_prepend(list, icon);
454 g_hash_table_insert(icons_hash, icon->path, list);
457 /* Remove this icon from the icons_hash table */
458 static void icon_unhash_path(Icon *icon)
460 GList *list;
462 g_return_if_fail(icon != NULL);
464 /* g_print("[ unhashing '%s' ]\n", icon->path); */
466 list = g_hash_table_lookup(icons_hash, icon->path);
467 g_return_if_fail(list != NULL);
469 list = g_list_remove(list, icon);
471 /* Remove it first; the hash key may have changed address */
472 g_hash_table_remove(icons_hash, icon->path);
473 if (list)
474 g_hash_table_insert(icons_hash,
475 ((Icon *) list->data)->path, list);
478 static void rename_activate(GtkWidget *dialog)
480 GtkWidget *entry, *src, *shortcut;
481 Icon *icon;
482 const guchar *new_name, *new_src, *new_shortcut;
484 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
485 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
486 src = g_object_get_data(G_OBJECT(dialog), "new_path");
487 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
489 g_return_if_fail(entry != NULL &&
490 src != NULL &&
491 icon != NULL &&
492 shortcut != NULL);
494 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
495 new_src = gtk_entry_get_text(GTK_ENTRY(src));
496 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
497 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
498 new_shortcut = NULL;
500 if (*new_src == '\0')
501 report_error(
502 _("The location must contain at least one character!"));
503 else
505 icon_set_path(icon, new_src, new_name);
506 icon_set_shortcut(icon, new_shortcut);
507 g_signal_emit_by_name(icon, "update");
508 gtk_widget_destroy(dialog);
512 static void menu_closed(GtkWidget *widget)
514 appmenu_remove();
515 menu_icon = NULL;
518 /* Called when another application takes the selection away from us */
519 static void lose_selection(GtkClipboard *primary, gpointer data)
521 have_primary = FALSE;
522 icon_select_only(NULL);
525 /* Called when another application wants the contents of our selection */
526 static void selection_get(GtkClipboard *primary,
527 GtkSelectionData *selection_data,
528 guint info,
529 gpointer data)
531 gchar *text;
533 if (info == TARGET_URI_LIST)
534 text = icon_create_uri_list();
535 else
537 GList *next;
538 GString *str;
540 str = g_string_new(NULL);
542 for (next = icon_selection; next; next = next->next)
544 Icon *icon = (Icon *) next->data;
546 g_string_append(str, icon->path);
547 g_string_append_c(str, ' ');
550 text = str->str;
551 g_string_free(str, FALSE);
554 gtk_selection_data_set(selection_data,
555 gdk_atom_intern("STRING", FALSE),
556 8, text, strlen(text));
559 static void set_backdrop(gpointer data, guint action, GtkWidget *widget)
561 pinboard_set_backdrop();
564 static void remove_items(gpointer data, guint action, GtkWidget *widget)
566 IconClass *iclass;
568 if (menu_icon)
569 icon_set_selected(menu_icon, TRUE);
571 if (!icon_selection)
573 delayed_error(
574 _("You must first select some items to remove"));
575 return;
578 iclass = (IconClass *)
579 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
581 iclass->remove_items();
584 static void file_op(gpointer data, guint action, GtkWidget *widget)
586 if (!menu_icon)
588 delayed_error(_("You must open the menu over an item"));
589 return;
592 switch (action)
594 case ACTION_SHIFT:
595 run_diritem(menu_icon->path, menu_icon->item,
596 NULL, NULL, TRUE);
597 break;
598 case ACTION_EDIT:
599 show_rename_box(menu_icon);
600 break;
601 case ACTION_LOCATION:
602 open_to_show(menu_icon->path);
603 break;
604 case ACTION_HELP:
605 show_item_help(menu_icon->path, menu_icon->item);
606 break;
607 case ACTION_INFO:
608 infobox_new(menu_icon->path);
609 break;
610 case ACTION_RUN_ACTION:
611 if (can_set_run_action(menu_icon->item))
612 type_set_handler_dialog(
613 menu_icon->item->mime_type);
614 else
615 report_error(
616 _("You can only set the run action for a "
617 "regular file"));
618 break;
619 case ACTION_SET_ICON:
620 icon_set_handler_dialog(menu_icon->item,
621 menu_icon->path);
622 break;
626 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
628 if (response == GTK_RESPONSE_OK)
629 rename_activate(dialog);
630 else if (response == GTK_RESPONSE_CANCEL)
631 gtk_widget_destroy(dialog);
634 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
635 GdkEvent *event,
636 gpointer data)
638 XKeyEvent *kev = (XKeyEvent *) xevent;
639 GtkWidget *popup = (GtkWidget *) data;
640 Display *dpy = GDK_DISPLAY();
642 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
643 return GDK_FILTER_CONTINUE;
645 if (kev->type == KeyRelease)
647 gchar *str;
648 KeySym sym;
649 unsigned int m = kev->state;
651 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
652 if (!sym)
653 return GDK_FILTER_CONTINUE;
655 str = g_strdup_printf("%s%s%s%s%s%s%s",
656 m & ControlMask ? "Control+" : "",
657 m & ShiftMask ? "Shift+" : "",
658 m & AltMask ? "Alt+" : "",
659 m & MetaMask ? "Hyper+" : "",
660 m & SuperMask ? "Super+" : "",
661 m & HyperMask ? "Hyper+" : "",
662 XKeysymToString(sym));
664 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
667 gdk_window_remove_filter(popup->window, filter_get_key, data);
668 gtk_widget_destroy(popup);
670 return GDK_FILTER_REMOVE;
673 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
675 gchar *str;
677 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
678 if (str)
680 gtk_label_set_text(GTK_LABEL(label), str);
681 g_free(str);
682 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
686 static void get_shortcut(GtkWidget *button, GtkWidget *label)
688 GtkWidget *popup, *frame, *msg;
689 Window xid;
690 Display *dpy = GDK_DISPLAY();
692 popup = gtk_window_new(GTK_WINDOW_POPUP);
694 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
696 frame = gtk_frame_new(NULL);
697 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
698 gtk_container_add(GTK_CONTAINER(popup), frame);
700 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
702 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
703 gtk_container_add(GTK_CONTAINER(frame), msg);
705 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
707 gtk_widget_add_events(popup,
708 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
710 g_signal_connect(popup, "destroy",
711 G_CALLBACK(may_set_shortcut), label);
713 gtk_widget_show_all(popup);
715 gdk_window_add_filter(popup->window, filter_get_key, popup);
717 xid = gdk_x11_drawable_get_xid(popup->window);
719 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
720 gtk_get_current_event_time()) != Success)
722 delayed_error(_("Failed to get keyboard grab!"));
723 gtk_widget_destroy(popup);
726 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
727 GrabModeAsync, xid, None,
728 gtk_get_current_event_time()) != Success)
730 g_warning("Failed to get mouse grab");
734 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
736 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
739 /* Opens a box allowing the user to change the name of a pinned icon.
740 * If the icon is destroyed then the box will close.
741 * If the user chooses OK then the callback is called once the icon's
742 * name, src_path and path fields have been updated and the item field
743 * restatted.
745 static void show_rename_box(Icon *icon)
747 GtkDialog *dialog;
748 GtkWidget *label, *entry, *button, *button2, *hbox;
750 if (icon->dialog)
752 gtk_window_present(GTK_WINDOW(icon->dialog));
753 return;
756 icon->dialog = gtk_dialog_new();
757 g_signal_connect(icon->dialog, "destroy",
758 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
760 dialog = GTK_DIALOG(icon->dialog);
762 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
763 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
765 label = gtk_label_new(_("Clicking the icon opens:"));
766 gtk_box_pack_start(GTK_BOX(dialog->vbox), label, TRUE, TRUE, 0);
768 entry = gtk_entry_new();
769 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, TRUE, FALSE, 2);
770 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
771 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
772 g_signal_connect_swapped(entry, "activate",
773 G_CALLBACK(rename_activate), dialog);
775 gtk_box_pack_start(GTK_BOX(dialog->vbox),
776 gtk_hseparator_new(), TRUE, TRUE, 2);
778 label = gtk_label_new(_("The text displayed under the icon is:"));
779 gtk_box_pack_start(GTK_BOX(dialog->vbox), label, TRUE, TRUE, 0);
780 entry = gtk_entry_new();
781 gtk_box_pack_start(GTK_BOX(dialog->vbox), entry, TRUE, FALSE, 2);
782 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
783 gtk_widget_grab_focus(entry);
784 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
785 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
787 label = gtk_label_new(_("The keyboard shortcut is:"));
788 gtk_box_pack_start(GTK_BOX(dialog->vbox), label, TRUE, TRUE, 0);
790 hbox = gtk_hbox_new(FALSE, 2);
791 gtk_box_pack_start(GTK_BOX(dialog->vbox), hbox, TRUE, FALSE, 0);
792 button = gtk_button_new_with_label(icon->shortcut
793 ? icon->shortcut
794 : CLICK_TO_SET);
795 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
796 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
797 GTK_BIN(button)->child);
798 g_signal_connect(button, "clicked",
799 G_CALLBACK(get_shortcut),
800 GTK_BIN(button)->child);
801 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
802 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
803 g_signal_connect(button2, "clicked",
804 G_CALLBACK(clear_shortcut),
805 GTK_BIN(button)->child);
807 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
809 gtk_dialog_add_buttons(dialog,
810 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
811 GTK_STOCK_OK, GTK_RESPONSE_OK,
812 NULL);
813 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
815 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
817 gtk_widget_show_all(GTK_WIDGET(dialog));
820 static gpointer parent_class = NULL;
822 static void icon_finialize(GObject *object)
824 Icon *icon = (Icon *) object;
826 g_return_if_fail(!icon->selected);
828 if (icon->dialog)
829 gtk_widget_destroy(icon->dialog);
831 if (icon == menu_icon)
832 menu_icon = NULL;
834 icon_set_path(icon, NULL, NULL);
835 icon_set_shortcut(icon, NULL);
837 G_OBJECT_CLASS(parent_class)->finalize(object);
840 static void icon_class_init(gpointer gclass, gpointer data)
842 guchar *tmp;
843 GList *items;
844 GtkItemFactory *item_factory;
845 GObjectClass *object = (GObjectClass *) gclass;
846 IconClass *icon = (IconClass *) gclass;
848 parent_class = g_type_class_peek_parent(gclass);
850 object->finalize = icon_finialize;
851 icon->destroy = NULL;
852 icon->redraw = NULL;
853 icon->update = NULL;
854 icon->same_group = NULL;
855 icon->wink = NULL;
857 g_signal_new("update",
858 G_TYPE_FROM_CLASS(gclass),
859 G_SIGNAL_RUN_LAST,
860 G_STRUCT_OFFSET(IconClass, update),
861 NULL, NULL,
862 g_cclosure_marshal_VOID__VOID,
863 G_TYPE_NONE, 0);
865 g_signal_new("destroy",
866 G_TYPE_FROM_CLASS(gclass),
867 G_SIGNAL_RUN_LAST,
868 G_STRUCT_OFFSET(IconClass, destroy),
869 NULL, NULL,
870 g_cclosure_marshal_VOID__VOID,
871 G_TYPE_NONE, 0);
873 g_signal_new("redraw",
874 G_TYPE_FROM_CLASS(gclass),
875 G_SIGNAL_RUN_LAST,
876 G_STRUCT_OFFSET(IconClass, redraw),
877 NULL, NULL,
878 g_cclosure_marshal_VOID__VOID,
879 G_TYPE_NONE, 0);
881 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
883 item_factory = menu_create(menu_def,
884 sizeof(menu_def) / sizeof(*menu_def),
885 "<icon>", NULL);
887 tmp = g_strconcat("<icon>/", _("File"), NULL);
888 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
889 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
890 g_free(tmp);
892 /* File '' label... */
893 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
894 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
895 g_list_free(items);
897 /* Shift Open... label */
898 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
899 file_shift_item = GTK_BIN(items->data)->child;
900 g_list_free(items);
902 g_signal_connect(icon_menu, "unmap_event",
903 G_CALLBACK(menu_closed), NULL);
906 static void icon_init(GTypeInstance *object, gpointer gclass)
908 Icon *icon = (Icon *) object;
910 icon->selected = FALSE;
911 icon->src_path = NULL;
912 icon->path = NULL;
913 icon->item = NULL;
914 icon->dialog = NULL;
915 icon->shortcut = NULL;
916 icon->shortcut_key.keycode = 0;
917 icon->shortcut_key.modifier = 0;
920 /* As icon_set_selected(), but doesn't automatically unselect incompatible
921 * icons.
923 static void icon_set_selected_int(Icon *icon, gboolean selected)
925 static GtkClipboard *primary;
927 g_return_if_fail(icon != NULL);
929 if (icon->selected == selected)
930 return;
932 if (!primary)
933 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
935 if (selected)
937 icon_selection = g_list_prepend(icon_selection, icon);
938 if (!have_primary)
940 GtkTargetEntry target_table[] =
942 {"text/uri-list", 0, TARGET_URI_LIST},
943 {"UTF8", 0, TARGET_STRING},
944 {"COMPOUND_TEXT", 0, TARGET_STRING},
945 {"STRING", 0, TARGET_STRING},
948 /* Grab selection */
949 have_primary = gtk_clipboard_set_with_data(primary,
950 target_table,
951 sizeof(target_table) / sizeof(*target_table),
952 selection_get, lose_selection, NULL);
955 else
957 icon_selection = g_list_remove(icon_selection, icon);
958 if (have_primary && !icon_selection)
960 have_primary = FALSE;
961 gtk_clipboard_clear(primary);
965 icon->selected = selected;
966 g_signal_emit_by_name(icon, "redraw");
969 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n)
971 GList *items, *item;
973 items = gtk_container_get_children(GTK_CONTAINER(menu));
975 item = g_list_nth(items, from);
976 while (item && n--)
978 if (hidden)
979 gtk_widget_hide(GTK_WIDGET(item->data));
980 else
981 gtk_widget_show(GTK_WIDGET(item->data));
982 item = item->next;
984 g_list_free(items);
987 /* Stolen from xfwm4 */
988 static void initModifiers(void)
990 Display *dpy = GDK_DISPLAY();
991 XModifierKeymap *xmk = XGetModifierMapping(dpy);
992 int m, k;
994 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
995 SuperMask = HyperMask = 0;
997 /* Work out which mask to use for each modifier group */
998 if (xmk)
1000 KeyCode *c = xmk->modifiermap;
1001 KeyCode numLockKeyCode;
1002 KeyCode scrollLockKeyCode;
1003 KeyCode capsLockKeyCode;
1004 KeyCode altKeyCode;
1005 KeyCode metaKeyCode;
1006 KeyCode superKeyCode;
1007 KeyCode hyperKeyCode;
1009 /* Find the codes to search for... */
1010 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1011 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1012 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1013 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1014 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1015 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1016 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1018 /* If some are missing, try alternatives... */
1019 if (!altKeyCode)
1020 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1021 if (!metaKeyCode)
1022 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1023 if (!superKeyCode)
1024 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1025 if (!hyperKeyCode)
1026 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1028 /* Check each of the eight modifier lists.
1029 * The idea (I think) is that we name the modifier group which
1030 * includes the Alt key as the 'Alt group', and so on for
1031 * the other modifiers.
1033 for (m = 0; m < 8; m++)
1035 for (k = 0; k < xmk->max_keypermod; k++, c++)
1037 if (*c == NoSymbol)
1038 continue;
1039 if (*c == numLockKeyCode)
1040 NumLockMask = (1 << m);
1041 if (*c == scrollLockKeyCode)
1042 ScrollLockMask = (1 << m);
1043 if (*c == capsLockKeyCode)
1044 CapsLockMask = (1 << m);
1045 if (*c == altKeyCode)
1046 AltMask = (1 << m);
1047 if (*c == metaKeyCode)
1048 MetaMask = (1 << m);
1049 if (*c == superKeyCode)
1050 SuperMask = (1 << m);
1051 if (*c == hyperKeyCode)
1052 HyperMask = (1 << m);
1055 XFreeModifiermap(xmk);
1058 if(MetaMask == AltMask)
1059 MetaMask = 0;
1061 if (AltMask != 0 && MetaMask == Mod1Mask)
1063 MetaMask = AltMask;
1064 AltMask = Mod1Mask;
1067 if (AltMask == 0 && MetaMask != 0)
1069 if (MetaMask != Mod1Mask)
1070 AltMask = Mod1Mask;
1071 else
1073 AltMask = MetaMask;
1074 MetaMask = 0;
1078 if (AltMask == 0)
1079 AltMask = Mod1Mask;
1082 #define PARSEKEY(s, k) strstr(g_ascii_strdown(s, strlen(s)), \
1083 g_ascii_strdown(k, strlen(k)))
1085 /* Fill in key from str. Sets keycode to zero if str is NULL.
1086 * Stolen from xfwm4.
1088 static void parseKeyString(MyKey *key, const char *str)
1090 char *k;
1091 Display *dpy = GDK_DISPLAY();
1093 key->keycode = 0;
1094 key->modifier = 0;
1096 if (!str)
1097 return;
1099 k = strrchr(str, '+');
1100 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1101 if (k)
1103 if (PARSEKEY(str, "Shift"))
1104 key->modifier = key->modifier | ShiftMask;
1105 if (PARSEKEY(str, "Control"))
1106 key->modifier = key->modifier | ControlMask;
1107 if (PARSEKEY(str, "Alt") || PARSEKEY(str, "Mod1"))
1108 key->modifier = key->modifier | AltMask;
1109 if (PARSEKEY(str, "Meta") || PARSEKEY(str, "Mod2"))
1110 key->modifier = key->modifier | MetaMask;
1113 if (!key->keycode)
1114 g_warning("Can't parse key '%s'\n", str);
1117 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1118 GdkEvent *event,
1119 gpointer data)
1121 GList *next;
1122 XKeyEvent *kev = (XKeyEvent *) xevent;
1123 guint state;
1125 if (kev->type != KeyPress)
1126 return GDK_FILTER_CONTINUE;
1128 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask);
1130 for (next = icon_shortcuts; next; next = next->next)
1132 Icon *icon = (Icon *) next->data;
1134 if (icon->shortcut_key.keycode == kev->keycode &&
1135 icon->shortcut_key.modifier == state)
1137 icon_wink(icon);
1138 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
1142 return GDK_FILTER_CONTINUE;
1145 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1146 root, False, GrabModeAsync, GrabModeAsync)
1147 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1148 root)
1150 static guint mykey_hash(gconstpointer key)
1152 MyKey *k = (MyKey *) key;
1154 return (k->keycode << 8) + k->modifier;
1157 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1159 MyKey *ka = (MyKey *) a;
1160 MyKey *kb = (MyKey *) b;
1162 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1165 /* Stolen from xfwm4 and modified.
1166 * FALSE on error.
1168 static gboolean grabKey(MyKey *key)
1170 Window root;
1171 Display *dpy = GDK_DISPLAY();
1172 gboolean need_init = TRUE;
1174 if (need_init)
1176 need_init = FALSE;
1178 initModifiers();
1179 gdk_window_add_filter(gdk_get_default_root_window(),
1180 filter_keys, NULL);
1183 gdk_error_trap_push();
1185 root = GDK_ROOT_WINDOW();
1187 GRAB(key, 0);
1189 if (key->modifier != AnyModifier && key->modifier != 0)
1191 /* Here we grab all combinations of well known
1192 * modifiers.
1194 GRAB(key, ScrollLockMask);
1195 GRAB(key, NumLockMask);
1196 GRAB(key, CapsLockMask);
1197 GRAB(key, ScrollLockMask | NumLockMask);
1198 GRAB(key, ScrollLockMask | CapsLockMask);
1199 GRAB(key, CapsLockMask | NumLockMask);
1200 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1203 gdk_flush();
1204 return gdk_error_trap_pop() == Success;
1207 static gboolean ungrabKey(MyKey *key)
1209 Window root = GDK_ROOT_WINDOW();
1210 Display *dpy = GDK_DISPLAY();
1212 gdk_error_trap_push();
1214 UNGRAB(key, 0);
1216 if (key->modifier != AnyModifier && key->modifier != 0)
1218 UNGRAB(key, ScrollLockMask);
1219 UNGRAB(key, NumLockMask);
1220 UNGRAB(key, CapsLockMask);
1221 UNGRAB(key, ScrollLockMask | NumLockMask);
1222 UNGRAB(key, ScrollLockMask | CapsLockMask);
1223 UNGRAB(key, CapsLockMask | NumLockMask);
1224 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1227 gdk_flush();
1228 return gdk_error_trap_pop() == Success;
1231 static void ungrab_key(Icon *icon)
1233 int *count;
1235 g_return_if_fail(icon != NULL);
1237 if (!icon->shortcut_key.keycode)
1238 return;
1240 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1242 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1243 g_return_if_fail(count != NULL);
1245 (*count)--;
1247 if (*count > 0)
1248 return;
1250 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1252 if (!ungrabKey(&icon->shortcut_key))
1253 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1254 icon->shortcut, icon->item->leafname);
1257 static void grab_key(Icon *icon)
1259 MyKey *hash_key;
1260 int *count;
1262 g_return_if_fail(icon != NULL);
1264 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1266 if (!icon->shortcut_key.keycode)
1267 return;
1269 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1271 if (!grab_counter)
1272 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1273 g_free, NULL);
1275 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1276 if (count)
1278 (*count)++;
1279 return; /* Already grabbed */
1282 hash_key = g_new(MyKey, 1);
1283 *hash_key = icon->shortcut_key;
1284 count = g_new(int, 1);
1285 *count = 1;
1286 g_hash_table_insert(grab_counter, hash_key, count);
1288 if (!grabKey(&icon->shortcut_key))
1289 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1290 "Some other application may be already using it!\n",
1291 icon->shortcut, icon->item->leafname);
1295 static void icon_wink(Icon *icon)
1297 IconClass *iclass;
1299 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1300 g_return_if_fail(iclass->wink != NULL);
1302 iclass->wink(icon);