r1447: Middle button clicks on the pinboard are passed to the window manager.
[rox-filer.git] / ROX-Filer / src / icon.c
blob5d4aa9798e920968ba0fc2c87709d6c26b71b0af
1 /*
2 * $Id$
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)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* icon.c - abstract base class for pinboard and panel icons.
24 * An Icon contains the full pathname of its file and the DirItem for that
25 * file. Icons emit the following signals:
27 * redraw - the image, details or selection state have changed.
28 * update - the name or path has changed.
29 * destroy - someone wishes to remove the icon.
31 * Note that an icon may be removed without emitting 'destroy'.
34 #include "config.h"
36 #include <string.h>
37 #include <gtk/gtk.h>
39 #include "global.h"
41 #include "main.h"
42 #include "gui_support.h"
43 #include "support.h"
44 #include "icon.h"
45 #include "diritem.h"
46 #include "menu.h"
47 #include "appmenu.h"
48 #include "dnd.h"
49 #include "run.h"
50 #include "infobox.h"
51 #include "pixmaps.h"
52 #include "mount.h"
53 #include "type.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
67 * same time).
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,
84 guint info,
85 gpointer 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);
96 enum {
97 ACTION_SHIFT,
98 ACTION_HELP,
99 ACTION_INFO,
100 ACTION_RUN_ACTION,
101 ACTION_SET_ICON,
102 ACTION_BACKDROP,
103 ACTION_EDIT,
104 ACTION_LOCATION,
107 #undef N_
108 #define N_(x) x
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)
134 MaskedPixmap *image;
135 int flags;
137 g_return_if_fail(icon != NULL);
139 image = icon->item->image;
140 flags = icon->item->flags;
142 if (image)
143 g_object_ref(image);
144 mount_update(FALSE);
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");
153 if (image)
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)
160 GList *affected;
162 if (icons_hash)
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;
172 struct _CheckData {
173 const gchar *path;
174 gboolean found;
177 static void check_has(gpointer key, GList *icons, CheckData *check)
179 Icon *icon;
181 g_return_if_fail(icons != NULL);
183 icon = icons->data;
185 if (is_sub_dir(icon->path, check->path))
186 check->found = TRUE;
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)
194 CheckData check;
196 if (!icons_hash)
197 return FALSE;
199 check.path = path;
200 check.found = FALSE;
201 g_hash_table_foreach(icons_hash, (GHFunc) check_has, &check);
203 return check.found;
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)
212 appmenu_remove();
214 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,
221 4, 1);
223 menu_show_shift_action(file_shift_item, icon ? icon->item : NULL,
224 FALSE);
226 /* Shade the File/Edit/Show items unless an item was clicked */
227 if (icon)
229 guchar *tmp;
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);
240 g_free(tmp);
242 /* Check for app-specific menu */
243 appmenu_add(icon->path, icon->item, icon_menu);
245 else
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)
260 IconClass *iclass;
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);
267 return;
271 icon_set_selected_int(icon, TRUE);
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;
281 if (select)
282 icon_set_selected_int(select, TRUE);
284 to_clear = g_list_copy(icon_selection);
286 if (select)
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)
308 GString *tmp;
309 guchar *retval;
310 guchar *leader;
311 GList *next;
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");
325 g_free(leader);
326 retval = tmp->str;
327 g_string_free(tmp, FALSE);
329 return retval;
332 GType icon_get_type(void)
334 static GType type = 0;
336 if (!type)
338 static const GTypeInfo info =
340 sizeof (IconClass),
341 NULL, /* base_init */
342 NULL, /* base_finalise */
343 icon_class_init,
344 NULL, /* class_finalise */
345 NULL, /* class_data */
346 sizeof(Icon),
347 0, /* n_preallocs */
348 icon_init
351 type = g_type_register_static(G_TYPE_OBJECT, "Icon",
352 &info, 0);
355 return type;
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)
365 if (icon->path)
367 icon_unhash_path(icon);
368 icon->src_path = NULL;
369 icon->path = NULL;
371 diritem_free(icon->item);
372 icon->item = NULL;
375 if (pathname)
377 icon->src_path = g_strdup(pathname);
378 icon->path = expand_path(pathname);
380 icon_hash_path(icon);
382 if (!name)
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)
400 GList *list;
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)
414 GList *list;
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);
427 if (list)
428 g_hash_table_insert(icons_hash,
429 ((Icon *) list->data)->path, list);
432 static void rename_activate(GtkWidget *dialog)
434 GtkWidget *entry, *src;
435 Icon *icon;
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 &&
443 src != NULL &&
444 icon != 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')
450 report_error(
451 _("The label must contain at least one character!"));
452 else if (*new_src == '\0')
453 report_error(
454 _("The location must contain at least one character!"));
455 else
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)
465 appmenu_remove();
466 menu_icon = NULL;
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,
479 guint info,
480 gpointer data)
482 gchar *text;
484 if (info == TARGET_URI_LIST)
485 text = icon_create_uri_list();
486 else
488 GList *next;
489 GString *str;
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, ' ');
501 text = str->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)
517 IconClass *iclass;
519 if (menu_icon)
520 icon_set_selected(menu_icon, TRUE);
522 if (!icon_selection)
524 delayed_error(
525 _("You must first select some items to remove"));
526 return;
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)
537 if (!menu_icon)
539 delayed_error(_("You must open the menu over an item"));
540 return;
543 switch (action)
545 case ACTION_SHIFT:
546 run_diritem(menu_icon->path, menu_icon->item,
547 NULL, NULL, TRUE);
548 break;
549 case ACTION_EDIT:
550 show_rename_box(menu_icon);
551 break;
552 case ACTION_LOCATION:
553 open_to_show(menu_icon->path);
554 break;
555 case ACTION_HELP:
556 show_item_help(menu_icon->path, menu_icon->item);
557 break;
558 case ACTION_INFO:
559 infobox_new(menu_icon->path);
560 break;
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);
565 else
566 report_error(
567 _("You can only set the run action for a "
568 "regular file"));
569 break;
570 case ACTION_SET_ICON:
571 icon_set_handler_dialog(menu_icon->item,
572 menu_icon->path);
573 break;
574 case ACTION_BACKDROP:
575 pinboard_set_backdrop(menu_icon->item, menu_icon->path);
576 break;
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
592 * restatted.
594 static void show_rename_box(Icon *icon)
596 GtkDialog *dialog;
597 GtkWidget *label, *entry;
599 if (icon->dialog)
601 gtk_window_present(GTK_WINDOW(icon->dialog));
602 return;
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,
641 NULL);
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);
657 if (icon->dialog)
658 gtk_widget_destroy(icon->dialog);
660 if (icon == menu_icon)
661 menu_icon = NULL;
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)
670 guchar *tmp;
671 GList *items;
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;
680 icon->redraw = NULL;
681 icon->update = NULL;
682 icon->same_group = NULL;
684 g_signal_new("update",
685 G_TYPE_FROM_CLASS(gclass),
686 G_SIGNAL_RUN_LAST,
687 G_STRUCT_OFFSET(IconClass, update),
688 NULL, NULL,
689 g_cclosure_marshal_VOID__VOID,
690 G_TYPE_NONE, 0);
692 g_signal_new("destroy",
693 G_TYPE_FROM_CLASS(gclass),
694 G_SIGNAL_RUN_LAST,
695 G_STRUCT_OFFSET(IconClass, destroy),
696 NULL, NULL,
697 g_cclosure_marshal_VOID__VOID,
698 G_TYPE_NONE, 0);
700 g_signal_new("redraw",
701 G_TYPE_FROM_CLASS(gclass),
702 G_SIGNAL_RUN_LAST,
703 G_STRUCT_OFFSET(IconClass, redraw),
704 NULL, NULL,
705 g_cclosure_marshal_VOID__VOID,
706 G_TYPE_NONE, 0);
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),
712 "<icon>", NULL);
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);
717 g_free(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;
722 g_list_free(items);
724 /* Shift Open... label */
725 items = gtk_container_get_children(GTK_CONTAINER(icon_file_menu));
726 file_shift_item = GTK_BIN(items->data)->child;
727 g_list_free(items);
729 items = gtk_container_get_children(GTK_CONTAINER(icon_menu));
730 icon_menu_remove_backdrop = g_list_nth(items, 5)->data;
731 g_list_free(items);
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;
743 icon->path = NULL;
744 icon->item = NULL;
745 icon->dialog = NULL;
748 /* As icon_set_selected(), but doesn't automatically unselect incompatible
749 * icons.
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)
758 return;
760 if (!primary)
761 primary = gtk_clipboard_get(gdk_atom_intern("PRIMARY", FALSE));
763 if (selected)
765 icon_selection = g_list_prepend(icon_selection, icon);
766 if (!have_primary)
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},
776 /* Grab selection */
777 have_primary = gtk_clipboard_set_with_data(primary,
778 target_table,
779 sizeof(target_table) / sizeof(*target_table),
780 selection_get, lose_selection, NULL);
783 else
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");