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)
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
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'.
38 #include <X11/keysym.h>
44 #include "gui_support.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
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
,
105 static void remove_items(gpointer data
, guint action
, GtkWidget
*widget
);
106 static void set_backdrop(gpointer data
, guint action
, GtkWidget
*widget
);
107 static void file_op(gpointer data
, guint action
, GtkWidget
*widget
);
108 static void show_rename_box(Icon
*icon
);
109 static void icon_set_selected_int(Icon
*icon
, gboolean selected
);
110 static void icon_class_init(gpointer gclass
, gpointer data
);
111 static void icon_init(GTypeInstance
*object
, gpointer gclass
);
112 static void icon_hash_path(Icon
*icon
);
113 static void icon_unhash_path(Icon
*icon
);
114 static void menu_set_hidden(GtkWidget
*menu
, gboolean hidden
, int from
, int n
);
115 static void ungrab_key(Icon
*icon
);
116 static void grab_key(Icon
*icon
);
117 static void parseKeyString(MyKey
*key
, const char *str
);
118 static void icon_wink(Icon
*icon
);
119 static void initModifiers(void);
120 static void create_menu(void);
133 static GtkItemFactoryEntry menu_def
[] = {
134 {N_("ROX-Filer"), NULL
, NULL
, 0, "<Branch>"},
135 {">" N_("About ROX-Filer..."), NULL
, menu_rox_help
, HELP_ABOUT
, "<StockItem>", GTK_STOCK_DIALOG_INFO
},
136 {">" N_("Show Help Files"), NULL
, menu_rox_help
, HELP_DIR
, "<StockItem>", GTK_STOCK_HELP
},
137 {">" N_("Manual"), NULL
, menu_rox_help
, HELP_MANUAL
, NULL
},
138 {">", NULL
, NULL
, 0, "<Separator>"},
139 {">" N_("Options..."), NULL
, menu_show_options
, 0, "<StockItem>", GTK_STOCK_PREFERENCES
},
140 {">" N_("Home Directory"), NULL
, open_home
, 0, "<StockItem>", GTK_STOCK_HOME
},
141 {N_("File"), NULL
, NULL
, 0, "<Branch>"},
142 {">" N_("Shift Open"), NULL
, file_op
, ACTION_SHIFT
, NULL
},
143 {">" N_("Properties"), NULL
, file_op
, ACTION_PROPERTIES
, "<StockItem>", GTK_STOCK_PROPERTIES
},
144 {">" N_("Set Run Action..."), NULL
, file_op
, ACTION_RUN_ACTION
, "<StockItem>", GTK_STOCK_EXECUTE
},
145 {">" N_("Set Icon..."), NULL
, file_op
, ACTION_SET_ICON
, NULL
},
146 {N_("Edit Item"), NULL
, file_op
, ACTION_EDIT
, "<StockItem>", GTK_STOCK_PROPERTIES
},
147 {N_("Show Location"), NULL
, file_op
, ACTION_LOCATION
, "<StockItem>", GTK_STOCK_JUMP_TO
},
148 {N_("Remove Item(s)"), NULL
, remove_items
, 0, "<StockItem>", GTK_STOCK_REMOVE
},
149 {"", NULL
, NULL
, 0, "<Separator>"},
150 {N_("Backdrop..."), NULL
, set_backdrop
, 0, NULL
},
153 /****************************************************************
154 * EXTERNAL INTERFACE *
155 ****************************************************************/
157 /* Called when the pointer moves over the icon */
158 void icon_may_update(Icon
*icon
)
163 g_return_if_fail(icon
!= NULL
);
165 image
= icon
->item
->image
;
166 flags
= icon
->item
->flags
;
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
, "update");
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
)
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
;
203 static void check_has(gpointer key
, GList
*icons
, CheckData
*check
)
207 g_return_if_fail(icons
!= NULL
);
211 if (is_sub_dir(icon
->path
, check
->path
))
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
)
227 g_hash_table_foreach(icons_hash
, (GHFunc
) check_has
, &check
);
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
)
245 menu_set_hidden(icon_menu
, !pinboard
, 5, 2);
247 /* Shade Remove Item(s) unless there is a selection */
248 menu_set_items_shaded(icon_menu
,
249 (icon_selection
|| menu_icon
) ? FALSE
: TRUE
,
252 menu_show_shift_action(file_shift_item
, icon
? icon
->item
: NULL
,
255 /* Shade the File/Edit/Show items unless an item was clicked */
260 menu_set_items_shaded(icon_menu
, FALSE
, 1, 3);
261 menu_set_items_shaded(icon_file_menu
, FALSE
, 0, 5);
262 if (!can_set_run_action(icon
->item
))
263 menu_set_items_shaded(icon_file_menu
, TRUE
, 2, 1);
265 tmp
= g_strdup_printf(_("%s '%s'"),
266 basetype_name(icon
->item
),
267 icon
->item
->leafname
);
268 gtk_label_set_text(GTK_LABEL(icon_file_item
), tmp
);
271 /* Check for app-specific menu */
272 appmenu_add(icon
->path
, icon
->item
, icon_menu
);
276 menu_set_items_shaded(icon_menu
, TRUE
, 1, 3);
277 menu_set_items_shaded(icon_file_menu
, TRUE
, 0, 5);
278 gtk_label_set_text(GTK_LABEL(icon_file_item
), _("Nothing"));
282 /* Set whether this icon is selected. Will automatically clear the selection
283 * if it contains icons from a different group.
285 void icon_set_selected(Icon
*icon
, gboolean selected
)
287 if (selected
&& icon_selection
)
290 Icon
*other
= (Icon
*) icon_selection
->data
;
292 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
293 if (!iclass
->same_group(icon
, other
))
295 icon_select_only(icon
);
300 icon_set_selected_int(icon
, selected
);
303 /* Clear everything, except 'select', which is selected.
304 * If select is NULL, unselects everything.
306 void icon_select_only(Icon
*select
)
308 GList
*to_clear
, *next
;
311 icon_set_selected_int(select
, TRUE
);
313 to_clear
= g_list_copy(icon_selection
);
316 to_clear
= g_list_remove(to_clear
, select
);
318 for (next
= to_clear
; next
; next
= next
->next
)
319 icon_set_selected_int((Icon
*) next
->data
, FALSE
);
321 g_list_free(to_clear
);
324 /* Destroys this icon's widget, causing it to be removed from the screen */
325 void icon_destroy(Icon
*icon
)
327 g_return_if_fail(icon
!= NULL
);
329 icon_set_selected_int(icon
, FALSE
);
331 g_signal_emit_by_name(icon
, "destroy");
334 /* Return a text/uri-list of all the icons in the selection */
335 gchar
*icon_create_uri_list(void)
341 tmp
= g_string_new(NULL
);
343 for (next
= icon_selection
; next
; next
= next
->next
)
345 Icon
*icon
= (Icon
*) next
->data
;
348 uri
= encode_path_as_uri(icon
->path
);
349 g_string_append(tmp
, uri
);
351 g_string_append(tmp
, "\r\n");
355 g_string_free(tmp
, FALSE
);
360 GType
icon_get_type(void)
362 static GType type
= 0;
366 static const GTypeInfo info
=
369 NULL
, /* base_init */
370 NULL
, /* base_finalise */
372 NULL
, /* class_finalise */
373 NULL
, /* class_data */
379 type
= g_type_register_static(G_TYPE_OBJECT
, "Icon",
386 /* Sets, unsets or changes the pathname and name for an icon.
387 * Updates icons_hash and (re)stats the item.
388 * If name is NULL then gets the leafname from pathname.
389 * If pathname is NULL, frees everything.
391 void icon_set_path(Icon
*icon
, const char *pathname
, const char *name
)
395 icon_unhash_path(icon
);
396 icon
->src_path
= NULL
;
399 diritem_free(icon
->item
);
405 if (g_utf8_validate(pathname
, -1, NULL
))
406 icon
->src_path
= g_strdup(pathname
);
408 icon
->src_path
= to_utf8(pathname
);
409 icon
->path
= expand_path(icon
->src_path
);
411 icon_hash_path(icon
);
414 name
= g_basename(icon
->src_path
);
416 icon
->item
= diritem_new(name
);
417 diritem_restat(icon
->path
, icon
->item
, NULL
);
421 void icon_set_shortcut(Icon
*icon
, const gchar
*shortcut
)
423 g_return_if_fail(icon
!= NULL
);
425 if (shortcut
&& !*shortcut
)
427 if (icon
->shortcut
== shortcut
)
429 if (icon
->shortcut
&& shortcut
&& strcmp(icon
->shortcut
, shortcut
) == 0)
436 g_free(icon
->shortcut
);
437 icon
->shortcut
= g_strdup(shortcut
);
438 parseKeyString(&icon
->shortcut_key
, shortcut
);
443 /****************************************************************
444 * INTERNAL FUNCTIONS *
445 ****************************************************************/
447 /* The icons_hash table allows us to convert from a path to a list
448 * of icons that use that path.
449 * Add this icon to the list for its path.
451 static void icon_hash_path(Icon
*icon
)
455 g_return_if_fail(icon
!= NULL
);
457 /* g_print("[ hashing '%s' ]\n", icon->path); */
459 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
460 list
= g_list_prepend(list
, icon
);
461 g_hash_table_insert(icons_hash
, icon
->path
, list
);
464 /* Remove this icon from the icons_hash table */
465 static void icon_unhash_path(Icon
*icon
)
469 g_return_if_fail(icon
!= NULL
);
471 /* g_print("[ unhashing '%s' ]\n", icon->path); */
473 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
474 g_return_if_fail(list
!= NULL
);
476 list
= g_list_remove(list
, icon
);
478 /* Remove it first; the hash key may have changed address */
479 g_hash_table_remove(icons_hash
, icon
->path
);
481 g_hash_table_insert(icons_hash
,
482 ((Icon
*) list
->data
)->path
, list
);
485 static void rename_activate(GtkWidget
*dialog
)
487 GtkWidget
*entry
, *src
, *shortcut
;
489 const guchar
*new_name
, *new_src
, *new_shortcut
;
491 entry
= g_object_get_data(G_OBJECT(dialog
), "new_name");
492 icon
= g_object_get_data(G_OBJECT(dialog
), "callback_icon");
493 src
= g_object_get_data(G_OBJECT(dialog
), "new_path");
494 shortcut
= g_object_get_data(G_OBJECT(dialog
), "new_shortcut");
496 g_return_if_fail(entry
!= NULL
&&
501 new_name
= gtk_entry_get_text(GTK_ENTRY(entry
));
502 new_src
= gtk_entry_get_text(GTK_ENTRY(src
));
503 new_shortcut
= gtk_label_get_text(GTK_LABEL(shortcut
));
504 if (strcmp(new_shortcut
, CLICK_TO_SET
) == 0)
507 if (*new_src
== '\0')
509 _("The location must contain at least one character!"));
512 icon_set_path(icon
, new_src
, new_name
);
513 icon_set_shortcut(icon
, new_shortcut
);
514 g_signal_emit_by_name(icon
, "update");
515 gtk_widget_destroy(dialog
);
519 static void menu_closed(GtkWidget
*widget
)
525 /* Called when another application takes the selection away from us */
526 static void lose_selection(GtkClipboard
*primary
, gpointer data
)
528 have_primary
= FALSE
;
529 icon_select_only(NULL
);
532 /* Called when another application wants the contents of our selection */
533 static void selection_get(GtkClipboard
*primary
,
534 GtkSelectionData
*selection_data
,
540 if (info
== TARGET_URI_LIST
)
541 text
= icon_create_uri_list();
547 str
= g_string_new(NULL
);
549 for (next
= icon_selection
; next
; next
= next
->next
)
551 Icon
*icon
= (Icon
*) next
->data
;
553 g_string_append(str
, icon
->path
);
554 g_string_append_c(str
, ' ');
558 g_string_free(str
, FALSE
);
561 gtk_selection_data_set(selection_data
,
562 gdk_atom_intern("STRING", FALSE
),
563 8, text
, strlen(text
));
566 static void set_backdrop(gpointer data
, guint action
, GtkWidget
*widget
)
568 pinboard_set_backdrop_box();
571 static void remove_items(gpointer data
, guint action
, GtkWidget
*widget
)
576 icon_set_selected(menu_icon
, TRUE
);
581 _("You must first select some items to remove"));
585 iclass
= (IconClass
*)
586 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection
->data
));
588 iclass
->remove_items();
591 static void file_op(gpointer data
, guint action
, GtkWidget
*widget
)
595 delayed_error(_("You must open the menu over an item"));
602 run_diritem(menu_icon
->path
, menu_icon
->item
,
606 show_rename_box(menu_icon
);
608 case ACTION_LOCATION
:
609 open_to_show(menu_icon
->path
);
611 case ACTION_PROPERTIES
:
612 infobox_new(menu_icon
->path
);
614 case ACTION_RUN_ACTION
:
615 if (can_set_run_action(menu_icon
->item
))
616 type_set_handler_dialog(
617 menu_icon
->item
->mime_type
);
620 _("You can only set the run action for a "
623 case ACTION_SET_ICON
:
624 icon_set_handler_dialog(menu_icon
->item
,
630 static void edit_response(GtkWidget
*dialog
, gint response
, gpointer data
)
632 if (response
== GTK_RESPONSE_OK
)
633 rename_activate(dialog
);
634 else if (response
== GTK_RESPONSE_CANCEL
)
635 gtk_widget_destroy(dialog
);
638 static GdkFilterReturn
filter_get_key(GdkXEvent
*xevent
,
642 XKeyEvent
*kev
= (XKeyEvent
*) xevent
;
643 GtkWidget
*popup
= (GtkWidget
*) data
;
644 Display
*dpy
= GDK_DISPLAY();
646 if (kev
->type
!= KeyRelease
&& kev
->type
!= ButtonPressMask
)
647 return GDK_FILTER_CONTINUE
;
651 if (kev
->type
== KeyRelease
)
655 unsigned int m
= kev
->state
;
657 sym
= XKeycodeToKeysym(dpy
, kev
->keycode
, 0);
659 return GDK_FILTER_CONTINUE
;
661 str
= g_strdup_printf("%s%s%s%s%s%s%s",
662 m
& ControlMask
? "Control+" : "",
663 m
& ShiftMask
? "Shift+" : "",
664 m
& AltMask
? "Alt+" : "",
665 m
& MetaMask
? "Meta+" : "",
666 m
& SuperMask
? "Super+" : "",
667 m
& HyperMask
? "Hyper+" : "",
668 XKeysymToString(sym
));
670 g_object_set_data(G_OBJECT(popup
), "chosen-key", str
);
673 gdk_window_remove_filter(popup
->window
, filter_get_key
, data
);
674 gtk_widget_destroy(popup
);
676 return GDK_FILTER_REMOVE
;
679 static void may_set_shortcut(GtkWidget
*popup
, GtkWidget
*label
)
683 str
= g_object_get_data(G_OBJECT(popup
), "chosen-key");
686 gtk_label_set_text(GTK_LABEL(label
), str
);
688 g_object_set_data(G_OBJECT(popup
), "chosen-key", NULL
);
692 static void get_shortcut(GtkWidget
*button
, GtkWidget
*label
)
694 GtkWidget
*popup
, *frame
, *msg
;
696 Display
*dpy
= GDK_DISPLAY();
698 popup
= gtk_window_new(GTK_WINDOW_POPUP
);
700 gtk_window_set_position(GTK_WINDOW(popup
), GTK_WIN_POS_CENTER
);
702 frame
= gtk_frame_new(NULL
);
703 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
704 gtk_container_add(GTK_CONTAINER(popup
), frame
);
706 msg
= gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
708 gtk_misc_set_padding(GTK_MISC(msg
), 20, 20);
709 gtk_container_add(GTK_CONTAINER(frame
), msg
);
711 gtk_window_set_modal(GTK_WINDOW(popup
), TRUE
);
713 gtk_widget_add_events(popup
,
714 GDK_KEY_RELEASE_MASK
| GDK_BUTTON_PRESS_MASK
);
716 g_signal_connect(popup
, "destroy",
717 G_CALLBACK(may_set_shortcut
), label
);
719 gtk_widget_show_all(popup
);
721 gdk_window_add_filter(popup
->window
, filter_get_key
, popup
);
723 xid
= gdk_x11_drawable_get_xid(popup
->window
);
725 if (XGrabKeyboard(dpy
, xid
, False
, GrabModeAsync
, GrabModeAsync
,
726 gtk_get_current_event_time()) != Success
)
728 delayed_error(_("Failed to get keyboard grab!"));
729 gtk_widget_destroy(popup
);
732 if (XGrabPointer(dpy
, xid
, False
, ButtonPressMask
, GrabModeAsync
,
733 GrabModeAsync
, xid
, None
,
734 gtk_get_current_event_time()) != Success
)
736 g_warning("Failed to get mouse grab");
740 static void clear_shortcut(GtkButton
*clear
, GtkLabel
*label
)
742 gtk_label_set_text(GTK_LABEL(label
), CLICK_TO_SET
);
745 /* Opens a box allowing the user to change the name of a pinned icon.
746 * If the icon is destroyed then the box will close.
747 * If the user chooses OK then the callback is called once the icon's
748 * name, src_path and path fields have been updated and the item field
751 static void show_rename_box(Icon
*icon
)
754 GtkWidget
*label
, *entry
, *button
, *button2
, *hbox
, *spacer
;
759 gtk_window_present(GTK_WINDOW(icon
->dialog
));
763 icon
->dialog
= gtk_dialog_new();
764 g_signal_connect(icon
->dialog
, "destroy",
765 G_CALLBACK(gtk_widget_destroyed
), &icon
->dialog
);
767 dialog
= GTK_DIALOG(icon
->dialog
);
769 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 1));
770 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), (GtkWidget
*) vbox
,
772 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 5);
774 gtk_window_set_title(GTK_WINDOW(dialog
), _("Edit Item"));
775 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
777 label
= gtk_label_new(_("Clicking the icon opens:"));
778 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
780 entry
= gtk_entry_new();
781 gtk_box_pack_start(vbox
, entry
, TRUE
, FALSE
, 2);
782 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->src_path
);
783 g_object_set_data(G_OBJECT(dialog
), "new_path", entry
);
784 g_signal_connect_swapped(entry
, "activate",
785 G_CALLBACK(rename_activate
), dialog
);
787 spacer
= gtk_drawing_area_new();
788 gtk_widget_set_size_request(spacer
, 4, 4);
789 gtk_box_pack_start(vbox
, spacer
, FALSE
, FALSE
, 0);
791 label
= gtk_label_new(_("The text displayed under the icon is:"));
792 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
793 entry
= gtk_entry_new();
794 gtk_box_pack_start(vbox
, entry
, TRUE
, FALSE
, 2);
795 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->item
->leafname
);
796 gtk_widget_grab_focus(entry
);
797 g_object_set_data(G_OBJECT(dialog
), "new_name", entry
);
798 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
800 spacer
= gtk_drawing_area_new();
801 gtk_widget_set_size_request(spacer
, 4, 4);
802 gtk_box_pack_start(vbox
, spacer
, FALSE
, FALSE
, 0);
804 label
= gtk_label_new(_("The keyboard shortcut is:"));
805 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
807 hbox
= gtk_hbox_new(FALSE
, 2);
808 gtk_box_pack_start(vbox
, hbox
, TRUE
, FALSE
, 0);
809 button
= gtk_button_new_with_label(icon
->shortcut
812 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
813 g_object_set_data(G_OBJECT(dialog
), "new_shortcut",
814 GTK_BIN(button
)->child
);
815 g_signal_connect(button
, "clicked",
816 G_CALLBACK(get_shortcut
),
817 GTK_BIN(button
)->child
);
818 button2
= gtk_button_new_from_stock(GTK_STOCK_CLEAR
);
819 gtk_box_pack_start(GTK_BOX(hbox
), button2
, FALSE
, FALSE
, 0);
820 g_signal_connect(button2
, "clicked",
821 G_CALLBACK(clear_shortcut
),
822 GTK_BIN(button
)->child
);
824 g_object_set_data(G_OBJECT(dialog
), "callback_icon", icon
);
826 gtk_dialog_add_buttons(dialog
,
827 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
828 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
830 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
832 g_signal_connect(dialog
, "response", G_CALLBACK(edit_response
), NULL
);
834 gtk_widget_show_all(GTK_WIDGET(dialog
));
837 static gpointer parent_class
= NULL
;
839 static void icon_finialize(GObject
*object
)
841 Icon
*icon
= (Icon
*) object
;
843 g_return_if_fail(!icon
->selected
);
846 gtk_widget_destroy(icon
->dialog
);
848 if (icon
== menu_icon
)
851 icon_set_path(icon
, NULL
, NULL
);
852 icon_set_shortcut(icon
, NULL
);
854 G_OBJECT_CLASS(parent_class
)->finalize(object
);
857 static void icon_class_init(gpointer gclass
, gpointer data
)
859 GObjectClass
*object
= (GObjectClass
*) gclass
;
860 IconClass
*icon
= (IconClass
*) gclass
;
862 parent_class
= g_type_class_peek_parent(gclass
);
864 object
->finalize
= icon_finialize
;
865 icon
->destroy
= NULL
;
868 icon
->same_group
= NULL
;
871 g_signal_new("update",
872 G_TYPE_FROM_CLASS(gclass
),
874 G_STRUCT_OFFSET(IconClass
, update
),
876 g_cclosure_marshal_VOID__VOID
,
879 g_signal_new("destroy",
880 G_TYPE_FROM_CLASS(gclass
),
882 G_STRUCT_OFFSET(IconClass
, destroy
),
884 g_cclosure_marshal_VOID__VOID
,
887 g_signal_new("redraw",
888 G_TYPE_FROM_CLASS(gclass
),
890 G_STRUCT_OFFSET(IconClass
, redraw
),
892 g_cclosure_marshal_VOID__VOID
,
895 icons_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
898 static void icon_init(GTypeInstance
*object
, gpointer gclass
)
900 Icon
*icon
= (Icon
*) object
;
902 icon
->selected
= FALSE
;
903 icon
->src_path
= NULL
;
907 icon
->shortcut
= NULL
;
908 icon
->shortcut_key
.keycode
= 0;
909 icon
->shortcut_key
.modifier
= 0;
912 /* As icon_set_selected(), but doesn't automatically unselect incompatible
915 static void icon_set_selected_int(Icon
*icon
, gboolean selected
)
917 static GtkClipboard
*primary
;
919 g_return_if_fail(icon
!= NULL
);
921 if (icon
->selected
== selected
)
925 primary
= gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE
));
929 icon_selection
= g_list_prepend(icon_selection
, icon
);
932 GtkTargetEntry target_table
[] =
934 {"text/uri-list", 0, TARGET_URI_LIST
},
935 {"UTF8", 0, TARGET_STRING
},
936 {"COMPOUND_TEXT", 0, TARGET_STRING
},
937 {"STRING", 0, TARGET_STRING
},
941 have_primary
= gtk_clipboard_set_with_data(primary
,
943 sizeof(target_table
) / sizeof(*target_table
),
944 selection_get
, lose_selection
, NULL
);
949 icon_selection
= g_list_remove(icon_selection
, icon
);
950 if (have_primary
&& !icon_selection
)
952 have_primary
= FALSE
;
953 gtk_clipboard_clear(primary
);
957 icon
->selected
= selected
;
958 g_signal_emit_by_name(icon
, "redraw");
961 static void menu_set_hidden(GtkWidget
*menu
, gboolean hidden
, int from
, int n
)
965 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
967 item
= g_list_nth(items
, from
);
971 gtk_widget_hide(GTK_WIDGET(item
->data
));
973 gtk_widget_show(GTK_WIDGET(item
->data
));
979 /* Stolen from xfwm4 */
980 static void initModifiers(void)
982 static gboolean need_init
= TRUE
;
983 Display
*dpy
= GDK_DISPLAY();
984 XModifierKeymap
*xmk
= XGetModifierMapping(dpy
);
991 AltMask
= MetaMask
= NumLockMask
= ScrollLockMask
= CapsLockMask
=
992 SuperMask
= HyperMask
= 0;
994 /* Work out which mask to use for each modifier group */
997 KeyCode
*c
= xmk
->modifiermap
;
998 KeyCode numLockKeyCode
;
999 KeyCode scrollLockKeyCode
;
1000 KeyCode capsLockKeyCode
;
1002 KeyCode metaKeyCode
;
1003 KeyCode superKeyCode
;
1004 KeyCode hyperKeyCode
;
1006 /* Find the codes to search for... */
1007 numLockKeyCode
= XKeysymToKeycode(dpy
, XK_Num_Lock
);
1008 scrollLockKeyCode
= XKeysymToKeycode(dpy
, XK_Scroll_Lock
);
1009 capsLockKeyCode
= XKeysymToKeycode(dpy
, XK_Caps_Lock
);
1010 altKeyCode
= XKeysymToKeycode(dpy
, XK_Alt_L
);
1011 metaKeyCode
= XKeysymToKeycode(dpy
, XK_Meta_L
);
1012 superKeyCode
= XKeysymToKeycode(dpy
, XK_Super_L
);
1013 hyperKeyCode
= XKeysymToKeycode(dpy
, XK_Hyper_L
);
1015 /* If some are missing, try alternatives... */
1017 altKeyCode
= XKeysymToKeycode(dpy
, XK_Alt_R
);
1019 metaKeyCode
= XKeysymToKeycode(dpy
, XK_Meta_R
);
1021 superKeyCode
= XKeysymToKeycode(dpy
, XK_Super_R
);
1023 hyperKeyCode
= XKeysymToKeycode(dpy
, XK_Hyper_R
);
1025 /* Check each of the eight modifier lists.
1026 * The idea (I think) is that we name the modifier group which
1027 * includes the Alt key as the 'Alt group', and so on for
1028 * the other modifiers.
1030 for (m
= 0; m
< 8; m
++)
1032 for (k
= 0; k
< xmk
->max_keypermod
; k
++, c
++)
1036 if (*c
== numLockKeyCode
)
1037 NumLockMask
= (1 << m
);
1038 if (*c
== scrollLockKeyCode
)
1039 ScrollLockMask
= (1 << m
);
1040 if (*c
== capsLockKeyCode
)
1041 CapsLockMask
= (1 << m
);
1042 if (*c
== altKeyCode
)
1044 if (*c
== metaKeyCode
)
1045 MetaMask
= (1 << m
);
1046 if (*c
== superKeyCode
)
1047 SuperMask
= (1 << m
);
1048 if (*c
== hyperKeyCode
)
1049 HyperMask
= (1 << m
);
1052 XFreeModifiermap(xmk
);
1055 if(MetaMask
== AltMask
)
1058 if (AltMask
!= 0 && MetaMask
== Mod1Mask
)
1064 if (AltMask
== 0 && MetaMask
!= 0)
1066 if (MetaMask
!= Mod1Mask
)
1080 /* Fill in key from str. Sets keycode to zero if str is NULL.
1081 * Stolen from xfwm4 and modified. Call initModifiers before this.
1083 static void parseKeyString(MyKey
*key
, const char *str
)
1086 Display
*dpy
= GDK_DISPLAY();
1094 k
= strrchr(str
, '+');
1095 key
->keycode
= XKeysymToKeycode(dpy
, XStringToKeysym(k
? k
+ 1 : str
));
1100 tmp
= g_ascii_strdown(str
, -1);
1102 if (strstr(tmp
, "shift"))
1103 key
->modifier
= key
->modifier
| ShiftMask
;
1104 if (strstr(tmp
, "control"))
1105 key
->modifier
= key
->modifier
| ControlMask
;
1106 if (strstr(tmp
, "alt") || strstr(tmp
, "mod1"))
1107 key
->modifier
= key
->modifier
| AltMask
;
1108 if (strstr(tmp
, "meta") || strstr(tmp
, "mod2"))
1109 key
->modifier
= key
->modifier
| MetaMask
;
1110 if (strstr(tmp
, "hyper"))
1111 key
->modifier
= key
->modifier
| HyperMask
;
1112 if (strstr(tmp
, "super"))
1113 key
->modifier
= key
->modifier
| SuperMask
;
1119 g_warning("Can't parse key '%s'\n", str
);
1122 static GdkFilterReturn
filter_keys(GdkXEvent
*xevent
,
1127 XKeyEvent
*kev
= (XKeyEvent
*) xevent
;
1130 if (kev
->type
!= KeyPress
)
1131 return GDK_FILTER_CONTINUE
;
1133 state
= kev
->state
& (ShiftMask
| ControlMask
| AltMask
| MetaMask
|
1134 HyperMask
| SuperMask
);
1136 for (next
= icon_shortcuts
; next
; next
= next
->next
)
1138 Icon
*icon
= (Icon
*) next
->data
;
1140 if (icon
->shortcut_key
.keycode
== kev
->keycode
&&
1141 icon
->shortcut_key
.modifier
== state
)
1144 run_diritem(icon
->path
, icon
->item
, NULL
, NULL
, FALSE
);
1148 return GDK_FILTER_CONTINUE
;
1151 #define GRAB(key, mods) XGrabKey(dpy, key->keycode, key->modifier | mods, \
1152 root, False, GrabModeAsync, GrabModeAsync)
1153 #define UNGRAB(key, mods) XUngrabKey(dpy, key->keycode, key->modifier | mods, \
1156 static guint
mykey_hash(gconstpointer key
)
1158 MyKey
*k
= (MyKey
*) key
;
1160 return (k
->keycode
<< 8) + k
->modifier
;
1163 static gboolean
mykey_cmp(gconstpointer a
, gconstpointer b
)
1165 MyKey
*ka
= (MyKey
*) a
;
1166 MyKey
*kb
= (MyKey
*) b
;
1168 return ka
->keycode
== kb
->keycode
&& kb
->modifier
== kb
->modifier
;
1171 /* Stolen from xfwm4 and modified.
1172 * FALSE on error. Call initModifiers before this.
1174 static gboolean
grabKey(MyKey
*key
)
1177 Display
*dpy
= GDK_DISPLAY();
1178 static gboolean need_init
= TRUE
;
1183 gdk_window_add_filter(gdk_get_default_root_window(),
1187 gdk_error_trap_push();
1189 root
= GDK_ROOT_WINDOW();
1193 /* Here we grab all combinations of well known modifiers */
1194 GRAB(key
, ScrollLockMask
);
1195 GRAB(key
, NumLockMask
);
1196 GRAB(key
, CapsLockMask
);
1197 GRAB(key
, ScrollLockMask
| NumLockMask
);
1198 GRAB(key
, ScrollLockMask
| CapsLockMask
);
1199 GRAB(key
, CapsLockMask
| NumLockMask
);
1200 GRAB(key
, ScrollLockMask
| CapsLockMask
| NumLockMask
);
1203 return gdk_error_trap_pop() == Success
;
1206 static gboolean
ungrabKey(MyKey
*key
)
1208 Window root
= GDK_ROOT_WINDOW();
1209 Display
*dpy
= GDK_DISPLAY();
1211 gdk_error_trap_push();
1215 UNGRAB(key
, ScrollLockMask
);
1216 UNGRAB(key
, NumLockMask
);
1217 UNGRAB(key
, CapsLockMask
);
1218 UNGRAB(key
, ScrollLockMask
| NumLockMask
);
1219 UNGRAB(key
, ScrollLockMask
| CapsLockMask
);
1220 UNGRAB(key
, CapsLockMask
| NumLockMask
);
1221 UNGRAB(key
, ScrollLockMask
| CapsLockMask
| NumLockMask
);
1224 return gdk_error_trap_pop() == Success
;
1227 static void ungrab_key(Icon
*icon
)
1231 g_return_if_fail(icon
!= NULL
);
1233 if (!icon
->shortcut_key
.keycode
)
1236 icon_shortcuts
= g_list_remove(icon_shortcuts
, icon
);
1238 count
= g_hash_table_lookup(grab_counter
, &icon
->shortcut_key
);
1239 g_return_if_fail(count
!= NULL
);
1246 g_hash_table_remove(grab_counter
, &icon
->shortcut_key
);
1248 if (!ungrabKey(&icon
->shortcut_key
))
1249 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1250 icon
->shortcut
, icon
->item
->leafname
);
1253 static void grab_key(Icon
*icon
)
1258 g_return_if_fail(icon
!= NULL
);
1260 g_return_if_fail(g_list_find(icon_shortcuts
, icon
) == NULL
);
1262 if (!icon
->shortcut_key
.keycode
)
1265 icon_shortcuts
= g_list_prepend(icon_shortcuts
, icon
);
1268 grab_counter
= g_hash_table_new_full(mykey_hash
, mykey_cmp
,
1271 count
= g_hash_table_lookup(grab_counter
, &icon
->shortcut_key
);
1275 return; /* Already grabbed */
1278 hash_key
= g_new(MyKey
, 1);
1279 *hash_key
= icon
->shortcut_key
;
1280 count
= g_new(int, 1);
1282 g_hash_table_insert(grab_counter
, hash_key
, count
);
1284 if (!grabKey(&icon
->shortcut_key
))
1285 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1286 "Some other application may be already using it!\n",
1287 icon
->shortcut
, icon
->item
->leafname
);
1291 static void icon_wink(Icon
*icon
)
1295 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
1296 g_return_if_fail(iclass
->wink
!= NULL
);
1301 /* Sets icon_menu */
1302 static void create_menu(void)
1306 GtkItemFactory
*item_factory
;
1308 g_return_if_fail(icon_menu
== NULL
);
1310 item_factory
= menu_create(menu_def
,
1311 sizeof(menu_def
) / sizeof(*menu_def
),
1314 tmp
= g_strconcat("<icon>/", _("File"), NULL
);
1315 icon_menu
= gtk_item_factory_get_widget(item_factory
, "<icon>");
1316 icon_file_menu
= gtk_item_factory_get_widget(item_factory
, tmp
);
1319 /* File '' label... */
1320 items
= gtk_container_get_children(GTK_CONTAINER(icon_menu
));
1321 icon_file_item
= GTK_BIN(g_list_nth(items
, 1)->data
)->child
;
1324 /* Shift Open... label */
1325 items
= gtk_container_get_children(GTK_CONTAINER(icon_file_menu
));
1326 file_shift_item
= GTK_BIN(items
->data
)->child
;
1329 g_signal_connect(icon_menu
, "unmap_event",
1330 G_CALLBACK(menu_closed
), NULL
);