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
);
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
)
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
, "redraw");
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
)
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
,
249 menu_show_shift_action(file_shift_item
, icon
? icon
->item
: NULL
,
252 /* Shade the File/Edit/Show items unless an item was clicked */
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
);
268 /* Check for app-specific menu */
269 appmenu_add(icon
->path
, icon
->item
, icon_menu
);
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
)
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
);
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
;
308 icon_set_selected_int(select
, TRUE
);
310 to_clear
= g_list_copy(icon_selection
);
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)
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");
353 g_string_free(tmp
, FALSE
);
358 GType
icon_get_type(void)
360 static GType type
= 0;
364 static const GTypeInfo info
=
367 NULL
, /* base_init */
368 NULL
, /* base_finalise */
370 NULL
, /* class_finalise */
371 NULL
, /* class_data */
377 type
= g_type_register_static(G_TYPE_OBJECT
, "Icon",
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
)
393 icon_unhash_path(icon
);
394 icon
->src_path
= NULL
;
397 diritem_free(icon
->item
);
403 icon
->src_path
= g_strdup(pathname
);
404 icon
->path
= expand_path(pathname
);
406 icon_hash_path(icon
);
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
)
422 if (icon
->shortcut
== shortcut
)
424 if (icon
->shortcut
&& shortcut
&& strcmp(icon
->shortcut
, shortcut
) == 0)
429 g_free(icon
->shortcut
);
430 icon
->shortcut
= g_strdup(shortcut
);
431 parseKeyString(&icon
->shortcut_key
, shortcut
);
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
)
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
)
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
);
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
;
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
&&
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)
500 if (*new_src
== '\0')
502 _("The location must contain at least one character!"));
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
)
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
,
533 if (info
== TARGET_URI_LIST
)
534 text
= icon_create_uri_list();
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
, ' ');
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
)
569 icon_set_selected(menu_icon
, TRUE
);
574 _("You must first select some items to remove"));
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
)
588 delayed_error(_("You must open the menu over an item"));
595 run_diritem(menu_icon
->path
, menu_icon
->item
,
599 show_rename_box(menu_icon
);
601 case ACTION_LOCATION
:
602 open_to_show(menu_icon
->path
);
605 show_item_help(menu_icon
->path
, menu_icon
->item
);
608 infobox_new(menu_icon
->path
);
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
);
616 _("You can only set the run action for a "
619 case ACTION_SET_ICON
:
620 icon_set_handler_dialog(menu_icon
->item
,
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
,
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
)
649 unsigned int m
= kev
->state
;
651 sym
= XKeycodeToKeysym(dpy
, kev
->keycode
, 0);
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
)
677 str
= g_object_get_data(G_OBJECT(popup
), "chosen-key");
680 gtk_label_set_text(GTK_LABEL(label
), 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
;
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
745 static void show_rename_box(Icon
*icon
)
748 GtkWidget
*label
, *entry
, *button
, *button2
, *hbox
, *spacer
;
753 gtk_window_present(GTK_WINDOW(icon
->dialog
));
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
,
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
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
,
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
);
840 gtk_widget_destroy(icon
->dialog
);
842 if (icon
== menu_icon
)
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
)
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
;
865 icon
->same_group
= NULL
;
868 g_signal_new("update",
869 G_TYPE_FROM_CLASS(gclass
),
871 G_STRUCT_OFFSET(IconClass
, update
),
873 g_cclosure_marshal_VOID__VOID
,
876 g_signal_new("destroy",
877 G_TYPE_FROM_CLASS(gclass
),
879 G_STRUCT_OFFSET(IconClass
, destroy
),
881 g_cclosure_marshal_VOID__VOID
,
884 g_signal_new("redraw",
885 G_TYPE_FROM_CLASS(gclass
),
887 G_STRUCT_OFFSET(IconClass
, redraw
),
889 g_cclosure_marshal_VOID__VOID
,
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
),
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
);
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
;
908 /* Shift Open... label */
909 items
= gtk_container_get_children(GTK_CONTAINER(icon_file_menu
));
910 file_shift_item
= GTK_BIN(items
->data
)->child
;
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
;
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
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
)
944 primary
= gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE
));
948 icon_selection
= g_list_prepend(icon_selection
, icon
);
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
},
960 have_primary
= gtk_clipboard_set_with_data(primary
,
962 sizeof(target_table
) / sizeof(*target_table
),
963 selection_get
, lose_selection
, NULL
);
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
)
984 items
= gtk_container_get_children(GTK_CONTAINER(menu
));
986 item
= g_list_nth(items
, from
);
990 gtk_widget_hide(GTK_WIDGET(item
->data
));
992 gtk_widget_show(GTK_WIDGET(item
->data
));
998 /* Stolen from xfwm4 */
999 static void initModifiers(void)
1001 Display
*dpy
= GDK_DISPLAY();
1002 XModifierKeymap
*xmk
= XGetModifierMapping(dpy
);
1005 AltMask
= MetaMask
= NumLockMask
= ScrollLockMask
= CapsLockMask
=
1006 SuperMask
= HyperMask
= 0;
1008 /* Work out which mask to use for each modifier group */
1011 KeyCode
*c
= xmk
->modifiermap
;
1012 KeyCode numLockKeyCode
;
1013 KeyCode scrollLockKeyCode
;
1014 KeyCode capsLockKeyCode
;
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... */
1031 altKeyCode
= XKeysymToKeycode(dpy
, XK_Alt_R
);
1033 metaKeyCode
= XKeysymToKeycode(dpy
, XK_Meta_R
);
1035 superKeyCode
= XKeysymToKeycode(dpy
, XK_Super_R
);
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
++)
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
)
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
)
1072 if (AltMask
!= 0 && MetaMask
== Mod1Mask
)
1078 if (AltMask
== 0 && MetaMask
!= 0)
1080 if (MetaMask
!= 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
)
1100 Display
*dpy
= GDK_DISPLAY();
1108 k
= strrchr(str
, '+');
1109 key
->keycode
= XKeysymToKeycode(dpy
, XStringToKeysym(k
? k
+ 1 : str
));
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
;
1129 g_warning("Can't parse key '%s'\n", str
);
1132 static GdkFilterReturn
filter_keys(GdkXEvent
*xevent
,
1137 XKeyEvent
*kev
= (XKeyEvent
*) xevent
;
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
)
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, \
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.
1183 static gboolean
grabKey(MyKey
*key
)
1186 Display
*dpy
= GDK_DISPLAY();
1187 static gboolean need_init
= TRUE
;
1194 gdk_window_add_filter(gdk_get_default_root_window(),
1198 gdk_error_trap_push();
1200 root
= GDK_ROOT_WINDOW();
1204 if (key
->modifier
!= AnyModifier
&& key
->modifier
!= 0)
1206 /* Here we grab all combinations of well known
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
);
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();
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
);
1243 return gdk_error_trap_pop() == Success
;
1246 static void ungrab_key(Icon
*icon
)
1250 g_return_if_fail(icon
!= NULL
);
1252 if (!icon
->shortcut_key
.keycode
)
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
);
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
)
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
)
1284 icon_shortcuts
= g_list_prepend(icon_shortcuts
, icon
);
1287 grab_counter
= g_hash_table_new_full(mykey_hash
, mykey_cmp
,
1290 count
= g_hash_table_lookup(grab_counter
, &icon
->shortcut_key
);
1294 return; /* Already grabbed */
1297 hash_key
= g_new(MyKey
, 1);
1298 *hash_key
= icon
->shortcut_key
;
1299 count
= g_new(int, 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
)
1314 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
1315 g_return_if_fail(iclass
->wink
!= NULL
);