r4523: Updated headers:
[rox-filer/dt.git] / ROX-Filer / src / icon.c
blob7d8280255e481064a2a2bd2399e74b652f4601fb
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 /* Menu was clicked over this icon. Set things up correctly (shade items,
229 * add app menu stuff, etc).
230 * You should show icon_menu after calling this...
231 * panel_name is NULL for the pinboard.
233 void icon_prepare_menu(Icon *icon, GtkWidget *options_item)
235 GtkWidget *image;
237 appmenu_remove();
239 if (current_options_item)
241 gtk_widget_destroy(current_options_item);
242 current_options_item = NULL;
245 menu_icon = icon;
247 if (!icon_menu)
248 create_menu();
250 current_options_item = options_item;
251 image = gtk_image_new_from_stock(GTK_STOCK_PREFERENCES,
252 GTK_ICON_SIZE_MENU);
253 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(options_item), image);
255 gtk_menu_shell_append(GTK_MENU_SHELL(icon_menu), options_item);
256 gtk_widget_show_all(options_item);
258 /* Shade Remove Item(s) unless there is a selection */
259 menu_set_items_shaded(icon_menu,
260 (icon_selection || menu_icon) ? FALSE : TRUE,
261 4, 1);
263 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
264 FALSE);
266 /* Shade the File/Edit/Show items unless an item was clicked */
267 if (icon)
269 guchar *tmp;
271 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
272 menu_set_items_shaded(icon_file_menu, FALSE, 0, 5);
273 if (!can_set_run_action(icon->item))
274 menu_set_items_shaded(icon_file_menu, TRUE, 2, 1);
276 tmp = g_strdup_printf(_("%s '%s'"),
277 basetype_name(icon->item),
278 icon->item->leafname);
279 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
280 g_free(tmp);
282 /* Check for app-specific menu */
283 appmenu_add(icon->path, icon->item, icon_menu);
285 else
287 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
288 menu_set_items_shaded(icon_file_menu, TRUE, 0, 5);
289 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
293 /* Set whether this icon is selected. Will automatically clear the selection
294 * if it contains icons from a different group.
296 void icon_set_selected(Icon *icon, gboolean selected)
298 if (selected && icon_selection)
300 IconClass *iclass;
301 Icon *other = (Icon *) icon_selection->data;
303 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
304 if (!iclass->same_group(icon, other))
306 icon_select_only(icon);
307 return;
311 icon_set_selected_int(icon, selected);
314 /* Clear everything, except 'select', which is selected.
315 * If select is NULL, unselects everything.
317 void icon_select_only(Icon *select)
319 GList *to_clear, *next;
321 if (select)
322 icon_set_selected_int(select, TRUE);
324 to_clear = g_list_copy(icon_selection);
326 if (select)
327 to_clear = g_list_remove(to_clear, select);
329 for (next = to_clear; next; next = next->next)
330 icon_set_selected_int((Icon *) next->data, FALSE);
332 g_list_free(to_clear);
335 /* Destroys this icon's widget, causing it to be removed from the screen */
336 void icon_destroy(Icon *icon)
338 g_return_if_fail(icon != NULL);
340 icon_set_selected_int(icon, FALSE);
342 g_signal_emit_by_name(icon, "destroy");
345 /* Return a text/uri-list of all the icons in the selection */
346 gchar *icon_create_uri_list(void)
348 GString *tmp;
349 guchar *retval;
350 GList *next;
352 tmp = g_string_new(NULL);
354 for (next = icon_selection; next; next = next->next)
356 Icon *icon = (Icon *) next->data;
357 EscapedPath *uri;
359 uri = encode_path_as_uri(icon->path);
360 g_string_append(tmp, (char *) uri);
361 g_free(uri);
362 g_string_append(tmp, "\r\n");
365 retval = tmp->str;
366 g_string_free(tmp, FALSE);
368 return retval;
371 GType icon_get_type(void)
373 static GType type = 0;
375 if (!type)
377 static const GTypeInfo info =
379 sizeof (IconClass),
380 NULL, /* base_init */
381 NULL, /* base_finalise */
382 icon_class_init,
383 NULL, /* class_finalise */
384 NULL, /* class_data */
385 sizeof(Icon),
386 0, /* n_preallocs */
387 icon_init
390 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
391 &info, 0);
394 return type;
397 /* Sets, unsets or changes the pathname and name for an icon.
398 * Updates icons_hash and (re)stats the item.
399 * If name is NULL then gets the leafname from pathname.
400 * If pathname is NULL, frees everything.
402 void icon_set_path(Icon *icon, const char *pathname, const char *name)
404 if (icon->path)
406 icon_unhash_path(icon);
407 icon->src_path = NULL;
408 icon->path = NULL;
410 diritem_free(icon->item);
411 icon->item = NULL;
414 if (pathname)
416 if (g_utf8_validate(pathname, -1, NULL))
417 icon->src_path = g_strdup(pathname);
418 else
419 icon->src_path = to_utf8(pathname);
420 icon->path = expand_path(icon->src_path);
422 icon_hash_path(icon);
424 if (!name)
425 name = g_basename(icon->src_path);
427 icon->item = diritem_new(name);
428 diritem_restat(icon->path, icon->item, NULL);
432 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
434 g_return_if_fail(icon != NULL);
436 if (shortcut && !*shortcut)
437 shortcut = NULL;
438 if (icon->shortcut == shortcut)
439 return;
440 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
441 return;
443 initModifiers();
445 ungrab_key(icon);
447 g_free(icon->shortcut);
448 icon->shortcut = g_strdup(shortcut);
449 parseKeyString(&icon->shortcut_key, shortcut);
451 grab_key(icon);
454 void icon_set_arguments(Icon *icon, const gchar *args)
456 g_return_if_fail(icon != NULL);
457 g_return_if_fail(args == NULL || icon->args != args);
459 if (args && !*args)
460 args = NULL;
461 if (icon->args && args && strcmp(icon->args, args) == 0)
462 return;
464 g_free(icon->args);
465 icon->args = g_strdup(args);
468 void icon_run(Icon *icon)
470 if (icon->args == NULL)
471 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
472 else
473 run_with_args(icon->path, icon->item, icon->args);
476 /****************************************************************
477 * INTERNAL FUNCTIONS *
478 ****************************************************************/
480 /* The icons_hash table allows us to convert from a path to a list
481 * of icons that use that path.
482 * Add this icon to the list for its path.
484 static void icon_hash_path(Icon *icon)
486 GList *list;
488 g_return_if_fail(icon != NULL);
490 /* g_print("[ hashing '%s' ]\n", icon->path); */
492 list = g_hash_table_lookup(icons_hash, icon->path);
493 list = g_list_prepend(list, icon);
494 g_hash_table_insert(icons_hash, icon->path, list);
497 /* Remove this icon from the icons_hash table */
498 static void icon_unhash_path(Icon *icon)
500 GList *list;
502 g_return_if_fail(icon != NULL);
504 /* g_print("[ unhashing '%s' ]\n", icon->path); */
506 list = g_hash_table_lookup(icons_hash, icon->path);
507 g_return_if_fail(list != NULL);
509 list = g_list_remove(list, icon);
511 /* Remove it first; the hash key may have changed address */
512 g_hash_table_remove(icons_hash, icon->path);
513 if (list)
514 g_hash_table_insert(icons_hash,
515 ((Icon *) list->data)->path, list);
518 static void rename_activate(GtkWidget *dialog)
520 GtkWidget *entry, *src, *shortcut, *arg;
521 Icon *icon;
522 const guchar *new_name, *new_src, *new_shortcut, *new_args;
524 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
525 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
526 src = g_object_get_data(G_OBJECT(dialog), "new_path");
527 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
528 arg = g_object_get_data(G_OBJECT(dialog), "new_arg");
530 g_return_if_fail(entry != NULL &&
531 src != NULL &&
532 icon != NULL &&
533 shortcut != NULL &&
534 arg != NULL);
536 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
537 new_src = gtk_entry_get_text(GTK_ENTRY(src));
538 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
539 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
540 new_shortcut = NULL;
541 new_args = gtk_entry_get_text(GTK_ENTRY(arg));
543 if (*new_src == '\0')
544 report_error(
545 _("The location must contain at least one character!"));
546 else
548 icon_set_path(icon, new_src, new_name);
549 icon_set_shortcut(icon, new_shortcut);
550 icon_set_arguments(icon, new_args);
551 g_signal_emit_by_name(icon, "update");
552 gtk_widget_destroy(dialog);
556 static void menu_closed(GtkWidget *widget)
558 appmenu_remove();
559 menu_icon = NULL;
562 /* Called when another application takes the selection away from us */
563 static void lose_selection(GtkClipboard *primary, gpointer data)
565 have_primary = FALSE;
566 icon_select_only(NULL);
569 /* Called when another application wants the contents of our selection */
570 static void selection_get(GtkClipboard *primary,
571 GtkSelectionData *selection_data,
572 guint info,
573 gpointer data)
575 gchar *text;
577 if (info == TARGET_URI_LIST)
578 text = icon_create_uri_list();
579 else
581 GList *next;
582 GString *str;
584 str = g_string_new(NULL);
586 for (next = icon_selection; next; next = next->next)
588 Icon *icon = (Icon *) next->data;
590 g_string_append(str, icon->path);
591 g_string_append_c(str, ' ');
594 text = str->str;
595 g_string_free(str, FALSE);
598 gtk_selection_data_set_text(selection_data, text, strlen(text));
601 static void remove_items(gpointer data, guint action, GtkWidget *widget)
603 IconClass *iclass;
605 if (menu_icon)
606 icon_set_selected(menu_icon, TRUE);
608 if (!icon_selection)
610 delayed_error(
611 _("You must first select some items to remove"));
612 return;
615 iclass = (IconClass *)
616 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
618 iclass->remove_items();
621 static void file_op(gpointer data, guint action, GtkWidget *widget)
623 if (!menu_icon)
625 delayed_error(_("You must open the menu over an item"));
626 return;
629 switch (action)
631 case ACTION_SHIFT:
632 run_diritem(menu_icon->path, menu_icon->item,
633 NULL, NULL, TRUE);
634 break;
635 case ACTION_EDIT:
636 show_rename_box(menu_icon);
637 break;
638 case ACTION_LOCATION:
639 open_to_show(menu_icon->path);
640 break;
641 case ACTION_PROPERTIES:
642 infobox_new(menu_icon->path);
643 break;
644 case ACTION_RUN_ACTION:
645 if (can_set_run_action(menu_icon->item))
646 type_set_handler_dialog(
647 menu_icon->item->mime_type);
648 else
649 report_error(
650 _("You can only set the run action for a "
651 "regular file"));
652 break;
653 case ACTION_SET_ICON:
654 icon_set_handler_dialog(menu_icon->item,
655 menu_icon->path);
656 break;
660 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
662 if (response == GTK_RESPONSE_OK)
663 rename_activate(dialog);
664 else if (response == GTK_RESPONSE_CANCEL)
665 gtk_widget_destroy(dialog);
668 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
669 GdkEvent *event,
670 gpointer data)
672 XKeyEvent *kev = (XKeyEvent *) xevent;
673 GtkWidget *popup = (GtkWidget *) data;
674 Display *dpy = GDK_DISPLAY();
676 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
677 return GDK_FILTER_CONTINUE;
679 initModifiers();
681 if (kev->type == KeyRelease)
683 gchar *str;
684 KeySym sym;
685 unsigned int m = kev->state;
687 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
688 if (!sym)
689 return GDK_FILTER_CONTINUE;
691 str = g_strdup_printf("%s%s%s%s%s%s%s",
692 m & ControlMask ? "Control+" : "",
693 m & ShiftMask ? "Shift+" : "",
694 m & AltMask ? "Alt+" : "",
695 m & MetaMask ? "Meta+" : "",
696 m & SuperMask ? "Super+" : "",
697 m & HyperMask ? "Hyper+" : "",
698 XKeysymToString(sym));
700 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
703 gdk_window_remove_filter(popup->window, filter_get_key, data);
704 gtk_widget_destroy(popup);
706 return GDK_FILTER_REMOVE;
709 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
711 gchar *str;
713 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
714 if (str)
716 gtk_label_set_text(GTK_LABEL(label), str);
717 g_free(str);
718 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
722 static void get_shortcut(GtkWidget *button, GtkWidget *label)
724 GtkWidget *popup, *frame, *msg;
725 Window xid;
726 Display *dpy = GDK_DISPLAY();
728 popup = gtk_window_new(GTK_WINDOW_POPUP);
730 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
732 frame = gtk_frame_new(NULL);
733 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
734 gtk_container_add(GTK_CONTAINER(popup), frame);
736 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
738 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
739 gtk_container_add(GTK_CONTAINER(frame), msg);
741 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
743 gtk_widget_add_events(popup,
744 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
746 g_signal_connect(popup, "destroy",
747 G_CALLBACK(may_set_shortcut), label);
749 gtk_widget_show_all(popup);
751 gdk_window_add_filter(popup->window, filter_get_key, popup);
753 xid = gdk_x11_drawable_get_xid(popup->window);
755 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
756 gtk_get_current_event_time()) != Success)
758 delayed_error(_("Failed to get keyboard grab!"));
759 gtk_widget_destroy(popup);
762 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
763 GrabModeAsync, xid, None,
764 gtk_get_current_event_time()) != Success)
766 g_warning("Failed to get mouse grab");
770 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
772 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
775 /* Opens a box allowing the user to change the name of a pinned icon.
776 * If the icon is destroyed then the box will close.
777 * If the user chooses OK then the callback is called once the icon's
778 * name, src_path and path fields have been updated and the item field
779 * restatted.
781 static void show_rename_box(Icon *icon)
783 GtkDialog *dialog;
784 GtkWidget *label, *entry, *button, *button2, *hbox, *spacer;
785 GtkBox *vbox;
787 if (icon->dialog)
789 gtk_window_present(GTK_WINDOW(icon->dialog));
790 return;
793 icon->dialog = gtk_dialog_new();
794 g_signal_connect(icon->dialog, "destroy",
795 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
797 dialog = GTK_DIALOG(icon->dialog);
799 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
800 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
801 TRUE, TRUE, 0);
802 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
804 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
805 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
807 label = gtk_label_new(_("Clicking the icon opens:"));
808 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
810 entry = gtk_entry_new();
811 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
812 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
813 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
814 g_signal_connect_swapped(entry, "activate",
815 G_CALLBACK(rename_activate), dialog);
817 label = gtk_label_new(_("Arguments to pass (for executables):"));
818 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
820 entry = gtk_entry_new();
821 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
822 gtk_entry_set_text(GTK_ENTRY(entry), icon->args ? icon->args : "");
823 g_object_set_data(G_OBJECT(dialog), "new_arg", entry);
824 g_signal_connect_swapped(entry, "activate",
825 G_CALLBACK(rename_activate), dialog);
827 spacer = gtk_drawing_area_new();
828 gtk_widget_set_size_request(spacer, 4, 4);
829 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
831 label = gtk_label_new(_("The text displayed under the icon is:"));
832 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
833 entry = gtk_entry_new();
834 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
835 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
836 gtk_widget_grab_focus(entry);
837 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
838 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
840 spacer = gtk_drawing_area_new();
841 gtk_widget_set_size_request(spacer, 4, 4);
842 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
844 label = gtk_label_new(_("The keyboard shortcut is:"));
845 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
847 hbox = gtk_hbox_new(FALSE, 2);
848 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
849 button = gtk_button_new_with_label(icon->shortcut
850 ? icon->shortcut
851 : CLICK_TO_SET);
852 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
853 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
854 GTK_BIN(button)->child);
855 g_signal_connect(button, "clicked",
856 G_CALLBACK(get_shortcut),
857 GTK_BIN(button)->child);
858 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
859 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
860 g_signal_connect(button2, "clicked",
861 G_CALLBACK(clear_shortcut),
862 GTK_BIN(button)->child);
864 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
866 gtk_dialog_add_buttons(dialog,
867 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
868 GTK_STOCK_OK, GTK_RESPONSE_OK,
869 NULL);
870 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
872 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
874 gtk_widget_show_all(GTK_WIDGET(dialog));
877 static gpointer parent_class = NULL;
879 static void icon_finialize(GObject *object)
881 Icon *icon = (Icon *) object;
883 g_return_if_fail(!icon->selected);
885 if (icon->dialog)
886 gtk_widget_destroy(icon->dialog);
888 if (icon == menu_icon)
889 menu_icon = NULL;
891 icon_set_path(icon, NULL, NULL);
892 icon_set_shortcut(icon, NULL);
893 icon_set_arguments(icon, NULL);
895 G_OBJECT_CLASS(parent_class)->finalize(object);
898 static void icon_class_init(gpointer gclass, gpointer data)
900 GObjectClass *object = (GObjectClass *) gclass;
901 IconClass *icon = (IconClass *) gclass;
903 parent_class = g_type_class_peek_parent(gclass);
905 object->finalize = icon_finialize;
906 icon->destroy = NULL;
907 icon->redraw = NULL;
908 icon->update = NULL;
909 icon->same_group = NULL;
910 icon->wink = NULL;
912 g_signal_new("update",
913 G_TYPE_FROM_CLASS(gclass),
914 G_SIGNAL_RUN_LAST,
915 G_STRUCT_OFFSET(IconClass, update),
916 NULL, NULL,
917 g_cclosure_marshal_VOID__VOID,
918 G_TYPE_NONE, 0);
920 g_signal_new("destroy",
921 G_TYPE_FROM_CLASS(gclass),
922 G_SIGNAL_RUN_LAST,
923 G_STRUCT_OFFSET(IconClass, destroy),
924 NULL, NULL,
925 g_cclosure_marshal_VOID__VOID,
926 G_TYPE_NONE, 0);
928 g_signal_new("redraw",
929 G_TYPE_FROM_CLASS(gclass),
930 G_SIGNAL_RUN_LAST,
931 G_STRUCT_OFFSET(IconClass, redraw),
932 NULL, NULL,
933 g_cclosure_marshal_VOID__VOID,
934 G_TYPE_NONE, 0);
936 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
939 static void icon_init(GTypeInstance *object, gpointer gclass)
941 Icon *icon = (Icon *) object;
943 icon->selected = FALSE;
944 icon->src_path = NULL;
945 icon->path = NULL;
946 icon->item = NULL;
947 icon->dialog = NULL;
948 icon->shortcut = NULL;
949 icon->shortcut_key.keycode = 0;
950 icon->shortcut_key.modifier = 0;
951 icon->args = NULL;
954 /* As icon_set_selected(), but doesn't automatically unselect incompatible
955 * icons.
957 static void icon_set_selected_int(Icon *icon, gboolean selected)
959 static GtkClipboard *primary;
961 g_return_if_fail(icon != NULL);
963 if (icon->selected == selected)
964 return;
966 if (!primary)
967 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
969 if (selected)
971 icon_selection = g_list_prepend(icon_selection, icon);
972 if (!have_primary)
974 GtkTargetEntry target_table[] =
976 {"text/uri-list", 0, TARGET_URI_LIST},
977 {"UTF8", 0, TARGET_STRING},
978 {"COMPOUND_TEXT", 0, TARGET_STRING},
979 {"STRING", 0, TARGET_STRING},
982 /* Grab selection */
983 have_primary = gtk_clipboard_set_with_data(primary,
984 target_table,
985 sizeof(target_table) / sizeof(*target_table),
986 selection_get, lose_selection, NULL);
989 else
991 icon_selection = g_list_remove(icon_selection, icon);
992 if (have_primary && !icon_selection)
994 have_primary = FALSE;
995 gtk_clipboard_clear(primary);
999 icon->selected = selected;
1000 g_signal_emit_by_name(icon, "redraw");
1003 /* From xfwm4 */
1004 static void initModifiers(void)
1006 static gboolean need_init = TRUE;
1007 Display *dpy = GDK_DISPLAY();
1008 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1009 int m, k;
1011 if (!need_init)
1012 return;
1013 need_init = FALSE;
1015 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1016 SuperMask = HyperMask = 0;
1018 /* Work out which mask to use for each modifier group */
1019 if (xmk)
1021 KeyCode *c = xmk->modifiermap;
1022 KeyCode numLockKeyCode;
1023 KeyCode scrollLockKeyCode;
1024 KeyCode capsLockKeyCode;
1025 KeyCode altKeyCode;
1026 KeyCode metaKeyCode;
1027 KeyCode superKeyCode;
1028 KeyCode hyperKeyCode;
1030 /* Find the codes to search for... */
1031 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1032 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1033 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1034 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1035 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1036 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1037 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1039 /* If some are missing, try alternatives... */
1040 if (!altKeyCode)
1041 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1042 if (!metaKeyCode)
1043 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1044 if (!superKeyCode)
1045 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1046 if (!hyperKeyCode)
1047 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1049 /* Check each of the eight modifier lists.
1050 * The idea (I think) is that we name the modifier group which
1051 * includes the Alt key as the 'Alt group', and so on for
1052 * the other modifiers.
1054 for (m = 0; m < 8; m++)
1056 for (k = 0; k < xmk->max_keypermod; k++, c++)
1058 if (*c == NoSymbol)
1059 continue;
1060 if (*c == numLockKeyCode)
1061 NumLockMask = (1 << m);
1062 if (*c == scrollLockKeyCode)
1063 ScrollLockMask = (1 << m);
1064 if (*c == capsLockKeyCode)
1065 CapsLockMask = (1 << m);
1066 if (*c == altKeyCode)
1067 AltMask = (1 << m);
1068 if (*c == metaKeyCode)
1069 MetaMask = (1 << m);
1070 if (*c == superKeyCode)
1071 SuperMask = (1 << m);
1072 if (*c == hyperKeyCode)
1073 HyperMask = (1 << m);
1076 XFreeModifiermap(xmk);
1079 if(MetaMask == AltMask)
1080 MetaMask = 0;
1082 if (AltMask != 0 && MetaMask == Mod1Mask)
1084 MetaMask = AltMask;
1085 AltMask = Mod1Mask;
1088 if (AltMask == 0 && MetaMask != 0)
1090 if (MetaMask != Mod1Mask)
1091 AltMask = Mod1Mask;
1092 else
1094 AltMask = MetaMask;
1095 MetaMask = 0;
1099 if (AltMask == 0)
1100 AltMask = Mod1Mask;
1104 /* Fill in key from str. Sets keycode to zero if str is NULL.
1105 * Stolen from xfwm4 and modified. Call initModifiers before this.
1107 static void parseKeyString(MyKey *key, const char *str)
1109 char *k;
1110 Display *dpy = GDK_DISPLAY();
1112 key->keycode = 0;
1113 key->modifier = 0;
1115 if (!str)
1116 return;
1118 k = strrchr(str, '+');
1119 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1120 if (k)
1122 gchar *tmp;
1124 tmp = g_ascii_strdown(str, -1);
1126 if (strstr(tmp, "shift"))
1127 key->modifier = key->modifier | ShiftMask;
1128 if (strstr(tmp, "control"))
1129 key->modifier = key->modifier | ControlMask;
1130 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1131 key->modifier = key->modifier | AltMask;
1132 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1133 key->modifier = key->modifier | MetaMask;
1134 if (strstr(tmp, "hyper"))
1135 key->modifier = key->modifier | HyperMask;
1136 if (strstr(tmp, "super"))
1137 key->modifier = key->modifier | SuperMask;
1139 g_free(tmp);
1142 if (!key->keycode)
1143 g_warning("Can't parse key '%s'\n", str);
1146 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1147 GdkEvent *event,
1148 gpointer data)
1150 GList *next;
1151 XKeyEvent *kev = (XKeyEvent *) xevent;
1152 guint state;
1154 if (kev->type != KeyPress)
1155 return GDK_FILTER_CONTINUE;
1157 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask |
1158 HyperMask | SuperMask);
1160 for (next = icon_shortcuts; next; next = next->next)
1162 Icon *icon = (Icon *) next->data;
1164 if (icon->shortcut_key.keycode == kev->keycode &&
1165 icon->shortcut_key.modifier == state)
1167 icon_wink(icon);
1168 icon_run(icon);
1172 return GDK_FILTER_CONTINUE;
1175 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1176 root, False, GrabModeAsync, GrabModeAsync)
1177 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1178 root)
1180 static guint mykey_hash(gconstpointer key)
1182 MyKey *k = (MyKey *) key;
1184 return (k->keycode << 8) + k->modifier;
1187 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1189 MyKey *ka = (MyKey *) a;
1190 MyKey *kb = (MyKey *) b;
1192 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1195 /* Stolen from xfwm4 and modified.
1196 * FALSE on error. Call initModifiers before this.
1198 static gboolean grabKey(MyKey *key)
1200 Window root;
1201 Display *dpy = GDK_DISPLAY();
1202 static gboolean need_init = TRUE;
1204 if (need_init)
1206 need_init = FALSE;
1207 gdk_window_add_filter(gdk_get_default_root_window(),
1208 filter_keys, NULL);
1211 gdk_error_trap_push();
1213 root = GDK_ROOT_WINDOW();
1215 GRAB(key, 0);
1217 /* Here we grab all combinations of well known modifiers */
1218 GRAB(key, ScrollLockMask);
1219 GRAB(key, NumLockMask);
1220 GRAB(key, CapsLockMask);
1221 GRAB(key, ScrollLockMask | NumLockMask);
1222 GRAB(key, ScrollLockMask | CapsLockMask);
1223 GRAB(key, CapsLockMask | NumLockMask);
1224 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1226 gdk_flush();
1227 return gdk_error_trap_pop() == Success;
1230 static gboolean ungrabKey(MyKey *key)
1232 Window root = GDK_ROOT_WINDOW();
1233 Display *dpy = GDK_DISPLAY();
1235 gdk_error_trap_push();
1237 UNGRAB(key, 0);
1239 UNGRAB(key, ScrollLockMask);
1240 UNGRAB(key, NumLockMask);
1241 UNGRAB(key, CapsLockMask);
1242 UNGRAB(key, ScrollLockMask | NumLockMask);
1243 UNGRAB(key, ScrollLockMask | CapsLockMask);
1244 UNGRAB(key, CapsLockMask | NumLockMask);
1245 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1247 gdk_flush();
1248 return gdk_error_trap_pop() == Success;
1251 static void ungrab_key(Icon *icon)
1253 int *count;
1255 g_return_if_fail(icon != NULL);
1257 if (!icon->shortcut_key.keycode)
1258 return;
1260 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1262 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1263 g_return_if_fail(count != NULL);
1265 (*count)--;
1267 if (*count > 0)
1268 return;
1270 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1272 if (!ungrabKey(&icon->shortcut_key))
1273 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1274 icon->shortcut, icon->item->leafname);
1277 static void grab_key(Icon *icon)
1279 MyKey *hash_key;
1280 int *count;
1282 g_return_if_fail(icon != NULL);
1284 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1286 if (!icon->shortcut_key.keycode)
1287 return;
1289 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1291 if (!grab_counter)
1292 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1293 g_free, NULL);
1295 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1296 if (count)
1298 (*count)++;
1299 return; /* Already grabbed */
1302 hash_key = g_new(MyKey, 1);
1303 *hash_key = icon->shortcut_key;
1304 count = g_new(int, 1);
1305 *count = 1;
1306 g_hash_table_insert(grab_counter, hash_key, count);
1308 if (!grabKey(&icon->shortcut_key))
1309 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1310 "Some other application may be already using it!\n",
1311 icon->shortcut, icon->item->leafname);
1315 static void icon_wink(Icon *icon)
1317 IconClass *iclass;
1319 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1320 g_return_if_fail(iclass->wink != NULL);
1322 iclass->wink(icon);
1325 /* Sets icon_menu */
1326 static void create_menu(void)
1328 GList *items;
1329 guchar *tmp;
1330 GtkItemFactory *item_factory;
1332 g_return_if_fail(icon_menu == NULL);
1334 item_factory = menu_create(menu_def,
1335 sizeof(menu_def) / sizeof(*menu_def),
1336 "<icon>", NULL);
1338 tmp = g_strconcat("<icon>/", _("File"), NULL);
1339 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
1340 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
1341 g_free(tmp);
1343 /* File '' label... */
1344 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
1345 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
1346 g_list_free(items);
1348 /* Shift Open... label */
1349 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
1350 file_shift_item = GTK_BIN(items->data)->child;
1351 g_list_free(items);
1353 g_signal_connect(icon_menu, "unmap_event",
1354 G_CALLBACK(menu_closed), NULL);