r4895: New MIME type for Jar archives.
[rox-filer.git] / ROX-Filer / src / icon.c
blobe70465bec17e08770c52a92a593b087cc183b7ea
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* icon.c - abstract base class for pinboard and panel icons.
22 * An Icon contains the full pathname of its file and the DirItem for that
23 * file. Icons emit the following signals:
25 * redraw - the image, details or selection state have changed.
26 * update - the name or path has changed.
27 * destroy - someone wishes to remove the icon.
29 * Note that an icon may be removed without emitting 'destroy'.
32 #include "config.h"
34 #include <string.h>
35 #include <gtk/gtk.h>
36 #include <X11/keysym.h>
37 #include <gdk/gdkx.h>
39 #include "global.h"
41 #include "main.h"
42 #include "gui_support.h"
43 #include "support.h"
44 #include "icon.h"
45 #include "diritem.h"
46 #include "menu.h"
47 #include "appmenu.h"
48 #include "dnd.h"
49 #include "run.h"
50 #include "infobox.h"
51 #include "pixmaps.h"
52 #include "mount.h"
53 #include "type.h"
54 #include "usericons.h"
55 #include "pinboard.h" /* For pinboard_set_backdrop_box */
57 static gboolean have_primary = FALSE; /* We own the PRIMARY selection? */
59 GtkWidget *icon_menu; /* The popup icon menu */
60 static GtkWidget *icon_file_menu; /* The file submenu */
61 static GtkWidget *icon_file_item; /* 'File' label */
62 static GtkWidget *file_shift_item; /* 'Shift Open' label */
63 static GtkWidget *current_options_item; /* Pin/Pan Options */
65 /* A list of selected Icons. Every icon in the list is from the same group
66 * (eg, you can't have icons from two different panels selected at the
67 * same time).
69 GList *icon_selection = NULL;
71 /* A list of Icons which have grabs in effect. The same combo may be
72 * listed more than once, but has only one X grab.
74 GList *icon_shortcuts = NULL;
76 #define CLICK_TO_SET _("(click to set)")
78 static unsigned int AltMask;
79 static unsigned int MetaMask;
80 static unsigned int NumLockMask;
81 static unsigned int ScrollLockMask;
82 static unsigned int CapsLockMask;
83 static unsigned int SuperMask;
84 static unsigned int HyperMask;
86 /* {MyKey -> Number of grabs} */
87 static GHashTable *grab_counter = NULL;
89 /* Each entry is a GList of Icons which have the given pathname.
90 * This allows us to update all necessary icons when something changes.
92 static GHashTable *icons_hash = NULL; /* path -> [Icon] */
94 static Icon *menu_icon = NULL; /* Item clicked if there is no selection */
96 /* Static prototypes */
97 static void rename_activate(GtkWidget *dialog);
98 static void menu_closed(GtkWidget *widget);
99 static void lose_selection(GtkClipboard *primary, gpointer data);
100 static void selection_get(GtkClipboard *primary,
101 GtkSelectionData *selection_data,
102 guint info,
103 gpointer data);
104 static void remove_items(gpointer data, guint action, GtkWidget *widget);
105 static void file_op(gpointer data, guint action, GtkWidget *widget);
106 static void show_rename_box(Icon *icon);
107 static void icon_set_selected_int(Icon *icon, gboolean selected);
108 static void icon_class_init(gpointer gclass, gpointer data);
109 static void icon_init(GTypeInstance *object, gpointer gclass);
110 static void icon_hash_path(Icon *icon);
111 static void icon_unhash_path(Icon *icon);
112 static void ungrab_key(Icon *icon);
113 static void grab_key(Icon *icon);
114 static void parseKeyString(MyKey *key, const char *str);
115 static void icon_wink(Icon *icon);
116 static void initModifiers(void);
117 static void create_menu(void);
119 enum {
120 ACTION_SHIFT,
121 ACTION_PROPERTIES,
122 ACTION_RUN_ACTION,
123 ACTION_SET_ICON,
124 ACTION_EDIT,
125 ACTION_LOCATION,
128 #undef N_
129 #define N_(x) x
130 static GtkItemFactoryEntry menu_def[] = {
131 {N_("ROX-Filer"), NULL, NULL, 0, "<Branch>"},
132 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, "<StockItem>", GTK_STOCK_DIALOG_INFO},
133 {">" N_("Show Help Files"), NULL, menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
134 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
135 {">", NULL, NULL, 0, "<Separator>"},
136 {">" N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
137 {">" N_("Home Directory"), NULL, open_home, 0, "<StockItem>", GTK_STOCK_HOME},
138 {N_("File"), NULL, NULL, 0, "<Branch>"},
139 {">" N_("Shift Open"), NULL, file_op, ACTION_SHIFT, NULL},
140 {">" N_("Properties"), NULL, file_op, ACTION_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
141 {">" N_("Set Run Action..."), NULL, file_op, ACTION_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
142 {">" N_("Set Icon..."), NULL, file_op, ACTION_SET_ICON, NULL},
143 {N_("Edit Item"), NULL, file_op, ACTION_EDIT, "<StockItem>", GTK_STOCK_PROPERTIES},
144 {N_("Show Location"), NULL, file_op, ACTION_LOCATION, "<StockItem>", GTK_STOCK_JUMP_TO},
145 {N_("Remove Item(s)"), NULL, remove_items, 0, "<StockItem>", GTK_STOCK_REMOVE},
146 {"", NULL, NULL, 0, "<Separator>"},
149 /****************************************************************
150 * EXTERNAL INTERFACE *
151 ****************************************************************/
153 /* Called when the pointer moves over the icon */
154 void icon_may_update(Icon *icon)
156 MaskedPixmap *image;
157 int flags;
159 g_return_if_fail(icon != NULL);
161 image = di_image(icon->item);
162 flags = icon->item->flags;
164 if (image)
165 g_object_ref(image);
166 mount_update(FALSE);
167 diritem_restat(icon->path, icon->item, NULL);
169 if (di_image(icon->item) != image || icon->item->flags != flags)
171 /* Appearance changed; need to redraw */
172 g_signal_emit_by_name(icon, "redraw");
175 if (image)
176 g_object_unref(image);
179 /* If path is on an icon then it may have changed... check! */
180 void icons_may_update(const gchar *path)
182 GList *affected;
184 if (icons_hash)
186 affected = g_hash_table_lookup(icons_hash, path);
188 for (; affected; affected = affected->next)
189 icon_may_update((Icon *) affected->data);
193 typedef struct _CheckData CheckData;
194 struct _CheckData {
195 const gchar *path;
196 gboolean found;
199 static void check_has(gpointer key, GList *icons, CheckData *check)
201 Icon *icon;
203 g_return_if_fail(icons != NULL);
205 icon = icons->data;
207 if (is_sub_dir(icon->path, check->path))
208 check->found = TRUE;
211 /* Returns TRUE if any icon links to this file (or any file inside
212 * this directory). Used to check that it's OK to delete 'path'.
214 gboolean icons_require(const gchar *path)
216 CheckData check;
218 if (!icons_hash)
219 return FALSE;
221 check.path = path;
222 check.found = FALSE;
223 g_hash_table_foreach(icons_hash, (GHFunc) check_has, &check);
225 return check.found;
228 static gboolean any_selected_item_is_locked()
230 GList *next;
232 for (next = icon_selection; next; next = next->next)
234 Icon *icon = (Icon *)next->data;
236 if (icon->locked)
237 return TRUE;
240 return FALSE;
243 /* Menu was clicked over this icon. Set things up correctly (shade items,
244 * add app menu stuff, etc).
245 * You should show icon_menu after calling this...
246 * panel_name is NULL for the pinboard.
248 void icon_prepare_menu(Icon *icon, GtkWidget *options_item)
250 GtkWidget *image;
251 gboolean shaded;
253 appmenu_remove();
255 if (current_options_item)
257 gtk_widget_destroy(current_options_item);
258 current_options_item = NULL;
261 menu_icon = icon;
263 if (!icon_menu)
264 create_menu();
266 current_options_item = options_item;
267 image = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES,
268 GTK_ICON_SIZE_MENU);
269 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(options_item), image);
271 gtk_menu_shell_append(GTK_MENU_SHELL(icon_menu), options_item);
272 gtk_widget_show_all(options_item);
274 /* Shade Remove Item(s) if any item is locked or nothing is selected */
275 if (icon_selection)
276 shaded = any_selected_item_is_locked();
277 else if (menu_icon)
278 shaded = menu_icon->locked;
279 else
280 shaded = TRUE;
282 menu_set_items_shaded(icon_menu, shaded, 4, 1);
284 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
285 FALSE);
287 /* Shade the File/Edit/Show items unless an item was clicked */
288 if (icon)
290 guchar *tmp;
292 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
293 menu_set_items_shaded(icon_file_menu, FALSE, 0, 5);
294 if (!can_set_run_action(icon->item))
295 menu_set_items_shaded(icon_file_menu, TRUE, 2, 1);
297 tmp = g_strdup_printf(_("%s '%s'"),
298 basetype_name(icon->item),
299 icon->item->leafname);
300 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
301 g_free(tmp);
303 /* Check for app-specific menu */
304 appmenu_add(icon->path, icon->item, icon_menu);
306 else
308 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
309 menu_set_items_shaded(icon_file_menu, TRUE, 0, 5);
310 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
314 /* Set whether this icon is selected. Will automatically clear the selection
315 * if it contains icons from a different group.
317 void icon_set_selected(Icon *icon, gboolean selected)
319 if (selected && icon_selection)
321 IconClass *iclass;
322 Icon *other = (Icon *) icon_selection->data;
324 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
325 if (!iclass->same_group(icon, other))
327 icon_select_only(icon);
328 return;
332 icon_set_selected_int(icon, selected);
335 /* Clear everything, except 'select', which is selected.
336 * If select is NULL, unselects everything.
338 void icon_select_only(Icon *select)
340 GList *to_clear, *next;
342 if (select)
343 icon_set_selected_int(select, TRUE);
345 to_clear = g_list_copy(icon_selection);
347 if (select)
348 to_clear = g_list_remove(to_clear, select);
350 for (next = to_clear; next; next = next->next)
351 icon_set_selected_int((Icon *) next->data, FALSE);
353 g_list_free(to_clear);
356 /* Destroys this icon's widget, causing it to be removed from the screen */
357 void icon_destroy(Icon *icon)
359 g_return_if_fail(icon != NULL);
361 icon_set_selected_int(icon, FALSE);
363 if (!icon->locked)
364 g_signal_emit_by_name(icon, "destroy");
367 /* Return a text/uri-list of all the icons in the selection */
368 gchar *icon_create_uri_list(void)
370 GString *tmp;
371 guchar *retval;
372 GList *next;
374 tmp = g_string_new(NULL);
376 for (next = icon_selection; next; next = next->next)
378 Icon *icon = (Icon *) next->data;
379 EscapedPath *uri;
381 uri = encode_path_as_uri(icon->path);
382 g_string_append(tmp, (char *) uri);
383 g_free(uri);
384 g_string_append(tmp, "\r\n");
387 retval = tmp->str;
388 g_string_free(tmp, FALSE);
390 return retval;
393 GType icon_get_type(void)
395 static GType type = 0;
397 if (!type)
399 static const GTypeInfo info =
401 sizeof (IconClass),
402 NULL, /* base_init */
403 NULL, /* base_finalise */
404 icon_class_init,
405 NULL, /* class_finalise */
406 NULL, /* class_data */
407 sizeof(Icon),
408 0, /* n_preallocs */
409 icon_init
412 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
413 &info, 0);
416 return type;
419 /* Sets, unsets or changes the pathname and name for an icon.
420 * Updates icons_hash and (re)stats the item.
421 * If name is NULL then gets the leafname from pathname.
422 * If pathname is NULL, frees everything.
424 void icon_set_path(Icon *icon, const char *pathname, const char *name)
426 if (icon->path)
428 icon_unhash_path(icon);
429 icon->src_path = NULL;
430 icon->path = NULL;
432 diritem_free(icon->item);
433 icon->item = NULL;
436 if (pathname)
438 if (g_utf8_validate(pathname, -1, NULL))
439 icon->src_path = g_strdup(pathname);
440 else
441 icon->src_path = to_utf8(pathname);
442 icon->path = expand_path(icon->src_path);
444 icon_hash_path(icon);
446 if (!name)
447 name = g_basename(icon->src_path);
449 icon->item = diritem_new(name);
450 diritem_restat(icon->path, icon->item, NULL);
454 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
456 g_return_if_fail(icon != NULL);
458 if (shortcut && !*shortcut)
459 shortcut = NULL;
460 if (icon->shortcut == shortcut)
461 return;
462 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
463 return;
465 initModifiers();
467 ungrab_key(icon);
469 g_free(icon->shortcut);
470 icon->shortcut = g_strdup(shortcut);
471 parseKeyString(&icon->shortcut_key, shortcut);
473 grab_key(icon);
476 void icon_set_arguments(Icon *icon, const gchar *args)
478 g_return_if_fail(icon != NULL);
479 g_return_if_fail(args == NULL || icon->args != args);
481 if (args && !*args)
482 args = NULL;
483 if (icon->args && args && strcmp(icon->args, args) == 0)
484 return;
486 g_free(icon->args);
487 icon->args = g_strdup(args);
490 void icon_run(Icon *icon)
492 if (icon->args == NULL)
493 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
494 else
495 run_with_args(icon->path, icon->item, icon->args);
498 /****************************************************************
499 * INTERNAL FUNCTIONS *
500 ****************************************************************/
502 /* The icons_hash table allows us to convert from a path to a list
503 * of icons that use that path.
504 * Add this icon to the list for its path.
506 static void icon_hash_path(Icon *icon)
508 GList *list;
510 g_return_if_fail(icon != NULL);
512 /* g_print("[ hashing '%s' ]\n", icon->path); */
514 list = g_hash_table_lookup(icons_hash, icon->path);
515 list = g_list_prepend(list, icon);
516 g_hash_table_insert(icons_hash, icon->path, list);
519 /* Remove this icon from the icons_hash table */
520 static void icon_unhash_path(Icon *icon)
522 GList *list;
524 g_return_if_fail(icon != NULL);
526 /* g_print("[ unhashing '%s' ]\n", icon->path); */
528 list = g_hash_table_lookup(icons_hash, icon->path);
529 g_return_if_fail(list != NULL);
531 list = g_list_remove(list, icon);
533 /* Remove it first; the hash key may have changed address */
534 g_hash_table_remove(icons_hash, icon->path);
535 if (list)
536 g_hash_table_insert(icons_hash,
537 ((Icon *) list->data)->path, list);
540 static void rename_activate(GtkWidget *dialog)
542 GtkWidget *entry, *src, *shortcut, *arg, *lock_state;
543 Icon *icon;
544 const guchar *new_name, *new_src, *new_shortcut, *new_args;
545 gboolean new_lock_state;
547 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
548 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
549 src = g_object_get_data(G_OBJECT(dialog), "new_path");
550 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
551 arg = g_object_get_data(G_OBJECT(dialog), "new_arg");
552 lock_state = g_object_get_data(G_OBJECT(dialog), "new_lock_state");
554 g_return_if_fail(entry != NULL &&
555 src != NULL &&
556 icon != NULL &&
557 shortcut != NULL &&
558 arg != NULL &&
559 lock_state != NULL);
561 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
562 new_src = gtk_entry_get_text(GTK_ENTRY(src));
563 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
564 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
565 new_shortcut = NULL;
566 new_args = gtk_entry_get_text(GTK_ENTRY(arg));
567 new_lock_state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(lock_state));
569 if (*new_src == '\0')
570 report_error(
571 _("The location must contain at least one character!"));
572 else
574 icon_set_path(icon, new_src, new_name);
575 icon_set_shortcut(icon, new_shortcut);
576 icon_set_arguments(icon, new_args);
577 icon->locked = new_lock_state;
578 g_signal_emit_by_name(icon, "update");
579 gtk_widget_destroy(dialog);
583 static void menu_closed(GtkWidget *widget)
585 appmenu_remove();
586 menu_icon = NULL;
589 /* Called when another application takes the selection away from us */
590 static void lose_selection(GtkClipboard *primary, gpointer data)
592 have_primary = FALSE;
593 icon_select_only(NULL);
596 /* Called when another application wants the contents of our selection */
597 static void selection_get(GtkClipboard *primary,
598 GtkSelectionData *selection_data,
599 guint info,
600 gpointer data)
602 gchar *text;
604 if (info == TARGET_URI_LIST)
605 text = icon_create_uri_list();
606 else
608 GList *next;
609 GString *str;
611 str = g_string_new(NULL);
613 for (next = icon_selection; next; next = next->next)
615 Icon *icon = (Icon *) next->data;
617 g_string_append(str, icon->path);
618 g_string_append_c(str, ' ');
621 text = str->str;
622 g_string_free(str, FALSE);
625 gtk_selection_data_set_text(selection_data, text, strlen(text));
628 static void remove_items(gpointer data, guint action, GtkWidget *widget)
630 IconClass *iclass;
632 if (menu_icon)
634 if (menu_icon->locked)
636 delayed_error(_("You must unlock '%s' before removing it"),
637 menu_icon->item->leafname);
638 return;
640 icon_set_selected(menu_icon, TRUE);
643 if (!icon_selection)
645 delayed_error(
646 _("You must first select some items to remove"));
647 return;
650 if (any_selected_item_is_locked())
652 delayed_error(_("An item must be unlocked before it can be removed."));
653 return;
656 iclass = (IconClass *)
657 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
659 iclass->remove_items();
662 static void file_op(gpointer data, guint action, GtkWidget *widget)
664 if (!menu_icon)
666 delayed_error(_("You must open the menu over an item"));
667 return;
670 switch (action)
672 case ACTION_SHIFT:
673 run_diritem(menu_icon->path, menu_icon->item,
674 NULL, NULL, TRUE);
675 break;
676 case ACTION_EDIT:
677 show_rename_box(menu_icon);
678 break;
679 case ACTION_LOCATION:
680 open_to_show(menu_icon->path);
681 break;
682 case ACTION_PROPERTIES:
683 infobox_new(menu_icon->path);
684 break;
685 case ACTION_RUN_ACTION:
686 if (can_set_run_action(menu_icon->item))
687 type_set_handler_dialog(
688 menu_icon->item->mime_type);
689 else
690 report_error(
691 _("You can only set the run action for a "
692 "regular file"));
693 break;
694 case ACTION_SET_ICON:
695 icon_set_handler_dialog(menu_icon->item,
696 menu_icon->path);
697 break;
701 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
703 if (response == GTK_RESPONSE_OK)
704 rename_activate(dialog);
705 else if (response == GTK_RESPONSE_CANCEL)
706 gtk_widget_destroy(dialog);
709 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
710 GdkEvent *event,
711 gpointer data)
713 XKeyEvent *kev = (XKeyEvent *) xevent;
714 GtkWidget *popup = (GtkWidget *) data;
715 Display *dpy = GDK_DISPLAY();
717 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
718 return GDK_FILTER_CONTINUE;
720 initModifiers();
722 if (kev->type == KeyRelease)
724 gchar *str;
725 KeySym sym;
726 unsigned int m = kev->state;
728 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
729 if (!sym)
730 return GDK_FILTER_CONTINUE;
732 str = g_strdup_printf("%s%s%s%s%s%s%s",
733 m & ControlMask ? "Control+" : "",
734 m & ShiftMask ? "Shift+" : "",
735 m & AltMask ? "Alt+" : "",
736 m & MetaMask ? "Meta+" : "",
737 m & SuperMask ? "Super+" : "",
738 m & HyperMask ? "Hyper+" : "",
739 XKeysymToString(sym));
741 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
744 gdk_window_remove_filter(popup->window, filter_get_key, data);
745 gtk_widget_destroy(popup);
747 return GDK_FILTER_REMOVE;
750 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
752 gchar *str;
754 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
755 if (str)
757 gtk_label_set_text(GTK_LABEL(label), str);
758 g_free(str);
759 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
763 static void get_shortcut(GtkWidget *button, GtkWidget *label)
765 GtkWidget *popup, *frame, *msg;
766 Window xid;
767 Display *dpy = GDK_DISPLAY();
769 popup = gtk_window_new(GTK_WINDOW_POPUP);
771 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
773 frame = gtk_frame_new(NULL);
774 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
775 gtk_container_add(GTK_CONTAINER(popup), frame);
777 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
779 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
780 gtk_container_add(GTK_CONTAINER(frame), msg);
782 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
784 gtk_widget_add_events(popup,
785 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
787 g_signal_connect(popup, "destroy",
788 G_CALLBACK(may_set_shortcut), label);
790 gtk_widget_show_all(popup);
792 gdk_window_add_filter(popup->window, filter_get_key, popup);
794 xid = gdk_x11_drawable_get_xid(popup->window);
796 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
797 gtk_get_current_event_time()) != Success)
799 delayed_error(_("Failed to get keyboard grab!"));
800 gtk_widget_destroy(popup);
803 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
804 GrabModeAsync, xid, None,
805 gtk_get_current_event_time()) != Success)
807 g_warning("Failed to get mouse grab");
811 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
813 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
816 /* Opens a box allowing the user to change the name of a pinned icon.
817 * If the icon is destroyed then the box will close.
818 * If the user chooses OK then the callback is called once the icon's
819 * name, src_path and path fields have been updated and the item field
820 * restatted.
822 static void show_rename_box(Icon *icon)
824 GtkDialog *dialog;
825 GtkWidget *label, *entry, *button, *button2, *hbox, *spacer, *lock_state;
826 GtkBox *vbox;
828 if (icon->dialog)
830 gtk_window_present(GTK_WINDOW(icon->dialog));
831 return;
834 icon->dialog = gtk_dialog_new();
835 gtk_dialog_set_has_separator(GTK_DIALOG(icon->dialog), FALSE);
836 g_signal_connect(icon->dialog, "destroy",
837 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
839 dialog = GTK_DIALOG(icon->dialog);
841 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
842 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
843 TRUE, TRUE, 0);
844 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
846 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
847 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
849 label = gtk_label_new(_("Clicking the icon opens:"));
850 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
852 entry = gtk_entry_new();
853 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
854 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
855 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
856 g_signal_connect_swapped(entry, "activate",
857 G_CALLBACK(rename_activate), dialog);
859 label = gtk_label_new(_("Arguments to pass (for executables):"));
860 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
862 entry = gtk_entry_new();
863 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
864 gtk_entry_set_text(GTK_ENTRY(entry), icon->args ? icon->args : "");
865 g_object_set_data(G_OBJECT(dialog), "new_arg", entry);
866 g_signal_connect_swapped(entry, "activate",
867 G_CALLBACK(rename_activate), dialog);
869 spacer = gtk_drawing_area_new();
870 gtk_widget_set_size_request(spacer, 4, 4);
871 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
873 label = gtk_label_new(_("The text displayed under the icon is:"));
874 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
875 entry = gtk_entry_new();
876 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
877 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
878 gtk_widget_grab_focus(entry);
879 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
880 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
882 spacer = gtk_drawing_area_new();
883 gtk_widget_set_size_request(spacer, 4, 4);
884 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
886 label = gtk_label_new(_("The keyboard shortcut is:"));
887 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
889 hbox = gtk_hbox_new(FALSE, 2);
890 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
891 button = gtk_button_new_with_label(icon->shortcut
892 ? icon->shortcut
893 : CLICK_TO_SET);
894 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
895 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
896 GTK_BIN(button)->child);
897 g_signal_connect(button, "clicked",
898 G_CALLBACK(get_shortcut),
899 GTK_BIN(button)->child);
900 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
901 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
902 g_signal_connect(button2, "clicked",
903 G_CALLBACK(clear_shortcut),
904 GTK_BIN(button)->child);
906 lock_state = gtk_check_button_new_with_label(_("Locked"));
907 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lock_state), icon->locked);
908 gtk_box_pack_start(vbox, lock_state, TRUE, TRUE, 0);
909 g_object_set_data(G_OBJECT(dialog), "new_lock_state", lock_state);
910 gtk_tooltips_set_tip(tooltips, lock_state,
911 _("Locking an item prevents it from being accidentally removed"),
912 NULL);
914 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
916 gtk_dialog_add_buttons(dialog,
917 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
918 GTK_STOCK_OK, GTK_RESPONSE_OK,
919 NULL);
920 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
922 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
924 gtk_widget_show_all(GTK_WIDGET(dialog));
927 static gpointer parent_class = NULL;
929 static void icon_finialize(GObject *object)
931 Icon *icon = (Icon *) object;
933 g_return_if_fail(!icon->selected);
935 if (icon->dialog)
936 gtk_widget_destroy(icon->dialog);
938 if (icon == menu_icon)
939 menu_icon = NULL;
941 icon_set_path(icon, NULL, NULL);
942 icon_set_shortcut(icon, NULL);
943 icon_set_arguments(icon, NULL);
945 G_OBJECT_CLASS(parent_class)->finalize(object);
948 static void icon_class_init(gpointer gclass, gpointer data)
950 GObjectClass *object = (GObjectClass *) gclass;
951 IconClass *icon = (IconClass *) gclass;
953 parent_class = g_type_class_peek_parent(gclass);
955 object->finalize = icon_finialize;
956 icon->destroy = NULL;
957 icon->redraw = NULL;
958 icon->update = NULL;
959 icon->same_group = NULL;
960 icon->wink = NULL;
962 g_signal_new("update",
963 G_TYPE_FROM_CLASS(gclass),
964 G_SIGNAL_RUN_LAST,
965 G_STRUCT_OFFSET(IconClass, update),
966 NULL, NULL,
967 g_cclosure_marshal_VOID__VOID,
968 G_TYPE_NONE, 0);
970 g_signal_new("destroy",
971 G_TYPE_FROM_CLASS(gclass),
972 G_SIGNAL_RUN_LAST,
973 G_STRUCT_OFFSET(IconClass, destroy),
974 NULL, NULL,
975 g_cclosure_marshal_VOID__VOID,
976 G_TYPE_NONE, 0);
978 g_signal_new("redraw",
979 G_TYPE_FROM_CLASS(gclass),
980 G_SIGNAL_RUN_LAST,
981 G_STRUCT_OFFSET(IconClass, redraw),
982 NULL, NULL,
983 g_cclosure_marshal_VOID__VOID,
984 G_TYPE_NONE, 0);
986 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
989 static void icon_init(GTypeInstance *object, gpointer gclass)
991 Icon *icon = (Icon *) object;
993 icon->selected = FALSE;
994 icon->src_path = NULL;
995 icon->path = NULL;
996 icon->item = NULL;
997 icon->dialog = NULL;
998 icon->shortcut = NULL;
999 icon->shortcut_key.keycode = 0;
1000 icon->shortcut_key.modifier = 0;
1001 icon->args = NULL;
1002 icon->locked = FALSE;
1005 /* As icon_set_selected(), but doesn't automatically unselect incompatible
1006 * icons.
1008 static void icon_set_selected_int(Icon *icon, gboolean selected)
1010 static GtkClipboard *primary;
1012 g_return_if_fail(icon != NULL);
1014 if (icon->selected == selected)
1015 return;
1017 if (!primary)
1018 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
1020 if (selected)
1022 icon_selection = g_list_prepend(icon_selection, icon);
1023 if (!have_primary)
1025 GtkTargetEntry target_table[] =
1027 {"text/uri-list", 0, TARGET_URI_LIST},
1028 {"UTF8", 0, TARGET_STRING},
1029 {"COMPOUND_TEXT", 0, TARGET_STRING},
1030 {"STRING", 0, TARGET_STRING},
1033 /* Grab selection */
1034 have_primary = gtk_clipboard_set_with_data(primary,
1035 target_table,
1036 sizeof(target_table) / sizeof(*target_table),
1037 selection_get, lose_selection, NULL);
1040 else
1042 icon_selection = g_list_remove(icon_selection, icon);
1043 if (have_primary && !icon_selection)
1045 have_primary = FALSE;
1046 gtk_clipboard_clear(primary);
1050 icon->selected = selected;
1051 g_signal_emit_by_name(icon, "redraw");
1054 /* From xfwm4 */
1055 static void initModifiers(void)
1057 static gboolean need_init = TRUE;
1058 Display *dpy = GDK_DISPLAY();
1059 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1060 int m, k;
1062 if (!need_init)
1063 return;
1064 need_init = FALSE;
1066 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1067 SuperMask = HyperMask = 0;
1069 /* Work out which mask to use for each modifier group */
1070 if (xmk)
1072 KeyCode *c = xmk->modifiermap;
1073 KeyCode numLockKeyCode;
1074 KeyCode scrollLockKeyCode;
1075 KeyCode capsLockKeyCode;
1076 KeyCode altKeyCode;
1077 KeyCode metaKeyCode;
1078 KeyCode superKeyCode;
1079 KeyCode hyperKeyCode;
1081 /* Find the codes to search for... */
1082 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1083 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1084 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1085 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1086 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1087 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1088 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1090 /* If some are missing, try alternatives... */
1091 if (!altKeyCode)
1092 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1093 if (!metaKeyCode)
1094 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1095 if (!superKeyCode)
1096 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1097 if (!hyperKeyCode)
1098 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1100 /* Check each of the eight modifier lists.
1101 * The idea (I think) is that we name the modifier group which
1102 * includes the Alt key as the 'Alt group', and so on for
1103 * the other modifiers.
1105 for (m = 0; m < 8; m++)
1107 for (k = 0; k < xmk->max_keypermod; k++, c++)
1109 if (*c == NoSymbol)
1110 continue;
1111 if (*c == numLockKeyCode)
1112 NumLockMask = (1 << m);
1113 if (*c == scrollLockKeyCode)
1114 ScrollLockMask = (1 << m);
1115 if (*c == capsLockKeyCode)
1116 CapsLockMask = (1 << m);
1117 if (*c == altKeyCode)
1118 AltMask = (1 << m);
1119 if (*c == metaKeyCode)
1120 MetaMask = (1 << m);
1121 if (*c == superKeyCode)
1122 SuperMask = (1 << m);
1123 if (*c == hyperKeyCode)
1124 HyperMask = (1 << m);
1127 XFreeModifiermap(xmk);
1130 if(MetaMask == AltMask)
1131 MetaMask = 0;
1133 if (AltMask != 0 && MetaMask == Mod1Mask)
1135 MetaMask = AltMask;
1136 AltMask = Mod1Mask;
1139 if (AltMask == 0 && MetaMask != 0)
1141 if (MetaMask != Mod1Mask)
1142 AltMask = Mod1Mask;
1143 else
1145 AltMask = MetaMask;
1146 MetaMask = 0;
1150 if (AltMask == 0)
1151 AltMask = Mod1Mask;
1155 /* Fill in key from str. Sets keycode to zero if str is NULL.
1156 * Stolen from xfwm4 and modified. Call initModifiers before this.
1158 static void parseKeyString(MyKey *key, const char *str)
1160 char *k;
1161 Display *dpy = GDK_DISPLAY();
1163 key->keycode = 0;
1164 key->modifier = 0;
1166 if (!str)
1167 return;
1169 k = strrchr(str, '+');
1170 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1171 if (k)
1173 gchar *tmp;
1175 tmp = g_ascii_strdown(str, -1);
1177 if (strstr(tmp, "shift"))
1178 key->modifier = key->modifier | ShiftMask;
1179 if (strstr(tmp, "control"))
1180 key->modifier = key->modifier | ControlMask;
1181 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1182 key->modifier = key->modifier | AltMask;
1183 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1184 key->modifier = key->modifier | MetaMask;
1185 if (strstr(tmp, "hyper"))
1186 key->modifier = key->modifier | HyperMask;
1187 if (strstr(tmp, "super"))
1188 key->modifier = key->modifier | SuperMask;
1190 g_free(tmp);
1193 if (!key->keycode)
1194 g_warning("Can't parse key '%s'\n", str);
1197 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1198 GdkEvent *event,
1199 gpointer data)
1201 GList *next;
1202 XKeyEvent *kev = (XKeyEvent *) xevent;
1203 guint state;
1205 if (kev->type != KeyPress)
1206 return GDK_FILTER_CONTINUE;
1208 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask |
1209 HyperMask | SuperMask);
1211 for (next = icon_shortcuts; next; next = next->next)
1213 Icon *icon = (Icon *) next->data;
1215 if (icon->shortcut_key.keycode == kev->keycode &&
1216 icon->shortcut_key.modifier == state)
1218 icon_wink(icon);
1219 icon_run(icon);
1223 return GDK_FILTER_CONTINUE;
1226 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1227 root, False, GrabModeAsync, GrabModeAsync)
1228 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1229 root)
1231 static guint mykey_hash(gconstpointer key)
1233 MyKey *k = (MyKey *) key;
1235 return (k->keycode << 8) + k->modifier;
1238 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1240 MyKey *ka = (MyKey *) a;
1241 MyKey *kb = (MyKey *) b;
1243 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1246 /* Stolen from xfwm4 and modified.
1247 * FALSE on error. Call initModifiers before this.
1249 static gboolean grabKey(MyKey *key)
1251 Window root;
1252 Display *dpy = GDK_DISPLAY();
1253 static gboolean need_init = TRUE;
1255 if (need_init)
1257 need_init = FALSE;
1258 gdk_window_add_filter(gdk_get_default_root_window(),
1259 filter_keys, NULL);
1262 gdk_error_trap_push();
1264 root = GDK_ROOT_WINDOW();
1266 GRAB(key, 0);
1268 /* Here we grab all combinations of well known modifiers */
1269 GRAB(key, ScrollLockMask);
1270 GRAB(key, NumLockMask);
1271 GRAB(key, CapsLockMask);
1272 GRAB(key, ScrollLockMask | NumLockMask);
1273 GRAB(key, ScrollLockMask | CapsLockMask);
1274 GRAB(key, CapsLockMask | NumLockMask);
1275 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1277 gdk_flush();
1278 return gdk_error_trap_pop() == Success;
1281 static gboolean ungrabKey(MyKey *key)
1283 Window root = GDK_ROOT_WINDOW();
1284 Display *dpy = GDK_DISPLAY();
1286 gdk_error_trap_push();
1288 UNGRAB(key, 0);
1290 UNGRAB(key, ScrollLockMask);
1291 UNGRAB(key, NumLockMask);
1292 UNGRAB(key, CapsLockMask);
1293 UNGRAB(key, ScrollLockMask | NumLockMask);
1294 UNGRAB(key, ScrollLockMask | CapsLockMask);
1295 UNGRAB(key, CapsLockMask | NumLockMask);
1296 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1298 gdk_flush();
1299 return gdk_error_trap_pop() == Success;
1302 static void ungrab_key(Icon *icon)
1304 int *count;
1306 g_return_if_fail(icon != NULL);
1308 if (!icon->shortcut_key.keycode)
1309 return;
1311 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1313 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1314 g_return_if_fail(count != NULL);
1316 (*count)--;
1318 if (*count > 0)
1319 return;
1321 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1323 if (!ungrabKey(&icon->shortcut_key))
1324 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1325 icon->shortcut, icon->item->leafname);
1328 static void grab_key(Icon *icon)
1330 MyKey *hash_key;
1331 int *count;
1333 g_return_if_fail(icon != NULL);
1335 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1337 if (!icon->shortcut_key.keycode)
1338 return;
1340 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1342 if (!grab_counter)
1343 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1344 g_free, NULL);
1346 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1347 if (count)
1349 (*count)++;
1350 return; /* Already grabbed */
1353 hash_key = g_new(MyKey, 1);
1354 *hash_key = icon->shortcut_key;
1355 count = g_new(int, 1);
1356 *count = 1;
1357 g_hash_table_insert(grab_counter, hash_key, count);
1359 if (!grabKey(&icon->shortcut_key))
1360 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1361 "Some other application may be already using it!\n",
1362 icon->shortcut, icon->item->leafname);
1366 static void icon_wink(Icon *icon)
1368 IconClass *iclass;
1370 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1371 g_return_if_fail(iclass->wink != NULL);
1373 iclass->wink(icon);
1376 /* Sets icon_menu */
1377 static void create_menu(void)
1379 GList *items;
1380 guchar *tmp;
1381 GtkItemFactory *item_factory;
1383 g_return_if_fail(icon_menu == NULL);
1385 item_factory = menu_create(menu_def,
1386 sizeof(menu_def) / sizeof(*menu_def),
1387 "<icon>", NULL);
1389 tmp = g_strconcat("<icon>/", _("File"), NULL);
1390 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
1391 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
1392 g_free(tmp);
1394 /* File '' label... */
1395 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
1396 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
1397 g_list_free(items);
1399 /* Shift Open... label */
1400 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
1401 file_shift_item = GTK_BIN(items->data)->child;
1402 g_list_free(items);
1404 g_signal_connect(icon_menu, "unmap_event",
1405 G_CALLBACK(menu_closed), NULL);