r4654: Allow pinboard and panel icons to be locked (Dennis Tomas, suggested by
[rox-filer.git] / ROX-Filer / src / icon.c
blobce61094587054644f60154101b809b826fa4792e
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 g_signal_connect(icon->dialog, "destroy",
836 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
838 dialog = GTK_DIALOG(icon->dialog);
840 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
841 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
842 TRUE, TRUE, 0);
843 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
845 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
846 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
848 label = gtk_label_new(_("Clicking the icon opens:"));
849 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
851 entry = gtk_entry_new();
852 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
853 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
854 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
855 g_signal_connect_swapped(entry, "activate",
856 G_CALLBACK(rename_activate), dialog);
858 label = gtk_label_new(_("Arguments to pass (for executables):"));
859 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
861 entry = gtk_entry_new();
862 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
863 gtk_entry_set_text(GTK_ENTRY(entry), icon->args ? icon->args : "");
864 g_object_set_data(G_OBJECT(dialog), "new_arg", entry);
865 g_signal_connect_swapped(entry, "activate",
866 G_CALLBACK(rename_activate), dialog);
868 spacer = gtk_drawing_area_new();
869 gtk_widget_set_size_request(spacer, 4, 4);
870 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
872 label = gtk_label_new(_("The text displayed under the icon is:"));
873 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
874 entry = gtk_entry_new();
875 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
876 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
877 gtk_widget_grab_focus(entry);
878 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
879 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
881 spacer = gtk_drawing_area_new();
882 gtk_widget_set_size_request(spacer, 4, 4);
883 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
885 label = gtk_label_new(_("The keyboard shortcut is:"));
886 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
888 hbox = gtk_hbox_new(FALSE, 2);
889 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
890 button = gtk_button_new_with_label(icon->shortcut
891 ? icon->shortcut
892 : CLICK_TO_SET);
893 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
894 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
895 GTK_BIN(button)->child);
896 g_signal_connect(button, "clicked",
897 G_CALLBACK(get_shortcut),
898 GTK_BIN(button)->child);
899 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
900 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
901 g_signal_connect(button2, "clicked",
902 G_CALLBACK(clear_shortcut),
903 GTK_BIN(button)->child);
905 lock_state = gtk_check_button_new_with_label(_("Lock"));
906 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lock_state), icon->locked);
907 gtk_box_pack_start(vbox, lock_state, TRUE, TRUE, 0);
908 g_object_set_data(G_OBJECT(dialog), "new_lock_state", lock_state);
910 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
912 gtk_dialog_add_buttons(dialog,
913 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
914 GTK_STOCK_OK, GTK_RESPONSE_OK,
915 NULL);
916 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
918 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
920 gtk_widget_show_all(GTK_WIDGET(dialog));
923 static gpointer parent_class = NULL;
925 static void icon_finialize(GObject *object)
927 Icon *icon = (Icon *) object;
929 g_return_if_fail(!icon->selected);
931 if (icon->dialog)
932 gtk_widget_destroy(icon->dialog);
934 if (icon == menu_icon)
935 menu_icon = NULL;
937 icon_set_path(icon, NULL, NULL);
938 icon_set_shortcut(icon, NULL);
939 icon_set_arguments(icon, NULL);
941 G_OBJECT_CLASS(parent_class)->finalize(object);
944 static void icon_class_init(gpointer gclass, gpointer data)
946 GObjectClass *object = (GObjectClass *) gclass;
947 IconClass *icon = (IconClass *) gclass;
949 parent_class = g_type_class_peek_parent(gclass);
951 object->finalize = icon_finialize;
952 icon->destroy = NULL;
953 icon->redraw = NULL;
954 icon->update = NULL;
955 icon->same_group = NULL;
956 icon->wink = NULL;
958 g_signal_new("update",
959 G_TYPE_FROM_CLASS(gclass),
960 G_SIGNAL_RUN_LAST,
961 G_STRUCT_OFFSET(IconClass, update),
962 NULL, NULL,
963 g_cclosure_marshal_VOID__VOID,
964 G_TYPE_NONE, 0);
966 g_signal_new("destroy",
967 G_TYPE_FROM_CLASS(gclass),
968 G_SIGNAL_RUN_LAST,
969 G_STRUCT_OFFSET(IconClass, destroy),
970 NULL, NULL,
971 g_cclosure_marshal_VOID__VOID,
972 G_TYPE_NONE, 0);
974 g_signal_new("redraw",
975 G_TYPE_FROM_CLASS(gclass),
976 G_SIGNAL_RUN_LAST,
977 G_STRUCT_OFFSET(IconClass, redraw),
978 NULL, NULL,
979 g_cclosure_marshal_VOID__VOID,
980 G_TYPE_NONE, 0);
982 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
985 static void icon_init(GTypeInstance *object, gpointer gclass)
987 Icon *icon = (Icon *) object;
989 icon->selected = FALSE;
990 icon->src_path = NULL;
991 icon->path = NULL;
992 icon->item = NULL;
993 icon->dialog = NULL;
994 icon->shortcut = NULL;
995 icon->shortcut_key.keycode = 0;
996 icon->shortcut_key.modifier = 0;
997 icon->args = NULL;
998 icon->locked = FALSE;
1001 /* As icon_set_selected(), but doesn't automatically unselect incompatible
1002 * icons.
1004 static void icon_set_selected_int(Icon *icon, gboolean selected)
1006 static GtkClipboard *primary;
1008 g_return_if_fail(icon != NULL);
1010 if (icon->selected == selected)
1011 return;
1013 if (!primary)
1014 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
1016 if (selected)
1018 icon_selection = g_list_prepend(icon_selection, icon);
1019 if (!have_primary)
1021 GtkTargetEntry target_table[] =
1023 {"text/uri-list", 0, TARGET_URI_LIST},
1024 {"UTF8", 0, TARGET_STRING},
1025 {"COMPOUND_TEXT", 0, TARGET_STRING},
1026 {"STRING", 0, TARGET_STRING},
1029 /* Grab selection */
1030 have_primary = gtk_clipboard_set_with_data(primary,
1031 target_table,
1032 sizeof(target_table) / sizeof(*target_table),
1033 selection_get, lose_selection, NULL);
1036 else
1038 icon_selection = g_list_remove(icon_selection, icon);
1039 if (have_primary && !icon_selection)
1041 have_primary = FALSE;
1042 gtk_clipboard_clear(primary);
1046 icon->selected = selected;
1047 g_signal_emit_by_name(icon, "redraw");
1050 /* From xfwm4 */
1051 static void initModifiers(void)
1053 static gboolean need_init = TRUE;
1054 Display *dpy = GDK_DISPLAY();
1055 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1056 int m, k;
1058 if (!need_init)
1059 return;
1060 need_init = FALSE;
1062 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1063 SuperMask = HyperMask = 0;
1065 /* Work out which mask to use for each modifier group */
1066 if (xmk)
1068 KeyCode *c = xmk->modifiermap;
1069 KeyCode numLockKeyCode;
1070 KeyCode scrollLockKeyCode;
1071 KeyCode capsLockKeyCode;
1072 KeyCode altKeyCode;
1073 KeyCode metaKeyCode;
1074 KeyCode superKeyCode;
1075 KeyCode hyperKeyCode;
1077 /* Find the codes to search for... */
1078 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1079 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1080 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1081 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1082 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1083 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1084 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1086 /* If some are missing, try alternatives... */
1087 if (!altKeyCode)
1088 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1089 if (!metaKeyCode)
1090 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1091 if (!superKeyCode)
1092 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1093 if (!hyperKeyCode)
1094 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1096 /* Check each of the eight modifier lists.
1097 * The idea (I think) is that we name the modifier group which
1098 * includes the Alt key as the 'Alt group', and so on for
1099 * the other modifiers.
1101 for (m = 0; m < 8; m++)
1103 for (k = 0; k < xmk->max_keypermod; k++, c++)
1105 if (*c == NoSymbol)
1106 continue;
1107 if (*c == numLockKeyCode)
1108 NumLockMask = (1 << m);
1109 if (*c == scrollLockKeyCode)
1110 ScrollLockMask = (1 << m);
1111 if (*c == capsLockKeyCode)
1112 CapsLockMask = (1 << m);
1113 if (*c == altKeyCode)
1114 AltMask = (1 << m);
1115 if (*c == metaKeyCode)
1116 MetaMask = (1 << m);
1117 if (*c == superKeyCode)
1118 SuperMask = (1 << m);
1119 if (*c == hyperKeyCode)
1120 HyperMask = (1 << m);
1123 XFreeModifiermap(xmk);
1126 if(MetaMask == AltMask)
1127 MetaMask = 0;
1129 if (AltMask != 0 && MetaMask == Mod1Mask)
1131 MetaMask = AltMask;
1132 AltMask = Mod1Mask;
1135 if (AltMask == 0 && MetaMask != 0)
1137 if (MetaMask != Mod1Mask)
1138 AltMask = Mod1Mask;
1139 else
1141 AltMask = MetaMask;
1142 MetaMask = 0;
1146 if (AltMask == 0)
1147 AltMask = Mod1Mask;
1151 /* Fill in key from str. Sets keycode to zero if str is NULL.
1152 * Stolen from xfwm4 and modified. Call initModifiers before this.
1154 static void parseKeyString(MyKey *key, const char *str)
1156 char *k;
1157 Display *dpy = GDK_DISPLAY();
1159 key->keycode = 0;
1160 key->modifier = 0;
1162 if (!str)
1163 return;
1165 k = strrchr(str, '+');
1166 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1167 if (k)
1169 gchar *tmp;
1171 tmp = g_ascii_strdown(str, -1);
1173 if (strstr(tmp, "shift"))
1174 key->modifier = key->modifier | ShiftMask;
1175 if (strstr(tmp, "control"))
1176 key->modifier = key->modifier | ControlMask;
1177 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1178 key->modifier = key->modifier | AltMask;
1179 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1180 key->modifier = key->modifier | MetaMask;
1181 if (strstr(tmp, "hyper"))
1182 key->modifier = key->modifier | HyperMask;
1183 if (strstr(tmp, "super"))
1184 key->modifier = key->modifier | SuperMask;
1186 g_free(tmp);
1189 if (!key->keycode)
1190 g_warning("Can't parse key '%s'\n", str);
1193 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1194 GdkEvent *event,
1195 gpointer data)
1197 GList *next;
1198 XKeyEvent *kev = (XKeyEvent *) xevent;
1199 guint state;
1201 if (kev->type != KeyPress)
1202 return GDK_FILTER_CONTINUE;
1204 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask |
1205 HyperMask | SuperMask);
1207 for (next = icon_shortcuts; next; next = next->next)
1209 Icon *icon = (Icon *) next->data;
1211 if (icon->shortcut_key.keycode == kev->keycode &&
1212 icon->shortcut_key.modifier == state)
1214 icon_wink(icon);
1215 icon_run(icon);
1219 return GDK_FILTER_CONTINUE;
1222 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1223 root, False, GrabModeAsync, GrabModeAsync)
1224 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1225 root)
1227 static guint mykey_hash(gconstpointer key)
1229 MyKey *k = (MyKey *) key;
1231 return (k->keycode << 8) + k->modifier;
1234 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1236 MyKey *ka = (MyKey *) a;
1237 MyKey *kb = (MyKey *) b;
1239 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1242 /* Stolen from xfwm4 and modified.
1243 * FALSE on error. Call initModifiers before this.
1245 static gboolean grabKey(MyKey *key)
1247 Window root;
1248 Display *dpy = GDK_DISPLAY();
1249 static gboolean need_init = TRUE;
1251 if (need_init)
1253 need_init = FALSE;
1254 gdk_window_add_filter(gdk_get_default_root_window(),
1255 filter_keys, NULL);
1258 gdk_error_trap_push();
1260 root = GDK_ROOT_WINDOW();
1262 GRAB(key, 0);
1264 /* Here we grab all combinations of well known modifiers */
1265 GRAB(key, ScrollLockMask);
1266 GRAB(key, NumLockMask);
1267 GRAB(key, CapsLockMask);
1268 GRAB(key, ScrollLockMask | NumLockMask);
1269 GRAB(key, ScrollLockMask | CapsLockMask);
1270 GRAB(key, CapsLockMask | NumLockMask);
1271 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1273 gdk_flush();
1274 return gdk_error_trap_pop() == Success;
1277 static gboolean ungrabKey(MyKey *key)
1279 Window root = GDK_ROOT_WINDOW();
1280 Display *dpy = GDK_DISPLAY();
1282 gdk_error_trap_push();
1284 UNGRAB(key, 0);
1286 UNGRAB(key, ScrollLockMask);
1287 UNGRAB(key, NumLockMask);
1288 UNGRAB(key, CapsLockMask);
1289 UNGRAB(key, ScrollLockMask | NumLockMask);
1290 UNGRAB(key, ScrollLockMask | CapsLockMask);
1291 UNGRAB(key, CapsLockMask | NumLockMask);
1292 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1294 gdk_flush();
1295 return gdk_error_trap_pop() == Success;
1298 static void ungrab_key(Icon *icon)
1300 int *count;
1302 g_return_if_fail(icon != NULL);
1304 if (!icon->shortcut_key.keycode)
1305 return;
1307 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1309 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1310 g_return_if_fail(count != NULL);
1312 (*count)--;
1314 if (*count > 0)
1315 return;
1317 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1319 if (!ungrabKey(&icon->shortcut_key))
1320 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1321 icon->shortcut, icon->item->leafname);
1324 static void grab_key(Icon *icon)
1326 MyKey *hash_key;
1327 int *count;
1329 g_return_if_fail(icon != NULL);
1331 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1333 if (!icon->shortcut_key.keycode)
1334 return;
1336 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1338 if (!grab_counter)
1339 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1340 g_free, NULL);
1342 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1343 if (count)
1345 (*count)++;
1346 return; /* Already grabbed */
1349 hash_key = g_new(MyKey, 1);
1350 *hash_key = icon->shortcut_key;
1351 count = g_new(int, 1);
1352 *count = 1;
1353 g_hash_table_insert(grab_counter, hash_key, count);
1355 if (!grabKey(&icon->shortcut_key))
1356 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1357 "Some other application may be already using it!\n",
1358 icon->shortcut, icon->item->leafname);
1362 static void icon_wink(Icon *icon)
1364 IconClass *iclass;
1366 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1367 g_return_if_fail(iclass->wink != NULL);
1369 iclass->wink(icon);
1372 /* Sets icon_menu */
1373 static void create_menu(void)
1375 GList *items;
1376 guchar *tmp;
1377 GtkItemFactory *item_factory;
1379 g_return_if_fail(icon_menu == NULL);
1381 item_factory = menu_create(menu_def,
1382 sizeof(menu_def) / sizeof(*menu_def),
1383 "<icon>", NULL);
1385 tmp = g_strconcat("<icon>/", _("File"), NULL);
1386 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
1387 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
1388 g_free(tmp);
1390 /* File '' label... */
1391 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
1392 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
1393 g_list_free(items);
1395 /* Shift Open... label */
1396 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
1397 file_shift_item = GTK_BIN(items->data)->child;
1398 g_list_free(items);
1400 g_signal_connect(icon_menu, "unmap_event",
1401 G_CALLBACK(menu_closed), NULL);