r509: Moving pinboard icons is nicer - it no longer assumes you dragged the middle
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob34c12b904feca59cebb195a26f22a97ca19aa22b
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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 /* pinboard.c - icons on the background */
24 #include "config.h"
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <gtk/gtkinvisible.h>
34 #include "global.h"
36 #include "pinboard.h"
37 #include "main.h"
38 #include "dnd.h"
39 #include "pixmaps.h"
40 #include "type.h"
41 #include "choices.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "run.h"
45 #include "menu.h"
46 #include "options.h"
47 #include "dir.h"
48 #include "mount.h"
49 #include "bind.h"
50 #include "icon.h"
51 #include "appmenu.h"
53 /* The number of pixels between the bottom of the image and the top
54 * of the text.
56 #define GAP 4
58 /* The size of the border around the icon which is used when winking */
59 #define WINK_FRAME 2
61 /* Grid sizes */
62 #define GRID_STEP_FINE 2
63 #define GRID_STEP_MED 16
64 #define GRID_STEP_COARSE 32
66 /* Used for the text colours (only) in the icons */
67 GdkColor text_fg_col, text_bg_col;
69 /* Style that all the icons should use. NULL => regenerate from text_fg/bg */
70 GtkStyle *pinicon_style = NULL;
72 struct _Pinboard {
73 guchar *name; /* Leaf name */
74 GList *icons;
77 static Pinboard *current_pinboard = NULL;
78 static gint loading_pinboard = 0; /* Non-zero => loading */
79 static gboolean tmp_icon_selected = FALSE;
82 static Icon *current_wink_icon = NULL;
83 static gint wink_timeout;
85 static gint number_selected = 0;
87 static GdkColor mask_solid = {1, 1, 1, 1};
88 static GdkColor mask_transp = {0, 0, 0, 0};
89 static GdkGC *mask_gc = NULL;
91 /* Proxy window for DnD and clicks on the desktop */
92 static GtkWidget *proxy_invisible;
94 /* The window (owned by the wm) which root clicks are forwarded to.
95 * NULL if wm does not support forwarding clicks.
97 static GdkWindow *click_proxy_gdk_window = NULL;
98 static GdkAtom win_button_proxy; /* _WIN_DESKTOP_BUTTON_PROXY */
100 static gboolean pinboard_drag_in_progress = FALSE;
102 /* Used when dragging icons around... */
103 static gboolean pinboard_modified = FALSE;
105 typedef enum {
106 TEXT_BG_NONE = 0,
107 TEXT_BG_OUTLINE = 1,
108 TEXT_BG_SOLID = 2,
109 } TextBgType;
111 TextBgType o_text_bg = TEXT_BG_SOLID;
112 gboolean o_clamp_icons = TRUE;
113 static int o_grid_step = GRID_STEP_COARSE;
114 static int old_x, old_y; /* For dragging (mouse start) */
115 static int icon_old_x, icon_old_y; /* For dragging (icon start) */
117 /* Static prototypes */
118 static void set_size_and_shape(Icon *icon, int *rwidth, int *rheight);
119 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, Icon *icon);
120 static void mask_wink_border(Icon *icon, GdkColor *alpha);
121 static gint end_wink(gpointer data);
122 static gboolean button_release_event(GtkWidget *widget,
123 GdkEventButton *event,
124 Icon *icon);
125 static gboolean root_property_event(GtkWidget *widget,
126 GdkEventProperty *event,
127 gpointer data);
128 static gboolean root_button_press(GtkWidget *widget,
129 GdkEventButton *event,
130 gpointer data);
131 static gboolean enter_notify(GtkWidget *widget,
132 GdkEventCrossing *event,
133 Icon *icon);
134 static gboolean button_press_event(GtkWidget *widget,
135 GdkEventButton *event,
136 Icon *icon);
137 static gint icon_motion_notify(GtkWidget *widget,
138 GdkEventMotion *event,
139 Icon *icon);
140 static char *pin_from_file(guchar *line);
141 static gboolean add_root_handlers(void);
142 static void pinboard_save(void);
143 static GdkFilterReturn proxy_filter(GdkXEvent *xevent,
144 GdkEvent *event,
145 gpointer data);
146 static void icon_destroyed(GtkWidget *widget, Icon *icon);
147 static void snap_to_grid(int *x, int *y);
148 static void offset_from_centre(Icon *icon,
149 int width, int height,
150 int *x, int *y);
151 static void offset_to_centre(Icon *icon,
152 int width, int height,
153 int *x, int *y);
154 static gboolean drag_motion(GtkWidget *widget,
155 GdkDragContext *context,
156 gint x,
157 gint y,
158 guint time,
159 Icon *icon);
160 static void drag_set_pinicon_dest(Icon *icon);
161 static void drag_leave(GtkWidget *widget,
162 GdkDragContext *context,
163 guint32 time,
164 Icon *icon);
165 static void forward_root_clicks(void);
166 static void change_number_selected(int delta);
167 static gint lose_selection(GtkWidget *widget, GdkEventSelection *event);
168 static void selection_get(GtkWidget *widget,
169 GtkSelectionData *selection_data,
170 guint info,
171 guint time,
172 gpointer data);
173 static gboolean bg_drag_motion(GtkWidget *widget,
174 GdkDragContext *context,
175 gint x,
176 gint y,
177 guint time,
178 gpointer data);
179 static void drag_end(GtkWidget *widget,
180 GdkDragContext *context,
181 Icon *icon);
182 static void reshape_icon(Icon *icon);
183 static void reshape_all(void);
184 static void menu_closed(GtkWidget *widget);
185 static void edit_icon(gpointer data, guint action, GtkWidget *widget);
186 static void show_location(gpointer data, guint action, GtkWidget *widget);
187 static void pin_help(gpointer data, guint action, GtkWidget *widget);
188 static void pin_remove(gpointer data, guint action, GtkWidget *widget);
189 static void show_pinboard_menu(GdkEventButton *event, Icon *icon);
191 static void pinboard_check_options(void);
193 static GtkItemFactoryEntry menu_def[] = {
194 {N_("ROX-Filer Help"), NULL, menu_rox_help, 0, NULL},
195 {N_("ROX-Filer Options..."), NULL, menu_show_options, 0, NULL},
196 {N_("Open Home Directory"), NULL, open_home, 0, NULL},
197 {"", NULL, NULL, 0, "<Separator>"},
198 {N_("Edit Icon"), NULL, edit_icon, 0, NULL},
199 {N_("Show Location"), NULL, show_location, 0, NULL},
200 {N_("Show Help"), NULL, pin_help, 0, NULL},
201 {N_("Remove Item(s)"), NULL, pin_remove, 0, NULL},
204 static GtkWidget *pinboard_menu; /* The popup pinboard menu */
206 /****************************************************************
207 * EXTERNAL INTERFACE *
208 ****************************************************************/
210 void pinboard_init(void)
212 GtkStyle *style;
214 option_add_string("pinboard_fg_colour", "#000", NULL);
215 option_add_string("pinboard_bg_colour", "#ddd", NULL);
217 option_add_int("pinboard_text_bg", TEXT_BG_SOLID, NULL);
218 option_add_int("pinboard_clamp_icons", 1, NULL);
219 option_add_int("pinboard_grid_step", GRID_STEP_COARSE, NULL);
220 option_add_notify(pinboard_check_options);
222 style = gtk_widget_get_default_style();
224 gdk_color_parse(option_get_static_string("pinboard_fg_colour"),
225 &text_fg_col);
226 gdk_color_parse(option_get_static_string("pinboard_bg_colour"),
227 &text_bg_col);
229 pinboard_menu = menu_create(menu_def,
230 sizeof(menu_def) / sizeof(*menu_def),
231 "<pinboard>");
232 gtk_signal_connect(GTK_OBJECT(pinboard_menu), "unmap_event",
233 GTK_SIGNAL_FUNC(menu_closed), NULL);
234 /* This is used for AppMenus */
235 gtk_object_set_data(GTK_OBJECT(pinboard_menu), "last_appmenu", NULL);
238 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
239 * and make it the current pinboard.
240 * Any existing pinned items are removed. You must call this
241 * at least once before using the pinboard. NULL disables the
242 * pinboard.
244 void pinboard_activate(guchar *name)
246 Pinboard *old_board = current_pinboard;
247 guchar *path, *slash;
249 /* Treat an empty name the same as NULL */
250 if (!*name)
251 name = NULL;
253 if (old_board)
255 pinboard_clear();
256 number_of_windows--;
259 if (!name)
261 if (number_of_windows < 1 && gtk_main_level() > 0)
262 gtk_main_quit();
263 return;
266 if (!add_root_handlers())
268 delayed_error(PROJECT, _("Another application is already "
269 "managing the pinboard!"));
270 return;
273 number_of_windows++;
275 slash = strchr(name, '/');
276 if (slash)
278 if (access(name, F_OK))
279 path = NULL; /* File does not (yet) exist */
280 else
281 path = g_strdup(name);
283 else
285 guchar *leaf;
287 leaf = g_strconcat("pb_", name, NULL);
288 path = choices_find_path_load(leaf, "ROX-Filer");
289 g_free(leaf);
292 current_pinboard = g_new(Pinboard, 1);
293 current_pinboard->name = g_strdup(name);
294 current_pinboard->icons = NULL;
296 if (path)
298 loading_pinboard++;
299 parse_file(path, pin_from_file);
300 loading_pinboard--;
301 g_free(path);
305 /* Add a new icon to the background.
306 * 'path' should be an absolute pathname.
307 * 'x' and 'y' are the coordinates of the point in the middle of the text.
308 * 'name' is the name to use. If NULL then the leafname of path is used.
310 void pinboard_pin(guchar *path, guchar *name, int x, int y)
312 Icon *icon;
313 int width, height;
315 g_return_if_fail(path != NULL);
316 g_return_if_fail(current_pinboard != NULL);
318 icon = g_new(Icon, 1);
319 icon->type = ICON_PINBOARD;
320 icon->selected = FALSE;
321 icon->src_path = g_strdup(path);
322 icon->path = icon_convert_path(path);
323 icon->mask = NULL;
324 snap_to_grid(&x, &y);
325 icon->x = x;
326 icon->y = y;
328 icon_hash_path(icon);
330 dir_stat(icon->path, &icon->item, FALSE);
332 if (!name)
334 name = strrchr(icon->path, '/');
335 if (name && name[1])
336 name++;
337 else
338 name = icon->path;
341 icon->item.leafname = g_strdup(name);
343 icon->win = gtk_window_new(GTK_WINDOW_DIALOG);
344 gtk_window_set_wmclass(GTK_WINDOW(icon->win), "ROX-Pinboard", PROJECT);
346 icon->widget = gtk_drawing_area_new();
347 gtk_container_add(GTK_CONTAINER(icon->win), icon->widget);
348 drag_set_pinicon_dest(icon);
349 gtk_signal_connect(GTK_OBJECT(icon->widget), "drag_data_get",
350 drag_data_get, NULL);
352 gtk_widget_realize(icon->win);
353 gtk_widget_realize(icon->widget);
355 set_size_and_shape(icon, &width, &height);
356 offset_from_centre(icon, width, height, &x, &y);
357 gtk_widget_set_uposition(icon->win, x, y);
358 /* Set the correct position in the icon */
359 offset_to_centre(icon, width, height, &x, &y);
360 icon->x = x;
361 icon->y = y;
363 make_panel_window(icon->win->window);
365 gtk_widget_add_events(icon->widget,
366 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
367 GDK_BUTTON1_MOTION_MASK | GDK_ENTER_NOTIFY_MASK |
368 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
369 gtk_signal_connect(GTK_OBJECT(icon->widget), "enter-notify-event",
370 GTK_SIGNAL_FUNC(enter_notify), icon);
371 gtk_signal_connect(GTK_OBJECT(icon->widget), "button-press-event",
372 GTK_SIGNAL_FUNC(button_press_event), icon);
373 gtk_signal_connect(GTK_OBJECT(icon->widget), "button-release-event",
374 GTK_SIGNAL_FUNC(button_release_event), icon);
375 gtk_signal_connect(GTK_OBJECT(icon->widget), "motion-notify-event",
376 GTK_SIGNAL_FUNC(icon_motion_notify), icon);
377 gtk_signal_connect(GTK_OBJECT(icon->widget), "expose-event",
378 GTK_SIGNAL_FUNC(draw_icon), icon);
379 gtk_signal_connect(GTK_OBJECT(icon->win), "destroy",
380 GTK_SIGNAL_FUNC(icon_destroyed), icon);
382 current_pinboard->icons = g_list_prepend(current_pinboard->icons,
383 icon);
384 gtk_widget_show_all(icon->win);
385 gdk_window_lower(icon->win->window);
387 if (!loading_pinboard)
388 pinboard_save();
391 /* Remove an icon from the pinboard */
392 void pinboard_unpin(Icon *icon)
394 g_return_if_fail(icon != NULL);
396 gtk_widget_destroy(icon->win);
397 pinboard_save();
400 /* Unpin all selected items */
401 void pinboard_unpin_selection(void)
403 GList *next;
405 g_return_if_fail(current_pinboard != NULL);
407 if (number_selected == 0)
409 delayed_error(PROJECT,
410 _("You should first select some pinned icons to "
411 "unpin. Hold down the Ctrl key to select some icons."));
412 return;
415 next = current_pinboard->icons;
416 while (next)
418 Icon *icon = (Icon *) next->data;
420 next = next->next;
422 if (icon->selected)
423 gtk_widget_destroy(icon->win);
426 pinboard_save();
429 /* Put a border around the icon, briefly.
430 * If icon is NULL then cancel any existing wink.
431 * The icon will automatically unhighlight unless timeout is FALSE,
432 * in which case you must call this function again (with NULL or another
433 * icon) to remove the highlight.
435 void pinboard_wink_item(Icon *icon, gboolean timeout)
437 if (current_wink_icon == icon)
438 return;
440 if (current_wink_icon)
442 mask_wink_border(current_wink_icon, &mask_transp);
443 if (wink_timeout != -1)
444 gtk_timeout_remove(wink_timeout);
447 current_wink_icon = icon;
449 if (current_wink_icon)
451 mask_wink_border(current_wink_icon, &mask_solid);
452 if (timeout)
453 wink_timeout = gtk_timeout_add(300, end_wink, NULL);
454 else
455 wink_timeout = -1;
459 /* Remove everything on the current pinboard and disables the pinboard.
460 * Does not change any files. Does not change number_of_windows.
462 void pinboard_clear(void)
464 GList *next;
466 g_return_if_fail(current_pinboard != NULL);
468 next = current_pinboard->icons;
469 while (next)
471 Icon *icon = (Icon *) next->data;
473 next = next->next;
475 gtk_widget_destroy(icon->win);
478 g_free(current_pinboard->name);
479 g_free(current_pinboard);
480 current_pinboard = NULL;
482 release_xdnd_proxy(GDK_ROOT_WINDOW());
483 gdk_window_remove_filter(GDK_ROOT_PARENT(), proxy_filter, NULL);
484 gdk_window_set_user_data(GDK_ROOT_PARENT(), NULL);
487 /* Return the single selected icon, or NULL */
488 Icon *pinboard_selected_icon(void)
490 GList *next;
491 Icon *found = NULL;
493 g_return_val_if_fail(current_pinboard != NULL, NULL);
495 for (next = current_pinboard->icons; next; next = next->next)
497 Icon *icon = (Icon *) next->data;
499 if (icon->selected)
501 if (found)
502 return NULL; /* >1 icon selected */
503 else
504 found = icon;
508 return found;
511 void pinboard_clear_selection(void)
513 pinboard_select_only(NULL);
516 /* Set whether an icon is selected or not */
517 void pinboard_set_selected(Icon *icon, gboolean selected)
519 g_return_if_fail(icon != NULL);
521 if (icon->selected == selected)
522 return;
524 if (selected)
525 change_number_selected(+1);
526 else
527 change_number_selected(-1);
529 icon->selected = selected;
530 gtk_widget_queue_draw(icon->win);
533 /* Return a list of all the selected icons.
534 * g_list_free() the result.
536 GList *pinboard_get_selected(void)
538 GList *next;
539 GList *selected = NULL;
541 for (next = current_pinboard->icons; next; next = next->next)
543 Icon *i = (Icon *) next->data;
545 if (i->selected)
546 selected = g_list_append(selected, i);
549 return selected;
552 /* Clear the selection and then select this icon.
553 * Doesn't release and claim the selection unnecessarily.
554 * If icon is NULL, then just clears the selection.
556 void pinboard_select_only(Icon *icon)
558 GList *next;
560 g_return_if_fail(current_pinboard != NULL);
562 if (icon)
563 pinboard_set_selected(icon, TRUE);
565 for (next = current_pinboard->icons; next; next = next->next)
567 Icon *i = (Icon *) next->data;
569 if (i->selected && i != icon)
570 pinboard_set_selected(i, FALSE);
575 /****************************************************************
576 * INTERNAL FUNCTIONS *
577 ****************************************************************/
579 static void pinboard_check_options(void)
581 int old_text_bg = o_text_bg;
582 GdkColor n_fg, n_bg;
584 o_text_bg = option_get_int("pinboard_text_bg");
585 o_grid_step = option_get_int("pinboard_grid_step");
586 o_clamp_icons = option_get_int("pinboard_clamp_icons");
588 gdk_color_parse(option_get_static_string("pinboard_fg_colour"), &n_fg);
589 gdk_color_parse(option_get_static_string("pinboard_bg_colour"), &n_bg);
591 if (o_text_bg != old_text_bg ||
592 gdk_color_equal(&n_fg, &text_fg_col) == 0 ||
593 gdk_color_equal(&n_bg, &text_bg_col) == 0)
595 memcpy(&text_fg_col, &n_fg, sizeof(GdkColor));
596 memcpy(&text_bg_col, &n_bg, sizeof(GdkColor));
598 if (pinicon_style)
600 gtk_style_unref(pinicon_style);
601 pinicon_style = NULL;
604 if (current_pinboard)
605 reshape_all();
609 /* Icon's size, shape or appearance has changed - update the display */
610 static void reshape_icon(Icon *icon)
612 int x = icon->x, y = icon->y;
613 int width, height;
615 set_size_and_shape(icon, &width, &height);
616 gdk_window_resize(icon->win->window, width, height);
617 offset_from_centre(icon, width, height, &x, &y);
618 gtk_widget_set_uposition(icon->win, x, y);
619 gtk_widget_queue_draw(icon->win);
622 /* See if the file the icon points to has changed. Update the icon
623 * if so.
625 void pinboard_icon_may_update(Icon *icon)
627 MaskedPixmap *image = icon->item.image;
628 int flags = icon->item.flags;
630 pixmap_ref(image);
631 mount_update(FALSE);
632 dir_restat(icon->path, &icon->item, FALSE);
634 if (icon->item.image != image || icon->item.flags != flags)
635 reshape_icon(icon);
637 pixmap_unref(image);
640 static gint end_wink(gpointer data)
642 pinboard_wink_item(NULL, FALSE);
643 return FALSE;
646 /* Make the wink border solid or transparent */
647 static void mask_wink_border(Icon *icon, GdkColor *alpha)
649 int width, height;
651 gdk_window_get_size(icon->widget->window, &width, &height);
653 gdk_gc_set_foreground(mask_gc, alpha);
654 gdk_draw_rectangle(icon->mask, mask_gc, FALSE,
655 0, 0, width - 1, height - 1);
656 gdk_draw_rectangle(icon->mask, mask_gc, FALSE,
657 1, 1, width - 3, height - 3);
659 gtk_widget_shape_combine_mask(icon->win, icon->mask, 0, 0);
661 gtk_widget_draw(icon->widget, NULL);
664 #define TEXT_AT(dx, dy) \
665 gdk_draw_string(icon->mask, font, mask_gc, \
666 text_x + dx, y + dy, \
667 item->leafname);
669 /* Updates the name_width field and resizes and masks the window.
670 * Also sets the style to pinicon_style, generating it if needed.
671 * Returns the new width and height.
673 static void set_size_and_shape(Icon *icon, int *rwidth, int *rheight)
675 int width, height;
676 GdkFont *font;
677 int font_height;
678 MaskedPixmap *image = icon->item.image;
679 DirItem *item = &icon->item;
680 int text_x, text_y;
682 if (!pinicon_style)
684 pinicon_style = gtk_style_copy(icon->widget->style);
685 memcpy(&pinicon_style->fg[GTK_STATE_NORMAL],
686 &text_fg_col, sizeof(GdkColor));
687 memcpy(&pinicon_style->bg[GTK_STATE_NORMAL],
688 &text_bg_col, sizeof(GdkColor));
690 gtk_widget_set_style(icon->widget, pinicon_style);
692 font = pinicon_style->font;
693 font_height = font->ascent + font->descent;
694 item->name_width = gdk_string_width(font, item->leafname);
696 width = MAX(image->width, item->name_width + 2) +
697 2 * WINK_FRAME;
698 height = image->height + GAP + (font_height + 2) + 2 * WINK_FRAME;
699 gtk_widget_set_usize(icon->win, width, height);
701 if (icon->mask)
702 gdk_pixmap_unref(icon->mask);
703 icon->mask = gdk_pixmap_new(icon->win->window, width, height, 1);
704 if (!mask_gc)
705 mask_gc = gdk_gc_new(icon->mask);
707 /* Clear the mask to transparent */
708 gdk_gc_set_foreground(mask_gc, &mask_transp);
709 gdk_draw_rectangle(icon->mask, mask_gc, TRUE, 0, 0, width, height);
711 gdk_gc_set_foreground(mask_gc, &mask_solid);
712 /* Make the icon area solid */
713 if (image->mask)
715 gdk_draw_pixmap(icon->mask, mask_gc, image->mask,
716 0, 0,
717 (width - image->width) >> 1,
718 WINK_FRAME,
719 image->width,
720 image->height);
722 else
724 gdk_draw_rectangle(icon->mask, mask_gc, TRUE,
725 (width - image->width) >> 1,
726 WINK_FRAME,
727 image->width,
728 image->height);
731 gdk_gc_set_function(mask_gc, GDK_OR);
732 if (item->flags & ITEM_FLAG_SYMLINK)
734 gdk_draw_pixmap(icon->mask, mask_gc, im_symlink->mask,
735 0, 0, /* Source x,y */
736 (width - image->width) >> 1, /* Dest x */
737 WINK_FRAME, /* Dest y */
738 -1, -1);
740 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
742 /* Note: Both mount state pixmaps must have the same mask */
743 gdk_draw_pixmap(icon->mask, mask_gc, im_mounted->mask,
744 0, 0, /* Source x,y */
745 (width - image->width) >> 1, /* Dest x */
746 WINK_FRAME, /* Dest y */
747 -1, -1);
749 gdk_gc_set_function(mask_gc, GDK_COPY);
751 /* Mask off an area for the text (from o_text_bg) */
753 text_x = (width - item->name_width) >> 1;
754 text_y = WINK_FRAME + image->height + GAP + 1;
756 if (o_text_bg == TEXT_BG_SOLID)
758 gdk_draw_rectangle(icon->mask, mask_gc, TRUE,
759 (width - (item->name_width + 2)) >> 1,
760 WINK_FRAME + image->height + GAP,
761 item->name_width + 2, font_height + 2);
763 else
765 int y = text_y + font->ascent;
767 TEXT_AT(0, 0);
769 if (o_text_bg == TEXT_BG_OUTLINE)
771 TEXT_AT(1, 0);
772 TEXT_AT(1, 1);
773 TEXT_AT(0, 1);
774 TEXT_AT(-1, 1);
775 TEXT_AT(-1, 0);
776 TEXT_AT(-1, -1);
777 TEXT_AT(0, -1);
778 TEXT_AT(1, -1);
782 gtk_widget_shape_combine_mask(icon->win, icon->mask, 0, 0);
784 *rwidth = width;
785 *rheight = height;
788 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, Icon *icon)
790 GdkFont *font = icon->widget->style->font;
791 int font_height;
792 int width, height;
793 int text_x, text_y;
794 DirItem *item = &icon->item;
795 MaskedPixmap *image = item->image;
796 int image_x;
797 GdkGC *gc = widget->style->black_gc;
798 GtkStateType state = icon->selected ? GTK_STATE_SELECTED
799 : GTK_STATE_NORMAL;
801 font_height = font->ascent + font->descent;
803 gdk_window_get_size(widget->window, &width, &height);
804 image_x = (width - image->width) >> 1;
806 /* TODO: If the shape extension is missing we might need to set
807 * the clip mask here...
809 gdk_draw_pixmap(widget->window, gc,
810 image->pixmap,
811 0, 0,
812 image_x,
813 WINK_FRAME,
814 image->width,
815 image->height);
817 if (item->flags & ITEM_FLAG_SYMLINK)
819 gdk_gc_set_clip_origin(gc, image_x, WINK_FRAME);
820 gdk_gc_set_clip_mask(gc, im_symlink->mask);
821 gdk_draw_pixmap(widget->window, gc,
822 im_symlink->pixmap,
823 0, 0, /* Source x,y */
824 image_x, WINK_FRAME, /* Dest x,y */
825 -1, -1);
826 gdk_gc_set_clip_mask(gc, NULL);
827 gdk_gc_set_clip_origin(gc, 0, 0);
829 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
831 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
832 ? im_mounted
833 : im_unmounted;
835 gdk_gc_set_clip_origin(gc, image_x, WINK_FRAME);
836 gdk_gc_set_clip_mask(gc, mp->mask);
837 gdk_draw_pixmap(widget->window, gc,
838 mp->pixmap,
839 0, 0, /* Source x,y */
840 image_x, WINK_FRAME, /* Dest x,y */
841 -1, -1);
842 gdk_gc_set_clip_mask(gc, NULL);
843 gdk_gc_set_clip_origin(gc, 0, 0);
846 text_x = (width - item->name_width) >> 1;
847 text_y = WINK_FRAME + image->height + GAP + 1;
849 if (o_text_bg != TEXT_BG_NONE)
851 gtk_paint_flat_box(widget->style, widget->window,
852 state,
853 GTK_SHADOW_NONE,
854 NULL, widget, "text",
855 text_x - 1,
856 text_y - 1,
857 item->name_width + 2,
858 font_height + 2);
861 gtk_paint_string(widget->style, widget->window,
862 state,
863 NULL, widget, "text",
864 text_x,
865 text_y + font->ascent,
866 item->leafname);
868 if (current_wink_icon == icon)
870 gdk_draw_rectangle(icon->widget->window,
871 icon->widget->style->white_gc,
872 FALSE,
873 0, 0, width - 1, height - 1);
874 gdk_draw_rectangle(icon->widget->window,
875 icon->widget->style->black_gc,
876 FALSE,
877 1, 1, width - 3, height - 3);
880 return FALSE;
883 static gboolean root_property_event(GtkWidget *widget,
884 GdkEventProperty *event,
885 gpointer data)
887 if (event->atom == win_button_proxy &&
888 event->state == GDK_PROPERTY_NEW_VALUE)
890 /* Setup forwarding on the new proxy window, if possible */
891 forward_root_clicks();
894 return FALSE;
897 static gboolean root_button_press(GtkWidget *widget,
898 GdkEventButton *event,
899 gpointer data)
901 BindAction action;
903 action = bind_lookup_bev(BIND_PINBOARD, event);
905 switch (action)
907 case ACT_CLEAR_SELECTION:
908 pinboard_clear_selection();
909 break;
910 case ACT_POPUP_MENU:
911 dnd_motion_ungrab();
912 show_pinboard_menu(event, NULL);
913 break;
914 case ACT_IGNORE:
915 break;
916 default:
917 g_warning("Unsupported action : %d\n", action);
918 break;
921 return TRUE;
924 static gboolean enter_notify(GtkWidget *widget,
925 GdkEventCrossing *event,
926 Icon *icon)
928 pinboard_icon_may_update(icon);
930 return FALSE;
933 static void perform_action(Icon *icon, GdkEventButton *event)
935 BindAction action;
937 action = bind_lookup_bev(BIND_PINBOARD_ICON, event);
939 switch (action)
941 case ACT_OPEN_ITEM:
942 dnd_motion_ungrab();
943 pinboard_wink_item(icon, TRUE);
944 run_diritem(icon->path, &icon->item, NULL, FALSE);
945 break;
946 case ACT_EDIT_ITEM:
947 dnd_motion_ungrab();
948 pinboard_wink_item(icon, TRUE);
949 run_diritem(icon->path, &icon->item, NULL, TRUE);
950 break;
951 case ACT_POPUP_MENU:
952 dnd_motion_ungrab();
953 show_pinboard_menu(event, icon);
954 break;
955 case ACT_MOVE_ICON:
956 old_x = event->x_root;
957 old_y = event->y_root;
958 icon_old_x = icon->x;
959 icon_old_y = icon->y;
960 dnd_motion_start(MOTION_REPOSITION);
961 break;
962 case ACT_PRIME_AND_SELECT:
963 if (!icon->selected)
964 pinboard_select_only(icon);
965 dnd_motion_start(MOTION_READY_FOR_DND);
966 break;
967 case ACT_PRIME_AND_TOGGLE:
968 pinboard_set_selected(icon, !icon->selected);
969 dnd_motion_start(MOTION_READY_FOR_DND);
970 break;
971 case ACT_PRIME_FOR_DND:
972 pinboard_wink_item(icon, TRUE);
973 dnd_motion_start(MOTION_READY_FOR_DND);
974 break;
975 case ACT_TOGGLE_SELECTED:
976 pinboard_set_selected(icon, !icon->selected);
977 break;
978 case ACT_SELECT_EXCL:
979 pinboard_select_only(icon);
980 break;
981 case ACT_IGNORE:
982 break;
983 default:
984 g_warning("Unsupported action : %d\n", action);
985 break;
989 static gboolean button_release_event(GtkWidget *widget,
990 GdkEventButton *event,
991 Icon *icon)
993 if (pinboard_modified)
994 pinboard_save();
996 if (dnd_motion_release(event))
997 return TRUE;
999 perform_action(icon, event);
1001 return TRUE;
1004 static gboolean button_press_event(GtkWidget *widget,
1005 GdkEventButton *event,
1006 Icon *icon)
1008 if (dnd_motion_press(widget, event))
1009 perform_action(icon, event);
1011 return TRUE;
1014 /* Return a text/uri-list of all the icons in the list */
1015 static guchar *create_uri_list(GList *list)
1017 GString *tmp;
1018 guchar *retval;
1019 guchar *leader;
1021 tmp = g_string_new(NULL);
1022 leader = g_strdup_printf("file://%s", our_host_name());
1024 for (; list; list = list->next)
1026 Icon *icon = (Icon *) list->data;
1028 g_string_append(tmp, leader);
1029 g_string_append(tmp, icon->path);
1030 g_string_append(tmp, "\r\n");
1033 g_free(leader);
1034 retval = tmp->str;
1035 g_string_free(tmp, FALSE);
1037 return retval;
1040 static void start_drag(Icon *icon, GdkEventMotion *event)
1042 GtkWidget *widget = icon->widget;
1043 GList *selected;
1045 if (!icon->selected)
1047 tmp_icon_selected = TRUE;
1048 pinboard_select_only(icon);
1051 selected = pinboard_get_selected();
1052 g_return_if_fail(selected != NULL);
1054 pinboard_drag_in_progress = TRUE;
1056 if (selected->next == NULL)
1057 drag_one_item(widget, event, icon->path, &icon->item);
1058 else
1060 guchar *uri_list;
1062 uri_list = create_uri_list(selected);
1063 drag_selection(widget, event, uri_list);
1064 g_free(uri_list);
1067 g_list_free(selected);
1070 /* An icon is being dragged around... */
1071 static gint icon_motion_notify(GtkWidget *widget,
1072 GdkEventMotion *event,
1073 Icon *icon)
1075 int x, y;
1076 int width, height;
1077 int dx,dy;
1079 if (motion_state == MOTION_READY_FOR_DND)
1081 if (dnd_motion_moved(event))
1082 start_drag(icon, event);
1083 return TRUE;
1085 else if (motion_state != MOTION_REPOSITION)
1086 return FALSE;
1088 /* How far the pointer has moved since the drag started */
1089 dx = event->x_root - old_x;
1090 dy = event->y_root - old_y;
1092 x = icon_old_x + dx;
1093 y = icon_old_y + dy;
1095 snap_to_grid(&x, &y);
1097 if (icon->x == x && icon->y == y)
1098 return TRUE;
1100 icon->x = x;
1101 icon->y = y;
1102 gdk_window_get_size(icon->win->window, &width, &height);
1103 offset_from_centre(icon, width, height, &x, &y);
1105 gdk_window_move(icon->win->window, x, y);
1107 /* Store the fixed position for the center of the icon */
1108 offset_to_centre(icon, width, height, &x, &y);
1109 icon->x = x;
1110 icon->y = y;
1112 pinboard_modified = TRUE;
1114 return TRUE;
1117 /* Called for each line in the pinboard file while loading a new board */
1118 static char *pin_from_file(guchar *line)
1120 guchar *leaf = NULL;
1121 int x, y, n;
1123 if (*line == '<')
1125 guchar *end;
1127 end = strchr(line + 1, '>');
1128 if (!end)
1129 return _("Missing '>' in icon label");
1131 leaf = g_strndup(line + 1, end - line - 1);
1133 line = end + 1;
1135 while (isspace(*line))
1136 line++;
1137 if (*line != ',')
1138 return _("Missing ',' after icon label");
1139 line++;
1142 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1143 return NULL; /* Ignore format errors */
1145 pinboard_pin(line + n, leaf, x, y);
1147 g_free(leaf);
1149 return NULL;
1152 /* Make sure that clicks and drops on the root window come to us...
1153 * False if an error occurred (ie, someone else is using it).
1155 static gboolean add_root_handlers(void)
1157 GdkWindow *root;
1159 if (!proxy_invisible)
1161 GtkTargetEntry target_table[] =
1163 {"text/uri-list", 0, TARGET_URI_LIST},
1164 {"STRING", 0, TARGET_STRING},
1167 win_button_proxy = gdk_atom_intern("_WIN_DESKTOP_BUTTON_PROXY",
1168 FALSE);
1169 proxy_invisible = gtk_invisible_new();
1170 gtk_widget_show(proxy_invisible);
1172 gdk_window_add_filter(proxy_invisible->window,
1173 proxy_filter, NULL);
1176 gtk_signal_connect(GTK_OBJECT(proxy_invisible),
1177 "property_notify_event",
1178 GTK_SIGNAL_FUNC(root_property_event), NULL);
1179 gtk_signal_connect(GTK_OBJECT(proxy_invisible),
1180 "button_press_event",
1181 GTK_SIGNAL_FUNC(root_button_press), NULL);
1183 /* Drag and drop handlers */
1184 drag_set_pinboard_dest(proxy_invisible);
1185 gtk_signal_connect(GTK_OBJECT(proxy_invisible), "drag_motion",
1186 GTK_SIGNAL_FUNC(bg_drag_motion),
1187 NULL);
1189 /* The proxy window is also used to hold the selection... */
1190 gtk_signal_connect(GTK_OBJECT(proxy_invisible),
1191 "selection_clear_event",
1192 GTK_SIGNAL_FUNC(lose_selection),
1193 NULL);
1195 gtk_signal_connect(GTK_OBJECT(proxy_invisible),
1196 "selection_get",
1197 GTK_SIGNAL_FUNC(selection_get), NULL);
1199 gtk_selection_add_targets(proxy_invisible,
1200 GDK_SELECTION_PRIMARY,
1201 target_table,
1202 sizeof(target_table) / sizeof(*target_table));
1205 root = gdk_window_lookup(GDK_ROOT_WINDOW());
1206 if (!root)
1207 root = gdk_window_foreign_new(GDK_ROOT_WINDOW());
1209 if (!setup_xdnd_proxy(GDK_ROOT_WINDOW(), proxy_invisible->window))
1210 return FALSE;
1212 /* Forward events from the root window to our proxy window */
1213 gdk_window_add_filter(GDK_ROOT_PARENT(), proxy_filter, NULL);
1214 gdk_window_set_user_data(GDK_ROOT_PARENT(), proxy_invisible);
1215 gdk_window_set_events(GDK_ROOT_PARENT(),
1216 gdk_window_get_events(GDK_ROOT_PARENT()) |
1217 GDK_PROPERTY_CHANGE_MASK);
1219 forward_root_clicks();
1221 return TRUE;
1224 /* See if the window manager is offering to forward root window clicks.
1225 * If so, grab them. Otherwise, do nothing.
1226 * Call this whenever the _WIN_DESKTOP_BUTTON_PROXY property changes.
1228 static void forward_root_clicks(void)
1230 click_proxy_gdk_window = find_click_proxy_window();
1231 if (!click_proxy_gdk_window)
1232 return;
1234 /* Events on the wm's proxy are dealt with by our proxy widget */
1235 gdk_window_set_user_data(click_proxy_gdk_window, proxy_invisible);
1236 gdk_window_add_filter(click_proxy_gdk_window, proxy_filter, NULL);
1238 /* The proxy window for clicks sends us button press events with
1239 * SubstructureNotifyMask. We need StructureNotifyMask to receive
1240 * DestroyNotify events, too.
1242 XSelectInput(GDK_DISPLAY(),
1243 GDK_WINDOW_XWINDOW(click_proxy_gdk_window),
1244 SubstructureNotifyMask | StructureNotifyMask);
1247 /* Write the current state of the pinboard to the current pinboard file */
1248 static void pinboard_save(void)
1250 guchar *save = NULL;
1251 GString *tmp = NULL;
1252 FILE *file = NULL;
1253 GList *next;
1254 guchar *save_new = NULL;
1256 g_return_if_fail(current_pinboard != NULL);
1258 pinboard_modified = FALSE;
1260 if (strchr(current_pinboard->name, '/'))
1261 save = g_strdup(current_pinboard->name);
1262 else
1264 guchar *leaf;
1266 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1267 save = choices_find_path_save(leaf, "ROX-Filer", TRUE);
1268 g_free(leaf);
1271 if (!save)
1272 return;
1274 save_new = g_strconcat(save, ".new", NULL);
1275 file = fopen(save_new, "wb");
1276 if (!file)
1277 goto err;
1279 tmp = g_string_new(NULL);
1280 for (next = current_pinboard->icons; next; next = next->next)
1282 Icon *icon = (Icon *) next->data;
1284 g_string_sprintf(tmp, "<%s>, %d, %d, %s\n",
1285 icon->item.leafname, icon->x, icon->y, icon->src_path);
1286 if (fwrite(tmp->str, 1, tmp->len, file) < tmp->len)
1287 goto err;
1290 if (fclose(file))
1292 file = NULL;
1293 goto err;
1296 file = NULL;
1298 if (rename(save_new, save))
1299 goto err;
1301 goto out;
1302 err:
1303 delayed_error(_("Error saving pinboard"), g_strerror(errno));
1304 out:
1305 if (file)
1306 fclose(file);
1307 if (tmp)
1308 g_string_free(tmp, TRUE);
1310 g_free(save_new);
1311 g_free(save);
1315 * Filter that translates proxied events from virtual root windows into normal
1316 * Gdk events for the proxy_invisible widget. Stolen from gmc.
1318 * Also gets events from the root window.
1320 static GdkFilterReturn proxy_filter(GdkXEvent *xevent,
1321 GdkEvent *event,
1322 gpointer data)
1324 XEvent *xev;
1325 GdkWindow *proxy = proxy_invisible->window;
1327 xev = xevent;
1329 switch (xev->type) {
1330 case ButtonPress:
1331 case ButtonRelease:
1332 /* Translate button events into events that come from
1333 * the proxy window, so that we can catch them as a
1334 * signal from the invisible widget.
1336 if (xev->type == ButtonPress)
1337 event->button.type = GDK_BUTTON_PRESS;
1338 else
1339 event->button.type = GDK_BUTTON_RELEASE;
1341 gdk_window_ref(proxy);
1343 event->button.window = proxy;
1344 event->button.send_event = xev->xbutton.send_event;
1345 event->button.time = xev->xbutton.time;
1346 event->button.x_root = xev->xbutton.x_root;
1347 event->button.y_root = xev->xbutton.y_root;
1348 event->button.x = xev->xbutton.x;
1349 event->button.y = xev->xbutton.y;
1350 event->button.state = xev->xbutton.state;
1351 event->button.button = xev->xbutton.button;
1353 return GDK_FILTER_TRANSLATE;
1355 case DestroyNotify:
1356 /* XXX: I have no idea why this helps, but it does! */
1357 /* The proxy window was destroyed (i.e. the window
1358 * manager died), so we have to cope with it
1360 if (((GdkEventAny *) event)->window == proxy)
1361 gdk_window_destroy_notify(proxy);
1363 return GDK_FILTER_REMOVE;
1365 default:
1366 break;
1369 return GDK_FILTER_CONTINUE;
1372 /* Does not save the new state */
1373 static void icon_destroyed(GtkWidget *widget, Icon *icon)
1375 g_return_if_fail(icon != NULL);
1377 icon_unhash_path(icon);
1379 if (icon->selected)
1380 change_number_selected(-1);
1382 if (current_wink_icon == icon)
1383 current_wink_icon = NULL;
1385 gdk_pixmap_unref(icon->mask);
1386 dir_item_clear(&icon->item);
1387 g_free(icon->src_path);
1388 g_free(icon->path);
1389 g_free(icon);
1391 if (current_pinboard)
1392 current_pinboard->icons =
1393 g_list_remove(current_pinboard->icons, icon);
1396 static void snap_to_grid(int *x, int *y)
1398 *x = ((*x + o_grid_step / 2) / o_grid_step) * o_grid_step;
1399 *y = ((*y + o_grid_step / 2) / o_grid_step) * o_grid_step;
1402 /* Convert (x,y) from a centre point to a window position */
1403 static void offset_from_centre(Icon *icon,
1404 int width, int height,
1405 int *x, int *y)
1407 *x -= width >> 1;
1408 *y -= height - (icon->widget->style->font->descent >> 1);
1409 *x = CLAMP(*x, 0, screen_width - (o_clamp_icons ? width : 0));
1410 *y = CLAMP(*y, 0, screen_height - (o_clamp_icons ? height : 0));
1413 /* Convert (x,y) from a window position to a centre point */
1414 static void offset_to_centre(Icon *icon,
1415 int width, int height,
1416 int *x, int *y)
1418 *x += width >> 1;
1419 *y += height - (icon->widget->style->font->descent >> 1);
1422 /* Same as drag_set_dest(), but for pinboard icons */
1423 static void drag_set_pinicon_dest(Icon *icon)
1425 GtkObject *obj = GTK_OBJECT(icon->widget);
1427 make_drop_target(icon->widget, 0);
1429 gtk_signal_connect(obj, "drag_motion",
1430 GTK_SIGNAL_FUNC(drag_motion), icon);
1431 gtk_signal_connect(obj, "drag_leave",
1432 GTK_SIGNAL_FUNC(drag_leave), icon);
1433 gtk_signal_connect(obj, "drag_end",
1434 GTK_SIGNAL_FUNC(drag_end), icon);
1437 /* Called during the drag when the mouse is in a widget registered
1438 * as a drop target. Returns TRUE if we can accept the drop.
1440 static gboolean drag_motion(GtkWidget *widget,
1441 GdkDragContext *context,
1442 gint x,
1443 gint y,
1444 guint time,
1445 Icon *icon)
1447 GdkDragAction action = context->suggested_action;
1448 char *type = NULL;
1449 DirItem *item = &icon->item;
1451 if (gtk_drag_get_source_widget(context) == widget)
1452 goto out; /* Can't drag something to itself! */
1454 if (icon->selected)
1455 goto out; /* Can't drag a selection to itself */
1457 type = dnd_motion_item(context, &item);
1459 if (!item)
1460 type = NULL;
1461 out:
1462 /* We actually must pretend to accept the drop, even if the
1463 * directory isn't writeable, so that the spring-opening
1464 * thing works.
1467 /* Don't allow drops to non-writeable directories */
1468 if (option_get_int("dnd_spring_open") == FALSE &&
1469 type == drop_dest_dir &&
1470 access(icon->path, W_OK) != 0)
1472 type = NULL;
1475 g_dataset_set_data(context, "drop_dest_type", type);
1476 if (type)
1478 gdk_drag_status(context, action, time);
1479 g_dataset_set_data_full(context, "drop_dest_path",
1480 g_strdup(icon->path), g_free);
1481 if (type == drop_dest_dir)
1482 dnd_spring_load(context);
1484 pinboard_wink_item(icon, FALSE);
1487 return type != NULL;
1490 static void drag_leave(GtkWidget *widget,
1491 GdkDragContext *context,
1492 guint32 time,
1493 Icon *icon)
1495 pinboard_wink_item(NULL, FALSE);
1496 dnd_spring_abort();
1499 /* When changing the 'selected' attribute of an icon, call this
1500 * to update the global counter and claim or release the primary
1501 * selection as needed.
1503 static void change_number_selected(int delta)
1505 guint32 time;
1507 g_return_if_fail(delta != 0);
1508 g_return_if_fail(number_selected + delta >= 0);
1510 if (number_selected == 0)
1512 time = gdk_event_get_time(gtk_get_current_event());
1514 gtk_selection_owner_set(proxy_invisible,
1515 GDK_SELECTION_PRIMARY,
1516 time);
1519 number_selected += delta;
1521 if (number_selected == 0)
1523 time = gdk_event_get_time(gtk_get_current_event());
1525 gtk_selection_owner_set(NULL,
1526 GDK_SELECTION_PRIMARY,
1527 time);
1531 /* Called when another application wants the contents of our selection */
1532 static void selection_get(GtkWidget *widget,
1533 GtkSelectionData *selection_data,
1534 guint info,
1535 guint time,
1536 gpointer data)
1538 GString *str;
1539 GList *next;
1540 guchar *leader = NULL;
1542 str = g_string_new(NULL);
1544 if (info == TARGET_URI_LIST)
1545 leader = g_strdup_printf("file://%s", our_host_name());
1547 for (next = current_pinboard->icons; next; next = next->next)
1549 Icon *icon = (Icon *) next->data;
1551 if (!icon->selected)
1552 continue;
1554 if (leader)
1555 g_string_append(str, leader);
1556 g_string_append(str, icon->path);
1557 g_string_append_c(str, ' ');
1560 g_free(leader);
1562 gtk_selection_data_set(selection_data,
1563 gdk_atom_intern("STRING", FALSE),
1565 str->str,
1566 str->len ? str->len - 1 : 0);
1568 g_string_free(str, TRUE);
1571 /* Called when another application takes the selection away from us */
1572 static gint lose_selection(GtkWidget *widget, GdkEventSelection *event)
1574 /* 'lock' number_selected so that we don't send any events */
1575 number_selected++;
1576 pinboard_clear_selection();
1577 number_selected--;
1579 return TRUE;
1582 static gboolean bg_drag_motion(GtkWidget *widget,
1583 GdkDragContext *context,
1584 gint x,
1585 gint y,
1586 guint time,
1587 gpointer data)
1589 /* Dragging from the pinboard to the pinboard is not allowed */
1590 if (pinboard_drag_in_progress)
1591 return FALSE;
1593 gdk_drag_status(context, context->suggested_action, time);
1594 return TRUE;
1597 static void drag_end(GtkWidget *widget,
1598 GdkDragContext *context,
1599 Icon *icon)
1601 pinboard_drag_in_progress = FALSE;
1602 if (tmp_icon_selected)
1604 pinboard_clear_selection();
1605 tmp_icon_selected = FALSE;
1609 /* Something which affects all the icons has changed - reshape
1610 * and redraw all of them.
1612 static void reshape_all(void)
1614 GList *next;
1616 g_return_if_fail(current_pinboard != NULL);
1618 for (next = current_pinboard->icons; next; next = next->next)
1620 Icon *icon = (Icon *) next->data;
1621 reshape_icon(icon);
1625 /* Display the pinboard menu. Set icon to NULL if no particular icon
1626 * was clicked.
1628 static void show_pinboard_menu(GdkEventButton *event, Icon *icon)
1630 int pos[2];
1631 GList *icons;
1632 AppMenus *menus = NULL;
1634 if (icon)
1636 if (icon->selected)
1637 tmp_icon_selected = FALSE;
1638 else
1640 pinboard_select_only(icon);
1641 tmp_icon_selected = TRUE;
1643 /* Check for icon-specific menu */
1644 if (icon->path)
1645 menus = appmenu_query(icon->path, &icon->item);
1648 /* Remove the previous appmenu used on this menu */
1649 appmenu_remove(pinboard_menu);
1651 icons = pinboard_get_selected();
1653 pos[0] = event->x_root;
1654 pos[1] = event->y_root;
1656 if (icons)
1658 menu_set_items_shaded(pinboard_menu,
1659 icons->next ? TRUE : FALSE, 4, 3);
1661 menu_set_items_shaded(pinboard_menu, FALSE, 7, 1);
1663 else
1664 menu_set_items_shaded(pinboard_menu, TRUE, 4, 4);
1666 /* Add the AppMenu items if necessary */
1667 if (menus)
1668 appmenu_add(menus, pinboard_menu);
1670 gtk_menu_popup(GTK_MENU(pinboard_menu), NULL, NULL, position_menu,
1671 (gpointer) pos, event->button, event->time);
1674 static void pin_help(gpointer data, guint action, GtkWidget *widget)
1676 Icon *icon;
1678 icon = pinboard_selected_icon();
1680 if (icon)
1681 show_item_help(icon->path, &icon->item);
1682 else
1683 delayed_error(PROJECT,
1684 _("You must first select a single pinned icon to get "
1685 "help on."));
1688 static void pin_remove(gpointer data, guint action, GtkWidget *widget)
1690 pinboard_unpin_selection();
1693 static void menu_closed(GtkWidget *widget)
1695 if (tmp_icon_selected)
1697 pinboard_clear_selection();
1698 tmp_icon_selected = FALSE;
1702 /* Show where this item is stored */
1703 static void show_location(gpointer data, guint action, GtkWidget *widget)
1705 Icon *icon;
1707 icon = pinboard_selected_icon();
1709 if (icon)
1710 open_to_show(icon->path);
1711 else
1713 delayed_error(PROJECT,
1714 _("Select a single item, then use this to find out "
1715 "where it is in the filesystem."));
1719 static void rename_cb(Icon *icon)
1721 reshape_icon(icon);
1723 pinboard_save();
1726 static void edit_icon(gpointer data, guint action, GtkWidget *widget)
1728 Icon *icon;
1730 icon = pinboard_selected_icon();
1732 if (icon)
1733 show_rename_box(icon->widget, icon, rename_cb);
1734 else
1736 delayed_error(PROJECT,
1737 _("First, select a single item to edit"));
1738 return;