4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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'.
42 #include "gui_support.h"
54 #include "usericons.h"
55 #include "pinboard.h" /* For pinboard_set_backdrop */
57 static gboolean have_primary
= FALSE
; /* We own the PRIMARY selection? */
59 GtkWidget
*icon_menu
; /* The popup icon menu */
60 GtkWidget
*icon_menu_remove_backdrop
;
61 static GtkWidget
*icon_file_menu
; /* The file submenu */
62 static GtkWidget
*icon_file_item
; /* 'File' label */
63 static GtkWidget
*file_shift_item
; /* 'Shift Open' label */
65 /* A list of selected Icons. Every icon in the list is from the same group
66 * (eg, you can't have icons from two different panels selected at the
69 GList
*icon_selection
= NULL
;
71 /* Each entry is a GList of Icons which have the given pathname.
72 * This allows us to update all necessary icons when something changes.
74 static GHashTable
*icons_hash
= NULL
; /* path -> [Icon] */
76 static Icon
*menu_icon
= NULL
; /* Item clicked if there is no selection */
78 /* Static prototypes */
79 static void rename_activate(GtkWidget
*dialog
);
80 static void menu_closed(GtkWidget
*widget
);
81 static void lose_selection(GtkClipboard
*primary
, gpointer data
);
82 static void selection_get(GtkClipboard
*primary
,
83 GtkSelectionData
*selection_data
,
86 static void remove_items(gpointer data
, guint action
, GtkWidget
*widget
);
87 static void remove_backdrop(gpointer data
, guint action
, GtkWidget
*widget
);
88 static void file_op(gpointer data
, guint action
, GtkWidget
*widget
);
89 static void show_rename_box(Icon
*icon
);
90 static void icon_set_selected_int(Icon
*icon
, gboolean selected
);
91 static void icon_class_init(gpointer gclass
, gpointer data
);
92 static void icon_init(GTypeInstance
*object
, gpointer gclass
);
93 static void icon_hash_path(Icon
*icon
);
94 static void icon_unhash_path(Icon
*icon
);
109 static GtkItemFactoryEntry menu_def
[] = {
110 {N_("ROX-Filer"), NULL
, NULL
, 0, "<Branch>"},
111 {">" N_("Help"), NULL
, menu_rox_help
, 0, NULL
},
112 {">" N_("Options..."), NULL
, menu_show_options
, 0, NULL
},
113 {">" N_("Home Directory"), NULL
, open_home
, 0, NULL
},
114 {N_("File"), NULL
, NULL
, 0, "<Branch>"},
115 {">" N_("Shift Open"), NULL
, file_op
, ACTION_SHIFT
, NULL
},
116 {">" N_("Help"), NULL
, file_op
, ACTION_HELP
, NULL
},
117 {">" N_("Info"), NULL
, file_op
, ACTION_INFO
, NULL
},
118 {">" N_("Set Run Action..."), NULL
, file_op
, ACTION_RUN_ACTION
, NULL
},
119 {">" N_("Set Icon..."), NULL
, file_op
, ACTION_SET_ICON
, NULL
},
120 {">" N_("Use for Backdrop"), NULL
, file_op
, ACTION_BACKDROP
, NULL
},
121 {N_("Edit Item"), NULL
, file_op
, ACTION_EDIT
, NULL
},
122 {N_("Show Location"), NULL
, file_op
, ACTION_LOCATION
, NULL
},
123 {N_("Remove Item(s)"), NULL
, remove_items
, 0, NULL
},
124 {N_("Remove Backdrop"), NULL
, remove_backdrop
, 0, NULL
},
127 /****************************************************************
128 * EXTERNAL INTERFACE *
129 ****************************************************************/
131 /* Called when the pointer moves over the icon */
132 void icon_may_update(Icon
*icon
)
137 g_return_if_fail(icon
!= NULL
);
139 image
= icon
->item
->image
;
140 flags
= icon
->item
->flags
;
145 diritem_restat(icon
->path
, icon
->item
, NULL
);
147 if (icon
->item
->image
!= image
|| icon
->item
->flags
!= flags
)
149 /* Appearance changed; need to redraw */
150 g_signal_emit_by_name(icon
, "redraw");
154 g_object_unref(image
);
157 /* If path is on an icon then it may have changed... check! */
158 void icons_may_update(const gchar
*path
)
164 affected
= g_hash_table_lookup(icons_hash
, path
);
166 for (; affected
; affected
= affected
->next
)
167 icon_may_update((Icon
*) affected
->data
);
171 typedef struct _CheckData CheckData
;
177 static void check_has(gpointer key
, GList
*icons
, CheckData
*check
)
181 g_return_if_fail(icons
!= NULL
);
185 if (is_sub_dir(icon
->path
, check
->path
))
189 /* Returns TRUE if any icon links to this file (or any file inside
190 * this directory). Used to check that it's OK to delete 'path'.
192 gboolean
icons_require(const gchar
*path
)
201 g_hash_table_foreach(icons_hash
, (GHFunc
) check_has
, &check
);
206 /* Menu was clicked over this icon. Set things up correctly (shade items,
207 * add app menu stuff, etc).
208 * You should show icon_menu after calling this...
210 void icon_prepare_menu(Icon
*icon
)
216 gtk_widget_hide(icon_menu_remove_backdrop
);
218 /* Shade Remove Item(s) unless there is a selection */
219 menu_set_items_shaded(icon_menu
,
220 (icon_selection
|| menu_icon
) ? FALSE
: TRUE
,
223 menu_show_shift_action(file_shift_item
, icon
? icon
->item
: NULL
,
226 /* Shade the File/Edit/Show items unless an item was clicked */
231 menu_set_items_shaded(icon_menu
, FALSE
, 1, 3);
232 menu_set_items_shaded(icon_file_menu
, FALSE
, 0, 6);
233 if (!can_set_run_action(icon
->item
))
234 menu_set_items_shaded(icon_file_menu
, TRUE
, 3, 1);
236 tmp
= g_strdup_printf("%s '%s'",
237 basetype_name(icon
->item
),
238 icon
->item
->leafname
);
239 gtk_label_set_text(GTK_LABEL(icon_file_item
), tmp
);
242 /* Check for app-specific menu */
243 appmenu_add(icon
->path
, icon
->item
, icon_menu
);
247 menu_set_items_shaded(icon_menu
, TRUE
, 1, 3);
248 menu_set_items_shaded(icon_file_menu
, TRUE
, 0, 6);
249 gtk_label_set_text(GTK_LABEL(icon_file_item
), _("Nothing"));
253 /* Set whether this icon is selected. Will automatically clear the selection
254 * if it contains icons from a different group.
256 void icon_set_selected(Icon
*icon
, gboolean selected
)
258 if (selected
&& icon_selection
)
261 Icon
*other
= (Icon
*) icon_selection
->data
;
263 iclass
= (IconClass
*) G_OBJECT_GET_CLASS(icon
);
264 if (!iclass
->same_group(icon
, other
))
266 icon_select_only(icon
);
271 icon_set_selected_int(icon
, selected
);
274 /* Clear everything, except 'select', which is selected.
275 * If select is NULL, unselects everything.
277 void icon_select_only(Icon
*select
)
279 GList
*to_clear
, *next
;
282 icon_set_selected_int(select
, TRUE
);
284 to_clear
= g_list_copy(icon_selection
);
287 to_clear
= g_list_remove(to_clear
, select
);
289 for (next
= to_clear
; next
; next
= next
->next
)
290 icon_set_selected_int((Icon
*) next
->data
, FALSE
);
292 g_list_free(to_clear
);
295 /* Destroys this icon's widget, causing it to be removed from the screen */
296 void icon_destroy(Icon
*icon
)
298 g_return_if_fail(icon
!= NULL
);
300 icon_set_selected_int(icon
, FALSE
);
302 g_signal_emit_by_name(icon
, "destroy");
305 /* Return a text/uri-list of all the icons in the selection */
306 gchar
*icon_create_uri_list(void)
313 tmp
= g_string_new(NULL
);
314 leader
= g_strdup_printf("file://%s", our_host_name_for_dnd());
316 for (next
= icon_selection
; next
; next
= next
->next
)
318 Icon
*icon
= (Icon
*) next
->data
;
320 g_string_append(tmp
, leader
);
321 g_string_append(tmp
, icon
->path
);
322 g_string_append(tmp
, "\r\n");
327 g_string_free(tmp
, FALSE
);
332 GType
icon_get_type(void)
334 static GType type
= 0;
338 static const GTypeInfo info
=
341 NULL
, /* base_init */
342 NULL
, /* base_finalise */
344 NULL
, /* class_finalise */
345 NULL
, /* class_data */
351 type
= g_type_register_static(G_TYPE_OBJECT
, "Icon",
358 /* Sets, unsets or changes the pathname and name for an icon.
359 * Updates icons_hash and (re)stats the item.
360 * If name is NULL then gets the leafname from pathname.
361 * If pathname is NULL, frees everything.
363 void icon_set_path(Icon
*icon
, const char *pathname
, const char *name
)
367 icon_unhash_path(icon
);
368 icon
->src_path
= NULL
;
371 diritem_free(icon
->item
);
377 icon
->src_path
= g_strdup(pathname
);
378 icon
->path
= expand_path(pathname
);
380 icon_hash_path(icon
);
383 name
= g_basename(pathname
);
385 icon
->item
= diritem_new(name
);
386 diritem_restat(icon
->path
, icon
->item
, NULL
);
390 /****************************************************************
391 * INTERNAL FUNCTIONS *
392 ****************************************************************/
394 /* The icons_hash table allows us to convert from a path to a list
395 * of icons that use that path.
396 * Add this icon to the list for its path.
398 static void icon_hash_path(Icon
*icon
)
402 g_return_if_fail(icon
!= NULL
);
404 /* g_print("[ hashing '%s' ]\n", icon->path); */
406 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
407 list
= g_list_prepend(list
, icon
);
408 g_hash_table_insert(icons_hash
, icon
->path
, list
);
411 /* Remove this icon from the icons_hash table */
412 static void icon_unhash_path(Icon
*icon
)
416 g_return_if_fail(icon
!= NULL
);
418 /* g_print("[ unhashing '%s' ]\n", icon->path); */
420 list
= g_hash_table_lookup(icons_hash
, icon
->path
);
421 g_return_if_fail(list
!= NULL
);
423 list
= g_list_remove(list
, icon
);
425 /* Remove it first; the hash key may have changed address */
426 g_hash_table_remove(icons_hash
, icon
->path
);
428 g_hash_table_insert(icons_hash
,
429 ((Icon
*) list
->data
)->path
, list
);
432 static void rename_activate(GtkWidget
*dialog
)
434 GtkWidget
*entry
, *src
;
436 const guchar
*new_name
, *new_src
;
438 entry
= g_object_get_data(G_OBJECT(dialog
), "new_name");
439 icon
= g_object_get_data(G_OBJECT(dialog
), "callback_icon");
440 src
= g_object_get_data(G_OBJECT(dialog
), "new_path");
442 g_return_if_fail(entry
!= NULL
&&
446 new_name
= gtk_entry_get_text(GTK_ENTRY(entry
));
447 new_src
= gtk_entry_get_text(GTK_ENTRY(src
));
449 if (*new_name
== '\0')
451 _("The label must contain at least one character!"));
452 else if (*new_src
== '\0')
454 _("The location must contain at least one character!"));
457 icon_set_path(icon
, new_src
, new_name
);
458 g_signal_emit_by_name(icon
, "update");
459 gtk_widget_destroy(dialog
);
463 static void menu_closed(GtkWidget
*widget
)
469 /* Called when another application takes the selection away from us */
470 static void lose_selection(GtkClipboard
*primary
, gpointer data
)
472 have_primary
= FALSE
;
473 icon_select_only(NULL
);
476 /* Called when another application wants the contents of our selection */
477 static void selection_get(GtkClipboard
*primary
,
478 GtkSelectionData
*selection_data
,
484 if (info
== TARGET_URI_LIST
)
485 text
= icon_create_uri_list();
491 str
= g_string_new(NULL
);
493 for (next
= icon_selection
; next
; next
= next
->next
)
495 Icon
*icon
= (Icon
*) next
->data
;
497 g_string_append(str
, icon
->path
);
498 g_string_append_c(str
, ' ');
502 g_string_free(str
, FALSE
);
505 gtk_selection_data_set(selection_data
,
506 gdk_atom_intern("STRING", FALSE
),
507 8, text
, strlen(text
));
510 static void remove_backdrop(gpointer data
, guint action
, GtkWidget
*widget
)
512 pinboard_set_backdrop(NULL
, NULL
);
515 static void remove_items(gpointer data
, guint action
, GtkWidget
*widget
)
520 icon_set_selected(menu_icon
, TRUE
);
525 _("You must first select some items to remove"));
529 iclass
= (IconClass
*)
530 G_OBJECT_GET_CLASS(G_OBJECT(icon_selection
->data
));
532 iclass
->remove_items();
535 static void file_op(gpointer data
, guint action
, GtkWidget
*widget
)
539 delayed_error(_("You must open the menu over an item"));
546 run_diritem(menu_icon
->path
, menu_icon
->item
,
550 show_rename_box(menu_icon
);
552 case ACTION_LOCATION
:
553 open_to_show(menu_icon
->path
);
556 show_item_help(menu_icon
->path
, menu_icon
->item
);
559 infobox_new(menu_icon
->path
);
561 case ACTION_RUN_ACTION
:
562 if (can_set_run_action(menu_icon
->item
))
563 type_set_handler_dialog(
564 menu_icon
->item
->mime_type
);
567 _("You can only set the run action for a "
570 case ACTION_SET_ICON
:
571 icon_set_handler_dialog(menu_icon
->item
,
574 case ACTION_BACKDROP
:
575 pinboard_set_backdrop(menu_icon
->item
, menu_icon
->path
);
580 static void edit_response(GtkWidget
*dialog
, gint response
, gpointer data
)
582 if (response
== GTK_RESPONSE_OK
)
583 rename_activate(dialog
);
584 else if (response
== GTK_RESPONSE_CANCEL
)
585 gtk_widget_destroy(dialog
);
588 /* Opens a box allowing the user to change the name of a pinned icon.
589 * If the icon is destroyed then the box will close.
590 * If the user chooses OK then the callback is called once the icon's
591 * name, src_path and path fields have been updated and the item field
594 static void show_rename_box(Icon
*icon
)
597 GtkWidget
*label
, *entry
;
601 gtk_window_present(GTK_WINDOW(icon
->dialog
));
605 icon
->dialog
= gtk_dialog_new();
606 g_signal_connect(icon
->dialog
, "destroy",
607 G_CALLBACK(gtk_widget_destroyed
), &icon
->dialog
);
609 dialog
= GTK_DIALOG(icon
->dialog
);
611 gtk_window_set_title(GTK_WINDOW(dialog
), _("Edit Item"));
612 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
614 label
= gtk_label_new(_("Clicking the icon opens:"));
615 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), label
, TRUE
, TRUE
, 0);
617 entry
= gtk_entry_new();
618 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, TRUE
, FALSE
, 2);
619 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->src_path
);
620 g_object_set_data(G_OBJECT(dialog
), "new_path", entry
);
621 g_signal_connect_swapped(entry
, "activate",
622 G_CALLBACK(rename_activate
), dialog
);
624 gtk_box_pack_start(GTK_BOX(dialog
->vbox
),
625 gtk_hseparator_new(), TRUE
, TRUE
, 2);
627 label
= gtk_label_new(_("The text displayed under the icon is:"));
628 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), label
, TRUE
, TRUE
, 0);
629 entry
= gtk_entry_new();
630 gtk_box_pack_start(GTK_BOX(dialog
->vbox
), entry
, TRUE
, FALSE
, 2);
631 gtk_entry_set_text(GTK_ENTRY(entry
), icon
->item
->leafname
);
632 gtk_widget_grab_focus(entry
);
633 g_object_set_data(G_OBJECT(dialog
), "new_name", entry
);
634 gtk_entry_set_activates_default(GTK_ENTRY(entry
), TRUE
);
636 g_object_set_data(G_OBJECT(dialog
), "callback_icon", icon
);
638 gtk_dialog_add_buttons(dialog
,
639 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
640 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
642 gtk_dialog_set_default_response(dialog
, GTK_RESPONSE_OK
);
644 g_signal_connect(dialog
, "response", G_CALLBACK(edit_response
), NULL
);
646 gtk_widget_show_all(GTK_WIDGET(dialog
));
649 static gpointer parent_class
= NULL
;
651 static void icon_finialize(GObject
*object
)
653 Icon
*icon
= (Icon
*) object
;
655 g_return_if_fail(!icon
->selected
);
658 gtk_widget_destroy(icon
->dialog
);
660 if (icon
== menu_icon
)
663 icon_set_path(icon
, NULL
, NULL
);
665 G_OBJECT_CLASS(parent_class
)->finalize(object
);
668 static void icon_class_init(gpointer gclass
, gpointer data
)
672 GtkItemFactory
*item_factory
;
673 GObjectClass
*object
= (GObjectClass
*) gclass
;
674 IconClass
*icon
= (IconClass
*) gclass
;
676 parent_class
= g_type_class_peek_parent(gclass
);
678 object
->finalize
= icon_finialize
;
679 icon
->destroy
= NULL
;
682 icon
->same_group
= NULL
;
684 g_signal_new("update",
685 G_TYPE_FROM_CLASS(gclass
),
687 G_STRUCT_OFFSET(IconClass
, update
),
689 g_cclosure_marshal_VOID__VOID
,
692 g_signal_new("destroy",
693 G_TYPE_FROM_CLASS(gclass
),
695 G_STRUCT_OFFSET(IconClass
, destroy
),
697 g_cclosure_marshal_VOID__VOID
,
700 g_signal_new("redraw",
701 G_TYPE_FROM_CLASS(gclass
),
703 G_STRUCT_OFFSET(IconClass
, redraw
),
705 g_cclosure_marshal_VOID__VOID
,
708 icons_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
710 item_factory
= menu_create(menu_def
,
711 sizeof(menu_def
) / sizeof(*menu_def
),
714 tmp
= g_strconcat("<icon>/", _("File"), NULL
);
715 icon_menu
= gtk_item_factory_get_widget(item_factory
, "<icon>");
716 icon_file_menu
= gtk_item_factory_get_widget(item_factory
, tmp
);
719 /* File '' label... */
720 items
= gtk_container_get_children(GTK_CONTAINER(icon_menu
));
721 icon_file_item
= GTK_BIN(g_list_nth(items
, 1)->data
)->child
;
724 /* Shift Open... label */
725 items
= gtk_container_get_children(GTK_CONTAINER(icon_file_menu
));
726 file_shift_item
= GTK_BIN(items
->data
)->child
;
729 items
= gtk_container_get_children(GTK_CONTAINER(icon_menu
));
730 icon_menu_remove_backdrop
= g_list_nth(items
, 5)->data
;
733 g_signal_connect(icon_menu
, "unmap_event",
734 G_CALLBACK(menu_closed
), NULL
);
737 static void icon_init(GTypeInstance
*object
, gpointer gclass
)
739 Icon
*icon
= (Icon
*) object
;
741 icon
->selected
= FALSE
;
742 icon
->src_path
= NULL
;
748 /* As icon_set_selected(), but doesn't automatically unselect incompatible
751 static void icon_set_selected_int(Icon
*icon
, gboolean selected
)
753 static GtkClipboard
*primary
;
755 g_return_if_fail(icon
!= NULL
);
757 if (icon
->selected
== selected
)
761 primary
= gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE
));
765 icon_selection
= g_list_prepend(icon_selection
, icon
);
768 GtkTargetEntry target_table
[] =
770 {"text/uri-list", 0, TARGET_URI_LIST
},
771 {"UTF8", 0, TARGET_STRING
},
772 {"COMPOUND_TEXT", 0, TARGET_STRING
},
773 {"STRING", 0, TARGET_STRING
},
777 have_primary
= gtk_clipboard_set_with_data(primary
,
779 sizeof(target_table
) / sizeof(*target_table
),
780 selection_get
, lose_selection
, NULL
);
785 icon_selection
= g_list_remove(icon_selection
, icon
);
786 if (have_primary
&& !icon_selection
)
788 have_primary
= FALSE
;
789 gtk_clipboard_clear(primary
);
793 icon
->selected
= selected
;
794 g_signal_emit_by_name(icon
, "redraw");