4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* pinboard.c - icons on the desktop background */
34 #include <libxml/parser.h>
46 #include "gui_support.h"
56 #include "panel.h" /* For panel_mark_used() */
58 static gboolean tmp_icon_selected
= FALSE
; /* When dragging */
61 guchar
*name
; /* Leaf name */
65 gchar
*backdrop
; /* Pathname */
66 BackdropStyle backdrop_style
;
67 gint to_backdrop_app
; /* pipe FD, or -1 */
68 gint from_backdrop_app
; /* pipe FD, or -1 */
70 GString
*input_buffer
;
72 GtkWidget
*window
; /* Screen-sized window */
77 #define IS_PIN_ICON(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), pin_icon_get_type())
79 typedef struct _PinIconClass PinIconClass
;
80 typedef struct _PinIcon PinIcon
;
82 struct _PinIconClass
{
91 GtkWidget
*widget
; /* The drawing area for the icon */
95 /* The number of pixels between the bottom of the image and the top
100 /* The size of the border around the icon which is used when winking */
104 #define GRID_STEP_FINE 2
105 #define GRID_STEP_MED 16
106 #define GRID_STEP_COARSE 32
108 /* Used in options */
109 #define CORNER_TOP_LEFT 0
110 #define CORNER_TOP_RIGHT 1
111 #define CORNER_BOTTOM_LEFT 2
112 #define CORNER_BOTTOM_RIGHT 3
117 static PinIcon
*current_wink_icon
= NULL
;
118 static gint wink_timeout
;
120 /* Used for the text colours (only) in the icons */
121 static GdkColor pin_text_fg_col
, pin_text_bg_col
;
122 PangoFontDescription
*pinboard_font
= NULL
; /* NULL => Gtk default */
124 static GdkColor pin_text_shadow_col
;
126 Pinboard
*current_pinboard
= NULL
;
127 static gint loading_pinboard
= 0; /* Non-zero => loading */
129 /* The Icon that was used to start the current drag, if any */
130 Icon
*pinboard_drag_in_progress
= NULL
;
132 /* For selecting groups of icons */
133 static gboolean lasso_in_progress
= FALSE
;
134 static int lasso_rect_x1
, lasso_rect_x2
;
135 static int lasso_rect_y1
, lasso_rect_y2
;
137 static Option o_pinboard_clamp_icons
, o_pinboard_grid_step
;
138 static Option o_pinboard_fg_colour
, o_pinboard_bg_colour
;
139 static Option o_pinboard_tasklist
, o_forward_buttons_13
;
140 static Option o_iconify_start
, o_iconify_dir
;
141 static Option o_label_font
, o_pinboard_shadow_colour
;
142 static Option o_pinboard_shadow_labels
;
144 /* Static prototypes */
145 static GType
pin_icon_get_type(void);
146 static void set_size_and_style(PinIcon
*pi
);
147 static gint
draw_icon(GtkWidget
*widget
, GdkEventExpose
*event
, PinIcon
*pi
);
148 static void draw_label_shadow(GtkLabel
*label
);
149 static gint
end_wink(gpointer data
);
150 static gboolean
button_release_event(GtkWidget
*widget
,
151 GdkEventButton
*event
,
153 static gboolean
enter_notify(GtkWidget
*widget
,
154 GdkEventCrossing
*event
,
156 static gboolean
button_press_event(GtkWidget
*widget
,
157 GdkEventButton
*event
,
159 static gint
icon_motion_notify(GtkWidget
*widget
,
160 GdkEventMotion
*event
,
162 static const char *pin_from_file(gchar
*line
);
163 static void snap_to_grid(int *x
, int *y
);
164 static void offset_from_centre(PinIcon
*pi
, int *x
, int *y
);
165 static gboolean
drag_motion(GtkWidget
*widget
,
166 GdkDragContext
*context
,
171 static void drag_set_pinicon_dest(PinIcon
*pi
);
172 static void drag_leave(GtkWidget
*widget
,
173 GdkDragContext
*context
,
176 static gboolean
bg_drag_motion(GtkWidget
*widget
,
177 GdkDragContext
*context
,
182 static gboolean
bg_drag_leave(GtkWidget
*widget
,
183 GdkDragContext
*context
,
186 static gboolean
bg_expose(GtkWidget
*window
,
187 GdkEventExpose
*event
, gpointer data
);
188 static void drag_end(GtkWidget
*widget
,
189 GdkDragContext
*context
,
191 static void reshape_all(void);
192 static void pinboard_check_options(void);
193 static void pinboard_load_from_xml(xmlDocPtr doc
);
194 static void pinboard_clear(void);
195 static void pinboard_save(void);
196 static PinIcon
*pin_icon_new(const char *pathname
, const char *name
);
197 static void pin_icon_destroyed(PinIcon
*pi
);
198 static void pin_icon_set_tip(PinIcon
*pi
);
199 static void pinboard_show_menu(GdkEventButton
*event
, PinIcon
*pi
);
200 static void create_pinboard_window(Pinboard
*pinboard
);
201 static void reload_backdrop(Pinboard
*pinboard
,
202 const gchar
*backdrop
,
203 BackdropStyle backdrop_style
);
204 static void set_backdrop(const gchar
*path
, BackdropStyle style
);
205 static void pinboard_reshape_icon(Icon
*icon
);
206 static gint
draw_wink(GtkWidget
*widget
, GdkEventExpose
*event
, PinIcon
*pi
);
207 static void abandon_backdrop_app(Pinboard
*pinboard
);
208 static void drag_backdrop_dropped(GtkWidget
*frame
,
209 GdkDragContext
*context
,
212 GtkSelectionData
*selection_data
,
216 static void backdrop_response(GtkWidget
*dialog
, gint response
, gpointer data
);
217 static void find_free_rect(Pinboard
*pinboard
, GdkRectangle
*rect
);
218 static void update_pinboard_font(void);
219 static void draw_lasso(void);
220 static gint
lasso_motion(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer d
);
223 /****************************************************************
224 * EXTERNAL INTERFACE *
225 ****************************************************************/
227 void pinboard_init(void)
229 option_add_string(&o_pinboard_fg_colour
, "pinboard_fg_colour", "#fff");
230 option_add_string(&o_pinboard_bg_colour
, "pinboard_bg_colour", "#888");
231 option_add_string(&o_pinboard_shadow_colour
, "pinboard_shadow_colour",
233 option_add_string(&o_label_font
, "label_font", "");
234 option_add_int(&o_pinboard_shadow_labels
, "pinboard_shadow_labels", 1);
236 option_add_int(&o_pinboard_clamp_icons
, "pinboard_clamp_icons", 1);
237 option_add_int(&o_pinboard_grid_step
, "pinboard_grid_step",
239 option_add_int(&o_pinboard_tasklist
, "pinboard_tasklist", TRUE
);
240 option_add_int(&o_forward_buttons_13
, "pinboard_forward_buttons_13",
243 option_add_int(&o_iconify_start
, "iconify_start", CORNER_TOP_RIGHT
);
244 option_add_int(&o_iconify_dir
, "iconify_dir", DIR_VERT
);
246 option_add_notify(pinboard_check_options
);
248 gdk_color_parse(o_pinboard_fg_colour
.value
, &pin_text_fg_col
);
249 gdk_color_parse(o_pinboard_bg_colour
.value
, &pin_text_bg_col
);
250 gdk_color_parse(o_pinboard_shadow_colour
.value
, &pin_text_shadow_col
);
251 update_pinboard_font();
254 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
255 * and make it the current pinboard.
256 * Any existing pinned items are removed. You must call this
257 * at least once before using the pinboard. NULL disables the
260 void pinboard_activate(const gchar
*name
)
262 Pinboard
*old_board
= current_pinboard
;
263 guchar
*path
, *slash
;
265 /* Treat an empty name the same as NULL */
274 if (number_of_windows
< 1 && gtk_main_level() > 0)
277 gdk_property_delete(gdk_get_default_root_window(),
278 gdk_atom_intern("_XROOTPMAP_ID", FALSE
));
284 slash
= strchr(name
, '/');
287 if (access(name
, F_OK
))
288 path
= NULL
; /* File does not (yet) exist */
290 path
= g_strdup(name
);
296 leaf
= g_strconcat("pb_", name
, NULL
);
297 path
= choices_find_path_load(leaf
, PROJECT
);
301 current_pinboard
= g_new(Pinboard
, 1);
302 current_pinboard
->name
= g_strdup(name
);
303 current_pinboard
->icons
= NULL
;
304 current_pinboard
->window
= NULL
;
305 current_pinboard
->backdrop
= NULL
;
306 current_pinboard
->backdrop_style
= BACKDROP_NONE
;
307 current_pinboard
->to_backdrop_app
= -1;
308 current_pinboard
->from_backdrop_app
= -1;
309 current_pinboard
->input_tag
= -1;
310 current_pinboard
->input_buffer
= NULL
;
312 create_pinboard_window(current_pinboard
);
318 doc
= xmlParseFile(path
);
321 pinboard_load_from_xml(doc
);
323 reload_backdrop(current_pinboard
,
324 current_pinboard
->backdrop
,
325 current_pinboard
->backdrop_style
);
329 parse_file(path
, pin_from_file
);
330 info_message(_("Your old pinboard file has been "
331 "converted to the new XML format."));
337 pinboard_pin(home_dir
, "Home",
343 if (o_pinboard_tasklist
.int_value
)
344 tasklist_set_active(TRUE
);
347 /* Return the window of the current pinboard, or NULL.
348 * Used to make sure lowering the panels doesn't lose them...
350 GdkWindow
*pinboard_get_window(void)
352 if (current_pinboard
)
353 return current_pinboard
->window
->window
;
357 const char *pinboard_get_name(void)
359 g_return_val_if_fail(current_pinboard
!= NULL
, NULL
);
361 return current_pinboard
->name
;
364 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
367 void pinboard_add_widget(GtkWidget
*widget
)
372 g_return_if_fail(current_pinboard
!= NULL
);
374 gtk_fixed_put(GTK_FIXED(current_pinboard
->fixed
), widget
, 0, 0);
376 gtk_widget_size_request(widget
, &req
);
378 rect
.width
= req
.width
;
379 rect
.height
= req
.height
;
380 find_free_rect(current_pinboard
, &rect
);
382 gtk_fixed_move(GTK_FIXED(current_pinboard
->fixed
),
383 widget
, rect
.x
, rect
.y
);
386 /* Add a new icon to the background.
387 * 'path' should be an absolute pathname.
388 * 'x' and 'y' are the coordinates of the point in the middle of the text.
389 * 'name' is the name to use. If NULL then the leafname of path is used.
391 * name and path are in UTF-8 for Gtk+-2.0 only.
393 void pinboard_pin(const gchar
*path
, const gchar
*name
, int x
, int y
,
394 const gchar
*shortcut
)
396 GtkWidget
*align
, *vbox
;
401 g_return_if_fail(path
!= NULL
);
402 g_return_if_fail(current_pinboard
!= NULL
);
404 pi
= pin_icon_new(path
, name
);
409 /* This is a bit complicated...
411 * An icon needs to be a NO_WINDOW widget so that the image can
412 * blend with the background (A ParentRelative window also works, but
413 * is slow, causes the xfree86's memory consumption to grow without
414 * bound, and doesn't even get freed when the filer quits!).
416 * However, the icon also needs to have a window, so we get events
417 * delivered correctly. The solution is to float an InputOnly window
418 * over the icon. Since GtkButton works the same way, we just use
422 /* Button takes the initial ref of Icon */
423 pi
->win
= gtk_button_new();
424 gtk_container_set_border_width(GTK_CONTAINER(pi
->win
), WINK_FRAME
);
425 g_signal_connect(pi
->win
, "expose-event", G_CALLBACK(draw_wink
), pi
);
426 gtk_button_set_relief(GTK_BUTTON(pi
->win
), GTK_RELIEF_NONE
);
428 vbox
= gtk_vbox_new(FALSE
, 0);
429 gtk_container_add(GTK_CONTAINER(pi
->win
), vbox
);
431 align
= gtk_alignment_new(0.5, 0.5, 0, 0);
432 pi
->widget
= gtk_hbox_new(FALSE
, 0); /* Placeholder */
433 gtk_container_add(GTK_CONTAINER(align
), pi
->widget
);
435 gtk_box_pack_start(GTK_BOX(vbox
), align
, FALSE
, TRUE
, 0);
436 drag_set_pinicon_dest(pi
);
437 g_signal_connect(pi
->win
, "drag_data_get",
438 G_CALLBACK(drag_data_get
), NULL
);
440 pi
->label
= gtk_label_new(icon
->item
->leafname
);
441 gtk_label_set_line_wrap(GTK_LABEL(pi
->label
), TRUE
);
442 gtk_box_pack_start(GTK_BOX(vbox
), pi
->label
, TRUE
, TRUE
, 0);
444 gtk_fixed_put(GTK_FIXED(current_pinboard
->fixed
), pi
->win
, 0, 0);
446 snap_to_grid(&x
, &y
);
449 gtk_widget_show_all(pi
->win
);
450 pinboard_reshape_icon((Icon
*) pi
);
452 gtk_widget_realize(pi
->win
);
453 events
= GTK_BUTTON(pi
->win
)->event_window
;
454 gdk_window_set_events(events
,
456 GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
|
457 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
458 GDK_BUTTON1_MOTION_MASK
| GDK_ENTER_NOTIFY_MASK
|
459 GDK_BUTTON2_MOTION_MASK
| GDK_BUTTON3_MOTION_MASK
);
460 g_signal_connect(pi
->win
, "enter-notify-event",
461 G_CALLBACK(enter_notify
), pi
);
462 g_signal_connect(pi
->win
, "button-press-event",
463 G_CALLBACK(button_press_event
), pi
);
464 g_signal_connect(pi
->win
, "button-release-event",
465 G_CALLBACK(button_release_event
), pi
);
466 g_signal_connect(pi
->win
, "motion-notify-event",
467 G_CALLBACK(icon_motion_notify
), pi
);
468 g_signal_connect(pi
->win
, "expose-event",
469 G_CALLBACK(draw_icon
), pi
);
470 g_signal_connect_swapped(pi
->win
, "style-set",
471 G_CALLBACK(pinboard_reshape_icon
), pi
);
472 g_signal_connect_swapped(pi
->win
, "destroy",
473 G_CALLBACK(pin_icon_destroyed
), pi
);
475 current_pinboard
->icons
= g_list_prepend(current_pinboard
->icons
, pi
);
476 pin_icon_set_tip(pi
);
478 icon_set_shortcut(icon
, shortcut
);
480 if (!loading_pinboard
)
484 /* Put a border around the icon, briefly.
485 * If icon is NULL then cancel any existing wink.
486 * The icon will automatically unhighlight unless timeout is FALSE,
487 * in which case you must call this function again (with NULL or another
488 * icon) to remove the highlight.
490 static void pinboard_wink_item(PinIcon
*pi
, gboolean timeout
)
492 PinIcon
*old
= current_wink_icon
;
497 current_wink_icon
= pi
;
501 gtk_widget_queue_draw(old
->win
);
502 gdk_window_process_updates(old
->widget
->window
, TRUE
);
504 if (wink_timeout
!= -1)
505 gtk_timeout_remove(wink_timeout
);
510 gtk_widget_queue_draw(pi
->win
);
511 gdk_window_process_updates(pi
->widget
->window
, TRUE
);
514 wink_timeout
= gtk_timeout_add(300, end_wink
, NULL
);
520 /* 'app' is saved as the new application to set the backdrop. It will then be
521 * run, and should communicate with the filer as described in the manual.
523 void pinboard_set_backdrop_app(const gchar
*app
)
529 item
= diritem_new("");
530 diritem_restat(app
, item
, NULL
);
531 if (!(item
->flags
& ITEM_FLAG_APPDIR
))
533 delayed_error(_("The backdrop handler must be an application "
534 "directory. Drag an application directory "
535 "into the Set Backdrop dialog box, or (for "
536 "programmers) pass it to the SOAP "
537 "SetBackdropApp method."));
542 ai
= appinfo_get(app
, item
);
545 can_set
= ai
&& xml_get_section(ai
, ROX_NS
, "CanSetBackdrop") != NULL
;
550 set_backdrop(app
, BACKDROP_PROGRAM
);
552 delayed_error(_("You can only set the backdrop to an image "
553 "or to a program which knows how to "
554 "manage ROX-Filer's backdrop.\n\n"
555 "Programmers: the application's AppInfo.xml "
556 "must contain the CanSetBackdrop element, as "
557 "described in ROX-Filer's manual."));
560 /* Open a dialog box allowing the user to set the backdrop */
561 void pinboard_set_backdrop(void)
563 GtkWidget
*dialog
, *frame
, *label
, *radio
, *hbox
;
565 GtkTargetEntry targets
[] = {
566 {"text/uri-list", 0, TARGET_URI_LIST
},
569 dialog
= gtk_dialog_new_with_buttons(_("Set backdrop"), NULL
,
570 GTK_DIALOG_NO_SEPARATOR
,
571 GTK_STOCK_CLEAR
, GTK_RESPONSE_NO
,
572 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
574 vbox
= GTK_BOX(GTK_DIALOG(dialog
)->vbox
);
576 gtk_window_set_position(GTK_WINDOW(dialog
), GTK_WIN_POS_MOUSE
);
578 label
= gtk_label_new(_("Display backdrop image:"));
579 gtk_misc_set_padding(GTK_MISC(label
), 4, 0);
580 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0);
581 gtk_box_pack_start(vbox
, label
, TRUE
, TRUE
, 4);
583 /* The Centred, Scaled, Tiled radios... */
584 hbox
= gtk_hbox_new(TRUE
, 2);
585 gtk_box_pack_start(vbox
, hbox
, TRUE
, TRUE
, 4);
587 radio
= gtk_radio_button_new_with_label(NULL
, _("Centred"));
588 g_object_set_data(G_OBJECT(dialog
), "radio_centred", radio
);
589 gtk_box_pack_start(GTK_BOX(hbox
), radio
, FALSE
, TRUE
, 0);
591 radio
= gtk_radio_button_new_with_label(
592 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio
)),
594 g_object_set_data(G_OBJECT(dialog
), "radio_scaled", radio
);
595 gtk_box_pack_start(GTK_BOX(hbox
), radio
, FALSE
, TRUE
, 0);
597 radio
= gtk_radio_button_new_with_label(
598 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio
)),
600 gtk_box_pack_start(GTK_BOX(hbox
), radio
, FALSE
, TRUE
, 0);
602 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio
), TRUE
);
604 /* The drop area... */
605 frame
= gtk_frame_new(NULL
);
606 gtk_box_pack_start(vbox
, frame
, TRUE
, TRUE
, 4);
607 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_IN
);
608 gtk_container_set_border_width(GTK_CONTAINER(frame
), 4);
610 gtk_drag_dest_set(frame
, GTK_DEST_DEFAULT_ALL
,
611 targets
, sizeof(targets
) / sizeof(*targets
),
613 g_signal_connect(frame
, "drag_data_received",
614 G_CALLBACK(drag_backdrop_dropped
), dialog
);
616 label
= gtk_label_new(_("Drop an image here"));
617 gtk_misc_set_padding(GTK_MISC(label
), 10, 20);
618 gtk_container_add(GTK_CONTAINER(frame
), label
);
620 g_signal_connect(dialog
, "response",
621 G_CALLBACK(backdrop_response
), NULL
);
622 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_OK
);
624 gtk_widget_show_all(dialog
);
627 /****************************************************************
628 * INTERNAL FUNCTIONS *
629 ****************************************************************/
631 static void backdrop_response(GtkWidget
*dialog
, gint response
, gpointer data
)
633 if (response
== GTK_RESPONSE_NO
)
634 set_backdrop(NULL
, BACKDROP_NONE
);
636 gtk_widget_destroy(dialog
);
639 static void drag_backdrop_dropped(GtkWidget
*frame
,
640 GdkDragContext
*context
,
643 GtkSelectionData
*selection_data
,
649 const gchar
*path
= NULL
;
652 if (!selection_data
->data
)
653 return; /* Timeout? */
655 uris
= uri_list_to_glist(selection_data
->data
);
657 if (g_list_length(uris
) == 1)
658 path
= get_local_path((guchar
*) uris
->data
);
664 _("You should drop a single (local) image file "
665 "onto the drop box - that image will be "
666 "used for the desktop background. You can also "
667 "drag certain applications onto this box."));
671 if (mc_stat(path
, &info
))
674 _("Can't access '%s':\n%s"), path
,
679 if (S_ISDIR(info
.st_mode
))
681 /* Use this program to set the backdrop */
682 pinboard_set_backdrop_app(path
);
684 else if (S_ISREG(info
.st_mode
))
688 radio
= g_object_get_data(G_OBJECT(dialog
), "radio_scaled");
689 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio
)))
691 set_backdrop(path
, BACKDROP_SCALE
);
695 radio
= g_object_get_data(G_OBJECT(dialog
), "radio_centred");
696 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio
)))
698 set_backdrop(path
, BACKDROP_CENTRE
);
702 set_backdrop(path
, BACKDROP_TILE
);
705 delayed_error(_("Only files (and certain applications) can be "
706 "used to set the background image."));
709 /* Do this in the idle loop so that we don't try to put an unmanaged
710 * pinboard behind a managed panel (crashes some WMs).
712 static gboolean
recreate_pinboard(gchar
*name
)
714 pinboard_activate(name
);
720 static void pinboard_check_options(void)
722 GdkColor n_fg
, n_bg
, n_shadow
;
724 gdk_color_parse(o_pinboard_fg_colour
.value
, &n_fg
);
725 gdk_color_parse(o_pinboard_bg_colour
.value
, &n_bg
);
726 gdk_color_parse(o_pinboard_shadow_colour
.value
, &n_shadow
);
728 if (o_override_redirect
.has_changed
&& current_pinboard
)
731 name
= g_strdup(current_pinboard
->name
);
732 pinboard_activate(NULL
);
733 gtk_idle_add((GtkFunction
) recreate_pinboard
, name
);
736 tasklist_set_active(o_pinboard_tasklist
.int_value
&& current_pinboard
);
738 if (gdk_color_equal(&n_fg
, &pin_text_fg_col
) == 0 ||
739 gdk_color_equal(&n_bg
, &pin_text_bg_col
) == 0 ||
740 gdk_color_equal(&n_shadow
, &pin_text_shadow_col
) == 0 ||
741 o_pinboard_shadow_labels
.has_changed
||
742 o_label_font
.has_changed
)
744 pin_text_fg_col
= n_fg
;
745 pin_text_bg_col
= n_bg
;
746 pin_text_shadow_col
= n_shadow
;
747 update_pinboard_font();
749 if (current_pinboard
)
751 GtkWidget
*w
= current_pinboard
->window
;
754 cm
= gtk_widget_get_colormap(w
);
756 gdk_colormap_alloc_color(cm
, &n_bg
, FALSE
, TRUE
);
757 gtk_widget_modify_bg(w
, GTK_STATE_NORMAL
, &n_bg
);
759 gdk_gc_set_rgb_fg_color(current_pinboard
->shadow_gc
,
762 /* Only redraw the background if there is no image */
763 if (!current_pinboard
->backdrop
)
764 reload_backdrop(current_pinboard
,
765 NULL
, BACKDROP_NONE
);
770 tasklist_style_changed();
774 static gint
end_wink(gpointer data
)
776 pinboard_wink_item(NULL
, FALSE
);
780 /* Sets the appearance from the options and updates the size request of
783 static void set_size_and_style(PinIcon
*pi
)
785 Icon
*icon
= (Icon
*) pi
;
786 MaskedPixmap
*image
= icon
->item
->image
;
787 int iwidth
= image
->width
;
788 int iheight
= image
->height
;
790 gtk_widget_modify_fg(pi
->label
, GTK_STATE_PRELIGHT
, &pin_text_fg_col
);
791 gtk_widget_modify_bg(pi
->label
, GTK_STATE_PRELIGHT
, &pin_text_bg_col
);
792 gtk_widget_modify_fg(pi
->label
, GTK_STATE_NORMAL
, &pin_text_fg_col
);
793 gtk_widget_modify_bg(pi
->label
, GTK_STATE_NORMAL
, &pin_text_bg_col
);
794 widget_modify_font(pi
->label
, pinboard_font
);
796 gtk_label_set_text(GTK_LABEL(pi
->label
), icon
->item
->leafname
);
798 gtk_widget_set_size_request(pi
->widget
, iwidth
, iheight
);
801 static gint
draw_icon(GtkWidget
*widget
, GdkEventExpose
*event
, PinIcon
*pi
)
803 static GtkWidgetClass
*parent_class
= NULL
;
804 Icon
*icon
= (Icon
*) pi
;
805 DirItem
*item
= icon
->item
;
806 MaskedPixmap
*image
= item
->image
;
807 int iwidth
= image
->width
;
808 int iheight
= image
->height
;
810 GtkStateType state
= icon
->selected
? GTK_STATE_SELECTED
815 gpointer c
= ((GTypeInstance
*) widget
)->g_class
;
816 parent_class
= (GtkWidgetClass
*) g_type_class_peek_parent(c
);
819 x
= pi
->widget
->allocation
.x
;
820 y
= pi
->widget
->allocation
.y
;
822 gdk_pixbuf_render_to_drawable_alpha(
823 icon
->selected
? image
->pixbuf_lit
: image
->pixbuf
,
828 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
829 GDK_RGB_DITHER_NORMAL
, 0, 0);
831 if (item
->flags
& ITEM_FLAG_SYMLINK
)
833 gdk_pixbuf_render_to_drawable_alpha(im_symlink
->pixbuf
,
838 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
839 GDK_RGB_DITHER_NORMAL
, 0, 0);
841 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
843 MaskedPixmap
*mp
= item
->flags
& ITEM_FLAG_MOUNTED
847 gdk_pixbuf_render_to_drawable_alpha(mp
->pixbuf
,
852 GDK_PIXBUF_ALPHA_FULL
, 128, /* (unused) */
853 GDK_RGB_DITHER_NORMAL
, 0, 0);
858 gtk_paint_flat_box(pi
->label
->style
, pi
->label
->window
,
861 NULL
, pi
->label
, "text",
862 pi
->label
->allocation
.x
,
863 pi
->label
->allocation
.y
,
864 pi
->label
->allocation
.width
,
865 pi
->label
->allocation
.height
);
867 else if (o_pinboard_shadow_labels
.int_value
)
868 draw_label_shadow((GtkLabel
*) pi
->label
);
871 (parent_class
->expose_event
)(widget
, event
);
873 /* Stop the button effect */
877 static void draw_label_shadow(GtkLabel
*label
)
885 misc
= GTK_MISC(label
);
886 widget
= GTK_WIDGET(label
);
887 layout
= gtk_label_get_layout(label
);
889 /* Taken from gtklabel.c ... */
890 if (gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_LTR
)
891 xalign
= misc
->xalign
;
893 xalign
= 1.0 - misc
->xalign
;
895 x
= floor (widget
->allocation
.x
+ (gint
)misc
->xpad
896 + ((widget
->allocation
.width
897 - widget
->requisition
.width
) * xalign
)
899 y
= floor (widget
->allocation
.y
+ (gint
)misc
->ypad
900 + ((widget
->allocation
.height
901 - widget
->requisition
.height
) * misc
->yalign
)
904 gdk_gc_set_clip_origin(current_pinboard
->shadow_gc
,
905 widget
->allocation
.x
, widget
->allocation
.y
);
906 gdk_gc_set_clip_rectangle(current_pinboard
->shadow_gc
,
907 &widget
->allocation
);
909 gdk_draw_layout(widget
->window
, current_pinboard
->shadow_gc
,
910 x
+ 1, y
+ 1, layout
);
912 if (o_pinboard_shadow_labels
.int_value
> 1)
913 gdk_draw_layout(widget
->window
, current_pinboard
->shadow_gc
,
914 x
+ 2, y
+ 2, layout
);
917 static gint
draw_wink(GtkWidget
*widget
, GdkEventExpose
*event
, PinIcon
*pi
)
919 gint x
, y
, width
, height
;
921 if (current_wink_icon
!= pi
)
924 x
= widget
->allocation
.x
;
925 y
= widget
->allocation
.y
;
926 width
= widget
->allocation
.width
;
927 height
= widget
->allocation
.height
;
929 gdk_draw_rectangle(widget
->window
,
930 pi
->widget
->style
->white_gc
,
932 x
, y
, width
- 1, height
- 1);
933 gdk_draw_rectangle(widget
->window
,
934 pi
->widget
->style
->black_gc
,
936 x
+ 1, y
+ 1, width
- 3, height
- 3);
941 static gboolean
enter_notify(GtkWidget
*widget
,
942 GdkEventCrossing
*event
,
945 icon_may_update((Icon
*) pi
);
950 static void select_lasso(void)
953 int minx
, miny
, maxx
, maxy
;
955 g_return_if_fail(lasso_in_progress
== TRUE
);
957 minx
= MIN(lasso_rect_x1
, lasso_rect_x2
);
958 miny
= MIN(lasso_rect_y1
, lasso_rect_y2
);
959 maxx
= MAX(lasso_rect_x1
, lasso_rect_x2
);
960 maxy
= MAX(lasso_rect_y1
, lasso_rect_y2
);
962 for (next
= current_pinboard
->icons
; next
; next
= next
->next
)
964 PinIcon
*pi
= (PinIcon
*) next
->data
;
965 GtkAllocation
*alloc
= &pi
->win
->allocation
;
966 int cx
= alloc
->x
+ alloc
->width
/ 2;
967 int cy
= alloc
->y
+ alloc
->height
/ 2;
969 if (cx
> minx
&& cx
< maxx
&& cy
> miny
&& cy
< maxy
)
970 icon_set_selected((Icon
*) pi
, TRUE
);
974 static void cancel_lasso(void)
977 lasso_in_progress
= FALSE
;
980 static void pinboard_lasso_box(int start_x
, int start_y
)
982 if (lasso_in_progress
)
984 lasso_in_progress
= TRUE
;
985 lasso_rect_x1
= lasso_rect_x2
= start_x
;
986 lasso_rect_y1
= lasso_rect_y2
= start_y
;
991 static gint
lasso_motion(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer d
)
993 if (!lasso_in_progress
)
996 if (lasso_rect_x2
!= event
->x
|| lasso_rect_y2
!= event
->y
)
999 lasso_rect_x2
= event
->x
;
1000 lasso_rect_y2
= event
->y
;
1007 /* Mark the area of the screen covered by the lasso box for redraw */
1008 static void draw_lasso(void)
1012 if (!lasso_in_progress
)
1015 area
.x
= MIN(lasso_rect_x1
, lasso_rect_x2
);
1016 area
.y
= MIN(lasso_rect_y1
, lasso_rect_y2
);
1017 area
.width
= ABS(lasso_rect_x1
- lasso_rect_x2
);
1018 area
.height
= ABS(lasso_rect_y1
- lasso_rect_y2
);
1020 gdk_window_invalidate_rect(current_pinboard
->window
->window
,
1024 static void perform_action(PinIcon
*pi
, GdkEventButton
*event
)
1027 Icon
*icon
= (Icon
*) pi
;
1029 action
= bind_lookup_bev(pi
? BIND_PINBOARD_ICON
: BIND_PINBOARD
,
1032 /* Actions that can happen with or without an icon */
1035 case ACT_LASSO_CLEAR
:
1036 icon_select_only(NULL
);
1038 case ACT_LASSO_MODIFY
:
1039 pinboard_lasso_box(event
->x
, event
->y
);
1041 case ACT_CLEAR_SELECTION
:
1042 icon_select_only(NULL
);
1044 case ACT_POPUP_MENU
:
1045 dnd_motion_ungrab();
1046 pinboard_show_menu(event
, pi
);
1054 g_return_if_fail(pi
!= NULL
);
1059 dnd_motion_ungrab();
1060 pinboard_wink_item(pi
, TRUE
);
1061 if (event
->type
== GDK_2BUTTON_PRESS
)
1062 icon_set_selected(icon
, FALSE
);
1063 run_diritem(icon
->path
, icon
->item
, NULL
, NULL
, FALSE
);
1066 dnd_motion_ungrab();
1067 pinboard_wink_item(pi
, TRUE
);
1068 if (event
->type
== GDK_2BUTTON_PRESS
)
1069 icon_set_selected(icon
, FALSE
);
1070 run_diritem(icon
->path
, icon
->item
, NULL
, NULL
, TRUE
);
1072 case ACT_PRIME_AND_SELECT
:
1073 if (!icon
->selected
)
1074 icon_select_only(icon
);
1075 dnd_motion_start(MOTION_READY_FOR_DND
);
1077 case ACT_PRIME_AND_TOGGLE
:
1078 icon_set_selected(icon
, !icon
->selected
);
1079 dnd_motion_start(MOTION_READY_FOR_DND
);
1081 case ACT_PRIME_FOR_DND
:
1082 dnd_motion_start(MOTION_READY_FOR_DND
);
1084 case ACT_TOGGLE_SELECTED
:
1085 icon_set_selected(icon
, !icon
->selected
);
1087 case ACT_SELECT_EXCL
:
1088 icon_select_only(icon
);
1091 g_warning("Unsupported action : %d\n", action
);
1096 static void forward_to_root(GdkEventButton
*event
)
1100 if (event
->type
== GDK_BUTTON_PRESS
)
1102 xev
.type
= ButtonPress
;
1103 XUngrabPointer(gdk_display
, event
->time
);
1106 xev
.type
= ButtonRelease
;
1108 xev
.window
= gdk_x11_get_default_root_xwindow();
1109 xev
.root
= xev
.window
;
1110 xev
.subwindow
= None
;
1111 xev
.time
= event
->time
;
1112 xev
.x
= event
->x_root
; /* Needed for icewm */
1113 xev
.y
= event
->y_root
;
1114 xev
.x_root
= event
->x_root
;
1115 xev
.y_root
= event
->y_root
;
1116 xev
.state
= event
->state
;
1117 xev
.button
= event
->button
;
1118 xev
.same_screen
= True
;
1120 XSendEvent(gdk_display
, xev
.window
, False
,
1121 ButtonPressMask
| ButtonReleaseMask
, (XEvent
*) &xev
);
1124 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1125 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1127 /* pi is NULL if this is a root event */
1128 static gboolean
button_release_event(GtkWidget
*widget
,
1129 GdkEventButton
*event
,
1132 if (FORWARDED_BUTTON(pi
, event
->button
))
1133 forward_to_root(event
);
1134 else if (dnd_motion_release(event
))
1136 if (motion_buttons_pressed
== 0 && lasso_in_progress
)
1144 perform_action(pi
, event
);
1149 /* pi is NULL if this is a root event */
1150 static gboolean
button_press_event(GtkWidget
*widget
,
1151 GdkEventButton
*event
,
1154 /* Just in case we've jumped in front of everything... */
1155 gdk_window_lower(current_pinboard
->window
->window
);
1157 if (FORWARDED_BUTTON(pi
, event
->button
))
1158 forward_to_root(event
);
1159 else if (dnd_motion_press(widget
, event
))
1160 perform_action(pi
, event
);
1165 static void start_drag(PinIcon
*pi
, GdkEventMotion
*event
)
1167 GtkWidget
*widget
= pi
->win
;
1168 Icon
*icon
= (Icon
*) pi
;
1170 if (!icon
->selected
)
1172 tmp_icon_selected
= TRUE
;
1173 icon_select_only(icon
);
1176 g_return_if_fail(icon_selection
!= NULL
);
1178 pinboard_drag_in_progress
= icon
;
1180 if (icon_selection
->next
== NULL
)
1181 drag_one_item(widget
, event
, icon
->path
, icon
->item
, NULL
);
1186 uri_list
= icon_create_uri_list();
1187 drag_selection(widget
, event
, uri_list
);
1192 /* An icon is being dragged around... */
1193 static gint
icon_motion_notify(GtkWidget
*widget
,
1194 GdkEventMotion
*event
,
1197 if (motion_state
== MOTION_READY_FOR_DND
)
1199 if (dnd_motion_moved(event
))
1200 start_drag(pi
, event
);
1207 static void backdrop_from_xml(xmlNode
*node
)
1211 g_free(current_pinboard
->backdrop
);
1212 current_pinboard
->backdrop
= xmlNodeGetContent(node
);
1214 style
= xmlGetProp(node
, "style");
1218 current_pinboard
->backdrop_style
=
1219 g_strcasecmp(style
, "Tiled") == 0 ? BACKDROP_TILE
:
1220 g_strcasecmp(style
, "Scaled") == 0 ? BACKDROP_SCALE
:
1221 g_strcasecmp(style
, "Centred") == 0 ? BACKDROP_CENTRE
:
1222 g_strcasecmp(style
, "Program") == 0 ? BACKDROP_PROGRAM
:
1227 current_pinboard
->backdrop_style
= BACKDROP_TILE
;
1230 /* Create one pinboard icon for each icon in the doc */
1231 static void pinboard_load_from_xml(xmlDocPtr doc
)
1233 xmlNodePtr node
, root
;
1234 char *tmp
, *label
, *path
, *shortcut
;
1237 root
= xmlDocGetRootElement(doc
);
1239 for (node
= root
->xmlChildrenNode
; node
; node
= node
->next
)
1241 if (node
->type
!= XML_ELEMENT_NODE
)
1243 if (strcmp(node
->name
, "backdrop") == 0)
1245 backdrop_from_xml(node
);
1248 if (strcmp(node
->name
, "icon") != 0)
1251 tmp
= xmlGetProp(node
, "x");
1257 tmp
= xmlGetProp(node
, "y");
1263 label
= xmlGetProp(node
, "label");
1265 label
= g_strdup("<missing label>");
1266 path
= xmlNodeGetContent(node
);
1268 path
= g_strdup("<missing path>");
1269 shortcut
= xmlGetProp(node
, "shortcut");
1271 pinboard_pin(path
, label
, x
, y
, shortcut
);
1279 /* Called for each line in the pinboard file while loading a new board.
1280 * Only used for old-format files when converting to XML.
1282 static const char *pin_from_file(gchar
*line
)
1291 end
= strchr(line
+ 1, '>');
1293 return _("Missing '>' in icon label");
1295 leaf
= g_strndup(line
+ 1, end
- line
- 1);
1299 while (isspace(*line
))
1302 return _("Missing ',' after icon label");
1306 if (sscanf(line
, " %d , %d , %n", &x
, &y
, &n
) < 2)
1307 return NULL
; /* Ignore format errors */
1309 pinboard_pin(line
+ n
, leaf
, x
, y
, NULL
);
1316 /* Write the current state of the pinboard to the current pinboard file */
1317 static void pinboard_save(void)
1319 guchar
*save
= NULL
;
1320 guchar
*save_new
= NULL
;
1322 xmlDocPtr doc
= NULL
;
1325 g_return_if_fail(current_pinboard
!= NULL
);
1327 if (strchr(current_pinboard
->name
, '/'))
1328 save
= g_strdup(current_pinboard
->name
);
1333 leaf
= g_strconcat("pb_", current_pinboard
->name
, NULL
);
1334 save
= choices_find_path_save(leaf
, PROJECT
, TRUE
);
1341 doc
= xmlNewDoc("1.0");
1342 xmlDocSetRootElement(doc
, xmlNewDocNode(doc
, NULL
, "pinboard", NULL
));
1344 root
= xmlDocGetRootElement(doc
);
1346 if (current_pinboard
->backdrop
)
1348 BackdropStyle style
= current_pinboard
->backdrop_style
;
1351 tree
= xmlNewTextChild(root
, NULL
, "backdrop",
1352 current_pinboard
->backdrop
);
1353 xmlSetProp(tree
, "style",
1354 style
== BACKDROP_TILE
? "Tiled" :
1355 style
== BACKDROP_CENTRE
? "Centred" :
1356 style
== BACKDROP_SCALE
? "Scaled" :
1360 for (next
= current_pinboard
->icons
; next
; next
= next
->next
)
1363 PinIcon
*pi
= (PinIcon
*) next
->data
;
1364 Icon
*icon
= (Icon
*) pi
;
1367 tree
= xmlNewTextChild(root
, NULL
, "icon", icon
->src_path
);
1369 tmp
= g_strdup_printf("%d", pi
->x
);
1370 xmlSetProp(tree
, "x", tmp
);
1373 tmp
= g_strdup_printf("%d", pi
->y
);
1374 xmlSetProp(tree
, "y", tmp
);
1377 xmlSetProp(tree
, "label", icon
->item
->leafname
);
1379 xmlSetProp(tree
, "shortcut", icon
->shortcut
);
1382 save_new
= g_strconcat(save
, ".new", NULL
);
1383 if (save_xml_file(doc
, save_new
) || rename(save_new
, save
))
1384 delayed_error(_("Error saving pinboard %s: %s"),
1385 save
, g_strerror(errno
));
1393 static void snap_to_grid(int *x
, int *y
)
1395 int step
= o_pinboard_grid_step
.int_value
;
1397 *x
= ((*x
+ step
/ 2) / step
) * step
;
1398 *y
= ((*y
+ step
/ 2) / step
) * step
;
1401 /* Convert (x,y) from a centre point to a window position */
1402 static void offset_from_centre(PinIcon
*pi
, int *x
, int *y
)
1404 gboolean clamp
= o_pinboard_clamp_icons
.int_value
;
1407 gtk_widget_size_request(pi
->win
, &req
);
1409 *x
-= req
.width
>> 1;
1410 *y
-= req
.height
>> 1;
1411 *x
= CLAMP(*x
, 0, screen_width
- (clamp
? req
.width
: 0));
1412 *y
= CLAMP(*y
, 0, screen_height
- (clamp
? req
.height
: 0));
1415 /* Same as drag_set_dest(), but for pinboard icons */
1416 static void drag_set_pinicon_dest(PinIcon
*pi
)
1418 GtkObject
*obj
= GTK_OBJECT(pi
->win
);
1420 make_drop_target(pi
->win
, 0);
1422 g_signal_connect(obj
, "drag_motion", G_CALLBACK(drag_motion
), pi
);
1423 g_signal_connect(obj
, "drag_leave", G_CALLBACK(drag_leave
), pi
);
1424 g_signal_connect(obj
, "drag_end", G_CALLBACK(drag_end
), pi
);
1427 /* Called during the drag when the mouse is in a widget registered
1428 * as a drop target. Returns TRUE if we can accept the drop.
1430 static gboolean
drag_motion(GtkWidget
*widget
,
1431 GdkDragContext
*context
,
1437 GdkDragAction action
= context
->suggested_action
;
1438 const char *type
= NULL
;
1439 Icon
*icon
= (Icon
*) pi
;
1440 DirItem
*item
= icon
->item
;
1442 if (gtk_drag_get_source_widget(context
) == widget
)
1443 goto out
; /* Can't drag something to itself! */
1446 goto out
; /* Can't drag a selection to itself */
1448 type
= dnd_motion_item(context
, &item
);
1453 /* We actually must pretend to accept the drop, even if the
1454 * directory isn't writeable, so that the spring-opening
1458 /* Don't allow drops to non-writeable directories */
1459 if (o_dnd_spring_open
.int_value
== FALSE
&&
1460 type
== drop_dest_dir
&&
1461 access(icon
->path
, W_OK
) != 0)
1466 g_dataset_set_data(context
, "drop_dest_type", (gpointer
) type
);
1469 gdk_drag_status(context
, action
, time
);
1470 g_dataset_set_data_full(context
, "drop_dest_path",
1471 g_strdup(icon
->path
), g_free
);
1472 if (type
== drop_dest_dir
)
1473 dnd_spring_load(context
, NULL
);
1475 pinboard_wink_item(pi
, FALSE
);
1478 gdk_drag_status(context
, 0, time
);
1480 /* Always return TRUE to stop the pinboard getting the events */
1484 static gboolean pinboard_shadow
= FALSE
;
1485 static gint shadow_x
, shadow_y
;
1486 #define SHADOW_SIZE (ICON_WIDTH)
1488 static gboolean
bg_expose(GtkWidget
*widget
,
1489 GdkEventExpose
*event
, gpointer data
)
1491 gpointer gclass
= ((GTypeInstance
*) widget
)->g_class
;
1493 /* TODO: Split large regions into smaller chunks... */
1495 gdk_window_begin_paint_region(widget
->window
, event
->region
);
1497 if (pinboard_shadow
)
1499 gdk_draw_rectangle(widget
->window
,
1500 widget
->style
->white_gc
, FALSE
,
1502 SHADOW_SIZE
, SHADOW_SIZE
);
1503 gdk_draw_rectangle(widget
->window
,
1504 widget
->style
->black_gc
, FALSE
,
1505 shadow_x
+ 1, shadow_y
+ 1,
1506 SHADOW_SIZE
- 2, SHADOW_SIZE
- 2);
1509 if (lasso_in_progress
)
1513 area
.x
= MIN(lasso_rect_x1
, lasso_rect_x2
);
1514 area
.y
= MIN(lasso_rect_y1
, lasso_rect_y2
);
1515 area
.width
= ABS(lasso_rect_x1
- lasso_rect_x2
);
1516 area
.height
= ABS(lasso_rect_y1
- lasso_rect_y2
);
1518 if (area
.width
> 4 && area
.height
> 4)
1520 gdk_draw_rectangle(widget
->window
,
1521 widget
->style
->white_gc
, FALSE
,
1523 area
.width
- 1, area
.height
- 1);
1524 gdk_draw_rectangle(widget
->window
,
1525 widget
->style
->black_gc
, FALSE
,
1526 area
.x
+ 1, area
.y
+ 1,
1527 area
.width
- 3, area
.height
- 3);
1531 ((GtkWidgetClass
*) gclass
)->expose_event(widget
, event
);
1533 gdk_window_end_paint(widget
->window
);
1538 /* Draw a 'shadow' under an icon being dragged, showing where
1541 static void pinboard_set_shadow(gboolean on
)
1545 if (pinboard_shadow
)
1549 area
.width
= SHADOW_SIZE
+ 1;
1550 area
.height
= SHADOW_SIZE
+ 1;
1552 gdk_window_invalidate_rect(current_pinboard
->window
->window
,
1558 int old_x
= shadow_x
, old_y
= shadow_y
;
1560 gdk_window_get_pointer(current_pinboard
->fixed
->window
,
1561 &shadow_x
, &shadow_y
, NULL
);
1562 snap_to_grid(&shadow_x
, &shadow_y
);
1563 shadow_x
-= SHADOW_SIZE
/ 2;
1564 shadow_y
-= SHADOW_SIZE
/ 2;
1567 if (pinboard_shadow
&& shadow_x
== old_x
&& shadow_y
== old_y
)
1572 area
.width
= SHADOW_SIZE
+ 1;
1573 area
.height
= SHADOW_SIZE
+ 1;
1575 gdk_window_invalidate_rect(current_pinboard
->window
->window
,
1579 pinboard_shadow
= on
;
1582 /* Called when dragging some pinboard icons finishes */
1583 void pinboard_move_icons(void)
1585 int x
= shadow_x
, y
= shadow_y
;
1586 PinIcon
*pi
= (PinIcon
*) pinboard_drag_in_progress
;
1591 g_return_if_fail(pi
!= NULL
);
1593 x
+= SHADOW_SIZE
/ 2;
1594 y
+= SHADOW_SIZE
/ 2;
1595 snap_to_grid(&x
, &y
);
1597 if (pi
->x
== x
&& pi
->y
== y
)
1600 /* Find out how much the dragged icon moved (after snapping).
1601 * Move all selected icons by the same amount.
1606 /* Move the other selected icons to keep the same relative
1609 for (next
= icon_selection
; next
; next
= next
->next
)
1611 PinIcon
*pi
= (PinIcon
*) next
->data
;
1614 g_return_if_fail(IS_PIN_ICON(pi
));
1621 gdk_drawable_get_size(pi
->win
->window
, &width
, &height
);
1622 offset_from_centre(pi
, &nx
, &ny
);
1624 fixed_move_fast(GTK_FIXED(current_pinboard
->fixed
),
1631 static void drag_leave(GtkWidget
*widget
,
1632 GdkDragContext
*context
,
1636 pinboard_wink_item(NULL
, FALSE
);
1640 static gboolean
bg_drag_leave(GtkWidget
*widget
,
1641 GdkDragContext
*context
,
1645 pinboard_set_shadow(FALSE
);
1649 static gboolean
bg_drag_motion(GtkWidget
*widget
,
1650 GdkDragContext
*context
,
1656 /* Dragging from the pinboard to the pinboard is not allowed */
1658 if (!provides(context
, text_uri_list
))
1661 pinboard_set_shadow(TRUE
);
1663 gdk_drag_status(context
,
1664 context
->suggested_action
== GDK_ACTION_ASK
1665 ? GDK_ACTION_LINK
: context
->suggested_action
,
1670 static void drag_end(GtkWidget
*widget
,
1671 GdkDragContext
*context
,
1674 pinboard_drag_in_progress
= NULL
;
1675 if (tmp_icon_selected
)
1677 icon_select_only(NULL
);
1678 tmp_icon_selected
= FALSE
;
1682 /* Something which affects all the icons has changed - reshape
1683 * and redraw all of them.
1685 static void reshape_all(void)
1689 g_return_if_fail(current_pinboard
!= NULL
);
1691 for (next
= current_pinboard
->icons
; next
; next
= next
->next
)
1693 Icon
*icon
= (Icon
*) next
->data
;
1694 pinboard_reshape_icon(icon
);
1698 /* Turns off the pinboard. Does not call gtk_main_quit. */
1699 static void pinboard_clear(void)
1703 g_return_if_fail(current_pinboard
!= NULL
);
1705 tasklist_set_active(FALSE
);
1707 next
= current_pinboard
->icons
;
1710 PinIcon
*pi
= (PinIcon
*) next
->data
;
1714 gtk_widget_destroy(pi
->win
);
1717 gtk_widget_destroy(current_pinboard
->window
);
1719 abandon_backdrop_app(current_pinboard
);
1721 g_object_unref(current_pinboard
->shadow_gc
);
1722 current_pinboard
->shadow_gc
= NULL
;
1724 g_free(current_pinboard
->name
);
1725 null_g_free(¤t_pinboard
);
1727 number_of_windows
--;
1730 static gpointer parent_class
;
1732 static void pin_icon_destroy(Icon
*icon
)
1734 PinIcon
*pi
= (PinIcon
*) icon
;
1736 g_return_if_fail(pi
->win
!= NULL
);
1738 gtk_widget_destroy(pi
->win
);
1741 static void pinboard_remove_items(void)
1743 g_return_if_fail(icon_selection
!= NULL
);
1745 while (icon_selection
)
1746 icon_destroy((Icon
*) icon_selection
->data
);
1751 static void pin_icon_update(Icon
*icon
)
1753 pinboard_reshape_icon(icon
);
1757 static gboolean
pin_icon_same_group(Icon
*icon
, Icon
*other
)
1759 return IS_PIN_ICON(other
);
1762 static void pin_wink_icon(Icon
*icon
)
1764 pinboard_wink_item((PinIcon
*) icon
, TRUE
);
1767 static void pin_icon_class_init(gpointer gclass
, gpointer data
)
1769 IconClass
*icon
= (IconClass
*) gclass
;
1771 parent_class
= g_type_class_peek_parent(gclass
);
1773 icon
->destroy
= pin_icon_destroy
;
1774 icon
->redraw
= pinboard_reshape_icon
;
1775 icon
->update
= pin_icon_update
;
1776 icon
->wink
= pin_wink_icon
;
1777 icon
->remove_items
= pinboard_remove_items
;
1778 icon
->same_group
= pin_icon_same_group
;
1781 static void pin_icon_init(GTypeInstance
*object
, gpointer gclass
)
1785 static GType
pin_icon_get_type(void)
1787 static GType type
= 0;
1791 static const GTypeInfo info
=
1793 sizeof (PinIconClass
),
1794 NULL
, /* base_init */
1795 NULL
, /* base_finalise */
1796 pin_icon_class_init
,
1797 NULL
, /* class_finalise */
1798 NULL
, /* class_data */
1800 0, /* n_preallocs */
1804 type
= g_type_register_static(icon_get_type(),
1805 "PinIcon", &info
, 0);
1811 static PinIcon
*pin_icon_new(const char *pathname
, const char *name
)
1816 pi
= g_object_new(pin_icon_get_type(), NULL
);
1819 icon_set_path(icon
, pathname
, name
);
1824 /* Called when the window widget is somehow destroyed */
1825 static void pin_icon_destroyed(PinIcon
*pi
)
1827 g_return_if_fail(pi
->win
!= NULL
);
1831 pinboard_wink_item(NULL
, FALSE
);
1833 if (pinboard_drag_in_progress
== (Icon
*) pi
)
1834 pinboard_drag_in_progress
= NULL
;
1836 if (current_pinboard
)
1837 current_pinboard
->icons
=
1838 g_list_remove(current_pinboard
->icons
, pi
);
1843 /* Set the tooltip */
1844 static void pin_icon_set_tip(PinIcon
*pi
)
1848 Icon
*icon
= (Icon
*) pi
;
1850 g_return_if_fail(pi
!= NULL
);
1852 ai
= appinfo_get(icon
->path
, icon
->item
);
1854 if (ai
&& ((node
= xml_get_section(ai
, NULL
, "Summary"))))
1857 str
= xmlNodeListGetString(node
->doc
,
1858 node
->xmlChildrenNode
, 1);
1861 gtk_tooltips_set_tip(tooltips
, pi
->win
, str
, NULL
);
1866 gtk_tooltips_set_tip(tooltips
, pi
->widget
, NULL
, NULL
);
1872 static void pinboard_show_menu(GdkEventButton
*event
, PinIcon
*pi
)
1876 pos
[0] = event
->x_root
;
1877 pos
[1] = event
->y_root
;
1880 icon_prepare_menu((Icon
*) pi
, TRUE
);
1882 gtk_menu_popup(GTK_MENU(icon_menu
), NULL
, NULL
,
1884 (gpointer
) pos
, event
->button
, event
->time
);
1887 static void create_pinboard_window(Pinboard
*pinboard
)
1891 g_return_if_fail(pinboard
->window
== NULL
);
1893 win
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1894 gtk_widget_set_style(win
, gtk_widget_get_default_style());
1896 gtk_widget_set_double_buffered(win
, FALSE
);
1897 gtk_widget_set_app_paintable(win
, TRUE
);
1898 gtk_widget_set_name(win
, "rox-pinboard");
1899 pinboard
->window
= win
;
1900 pinboard
->fixed
= gtk_fixed_new();
1901 gtk_container_add(GTK_CONTAINER(win
), pinboard
->fixed
);
1903 gtk_window_set_wmclass(GTK_WINDOW(win
), "ROX-Pinboard", PROJECT
);
1905 gtk_widget_set_size_request(win
, screen_width
, screen_height
);
1906 gtk_widget_realize(win
);
1907 gtk_window_move(GTK_WINDOW(win
), 0, 0);
1908 make_panel_window(win
);
1910 /* TODO: Use gdk function when it supports this type */
1912 GdkAtom desktop_type
;
1914 desktop_type
= gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
1916 gdk_property_change(win
->window
,
1917 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE
),
1918 gdk_atom_intern("ATOM", FALSE
), 32,
1919 GDK_PROP_MODE_REPLACE
, (guchar
*) &desktop_type
, 1);
1922 gtk_widget_add_events(win
,
1923 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
1925 GDK_BUTTON1_MOTION_MASK
|
1926 GDK_BUTTON2_MOTION_MASK
| GDK_BUTTON3_MOTION_MASK
);
1927 g_signal_connect(win
, "button-press-event",
1928 G_CALLBACK(button_press_event
), NULL
);
1929 g_signal_connect(win
, "button-release-event",
1930 G_CALLBACK(button_release_event
), NULL
);
1931 g_signal_connect(win
, "motion-notify-event",
1932 G_CALLBACK(lasso_motion
), NULL
);
1933 g_signal_connect(pinboard
->fixed
, "expose_event",
1934 G_CALLBACK(bg_expose
), NULL
);
1936 /* Drag and drop handlers */
1937 drag_set_pinboard_dest(win
);
1938 g_signal_connect(win
, "drag_motion", G_CALLBACK(bg_drag_motion
), NULL
);
1939 g_signal_connect(win
, "drag_leave", G_CALLBACK(bg_drag_leave
), NULL
);
1941 pinboard
->shadow_gc
= gdk_gc_new(win
->window
);
1942 gdk_gc_set_rgb_fg_color(pinboard
->shadow_gc
, &pin_text_shadow_col
);
1944 reload_backdrop(current_pinboard
, NULL
, BACKDROP_NONE
);
1946 gtk_widget_show_all(win
);
1947 gdk_window_lower(win
->window
);
1950 /* Load image 'path' and scale according to 'style' */
1951 static GdkPixmap
*load_backdrop(const gchar
*path
, BackdropStyle style
)
1955 GError
*error
= NULL
;
1957 pixbuf
= gdk_pixbuf_new_from_file(path
, &error
);
1960 delayed_error(_("Error loading backdrop image:\n%s\n"
1961 "Backdrop removed."),
1963 g_error_free(error
);
1964 set_backdrop(NULL
, BACKDROP_NONE
);
1968 if (style
== BACKDROP_SCALE
)
1970 GdkPixbuf
*old
= pixbuf
;
1972 pixbuf
= gdk_pixbuf_scale_simple(old
,
1973 screen_width
, screen_height
,
1976 g_object_unref(old
);
1978 else if (style
== BACKDROP_CENTRE
)
1980 GdkPixbuf
*old
= pixbuf
;
1981 int x
, y
, width
, height
;
1983 width
= gdk_pixbuf_get_width(pixbuf
);
1984 height
= gdk_pixbuf_get_height(pixbuf
);
1986 pixbuf
= gdk_pixbuf_new(
1987 gdk_pixbuf_get_colorspace(pixbuf
), 0,
1988 8, screen_width
, screen_height
);
1989 gdk_pixbuf_fill(pixbuf
, 0);
1991 x
= (screen_width
- width
) / 2;
1992 y
= (screen_height
- height
) / 2;
1996 gdk_pixbuf_composite(old
, pixbuf
,
1998 MIN(screen_width
, width
),
1999 MIN(screen_height
, height
),
2001 GDK_INTERP_NEAREST
, 255);
2002 g_object_unref(old
);
2005 gdk_pixbuf_render_pixmap_and_mask(pixbuf
,
2007 g_object_unref(pixbuf
);
2012 static void abandon_backdrop_app(Pinboard
*pinboard
)
2014 g_return_if_fail(pinboard
!= NULL
);
2016 if (pinboard
->to_backdrop_app
!= -1)
2018 close(pinboard
->to_backdrop_app
);
2019 close(pinboard
->from_backdrop_app
);
2020 gtk_input_remove(pinboard
->input_tag
);
2021 g_string_free(pinboard
->input_buffer
, TRUE
);
2022 pinboard
->to_backdrop_app
= -1;
2023 pinboard
->from_backdrop_app
= -1;
2024 pinboard
->input_tag
= -1;
2025 pinboard
->input_buffer
= NULL
;
2028 g_return_if_fail(pinboard
->to_backdrop_app
== -1);
2029 g_return_if_fail(pinboard
->from_backdrop_app
== -1);
2030 g_return_if_fail(pinboard
->input_tag
== -1);
2031 g_return_if_fail(pinboard
->input_buffer
== NULL
);
2034 /* A single line has been read from the child.
2035 * Processes the command, and replies 'ok' (or abandons the child on error).
2037 static void command_from_backdrop_app(Pinboard
*pinboard
, const gchar
*command
)
2039 BackdropStyle style
;
2040 const char *ok
= "ok\n";
2042 if (strncmp(command
, "tile ", 5) == 0)
2044 style
= BACKDROP_TILE
;
2047 else if (strncmp(command
, "scale ", 6) == 0)
2049 style
= BACKDROP_SCALE
;
2052 else if (strncmp(command
, "centre ", 7) == 0)
2054 style
= BACKDROP_CENTRE
;
2059 g_warning("Invalid command '%s' from backdrop app\n",
2061 abandon_backdrop_app(pinboard
);
2065 /* Load the backdrop. May abandon the program if loading fails. */
2066 reload_backdrop(pinboard
, command
, style
);
2068 if (pinboard
->to_backdrop_app
== -1)
2075 sent
= write(pinboard
->to_backdrop_app
, ok
, strlen(ok
));
2078 /* Remote app quit? Not an error. */
2079 abandon_backdrop_app(pinboard
);
2086 static void backdrop_from_child(Pinboard
*pinboard
,
2087 int src
, GdkInputCondition cond
)
2092 got
= read(src
, buf
, sizeof(buf
));
2097 g_warning("backdrop_from_child: %s\n",
2099 abandon_backdrop_app(pinboard
);
2103 g_string_append_len(pinboard
->input_buffer
, buf
, got
);
2105 while (pinboard
->from_backdrop_app
!= -1)
2110 nl
= strchr(pinboard
->input_buffer
->str
, '\n');
2112 return; /* Haven't got a whole line yet */
2114 len
= nl
- pinboard
->input_buffer
->str
;
2115 command
= g_strndup(pinboard
->input_buffer
->str
, len
);
2116 g_string_erase(pinboard
->input_buffer
, 0, len
+ 1);
2118 command_from_backdrop_app(pinboard
, command
);
2124 static void reload_backdrop(Pinboard
*pinboard
,
2125 const gchar
*backdrop
,
2126 BackdropStyle backdrop_style
)
2130 if (backdrop
&& backdrop_style
== BACKDROP_PROGRAM
)
2132 const char *argv
[] = {NULL
, "--backdrop", NULL
};
2133 GError
*error
= NULL
;
2135 g_return_if_fail(pinboard
->to_backdrop_app
== -1);
2136 g_return_if_fail(pinboard
->from_backdrop_app
== -1);
2137 g_return_if_fail(pinboard
->input_tag
== -1);
2138 g_return_if_fail(pinboard
->input_buffer
== NULL
);
2140 argv
[0] = make_path(backdrop
, "AppRun");
2142 /* Run the program. It'll send us a SOAP message and we'll
2143 * get back here with a different style and image.
2146 if (g_spawn_async_with_pipes(NULL
, (gchar
**) argv
, NULL
,
2147 G_SPAWN_DO_NOT_REAP_CHILD
|
2148 G_SPAWN_SEARCH_PATH
,
2149 NULL
, NULL
, /* Child setup fn */
2150 NULL
, /* Child PID */
2151 &pinboard
->to_backdrop_app
,
2152 &pinboard
->from_backdrop_app
,
2153 NULL
, /* Standard error */
2156 pinboard
->input_buffer
= g_string_new(NULL
);
2157 pinboard
->input_tag
= gtk_input_add_full(
2158 pinboard
->from_backdrop_app
,
2160 (GdkInputFunction
) backdrop_from_child
,
2161 NULL
, pinboard
, NULL
);
2165 delayed_error("%s", error
? error
->message
: "(null)");
2166 g_error_free(error
);
2171 /* Note: Copying a style does not ref the pixmaps! */
2173 style
= gtk_style_copy(gtk_widget_get_style(pinboard
->window
));
2174 style
->bg_pixmap
[GTK_STATE_NORMAL
] = NULL
;
2177 style
->bg_pixmap
[GTK_STATE_NORMAL
] =
2178 load_backdrop(backdrop
, backdrop_style
);
2180 gdk_color_parse(o_pinboard_bg_colour
.value
,
2181 &style
->bg
[GTK_STATE_NORMAL
]);
2183 gtk_widget_set_style(pinboard
->window
, style
);
2185 g_object_unref(style
);
2187 gtk_widget_queue_draw(pinboard
->window
);
2189 /* Also update root window property (for transparent xterms, etc) */
2190 if (style
->bg_pixmap
[GTK_STATE_NORMAL
])
2192 XID id
= GDK_DRAWABLE_XID(style
->bg_pixmap
[GTK_STATE_NORMAL
]);
2193 gdk_property_change(gdk_get_default_root_window(),
2194 gdk_atom_intern("_XROOTPMAP_ID", FALSE
),
2195 gdk_atom_intern("PIXMAP", FALSE
),
2196 32, GDK_PROP_MODE_REPLACE
,
2201 gdk_property_delete(gdk_get_default_root_window(),
2202 gdk_atom_intern("_XROOTPMAP_ID", FALSE
));
2206 /* Set and save (path, style) as the new backdrop.
2207 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
2208 * Otherwise, the image is displayed now.
2210 static void set_backdrop(const gchar
*path
, BackdropStyle style
)
2212 g_return_if_fail((path
== NULL
&& style
== BACKDROP_NONE
) ||
2213 (path
!= NULL
&& style
!= BACKDROP_NONE
));
2215 if (!current_pinboard
)
2219 pinboard_activate("Default");
2220 delayed_error(_("No pinboard was in use... "
2221 "the 'Default' pinboard has been selected. "
2222 "Use 'rox -p=Default' to turn it on in "
2224 g_return_if_fail(current_pinboard
!= NULL
);
2227 /* We might have just run the old backdrop program and now
2228 * we're going to set a new one! Seems a bit mean...
2231 abandon_backdrop_app(current_pinboard
);
2233 g_free(current_pinboard
->backdrop
);
2234 current_pinboard
->backdrop
= g_strdup(path
);
2235 current_pinboard
->backdrop_style
= style
;
2236 reload_backdrop(current_pinboard
,
2237 current_pinboard
->backdrop
,
2238 current_pinboard
->backdrop_style
);
2243 #define SEARCH_STEP 32
2245 static void search_free(GdkRectangle
*rect
, GdkRegion
*used
,
2246 int *outer
, int od
, int omax
,
2247 int *inner
, int id
, int imax
)
2249 *outer
= od
> 0 ? 0 : omax
;
2250 while (*outer
>= 0 && *outer
<= omax
)
2252 *inner
= id
> 0 ? 0 : imax
;
2253 while (*inner
>= 0 && *inner
<= imax
)
2255 if (gdk_region_rect_in(used
, rect
) ==
2256 GDK_OVERLAP_RECTANGLE_OUT
)
2268 /* Finds a free area on the pinboard large enough for the width and height
2269 * of the given rectangle, by filling in the x and y fields of 'rect'.
2270 * The search order respects user preferences.
2271 * If no area is free, returns any old area.
2273 static void find_free_rect(Pinboard
*pinboard
, GdkRectangle
*rect
)
2277 GdkRectangle used_rect
;
2278 int dx
= SEARCH_STEP
, dy
= SEARCH_STEP
;
2280 used
= gdk_region_new();
2282 panel_mark_used(used
);
2284 /* Subtract the used areas... */
2286 next
= GTK_FIXED(pinboard
->fixed
)->children
;
2287 for (; next
; next
= next
->next
)
2289 GtkFixedChild
*fix
= (GtkFixedChild
*) next
->data
;
2291 if (!GTK_WIDGET_VISIBLE(fix
->widget
))
2294 used_rect
.x
= fix
->x
;
2295 used_rect
.y
= fix
->y
;
2296 used_rect
.width
= fix
->widget
->requisition
.width
;
2297 used_rect
.height
= fix
->widget
->requisition
.height
;
2299 gdk_region_union_with_rect(used
, &used_rect
);
2302 /* Find the first free area (yes, this isn't exactly pretty, but
2303 * it works). If you know a better (fast!) algorithm, let me know!
2307 if (o_iconify_start
.int_value
== CORNER_TOP_RIGHT
||
2308 o_iconify_start
.int_value
== CORNER_BOTTOM_RIGHT
)
2311 if (o_iconify_start
.int_value
== CORNER_BOTTOM_LEFT
||
2312 o_iconify_start
.int_value
== CORNER_BOTTOM_RIGHT
)
2315 if (o_iconify_dir
.int_value
== DIR_VERT
)
2317 search_free(rect
, used
,
2318 &rect
->x
, dx
, screen_width
- rect
->width
,
2319 &rect
->y
, dy
, screen_height
- rect
->height
);
2323 search_free(rect
, used
,
2324 &rect
->y
, dy
, screen_height
- rect
->height
,
2325 &rect
->x
, dx
, screen_width
- rect
->width
);
2328 gdk_region_destroy(used
);
2337 /* Icon's size, shape or appearance has changed - update the display */
2338 static void pinboard_reshape_icon(Icon
*icon
)
2340 PinIcon
*pi
= (PinIcon
*) icon
;
2341 int x
= pi
->x
, y
= pi
->y
;
2343 set_size_and_style(pi
);
2344 offset_from_centre(pi
, &x
, &y
);
2346 if (pi
->win
->allocation
.x
!= x
|| pi
->win
->allocation
.y
!= y
)
2348 fixed_move_fast(GTK_FIXED(current_pinboard
->fixed
),
2353 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2354 static void update_pinboard_font(void)
2357 pango_font_description_free(pinboard_font
);
2358 pinboard_font
= o_label_font
.value
[0] != '\0'
2359 ? pango_font_description_from_string(o_label_font
.value
)