r2313: 2002 -> 2003
[rox-filer.git] / ROX-Filer / src / icon.c
blob12ba8bdbc3db0733e29d81991a402d3aaaaa5c16
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* icon.c - abstract base class for pinboard and panel icons.
24 * An Icon contains the full pathname of its file and the DirItem for that
25 * file. Icons emit the following signals:
27 * redraw - the image, details or selection state have changed.
28 * update - the name or path has changed.
29 * destroy - someone wishes to remove the icon.
31 * Note that an icon may be removed without emitting 'destroy'.
34 #include "config.h"
36 #include <string.h>
37 #include <gtk/gtk.h>
38 #include <X11/keysym.h>
39 #include <gdk/gdkx.h>
41 #include "global.h"
43 #include "main.h"
44 #include "gui_support.h"
45 #include "support.h"
46 #include "icon.h"
47 #include "diritem.h"
48 #include "menu.h"
49 #include "appmenu.h"
50 #include "dnd.h"
51 #include "run.h"
52 #include "infobox.h"
53 #include "pixmaps.h"
54 #include "mount.h"
55 #include "type.h"
56 #include "usericons.h"
57 #include "pinboard.h" /* For pinboard_set_backdrop */
59 static gboolean have_primary = FALSE; /* We own the PRIMARY selection? */
61 GtkWidget *icon_menu; /* The popup icon menu */
62 static GtkWidget *icon_file_menu; /* The file submenu */
63 static GtkWidget *icon_file_item; /* 'File' label */
64 static GtkWidget *file_shift_item; /* 'Shift Open' label */
66 /* A list of selected Icons. Every icon in the list is from the same group
67 * (eg, you can't have icons from two different panels selected at the
68 * same time).
70 GList *icon_selection = NULL;
72 /* A list of Icons which have grabs in effect. The same combo may be
73 * listed more than once, but has only one X grab.
75 GList *icon_shortcuts = NULL;
77 #define CLICK_TO_SET _("(click to set)")
79 static unsigned int AltMask;
80 static unsigned int MetaMask;
81 static unsigned int NumLockMask;
82 static unsigned int ScrollLockMask;
83 static unsigned int CapsLockMask;
84 static unsigned int SuperMask;
85 static unsigned int HyperMask;
87 /* {MyKey -> Number of grabs} */
88 static GHashTable *grab_counter = NULL;
90 /* Each entry is a GList of Icons which have the given pathname.
91 * This allows us to update all necessary icons when something changes.
93 static GHashTable *icons_hash = NULL; /* path -> [Icon] */
95 static Icon *menu_icon = NULL; /* Item clicked if there is no selection */
97 /* Static prototypes */
98 static void rename_activate(GtkWidget *dialog);
99 static void menu_closed(GtkWidget *widget);
100 static void lose_selection(GtkClipboard *primary, gpointer data);
101 static void selection_get(GtkClipboard *primary,
102 GtkSelectionData *selection_data,
103 guint info,
104 gpointer data);
105 static void remove_items(gpointer data, guint action, GtkWidget *widget);
106 static void set_backdrop(gpointer data, guint action, GtkWidget *widget);
107 static void file_op(gpointer data, guint action, GtkWidget *widget);
108 static void show_rename_box(Icon *icon);
109 static void icon_set_selected_int(Icon *icon, gboolean selected);
110 static void icon_class_init(gpointer gclass, gpointer data);
111 static void icon_init(GTypeInstance *object, gpointer gclass);
112 static void icon_hash_path(Icon *icon);
113 static void icon_unhash_path(Icon *icon);
114 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n);
115 static void ungrab_key(Icon *icon);
116 static void grab_key(Icon *icon);
117 static void parseKeyString(MyKey *key, const char *str);
118 static void icon_wink(Icon *icon);
120 enum {
121 ACTION_SHIFT,
122 ACTION_HELP,
123 ACTION_INFO,
124 ACTION_RUN_ACTION,
125 ACTION_SET_ICON,
126 ACTION_EDIT,
127 ACTION_LOCATION,
130 #undef N_
131 #define N_(x) x
132 static GtkItemFactoryEntry menu_def[] = {
133 {N_("ROX-Filer"), NULL, NULL, 0, "<Branch>"},
134 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
135 {">" N_("Show Help Files"), NULL, menu_rox_help, HELP_DIR, NULL},
136 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
137 {">", NULL, NULL, 0, "<Separator>"},
138 {">" N_("Options..."), NULL, menu_show_options, 0, NULL},
139 {">" N_("Home Directory"), NULL, open_home, 0, NULL},
140 {N_("File"), NULL, NULL, 0, "<Branch>"},
141 {">" N_("Shift Open"), NULL, file_op, ACTION_SHIFT, NULL},
142 {">" N_("Help"), NULL, file_op, ACTION_HELP, NULL},
143 {">" N_("Info"), NULL, file_op, ACTION_INFO, NULL},
144 {">" N_("Set Run Action..."), NULL, file_op, ACTION_RUN_ACTION, NULL},
145 {">" N_("Set Icon..."), NULL, file_op, ACTION_SET_ICON, NULL},
146 {N_("Edit Item"), NULL, file_op, ACTION_EDIT, NULL},
147 {N_("Show Location"), NULL, file_op, ACTION_LOCATION, NULL},
148 {N_("Remove Item(s)"), NULL, remove_items, 0, NULL},
149 {"", NULL, NULL, 0, "<Separator>"},
150 {N_("Backdrop..."), NULL, set_backdrop, 0, NULL},
153 /****************************************************************
154 * EXTERNAL INTERFACE *
155 ****************************************************************/
157 /* Called when the pointer moves over the icon */
158 void icon_may_update(Icon *icon)
160 MaskedPixmap *image;
161 int flags;
163 g_return_if_fail(icon != NULL);
165 image = icon->item->image;
166 flags = icon->item->flags;
168 if (image)
169 g_object_ref(image);
170 mount_update(FALSE);
171 diritem_restat(icon->path, icon->item, NULL);
173 if (icon->item->image != image || icon->item->flags != flags)
175 /* Appearance changed; need to redraw */
176 g_signal_emit_by_name(icon, "redraw");
179 if (image)
180 g_object_unref(image);
183 /* If path is on an icon then it may have changed... check! */
184 void icons_may_update(const gchar *path)
186 GList *affected;
188 if (icons_hash)
190 affected = g_hash_table_lookup(icons_hash, path);
192 for (; affected; affected = affected->next)
193 icon_may_update((Icon *) affected->data);
197 typedef struct _CheckData CheckData;
198 struct _CheckData {
199 const gchar *path;
200 gboolean found;
203 static void check_has(gpointer key, GList *icons, CheckData *check)
205 Icon *icon;
207 g_return_if_fail(icons != NULL);
209 icon = icons->data;
211 if (is_sub_dir(icon->path, check->path))
212 check->found = TRUE;
215 /* Returns TRUE if any icon links to this file (or any file inside
216 * this directory). Used to check that it's OK to delete 'path'.
218 gboolean icons_require(const gchar *path)
220 CheckData check;
222 if (!icons_hash)
223 return FALSE;
225 check.path = path;
226 check.found = FALSE;
227 g_hash_table_foreach(icons_hash, (GHFunc) check_has, &check);
229 return check.found;
232 /* Menu was clicked over this icon. Set things up correctly (shade items,
233 * add app menu stuff, etc).
234 * You should show icon_menu after calling this...
236 void icon_prepare_menu(Icon *icon, gboolean pinboard)
238 appmenu_remove();
240 menu_icon = icon;
242 menu_set_hidden(icon_menu, !pinboard, 5, 2);
244 /* Shade Remove Item(s) unless there is a selection */
245 menu_set_items_shaded(icon_menu,
246 (icon_selection || menu_icon) ? FALSE : TRUE,
247 4, 1);
249 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
250 FALSE);
252 /* Shade the File/Edit/Show items unless an item was clicked */
253 if (icon)
255 guchar *tmp;
257 menu_set_items_shaded(icon_menu, FALSE, 1, 3);
258 menu_set_items_shaded(icon_file_menu, FALSE, 0, 6);
259 if (!can_set_run_action(icon->item))
260 menu_set_items_shaded(icon_file_menu, TRUE, 3, 1);
262 tmp = g_strdup_printf("%s '%s'",
263 basetype_name(icon->item),
264 icon->item->leafname);
265 gtk_label_set_text(GTK_LABEL(icon_file_item), tmp);
266 g_free(tmp);
268 /* Check for app-specific menu */
269 appmenu_add(icon->path, icon->item, icon_menu);
271 else
273 menu_set_items_shaded(icon_menu, TRUE, 1, 3);
274 menu_set_items_shaded(icon_file_menu, TRUE, 0, 6);
275 gtk_label_set_text(GTK_LABEL(icon_file_item), _("Nothing"));
279 /* Set whether this icon is selected. Will automatically clear the selection
280 * if it contains icons from a different group.
282 void icon_set_selected(Icon *icon, gboolean selected)
284 if (selected && icon_selection)
286 IconClass *iclass;
287 Icon *other = (Icon *) icon_selection->data;
289 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
290 if (!iclass->same_group(icon, other))
292 icon_select_only(icon);
293 return;
297 icon_set_selected_int(icon, selected);
300 /* Clear everything, except 'select', which is selected.
301 * If select is NULL, unselects everything.
303 void icon_select_only(Icon *select)
305 GList *to_clear, *next;
307 if (select)
308 icon_set_selected_int(select, TRUE);
310 to_clear = g_list_copy(icon_selection);
312 if (select)
313 to_clear = g_list_remove(to_clear, select);
315 for (next = to_clear; next; next = next->next)
316 icon_set_selected_int((Icon *) next->data, FALSE);
318 g_list_free(to_clear);
321 /* Destroys this icon's widget, causing it to be removed from the screen */
322 void icon_destroy(Icon *icon)
324 g_return_if_fail(icon != NULL);
326 icon_set_selected_int(icon, FALSE);
328 g_signal_emit_by_name(icon, "destroy");
331 /* Return a text/uri-list of all the icons in the selection */
332 gchar *icon_create_uri_list(void)
334 GString *tmp;
335 guchar *retval;
336 guchar *leader;
337 GList *next;
339 tmp = g_string_new(NULL);
340 leader = g_strdup_printf("file://%s", our_host_name_for_dnd());
342 for (next = icon_selection; next; next = next->next)
344 Icon *icon = (Icon *) next->data;
346 g_string_append(tmp, leader);
347 g_string_append(tmp, icon->path);
348 g_string_append(tmp, "\r\n");
351 g_free(leader);
352 retval = tmp->str;
353 g_string_free(tmp, FALSE);
355 return retval;
358 GType icon_get_type(void)
360 static GType type = 0;
362 if (!type)
364 static const GTypeInfo info =
366 sizeof (IconClass),
367 NULL, /* base_init */
368 NULL, /* base_finalise */
369 icon_class_init,
370 NULL, /* class_finalise */
371 NULL, /* class_data */
372 sizeof(Icon),
373 0, /* n_preallocs */
374 icon_init
377 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
378 &info, 0);
381 return type;
384 /* Sets, unsets or changes the pathname and name for an icon.
385 * Updates icons_hash and (re)stats the item.
386 * If name is NULL then gets the leafname from pathname.
387 * If pathname is NULL, frees everything.
389 void icon_set_path(Icon *icon, const char *pathname, const char *name)
391 if (icon->path)
393 icon_unhash_path(icon);
394 icon->src_path = NULL;
395 icon->path = NULL;
397 diritem_free(icon->item);
398 icon->item = NULL;
401 if (pathname)
403 icon->src_path = g_strdup(pathname);
404 icon->path = expand_path(pathname);
406 icon_hash_path(icon);
408 if (!name)
409 name = g_basename(pathname);
411 icon->item = diritem_new(name);
412 diritem_restat(icon->path, icon->item, NULL);
416 void icon_set_shortcut(Icon *icon, const gchar *shortcut)
418 g_return_if_fail(icon != NULL);
420 if (shortcut && !*shortcut)
421 shortcut = NULL;
422 if (icon->shortcut == shortcut)
423 return;
424 if (icon->shortcut && shortcut && strcmp(icon->shortcut, shortcut) == 0)
425 return;
427 ungrab_key(icon);
429 g_free(icon->shortcut);
430 icon->shortcut = g_strdup(shortcut);
431 parseKeyString(&icon->shortcut_key, shortcut);
433 grab_key(icon);
436 /****************************************************************
437 * INTERNAL FUNCTIONS *
438 ****************************************************************/
440 /* The icons_hash table allows us to convert from a path to a list
441 * of icons that use that path.
442 * Add this icon to the list for its path.
444 static void icon_hash_path(Icon *icon)
446 GList *list;
448 g_return_if_fail(icon != NULL);
450 /* g_print("[ hashing '%s' ]\n", icon->path); */
452 list = g_hash_table_lookup(icons_hash, icon->path);
453 list = g_list_prepend(list, icon);
454 g_hash_table_insert(icons_hash, icon->path, list);
457 /* Remove this icon from the icons_hash table */
458 static void icon_unhash_path(Icon *icon)
460 GList *list;
462 g_return_if_fail(icon != NULL);
464 /* g_print("[ unhashing '%s' ]\n", icon->path); */
466 list = g_hash_table_lookup(icons_hash, icon->path);
467 g_return_if_fail(list != NULL);
469 list = g_list_remove(list, icon);
471 /* Remove it first; the hash key may have changed address */
472 g_hash_table_remove(icons_hash, icon->path);
473 if (list)
474 g_hash_table_insert(icons_hash,
475 ((Icon *) list->data)->path, list);
478 static void rename_activate(GtkWidget *dialog)
480 GtkWidget *entry, *src, *shortcut;
481 Icon *icon;
482 const guchar *new_name, *new_src, *new_shortcut;
484 entry = g_object_get_data(G_OBJECT(dialog), "new_name");
485 icon = g_object_get_data(G_OBJECT(dialog), "callback_icon");
486 src = g_object_get_data(G_OBJECT(dialog), "new_path");
487 shortcut = g_object_get_data(G_OBJECT(dialog), "new_shortcut");
489 g_return_if_fail(entry != NULL &&
490 src != NULL &&
491 icon != NULL &&
492 shortcut != NULL);
494 new_name = gtk_entry_get_text(GTK_ENTRY(entry));
495 new_src = gtk_entry_get_text(GTK_ENTRY(src));
496 new_shortcut = gtk_label_get_text(GTK_LABEL(shortcut));
497 if (strcmp(new_shortcut, CLICK_TO_SET) == 0)
498 new_shortcut = NULL;
500 if (*new_src == '\0')
501 report_error(
502 _("The location must contain at least one character!"));
503 else
505 icon_set_path(icon, new_src, new_name);
506 icon_set_shortcut(icon, new_shortcut);
507 g_signal_emit_by_name(icon, "update");
508 gtk_widget_destroy(dialog);
512 static void menu_closed(GtkWidget *widget)
514 appmenu_remove();
515 menu_icon = NULL;
518 /* Called when another application takes the selection away from us */
519 static void lose_selection(GtkClipboard *primary, gpointer data)
521 have_primary = FALSE;
522 icon_select_only(NULL);
525 /* Called when another application wants the contents of our selection */
526 static void selection_get(GtkClipboard *primary,
527 GtkSelectionData *selection_data,
528 guint info,
529 gpointer data)
531 gchar *text;
533 if (info == TARGET_URI_LIST)
534 text = icon_create_uri_list();
535 else
537 GList *next;
538 GString *str;
540 str = g_string_new(NULL);
542 for (next = icon_selection; next; next = next->next)
544 Icon *icon = (Icon *) next->data;
546 g_string_append(str, icon->path);
547 g_string_append_c(str, ' ');
550 text = str->str;
551 g_string_free(str, FALSE);
554 gtk_selection_data_set(selection_data,
555 gdk_atom_intern("STRING", FALSE),
556 8, text, strlen(text));
559 static void set_backdrop(gpointer data, guint action, GtkWidget *widget)
561 pinboard_set_backdrop();
564 static void remove_items(gpointer data, guint action, GtkWidget *widget)
566 IconClass *iclass;
568 if (menu_icon)
569 icon_set_selected(menu_icon, TRUE);
571 if (!icon_selection)
573 delayed_error(
574 _("You must first select some items to remove"));
575 return;
578 iclass = (IconClass *)
579 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection->data));
581 iclass->remove_items();
584 static void file_op(gpointer data, guint action, GtkWidget *widget)
586 if (!menu_icon)
588 delayed_error(_("You must open the menu over an item"));
589 return;
592 switch (action)
594 case ACTION_SHIFT:
595 run_diritem(menu_icon->path, menu_icon->item,
596 NULL, NULL, TRUE);
597 break;
598 case ACTION_EDIT:
599 show_rename_box(menu_icon);
600 break;
601 case ACTION_LOCATION:
602 open_to_show(menu_icon->path);
603 break;
604 case ACTION_HELP:
605 show_item_help(menu_icon->path, menu_icon->item);
606 break;
607 case ACTION_INFO:
608 infobox_new(menu_icon->path);
609 break;
610 case ACTION_RUN_ACTION:
611 if (can_set_run_action(menu_icon->item))
612 type_set_handler_dialog(
613 menu_icon->item->mime_type);
614 else
615 report_error(
616 _("You can only set the run action for a "
617 "regular file"));
618 break;
619 case ACTION_SET_ICON:
620 icon_set_handler_dialog(menu_icon->item,
621 menu_icon->path);
622 break;
626 static void edit_response(GtkWidget *dialog, gint response, gpointer data)
628 if (response == GTK_RESPONSE_OK)
629 rename_activate(dialog);
630 else if (response == GTK_RESPONSE_CANCEL)
631 gtk_widget_destroy(dialog);
634 static GdkFilterReturn filter_get_key(GdkXEvent *xevent,
635 GdkEvent *event,
636 gpointer data)
638 XKeyEvent *kev = (XKeyEvent *) xevent;
639 GtkWidget *popup = (GtkWidget *) data;
640 Display *dpy = GDK_DISPLAY();
642 if (kev->type != KeyRelease && kev->type != ButtonPressMask)
643 return GDK_FILTER_CONTINUE;
645 if (kev->type == KeyRelease)
647 gchar *str;
648 KeySym sym;
649 unsigned int m = kev->state;
651 sym = XKeycodeToKeysym(dpy, kev->keycode, 0);
652 if (!sym)
653 return GDK_FILTER_CONTINUE;
655 str = g_strdup_printf("%s%s%s%s%s%s%s",
656 m & ControlMask ? "Control+" : "",
657 m & ShiftMask ? "Shift+" : "",
658 m & AltMask ? "Alt+" : "",
659 m & MetaMask ? "Hyper+" : "",
660 m & SuperMask ? "Super+" : "",
661 m & HyperMask ? "Hyper+" : "",
662 XKeysymToString(sym));
664 g_object_set_data(G_OBJECT(popup), "chosen-key", str);
667 gdk_window_remove_filter(popup->window, filter_get_key, data);
668 gtk_widget_destroy(popup);
670 return GDK_FILTER_REMOVE;
673 static void may_set_shortcut(GtkWidget *popup, GtkWidget *label)
675 gchar *str;
677 str = g_object_get_data(G_OBJECT(popup), "chosen-key");
678 if (str)
680 gtk_label_set_text(GTK_LABEL(label), str);
681 g_free(str);
682 g_object_set_data(G_OBJECT(popup), "chosen-key", NULL);
686 static void get_shortcut(GtkWidget *button, GtkWidget *label)
688 GtkWidget *popup, *frame, *msg;
689 Window xid;
690 Display *dpy = GDK_DISPLAY();
692 popup = gtk_window_new(GTK_WINDOW_POPUP);
694 gtk_window_set_position(GTK_WINDOW(popup), GTK_WIN_POS_CENTER);
696 frame = gtk_frame_new(NULL);
697 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
698 gtk_container_add(GTK_CONTAINER(popup), frame);
700 msg = gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
702 gtk_misc_set_padding(GTK_MISC(msg), 20, 20);
703 gtk_container_add(GTK_CONTAINER(frame), msg);
705 gtk_window_set_modal(GTK_WINDOW(popup), TRUE);
707 gtk_widget_add_events(popup,
708 GDK_KEY_RELEASE_MASK | GDK_BUTTON_PRESS_MASK);
710 g_signal_connect(popup, "destroy",
711 G_CALLBACK(may_set_shortcut), label);
713 gtk_widget_show_all(popup);
715 gdk_window_add_filter(popup->window, filter_get_key, popup);
717 xid = gdk_x11_drawable_get_xid(popup->window);
719 if (XGrabKeyboard(dpy, xid, False, GrabModeAsync, GrabModeAsync,
720 gtk_get_current_event_time()) != Success)
722 delayed_error(_("Failed to get keyboard grab!"));
723 gtk_widget_destroy(popup);
726 if (XGrabPointer(dpy, xid, False, ButtonPressMask, GrabModeAsync,
727 GrabModeAsync, xid, None,
728 gtk_get_current_event_time()) != Success)
730 g_warning("Failed to get mouse grab");
734 static void clear_shortcut(GtkButton *clear, GtkLabel *label)
736 gtk_label_set_text(GTK_LABEL(label), CLICK_TO_SET);
739 /* Opens a box allowing the user to change the name of a pinned icon.
740 * If the icon is destroyed then the box will close.
741 * If the user chooses OK then the callback is called once the icon's
742 * name, src_path and path fields have been updated and the item field
743 * restatted.
745 static void show_rename_box(Icon *icon)
747 GtkDialog *dialog;
748 GtkWidget *label, *entry, *button, *button2, *hbox, *spacer;
749 GtkBox *vbox;
751 if (icon->dialog)
753 gtk_window_present(GTK_WINDOW(icon->dialog));
754 return;
757 icon->dialog = gtk_dialog_new();
758 g_signal_connect(icon->dialog, "destroy",
759 G_CALLBACK(gtk_widget_destroyed), &icon->dialog);
761 dialog = GTK_DIALOG(icon->dialog);
763 vbox = GTK_BOX(gtk_vbox_new(FALSE, 1));
764 gtk_box_pack_start(GTK_BOX(dialog->vbox), (GtkWidget *) vbox,
765 TRUE, TRUE, 0);
766 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
768 gtk_window_set_title(GTK_WINDOW(dialog), _("Edit Item"));
769 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
771 label = gtk_label_new(_("Clicking the icon opens:"));
772 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
774 entry = gtk_entry_new();
775 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
776 gtk_entry_set_text(GTK_ENTRY(entry), icon->src_path);
777 g_object_set_data(G_OBJECT(dialog), "new_path", entry);
778 g_signal_connect_swapped(entry, "activate",
779 G_CALLBACK(rename_activate), dialog);
781 spacer = gtk_drawing_area_new();
782 gtk_widget_set_size_request(spacer, 4, 4);
783 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
785 label = gtk_label_new(_("The text displayed under the icon is:"));
786 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
787 entry = gtk_entry_new();
788 gtk_box_pack_start(vbox, entry, TRUE, FALSE, 2);
789 gtk_entry_set_text(GTK_ENTRY(entry), icon->item->leafname);
790 gtk_widget_grab_focus(entry);
791 g_object_set_data(G_OBJECT(dialog), "new_name", entry);
792 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
794 spacer = gtk_drawing_area_new();
795 gtk_widget_set_size_request(spacer, 4, 4);
796 gtk_box_pack_start(vbox, spacer, FALSE, FALSE, 0);
798 label = gtk_label_new(_("The keyboard shortcut is:"));
799 gtk_box_pack_start(vbox, label, TRUE, TRUE, 0);
801 hbox = gtk_hbox_new(FALSE, 2);
802 gtk_box_pack_start(vbox, hbox, TRUE, FALSE, 0);
803 button = gtk_button_new_with_label(icon->shortcut
804 ? icon->shortcut
805 : CLICK_TO_SET);
806 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
807 g_object_set_data(G_OBJECT(dialog), "new_shortcut",
808 GTK_BIN(button)->child);
809 g_signal_connect(button, "clicked",
810 G_CALLBACK(get_shortcut),
811 GTK_BIN(button)->child);
812 button2 = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
813 gtk_box_pack_start(GTK_BOX(hbox), button2, FALSE, FALSE, 0);
814 g_signal_connect(button2, "clicked",
815 G_CALLBACK(clear_shortcut),
816 GTK_BIN(button)->child);
818 g_object_set_data(G_OBJECT(dialog), "callback_icon", icon);
820 gtk_dialog_add_buttons(dialog,
821 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
822 GTK_STOCK_OK, GTK_RESPONSE_OK,
823 NULL);
824 gtk_dialog_set_default_response(dialog, GTK_RESPONSE_OK);
826 g_signal_connect(dialog, "response", G_CALLBACK(edit_response), NULL);
828 gtk_widget_show_all(GTK_WIDGET(dialog));
831 static gpointer parent_class = NULL;
833 static void icon_finialize(GObject *object)
835 Icon *icon = (Icon *) object;
837 g_return_if_fail(!icon->selected);
839 if (icon->dialog)
840 gtk_widget_destroy(icon->dialog);
842 if (icon == menu_icon)
843 menu_icon = NULL;
845 icon_set_path(icon, NULL, NULL);
846 icon_set_shortcut(icon, NULL);
848 G_OBJECT_CLASS(parent_class)->finalize(object);
851 static void icon_class_init(gpointer gclass, gpointer data)
853 guchar *tmp;
854 GList *items;
855 GtkItemFactory *item_factory;
856 GObjectClass *object = (GObjectClass *) gclass;
857 IconClass *icon = (IconClass *) gclass;
859 parent_class = g_type_class_peek_parent(gclass);
861 object->finalize = icon_finialize;
862 icon->destroy = NULL;
863 icon->redraw = NULL;
864 icon->update = NULL;
865 icon->same_group = NULL;
866 icon->wink = NULL;
868 g_signal_new("update",
869 G_TYPE_FROM_CLASS(gclass),
870 G_SIGNAL_RUN_LAST,
871 G_STRUCT_OFFSET(IconClass, update),
872 NULL, NULL,
873 g_cclosure_marshal_VOID__VOID,
874 G_TYPE_NONE, 0);
876 g_signal_new("destroy",
877 G_TYPE_FROM_CLASS(gclass),
878 G_SIGNAL_RUN_LAST,
879 G_STRUCT_OFFSET(IconClass, destroy),
880 NULL, NULL,
881 g_cclosure_marshal_VOID__VOID,
882 G_TYPE_NONE, 0);
884 g_signal_new("redraw",
885 G_TYPE_FROM_CLASS(gclass),
886 G_SIGNAL_RUN_LAST,
887 G_STRUCT_OFFSET(IconClass, redraw),
888 NULL, NULL,
889 g_cclosure_marshal_VOID__VOID,
890 G_TYPE_NONE, 0);
892 icons_hash = g_hash_table_new(g_str_hash, g_str_equal);
894 item_factory = menu_create(menu_def,
895 sizeof(menu_def) / sizeof(*menu_def),
896 "<icon>", NULL);
898 tmp = g_strconcat("<icon>/", _("File"), NULL);
899 icon_menu = gtk_item_factory_get_widget(item_factory, "<icon>");
900 icon_file_menu = gtk_item_factory_get_widget(item_factory, tmp);
901 g_free(tmp);
903 /* File '' label... */
904 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
905 icon_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
906 g_list_free(items);
908 /* Shift Open... label */
909 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
910 file_shift_item = GTK_BIN(items->data)->child;
911 g_list_free(items);
913 g_signal_connect(icon_menu, "unmap_event",
914 G_CALLBACK(menu_closed), NULL);
917 static void icon_init(GTypeInstance *object, gpointer gclass)
919 Icon *icon = (Icon *) object;
921 icon->selected = FALSE;
922 icon->src_path = NULL;
923 icon->path = NULL;
924 icon->item = NULL;
925 icon->dialog = NULL;
926 icon->shortcut = NULL;
927 icon->shortcut_key.keycode = 0;
928 icon->shortcut_key.modifier = 0;
931 /* As icon_set_selected(), but doesn't automatically unselect incompatible
932 * icons.
934 static void icon_set_selected_int(Icon *icon, gboolean selected)
936 static GtkClipboard *primary;
938 g_return_if_fail(icon != NULL);
940 if (icon->selected == selected)
941 return;
943 if (!primary)
944 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
946 if (selected)
948 icon_selection = g_list_prepend(icon_selection, icon);
949 if (!have_primary)
951 GtkTargetEntry target_table[] =
953 {"text/uri-list", 0, TARGET_URI_LIST},
954 {"UTF8", 0, TARGET_STRING},
955 {"COMPOUND_TEXT", 0, TARGET_STRING},
956 {"STRING", 0, TARGET_STRING},
959 /* Grab selection */
960 have_primary = gtk_clipboard_set_with_data(primary,
961 target_table,
962 sizeof(target_table) / sizeof(*target_table),
963 selection_get, lose_selection, NULL);
966 else
968 icon_selection = g_list_remove(icon_selection, icon);
969 if (have_primary && !icon_selection)
971 have_primary = FALSE;
972 gtk_clipboard_clear(primary);
976 icon->selected = selected;
977 g_signal_emit_by_name(icon, "redraw");
980 static void menu_set_hidden(GtkWidget *menu, gboolean hidden, int from, int n)
982 GList *items, *item;
984 items = gtk_container_get_children(GTK_CONTAINER(menu));
986 item = g_list_nth(items, from);
987 while (item && n--)
989 if (hidden)
990 gtk_widget_hide(GTK_WIDGET(item->data));
991 else
992 gtk_widget_show(GTK_WIDGET(item->data));
993 item = item->next;
995 g_list_free(items);
998 /* Stolen from xfwm4 */
999 static void initModifiers(void)
1001 Display *dpy = GDK_DISPLAY();
1002 XModifierKeymap *xmk = XGetModifierMapping(dpy);
1003 int m, k;
1005 AltMask = MetaMask = NumLockMask = ScrollLockMask = CapsLockMask =
1006 SuperMask = HyperMask = 0;
1008 /* Work out which mask to use for each modifier group */
1009 if (xmk)
1011 KeyCode *c = xmk->modifiermap;
1012 KeyCode numLockKeyCode;
1013 KeyCode scrollLockKeyCode;
1014 KeyCode capsLockKeyCode;
1015 KeyCode altKeyCode;
1016 KeyCode metaKeyCode;
1017 KeyCode superKeyCode;
1018 KeyCode hyperKeyCode;
1020 /* Find the codes to search for... */
1021 numLockKeyCode = XKeysymToKeycode(dpy, XK_Num_Lock);
1022 scrollLockKeyCode = XKeysymToKeycode(dpy, XK_Scroll_Lock);
1023 capsLockKeyCode = XKeysymToKeycode(dpy, XK_Caps_Lock);
1024 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_L);
1025 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_L);
1026 superKeyCode = XKeysymToKeycode(dpy, XK_Super_L);
1027 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_L);
1029 /* If some are missing, try alternatives... */
1030 if (!altKeyCode)
1031 altKeyCode = XKeysymToKeycode(dpy, XK_Alt_R);
1032 if (!metaKeyCode)
1033 metaKeyCode = XKeysymToKeycode(dpy, XK_Meta_R);
1034 if (!superKeyCode)
1035 superKeyCode = XKeysymToKeycode(dpy, XK_Super_R);
1036 if (!hyperKeyCode)
1037 hyperKeyCode = XKeysymToKeycode(dpy, XK_Hyper_R);
1039 /* Check each of the eight modifier lists.
1040 * The idea (I think) is that we name the modifier group which
1041 * includes the Alt key as the 'Alt group', and so on for
1042 * the other modifiers.
1044 for (m = 0; m < 8; m++)
1046 for (k = 0; k < xmk->max_keypermod; k++, c++)
1048 if (*c == NoSymbol)
1049 continue;
1050 if (*c == numLockKeyCode)
1051 NumLockMask = (1 << m);
1052 if (*c == scrollLockKeyCode)
1053 ScrollLockMask = (1 << m);
1054 if (*c == capsLockKeyCode)
1055 CapsLockMask = (1 << m);
1056 if (*c == altKeyCode)
1057 AltMask = (1 << m);
1058 if (*c == metaKeyCode)
1059 MetaMask = (1 << m);
1060 if (*c == superKeyCode)
1061 SuperMask = (1 << m);
1062 if (*c == hyperKeyCode)
1063 HyperMask = (1 << m);
1066 XFreeModifiermap(xmk);
1069 if(MetaMask == AltMask)
1070 MetaMask = 0;
1072 if (AltMask != 0 && MetaMask == Mod1Mask)
1074 MetaMask = AltMask;
1075 AltMask = Mod1Mask;
1078 if (AltMask == 0 && MetaMask != 0)
1080 if (MetaMask != Mod1Mask)
1081 AltMask = Mod1Mask;
1082 else
1084 AltMask = MetaMask;
1085 MetaMask = 0;
1089 if (AltMask == 0)
1090 AltMask = Mod1Mask;
1094 /* Fill in key from str. Sets keycode to zero if str is NULL.
1095 * Stolen from xfwm4 and modified.
1097 static void parseKeyString(MyKey *key, const char *str)
1099 char *k;
1100 Display *dpy = GDK_DISPLAY();
1102 key->keycode = 0;
1103 key->modifier = 0;
1105 if (!str)
1106 return;
1108 k = strrchr(str, '+');
1109 key->keycode = XKeysymToKeycode(dpy, XStringToKeysym(k ? k + 1 : str));
1110 if (k)
1112 gchar *tmp;
1114 tmp = g_ascii_strdown(str, -1);
1116 if (strstr(tmp, "shift"))
1117 key->modifier = key->modifier | ShiftMask;
1118 if (strstr(tmp, "control"))
1119 key->modifier = key->modifier | ControlMask;
1120 if (strstr(tmp, "alt") || strstr(tmp, "mod1"))
1121 key->modifier = key->modifier | AltMask;
1122 if (strstr(tmp, "meta") || strstr(tmp, "mod2"))
1123 key->modifier = key->modifier | MetaMask;
1125 g_free(tmp);
1128 if (!key->keycode)
1129 g_warning("Can't parse key '%s'\n", str);
1132 static GdkFilterReturn filter_keys(GdkXEvent *xevent,
1133 GdkEvent *event,
1134 gpointer data)
1136 GList *next;
1137 XKeyEvent *kev = (XKeyEvent *) xevent;
1138 guint state;
1140 if (kev->type != KeyPress)
1141 return GDK_FILTER_CONTINUE;
1143 state = kev->state & (ShiftMask | ControlMask | AltMask | MetaMask);
1145 for (next = icon_shortcuts; next; next = next->next)
1147 Icon *icon = (Icon *) next->data;
1149 if (icon->shortcut_key.keycode == kev->keycode &&
1150 icon->shortcut_key.modifier == state)
1152 icon_wink(icon);
1153 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
1157 return GDK_FILTER_CONTINUE;
1160 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1161 root, False, GrabModeAsync, GrabModeAsync)
1162 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1163 root)
1165 static guint mykey_hash(gconstpointer key)
1167 MyKey *k = (MyKey *) key;
1169 return (k->keycode << 8) + k->modifier;
1172 static gboolean mykey_cmp(gconstpointer a, gconstpointer b)
1174 MyKey *ka = (MyKey *) a;
1175 MyKey *kb = (MyKey *) b;
1177 return ka->keycode == kb->keycode && kb->modifier == kb->modifier;
1180 /* Stolen from xfwm4 and modified.
1181 * FALSE on error.
1183 static gboolean grabKey(MyKey *key)
1185 Window root;
1186 Display *dpy = GDK_DISPLAY();
1187 static gboolean need_init = TRUE;
1189 if (need_init)
1191 need_init = FALSE;
1193 initModifiers();
1194 gdk_window_add_filter(gdk_get_default_root_window(),
1195 filter_keys, NULL);
1198 gdk_error_trap_push();
1200 root = GDK_ROOT_WINDOW();
1202 GRAB(key, 0);
1204 if (key->modifier != AnyModifier && key->modifier != 0)
1206 /* Here we grab all combinations of well known
1207 * modifiers.
1209 GRAB(key, ScrollLockMask);
1210 GRAB(key, NumLockMask);
1211 GRAB(key, CapsLockMask);
1212 GRAB(key, ScrollLockMask | NumLockMask);
1213 GRAB(key, ScrollLockMask | CapsLockMask);
1214 GRAB(key, CapsLockMask | NumLockMask);
1215 GRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1218 gdk_flush();
1219 return gdk_error_trap_pop() == Success;
1222 static gboolean ungrabKey(MyKey *key)
1224 Window root = GDK_ROOT_WINDOW();
1225 Display *dpy = GDK_DISPLAY();
1227 gdk_error_trap_push();
1229 UNGRAB(key, 0);
1231 if (key->modifier != AnyModifier && key->modifier != 0)
1233 UNGRAB(key, ScrollLockMask);
1234 UNGRAB(key, NumLockMask);
1235 UNGRAB(key, CapsLockMask);
1236 UNGRAB(key, ScrollLockMask | NumLockMask);
1237 UNGRAB(key, ScrollLockMask | CapsLockMask);
1238 UNGRAB(key, CapsLockMask | NumLockMask);
1239 UNGRAB(key, ScrollLockMask | CapsLockMask | NumLockMask);
1242 gdk_flush();
1243 return gdk_error_trap_pop() == Success;
1246 static void ungrab_key(Icon *icon)
1248 int *count;
1250 g_return_if_fail(icon != NULL);
1252 if (!icon->shortcut_key.keycode)
1253 return;
1255 icon_shortcuts = g_list_remove(icon_shortcuts, icon);
1257 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1258 g_return_if_fail(count != NULL);
1260 (*count)--;
1262 if (*count > 0)
1263 return;
1265 g_hash_table_remove(grab_counter, &icon->shortcut_key);
1267 if (!ungrabKey(&icon->shortcut_key))
1268 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1269 icon->shortcut, icon->item->leafname);
1272 static void grab_key(Icon *icon)
1274 MyKey *hash_key;
1275 int *count;
1277 g_return_if_fail(icon != NULL);
1279 g_return_if_fail(g_list_find(icon_shortcuts, icon) == NULL);
1281 if (!icon->shortcut_key.keycode)
1282 return;
1284 icon_shortcuts = g_list_prepend(icon_shortcuts, icon);
1286 if (!grab_counter)
1287 grab_counter = g_hash_table_new_full(mykey_hash, mykey_cmp,
1288 g_free, NULL);
1290 count = g_hash_table_lookup(grab_counter, &icon->shortcut_key);
1291 if (count)
1293 (*count)++;
1294 return; /* Already grabbed */
1297 hash_key = g_new(MyKey, 1);
1298 *hash_key = icon->shortcut_key;
1299 count = g_new(int, 1);
1300 *count = 1;
1301 g_hash_table_insert(grab_counter, hash_key, count);
1303 if (!grabKey(&icon->shortcut_key))
1304 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1305 "Some other application may be already using it!\n",
1306 icon->shortcut, icon->item->leafname);
1310 static void icon_wink(Icon *icon)
1312 IconClass *iclass;
1314 iclass = (IconClass *) G_OBJECT_GET_CLASS(icon);
1315 g_return_if_fail(iclass->wink != NULL);
1317 iclass->wink(icon);