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);
133 static GtkItemFactoryEntry menu_def
[] = {
134 {N_("ROX-Filer"), NULL
, NULL
, 0, "<Branch>"},
135 {">" N_("About ROX-Filer..."), NULL
, menu_rox_help
, HELP_ABOUT
, NULL
},
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_("Help"), NULL
, file_op
, ACTION_HELP
, "<StockItem>", GTK_STOCK_HELP
},
144 {">" N_("Info"), NULL
, file_op
, ACTION_INFO
, "<StockItem>", GTK_STOCK_DIALOG_INFO
},
145 {">" N_("Set Run Action..."), NULL
, file_op
, ACTION_RUN_ACTION
, "<StockItem>", GTK_STOCK_EXECUTE
},
146 {">" N_("Set Icon..."), NULL
, file_op
, ACTION_SET_ICON
, NULL
},
147 {N_("Edit Item"), NULL
, file_op
, ACTION_EDIT
, NULL
},
148 {N_("Show Location"), NULL
, file_op
, ACTION_LOCATION
, NULL
},
149 {N_("Remove Item(s)"), NULL
, remove_items
, 0, NULL
},
150 {"", NULL
, NULL
, 0, "<Separator>"},
151 {N_("Backdrop..."), NULL
, set_backdrop
, 0, NULL
},
154 /****************************************************************
155 * EXTERNAL INTERFACE *
156 ****************************************************************/
158 /* Called when the pointer moves over the icon */
159 void icon_may_update(Icon
*icon
)
164 g_return_if_fail(icon
!= NULL
);
166 image
= icon
->item
->image
;
167 flags
= icon
->item
->flags
;
172 diritem_restat(icon
->path
, icon
->item
, NULL
);
174 if (icon
->item
->image
!= image
|| icon
->item
->flags
!= flags
)
176 /* Appearance changed; need to redraw */
177 g_signal_emit_by_name(icon
, "update");
181 g_object_unref(image
);
184 /* If path is on an icon then it may have changed... check! */
185 void icons_may_update(const gchar
*path
)
191 affected
= g_hash_table_lookup(icons_hash
, path
);
193 for (; affected
; affected
= affected
->next
)
194 icon_may_update((Icon
*) affected
->data
);
198 typedef struct _CheckData CheckData
;
204 static void check_has(gpointer key
, GList
*icons
, CheckData
*check
)
208 g_return_if_fail(icons
!= NULL
);
212 if (is_sub_dir(icon
->path
, check
->path
))
216 /* Returns TRUE if any icon links to this file (or any file inside
217 * this directory). Used to check that it's OK to delete 'path'.
219 gboolean
icons_require(const gchar
*path
)
228 g_hash_table_foreach(icons_hash
, (GHFunc
) check_has
, &check
);
233 /* Menu was clicked over this icon. Set things up correctly (shade items,
234 * add app menu stuff, etc).
235 * You should show icon_menu after calling this...
237 void icon_prepare_menu(Icon
*icon
, gboolean pinboard
)
243 menu_set_hidden(icon_menu
, !pinboard
, 5, 2);
245 /* Shade Remove Item(s) unless there is a selection */
246 menu_set_items_shaded(icon_menu
,
247 (icon_selection
|| menu_icon
) ? FALSE
: TRUE
,
250 menu_show_shift_action(file_shift_item
, icon
? icon
->item
: NULL
,
253 /* Shade the File/Edit/Show items unless an item was clicked */
258 menu_set_items_shaded(icon_menu
, FALSE
, 1, 3);
259 menu_set_items_shaded(icon_file_menu
, FALSE
, 0, 6);
260 if (!can_set_run_action(icon
->item
))
261 menu_set_items_shaded(icon_file_menu
, TRUE
, 3, 1);
263 tmp
= g_strdup_printf("%s '%s'",
264 basetype_name(icon
->item
),
265 icon
->item
->leafname
);
266 gtk_label_set_text(GTK_LABEL(icon_file_item
), tmp
);
269 /* Check for app-specific menu */
270 appmenu_add(icon
->path
, icon
->item
, icon_menu
);
274 menu_set_items_shaded(icon_menu
, TRUE
, 1, 3);
275 menu_set_items_shaded(icon_file_menu
, TRUE
, 0, 6);
276 gtk_label_set_text(GTK_LABEL(icon_file_item
), _("Nothing"));
280 /* Set whether this icon is selected. Will automatically clear the selection
281 * if it contains icons from a different group.
283 void icon_set_selected(Icon
*icon
, gboolean selected
)
285 if (selected
&& icon_selection
)
288 Icon
*other
= (Icon
*) icon_selection
->data
;
290 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
291 if (!iclass
->same_group(icon
, other
))
293 icon_select_only(icon
);
298 icon_set_selected_int(icon
, selected
);
301 /* Clear everything, except 'select', which is selected.
302 * If select is NULL, unselects everything.
304 void icon_select_only(Icon
*select
)
306 GList
*to_clear
, *next
;
309 icon_set_selected_int(select
, TRUE
);
311 to_clear
= g_list_copy(icon_selection
);
314 to_clear
= g_list_remove(to_clear
, select
);
316 for (next
= to_clear
; next
; next
= next
->next
)
317 icon_set_selected_int((Icon
*) next
->data
, FALSE
);
319 g_list_free(to_clear
);
322 /* Destroys this icon's widget, causing it to be removed from the screen */
323 void icon_destroy(Icon
*icon
)
325 g_return_if_fail(icon
!= NULL
);
327 icon_set_selected_int(icon
, FALSE
);
329 g_signal_emit_by_name(icon
, "destroy");
332 /* Return a text/uri-list of all the icons in the selection */
333 gchar
*icon_create_uri_list(void)
340 tmp
= g_string_new(NULL
);
341 leader
= g_strdup_printf("file://%s", our_host_name_for_dnd());
343 for (next
= icon_selection
; next
; next
= next
->next
)
345 Icon
*icon
= (Icon
*) next
->data
;
347 g_string_append(tmp
, leader
);
348 g_string_append(tmp
, icon
->path
);
349 g_string_append(tmp
, "\r\n");
354 g_string_free(tmp
, FALSE
);
359 GType
icon_get_type(void)
361 static GType type
= 0;
365 static const GTypeInfo info
=
368 NULL
, /* base_init */
369 NULL
, /* base_finalise */
371 NULL
, /* class_finalise */
372 NULL
, /* class_data */
378 type
= g_type_register_static(G_TYPE_OBJECT
, "Icon",
385 /* Sets, unsets or changes the pathname and name for an icon.
386 * Updates icons_hash and (re)stats the item.
387 * If name is NULL then gets the leafname from pathname.
388 * If pathname is NULL, frees everything.
390 void icon_set_path(Icon
*icon
, const char *pathname
, const char *name
)
394 icon_unhash_path(icon
);
395 icon
->src_path
= NULL
;
398 diritem_free(icon
->item
);
404 icon
->src_path
= g_strdup(pathname
);
405 icon
->path
= expand_path(pathname
);
407 icon_hash_path(icon
);
410 name
= g_basename(pathname
);
412 icon
->item
= diritem_new(name
);
413 diritem_restat(icon
->path
, icon
->item
, NULL
);
417 void icon_set_shortcut(Icon
*icon
, const gchar
*shortcut
)
419 g_return_if_fail(icon
!= NULL
);
421 if (shortcut
&& !*shortcut
)
423 if (icon
->shortcut
== shortcut
)
425 if (icon
->shortcut
&& shortcut
&& strcmp(icon
->shortcut
, shortcut
) == 0)
432 g_free(icon
->shortcut
);
433 icon
->shortcut
= g_strdup(shortcut
);
434 parseKeyString(&icon
->shortcut_key
, shortcut
);
439 /****************************************************************
440 * INTERNAL FUNCTIONS *
441 ****************************************************************/
443 /* The icons_hash table allows us to convert from a path to a list
444 * of icons that use that path.
445 * Add this icon to the list for its path.
447 static void icon_hash_path(Icon
*icon
)
451 g_return_if_fail(icon
!= NULL
);
453 /* g_print("[ hashing '%s' ]\n", icon->path); */
455 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
456 list
= g_list_prepend(list
, icon
);
457 g_hash_table_insert(icons_hash
, icon
->path
, list
);
460 /* Remove this icon from the icons_hash table */
461 static void icon_unhash_path(Icon
*icon
)
465 g_return_if_fail(icon
!= NULL
);
467 /* g_print("[ unhashing '%s' ]\n", icon->path); */
469 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
470 g_return_if_fail(list
!= NULL
);
472 list
= g_list_remove(list
, icon
);
474 /* Remove it first; the hash key may have changed address */
475 g_hash_table_remove(icons_hash
, icon
->path
);
477 g_hash_table_insert(icons_hash
,
478 ((Icon
*) list
->data
)->path
, list
);
481 static void rename_activate(GtkWidget
*dialog
)
483 GtkWidget
*entry
, *src
, *shortcut
;
485 const guchar
*new_name
, *new_src
, *new_shortcut
;
487 entry
= g_object_get_data(G_OBJECT(dialog
), "new_name");
488 icon
= g_object_get_data(G_OBJECT(dialog
), "callback_icon");
489 src
= g_object_get_data(G_OBJECT(dialog
), "new_path");
490 shortcut
= g_object_get_data(G_OBJECT(dialog
), "new_shortcut");
492 g_return_if_fail(entry
!= NULL
&&
497 new_name
= gtk_entry_get_text(GTK_ENTRY(entry
));
498 new_src
= gtk_entry_get_text(GTK_ENTRY(src
));
499 new_shortcut
= gtk_label_get_text(GTK_LABEL(shortcut
));
500 if (strcmp(new_shortcut
, CLICK_TO_SET
) == 0)
503 if (*new_src
== '\0')
505 _("The location must contain at least one character!"));
508 icon_set_path(icon
, new_src
, new_name
);
509 icon_set_shortcut(icon
, new_shortcut
);
510 g_signal_emit_by_name(icon
, "update");
511 gtk_widget_destroy(dialog
);
515 static void menu_closed(GtkWidget
*widget
)
521 /* Called when another application takes the selection away from us */
522 static void lose_selection(GtkClipboard
*primary
, gpointer data
)
524 have_primary
= FALSE
;
525 icon_select_only(NULL
);
528 /* Called when another application wants the contents of our selection */
529 static void selection_get(GtkClipboard
*primary
,
530 GtkSelectionData
*selection_data
,
536 if (info
== TARGET_URI_LIST
)
537 text
= icon_create_uri_list();
543 str
= g_string_new(NULL
);
545 for (next
= icon_selection
; next
; next
= next
->next
)
547 Icon
*icon
= (Icon
*) next
->data
;
549 g_string_append(str
, icon
->path
);
550 g_string_append_c(str
, ' ');
554 g_string_free(str
, FALSE
);
557 gtk_selection_data_set(selection_data
,
558 gdk_atom_intern("STRING", FALSE
),
559 8, text
, strlen(text
));
562 static void set_backdrop(gpointer data
, guint action
, GtkWidget
*widget
)
564 pinboard_set_backdrop_box();
567 static void remove_items(gpointer data
, guint action
, GtkWidget
*widget
)
572 icon_set_selected(menu_icon
, TRUE
);
577 _("You must first select some items to remove"));
581 iclass
= (IconClass
*)
582 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection
->data
));
584 iclass
->remove_items();
587 static void file_op(gpointer data
, guint action
, GtkWidget
*widget
)
591 delayed_error(_("You must open the menu over an item"));
598 run_diritem(menu_icon
->path
, menu_icon
->item
,
602 show_rename_box(menu_icon
);
604 case ACTION_LOCATION
:
605 open_to_show(menu_icon
->path
);
608 show_item_help(menu_icon
->path
, menu_icon
->item
);
611 infobox_new(menu_icon
->path
);
613 case ACTION_RUN_ACTION
:
614 if (can_set_run_action(menu_icon
->item
))
615 type_set_handler_dialog(
616 menu_icon
->item
->mime_type
);
619 _("You can only set the run action for a "
622 case ACTION_SET_ICON
:
623 icon_set_handler_dialog(menu_icon
->item
,
629 static void edit_response(GtkWidget
*dialog
, gint response
, gpointer data
)
631 if (response
== GTK_RESPONSE_OK
)
632 rename_activate(dialog
);
633 else if (response
== GTK_RESPONSE_CANCEL
)
634 gtk_widget_destroy(dialog
);
637 static GdkFilterReturn
filter_get_key(GdkXEvent
*xevent
,
641 XKeyEvent
*kev
= (XKeyEvent
*) xevent
;
642 GtkWidget
*popup
= (GtkWidget
*) data
;
643 Display
*dpy
= GDK_DISPLAY();
645 if (kev
->type
!= KeyRelease
&& kev
->type
!= ButtonPressMask
)
646 return GDK_FILTER_CONTINUE
;
650 if (kev
->type
== KeyRelease
)
654 unsigned int m
= kev
->state
;
656 sym
= XKeycodeToKeysym(dpy
, kev
->keycode
, 0);
658 return GDK_FILTER_CONTINUE
;
660 str
= g_strdup_printf("%s%s%s%s%s%s%s",
661 m
& ControlMask
? "Control+" : "",
662 m
& ShiftMask
? "Shift+" : "",
663 m
& AltMask
? "Alt+" : "",
664 m
& MetaMask
? "Meta+" : "",
665 m
& SuperMask
? "Super+" : "",
666 m
& HyperMask
? "Hyper+" : "",
667 XKeysymToString(sym
));
669 g_object_set_data(G_OBJECT(popup
), "chosen-key", str
);
672 gdk_window_remove_filter(popup
->window
, filter_get_key
, data
);
673 gtk_widget_destroy(popup
);
675 return GDK_FILTER_REMOVE
;
678 static void may_set_shortcut(GtkWidget
*popup
, GtkWidget
*label
)
682 str
= g_object_get_data(G_OBJECT(popup
), "chosen-key");
685 gtk_label_set_text(GTK_LABEL(label
), str
);
687 g_object_set_data(G_OBJECT(popup
), "chosen-key", NULL
);
691 static void get_shortcut(GtkWidget
*button
, GtkWidget
*label
)
693 GtkWidget
*popup
, *frame
, *msg
;
695 Display
*dpy
= GDK_DISPLAY();
697 popup
= gtk_window_new(GTK_WINDOW_POPUP
);
699 gtk_window_set_position(GTK_WINDOW(popup
), GTK_WIN_POS_CENTER
);
701 frame
= gtk_frame_new(NULL
);
702 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
703 gtk_container_add(GTK_CONTAINER(popup
), frame
);
705 msg
= gtk_label_new(_("Press the desired shortcut (eg, Control+F1)"));
707 gtk_misc_set_padding(GTK_MISC(msg
), 20, 20);
708 gtk_container_add(GTK_CONTAINER(frame
), msg
);
710 gtk_window_set_modal(GTK_WINDOW(popup
), TRUE
);
712 gtk_widget_add_events(popup
,
713 GDK_KEY_RELEASE_MASK
| GDK_BUTTON_PRESS_MASK
);
715 g_signal_connect(popup
, "destroy",
716 G_CALLBACK(may_set_shortcut
), label
);
718 gtk_widget_show_all(popup
);
720 gdk_window_add_filter(popup
->window
, filter_get_key
, popup
);
722 xid
= gdk_x11_drawable_get_xid(popup
->window
);
724 if (XGrabKeyboard(dpy
, xid
, False
, GrabModeAsync
, GrabModeAsync
,
725 gtk_get_current_event_time()) != Success
)
727 delayed_error(_("Failed to get keyboard grab!"));
728 gtk_widget_destroy(popup
);
731 if (XGrabPointer(dpy
, xid
, False
, ButtonPressMask
, GrabModeAsync
,
732 GrabModeAsync
, xid
, None
,
733 gtk_get_current_event_time()) != Success
)
735 g_warning("Failed to get mouse grab");
739 static void clear_shortcut(GtkButton
*clear
, GtkLabel
*label
)
741 gtk_label_set_text(GTK_LABEL(label
), CLICK_TO_SET
);
744 /* Opens a box allowing the user to change the name of a pinned icon.
745 * If the icon is destroyed then the box will close.
746 * If the user chooses OK then the callback is called once the icon's
747 * name, src_path and path fields have been updated and the item field
750 static void show_rename_box(Icon
*icon
)
753 GtkWidget
*label
, *entry
, *button
, *button2
, *hbox
, *spacer
;
758 gtk_window_present(GTK_WINDOW(icon
->dialog
));
762 icon
->dialog
= gtk_dialog_new();
763 g_signal_connect(icon
->dialog
, "destroy",
764 G_CALLBACK(gtk_widget_destroyed
), &icon
->dialog
);
766 dialog
= GTK_DIALOG(icon
->dialog
);
768 vbox
= GTK_BOX(gtk_vbox_new(FALSE
, 1));
769 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), (GtkWidget
*) vbox
,
771 gtk_container_set_border_width(GTK_CONTAINER(vbox
), 5);
773 gtk_window_set_title(GTK_WINDOW(dialog
), _("Edit Item"));
774 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
776 label
= gtk_label_new(_("Clicking the icon opens:"));
777 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
779 entry
= gtk_entry_new();
780 gtk_box_pack_start(vbox
, entry
, TRUE
, FALSE
, 2);
781 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->src_path
);
782 g_object_set_data(G_OBJECT(dialog
), "new_path", entry
);
783 g_signal_connect_swapped(entry
, "activate",
784 G_CALLBACK(rename_activate
), dialog
);
786 spacer
= gtk_drawing_area_new();
787 gtk_widget_set_size_request(spacer
, 4, 4);
788 gtk_box_pack_start(vbox
, spacer
, FALSE
, FALSE
, 0);
790 label
= gtk_label_new(_("The text displayed under the icon is:"));
791 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
792 entry
= gtk_entry_new();
793 gtk_box_pack_start(vbox
, entry
, TRUE
, FALSE
, 2);
794 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->item
->leafname
);
795 gtk_widget_grab_focus(entry
);
796 g_object_set_data(G_OBJECT(dialog
), "new_name", entry
);
797 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
799 spacer
= gtk_drawing_area_new();
800 gtk_widget_set_size_request(spacer
, 4, 4);
801 gtk_box_pack_start(vbox
, spacer
, FALSE
, FALSE
, 0);
803 label
= gtk_label_new(_("The keyboard shortcut is:"));
804 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 0);
806 hbox
= gtk_hbox_new(FALSE
, 2);
807 gtk_box_pack_start(vbox
, hbox
, TRUE
, FALSE
, 0);
808 button
= gtk_button_new_with_label(icon
->shortcut
811 gtk_box_pack_start(GTK_BOX(hbox
), button
, TRUE
, TRUE
, 0);
812 g_object_set_data(G_OBJECT(dialog
), "new_shortcut",
813 GTK_BIN(button
)->child
);
814 g_signal_connect(button
, "clicked",
815 G_CALLBACK(get_shortcut
),
816 GTK_BIN(button
)->child
);
817 button2
= gtk_button_new_from_stock(GTK_STOCK_CLEAR
);
818 gtk_box_pack_start(GTK_BOX(hbox
), button2
, FALSE
, FALSE
, 0);
819 g_signal_connect(button2
, "clicked",
820 G_CALLBACK(clear_shortcut
),
821 GTK_BIN(button
)->child
);
823 g_object_set_data(G_OBJECT(dialog
), "callback_icon", icon
);
825 gtk_dialog_add_buttons(dialog
,
826 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
827 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
829 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
831 g_signal_connect(dialog
, "response", G_CALLBACK(edit_response
), NULL
);
833 gtk_widget_show_all(GTK_WIDGET(dialog
));
836 static gpointer parent_class
= NULL
;
838 static void icon_finialize(GObject
*object
)
840 Icon
*icon
= (Icon
*) object
;
842 g_return_if_fail(!icon
->selected
);
845 gtk_widget_destroy(icon
->dialog
);
847 if (icon
== menu_icon
)
850 icon_set_path(icon
, NULL
, NULL
);
851 icon_set_shortcut(icon
, NULL
);
853 G_OBJECT_CLASS(parent_class
)->finalize(object
);
856 static void icon_class_init(gpointer gclass
, gpointer data
)
860 GtkItemFactory
*item_factory
;
861 GObjectClass
*object
= (GObjectClass
*) gclass
;
862 IconClass
*icon
= (IconClass
*) gclass
;
864 parent_class
= g_type_class_peek_parent(gclass
);
866 object
->finalize
= icon_finialize
;
867 icon
->destroy
= NULL
;
870 icon
->same_group
= NULL
;
873 g_signal_new("update",
874 G_TYPE_FROM_CLASS(gclass
),
876 G_STRUCT_OFFSET(IconClass
, update
),
878 g_cclosure_marshal_VOID__VOID
,
881 g_signal_new("destroy",
882 G_TYPE_FROM_CLASS(gclass
),
884 G_STRUCT_OFFSET(IconClass
, destroy
),
886 g_cclosure_marshal_VOID__VOID
,
889 g_signal_new("redraw",
890 G_TYPE_FROM_CLASS(gclass
),
892 G_STRUCT_OFFSET(IconClass
, redraw
),
894 g_cclosure_marshal_VOID__VOID
,
897 icons_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
899 item_factory
= menu_create(menu_def
,
900 sizeof(menu_def
) / sizeof(*menu_def
),
903 tmp
= g_strconcat("<icon>/", _("File"), NULL
);
904 icon_menu
= gtk_item_factory_get_widget(item_factory
, "<icon>");
905 icon_file_menu
= gtk_item_factory_get_widget(item_factory
, tmp
);
908 /* File '' label... */
909 items
= gtk_container_get_children(GTK_CONTAINER(icon_menu
));
910 icon_file_item
= GTK_BIN(g_list_nth(items
, 1)->data
)->child
;
913 /* Shift Open... label */
914 items
= gtk_container_get_children(GTK_CONTAINER(icon_file_menu
));
915 file_shift_item
= GTK_BIN(items
->data
)->child
;
918 g_signal_connect(icon_menu
, "unmap_event",
919 G_CALLBACK(menu_closed
), NULL
);
922 static void icon_init(GTypeInstance
*object
, gpointer gclass
)
924 Icon
*icon
= (Icon
*) object
;
926 icon
->selected
= FALSE
;
927 icon
->src_path
= NULL
;
931 icon
->shortcut
= NULL
;
932 icon
->shortcut_key
.keycode
= 0;
933 icon
->shortcut_key
.modifier
= 0;
936 /* As icon_set_selected(), but doesn't automatically unselect incompatible
939 static void icon_set_selected_int(Icon
*icon
, gboolean selected
)
941 static GtkClipboard
*primary
;
943 g_return_if_fail(icon
!= NULL
);
945 if (icon
->selected
== selected
)
949 primary
= gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE
));
953 icon_selection
= g_list_prepend(icon_selection
, icon
);
956 GtkTargetEntry target_table
[] =
958 {"text/uri-list", 0, TARGET_URI_LIST
},
959 {"UTF8", 0, TARGET_STRING
},
960 {"COMPOUND_TEXT", 0, TARGET_STRING
},
961 {"STRING", 0, TARGET_STRING
},
965 have_primary
= gtk_clipboard_set_with_data(primary
,
967 sizeof(target_table
) / sizeof(*target_table
),
968 selection_get
, lose_selection
, NULL
);
973 icon_selection
= g_list_remove(icon_selection
, icon
);
974 if (have_primary
&& !icon_selection
)
976 have_primary
= FALSE
;
977 gtk_clipboard_clear(primary
);
981 icon
->selected
= selected
;
982 g_signal_emit_by_name(icon
, "redraw");
985 static void menu_set_hidden(GtkWidget
*menu
, gboolean hidden
, int from
, int n
)
989 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
991 item
= g_list_nth(items
, from
);
995 gtk_widget_hide(GTK_WIDGET(item
->data
));
997 gtk_widget_show(GTK_WIDGET(item
->data
));
1003 /* Stolen from xfwm4 */
1004 static void initModifiers(void)
1006 static gboolean need_init
= TRUE
;
1007 Display
*dpy
= GDK_DISPLAY();
1008 XModifierKeymap
*xmk
= XGetModifierMapping(dpy
);
1015 AltMask
= MetaMask
= NumLockMask
= ScrollLockMask
= CapsLockMask
=
1016 SuperMask
= HyperMask
= 0;
1018 /* Work out which mask to use for each modifier group */
1021 KeyCode
*c
= xmk
->modifiermap
;
1022 KeyCode numLockKeyCode
;
1023 KeyCode scrollLockKeyCode
;
1024 KeyCode capsLockKeyCode
;
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... */
1041 altKeyCode
= XKeysymToKeycode(dpy
, XK_Alt_R
);
1043 metaKeyCode
= XKeysymToKeycode(dpy
, XK_Meta_R
);
1045 superKeyCode
= XKeysymToKeycode(dpy
, XK_Super_R
);
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
++)
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
)
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
)
1082 if (AltMask
!= 0 && MetaMask
== Mod1Mask
)
1088 if (AltMask
== 0 && MetaMask
!= 0)
1090 if (MetaMask
!= 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
)
1110 Display
*dpy
= GDK_DISPLAY();
1118 k
= strrchr(str
, '+');
1119 key
->keycode
= XKeysymToKeycode(dpy
, XStringToKeysym(k
? k
+ 1 : str
));
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
;
1143 g_warning("Can't parse key '%s'\n", str
);
1146 static GdkFilterReturn
filter_keys(GdkXEvent
*xevent
,
1151 XKeyEvent
*kev
= (XKeyEvent
*) xevent
;
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
)
1168 run_diritem(icon
->path
, icon
->item
, NULL
, NULL
, FALSE
);
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, \
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
)
1201 Display
*dpy
= GDK_DISPLAY();
1202 static gboolean need_init
= TRUE
;
1207 gdk_window_add_filter(gdk_get_default_root_window(),
1211 gdk_error_trap_push();
1213 root
= GDK_ROOT_WINDOW();
1217 if (key
->modifier
!= AnyModifier
&& key
->modifier
!= 0)
1219 /* Here we grab all combinations of well known
1222 GRAB(key
, ScrollLockMask
);
1223 GRAB(key
, NumLockMask
);
1224 GRAB(key
, CapsLockMask
);
1225 GRAB(key
, ScrollLockMask
| NumLockMask
);
1226 GRAB(key
, ScrollLockMask
| CapsLockMask
);
1227 GRAB(key
, CapsLockMask
| NumLockMask
);
1228 GRAB(key
, ScrollLockMask
| CapsLockMask
| NumLockMask
);
1232 return gdk_error_trap_pop() == Success
;
1235 static gboolean
ungrabKey(MyKey
*key
)
1237 Window root
= GDK_ROOT_WINDOW();
1238 Display
*dpy
= GDK_DISPLAY();
1240 gdk_error_trap_push();
1244 if (key
->modifier
!= AnyModifier
&& key
->modifier
!= 0)
1246 UNGRAB(key
, ScrollLockMask
);
1247 UNGRAB(key
, NumLockMask
);
1248 UNGRAB(key
, CapsLockMask
);
1249 UNGRAB(key
, ScrollLockMask
| NumLockMask
);
1250 UNGRAB(key
, ScrollLockMask
| CapsLockMask
);
1251 UNGRAB(key
, CapsLockMask
| NumLockMask
);
1252 UNGRAB(key
, ScrollLockMask
| CapsLockMask
| NumLockMask
);
1256 return gdk_error_trap_pop() == Success
;
1259 static void ungrab_key(Icon
*icon
)
1263 g_return_if_fail(icon
!= NULL
);
1265 if (!icon
->shortcut_key
.keycode
)
1268 icon_shortcuts
= g_list_remove(icon_shortcuts
, icon
);
1270 count
= g_hash_table_lookup(grab_counter
, &icon
->shortcut_key
);
1271 g_return_if_fail(count
!= NULL
);
1278 g_hash_table_remove(grab_counter
, &icon
->shortcut_key
);
1280 if (!ungrabKey(&icon
->shortcut_key
))
1281 g_warning("Failed to ungrab shortcut '%s' for '%s' icon.",
1282 icon
->shortcut
, icon
->item
->leafname
);
1285 static void grab_key(Icon
*icon
)
1290 g_return_if_fail(icon
!= NULL
);
1292 g_return_if_fail(g_list_find(icon_shortcuts
, icon
) == NULL
);
1294 if (!icon
->shortcut_key
.keycode
)
1297 icon_shortcuts
= g_list_prepend(icon_shortcuts
, icon
);
1300 grab_counter
= g_hash_table_new_full(mykey_hash
, mykey_cmp
,
1303 count
= g_hash_table_lookup(grab_counter
, &icon
->shortcut_key
);
1307 return; /* Already grabbed */
1310 hash_key
= g_new(MyKey
, 1);
1311 *hash_key
= icon
->shortcut_key
;
1312 count
= g_new(int, 1);
1314 g_hash_table_insert(grab_counter
, hash_key
, count
);
1316 if (!grabKey(&icon
->shortcut_key
))
1317 g_warning("Failed to grab shortcut '%s' for '%s' icon.\n"
1318 "Some other application may be already using it!\n",
1319 icon
->shortcut
, icon
->item
->leafname
);
1323 static void icon_wink(Icon
*icon
)
1327 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
1328 g_return_if_fail(iclass
->wink
!= NULL
);