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)
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 /* pinboard.c - icons on the background */
32 #include <gtk/gtkinvisible.h>
43 #include "gui_support.h"
53 /* The number of pixels between the bottom of the image and the top
58 /* The size of the border around the icon which is used when winking */
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
;
73 guchar
*name
; /* Leaf name */
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
;
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
,
125 static gboolean
root_property_event(GtkWidget
*widget
,
126 GdkEventProperty
*event
,
128 static gboolean
root_button_press(GtkWidget
*widget
,
129 GdkEventButton
*event
,
131 static gboolean
enter_notify(GtkWidget
*widget
,
132 GdkEventCrossing
*event
,
134 static gboolean
button_press_event(GtkWidget
*widget
,
135 GdkEventButton
*event
,
137 static gint
icon_motion_notify(GtkWidget
*widget
,
138 GdkEventMotion
*event
,
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
,
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
,
151 static void offset_to_centre(Icon
*icon
,
152 int width
, int height
,
154 static gboolean
drag_motion(GtkWidget
*widget
,
155 GdkDragContext
*context
,
160 static void drag_set_pinicon_dest(Icon
*icon
);
161 static void drag_leave(GtkWidget
*widget
,
162 GdkDragContext
*context
,
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
,
173 static gboolean
bg_drag_motion(GtkWidget
*widget
,
174 GdkDragContext
*context
,
179 static void drag_end(GtkWidget
*widget
,
180 GdkDragContext
*context
,
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)
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"),
226 gdk_color_parse(option_get_static_string("pinboard_bg_colour"),
229 pinboard_menu
= menu_create(menu_def
,
230 sizeof(menu_def
) / sizeof(*menu_def
),
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
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 */
261 if (number_of_windows
< 1 && gtk_main_level() > 0)
266 if (!add_root_handlers())
268 delayed_error(PROJECT
, _("Another application is already "
269 "managing the pinboard!"));
275 slash
= strchr(name
, '/');
278 if (access(name
, F_OK
))
279 path
= NULL
; /* File does not (yet) exist */
281 path
= g_strdup(name
);
287 leaf
= g_strconcat("pb_", name
, NULL
);
288 path
= choices_find_path_load(leaf
, "ROX-Filer");
292 current_pinboard
= g_new(Pinboard
, 1);
293 current_pinboard
->name
= g_strdup(name
);
294 current_pinboard
->icons
= NULL
;
299 parse_file(path
, pin_from_file
);
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
)
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
);
324 snap_to_grid(&x
, &y
);
328 icon_hash_path(icon
);
330 dir_stat(icon
->path
, &icon
->item
, FALSE
);
334 name
= strrchr(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
);
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
,
384 gtk_widget_show_all(icon
->win
);
385 gdk_window_lower(icon
->win
->window
);
387 if (!loading_pinboard
)
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
);
400 /* Unpin all selected items */
401 void pinboard_unpin_selection(void)
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."));
415 next
= current_pinboard
->icons
;
418 Icon
*icon
= (Icon
*) next
->data
;
423 gtk_widget_destroy(icon
->win
);
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
)
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
);
453 wink_timeout
= gtk_timeout_add(300, end_wink
, NULL
);
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)
466 g_return_if_fail(current_pinboard
!= NULL
);
468 next
= current_pinboard
->icons
;
471 Icon
*icon
= (Icon
*) next
->data
;
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)
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
;
502 return NULL
; /* >1 icon selected */
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
)
525 change_number_selected(+1);
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)
539 GList
*selected
= NULL
;
541 for (next
= current_pinboard
->icons
; next
; next
= next
->next
)
543 Icon
*i
= (Icon
*) next
->data
;
546 selected
= g_list_append(selected
, i
);
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
)
560 g_return_if_fail(current_pinboard
!= NULL
);
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
;
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
));
600 gtk_style_unref(pinicon_style
);
601 pinicon_style
= NULL
;
604 if (current_pinboard
)
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
;
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
625 void pinboard_icon_may_update(Icon
*icon
)
627 MaskedPixmap
*image
= icon
->item
.image
;
628 int flags
= icon
->item
.flags
;
632 dir_restat(icon
->path
, &icon
->item
, FALSE
);
634 if (icon
->item
.image
!= image
|| icon
->item
.flags
!= flags
)
640 static gint
end_wink(gpointer data
)
642 pinboard_wink_item(NULL
, FALSE
);
646 /* Make the wink border solid or transparent */
647 static void mask_wink_border(Icon
*icon
, GdkColor
*alpha
)
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, \
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
)
678 MaskedPixmap
*image
= icon
->item
.image
;
679 DirItem
*item
= &icon
->item
;
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) +
698 height
= image
->height
+ GAP
+ (font_height
+ 2) + 2 * WINK_FRAME
;
699 gtk_widget_set_usize(icon
->win
, width
, height
);
702 gdk_pixmap_unref(icon
->mask
);
703 icon
->mask
= gdk_pixmap_new(icon
->win
->window
, width
, height
, 1);
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 */
715 gdk_draw_pixmap(icon
->mask
, mask_gc
, image
->mask
,
717 (width
- image
->width
) >> 1,
724 gdk_draw_rectangle(icon
->mask
, mask_gc
, TRUE
,
725 (width
- image
->width
) >> 1,
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 */
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 */
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);
765 int y
= text_y
+ font
->ascent
;
769 if (o_text_bg
== TEXT_BG_OUTLINE
)
782 gtk_widget_shape_combine_mask(icon
->win
, icon
->mask
, 0, 0);
788 static gint
draw_icon(GtkWidget
*widget
, GdkEventExpose
*event
, Icon
*icon
)
790 GdkFont
*font
= icon
->widget
->style
->font
;
794 DirItem
*item
= &icon
->item
;
795 MaskedPixmap
*image
= item
->image
;
797 GdkGC
*gc
= widget
->style
->black_gc
;
798 GtkStateType state
= icon
->selected
? GTK_STATE_SELECTED
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
,
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
,
823 0, 0, /* Source x,y */
824 image_x
, WINK_FRAME
, /* Dest x,y */
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
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
,
839 0, 0, /* Source x,y */
840 image_x
, WINK_FRAME
, /* Dest x,y */
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
,
854 NULL
, widget
, "text",
857 item
->name_width
+ 2,
861 gtk_paint_string(widget
->style
, widget
->window
,
863 NULL
, widget
, "text",
865 text_y
+ font
->ascent
,
868 if (current_wink_icon
== icon
)
870 gdk_draw_rectangle(icon
->widget
->window
,
871 icon
->widget
->style
->white_gc
,
873 0, 0, width
- 1, height
- 1);
874 gdk_draw_rectangle(icon
->widget
->window
,
875 icon
->widget
->style
->black_gc
,
877 1, 1, width
- 3, height
- 3);
883 static gboolean
root_property_event(GtkWidget
*widget
,
884 GdkEventProperty
*event
,
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();
897 static gboolean
root_button_press(GtkWidget
*widget
,
898 GdkEventButton
*event
,
903 action
= bind_lookup_bev(BIND_PINBOARD
, event
);
907 case ACT_CLEAR_SELECTION
:
908 pinboard_clear_selection();
912 show_pinboard_menu(event
, NULL
);
917 g_warning("Unsupported action : %d\n", action
);
924 static gboolean
enter_notify(GtkWidget
*widget
,
925 GdkEventCrossing
*event
,
928 pinboard_icon_may_update(icon
);
933 static void perform_action(Icon
*icon
, GdkEventButton
*event
)
937 action
= bind_lookup_bev(BIND_PINBOARD_ICON
, event
);
943 pinboard_wink_item(icon
, TRUE
);
944 run_diritem(icon
->path
, &icon
->item
, NULL
, FALSE
);
948 pinboard_wink_item(icon
, TRUE
);
949 run_diritem(icon
->path
, &icon
->item
, NULL
, TRUE
);
953 show_pinboard_menu(event
, 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
);
962 case ACT_PRIME_AND_SELECT
:
964 pinboard_select_only(icon
);
965 dnd_motion_start(MOTION_READY_FOR_DND
);
967 case ACT_PRIME_AND_TOGGLE
:
968 pinboard_set_selected(icon
, !icon
->selected
);
969 dnd_motion_start(MOTION_READY_FOR_DND
);
971 case ACT_PRIME_FOR_DND
:
972 pinboard_wink_item(icon
, TRUE
);
973 dnd_motion_start(MOTION_READY_FOR_DND
);
975 case ACT_TOGGLE_SELECTED
:
976 pinboard_set_selected(icon
, !icon
->selected
);
978 case ACT_SELECT_EXCL
:
979 pinboard_select_only(icon
);
984 g_warning("Unsupported action : %d\n", action
);
989 static gboolean
button_release_event(GtkWidget
*widget
,
990 GdkEventButton
*event
,
993 if (pinboard_modified
)
996 if (dnd_motion_release(event
))
999 perform_action(icon
, event
);
1004 static gboolean
button_press_event(GtkWidget
*widget
,
1005 GdkEventButton
*event
,
1008 if (dnd_motion_press(widget
, event
))
1009 perform_action(icon
, event
);
1014 /* Return a text/uri-list of all the icons in the list */
1015 static guchar
*create_uri_list(GList
*list
)
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");
1035 g_string_free(tmp
, FALSE
);
1040 static void start_drag(Icon
*icon
, GdkEventMotion
*event
)
1042 GtkWidget
*widget
= icon
->widget
;
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
);
1062 uri_list
= create_uri_list(selected
);
1063 drag_selection(widget
, event
, 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
,
1079 if (motion_state
== MOTION_READY_FOR_DND
)
1081 if (dnd_motion_moved(event
))
1082 start_drag(icon
, event
);
1085 else if (motion_state
!= MOTION_REPOSITION
)
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
)
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
);
1112 pinboard_modified
= 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
;
1127 end
= strchr(line
+ 1, '>');
1129 return _("Missing '>' in icon label");
1131 leaf
= g_strndup(line
+ 1, end
- line
- 1);
1135 while (isspace(*line
))
1138 return _("Missing ',' after icon label");
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
);
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)
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",
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
),
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
),
1195 gtk_signal_connect(GTK_OBJECT(proxy_invisible
),
1197 GTK_SIGNAL_FUNC(selection_get
), NULL
);
1199 gtk_selection_add_targets(proxy_invisible
,
1200 GDK_SELECTION_PRIMARY
,
1202 sizeof(target_table
) / sizeof(*target_table
));
1205 root
= gdk_window_lookup(GDK_ROOT_WINDOW());
1207 root
= gdk_window_foreign_new(GDK_ROOT_WINDOW());
1209 if (!setup_xdnd_proxy(GDK_ROOT_WINDOW(), proxy_invisible
->window
))
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();
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
)
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
;
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
);
1266 leaf
= g_strconcat("pb_", current_pinboard
->name
, NULL
);
1267 save
= choices_find_path_save(leaf
, "ROX-Filer", TRUE
);
1274 save_new
= g_strconcat(save
, ".new", NULL
);
1275 file
= fopen(save_new
, "wb");
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
)
1298 if (rename(save_new
, save
))
1303 delayed_error(_("Error saving pinboard"), g_strerror(errno
));
1308 g_string_free(tmp
, TRUE
);
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
,
1325 GdkWindow
*proxy
= proxy_invisible
->window
;
1329 switch (xev
->type
) {
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
;
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
;
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
;
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
);
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
);
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
,
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
,
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
,
1447 GdkDragAction action
= context
->suggested_action
;
1449 DirItem
*item
= &icon
->item
;
1451 if (gtk_drag_get_source_widget(context
) == widget
)
1452 goto out
; /* Can't drag something to itself! */
1455 goto out
; /* Can't drag a selection to itself */
1457 type
= dnd_motion_item(context
, &item
);
1462 /* We actually must pretend to accept the drop, even if the
1463 * directory isn't writeable, so that the spring-opening
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)
1475 g_dataset_set_data(context
, "drop_dest_type", 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
,
1495 pinboard_wink_item(NULL
, FALSE
);
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
)
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
,
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
,
1531 /* Called when another application wants the contents of our selection */
1532 static void selection_get(GtkWidget
*widget
,
1533 GtkSelectionData
*selection_data
,
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
)
1555 g_string_append(str
, leader
);
1556 g_string_append(str
, icon
->path
);
1557 g_string_append_c(str
, ' ');
1562 gtk_selection_data_set(selection_data
,
1563 gdk_atom_intern("STRING", FALSE
),
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 */
1576 pinboard_clear_selection();
1582 static gboolean
bg_drag_motion(GtkWidget
*widget
,
1583 GdkDragContext
*context
,
1589 /* Dragging from the pinboard to the pinboard is not allowed */
1590 if (pinboard_drag_in_progress
)
1593 gdk_drag_status(context
, context
->suggested_action
, time
);
1597 static void drag_end(GtkWidget
*widget
,
1598 GdkDragContext
*context
,
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)
1616 g_return_if_fail(current_pinboard
!= NULL
);
1618 for (next
= current_pinboard
->icons
; next
; next
= next
->next
)
1620 Icon
*icon
= (Icon
*) next
->data
;
1625 /* Display the pinboard menu. Set icon to NULL if no particular icon
1628 static void show_pinboard_menu(GdkEventButton
*event
, Icon
*icon
)
1632 AppMenus
*menus
= NULL
;
1637 tmp_icon_selected
= FALSE
;
1640 pinboard_select_only(icon
);
1641 tmp_icon_selected
= TRUE
;
1643 /* Check for icon-specific menu */
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
;
1658 menu_set_items_shaded(pinboard_menu
,
1659 icons
->next
? TRUE
: FALSE
, 4, 3);
1661 menu_set_items_shaded(pinboard_menu
, FALSE
, 7, 1);
1664 menu_set_items_shaded(pinboard_menu
, TRUE
, 4, 4);
1666 /* Add the AppMenu items if necessary */
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
)
1678 icon
= pinboard_selected_icon();
1681 show_item_help(icon
->path
, &icon
->item
);
1683 delayed_error(PROJECT
,
1684 _("You must first select a single pinned icon to get "
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
)
1707 icon
= pinboard_selected_icon();
1710 open_to_show(icon
->path
);
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
)
1726 static void edit_icon(gpointer data
, guint action
, GtkWidget
*widget
)
1730 icon
= pinboard_selected_icon();
1733 show_rename_box(icon
->widget
, icon
, rename_cb
);
1736 delayed_error(PROJECT
,
1737 _("First, select a single item to edit"));