r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / icon.c
blob9f83ee29e8a7f19005928e74963690428e72047c
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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);
120 static void create_menu(void);
122 enum {
123 ACTION_SHIFT,
124 ACTION_PROPERTIES,
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, "<StockItem>", GTK_STOCK_DIALOG_INFO},
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_("Properties"), NULL, file_op, ACTION_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
144 {">" N_("Set Run Action..."), NULL, file_op, ACTION_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
145 {">" N_("Set Icon..."), NULL, file_op, ACTION_SET_ICON, NULL},
146 {N_("Edit Item"), NULL, file_op, ACTION_EDIT, "<StockItem>", GTK_STOCK_PROPERTIES},
147 {N_("Show Location"), NULL, file_op, ACTION_LOCATION, "<StockItem>", GTK_STOCK_JUMP_TO},
148 {N_("Remove Item(s)"), NULL, remove_items, 0, "<StockItem>", GTK_STOCK_REMOVE},
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 = di_image(icon->item);
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 (di_image(icon->item) != image || icon->item->flags != flags)
175 /* Appearance changed; need to redraw */
176 g_signal_emit_by_name(icon, "update");
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 if (!icon_menu)
243 create_menu();
245 menu_set_hidden(icon_menu, !pinboard, 5, 2);
247 /* Shade Remove Item(s) unless there is a selection */
248 menu_set_items_shaded(icon_menu,
249 (icon_selection || menu_icon) ? FALSE : TRUE,
250 4, 1);
252 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
253 FALSE);
255 /* Shade the File/Edit/Show items unless an item was clicked */
256 if (icon)
258 guchar *tmp;
260 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
261 menu_set_items_shaded(icon_file_menu, FALSE, 0, 5);
262 if (!can_set_run_action(icon->item))
263 menu_set_items_shaded(icon_file_menu, TRUE, 2, 1);
265 tmp = g_strdup_printf(_("%s '%s'"),
266 basetype_name(icon->item),
267 icon->item->leafname);
268 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
269 g_free(tmp);
271 /* Check for app-specific menu */
272 appmenu_add(icon->path, icon->item, icon_menu);
274 else
276 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
277 menu_set_items_shaded(icon_file_menu, TRUE, 0, 5);
278 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
282 /* Set whether this icon is selected. Will automatically clear the selection
283 * if it contains icons from a different group.
285 void icon_set_selected(Icon *icon, gboolean selected)
287 if (selected && icon_selection)
289 IconClass *iclass;
290 Icon *other = (Icon *) icon_selection->data;
292 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
293 if (!iclass->same_group(icon, other))
295 icon_select_only(icon);
296 return;
300 icon_set_selected_int(icon, selected);
303 /* Clear everything, except 'select', which is selected.
304 * If select is NULL, unselects everything.
306 void icon_select_only(Icon *select)
308 GList *to_clear, *next;
310 if (select)
311 icon_set_selected_int(select, TRUE);
313 to_clear = g_list_copy(icon_selection);
315 if (select)
316 to_clear = g_list_remove(to_clear, select);
318 for (next = to_clear; next; next = next->next)
319 icon_set_selected_int((Icon *) next->data, FALSE);
321 g_list_free(to_clear);
324 /* Destroys this icon's widget, causing it to be removed from the screen */
325 void icon_destroy(Icon *icon)
327 g_return_if_fail(icon != NULL);
329 icon_set_selected_int(icon, FALSE);
331 g_signal_emit_by_name(icon, "destroy");
334 /* Return a text/uri-list of all the icons in the selection */
335 gchar *icon_create_uri_list(void)
337 GString *tmp;
338 guchar *retval;
339 GList *next;
341 tmp = g_string_new(NULL);
343 for (next = icon_selection; next; next = next->next)
345 Icon *icon = (Icon *) next->data;
346 EscapedPath *uri;
348 uri = encode_path_as_uri(icon->path);
349 g_string_append(tmp, (char *) uri);
350 g_free(uri);
351 g_string_append(tmp, "\r\n");
354 retval = tmp->str;
355 g_string_free(tmp, FALSE);
357 return retval;
360 GType icon_get_type(void)
362 static GType type = 0;
364 if (!type)
366 static const GTypeInfo info =
368 sizeof (IconClass),
369 NULL, /* base_init */
370 NULL, /* base_finalise */
371 icon_class_init,
372 NULL, /* class_finalise */
373 NULL, /* class_data */
374 sizeof(Icon),
375 0, /* n_preallocs */
376 icon_init
379 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
380 &info, 0);
383 return type;
386 /* Sets, unsets or changes the pathname and name for an icon.
387 * Updates icons_hash and (re)stats the item.
388 * If name is NULL then gets the leafname from pathname.
389 * If pathname is NULL, frees everything.
391 void icon_set_path(Icon *icon, const char *pathname, const char *name)
393 if (icon->path)
395 icon_unhash_path(icon);
396 icon->src_path = NULL;
397 icon->path = NULL;
399 diritem_free(icon->item);
400 icon->item = NULL;
403 if (pathname)
405 if (g_utf8_validate(pathname, -1, NULL))
406 icon->src_path = g_strdup(pathname);
407 else
408 icon->src_path = to_utf8(pathname);
409 icon->path = expand_path(icon->src_path);
411 icon_hash_path(icon);
413 if (!name)
414 name = g_basename(icon->src_path);
416 icon->item = diritem_new(name);
417 diritem_restat(icon->path, icon->item, NULL);
421 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
423 g_return_if_fail(icon != NULL);
425 if (shortcut && !*shortcut)
426 shortcut = NULL;
427 if (icon->shortcut == shortcut)
428 return;
429 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
430 return;
432 initModifiers();
434 ungrab_key(icon);
436 g_free(icon->shortcut);
437 icon->shortcut = g_strdup(shortcut);
438 parseKeyString(&icon->shortcut_key, shortcut);
440 grab_key(icon);
443 void icon_set_arguments(Icon *icon, const gchar *args)
445 g_return_if_fail(icon != NULL);
446 g_return_if_fail(args == NULL || icon->args != args);
448 if (args && !*args)
449 args = NULL;
450 if (icon->args && args && strcmp(icon->args, args) == 0)
451 return;
453 g_free(icon->args);
454 icon->args = g_strdup(args);
457 void icon_run(Icon *icon)
459 if (icon->args == NULL)
460 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
461 else
462 run_with_args(icon->path, icon->item, icon->args);
465 /****************************************************************
466 * INTERNAL FUNCTIONS *
467 ****************************************************************/
469 /* The icons_hash table allows us to convert from a path to a list
470 * of icons that use that path.
471 * Add this icon to the list for its path.
473 static void icon_hash_path(Icon *icon)
475 GList *list;
477 g_return_if_fail(icon != NULL);
479 /* g_print("[ hashing '%s' ]\n", icon->path); */
481 list = g_hash_table_lookup(icons_hash, icon->path);
482 list = g_list_prepend(list, icon);
483 g_hash_table_insert(icons_hash, icon->path, list);
486 /* Remove this icon from the icons_hash table */
487 static void icon_unhash_path(Icon *icon)
489 GList *list;
491 g_return_if_fail(icon != NULL);
493 /* g_print("[ unhashing '%s' ]\n", icon->path); */
495 list = g_hash_table_lookup(icons_hash, icon->path);
496 g_return_if_fail(list != NULL);
498 list = g_list_remove(list, icon);
500 /* Remove it first; the hash key may have changed address */
501 g_hash_table_remove(icons_hash, icon->path);
502 if (list)
503 g_hash_table_insert(icons_hash,
504 ((Icon *) list->data)->path, list);
507 static void rename_activate(GtkWidget *dialog)
509 GtkWidget *entry, *src, *shortcut, *arg;
510 Icon *icon;
511 const guchar *new_name, *new_src, *new_shortcut, *new_args;
513 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
514 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
515 src = g_object_get_data(G_OBJECT(dialog), "new_path");
516 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
517 arg = g_object_get_data(G_OBJECT(dialog), "new_arg");
519 g_return_if_fail(entry != NULL &&
520 src != NULL &&
521 icon != NULL &&
522 shortcut != NULL &&
523 arg != NULL);
525 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
526 new_src = gtk_entry_get_text(GTK_ENTRY(src));
527 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
528 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
529 new_shortcut = NULL;
530 new_args = gtk_entry_get_text(GTK_ENTRY(arg));
532 if (*new_src == '\0')
533 report_error(
534 _("The location must contain at least one character!"));
535 else
537 icon_set_path(icon, new_src, new_name);
538 icon_set_shortcut(icon, new_shortcut);
539 icon_set_arguments(icon, new_args);
540 g_signal_emit_by_name(icon, "update");
541 gtk_widget_destroy(dialog);
545 static void menu_closed(GtkWidget *widget)
547 appmenu_remove();
548 menu_icon = NULL;
551 /* Called when another application takes the selection away from us */
552 static void lose_selection(GtkClipboard *primary, gpointer data)
554 have_primary = FALSE;
555 icon_select_only(NULL);
558 /* Called when another application wants the contents of our selection */
559 static void selection_get(GtkClipboard *primary,
560 GtkSelectionData *selection_data,
561 guint info,
562 gpointer data)
564 gchar *text;
566 if (info == TARGET_URI_LIST)
567 text = icon_create_uri_list();
568 else
570 GList *next;
571 GString *str;
573 str = g_string_new(NULL);
575 for (next = icon_selection; next; next = next->next)
577 Icon *icon = (Icon *) next->data;
579 g_string_append(str, icon->path);
580 g_string_append_c(str, ' ');
583 text = str->str;
584 g_string_free(str, FALSE);
587 gtk_selection_data_set_text(selection_data, text, strlen(text));
590 static void set_backdrop(gpointer data, guint action, GtkWidget *widget)
592 pinboard_set_backdrop_box();
595 static void remove_items(gpointer data, guint action, GtkWidget *widget)
597 IconClass *iclass;
599 if (menu_icon)
600 icon_set_selected(menu_icon, TRUE);
602 if (!icon_selection)
604 delayed_error(
605 _("You must first select some items to remove"));
606 return;
609 iclass = (IconClass *)
610 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
612 iclass->remove_items();
615 static void file_op(gpointer data, guint action, GtkWidget *widget)
617 if (!menu_icon)
619 delayed_error(_("You must open the menu over an item"));
620 return;
623 switch (action)
625 case ACTION_SHIFT:
626 run_diritem(menu_icon->path, menu_icon->item,
627 NULL, NULL, TRUE);
628 break;
629 case ACTION_EDIT:
630 show_rename_box(menu_icon);
631 break;
632 case ACTION_LOCATION:
633 open_to_show(menu_icon->path);
634 break;
635 case ACTION_PROPERTIES:
636 infobox_new(menu_icon->path);
637 break;
638 case ACTION_RUN_ACTION:
639 if (can_set_run_action(menu_icon->item))
640 type_set_handler_dialog(
641 menu_icon->item->mime_type);
642 else
643 report_error(
644 _("You can only set the run action for a "
645 "regular file"));
646 break;
647 case ACTION_SET_ICON:
648 icon_set_handler_dialog(menu_icon->item,
649 menu_icon->path);
650 break;
654 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
656 if (response == GTK_RESPONSE_OK)
657 rename_activate(dialog);
658 else if (response == GTK_RESPONSE_CANCEL)
659 gtk_widget_destroy(dialog);
662 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
663 GdkEvent *event,
664 gpointer data)
666 XKeyEvent *kev = (XKeyEvent *) xevent;
667 GtkWidget *popup = (GtkWidget *) data;
668 Display *dpy = GDK_DISPLAY();
670 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
671 return GDK_FILTER_CONTINUE;
673 initModifiers();
675 if (kev->type == KeyRelease)
677 gchar *str;
678 KeySym sym;
679 unsigned int m = kev->state;
681 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
682 if (!sym)
683 return GDK_FILTER_CONTINUE;
685 str = g_strdup_printf("%s%s%s%s%s%s%s",
686 m & ControlMask ? "Control+" : "",
687 m & ShiftMask ? "Shift+" : "",
688 m & AltMask ? "Alt+" : "",
689 m & MetaMask ? "Meta+" : "",
690 m & SuperMask ? "Super+" : "",
691 m & HyperMask ? "Hyper+" : "",
692 XKeysymToString(sym));
694 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
697 gdk_window_remove_filter(popup->window, filter_get_key, data);
698 gtk_widget_destroy(popup);
700 return GDK_FILTER_REMOVE;
703 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
705 gchar *str;
707 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
708 if (str)
710 gtk_label_set_text(GTK_LABEL(label), str);
711 g_free(str);
712 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
716 static void get_shortcut(GtkWidget *button, GtkWidget *label)
718 GtkWidget *popup, *frame, *msg;
719 Window xid;
720 Display *dpy = GDK_DISPLAY();
722 popup = gtk_window_new(GTK_WINDOW_POPUP);
724 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
726 frame = gtk_frame_new(NULL);
727 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
728 gtk_container_add(GTK_CONTAINER(popup), frame);
730 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
732 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
733 gtk_container_add(GTK_CONTAINER(frame), msg);
735 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
737 gtk_widget_add_events(popup,
738 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
740 g_signal_connect(popup, "destroy",
741 G_CALLBACK(may_set_shortcut), label);
743 gtk_widget_show_all(popup);
745 gdk_window_add_filter(popup->window, filter_get_key, popup);
747 xid = gdk_x11_drawable_get_xid(popup->window);
749 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
750 gtk_get_current_event_time()) != Success)
752 delayed_error(_("Failed to get keyboard grab!"));
753 gtk_widget_destroy(popup);
756 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
757 GrabModeAsync, xid, None,
758 gtk_get_current_event_time()) != Success)
760 g_warning("Failed to get mouse grab");
764 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
766 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
769 /* Opens a box allowing the user to change the name of a pinned icon.
770 * If the icon is destroyed then the box will close.
771 * If the user chooses OK then the callback is called once the icon's
772 * name, src_path and path fields have been updated and the item field
773 * restatted.
775 static void show_rename_box(Icon *icon)
777 GtkDialog *dialog;
778 GtkWidget *label, *entry, *button, *button2, *hbox, *spacer;
779 GtkBox *vbox;
781 if (icon->dialog)
783 gtk_window_present(GTK_WINDOW(icon->dialog));
784 return;
787 icon->dialog = gtk_dialog_new();
788 g_signal_connect(icon->dialog, "destroy",
789 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
791 dialog = GTK_DIALOG(icon->dialog);
793 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
794 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
795 TRUE, TRUE, 0);
796 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
798 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
799 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
801 label = gtk_label_new(_("Clicking the icon opens:"));
802 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
804 entry = gtk_entry_new();
805 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
806 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
807 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
808 g_signal_connect_swapped(entry, "activate",
809 G_CALLBACK(rename_activate), dialog);
811 label = gtk_label_new(_("Arguments to pass (for executables):"));
812 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
814 entry = gtk_entry_new();
815 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
816 gtk_entry_set_text(GTK_ENTRY(entry), icon->args ? icon->args : "");
817 g_object_set_data(G_OBJECT(dialog), "new_arg", entry);
818 g_signal_connect_swapped(entry, "activate",
819 G_CALLBACK(rename_activate), dialog);
821 spacer = gtk_drawing_area_new();
822 gtk_widget_set_size_request(spacer, 4, 4);
823 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
825 label = gtk_label_new(_("The text displayed under the icon is:"));
826 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
827 entry = gtk_entry_new();
828 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
829 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
830 gtk_widget_grab_focus(entry);
831 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
832 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
834 spacer = gtk_drawing_area_new();
835 gtk_widget_set_size_request(spacer, 4, 4);
836 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
838 label = gtk_label_new(_("The keyboard shortcut is:"));
839 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
841 hbox = gtk_hbox_new(FALSE, 2);
842 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
843 button = gtk_button_new_with_label(icon->shortcut
844 ? icon->shortcut
845 : CLICK_TO_SET);
846 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
847 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
848 GTK_BIN(button)->child);
849 g_signal_connect(button, "clicked",
850 G_CALLBACK(get_shortcut),
851 GTK_BIN(button)->child);
852 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
853 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
854 g_signal_connect(button2, "clicked",
855 G_CALLBACK(clear_shortcut),
856 GTK_BIN(button)->child);
858 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
860 gtk_dialog_add_buttons(dialog,
861 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
862 GTK_STOCK_OK, GTK_RESPONSE_OK,
863 NULL);
864 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
866 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
868 gtk_widget_show_all(GTK_WIDGET(dialog));
871 static gpointer parent_class = NULL;
873 static void icon_finialize(GObject *object)
875 Icon *icon = (Icon *) object;
877 g_return_if_fail(!icon->selected);
879 if (icon->dialog)
880 gtk_widget_destroy(icon->dialog);
882 if (icon == menu_icon)
883 menu_icon = NULL;
885 icon_set_path(icon, NULL, NULL);
886 icon_set_shortcut(icon, NULL);
887 icon_set_arguments(icon, NULL);
889 G_OBJECT_CLASS(parent_class)->finalize(object);
892 static void icon_class_init(gpointer gclass, gpointer data)
894 GObjectClass *object = (GObjectClass *) gclass;
895 IconClass *icon = (IconClass *) gclass;
897 parent_class = g_type_class_peek_parent(gclass);
899 object->finalize = icon_finialize;
900 icon->destroy = NULL;
901 icon->redraw = NULL;
902 icon->update = NULL;
903 icon->same_group = NULL;
904 icon->wink = NULL;
906 g_signal_new("update",
907 G_TYPE_FROM_CLASS(gclass),
908 G_SIGNAL_RUN_LAST,
909 G_STRUCT_OFFSET(IconClass, update),
910 NULL, NULL,
911 g_cclosure_marshal_VOID__VOID,
912 G_TYPE_NONE, 0);
914 g_signal_new("destroy",
915 G_TYPE_FROM_CLASS(gclass),
916 G_SIGNAL_RUN_LAST,
917 G_STRUCT_OFFSET(IconClass, destroy),
918 NULL, NULL,
919 g_cclosure_marshal_VOID__VOID,
920 G_TYPE_NONE, 0);
922 g_signal_new("redraw",
923 G_TYPE_FROM_CLASS(gclass),
924 G_SIGNAL_RUN_LAST,
925 G_STRUCT_OFFSET(IconClass, redraw),
926 NULL, NULL,
927 g_cclosure_marshal_VOID__VOID,
928 G_TYPE_NONE, 0);
930 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
933 static void icon_init(GTypeInstance *object, gpointer gclass)
935 Icon *icon = (Icon *) object;
937 icon->selected = FALSE;
938 icon->src_path = NULL;
939 icon->path = NULL;
940 icon->item = NULL;
941 icon->dialog = NULL;
942 icon->shortcut = NULL;
943 icon->shortcut_key.keycode = 0;
944 icon->shortcut_key.modifier = 0;
945 icon->args = NULL;
948 /* As icon_set_selected(), but doesn't automatically unselect incompatible
949 * icons.
951 static void icon_set_selected_int(Icon *icon, gboolean selected)
953 static GtkClipboard *primary;
955 g_return_if_fail(icon != NULL);
957 if (icon->selected == selected)
958 return;
960 if (!primary)
961 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
963 if (selected)
965 icon_selection = g_list_prepend(icon_selection, icon);
966 if (!have_primary)
968 GtkTargetEntry target_table[] =
970 {"text/uri-list", 0, TARGET_URI_LIST},
971 {"UTF8", 0, TARGET_STRING},
972 {"COMPOUND_TEXT", 0, TARGET_STRING},
973 {"STRING", 0, TARGET_STRING},
976 /* Grab selection */
977 have_primary = gtk_clipboard_set_with_data(primary,
978 target_table,
979 sizeof(target_table) / sizeof(*target_table),
980 selection_get, lose_selection, NULL);
983 else
985 icon_selection = g_list_remove(icon_selection, icon);
986 if (have_primary && !icon_selection)
988 have_primary = FALSE;
989 gtk_clipboard_clear(primary);
993 icon->selected = selected;
994 g_signal_emit_by_name(icon, "redraw");
997 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n)
999 GList *items, *item;
1001 items = gtk_container_get_children(GTK_CONTAINER(menu));
1003 item = g_list_nth(items, from);
1004 while (item && n--)
1006 if (hidden)
1007 gtk_widget_hide(GTK_WIDGET(item->data));
1008 else
1009 gtk_widget_show(GTK_WIDGET(item->data));
1010 item = item->next;
1012 g_list_free(items);
1015 /* Stolen from xfwm4 */
1016 static void initModifiers(void)
1018 static gboolean need_init = TRUE;
1019 Display *dpy = GDK_DISPLAY();
1020 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1021 int m, k;
1023 if (!need_init)
1024 return;
1025 need_init = FALSE;
1027 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1028 SuperMask = HyperMask = 0;
1030 /* Work out which mask to use for each modifier group */
1031 if (xmk)
1033 KeyCode *c = xmk->modifiermap;
1034 KeyCode numLockKeyCode;
1035 KeyCode scrollLockKeyCode;
1036 KeyCode capsLockKeyCode;
1037 KeyCode altKeyCode;
1038 KeyCode metaKeyCode;
1039 KeyCode superKeyCode;
1040 KeyCode hyperKeyCode;
1042 /* Find the codes to search for... */
1043 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1044 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1045 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1046 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1047 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1048 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1049 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1051 /* If some are missing, try alternatives... */
1052 if (!altKeyCode)
1053 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1054 if (!metaKeyCode)
1055 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1056 if (!superKeyCode)
1057 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1058 if (!hyperKeyCode)
1059 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1061 /* Check each of the eight modifier lists.
1062 * The idea (I think) is that we name the modifier group which
1063 * includes the Alt key as the 'Alt group', and so on for
1064 * the other modifiers.
1066 for (m = 0; m < 8; m++)
1068 for (k = 0; k < xmk->max_keypermod; k++, c++)
1070 if (*c == NoSymbol)
1071 continue;
1072 if (*c == numLockKeyCode)
1073 NumLockMask = (1 << m);
1074 if (*c == scrollLockKeyCode)
1075 ScrollLockMask = (1 << m);
1076 if (*c == capsLockKeyCode)
1077 CapsLockMask = (1 << m);
1078 if (*c == altKeyCode)
1079 AltMask = (1 << m);
1080 if (*c == metaKeyCode)
1081 MetaMask = (1 << m);
1082 if (*c == superKeyCode)
1083 SuperMask = (1 << m);
1084 if (*c == hyperKeyCode)
1085 HyperMask = (1 << m);
1088 XFreeModifiermap(xmk);
1091 if(MetaMask == AltMask)
1092 MetaMask = 0;
1094 if (AltMask != 0 && MetaMask == Mod1Mask)
1096 MetaMask = AltMask;
1097 AltMask = Mod1Mask;
1100 if (AltMask == 0 && MetaMask != 0)
1102 if (MetaMask != Mod1Mask)
1103 AltMask = Mod1Mask;
1104 else
1106 AltMask = MetaMask;
1107 MetaMask = 0;
1111 if (AltMask == 0)
1112 AltMask = Mod1Mask;
1116 /* Fill in key from str. Sets keycode to zero if str is NULL.
1117 * Stolen from xfwm4 and modified. Call initModifiers before this.
1119 static void parseKeyString(MyKey *key, const char *str)
1121 char *k;
1122 Display *dpy = GDK_DISPLAY();
1124 key->keycode = 0;
1125 key->modifier = 0;
1127 if (!str)
1128 return;
1130 k = strrchr(str, '+');
1131 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1132 if (k)
1134 gchar *tmp;
1136 tmp = g_ascii_strdown(str, -1);
1138 if (strstr(tmp, "shift"))
1139 key->modifier = key->modifier | ShiftMask;
1140 if (strstr(tmp, "control"))
1141 key->modifier = key->modifier | ControlMask;
1142 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1143 key->modifier = key->modifier | AltMask;
1144 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1145 key->modifier = key->modifier | MetaMask;
1146 if (strstr(tmp, "hyper"))
1147 key->modifier = key->modifier | HyperMask;
1148 if (strstr(tmp, "super"))
1149 key->modifier = key->modifier | SuperMask;
1151 g_free(tmp);
1154 if (!key->keycode)
1155 g_warning("Can't parse key '%s'\n", str);
1158 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1159 GdkEvent *event,
1160 gpointer data)
1162 GList *next;
1163 XKeyEvent *kev = (XKeyEvent *) xevent;
1164 guint state;
1166 if (kev->type != KeyPress)
1167 return GDK_FILTER_CONTINUE;
1169 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask |
1170 HyperMask | SuperMask);
1172 for (next = icon_shortcuts; next; next = next->next)
1174 Icon *icon = (Icon *) next->data;
1176 if (icon->shortcut_key.keycode == kev->keycode &&
1177 icon->shortcut_key.modifier == state)
1179 icon_wink(icon);
1180 icon_run(icon);
1184 return GDK_FILTER_CONTINUE;
1187 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1188 root, False, GrabModeAsync, GrabModeAsync)
1189 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1190 root)
1192 static guint mykey_hash(gconstpointer key)
1194 MyKey *k = (MyKey *) key;
1196 return (k->keycode << 8) + k->modifier;
1199 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1201 MyKey *ka = (MyKey *) a;
1202 MyKey *kb = (MyKey *) b;
1204 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1207 /* Stolen from xfwm4 and modified.
1208 * FALSE on error. Call initModifiers before this.
1210 static gboolean grabKey(MyKey *key)
1212 Window root;
1213 Display *dpy = GDK_DISPLAY();
1214 static gboolean need_init = TRUE;
1216 if (need_init)
1218 need_init = FALSE;
1219 gdk_window_add_filter(gdk_get_default_root_window(),
1220 filter_keys, NULL);
1223 gdk_error_trap_push();
1225 root = GDK_ROOT_WINDOW();
1227 GRAB(key, 0);
1229 /* Here we grab all combinations of well known modifiers */
1230 GRAB(key, ScrollLockMask);
1231 GRAB(key, NumLockMask);
1232 GRAB(key, CapsLockMask);
1233 GRAB(key, ScrollLockMask | NumLockMask);
1234 GRAB(key, ScrollLockMask | CapsLockMask);
1235 GRAB(key, CapsLockMask | NumLockMask);
1236 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1238 gdk_flush();
1239 return gdk_error_trap_pop() == Success;
1242 static gboolean ungrabKey(MyKey *key)
1244 Window root = GDK_ROOT_WINDOW();
1245 Display *dpy = GDK_DISPLAY();
1247 gdk_error_trap_push();
1249 UNGRAB(key, 0);
1251 UNGRAB(key, ScrollLockMask);
1252 UNGRAB(key, NumLockMask);
1253 UNGRAB(key, CapsLockMask);
1254 UNGRAB(key, ScrollLockMask | NumLockMask);
1255 UNGRAB(key, ScrollLockMask | CapsLockMask);
1256 UNGRAB(key, CapsLockMask | NumLockMask);
1257 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1259 gdk_flush();
1260 return gdk_error_trap_pop() == Success;
1263 static void ungrab_key(Icon *icon)
1265 int *count;
1267 g_return_if_fail(icon != NULL);
1269 if (!icon->shortcut_key.keycode)
1270 return;
1272 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1274 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1275 g_return_if_fail(count != NULL);
1277 (*count)--;
1279 if (*count > 0)
1280 return;
1282 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1284 if (!ungrabKey(&icon->shortcut_key))
1285 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1286 icon->shortcut, icon->item->leafname);
1289 static void grab_key(Icon *icon)
1291 MyKey *hash_key;
1292 int *count;
1294 g_return_if_fail(icon != NULL);
1296 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1298 if (!icon->shortcut_key.keycode)
1299 return;
1301 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1303 if (!grab_counter)
1304 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1305 g_free, NULL);
1307 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1308 if (count)
1310 (*count)++;
1311 return; /* Already grabbed */
1314 hash_key = g_new(MyKey, 1);
1315 *hash_key = icon->shortcut_key;
1316 count = g_new(int, 1);
1317 *count = 1;
1318 g_hash_table_insert(grab_counter, hash_key, count);
1320 if (!grabKey(&icon->shortcut_key))
1321 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1322 "Some other application may be already using it!\n",
1323 icon->shortcut, icon->item->leafname);
1327 static void icon_wink(Icon *icon)
1329 IconClass *iclass;
1331 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1332 g_return_if_fail(iclass->wink != NULL);
1334 iclass->wink(icon);
1337 /* Sets icon_menu */
1338 static void create_menu(void)
1340 GList *items;
1341 guchar *tmp;
1342 GtkItemFactory *item_factory;
1344 g_return_if_fail(icon_menu == NULL);
1346 item_factory = menu_create(menu_def,
1347 sizeof(menu_def) / sizeof(*menu_def),
1348 "<icon>", NULL);
1350 tmp = g_strconcat("<icon>/", _("File"), NULL);
1351 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
1352 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
1353 g_free(tmp);
1355 /* File '' label... */
1356 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
1357 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
1358 g_list_free(items);
1360 /* Shift Open... label */
1361 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
1362 file_shift_item = GTK_BIN(items->data)->child;
1363 g_list_free(items);
1365 g_signal_connect(icon_menu, "unmap_event",
1366 G_CALLBACK(menu_closed), NULL);