r3562: Remember position of minimized windows on pinboard
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob528412387eeeffbba35644a173dae0c930c76831
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* pinboard.c - icons on the desktop background */
24 #include "config.h"
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <libxml/parser.h>
35 #include <signal.h>
37 #include "global.h"
39 #include "pinboard.h"
40 #include "main.h"
41 #include "dnd.h"
42 #include "pixmaps.h"
43 #include "type.h"
44 #include "choices.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "diritem.h"
49 #include "bind.h"
50 #include "icon.h"
51 #include "run.h"
52 #include "appinfo.h"
53 #include "menu.h"
54 #include "xml.h"
55 #include "tasklist.h"
56 #include "panel.h" /* For panel_mark_used() */
57 #include "wrapped.h"
58 #include "dropbox.h"
60 static gboolean tmp_icon_selected = FALSE; /* When dragging */
62 static GtkWidget *set_backdrop_dialog = NULL;
64 struct _Pinboard {
65 guchar *name; /* Leaf name */
66 GList *icons;
67 GtkStyle *style;
69 gchar *backdrop; /* Pathname */
70 BackdropStyle backdrop_style;
71 gint to_backdrop_app; /* pipe FD, or -1 */
72 gint from_backdrop_app; /* pipe FD, or -1 */
73 gint input_tag;
74 GString *input_buffer;
76 GtkWidget *window; /* Screen-sized window */
77 GtkWidget *fixed;
78 GdkGC *shadow_gc;
81 #define IS_PIN_ICON(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), pin_icon_get_type())
83 typedef struct _PinIconClass PinIconClass;
84 typedef struct _PinIcon PinIcon;
86 struct _PinIconClass {
87 IconClass parent;
90 struct _PinIcon {
91 Icon icon;
93 int x, y;
94 GtkWidget *win;
95 GtkWidget *widget; /* The drawing area for the icon */
96 GtkWidget *label;
99 /* The number of pixels between the bottom of the image and the top
100 * of the text.
102 #define GAP 4
104 /* The size of the border around the icon which is used when winking */
105 #define WINK_FRAME 2
107 /* Grid sizes */
108 #define GRID_STEP_FINE 2
109 #define GRID_STEP_MED 16
110 #define GRID_STEP_COARSE 32
112 /* Used in options */
113 #define CORNER_TOP_LEFT 0
114 #define CORNER_TOP_RIGHT 1
115 #define CORNER_BOTTOM_LEFT 2
116 #define CORNER_BOTTOM_RIGHT 3
118 #define DIR_HORZ 0
119 #define DIR_VERT 1
121 static PinIcon *current_wink_icon = NULL;
122 static gint wink_timeout;
124 /* Used for the text colours (only) in the icons */
125 GdkColor pin_text_fg_col, pin_text_bg_col;
126 PangoFontDescription *pinboard_font = NULL; /* NULL => Gtk default */
128 static GdkColor pin_text_shadow_col;
130 Pinboard *current_pinboard = NULL;
131 static gint loading_pinboard = 0; /* Non-zero => loading */
133 /* The Icon that was used to start the current drag, if any */
134 Icon *pinboard_drag_in_progress = NULL;
136 /* For selecting groups of icons */
137 static gboolean lasso_in_progress = FALSE;
138 static int lasso_rect_x1, lasso_rect_x2;
139 static int lasso_rect_y1, lasso_rect_y2;
141 /* Tracking icon positions */
142 static GHashTable *placed_icons=NULL;
144 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
145 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
146 static Option o_pinboard_tasklist, o_forward_buttons_13;
147 static Option o_iconify_start, o_iconify_dir;
148 static Option o_label_font, o_pinboard_shadow_colour;
149 static Option o_pinboard_shadow_labels;
150 static Option o_blackbox_hack;
152 static Option o_top_margin, o_bottom_margin;
154 /* Static prototypes */
155 static GType pin_icon_get_type(void);
156 static void set_size_and_style(PinIcon *pi);
157 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
158 static gint end_wink(gpointer data);
159 static gboolean button_release_event(GtkWidget *widget,
160 GdkEventButton *event,
161 PinIcon *pi);
162 static gboolean enter_notify(GtkWidget *widget,
163 GdkEventCrossing *event,
164 PinIcon *pi);
165 static gint leave_notify(GtkWidget *widget,
166 GdkEventCrossing *event,
167 PinIcon *pi);
168 static gboolean button_press_event(GtkWidget *widget,
169 GdkEventButton *event,
170 PinIcon *pi);
171 static gboolean scroll_event(GtkWidget *widget,
172 GdkEventScroll *event);
173 static gint icon_motion_notify(GtkWidget *widget,
174 GdkEventMotion *event,
175 PinIcon *pi);
176 static const char *pin_from_file(gchar *line);
177 static void snap_to_grid(int *x, int *y);
178 static void offset_from_centre(PinIcon *pi, int *x, int *y);
179 static gboolean drag_motion(GtkWidget *widget,
180 GdkDragContext *context,
181 gint x,
182 gint y,
183 guint time,
184 PinIcon *pi);
185 static void drag_set_pinicon_dest(PinIcon *pi);
186 static void drag_leave(GtkWidget *widget,
187 GdkDragContext *context,
188 guint32 time,
189 PinIcon *pi);
190 static gboolean bg_drag_motion(GtkWidget *widget,
191 GdkDragContext *context,
192 gint x,
193 gint y,
194 guint time,
195 gpointer data);
196 static gboolean bg_drag_leave(GtkWidget *widget,
197 GdkDragContext *context,
198 guint32 time,
199 gpointer data);
200 static gboolean bg_expose(GtkWidget *window,
201 GdkEventExpose *event, gpointer data);
202 static void drag_end(GtkWidget *widget,
203 GdkDragContext *context,
204 PinIcon *pi);
205 static void reshape_all(void);
206 static void pinboard_check_options(void);
207 static void pinboard_load_from_xml(xmlDocPtr doc);
208 static void pinboard_clear(void);
209 static void pinboard_save(void);
210 static PinIcon *pin_icon_new(const char *pathname, const char *name);
211 static void pin_icon_destroyed(PinIcon *pi);
212 static void pin_icon_set_tip(PinIcon *pi);
213 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
214 static void create_pinboard_window(Pinboard *pinboard);
215 static void reload_backdrop(Pinboard *pinboard,
216 const gchar *backdrop,
217 BackdropStyle backdrop_style);
218 static void pinboard_reshape_icon(Icon *icon);
219 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
220 static void abandon_backdrop_app(Pinboard *pinboard);
221 static void drag_backdrop_dropped(GtkWidget *drop_box,
222 const guchar *path,
223 GtkWidget *dialog);
224 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
225 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
226 gboolean old);
227 static void update_pinboard_font(void);
228 static void draw_lasso(void);
229 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d);
230 static void clear_backdrop(GtkWidget *drop_box, gpointer data);
231 static void radios_changed(gpointer data);
232 static void update_radios(GtkWidget *dialog);
234 /****************************************************************
235 * EXTERNAL INTERFACE *
236 ****************************************************************/
238 void pinboard_init(void)
240 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
241 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
242 option_add_string(&o_pinboard_shadow_colour, "pinboard_shadow_colour",
243 "#000");
244 option_add_string(&o_label_font, "label_font", "");
245 option_add_int(&o_pinboard_shadow_labels, "pinboard_shadow_labels", 1);
247 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
248 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
249 GRID_STEP_COARSE);
250 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
251 option_add_int(&o_forward_buttons_13, "pinboard_forward_buttons_13",
252 FALSE);
254 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
255 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
257 option_add_int(&o_blackbox_hack, "blackbox_hack", FALSE);
259 option_add_int(&o_top_margin, "pinboard_top_margin", 0);
260 option_add_int(&o_bottom_margin, "pinboard_bottom_margin", 0);
262 option_add_notify(pinboard_check_options);
264 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
265 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
266 gdk_color_parse(o_pinboard_shadow_colour.value, &pin_text_shadow_col);
267 update_pinboard_font();
269 placed_icons=g_hash_table_new(g_str_hash, g_str_equal);
272 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
273 * and make it the current pinboard.
274 * Any existing pinned items are removed. You must call this
275 * at least once before using the pinboard. NULL disables the
276 * pinboard.
278 void pinboard_activate(const gchar *name)
280 Pinboard *old_board = current_pinboard;
281 guchar *path, *slash;
283 /* Treat an empty name the same as NULL */
284 if (name && !*name)
285 name = NULL;
287 if (old_board)
288 pinboard_clear();
290 if (!name)
292 if (number_of_windows < 1 && gtk_main_level() > 0)
293 gtk_main_quit();
295 gdk_property_delete(gdk_get_default_root_window(),
296 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
297 return;
300 number_of_windows++;
302 slash = strchr(name, '/');
303 if (slash)
305 if (access(name, F_OK))
306 path = NULL; /* File does not (yet) exist */
307 else
308 path = g_strdup(name);
310 else
312 guchar *leaf;
314 leaf = g_strconcat("pb_", name, NULL);
315 path = choices_find_path_load(leaf, PROJECT);
316 g_free(leaf);
319 current_pinboard = g_new(Pinboard, 1);
320 current_pinboard->name = g_strdup(name);
321 current_pinboard->icons = NULL;
322 current_pinboard->window = NULL;
323 current_pinboard->backdrop = NULL;
324 current_pinboard->backdrop_style = BACKDROP_NONE;
325 current_pinboard->to_backdrop_app = -1;
326 current_pinboard->from_backdrop_app = -1;
327 current_pinboard->input_tag = -1;
328 current_pinboard->input_buffer = NULL;
330 create_pinboard_window(current_pinboard);
332 loading_pinboard++;
333 if (path)
335 xmlDocPtr doc;
336 doc = xmlParseFile(path);
337 if (doc)
339 pinboard_load_from_xml(doc);
340 xmlFreeDoc(doc);
341 reload_backdrop(current_pinboard,
342 current_pinboard->backdrop,
343 current_pinboard->backdrop_style);
345 else
347 parse_file(path, pin_from_file);
348 info_message(_("Your old pinboard file has been "
349 "converted to the new XML format."));
350 pinboard_save();
352 g_free(path);
354 else
355 pinboard_pin(home_dir, "Home",
356 4 + ICON_WIDTH / 2,
357 4 + ICON_HEIGHT / 2,
358 NULL);
359 loading_pinboard--;
361 if (o_pinboard_tasklist.int_value)
362 tasklist_set_active(TRUE);
365 /* Return the window of the current pinboard, or NULL.
366 * Used to make sure lowering the panels doesn't lose them...
368 GdkWindow *pinboard_get_window(void)
370 if (current_pinboard)
371 return current_pinboard->window->window;
372 return NULL;
375 const char *pinboard_get_name(void)
377 g_return_val_if_fail(current_pinboard != NULL, NULL);
379 return current_pinboard->name;
382 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
383 * being cleared.
385 void pinboard_add_widget(GtkWidget *widget, const gchar *name)
387 GtkRequisition req;
388 GdkRectangle *rect=NULL;
389 gboolean found=FALSE;
391 g_return_if_fail(current_pinboard != NULL);
393 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, 0, 0);
395 gtk_widget_size_request(widget, &req);
397 if(name) {
398 rect=g_hash_table_lookup(placed_icons, name);
399 if(rect)
400 found=TRUE;
402 /*printf("%s at %p %d\n", name? name: "(nil)", rect, found);*/
404 if(rect) {
405 if(rect->width<req.width || rect->height<req.height) {
406 found=FALSE;
408 } else {
409 rect=g_new(GdkRectangle, 1);
410 rect->width = req.width;
411 rect->height = req.height;
413 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
414 rect->y, found);*/
415 find_free_rect(current_pinboard, rect, found);
416 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
417 rect->y, found);*/
419 gtk_fixed_move(GTK_FIXED(current_pinboard->fixed),
420 widget, rect->x, rect->y);
422 /* Store the new position (key and value are never freed) */
423 if(name)
424 g_hash_table_insert(placed_icons, g_strdup(name),
425 rect);
428 void pinboard_moved_widget(GtkWidget *widget, const gchar *name,
429 int x, int y)
431 GdkRectangle *rect;
433 if(!name)
434 return;
435 rect=g_hash_table_lookup(placed_icons, name);
436 if(!rect)
437 return;
439 rect->x=x;
440 rect->y=y;
443 /* Add a new icon to the background.
444 * 'path' should be an absolute pathname.
445 * 'x' and 'y' are the coordinates of the point in the middle of the text.
446 * 'name' is the name to use. If NULL then the leafname of path is used.
448 void pinboard_pin(const gchar *path, const gchar *name, int x, int y,
449 const gchar *shortcut)
451 GtkWidget *align, *vbox;
452 GdkWindow *events;
453 PinIcon *pi;
454 Icon *icon;
456 g_return_if_fail(path != NULL);
457 g_return_if_fail(current_pinboard != NULL);
459 pi = pin_icon_new(path, name);
460 icon = (Icon *) pi;
461 pi->x = x;
462 pi->y = y;
464 /* This is a bit complicated...
466 * An icon needs to be a NO_WINDOW widget so that the image can
467 * blend with the background (A ParentRelative window also works, but
468 * is slow, causes the xfree86's memory consumption to grow without
469 * bound, and doesn't even get freed when the filer quits!).
471 * However, the icon also needs to have a window, so we get events
472 * delivered correctly. The solution is to float an InputOnly window
473 * over the icon. Since GtkButton works the same way, we just use
474 * that :-)
477 /* Button takes the initial ref of Icon */
478 pi->win = gtk_button_new();
479 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
480 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
481 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
483 vbox = gtk_vbox_new(FALSE, 0);
484 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
486 align = gtk_alignment_new(0.5, 0.5, 0, 0);
487 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
488 gtk_container_add(GTK_CONTAINER(align), pi->widget);
490 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
491 drag_set_pinicon_dest(pi);
492 g_signal_connect(pi->win, "drag_data_get",
493 G_CALLBACK(drag_data_get), NULL);
495 pi->label = wrapped_label_new(icon->item->leafname, 180);
496 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
498 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
500 snap_to_grid(&x, &y);
501 pi->x = x;
502 pi->y = y;
503 gtk_widget_show_all(pi->win);
504 pinboard_reshape_icon((Icon *) pi);
506 gtk_widget_realize(pi->win);
507 events = GTK_BUTTON(pi->win)->event_window;
508 gdk_window_set_events(events,
509 GDK_EXPOSURE_MASK |
510 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
511 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
512 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
513 GDK_BUTTON3_MOTION_MASK);
514 g_signal_connect(pi->win, "enter-notify-event",
515 G_CALLBACK(enter_notify), pi);
516 g_signal_connect(pi->win, "leave-notify-event",
517 G_CALLBACK(leave_notify), pi);
518 g_signal_connect(pi->win, "button-press-event",
519 G_CALLBACK(button_press_event), pi);
520 g_signal_connect(pi->win, "button-release-event",
521 G_CALLBACK(button_release_event), pi);
522 g_signal_connect(pi->win, "motion-notify-event",
523 G_CALLBACK(icon_motion_notify), pi);
524 g_signal_connect(pi->win, "expose-event",
525 G_CALLBACK(draw_icon), pi);
526 g_signal_connect_swapped(pi->win, "style-set",
527 G_CALLBACK(pinboard_reshape_icon), pi);
528 g_signal_connect_swapped(pi->win, "destroy",
529 G_CALLBACK(pin_icon_destroyed), pi);
531 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
532 pin_icon_set_tip(pi);
534 icon_set_shortcut(icon, shortcut);
536 if (!loading_pinboard)
537 pinboard_save();
540 /* Put a border around the icon, briefly.
541 * If icon is NULL then cancel any existing wink.
542 * The icon will automatically unhighlight unless timeout is FALSE,
543 * in which case you must call this function again (with NULL or another
544 * icon) to remove the highlight.
546 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
548 PinIcon *old = current_wink_icon;
550 if (old == pi)
551 return;
553 current_wink_icon = pi;
555 if (old)
557 gtk_widget_queue_draw(old->win);
558 gdk_window_process_updates(old->widget->window, TRUE);
560 if (wink_timeout != -1)
561 gtk_timeout_remove(wink_timeout);
564 if (pi)
566 gtk_widget_queue_draw(pi->win);
567 gdk_window_process_updates(pi->widget->window, TRUE);
569 if (timeout)
570 wink_timeout = gtk_timeout_add(300, end_wink, NULL);
571 else
572 wink_timeout = -1;
576 /* 'app' is saved as the new application to set the backdrop. It will then be
577 * run, and should communicate with the filer as described in the manual.
579 void pinboard_set_backdrop_app(const gchar *app)
581 XMLwrapper *ai;
582 DirItem *item;
583 gboolean can_set;
585 item = diritem_new("");
586 diritem_restat(app, item, NULL);
587 if (!(item->flags & ITEM_FLAG_APPDIR))
589 delayed_error(_("The backdrop handler must be an application "
590 "directory. Drag an application directory "
591 "into the Set Backdrop dialog box, or (for "
592 "programmers) pass it to the SOAP "
593 "SetBackdropApp method."));
594 diritem_free(item);
595 return;
598 ai = appinfo_get(app, item);
599 diritem_free(item);
601 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
602 if (ai)
603 g_object_unref(ai);
605 if (can_set)
606 pinboard_set_backdrop(app, BACKDROP_PROGRAM);
607 else
608 delayed_error(_("You can only set the backdrop to an image "
609 "or to a program which knows how to "
610 "manage ROX-Filer's backdrop.\n\n"
611 "Programmers: the application's AppInfo.xml "
612 "must contain the CanSetBackdrop element, as "
613 "described in ROX-Filer's manual."));
616 /* Open a dialog box allowing the user to set the backdrop */
617 void pinboard_set_backdrop_box(void)
619 GtkWidget *dialog, *frame, *label, *hbox;
620 GtkBox *vbox;
621 Radios *radios;
623 g_return_if_fail(current_pinboard != NULL);
625 if (set_backdrop_dialog)
626 gtk_widget_destroy(set_backdrop_dialog);
628 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
629 GTK_DIALOG_NO_SEPARATOR,
630 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
631 NULL);
632 set_backdrop_dialog = dialog;
633 g_signal_connect(dialog, "destroy",
634 G_CALLBACK(gtk_widget_destroyed), &set_backdrop_dialog);
635 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
637 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
639 label = gtk_label_new(_("Choose a style and drag an image in:"));
640 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
641 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
642 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
644 /* The Centred, Scaled, Tiled radios... */
645 hbox = gtk_hbox_new(TRUE, 2);
646 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
648 radios = radios_new(radios_changed, dialog);
649 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
650 g_object_set_data(G_OBJECT(dialog), "rox-radios-hbox", hbox);
652 radios_add(radios, _("Centre the image without scaling it"),
653 BACKDROP_CENTRE, _("Centre"));
654 radios_add(radios, _("Scale the image to fit the backdrop area, "
655 "without distorting it"),
656 BACKDROP_SCALE, _("Scale"));
657 radios_add(radios, _("Stretch the image to fill the backdrop area"),
658 BACKDROP_STRETCH, _("Stretch"));
659 radios_add(radios, _("Tile the image over the backdrop area"),
660 BACKDROP_TILE, _("Tile"));
662 update_radios(dialog);
664 /* The drop area... */
665 frame = drop_box_new(_("Drop an image here"));
666 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
668 radios_pack(radios, GTK_BOX(hbox));
669 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
671 drop_box_set_path(DROP_BOX(frame), current_pinboard->backdrop);
673 g_signal_connect(frame, "path_dropped",
674 G_CALLBACK(drag_backdrop_dropped), dialog);
675 g_signal_connect(frame, "clear",
676 G_CALLBACK(clear_backdrop), dialog);
678 g_signal_connect(dialog, "response",
679 G_CALLBACK(backdrop_response), NULL);
680 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
682 gtk_widget_show_all(dialog);
685 /* Also used by tasklist.c */
686 void draw_label_shadow(WrappedLabel *wl, GdkRegion *region)
688 GtkWidget *widget;
689 gint x, y;
691 if (!o_pinboard_shadow_labels.int_value)
692 return;
694 gdk_gc_set_clip_region(current_pinboard->shadow_gc, region);
696 widget = GTK_WIDGET(wl);
698 y = widget->allocation.y - wl->y_off;
699 x = widget->allocation.x - wl->x_off -
700 ((wl->text_width - widget->allocation.width) >> 1);
702 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
703 x + 1, y + 1, wl->layout);
705 if (o_pinboard_shadow_labels.int_value > 1)
706 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
707 x + 2, y + 2, wl->layout);
709 gdk_gc_set_clip_region(current_pinboard->shadow_gc, NULL);
712 /* Set and save (path, style) as the new backdrop.
713 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
714 * Otherwise, the image is displayed now.
716 void pinboard_set_backdrop(const gchar *path, BackdropStyle style)
718 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
719 (path != NULL && style != BACKDROP_NONE));
721 if (!current_pinboard)
723 if (!path)
724 return;
725 pinboard_activate("Default");
726 delayed_error(_("No pinboard was in use... "
727 "the 'Default' pinboard has been selected. "
728 "Use 'rox -p=Default' to turn it on in "
729 "future."));
730 g_return_if_fail(current_pinboard != NULL);
733 /* We might have just run the old backdrop program and now
734 * we're going to set a new one! Seems a bit mean...
737 abandon_backdrop_app(current_pinboard);
739 g_free(current_pinboard->backdrop);
740 current_pinboard->backdrop = g_strdup(path);
741 current_pinboard->backdrop_style = style;
742 reload_backdrop(current_pinboard,
743 current_pinboard->backdrop,
744 current_pinboard->backdrop_style);
746 pinboard_save();
748 if (set_backdrop_dialog)
750 DropBox *box = g_object_get_data(G_OBJECT(set_backdrop_dialog),
751 "rox-dropbox");
752 g_return_if_fail(box != NULL);
753 drop_box_set_path(box, current_pinboard->backdrop);
754 update_radios(set_backdrop_dialog);
758 /* Called on xrandr screen resizes */
759 void pinboard_update_size(void)
761 int width, height;
763 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
764 &width, &height);
766 /* Only update the pinboard's size if the screen gets bigger,
767 * not smaller. Not sure what to do about icons that end up
768 * offscreen if the screen shrinks, but perhaps a good policy
769 * is to leave them there until the screen size is increased
770 * again rather than mess them around. */
771 width = MAX(width, screen_width);
772 height = MAX(height, screen_height);
774 gtk_widget_set_size_request(current_pinboard->window, width, height);
777 /****************************************************************
778 * INTERNAL FUNCTIONS *
779 ****************************************************************/
781 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
783 gtk_widget_destroy(dialog);
786 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
788 pinboard_set_backdrop(NULL, BACKDROP_NONE);
791 static void drag_backdrop_dropped(GtkWidget *drop_box,
792 const guchar *path,
793 GtkWidget *dialog)
795 struct stat info;
796 Radios *radios;
798 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
799 g_return_if_fail(radios != NULL);
801 if (mc_stat(path, &info))
803 delayed_error(
804 _("Can't access '%s':\n%s"), path,
805 g_strerror(errno));
806 return;
809 if (S_ISDIR(info.st_mode))
811 /* Use this program to set the backdrop */
812 pinboard_set_backdrop_app(path);
814 else if (S_ISREG(info.st_mode))
815 pinboard_set_backdrop(path, radios_get_value(radios));
816 else
817 delayed_error(_("Only files (and certain applications) can be "
818 "used to set the background image."));
821 /* Do this in the idle loop so that we don't try to put an unmanaged
822 * pinboard behind a managed panel (crashes some WMs).
824 static gboolean recreate_pinboard(gchar *name)
826 pinboard_activate(name);
827 g_free(name);
829 return FALSE;
832 static void pinboard_check_options(void)
834 GdkColor n_fg, n_bg, n_shadow;
836 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
837 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
838 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
840 if (o_override_redirect.has_changed && current_pinboard)
842 gchar *name;
843 name = g_strdup(current_pinboard->name);
844 pinboard_activate(NULL);
845 gtk_idle_add((GtkFunction) recreate_pinboard, name);
848 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
850 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
851 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
852 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
853 o_pinboard_shadow_labels.has_changed ||
854 o_label_font.has_changed)
856 pin_text_fg_col = n_fg;
857 pin_text_bg_col = n_bg;
858 pin_text_shadow_col = n_shadow;
859 update_pinboard_font();
861 if (current_pinboard)
863 GtkWidget *w = current_pinboard->window;
864 GdkColormap *cm;
866 cm = gtk_widget_get_colormap(w);
868 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
869 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
871 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
872 &n_shadow);
874 abandon_backdrop_app(current_pinboard);
875 reload_backdrop(current_pinboard,
876 current_pinboard->backdrop,
877 current_pinboard->backdrop_style);
879 reshape_all();
882 tasklist_style_changed();
886 static gint end_wink(gpointer data)
888 pinboard_wink_item(NULL, FALSE);
889 return FALSE;
892 /* Sets the appearance from the options and updates the size request of
893 * the image.
895 static void set_size_and_style(PinIcon *pi)
897 Icon *icon = (Icon *) pi;
898 MaskedPixmap *image = icon->item->image;
899 int iwidth = image->width;
900 int iheight = image->height;
902 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
903 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
904 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
905 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
906 widget_modify_font(pi->label, pinboard_font);
908 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
910 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
913 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
915 static GtkWidgetClass *parent_class = NULL;
916 Icon *icon = (Icon *) pi;
917 DirItem *item = icon->item;
918 MaskedPixmap *image = item->image;
919 int iwidth = image->width;
920 int iheight = image->height;
921 int x, y;
923 if (!parent_class)
925 gpointer c = ((GTypeInstance *) widget)->g_class;
926 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
929 x = pi->widget->allocation.x;
930 y = pi->widget->allocation.y;
932 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
934 render_pixbuf(icon->selected ? image->pixbuf_lit : image->pixbuf,
935 pi->widget->window,
936 pi->widget->style->black_gc,
937 x, y, iwidth, iheight);
939 if (item->flags & ITEM_FLAG_SYMLINK)
941 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
942 pi->widget->style->black_gc,
943 x, y, -1, -1);
945 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
947 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
948 ? im_mounted
949 : im_unmounted;
951 render_pixbuf(mp->pixbuf, pi->widget->window,
952 pi->widget->style->black_gc,
953 x, y, -1, -1);
956 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
958 if (icon->selected)
960 GtkStyle *style = pi->label->style;
961 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
963 gdk_gc_set_clip_region(gc, event->region);
964 gdk_draw_rectangle(pi->label->window, gc, TRUE,
965 pi->label->allocation.x,
966 pi->label->allocation.y,
967 pi->label->allocation.width,
968 pi->label->allocation.height);
969 gdk_gc_set_clip_region(gc, NULL);
971 else if (o_pinboard_shadow_labels.int_value)
972 draw_label_shadow((WrappedLabel *) pi->label, event->region);
974 /* Draw children */
975 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
976 event->region);
977 (parent_class->expose_event)(widget, event);
978 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
980 /* Stop the button effect */
981 return TRUE;
984 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
986 gint x, y, width, height;
988 if (current_wink_icon != pi)
989 return FALSE;
991 x = widget->allocation.x;
992 y = widget->allocation.y;
993 width = widget->allocation.width;
994 height = widget->allocation.height;
996 gdk_draw_rectangle(widget->window,
997 pi->widget->style->white_gc,
998 FALSE,
999 x, y, width - 1, height - 1);
1000 gdk_draw_rectangle(widget->window,
1001 pi->widget->style->black_gc,
1002 FALSE,
1003 x + 1, y + 1, width - 3, height - 3);
1005 return FALSE;
1008 static gboolean enter_notify(GtkWidget *widget,
1009 GdkEventCrossing *event,
1010 PinIcon *pi)
1012 icon_may_update((Icon *) pi);
1013 pin_icon_set_tip(pi);
1014 return TRUE;
1017 static gint leave_notify(GtkWidget *widget,
1018 GdkEventCrossing *event,
1019 PinIcon *pi)
1021 return TRUE;
1024 static void select_lasso(void)
1026 GList *next;
1027 int minx, miny, maxx, maxy;
1029 g_return_if_fail(lasso_in_progress == TRUE);
1031 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1032 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1033 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1034 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1036 for (next = current_pinboard->icons; next; next = next->next)
1038 PinIcon *pi = (PinIcon *) next->data;
1039 GtkAllocation *alloc = &pi->win->allocation;
1040 int cx = alloc->x + alloc->width / 2;
1041 int cy = alloc->y + alloc->height / 2;
1043 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1044 icon_set_selected((Icon *) pi, TRUE);
1048 static void cancel_lasso(void)
1050 draw_lasso();
1051 lasso_in_progress = FALSE;
1054 static void pinboard_lasso_box(int start_x, int start_y)
1056 if (lasso_in_progress)
1057 cancel_lasso();
1058 lasso_in_progress = TRUE;
1059 lasso_rect_x1 = lasso_rect_x2 = start_x;
1060 lasso_rect_y1 = lasso_rect_y2 = start_y;
1062 draw_lasso();
1065 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1067 if (!lasso_in_progress)
1068 return FALSE;
1070 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1072 draw_lasso();
1073 lasso_rect_x2 = event->x;
1074 lasso_rect_y2 = event->y;
1075 draw_lasso();
1078 return FALSE;
1081 /* Mark the area of the screen covered by the lasso box for redraw */
1082 static void draw_lasso(void)
1084 GdkRectangle area, edge;
1086 if (!lasso_in_progress)
1087 return;
1089 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1090 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1091 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1092 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1094 edge.x = area.x;
1095 edge.y = area.y;
1096 edge.width = area.width;
1098 edge.height = 2; /* Top */
1099 gdk_window_invalidate_rect(current_pinboard->window->window,
1100 &edge, TRUE);
1102 edge.y += area.height - 2; /* Bottom */
1103 gdk_window_invalidate_rect(current_pinboard->window->window,
1104 &edge, TRUE);
1106 edge.y = area.y;
1107 edge.height = area.height;
1108 edge.width = 2; /* Left */
1109 gdk_window_invalidate_rect(current_pinboard->window->window,
1110 &edge, TRUE);
1112 edge.x += area.width - 2; /* Right */
1113 gdk_window_invalidate_rect(current_pinboard->window->window,
1114 &edge, TRUE);
1117 static void perform_action(PinIcon *pi, GdkEventButton *event)
1119 BindAction action;
1120 Icon *icon = (Icon *) pi;
1122 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1123 event);
1125 /* Actions that can happen with or without an icon */
1126 switch (action)
1128 case ACT_LASSO_CLEAR:
1129 icon_select_only(NULL);
1130 /* (no break) */
1131 case ACT_LASSO_MODIFY:
1132 pinboard_lasso_box(event->x, event->y);
1133 return;
1134 case ACT_CLEAR_SELECTION:
1135 icon_select_only(NULL);
1136 return;
1137 case ACT_POPUP_MENU:
1138 dnd_motion_ungrab();
1139 pinboard_show_menu(event, pi);
1140 return;
1141 case ACT_IGNORE:
1142 return;
1143 default:
1144 break;
1147 g_return_if_fail(pi != NULL);
1149 switch (action)
1151 case ACT_OPEN_ITEM:
1152 dnd_motion_ungrab();
1153 pinboard_wink_item(pi, TRUE);
1154 if (event->type == GDK_2BUTTON_PRESS)
1155 icon_set_selected(icon, FALSE);
1156 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
1157 break;
1158 case ACT_EDIT_ITEM:
1159 dnd_motion_ungrab();
1160 pinboard_wink_item(pi, TRUE);
1161 if (event->type == GDK_2BUTTON_PRESS)
1162 icon_set_selected(icon, FALSE);
1163 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1164 break;
1165 case ACT_PRIME_AND_SELECT:
1166 if (!icon->selected)
1167 icon_select_only(icon);
1168 dnd_motion_start(MOTION_READY_FOR_DND);
1169 break;
1170 case ACT_PRIME_AND_TOGGLE:
1171 icon_set_selected(icon, !icon->selected);
1172 dnd_motion_start(MOTION_READY_FOR_DND);
1173 break;
1174 case ACT_PRIME_FOR_DND:
1175 dnd_motion_start(MOTION_READY_FOR_DND);
1176 break;
1177 case ACT_TOGGLE_SELECTED:
1178 icon_set_selected(icon, !icon->selected);
1179 break;
1180 case ACT_SELECT_EXCL:
1181 icon_select_only(icon);
1182 break;
1183 default:
1184 g_warning("Unsupported action : %d\n", action);
1185 break;
1189 static void forward_to_root(GdkEventButton *event)
1191 XButtonEvent xev;
1193 if (event->type == GDK_BUTTON_PRESS)
1195 xev.type = ButtonPress;
1196 if (!o_blackbox_hack.int_value)
1197 XUngrabPointer(gdk_display, event->time);
1199 else
1200 xev.type = ButtonRelease;
1202 xev.window = gdk_x11_get_default_root_xwindow();
1203 xev.root = xev.window;
1204 xev.subwindow = None;
1205 xev.time = event->time;
1206 xev.x = event->x_root; /* Needed for icewm */
1207 xev.y = event->y_root;
1208 xev.x_root = event->x_root;
1209 xev.y_root = event->y_root;
1210 xev.state = event->state;
1211 xev.button = event->button;
1212 xev.same_screen = True;
1214 XSendEvent(gdk_display, xev.window, False,
1215 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1218 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1219 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1221 /* pi is NULL if this is a root event */
1222 static gboolean button_release_event(GtkWidget *widget,
1223 GdkEventButton *event,
1224 PinIcon *pi)
1226 if (FORWARDED_BUTTON(pi, event->button))
1227 forward_to_root(event);
1228 else if (dnd_motion_release(event))
1230 if (motion_buttons_pressed == 0 && lasso_in_progress)
1232 select_lasso();
1233 cancel_lasso();
1235 return FALSE;
1238 perform_action(pi, event);
1240 return TRUE;
1243 /* pi is NULL if this is a root event */
1244 static gboolean button_press_event(GtkWidget *widget,
1245 GdkEventButton *event,
1246 PinIcon *pi)
1248 /* Just in case we've jumped in front of everything... */
1249 gdk_window_lower(current_pinboard->window->window);
1251 if (FORWARDED_BUTTON(pi, event->button))
1252 forward_to_root(event);
1253 else if (dnd_motion_press(widget, event))
1254 perform_action(pi, event);
1256 return TRUE;
1259 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1260 * (for old window managers that don't catch the buttons themselves)
1262 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1264 XButtonEvent xev;
1266 xev.type = ButtonPress;
1267 xev.window = gdk_x11_get_default_root_xwindow();
1268 xev.root = xev.window;
1269 xev.subwindow = None;
1270 xev.time = event->time;
1271 xev.x = event->x_root; /* Needed for icewm */
1272 xev.y = event->y_root;
1273 xev.x_root = event->x_root;
1274 xev.y_root = event->y_root;
1275 xev.state = event->state;
1276 xev.same_screen = True;
1278 if (event->direction == GDK_SCROLL_UP)
1279 xev.button = 4;
1280 else if (event->direction == GDK_SCROLL_DOWN)
1281 xev.button = 5;
1282 else
1283 return FALSE;
1285 XSendEvent(gdk_display, xev.window, False,
1286 ButtonPressMask, (XEvent *) &xev);
1288 return TRUE;
1291 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1293 GtkWidget *widget = pi->win;
1294 Icon *icon = (Icon *) pi;
1296 if (!icon->selected)
1298 tmp_icon_selected = TRUE;
1299 icon_select_only(icon);
1302 g_return_if_fail(icon_selection != NULL);
1304 pinboard_drag_in_progress = icon;
1306 if (icon_selection->next == NULL)
1307 drag_one_item(widget, event, icon->path, icon->item, NULL);
1308 else
1310 guchar *uri_list;
1312 uri_list = icon_create_uri_list();
1313 drag_selection(widget, event, uri_list);
1314 g_free(uri_list);
1318 /* An icon is being dragged around... */
1319 static gint icon_motion_notify(GtkWidget *widget,
1320 GdkEventMotion *event,
1321 PinIcon *pi)
1323 if (motion_state == MOTION_READY_FOR_DND)
1325 if (dnd_motion_moved(event))
1326 start_drag(pi, event);
1327 return TRUE;
1330 return FALSE;
1333 static void backdrop_from_xml(xmlNode *node)
1335 gchar *style;
1337 g_free(current_pinboard->backdrop);
1338 current_pinboard->backdrop = xmlNodeGetContent(node);
1340 style = xmlGetProp(node, "style");
1342 if (style)
1344 current_pinboard->backdrop_style =
1345 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1346 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1347 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1348 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1349 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1350 BACKDROP_NONE;
1351 g_free(style);
1353 else
1354 current_pinboard->backdrop_style = BACKDROP_TILE;
1357 /* Create one pinboard icon for each icon in the doc */
1358 static void pinboard_load_from_xml(xmlDocPtr doc)
1360 xmlNodePtr node, root;
1361 char *tmp, *label, *path, *shortcut;
1362 int x, y;
1364 root = xmlDocGetRootElement(doc);
1366 for (node = root->xmlChildrenNode; node; node = node->next)
1368 if (node->type != XML_ELEMENT_NODE)
1369 continue;
1370 if (strcmp(node->name, "backdrop") == 0)
1372 backdrop_from_xml(node);
1373 continue;
1375 if (strcmp(node->name, "icon") != 0)
1376 continue;
1378 tmp = xmlGetProp(node, "x");
1379 if (!tmp)
1380 continue;
1381 x = atoi(tmp);
1382 g_free(tmp);
1384 tmp = xmlGetProp(node, "y");
1385 if (!tmp)
1386 continue;
1387 y = atoi(tmp);
1388 g_free(tmp);
1390 label = xmlGetProp(node, "label");
1391 if (!label)
1392 label = g_strdup("<missing label>");
1393 path = xmlNodeGetContent(node);
1394 if (!path)
1395 path = g_strdup("<missing path>");
1396 shortcut = xmlGetProp(node, "shortcut");
1398 pinboard_pin(path, label, x, y, shortcut);
1400 g_free(path);
1401 g_free(label);
1402 g_free(shortcut);
1406 /* Called for each line in the pinboard file while loading a new board.
1407 * Only used for old-format files when converting to XML.
1409 static const char *pin_from_file(gchar *line)
1411 gchar *leaf = NULL;
1412 int x, y, n;
1414 if (*line == '<')
1416 gchar *end;
1418 end = strchr(line + 1, '>');
1419 if (!end)
1420 return _("Missing '>' in icon label");
1422 leaf = g_strndup(line + 1, end - line - 1);
1424 line = end + 1;
1426 while (g_ascii_isspace(*line))
1427 line++;
1428 if (*line != ',')
1429 return _("Missing ',' after icon label");
1430 line++;
1433 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1434 return NULL; /* Ignore format errors */
1436 pinboard_pin(line + n, leaf, x, y, NULL);
1438 g_free(leaf);
1440 return NULL;
1443 /* Write the current state of the pinboard to the current pinboard file */
1444 static void pinboard_save(void)
1446 guchar *save = NULL;
1447 guchar *save_new = NULL;
1448 GList *next;
1449 xmlDocPtr doc = NULL;
1450 xmlNodePtr root;
1452 g_return_if_fail(current_pinboard != NULL);
1454 if (strchr(current_pinboard->name, '/'))
1455 save = g_strdup(current_pinboard->name);
1456 else
1458 guchar *leaf;
1460 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1461 save = choices_find_path_save(leaf, PROJECT, TRUE);
1462 g_free(leaf);
1465 if (!save)
1466 return;
1468 doc = xmlNewDoc("1.0");
1469 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1471 root = xmlDocGetRootElement(doc);
1473 if (current_pinboard->backdrop)
1475 BackdropStyle style = current_pinboard->backdrop_style;
1476 xmlNodePtr tree;
1478 tree = xmlNewTextChild(root, NULL, "backdrop",
1479 current_pinboard->backdrop);
1480 xmlSetProp(tree, "style",
1481 style == BACKDROP_TILE ? "Tiled" :
1482 style == BACKDROP_CENTRE ? "Centred" :
1483 style == BACKDROP_SCALE ? "Scaled" :
1484 style == BACKDROP_STRETCH ? "Stretched" :
1485 "Program");
1488 for (next = current_pinboard->icons; next; next = next->next)
1490 xmlNodePtr tree;
1491 PinIcon *pi = (PinIcon *) next->data;
1492 Icon *icon = (Icon *) pi;
1493 char *tmp;
1495 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1497 tmp = g_strdup_printf("%d", pi->x);
1498 xmlSetProp(tree, "x", tmp);
1499 g_free(tmp);
1501 tmp = g_strdup_printf("%d", pi->y);
1502 xmlSetProp(tree, "y", tmp);
1503 g_free(tmp);
1505 xmlSetProp(tree, "label", icon->item->leafname);
1506 if (icon->shortcut)
1507 xmlSetProp(tree, "shortcut", icon->shortcut);
1510 save_new = g_strconcat(save, ".new", NULL);
1511 if (save_xml_file(doc, save_new) || rename(save_new, save))
1512 delayed_error(_("Error saving pinboard %s: %s"),
1513 save, g_strerror(errno));
1514 g_free(save_new);
1516 g_free(save);
1517 if (doc)
1518 xmlFreeDoc(doc);
1521 static void snap_to_grid(int *x, int *y)
1523 int step = o_pinboard_grid_step.int_value;
1525 *x = ((*x + step / 2) / step) * step;
1526 *y = ((*y + step / 2) / step) * step;
1529 /* Convert (x,y) from a centre point to a window position */
1530 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1532 gboolean clamp = o_pinboard_clamp_icons.int_value;
1533 GtkRequisition req;
1535 gtk_widget_size_request(pi->win, &req);
1537 *x -= req.width >> 1;
1538 *y -= req.height >> 1;
1539 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1540 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1543 /* Same as drag_set_dest(), but for pinboard icons */
1544 static void drag_set_pinicon_dest(PinIcon *pi)
1546 GtkObject *obj = GTK_OBJECT(pi->win);
1548 make_drop_target(pi->win, 0);
1550 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1551 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1552 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1555 /* Called during the drag when the mouse is in a widget registered
1556 * as a drop target. Returns TRUE if we can accept the drop.
1558 static gboolean drag_motion(GtkWidget *widget,
1559 GdkDragContext *context,
1560 gint x,
1561 gint y,
1562 guint time,
1563 PinIcon *pi)
1565 GdkDragAction action = context->suggested_action;
1566 const char *type = NULL;
1567 Icon *icon = (Icon *) pi;
1568 DirItem *item = icon->item;
1570 if (gtk_drag_get_source_widget(context) == widget)
1572 g_dataset_set_data(context, "drop_dest_type",
1573 (gpointer) drop_dest_pass_through);
1574 return FALSE; /* Can't drag something to itself! */
1577 if (icon->selected)
1578 goto out; /* Can't drag a selection to itself */
1580 type = dnd_motion_item(context, &item);
1582 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1583 && type != drop_dest_prog)
1585 guint state;
1586 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1587 if (state & GDK_BUTTON1_MASK)
1588 action = GDK_ACTION_ASK;
1591 if (!item)
1592 type = NULL;
1593 out:
1594 /* We actually must pretend to accept the drop, even if the
1595 * directory isn't writeable, so that the spring-opening
1596 * thing works.
1599 /* Don't allow drops to non-writeable directories */
1600 if (o_dnd_spring_open.int_value == FALSE &&
1601 type == drop_dest_dir &&
1602 access(icon->path, W_OK) != 0)
1604 type = NULL;
1607 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1608 if (type)
1610 gdk_drag_status(context, action, time);
1611 g_dataset_set_data_full(context, "drop_dest_path",
1612 g_strdup(icon->path), g_free);
1613 if (type == drop_dest_dir)
1614 dnd_spring_load(context, NULL);
1616 pinboard_wink_item(pi, FALSE);
1618 else
1619 gdk_drag_status(context, 0, time);
1621 /* Always return TRUE to stop the pinboard getting the events */
1622 return TRUE;
1625 static gboolean pinboard_shadow = FALSE;
1626 static gint shadow_x, shadow_y;
1627 #define SHADOW_SIZE (ICON_WIDTH)
1629 static gboolean bg_expose(GtkWidget *widget,
1630 GdkEventExpose *event, gpointer data)
1632 GdkRectangle clipbox;
1633 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1634 gboolean double_buffer;
1636 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1637 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1639 gdk_region_get_clipbox(event->region, &clipbox);
1641 double_buffer = (clipbox.width * clipbox.height) < 20000;
1642 if (double_buffer)
1643 gdk_window_begin_paint_region(widget->window, event->region);
1645 /* Clear the area to the background image */
1647 GtkStyle *style = current_pinboard->window->style;
1648 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1650 gdk_gc_set_clip_region(gc, event->region);
1651 if (style->bg_pixmap[GTK_STATE_NORMAL])
1653 gdk_gc_set_ts_origin(gc, 0, 0);
1654 gdk_gc_set_fill(gc, GDK_TILED);
1655 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1658 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1659 clipbox.x, clipbox.y,
1660 clipbox.width, clipbox.height);
1661 if (style->bg_pixmap[GTK_STATE_NORMAL])
1662 gdk_gc_set_fill(gc, GDK_SOLID);
1664 gdk_gc_set_clip_region(gc, NULL);
1667 if (pinboard_shadow)
1669 gdk_draw_rectangle(widget->window,
1670 widget->style->white_gc, FALSE,
1671 shadow_x, shadow_y,
1672 SHADOW_SIZE, SHADOW_SIZE);
1673 gdk_draw_rectangle(widget->window,
1674 widget->style->black_gc, FALSE,
1675 shadow_x + 1, shadow_y + 1,
1676 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1679 if (lasso_in_progress)
1681 GdkRectangle area;
1683 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1684 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1685 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1686 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1688 if (area.width > 4 && area.height > 4)
1690 gdk_draw_rectangle(widget->window,
1691 widget->style->white_gc, FALSE,
1692 area.x, area.y,
1693 area.width - 1, area.height - 1);
1694 gdk_draw_rectangle(widget->window,
1695 widget->style->black_gc, FALSE,
1696 area.x + 1, area.y + 1,
1697 area.width - 3, area.height - 3);
1701 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1702 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1704 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1706 if (double_buffer)
1707 gdk_window_end_paint(widget->window);
1709 return TRUE;
1712 /* Draw a 'shadow' under an icon being dragged, showing where
1713 * it will land.
1715 static void pinboard_set_shadow(gboolean on)
1717 GdkRectangle area;
1719 if (pinboard_shadow)
1721 area.x = shadow_x;
1722 area.y = shadow_y;
1723 area.width = SHADOW_SIZE + 1;
1724 area.height = SHADOW_SIZE + 1;
1726 gdk_window_invalidate_rect(current_pinboard->window->window,
1727 &area, TRUE);
1730 if (on)
1732 int old_x = shadow_x, old_y = shadow_y;
1734 gdk_window_get_pointer(current_pinboard->fixed->window,
1735 &shadow_x, &shadow_y, NULL);
1736 snap_to_grid(&shadow_x, &shadow_y);
1737 shadow_x -= SHADOW_SIZE / 2;
1738 shadow_y -= SHADOW_SIZE / 2;
1741 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1742 return;
1744 area.x = shadow_x;
1745 area.y = shadow_y;
1746 area.width = SHADOW_SIZE + 1;
1747 area.height = SHADOW_SIZE + 1;
1749 gdk_window_invalidate_rect(current_pinboard->window->window,
1750 &area, TRUE);
1753 pinboard_shadow = on;
1756 /* Called when dragging some pinboard icons finishes */
1757 void pinboard_move_icons(void)
1759 int x = shadow_x, y = shadow_y;
1760 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1761 int width, height;
1762 int dx, dy;
1763 GList *next;
1765 g_return_if_fail(pi != NULL);
1767 x += SHADOW_SIZE / 2;
1768 y += SHADOW_SIZE / 2;
1769 snap_to_grid(&x, &y);
1771 if (pi->x == x && pi->y == y)
1772 return;
1774 /* Find out how much the dragged icon moved (after snapping).
1775 * Move all selected icons by the same amount.
1777 dx = x - pi->x;
1778 dy = y - pi->y;
1780 /* Move the other selected icons to keep the same relative
1781 * position.
1783 for (next = icon_selection; next; next = next->next)
1785 PinIcon *pi = (PinIcon *) next->data;
1786 int nx, ny;
1788 g_return_if_fail(IS_PIN_ICON(pi));
1790 pi->x += dx;
1791 pi->y += dy;
1792 nx = pi->x;
1793 ny = pi->y;
1795 gdk_drawable_get_size(pi->win->window, &width, &height);
1796 offset_from_centre(pi, &nx, &ny);
1798 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1799 pi->win, nx, ny);
1802 pinboard_save();
1805 static void drag_leave(GtkWidget *widget,
1806 GdkDragContext *context,
1807 guint32 time,
1808 PinIcon *pi)
1810 pinboard_wink_item(NULL, FALSE);
1811 dnd_spring_abort();
1814 static gboolean bg_drag_leave(GtkWidget *widget,
1815 GdkDragContext *context,
1816 guint32 time,
1817 gpointer data)
1819 pinboard_set_shadow(FALSE);
1820 return TRUE;
1823 static gboolean bg_drag_motion(GtkWidget *widget,
1824 GdkDragContext *context,
1825 gint x,
1826 gint y,
1827 guint time,
1828 gpointer data)
1830 /* Dragging from the pinboard to the pinboard is not allowed */
1832 if (!provides(context, text_uri_list))
1833 return FALSE;
1835 pinboard_set_shadow(TRUE);
1837 gdk_drag_status(context,
1838 context->suggested_action == GDK_ACTION_ASK
1839 ? GDK_ACTION_LINK : context->suggested_action,
1840 time);
1841 return TRUE;
1844 static void drag_end(GtkWidget *widget,
1845 GdkDragContext *context,
1846 PinIcon *pi)
1848 pinboard_drag_in_progress = NULL;
1849 if (tmp_icon_selected)
1851 icon_select_only(NULL);
1852 tmp_icon_selected = FALSE;
1856 /* Something which affects all the icons has changed - reshape
1857 * and redraw all of them.
1859 static void reshape_all(void)
1861 GList *next;
1863 g_return_if_fail(current_pinboard != NULL);
1865 for (next = current_pinboard->icons; next; next = next->next)
1867 Icon *icon = (Icon *) next->data;
1868 pinboard_reshape_icon(icon);
1872 /* Turns off the pinboard. Does not call gtk_main_quit. */
1873 static void pinboard_clear(void)
1875 GList *next;
1877 g_return_if_fail(current_pinboard != NULL);
1879 tasklist_set_active(FALSE);
1881 next = current_pinboard->icons;
1882 while (next)
1884 PinIcon *pi = (PinIcon *) next->data;
1886 next = next->next;
1888 gtk_widget_destroy(pi->win);
1891 gtk_widget_destroy(current_pinboard->window);
1893 abandon_backdrop_app(current_pinboard);
1895 g_object_unref(current_pinboard->shadow_gc);
1896 current_pinboard->shadow_gc = NULL;
1898 g_free(current_pinboard->name);
1899 null_g_free(&current_pinboard);
1901 number_of_windows--;
1904 static gpointer parent_class;
1906 static void pin_icon_destroy(Icon *icon)
1908 PinIcon *pi = (PinIcon *) icon;
1910 g_return_if_fail(pi->win != NULL);
1912 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1913 gtk_widget_destroy(pi->win);
1916 static void pinboard_remove_items(void)
1918 g_return_if_fail(icon_selection != NULL);
1920 while (icon_selection)
1921 icon_destroy((Icon *) icon_selection->data);
1923 pinboard_save();
1926 static void pin_icon_update(Icon *icon)
1928 pinboard_reshape_icon(icon);
1929 pinboard_save();
1932 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1934 return IS_PIN_ICON(other);
1937 static void pin_wink_icon(Icon *icon)
1939 pinboard_wink_item((PinIcon *) icon, TRUE);
1942 static void pin_icon_class_init(gpointer gclass, gpointer data)
1944 IconClass *icon = (IconClass *) gclass;
1946 parent_class = g_type_class_peek_parent(gclass);
1948 icon->destroy = pin_icon_destroy;
1949 icon->redraw = pinboard_reshape_icon;
1950 icon->update = pin_icon_update;
1951 icon->wink = pin_wink_icon;
1952 icon->remove_items = pinboard_remove_items;
1953 icon->same_group = pin_icon_same_group;
1956 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
1960 static GType pin_icon_get_type(void)
1962 static GType type = 0;
1964 if (!type)
1966 static const GTypeInfo info =
1968 sizeof (PinIconClass),
1969 NULL, /* base_init */
1970 NULL, /* base_finalise */
1971 pin_icon_class_init,
1972 NULL, /* class_finalise */
1973 NULL, /* class_data */
1974 sizeof(PinIcon),
1975 0, /* n_preallocs */
1976 pin_icon_init
1979 type = g_type_register_static(icon_get_type(),
1980 "PinIcon", &info, 0);
1983 return type;
1986 static PinIcon *pin_icon_new(const char *pathname, const char *name)
1988 PinIcon *pi;
1989 Icon *icon;
1991 pi = g_object_new(pin_icon_get_type(), NULL);
1992 icon = (Icon *) pi;
1994 icon_set_path(icon, pathname, name);
1996 return pi;
1999 /* Called when the window widget is somehow destroyed */
2000 static void pin_icon_destroyed(PinIcon *pi)
2002 g_return_if_fail(pi->win != NULL);
2004 pi->win = NULL;
2006 pinboard_wink_item(NULL, FALSE);
2008 if (pinboard_drag_in_progress == (Icon *) pi)
2009 pinboard_drag_in_progress = NULL;
2011 if (current_pinboard)
2012 current_pinboard->icons =
2013 g_list_remove(current_pinboard->icons, pi);
2015 g_object_unref(pi);
2018 /* Set the tooltip */
2019 static void pin_icon_set_tip(PinIcon *pi)
2021 XMLwrapper *ai;
2022 xmlNode *node;
2023 Icon *icon = (Icon *) pi;
2025 g_return_if_fail(pi != NULL);
2027 ai = appinfo_get(icon->path, icon->item);
2029 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2031 guchar *str;
2032 str = xmlNodeListGetString(node->doc,
2033 node->xmlChildrenNode, 1);
2034 if (str)
2036 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2037 g_free(str);
2040 else
2041 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2043 if (ai)
2044 g_object_unref(ai);
2047 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2049 int pos[3];
2050 GList *list;
2052 pos[0] = event->x_root;
2053 pos[1] = event->y_root;
2055 icon_prepare_menu((Icon *) pi, TRUE);
2057 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2058 pos[2] = g_list_length(list) - 6;
2059 g_list_free(list);
2061 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2062 position_menu,
2063 (gpointer) pos, event->button, event->time);
2066 static void create_pinboard_window(Pinboard *pinboard)
2068 GtkWidget *win;
2070 g_return_if_fail(pinboard->window == NULL);
2072 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2073 gtk_widget_set_style(win, gtk_widget_get_default_style());
2075 gtk_widget_set_double_buffered(win, FALSE);
2076 gtk_widget_set_app_paintable(win, TRUE);
2077 gtk_widget_set_name(win, "rox-pinboard");
2078 pinboard->window = win;
2079 pinboard->fixed = gtk_fixed_new();
2080 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2082 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2084 gtk_widget_set_size_request(win, screen_width, screen_height);
2085 gtk_widget_realize(win);
2086 gtk_window_move(GTK_WINDOW(win), 0, 0);
2087 make_panel_window(win);
2089 /* TODO: Use gdk function when it supports this type */
2091 GdkAtom desktop_type;
2093 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2094 FALSE);
2095 gdk_property_change(win->window,
2096 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2097 gdk_atom_intern("ATOM", FALSE), 32,
2098 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2101 gtk_widget_add_events(win,
2102 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2103 GDK_EXPOSURE_MASK |
2104 GDK_BUTTON1_MOTION_MASK |
2105 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2106 g_signal_connect(win, "button-press-event",
2107 G_CALLBACK(button_press_event), NULL);
2108 g_signal_connect(win, "button-release-event",
2109 G_CALLBACK(button_release_event), NULL);
2110 g_signal_connect(win, "motion-notify-event",
2111 G_CALLBACK(lasso_motion), NULL);
2112 g_signal_connect(pinboard->fixed, "expose_event",
2113 G_CALLBACK(bg_expose), NULL);
2115 /* Some window managers use scroll events on the root to switch
2116 * desktops, but don't cope with our pinboard window, so we forward
2117 * them manually in that case.
2119 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2121 /* Drag and drop handlers */
2122 drag_set_pinboard_dest(win);
2123 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2124 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2126 pinboard->shadow_gc = gdk_gc_new(win->window);
2127 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2129 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2131 gtk_widget_show_all(win);
2132 gdk_window_lower(win->window);
2135 /* Load image 'path' and scale according to 'style' */
2136 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2138 GdkPixmap *pixmap;
2139 GdkPixbuf *pixbuf;
2140 GError *error = NULL;
2142 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2143 if (error)
2145 delayed_error(_("Error loading backdrop image:\n%s\n"
2146 "Backdrop removed."),
2147 error->message);
2148 g_error_free(error);
2149 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2150 return NULL;
2153 if (style == BACKDROP_STRETCH)
2155 GdkPixbuf *old = pixbuf;
2157 pixbuf = gdk_pixbuf_scale_simple(old,
2158 screen_width, screen_height,
2159 GDK_INTERP_HYPER);
2161 g_object_unref(old);
2163 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2165 GdkPixbuf *old = pixbuf;
2166 int x, y, width, height;
2167 float scale;
2169 width = gdk_pixbuf_get_width(pixbuf);
2170 height = gdk_pixbuf_get_height(pixbuf);
2172 if (style == BACKDROP_SCALE)
2174 float scale_x, scale_y;
2175 scale_x = screen_width / ((float) width);
2176 scale_y = screen_height / ((float) height);
2177 scale = MIN(scale_x, scale_y);
2179 else
2180 scale = 1;
2182 pixbuf = gdk_pixbuf_new(
2183 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2184 8, screen_width, screen_height);
2185 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2186 ((pin_text_bg_col.green & 0xff00) << 8) |
2187 ((pin_text_bg_col.blue & 0xff00)));
2189 x = (screen_width - width * scale) / 2;
2190 y = (screen_height - height * scale) / 2;
2191 x = MAX(x, 0);
2192 y = MAX(y, 0);
2194 gdk_pixbuf_composite(old, pixbuf,
2195 x, y,
2196 MIN(screen_width, width * scale),
2197 MIN(screen_height, height * scale),
2198 x, y, scale, scale,
2199 GDK_INTERP_NEAREST, 255);
2200 g_object_unref(old);
2203 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2204 &pixmap, NULL, 0);
2205 g_object_unref(pixbuf);
2207 return pixmap;
2210 static void abandon_backdrop_app(Pinboard *pinboard)
2212 g_return_if_fail(pinboard != NULL);
2214 if (pinboard->to_backdrop_app != -1)
2216 close(pinboard->to_backdrop_app);
2217 close(pinboard->from_backdrop_app);
2218 gtk_input_remove(pinboard->input_tag);
2219 g_string_free(pinboard->input_buffer, TRUE);
2220 pinboard->to_backdrop_app = -1;
2221 pinboard->from_backdrop_app = -1;
2222 pinboard->input_tag = -1;
2223 pinboard->input_buffer = NULL;
2226 g_return_if_fail(pinboard->to_backdrop_app == -1);
2227 g_return_if_fail(pinboard->from_backdrop_app == -1);
2228 g_return_if_fail(pinboard->input_tag == -1);
2229 g_return_if_fail(pinboard->input_buffer == NULL);
2232 /* A single line has been read from the child.
2233 * Processes the command, and replies 'ok' (or abandons the child on error).
2235 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2237 BackdropStyle style;
2238 const char *ok = "ok\n";
2240 if (strncmp(command, "tile ", 5) == 0)
2242 style = BACKDROP_TILE;
2243 command += 5;
2245 else if (strncmp(command, "scale ", 6) == 0)
2247 style = BACKDROP_SCALE;
2248 command += 6;
2250 else if (strncmp(command, "stretch ", 8) == 0)
2252 style = BACKDROP_STRETCH;
2253 command += 8;
2255 else if (strncmp(command, "centre ", 7) == 0)
2257 style = BACKDROP_CENTRE;
2258 command += 7;
2260 else
2262 g_warning("Invalid command '%s' from backdrop app\n",
2263 command);
2264 abandon_backdrop_app(pinboard);
2265 return;
2268 /* Load the backdrop. May abandon the program if loading fails. */
2269 reload_backdrop(pinboard, command, style);
2271 if (pinboard->to_backdrop_app == -1)
2272 return;
2274 while (*ok)
2276 int sent;
2278 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2279 if (sent <= 0)
2281 /* Remote app quit? Not an error. */
2282 abandon_backdrop_app(pinboard);
2283 break;
2285 ok += sent;
2289 static void backdrop_from_child(Pinboard *pinboard,
2290 int src, GdkInputCondition cond)
2292 char buf[256];
2293 int got;
2295 got = read(src, buf, sizeof(buf));
2297 if (got <= 0)
2299 if (got < 0)
2300 g_warning("backdrop_from_child: %s\n",
2301 g_strerror(errno));
2302 abandon_backdrop_app(pinboard);
2303 return;
2306 g_string_append_len(pinboard->input_buffer, buf, got);
2308 while (pinboard->from_backdrop_app != -1)
2310 int len;
2311 char *nl, *command;
2313 nl = strchr(pinboard->input_buffer->str, '\n');
2314 if (!nl)
2315 return; /* Haven't got a whole line yet */
2317 len = nl - pinboard->input_buffer->str;
2318 command = g_strndup(pinboard->input_buffer->str, len);
2319 g_string_erase(pinboard->input_buffer, 0, len + 1);
2321 command_from_backdrop_app(pinboard, command);
2323 g_free(command);
2327 static void reload_backdrop(Pinboard *pinboard,
2328 const gchar *backdrop,
2329 BackdropStyle backdrop_style)
2331 GtkStyle *style;
2333 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2335 const char *argv[] = {NULL, "--backdrop", NULL};
2336 GError *error = NULL;
2338 g_return_if_fail(pinboard->to_backdrop_app == -1);
2339 g_return_if_fail(pinboard->from_backdrop_app == -1);
2340 g_return_if_fail(pinboard->input_tag == -1);
2341 g_return_if_fail(pinboard->input_buffer == NULL);
2343 argv[0] = make_path(backdrop, "AppRun");
2345 /* Run the program. It'll send us a SOAP message and we'll
2346 * get back here with a different style and image.
2349 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2350 G_SPAWN_DO_NOT_REAP_CHILD |
2351 G_SPAWN_SEARCH_PATH,
2352 NULL, NULL, /* Child setup fn */
2353 NULL, /* Child PID */
2354 &pinboard->to_backdrop_app,
2355 &pinboard->from_backdrop_app,
2356 NULL, /* Standard error */
2357 &error))
2359 pinboard->input_buffer = g_string_new(NULL);
2360 pinboard->input_tag = gtk_input_add_full(
2361 pinboard->from_backdrop_app,
2362 GDK_INPUT_READ,
2363 (GdkInputFunction) backdrop_from_child,
2364 NULL, pinboard, NULL);
2366 else
2368 delayed_error("%s", error ? error->message : "(null)");
2369 g_error_free(error);
2371 return;
2374 /* Note: Copying a style does not ref the pixmaps! */
2376 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2377 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2379 if (backdrop)
2380 style->bg_pixmap[GTK_STATE_NORMAL] =
2381 load_backdrop(backdrop, backdrop_style);
2383 gdk_color_parse(o_pinboard_bg_colour.value,
2384 &style->bg[GTK_STATE_NORMAL]);
2386 gtk_widget_set_style(pinboard->window, style);
2388 g_object_unref(style);
2390 gtk_widget_queue_draw(pinboard->window);
2392 /* Also update root window property (for transparent xterms, etc) */
2393 if (style->bg_pixmap[GTK_STATE_NORMAL])
2395 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2396 gdk_property_change(gdk_get_default_root_window(),
2397 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2398 gdk_atom_intern("PIXMAP", FALSE),
2399 32, GDK_PROP_MODE_REPLACE,
2400 (guchar *) &id, 1);
2402 else
2404 gdk_property_delete(gdk_get_default_root_window(),
2405 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2409 #define SEARCH_STEP 32
2411 static void search_free(GdkRectangle *rect, GdkRegion *used,
2412 int *outer, int od, int omax,
2413 int *inner, int id, int imax)
2415 *outer = od > 0 ? 0 : omax;
2416 while (*outer >= 0 && *outer <= omax)
2418 *inner = id > 0 ? 0 : imax;
2419 while (*inner >= 0 && *inner <= imax)
2421 if (gdk_region_rect_in(used, rect) ==
2422 GDK_OVERLAP_RECTANGLE_OUT)
2423 return;
2424 *inner += id;
2427 *outer += od;
2430 rect->x = -1;
2431 rect->y = -1;
2434 /* Finds a free area on the pinboard large enough for the width and height
2435 * of the given rectangle, by filling in the x and y fields of 'rect'.
2436 * If 'old' is true, 'rect' has a previous position and we first check
2437 * if it is viable.
2438 * The search order respects user preferences.
2439 * If no area is free, returns any old area.
2441 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2442 gboolean old)
2444 GdkRegion *used;
2445 GList *next;
2446 GdkRectangle used_rect;
2447 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2449 used = gdk_region_new();
2451 panel_mark_used(used);
2453 /* Subtract the no-go areas... */
2455 if (o_top_margin.int_value > 0)
2457 used_rect.x = 0;
2458 used_rect.y = 0;
2459 used_rect.width = gdk_screen_width();
2460 used_rect.height = o_top_margin.int_value;
2461 gdk_region_union_with_rect(used, &used_rect);
2464 if (o_bottom_margin.int_value > 0)
2466 used_rect.x = 0;
2467 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2468 used_rect.width = gdk_screen_width();
2469 used_rect.height = o_bottom_margin.int_value;
2470 gdk_region_union_with_rect(used, &used_rect);
2473 /* Subtract the used areas... */
2475 next = GTK_FIXED(pinboard->fixed)->children;
2476 for (; next; next = next->next)
2478 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2480 if (!GTK_WIDGET_VISIBLE(fix->widget))
2481 continue;
2483 used_rect.x = fix->x;
2484 used_rect.y = fix->y;
2485 used_rect.width = fix->widget->requisition.width;
2486 used_rect.height = fix->widget->requisition.height;
2488 gdk_region_union_with_rect(used, &used_rect);
2491 /* Check the previous area */
2492 if(old) {
2493 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2494 gdk_region_destroy(used);
2495 return;
2499 /* Find the first free area (yes, this isn't exactly pretty, but
2500 * it works). If you know a better (fast!) algorithm, let me know!
2504 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2505 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2506 dx = -SEARCH_STEP;
2508 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2509 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2510 dy = -SEARCH_STEP;
2512 if (o_iconify_dir.int_value == DIR_VERT)
2514 search_free(rect, used,
2515 &rect->x, dx, screen_width - rect->width,
2516 &rect->y, dy, screen_height - rect->height);
2518 else
2520 search_free(rect, used,
2521 &rect->y, dy, screen_height - rect->height,
2522 &rect->x, dx, screen_width - rect->width);
2525 gdk_region_destroy(used);
2527 if (rect->x == -1)
2529 rect->x = 0;
2530 rect->y = 0;
2534 /* Icon's size, shape or appearance has changed - update the display */
2535 static void pinboard_reshape_icon(Icon *icon)
2537 PinIcon *pi = (PinIcon *) icon;
2538 int x = pi->x, y = pi->y;
2540 set_size_and_style(pi);
2541 offset_from_centre(pi, &x, &y);
2543 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2545 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2546 pi->win, x, y);
2550 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2551 static void update_pinboard_font(void)
2553 if (pinboard_font)
2554 pango_font_description_free(pinboard_font);
2555 pinboard_font = o_label_font.value[0] != '\0'
2556 ? pango_font_description_from_string(o_label_font.value)
2557 : NULL;
2560 static void radios_changed(gpointer data)
2562 GObject *dialog = G_OBJECT(data);
2563 DropBox *drop_box;
2564 const guchar *path;
2565 Radios *radios;
2567 g_return_if_fail(dialog != NULL);
2569 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2570 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2572 g_return_if_fail(radios != NULL);
2573 g_return_if_fail(drop_box != NULL);
2574 g_return_if_fail(current_pinboard != NULL);
2576 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2578 path = drop_box_get_path(drop_box);
2579 if (path)
2580 pinboard_set_backdrop(path, radios_get_value(radios));
2584 static void update_radios(GtkWidget *dialog)
2586 Radios *radios;
2587 GtkWidget *hbox;
2589 g_return_if_fail(dialog != NULL);
2591 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2592 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2594 g_return_if_fail(current_pinboard != NULL);
2595 g_return_if_fail(radios != NULL);
2596 g_return_if_fail(hbox != NULL);
2598 switch (current_pinboard->backdrop_style)
2600 case BACKDROP_TILE:
2601 case BACKDROP_STRETCH:
2602 case BACKDROP_SCALE:
2603 case BACKDROP_CENTRE:
2604 radios_set_value(radios,
2605 current_pinboard->backdrop_style);
2606 gtk_widget_set_sensitive(hbox, TRUE);
2607 break;
2608 default:
2609 gtk_widget_set_sensitive(hbox, FALSE);
2610 radios_set_value(radios, BACKDROP_TILE);
2611 break;