r3723: Pinboard background image scaling was poor on some systems, can now choose...
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob0cc7753b7646604e3fcdce11184988736b7088ca
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;
153 static Option o_pinboard_image_scaling;
155 /* Static prototypes */
156 static GType pin_icon_get_type(void);
157 static void set_size_and_style(PinIcon *pi);
158 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
159 static gint end_wink(gpointer data);
160 static gboolean button_release_event(GtkWidget *widget,
161 GdkEventButton *event,
162 PinIcon *pi);
163 static gboolean enter_notify(GtkWidget *widget,
164 GdkEventCrossing *event,
165 PinIcon *pi);
166 static gint leave_notify(GtkWidget *widget,
167 GdkEventCrossing *event,
168 PinIcon *pi);
169 static gboolean button_press_event(GtkWidget *widget,
170 GdkEventButton *event,
171 PinIcon *pi);
172 static gboolean scroll_event(GtkWidget *widget,
173 GdkEventScroll *event);
174 static gint icon_motion_notify(GtkWidget *widget,
175 GdkEventMotion *event,
176 PinIcon *pi);
177 static const char *pin_from_file(gchar *line);
178 static void snap_to_grid(int *x, int *y);
179 static void offset_from_centre(PinIcon *pi, int *x, int *y);
180 static gboolean drag_motion(GtkWidget *widget,
181 GdkDragContext *context,
182 gint x,
183 gint y,
184 guint time,
185 PinIcon *pi);
186 static void drag_set_pinicon_dest(PinIcon *pi);
187 static void drag_leave(GtkWidget *widget,
188 GdkDragContext *context,
189 guint32 time,
190 PinIcon *pi);
191 static gboolean bg_drag_motion(GtkWidget *widget,
192 GdkDragContext *context,
193 gint x,
194 gint y,
195 guint time,
196 gpointer data);
197 static gboolean bg_drag_leave(GtkWidget *widget,
198 GdkDragContext *context,
199 guint32 time,
200 gpointer data);
201 static gboolean bg_expose(GtkWidget *window,
202 GdkEventExpose *event, gpointer data);
203 static void drag_end(GtkWidget *widget,
204 GdkDragContext *context,
205 PinIcon *pi);
206 static void reshape_all(void);
207 static void pinboard_check_options(void);
208 static void pinboard_load_from_xml(xmlDocPtr doc);
209 static void pinboard_clear(void);
210 static void pinboard_save(void);
211 static PinIcon *pin_icon_new(const char *pathname, const char *name);
212 static void pin_icon_destroyed(PinIcon *pi);
213 static void pin_icon_set_tip(PinIcon *pi);
214 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
215 static void create_pinboard_window(Pinboard *pinboard);
216 static void reload_backdrop(Pinboard *pinboard,
217 const gchar *backdrop,
218 BackdropStyle backdrop_style);
219 static void pinboard_reshape_icon(Icon *icon);
220 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
221 static void abandon_backdrop_app(Pinboard *pinboard);
222 static void drag_backdrop_dropped(GtkWidget *drop_box,
223 const guchar *path,
224 GtkWidget *dialog);
225 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
226 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
227 gboolean old);
228 static void update_pinboard_font(void);
229 static void draw_lasso(void);
230 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d);
231 static void clear_backdrop(GtkWidget *drop_box, gpointer data);
232 static void radios_changed(gpointer data);
233 static void update_radios(GtkWidget *dialog);
235 /****************************************************************
236 * EXTERNAL INTERFACE *
237 ****************************************************************/
239 void pinboard_init(void)
241 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
242 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
243 option_add_string(&o_pinboard_shadow_colour, "pinboard_shadow_colour",
244 "#000");
245 option_add_string(&o_label_font, "label_font", "");
246 option_add_int(&o_pinboard_shadow_labels, "pinboard_shadow_labels", 1);
248 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
249 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
250 GRID_STEP_COARSE);
251 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
252 option_add_int(&o_forward_buttons_13, "pinboard_forward_buttons_13",
253 FALSE);
255 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
256 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
258 option_add_int(&o_blackbox_hack, "blackbox_hack", FALSE);
260 option_add_int(&o_top_margin, "pinboard_top_margin", 0);
261 option_add_int(&o_bottom_margin, "pinboard_bottom_margin", 0);
263 option_add_int(&o_pinboard_image_scaling, "pinboard_image_scaling", 0);
265 option_add_notify(pinboard_check_options);
267 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
268 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
269 gdk_color_parse(o_pinboard_shadow_colour.value, &pin_text_shadow_col);
270 update_pinboard_font();
272 placed_icons=g_hash_table_new(g_str_hash, g_str_equal);
275 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
276 * and make it the current pinboard.
277 * Any existing pinned items are removed. You must call this
278 * at least once before using the pinboard. NULL disables the
279 * pinboard.
281 void pinboard_activate(const gchar *name)
283 Pinboard *old_board = current_pinboard;
284 guchar *path, *slash;
286 /* Treat an empty name the same as NULL */
287 if (name && !*name)
288 name = NULL;
290 if (old_board)
291 pinboard_clear();
293 if (!name)
295 if (number_of_windows < 1 && gtk_main_level() > 0)
296 gtk_main_quit();
298 gdk_property_delete(gdk_get_default_root_window(),
299 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
300 return;
303 number_of_windows++;
305 slash = strchr(name, '/');
306 if (slash)
308 if (access(name, F_OK))
309 path = NULL; /* File does not (yet) exist */
310 else
311 path = g_strdup(name);
313 else
315 guchar *leaf;
317 leaf = g_strconcat("pb_", name, NULL);
318 path = choices_find_path_load(leaf, PROJECT);
319 g_free(leaf);
322 current_pinboard = g_new(Pinboard, 1);
323 current_pinboard->name = g_strdup(name);
324 current_pinboard->icons = NULL;
325 current_pinboard->window = NULL;
326 current_pinboard->backdrop = NULL;
327 current_pinboard->backdrop_style = BACKDROP_NONE;
328 current_pinboard->to_backdrop_app = -1;
329 current_pinboard->from_backdrop_app = -1;
330 current_pinboard->input_tag = -1;
331 current_pinboard->input_buffer = NULL;
333 create_pinboard_window(current_pinboard);
335 loading_pinboard++;
336 if (path)
338 xmlDocPtr doc;
339 doc = xmlParseFile(path);
340 if (doc)
342 pinboard_load_from_xml(doc);
343 xmlFreeDoc(doc);
344 reload_backdrop(current_pinboard,
345 current_pinboard->backdrop,
346 current_pinboard->backdrop_style);
348 else
350 parse_file(path, pin_from_file);
351 info_message(_("Your old pinboard file has been "
352 "converted to the new XML format."));
353 pinboard_save();
355 g_free(path);
357 else
358 pinboard_pin(home_dir, "Home",
359 4 + ICON_WIDTH / 2,
360 4 + ICON_HEIGHT / 2,
361 NULL);
362 loading_pinboard--;
364 if (o_pinboard_tasklist.int_value)
365 tasklist_set_active(TRUE);
368 /* Return the window of the current pinboard, or NULL.
369 * Used to make sure lowering the panels doesn't lose them...
371 GdkWindow *pinboard_get_window(void)
373 if (current_pinboard)
374 return current_pinboard->window->window;
375 return NULL;
378 const char *pinboard_get_name(void)
380 g_return_val_if_fail(current_pinboard != NULL, NULL);
382 return current_pinboard->name;
385 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
386 * being cleared.
388 void pinboard_add_widget(GtkWidget *widget, const gchar *name)
390 GtkRequisition req;
391 GdkRectangle *rect=NULL;
392 gboolean found=FALSE;
394 g_return_if_fail(current_pinboard != NULL);
396 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, 0, 0);
398 gtk_widget_size_request(widget, &req);
400 if(name) {
401 rect=g_hash_table_lookup(placed_icons, name);
402 if(rect)
403 found=TRUE;
405 /*printf("%s at %p %d\n", name? name: "(nil)", rect, found);*/
407 if(rect) {
408 if(rect->width<req.width || rect->height<req.height) {
409 found=FALSE;
411 } else {
412 rect=g_new(GdkRectangle, 1);
413 rect->width = req.width;
414 rect->height = req.height;
416 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
417 rect->y, found);*/
418 find_free_rect(current_pinboard, rect, found);
419 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
420 rect->y, found);*/
422 gtk_fixed_move(GTK_FIXED(current_pinboard->fixed),
423 widget, rect->x, rect->y);
425 /* Store the new position (key and value are never freed) */
426 if(name)
427 g_hash_table_insert(placed_icons, g_strdup(name),
428 rect);
431 void pinboard_moved_widget(GtkWidget *widget, const gchar *name,
432 int x, int y)
434 GdkRectangle *rect;
436 if(!name)
437 return;
438 rect=g_hash_table_lookup(placed_icons, name);
439 if(!rect)
440 return;
442 rect->x=x;
443 rect->y=y;
446 /* Add a new icon to the background.
447 * 'path' should be an absolute pathname.
448 * 'x' and 'y' are the coordinates of the point in the middle of the text.
449 * 'name' is the name to use. If NULL then the leafname of path is used.
451 static void pinboard_pin_with_args(const gchar *path, const gchar *name,
452 int x, int y, const gchar *shortcut,
453 const gchar *args)
455 GtkWidget *align, *vbox;
456 GdkWindow *events;
457 PinIcon *pi;
458 Icon *icon;
460 g_return_if_fail(path != NULL);
461 g_return_if_fail(current_pinboard != NULL);
463 pi = pin_icon_new(path, name);
464 icon = (Icon *) pi;
465 pi->x = x;
466 pi->y = y;
468 /* This is a bit complicated...
470 * An icon needs to be a NO_WINDOW widget so that the image can
471 * blend with the background (A ParentRelative window also works, but
472 * is slow, causes the xfree86's memory consumption to grow without
473 * bound, and doesn't even get freed when the filer quits!).
475 * However, the icon also needs to have a window, so we get events
476 * delivered correctly. The solution is to float an InputOnly window
477 * over the icon. Since GtkButton works the same way, we just use
478 * that :-)
481 /* Button takes the initial ref of Icon */
482 pi->win = gtk_button_new();
483 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
484 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
485 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
487 vbox = gtk_vbox_new(FALSE, 0);
488 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
490 align = gtk_alignment_new(0.5, 0.5, 0, 0);
491 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
492 gtk_container_add(GTK_CONTAINER(align), pi->widget);
494 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
495 drag_set_pinicon_dest(pi);
496 g_signal_connect(pi->win, "drag_data_get",
497 G_CALLBACK(drag_data_get), NULL);
499 pi->label = wrapped_label_new(icon->item->leafname, 180);
500 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
502 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
504 snap_to_grid(&x, &y);
505 pi->x = x;
506 pi->y = y;
507 gtk_widget_show_all(pi->win);
508 pinboard_reshape_icon((Icon *) pi);
510 gtk_widget_realize(pi->win);
511 events = GTK_BUTTON(pi->win)->event_window;
512 gdk_window_set_events(events,
513 GDK_EXPOSURE_MASK |
514 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
515 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
516 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
517 GDK_BUTTON3_MOTION_MASK);
518 g_signal_connect(pi->win, "enter-notify-event",
519 G_CALLBACK(enter_notify), pi);
520 g_signal_connect(pi->win, "leave-notify-event",
521 G_CALLBACK(leave_notify), pi);
522 g_signal_connect(pi->win, "button-press-event",
523 G_CALLBACK(button_press_event), pi);
524 g_signal_connect(pi->win, "button-release-event",
525 G_CALLBACK(button_release_event), pi);
526 g_signal_connect(pi->win, "motion-notify-event",
527 G_CALLBACK(icon_motion_notify), pi);
528 g_signal_connect(pi->win, "expose-event",
529 G_CALLBACK(draw_icon), pi);
530 g_signal_connect_swapped(pi->win, "style-set",
531 G_CALLBACK(pinboard_reshape_icon), pi);
532 g_signal_connect_swapped(pi->win, "destroy",
533 G_CALLBACK(pin_icon_destroyed), pi);
535 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
536 pin_icon_set_tip(pi);
538 icon_set_shortcut(icon, shortcut);
539 icon_set_arguments(icon, args);
541 if (!loading_pinboard)
542 pinboard_save();
545 void pinboard_pin(const gchar *path, const gchar *name, int x, int y,
546 const gchar *shortcut)
548 pinboard_pin_with_args(path, name, x, y, shortcut, NULL);
552 * Remove an icon from the background. The first icon with a matching
553 * path is removed. If name is not NULL then that also must match
555 gboolean pinboard_remove(const gchar *path, const gchar *name)
557 GList *item;
558 PinIcon *pi;
559 Icon *icon;
561 g_return_val_if_fail(current_pinboard != NULL, FALSE);
563 for(item=current_pinboard->icons; item; item=g_list_next(item)) {
564 pi=(PinIcon *) item->data;
565 icon=(Icon *) pi;
567 if(strcmp(icon->path, path)!=0)
568 continue;
570 if(name && strcmp(name, icon->item->leafname)!=0)
571 continue;
573 icon_destroy(icon);
575 pinboard_save();
577 return TRUE;
580 return FALSE;
583 /* Put a border around the icon, briefly.
584 * If icon is NULL then cancel any existing wink.
585 * The icon will automatically unhighlight unless timeout is FALSE,
586 * in which case you must call this function again (with NULL or another
587 * icon) to remove the highlight.
589 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
591 PinIcon *old = current_wink_icon;
593 if (old == pi)
594 return;
596 current_wink_icon = pi;
598 if (old)
600 gtk_widget_queue_draw(old->win);
601 gdk_window_process_updates(old->widget->window, TRUE);
603 if (wink_timeout != -1)
604 g_source_remove(wink_timeout);
607 if (pi)
609 gtk_widget_queue_draw(pi->win);
610 gdk_window_process_updates(pi->widget->window, TRUE);
612 if (timeout)
613 wink_timeout = g_timeout_add(300, end_wink, NULL);
614 else
615 wink_timeout = -1;
619 /* 'app' is saved as the new application to set the backdrop. It will then be
620 * run, and should communicate with the filer as described in the manual.
622 void pinboard_set_backdrop_app(const gchar *app)
624 XMLwrapper *ai;
625 DirItem *item;
626 gboolean can_set;
628 item = diritem_new("");
629 diritem_restat(app, item, NULL);
630 if (!(item->flags & ITEM_FLAG_APPDIR))
632 delayed_error(_("The backdrop handler must be an application "
633 "directory. Drag an application directory "
634 "into the Set Backdrop dialog box, or (for "
635 "programmers) pass it to the SOAP "
636 "SetBackdropApp method."));
637 diritem_free(item);
638 return;
641 ai = appinfo_get(app, item);
642 diritem_free(item);
644 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
645 if (ai)
646 g_object_unref(ai);
648 if (can_set)
649 pinboard_set_backdrop(app, BACKDROP_PROGRAM);
650 else
651 delayed_error(_("You can only set the backdrop to an image "
652 "or to a program which knows how to "
653 "manage ROX-Filer's backdrop.\n\n"
654 "Programmers: the application's AppInfo.xml "
655 "must contain the CanSetBackdrop element, as "
656 "described in ROX-Filer's manual."));
659 /* Open a dialog box allowing the user to set the backdrop */
660 void pinboard_set_backdrop_box(void)
662 GtkWidget *dialog, *frame, *label, *hbox;
663 GtkBox *vbox;
664 Radios *radios;
666 g_return_if_fail(current_pinboard != NULL);
668 if (set_backdrop_dialog)
669 gtk_widget_destroy(set_backdrop_dialog);
671 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
672 GTK_DIALOG_NO_SEPARATOR,
673 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
674 NULL);
675 set_backdrop_dialog = dialog;
676 g_signal_connect(dialog, "destroy",
677 G_CALLBACK(gtk_widget_destroyed), &set_backdrop_dialog);
678 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
680 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
682 label = gtk_label_new(_("Choose a style and drag an image in:"));
683 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
684 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
685 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
687 /* The Centred, Scaled, Tiled radios... */
688 hbox = gtk_hbox_new(TRUE, 2);
689 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
691 radios = radios_new(radios_changed, dialog);
692 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
693 g_object_set_data(G_OBJECT(dialog), "rox-radios-hbox", hbox);
695 radios_add(radios, _("Centre the image without scaling it"),
696 BACKDROP_CENTRE, _("Centre"));
697 radios_add(radios, _("Scale the image to fit the backdrop area, "
698 "without distorting it"),
699 BACKDROP_SCALE, _("Scale"));
700 radios_add(radios, _("Stretch the image to fill the backdrop area"),
701 BACKDROP_STRETCH, _("Stretch"));
702 radios_add(radios, _("Tile the image over the backdrop area"),
703 BACKDROP_TILE, _("Tile"));
705 update_radios(dialog);
707 /* The drop area... */
708 frame = drop_box_new(_("Drop an image here"));
709 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
711 radios_pack(radios, GTK_BOX(hbox));
712 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
714 drop_box_set_path(DROP_BOX(frame), current_pinboard->backdrop);
716 g_signal_connect(frame, "path_dropped",
717 G_CALLBACK(drag_backdrop_dropped), dialog);
718 g_signal_connect(frame, "clear",
719 G_CALLBACK(clear_backdrop), dialog);
721 g_signal_connect(dialog, "response",
722 G_CALLBACK(backdrop_response), NULL);
723 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
725 gtk_widget_show_all(dialog);
728 /* Also used by tasklist.c */
729 void draw_label_shadow(WrappedLabel *wl, GdkRegion *region)
731 GtkWidget *widget;
732 gint x, y;
734 if (!o_pinboard_shadow_labels.int_value)
735 return;
737 gdk_gc_set_clip_region(current_pinboard->shadow_gc, region);
739 widget = GTK_WIDGET(wl);
741 y = widget->allocation.y - wl->y_off;
742 x = widget->allocation.x - wl->x_off -
743 ((wl->text_width - widget->allocation.width) >> 1);
745 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
746 x + 1, y + 1, wl->layout);
748 if (o_pinboard_shadow_labels.int_value > 1)
749 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
750 x + 2, y + 2, wl->layout);
752 gdk_gc_set_clip_region(current_pinboard->shadow_gc, NULL);
755 /* Set and save (path, style) as the new backdrop.
756 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
757 * Otherwise, the image is displayed now.
759 void pinboard_set_backdrop(const gchar *path, BackdropStyle style)
761 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
762 (path != NULL && style != BACKDROP_NONE));
764 if (!current_pinboard)
766 if (!path)
767 return;
768 pinboard_activate("Default");
769 delayed_error(_("No pinboard was in use... "
770 "the 'Default' pinboard has been selected. "
771 "Use 'rox -p=Default' to turn it on in "
772 "future."));
773 g_return_if_fail(current_pinboard != NULL);
776 /* We might have just run the old backdrop program and now
777 * we're going to set a new one! Seems a bit mean...
780 abandon_backdrop_app(current_pinboard);
782 g_free(current_pinboard->backdrop);
783 current_pinboard->backdrop = g_strdup(path);
784 current_pinboard->backdrop_style = style;
785 reload_backdrop(current_pinboard,
786 current_pinboard->backdrop,
787 current_pinboard->backdrop_style);
789 pinboard_save();
791 if (set_backdrop_dialog)
793 DropBox *box = g_object_get_data(G_OBJECT(set_backdrop_dialog),
794 "rox-dropbox");
795 g_return_if_fail(box != NULL);
796 drop_box_set_path(box, current_pinboard->backdrop);
797 update_radios(set_backdrop_dialog);
801 /* Called on xrandr screen resizes */
802 void pinboard_update_size(void)
804 int width, height;
806 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
807 &width, &height);
809 /* Only update the pinboard's size if the screen gets bigger,
810 * not smaller. Not sure what to do about icons that end up
811 * offscreen if the screen shrinks, but perhaps a good policy
812 * is to leave them there until the screen size is increased
813 * again rather than mess them around. */
814 width = MAX(width, screen_width);
815 height = MAX(height, screen_height);
817 gtk_widget_set_size_request(current_pinboard->window, width, height);
820 /****************************************************************
821 * INTERNAL FUNCTIONS *
822 ****************************************************************/
824 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
826 gtk_widget_destroy(dialog);
829 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
831 pinboard_set_backdrop(NULL, BACKDROP_NONE);
834 static void drag_backdrop_dropped(GtkWidget *drop_box,
835 const guchar *path,
836 GtkWidget *dialog)
838 struct stat info;
839 Radios *radios;
841 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
842 g_return_if_fail(radios != NULL);
844 if (mc_stat(path, &info))
846 delayed_error(
847 _("Can't access '%s':\n%s"), path,
848 g_strerror(errno));
849 return;
852 if (S_ISDIR(info.st_mode))
854 /* Use this program to set the backdrop */
855 pinboard_set_backdrop_app(path);
857 else if (S_ISREG(info.st_mode))
858 pinboard_set_backdrop(path, radios_get_value(radios));
859 else
860 delayed_error(_("Only files (and certain applications) can be "
861 "used to set the background image."));
864 /* Do this in the idle loop so that we don't try to put an unmanaged
865 * pinboard behind a managed panel (crashes some WMs).
867 static gboolean recreate_pinboard(gchar *name)
869 pinboard_activate(name);
870 g_free(name);
872 return FALSE;
875 static void pinboard_check_options(void)
877 GdkColor n_fg, n_bg, n_shadow;
879 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
880 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
881 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
883 if (o_override_redirect.has_changed && current_pinboard)
885 gchar *name;
886 name = g_strdup(current_pinboard->name);
887 pinboard_activate(NULL);
888 g_idle_add((GtkFunction) recreate_pinboard, name);
891 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
893 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
894 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
895 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
896 o_pinboard_shadow_labels.has_changed ||
897 o_label_font.has_changed)
899 pin_text_fg_col = n_fg;
900 pin_text_bg_col = n_bg;
901 pin_text_shadow_col = n_shadow;
902 update_pinboard_font();
904 if (current_pinboard)
906 GtkWidget *w = current_pinboard->window;
907 GdkColormap *cm;
909 cm = gtk_widget_get_colormap(w);
911 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
912 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
914 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
915 &n_shadow);
917 abandon_backdrop_app(current_pinboard);
918 reload_backdrop(current_pinboard,
919 current_pinboard->backdrop,
920 current_pinboard->backdrop_style);
922 reshape_all();
925 tasklist_style_changed();
929 static gint end_wink(gpointer data)
931 pinboard_wink_item(NULL, FALSE);
932 return FALSE;
935 /* Sets the appearance from the options and updates the size request of
936 * the image.
938 static void set_size_and_style(PinIcon *pi)
940 Icon *icon = (Icon *) pi;
941 MaskedPixmap *image = di_image(icon->item);
942 int iwidth = image->width;
943 int iheight = image->height;
945 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
946 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
947 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
948 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
949 widget_modify_font(pi->label, pinboard_font);
951 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
953 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
956 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
958 static GtkWidgetClass *parent_class = NULL;
959 Icon *icon = (Icon *) pi;
960 DirItem *item = icon->item;
961 MaskedPixmap *image = di_image(item);
962 int iwidth = image->width;
963 int iheight = image->height;
964 int x, y;
966 if (!parent_class)
968 gpointer c = ((GTypeInstance *) widget)->g_class;
969 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
972 x = pi->widget->allocation.x;
973 y = pi->widget->allocation.y;
975 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
977 render_pixbuf(icon->selected ? image->pixbuf_lit : image->pixbuf,
978 pi->widget->window,
979 pi->widget->style->black_gc,
980 x, y, iwidth, iheight);
982 if (item->flags & ITEM_FLAG_SYMLINK)
984 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
985 pi->widget->style->black_gc,
986 x, y, -1, -1);
988 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
990 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
991 ? im_mounted
992 : im_unmounted;
994 render_pixbuf(mp->pixbuf, pi->widget->window,
995 pi->widget->style->black_gc,
996 x, y, -1, -1);
999 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
1001 if (icon->selected)
1003 GtkStyle *style = pi->label->style;
1004 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
1006 gdk_gc_set_clip_region(gc, event->region);
1007 gdk_draw_rectangle(pi->label->window, gc, TRUE,
1008 pi->label->allocation.x,
1009 pi->label->allocation.y,
1010 pi->label->allocation.width,
1011 pi->label->allocation.height);
1012 gdk_gc_set_clip_region(gc, NULL);
1014 else if (o_pinboard_shadow_labels.int_value)
1015 draw_label_shadow((WrappedLabel *) pi->label, event->region);
1017 /* Draw children */
1018 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
1019 event->region);
1020 (parent_class->expose_event)(widget, event);
1021 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
1023 /* Stop the button effect */
1024 return TRUE;
1027 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
1029 gint x, y, width, height;
1031 if (current_wink_icon != pi)
1032 return FALSE;
1034 x = widget->allocation.x;
1035 y = widget->allocation.y;
1036 width = widget->allocation.width;
1037 height = widget->allocation.height;
1039 gdk_draw_rectangle(widget->window,
1040 pi->widget->style->white_gc,
1041 FALSE,
1042 x, y, width - 1, height - 1);
1043 gdk_draw_rectangle(widget->window,
1044 pi->widget->style->black_gc,
1045 FALSE,
1046 x + 1, y + 1, width - 3, height - 3);
1048 return FALSE;
1051 static gboolean enter_notify(GtkWidget *widget,
1052 GdkEventCrossing *event,
1053 PinIcon *pi)
1055 icon_may_update((Icon *) pi);
1056 pin_icon_set_tip(pi);
1057 return TRUE;
1060 static gint leave_notify(GtkWidget *widget,
1061 GdkEventCrossing *event,
1062 PinIcon *pi)
1064 return TRUE;
1067 static void select_lasso(void)
1069 GList *next;
1070 int minx, miny, maxx, maxy;
1072 g_return_if_fail(lasso_in_progress == TRUE);
1074 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1075 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1076 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1077 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1079 for (next = current_pinboard->icons; next; next = next->next)
1081 PinIcon *pi = (PinIcon *) next->data;
1082 GtkAllocation *alloc = &pi->win->allocation;
1083 int cx = alloc->x + alloc->width / 2;
1084 int cy = alloc->y + alloc->height / 2;
1086 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1087 icon_set_selected((Icon *) pi, TRUE);
1091 static void cancel_lasso(void)
1093 draw_lasso();
1094 lasso_in_progress = FALSE;
1097 static void pinboard_lasso_box(int start_x, int start_y)
1099 if (lasso_in_progress)
1100 cancel_lasso();
1101 lasso_in_progress = TRUE;
1102 lasso_rect_x1 = lasso_rect_x2 = start_x;
1103 lasso_rect_y1 = lasso_rect_y2 = start_y;
1105 draw_lasso();
1108 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1110 if (!lasso_in_progress)
1111 return FALSE;
1113 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1115 draw_lasso();
1116 lasso_rect_x2 = event->x;
1117 lasso_rect_y2 = event->y;
1118 draw_lasso();
1121 return FALSE;
1124 /* Mark the area of the screen covered by the lasso box for redraw */
1125 static void draw_lasso(void)
1127 GdkRectangle area, edge;
1129 if (!lasso_in_progress)
1130 return;
1132 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1133 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1134 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1135 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1137 edge.x = area.x;
1138 edge.y = area.y;
1139 edge.width = area.width;
1141 edge.height = 2; /* Top */
1142 gdk_window_invalidate_rect(current_pinboard->window->window,
1143 &edge, TRUE);
1145 edge.y += area.height - 2; /* Bottom */
1146 gdk_window_invalidate_rect(current_pinboard->window->window,
1147 &edge, TRUE);
1149 edge.y = area.y;
1150 edge.height = area.height;
1151 edge.width = 2; /* Left */
1152 gdk_window_invalidate_rect(current_pinboard->window->window,
1153 &edge, TRUE);
1155 edge.x += area.width - 2; /* Right */
1156 gdk_window_invalidate_rect(current_pinboard->window->window,
1157 &edge, TRUE);
1160 static void perform_action(PinIcon *pi, GdkEventButton *event)
1162 BindAction action;
1163 Icon *icon = (Icon *) pi;
1165 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1166 event);
1168 /* Actions that can happen with or without an icon */
1169 switch (action)
1171 case ACT_LASSO_CLEAR:
1172 icon_select_only(NULL);
1173 /* (no break) */
1174 case ACT_LASSO_MODIFY:
1175 pinboard_lasso_box(event->x, event->y);
1176 return;
1177 case ACT_CLEAR_SELECTION:
1178 icon_select_only(NULL);
1179 return;
1180 case ACT_POPUP_MENU:
1181 dnd_motion_ungrab();
1182 pinboard_show_menu(event, pi);
1183 return;
1184 case ACT_IGNORE:
1185 return;
1186 default:
1187 break;
1190 g_return_if_fail(pi != NULL);
1192 switch (action)
1194 case ACT_OPEN_ITEM:
1195 dnd_motion_ungrab();
1196 pinboard_wink_item(pi, TRUE);
1197 if (event->type == GDK_2BUTTON_PRESS)
1198 icon_set_selected(icon, FALSE);
1199 icon_run(icon);
1200 break;
1201 case ACT_EDIT_ITEM:
1202 dnd_motion_ungrab();
1203 pinboard_wink_item(pi, TRUE);
1204 if (event->type == GDK_2BUTTON_PRESS)
1205 icon_set_selected(icon, FALSE);
1206 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1207 break;
1208 case ACT_PRIME_AND_SELECT:
1209 if (!icon->selected)
1210 icon_select_only(icon);
1211 dnd_motion_start(MOTION_READY_FOR_DND);
1212 break;
1213 case ACT_PRIME_AND_TOGGLE:
1214 icon_set_selected(icon, !icon->selected);
1215 dnd_motion_start(MOTION_READY_FOR_DND);
1216 break;
1217 case ACT_PRIME_FOR_DND:
1218 dnd_motion_start(MOTION_READY_FOR_DND);
1219 break;
1220 case ACT_TOGGLE_SELECTED:
1221 icon_set_selected(icon, !icon->selected);
1222 break;
1223 case ACT_SELECT_EXCL:
1224 icon_select_only(icon);
1225 break;
1226 default:
1227 g_warning("Unsupported action : %d\n", action);
1228 break;
1232 static void forward_to_root(GdkEventButton *event)
1234 XButtonEvent xev;
1236 if (event->type == GDK_BUTTON_PRESS)
1238 xev.type = ButtonPress;
1239 if (!o_blackbox_hack.int_value)
1240 XUngrabPointer(gdk_display, event->time);
1242 else
1243 xev.type = ButtonRelease;
1245 xev.window = gdk_x11_get_default_root_xwindow();
1246 xev.root = xev.window;
1247 xev.subwindow = None;
1248 xev.time = event->time;
1249 xev.x = event->x_root; /* Needed for icewm */
1250 xev.y = event->y_root;
1251 xev.x_root = event->x_root;
1252 xev.y_root = event->y_root;
1253 xev.state = event->state;
1254 xev.button = event->button;
1255 xev.same_screen = True;
1257 XSendEvent(gdk_display, xev.window, False,
1258 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1261 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1262 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1264 /* pi is NULL if this is a root event */
1265 static gboolean button_release_event(GtkWidget *widget,
1266 GdkEventButton *event,
1267 PinIcon *pi)
1269 if (FORWARDED_BUTTON(pi, event->button))
1270 forward_to_root(event);
1271 else if (dnd_motion_release(event))
1273 if (motion_buttons_pressed == 0 && lasso_in_progress)
1275 select_lasso();
1276 cancel_lasso();
1278 return FALSE;
1281 perform_action(pi, event);
1283 return TRUE;
1286 /* pi is NULL if this is a root event */
1287 static gboolean button_press_event(GtkWidget *widget,
1288 GdkEventButton *event,
1289 PinIcon *pi)
1291 /* Just in case we've jumped in front of everything... */
1292 gdk_window_lower(current_pinboard->window->window);
1294 if (FORWARDED_BUTTON(pi, event->button))
1295 forward_to_root(event);
1296 else if (dnd_motion_press(widget, event))
1297 perform_action(pi, event);
1299 return TRUE;
1302 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1303 * (for old window managers that don't catch the buttons themselves)
1305 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1307 XButtonEvent xev;
1309 xev.type = ButtonPress;
1310 xev.window = gdk_x11_get_default_root_xwindow();
1311 xev.root = xev.window;
1312 xev.subwindow = None;
1313 xev.time = event->time;
1314 xev.x = event->x_root; /* Needed for icewm */
1315 xev.y = event->y_root;
1316 xev.x_root = event->x_root;
1317 xev.y_root = event->y_root;
1318 xev.state = event->state;
1319 xev.same_screen = True;
1321 if (event->direction == GDK_SCROLL_UP)
1322 xev.button = 4;
1323 else if (event->direction == GDK_SCROLL_DOWN)
1324 xev.button = 5;
1325 else
1326 return FALSE;
1328 XSendEvent(gdk_display, xev.window, False,
1329 ButtonPressMask, (XEvent *) &xev);
1331 return TRUE;
1334 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1336 GtkWidget *widget = pi->win;
1337 Icon *icon = (Icon *) pi;
1339 if (!icon->selected)
1341 tmp_icon_selected = TRUE;
1342 icon_select_only(icon);
1345 g_return_if_fail(icon_selection != NULL);
1347 pinboard_drag_in_progress = icon;
1349 if (icon_selection->next == NULL)
1350 drag_one_item(widget, event, icon->path, icon->item, NULL);
1351 else
1353 guchar *uri_list;
1355 uri_list = icon_create_uri_list();
1356 drag_selection(widget, event, uri_list);
1357 g_free(uri_list);
1361 /* An icon is being dragged around... */
1362 static gint icon_motion_notify(GtkWidget *widget,
1363 GdkEventMotion *event,
1364 PinIcon *pi)
1366 if (motion_state == MOTION_READY_FOR_DND)
1368 if (dnd_motion_moved(event))
1369 start_drag(pi, event);
1370 return TRUE;
1373 return FALSE;
1376 static void backdrop_from_xml(xmlNode *node)
1378 gchar *style;
1380 g_free(current_pinboard->backdrop);
1381 current_pinboard->backdrop = xmlNodeGetContent(node);
1383 style = xmlGetProp(node, "style");
1385 if (style)
1387 current_pinboard->backdrop_style =
1388 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1389 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1390 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1391 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1392 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1393 BACKDROP_NONE;
1394 g_free(style);
1396 else
1397 current_pinboard->backdrop_style = BACKDROP_TILE;
1400 /* Create one pinboard icon for each icon in the doc */
1401 static void pinboard_load_from_xml(xmlDocPtr doc)
1403 xmlNodePtr node, root;
1404 char *tmp, *label, *path, *shortcut, *args;
1405 int x, y;
1407 root = xmlDocGetRootElement(doc);
1409 for (node = root->xmlChildrenNode; node; node = node->next)
1411 if (node->type != XML_ELEMENT_NODE)
1412 continue;
1413 if (strcmp(node->name, "backdrop") == 0)
1415 backdrop_from_xml(node);
1416 continue;
1418 if (strcmp(node->name, "icon") != 0)
1419 continue;
1421 tmp = xmlGetProp(node, "x");
1422 if (!tmp)
1423 continue;
1424 x = atoi(tmp);
1425 g_free(tmp);
1427 tmp = xmlGetProp(node, "y");
1428 if (!tmp)
1429 continue;
1430 y = atoi(tmp);
1431 g_free(tmp);
1433 label = xmlGetProp(node, "label");
1434 if (!label)
1435 label = g_strdup("<missing label>");
1436 path = xmlNodeGetContent(node);
1437 if (!path)
1438 path = g_strdup("<missing path>");
1439 shortcut = xmlGetProp(node, "shortcut");
1440 args = xmlGetProp(node, "args");
1442 pinboard_pin_with_args(path, label, x, y, shortcut, args);
1444 g_free(path);
1445 g_free(label);
1446 g_free(shortcut);
1447 g_free(args);
1451 /* Called for each line in the pinboard file while loading a new board.
1452 * Only used for old-format files when converting to XML.
1454 static const char *pin_from_file(gchar *line)
1456 gchar *leaf = NULL;
1457 int x, y, n;
1459 if (*line == '<')
1461 gchar *end;
1463 end = strchr(line + 1, '>');
1464 if (!end)
1465 return _("Missing '>' in icon label");
1467 leaf = g_strndup(line + 1, end - line - 1);
1469 line = end + 1;
1471 while (g_ascii_isspace(*line))
1472 line++;
1473 if (*line != ',')
1474 return _("Missing ',' after icon label");
1475 line++;
1478 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1479 return NULL; /* Ignore format errors */
1481 pinboard_pin(line + n, leaf, x, y, NULL);
1483 g_free(leaf);
1485 return NULL;
1488 /* Write the current state of the pinboard to the current pinboard file */
1489 static void pinboard_save(void)
1491 guchar *save = NULL;
1492 guchar *save_new = NULL;
1493 GList *next;
1494 xmlDocPtr doc = NULL;
1495 xmlNodePtr root;
1497 g_return_if_fail(current_pinboard != NULL);
1499 if (strchr(current_pinboard->name, '/'))
1500 save = g_strdup(current_pinboard->name);
1501 else
1503 guchar *leaf;
1505 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1506 save = choices_find_path_save(leaf, PROJECT, TRUE);
1507 g_free(leaf);
1510 if (!save)
1511 return;
1513 doc = xmlNewDoc("1.0");
1514 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1516 root = xmlDocGetRootElement(doc);
1518 if (current_pinboard->backdrop)
1520 BackdropStyle style = current_pinboard->backdrop_style;
1521 xmlNodePtr tree;
1523 tree = xmlNewTextChild(root, NULL, "backdrop",
1524 current_pinboard->backdrop);
1525 xmlSetProp(tree, "style",
1526 style == BACKDROP_TILE ? "Tiled" :
1527 style == BACKDROP_CENTRE ? "Centred" :
1528 style == BACKDROP_SCALE ? "Scaled" :
1529 style == BACKDROP_STRETCH ? "Stretched" :
1530 "Program");
1533 for (next = current_pinboard->icons; next; next = next->next)
1535 xmlNodePtr tree;
1536 PinIcon *pi = (PinIcon *) next->data;
1537 Icon *icon = (Icon *) pi;
1538 char *tmp;
1540 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1542 tmp = g_strdup_printf("%d", pi->x);
1543 xmlSetProp(tree, "x", tmp);
1544 g_free(tmp);
1546 tmp = g_strdup_printf("%d", pi->y);
1547 xmlSetProp(tree, "y", tmp);
1548 g_free(tmp);
1550 xmlSetProp(tree, "label", icon->item->leafname);
1551 if (icon->shortcut)
1552 xmlSetProp(tree, "shortcut", icon->shortcut);
1553 if (icon->args)
1554 xmlSetProp(tree, "args", icon->args);
1557 save_new = g_strconcat(save, ".new", NULL);
1558 if (save_xml_file(doc, save_new) || rename(save_new, save))
1559 delayed_error(_("Error saving pinboard %s: %s"),
1560 save, g_strerror(errno));
1561 g_free(save_new);
1563 g_free(save);
1564 if (doc)
1565 xmlFreeDoc(doc);
1568 static void snap_to_grid(int *x, int *y)
1570 int step = o_pinboard_grid_step.int_value;
1572 *x = ((*x + step / 2) / step) * step;
1573 *y = ((*y + step / 2) / step) * step;
1576 /* Convert (x,y) from a centre point to a window position */
1577 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1579 gboolean clamp = o_pinboard_clamp_icons.int_value;
1580 GtkRequisition req;
1582 gtk_widget_size_request(pi->win, &req);
1584 *x -= req.width >> 1;
1585 *y -= req.height >> 1;
1586 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1587 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1590 /* Same as drag_set_dest(), but for pinboard icons */
1591 static void drag_set_pinicon_dest(PinIcon *pi)
1593 GtkObject *obj = GTK_OBJECT(pi->win);
1595 make_drop_target(pi->win, 0);
1597 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1598 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1599 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1602 /* Called during the drag when the mouse is in a widget registered
1603 * as a drop target. Returns TRUE if we can accept the drop.
1605 static gboolean drag_motion(GtkWidget *widget,
1606 GdkDragContext *context,
1607 gint x,
1608 gint y,
1609 guint time,
1610 PinIcon *pi)
1612 GdkDragAction action = context->suggested_action;
1613 const char *type = NULL;
1614 Icon *icon = (Icon *) pi;
1615 DirItem *item = icon->item;
1617 if (gtk_drag_get_source_widget(context) == widget)
1619 g_dataset_set_data(context, "drop_dest_type",
1620 (gpointer) drop_dest_pass_through);
1621 return FALSE; /* Can't drag something to itself! */
1624 if (icon->selected)
1625 goto out; /* Can't drag a selection to itself */
1627 type = dnd_motion_item(context, &item);
1629 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1630 && type != drop_dest_prog)
1632 guint state;
1633 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1634 if (state & GDK_BUTTON1_MASK)
1635 action = GDK_ACTION_ASK;
1638 if (!item)
1639 type = NULL;
1640 out:
1641 /* We actually must pretend to accept the drop, even if the
1642 * directory isn't writeable, so that the spring-opening
1643 * thing works.
1646 /* Don't allow drops to non-writeable directories */
1647 if (o_dnd_spring_open.int_value == FALSE &&
1648 type == drop_dest_dir &&
1649 access(icon->path, W_OK) != 0)
1651 type = NULL;
1654 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1655 if (type)
1657 gdk_drag_status(context, action, time);
1658 g_dataset_set_data_full(context, "drop_dest_path",
1659 g_strdup(icon->path), g_free);
1660 if (type == drop_dest_dir)
1661 dnd_spring_load(context, NULL);
1663 pinboard_wink_item(pi, FALSE);
1665 else
1666 gdk_drag_status(context, 0, time);
1668 /* Always return TRUE to stop the pinboard getting the events */
1669 return TRUE;
1672 static gboolean pinboard_shadow = FALSE;
1673 static gint shadow_x, shadow_y;
1674 #define SHADOW_SIZE (ICON_WIDTH)
1676 static gboolean bg_expose(GtkWidget *widget,
1677 GdkEventExpose *event, gpointer data)
1679 GdkRectangle clipbox;
1680 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1681 gboolean double_buffer;
1683 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1684 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1686 gdk_region_get_clipbox(event->region, &clipbox);
1688 double_buffer = (clipbox.width * clipbox.height) < 20000;
1689 if (double_buffer)
1690 gdk_window_begin_paint_region(widget->window, event->region);
1692 /* Clear the area to the background image */
1694 GtkStyle *style = current_pinboard->window->style;
1695 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1697 gdk_gc_set_clip_region(gc, event->region);
1698 if (style->bg_pixmap[GTK_STATE_NORMAL])
1700 gdk_gc_set_ts_origin(gc, 0, 0);
1701 gdk_gc_set_fill(gc, GDK_TILED);
1702 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1705 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1706 clipbox.x, clipbox.y,
1707 clipbox.width, clipbox.height);
1708 if (style->bg_pixmap[GTK_STATE_NORMAL])
1709 gdk_gc_set_fill(gc, GDK_SOLID);
1711 gdk_gc_set_clip_region(gc, NULL);
1714 if (pinboard_shadow)
1716 gdk_draw_rectangle(widget->window,
1717 widget->style->white_gc, FALSE,
1718 shadow_x, shadow_y,
1719 SHADOW_SIZE, SHADOW_SIZE);
1720 gdk_draw_rectangle(widget->window,
1721 widget->style->black_gc, FALSE,
1722 shadow_x + 1, shadow_y + 1,
1723 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1726 if (lasso_in_progress)
1728 GdkRectangle area;
1730 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1731 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1732 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1733 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1735 if (area.width > 4 && area.height > 4)
1737 gdk_draw_rectangle(widget->window,
1738 widget->style->white_gc, FALSE,
1739 area.x, area.y,
1740 area.width - 1, area.height - 1);
1741 gdk_draw_rectangle(widget->window,
1742 widget->style->black_gc, FALSE,
1743 area.x + 1, area.y + 1,
1744 area.width - 3, area.height - 3);
1748 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1749 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1751 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1753 if (double_buffer)
1754 gdk_window_end_paint(widget->window);
1756 return TRUE;
1759 /* Draw a 'shadow' under an icon being dragged, showing where
1760 * it will land.
1762 static void pinboard_set_shadow(gboolean on)
1764 GdkRectangle area;
1766 if (pinboard_shadow)
1768 area.x = shadow_x;
1769 area.y = shadow_y;
1770 area.width = SHADOW_SIZE + 1;
1771 area.height = SHADOW_SIZE + 1;
1773 gdk_window_invalidate_rect(current_pinboard->window->window,
1774 &area, TRUE);
1777 if (on)
1779 int old_x = shadow_x, old_y = shadow_y;
1781 gdk_window_get_pointer(current_pinboard->fixed->window,
1782 &shadow_x, &shadow_y, NULL);
1783 snap_to_grid(&shadow_x, &shadow_y);
1784 shadow_x -= SHADOW_SIZE / 2;
1785 shadow_y -= SHADOW_SIZE / 2;
1788 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1789 return;
1791 area.x = shadow_x;
1792 area.y = shadow_y;
1793 area.width = SHADOW_SIZE + 1;
1794 area.height = SHADOW_SIZE + 1;
1796 gdk_window_invalidate_rect(current_pinboard->window->window,
1797 &area, TRUE);
1800 pinboard_shadow = on;
1803 /* Called when dragging some pinboard icons finishes */
1804 void pinboard_move_icons(void)
1806 int x = shadow_x, y = shadow_y;
1807 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1808 int width, height;
1809 int dx, dy;
1810 GList *next;
1812 g_return_if_fail(pi != NULL);
1814 x += SHADOW_SIZE / 2;
1815 y += SHADOW_SIZE / 2;
1816 snap_to_grid(&x, &y);
1818 if (pi->x == x && pi->y == y)
1819 return;
1821 /* Find out how much the dragged icon moved (after snapping).
1822 * Move all selected icons by the same amount.
1824 dx = x - pi->x;
1825 dy = y - pi->y;
1827 /* Move the other selected icons to keep the same relative
1828 * position.
1830 for (next = icon_selection; next; next = next->next)
1832 PinIcon *pi = (PinIcon *) next->data;
1833 int nx, ny;
1835 g_return_if_fail(IS_PIN_ICON(pi));
1837 pi->x += dx;
1838 pi->y += dy;
1839 nx = pi->x;
1840 ny = pi->y;
1842 gdk_drawable_get_size(pi->win->window, &width, &height);
1843 offset_from_centre(pi, &nx, &ny);
1845 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1846 pi->win, nx, ny);
1849 pinboard_save();
1852 static void drag_leave(GtkWidget *widget,
1853 GdkDragContext *context,
1854 guint32 time,
1855 PinIcon *pi)
1857 pinboard_wink_item(NULL, FALSE);
1858 dnd_spring_abort();
1861 static gboolean bg_drag_leave(GtkWidget *widget,
1862 GdkDragContext *context,
1863 guint32 time,
1864 gpointer data)
1866 pinboard_set_shadow(FALSE);
1867 return TRUE;
1870 static gboolean bg_drag_motion(GtkWidget *widget,
1871 GdkDragContext *context,
1872 gint x,
1873 gint y,
1874 guint time,
1875 gpointer data)
1877 /* Dragging from the pinboard to the pinboard is not allowed */
1879 if (!provides(context, text_uri_list))
1880 return FALSE;
1882 pinboard_set_shadow(TRUE);
1884 gdk_drag_status(context,
1885 context->suggested_action == GDK_ACTION_ASK
1886 ? GDK_ACTION_LINK : context->suggested_action,
1887 time);
1888 return TRUE;
1891 static void drag_end(GtkWidget *widget,
1892 GdkDragContext *context,
1893 PinIcon *pi)
1895 pinboard_drag_in_progress = NULL;
1896 if (tmp_icon_selected)
1898 icon_select_only(NULL);
1899 tmp_icon_selected = FALSE;
1903 /* Something which affects all the icons has changed - reshape
1904 * and redraw all of them.
1906 static void reshape_all(void)
1908 GList *next;
1910 g_return_if_fail(current_pinboard != NULL);
1912 for (next = current_pinboard->icons; next; next = next->next)
1914 Icon *icon = (Icon *) next->data;
1915 pinboard_reshape_icon(icon);
1919 /* Turns off the pinboard. Does not call gtk_main_quit. */
1920 static void pinboard_clear(void)
1922 GList *next;
1924 g_return_if_fail(current_pinboard != NULL);
1926 tasklist_set_active(FALSE);
1928 next = current_pinboard->icons;
1929 while (next)
1931 PinIcon *pi = (PinIcon *) next->data;
1933 next = next->next;
1935 gtk_widget_destroy(pi->win);
1938 gtk_widget_destroy(current_pinboard->window);
1940 abandon_backdrop_app(current_pinboard);
1942 g_object_unref(current_pinboard->shadow_gc);
1943 current_pinboard->shadow_gc = NULL;
1945 g_free(current_pinboard->name);
1946 null_g_free(&current_pinboard);
1948 number_of_windows--;
1951 static gpointer parent_class;
1953 static void pin_icon_destroy(Icon *icon)
1955 PinIcon *pi = (PinIcon *) icon;
1957 g_return_if_fail(pi->win != NULL);
1959 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1960 gtk_widget_destroy(pi->win);
1963 static void pinboard_remove_items(void)
1965 g_return_if_fail(icon_selection != NULL);
1967 while (icon_selection)
1968 icon_destroy((Icon *) icon_selection->data);
1970 pinboard_save();
1973 static void pin_icon_update(Icon *icon)
1975 pinboard_reshape_icon(icon);
1976 pinboard_save();
1979 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1981 return IS_PIN_ICON(other);
1984 static void pin_wink_icon(Icon *icon)
1986 pinboard_wink_item((PinIcon *) icon, TRUE);
1989 static void pin_icon_class_init(gpointer gclass, gpointer data)
1991 IconClass *icon = (IconClass *) gclass;
1993 parent_class = g_type_class_peek_parent(gclass);
1995 icon->destroy = pin_icon_destroy;
1996 icon->redraw = pinboard_reshape_icon;
1997 icon->update = pin_icon_update;
1998 icon->wink = pin_wink_icon;
1999 icon->remove_items = pinboard_remove_items;
2000 icon->same_group = pin_icon_same_group;
2003 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
2007 static GType pin_icon_get_type(void)
2009 static GType type = 0;
2011 if (!type)
2013 static const GTypeInfo info =
2015 sizeof (PinIconClass),
2016 NULL, /* base_init */
2017 NULL, /* base_finalise */
2018 pin_icon_class_init,
2019 NULL, /* class_finalise */
2020 NULL, /* class_data */
2021 sizeof(PinIcon),
2022 0, /* n_preallocs */
2023 pin_icon_init
2026 type = g_type_register_static(icon_get_type(),
2027 "PinIcon", &info, 0);
2030 return type;
2033 static PinIcon *pin_icon_new(const char *pathname, const char *name)
2035 PinIcon *pi;
2036 Icon *icon;
2038 pi = g_object_new(pin_icon_get_type(), NULL);
2039 icon = (Icon *) pi;
2041 icon_set_path(icon, pathname, name);
2043 return pi;
2046 /* Called when the window widget is somehow destroyed */
2047 static void pin_icon_destroyed(PinIcon *pi)
2049 g_return_if_fail(pi->win != NULL);
2051 pi->win = NULL;
2053 pinboard_wink_item(NULL, FALSE);
2055 if (pinboard_drag_in_progress == (Icon *) pi)
2056 pinboard_drag_in_progress = NULL;
2058 if (current_pinboard)
2059 current_pinboard->icons =
2060 g_list_remove(current_pinboard->icons, pi);
2062 g_object_unref(pi);
2065 /* Set the tooltip */
2066 static void pin_icon_set_tip(PinIcon *pi)
2068 XMLwrapper *ai;
2069 xmlNode *node;
2070 Icon *icon = (Icon *) pi;
2072 g_return_if_fail(pi != NULL);
2074 ai = appinfo_get(icon->path, icon->item);
2076 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2078 guchar *str;
2079 str = xmlNodeListGetString(node->doc,
2080 node->xmlChildrenNode, 1);
2081 if (str)
2083 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2084 g_free(str);
2087 else
2088 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2090 if (ai)
2091 g_object_unref(ai);
2094 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2096 int pos[3];
2097 GList *list;
2099 pos[0] = event->x_root;
2100 pos[1] = event->y_root;
2102 icon_prepare_menu((Icon *) pi, TRUE);
2104 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2105 pos[2] = g_list_length(list) - 6;
2106 g_list_free(list);
2108 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2109 position_menu,
2110 (gpointer) pos, event->button, event->time);
2113 static void create_pinboard_window(Pinboard *pinboard)
2115 GtkWidget *win;
2117 g_return_if_fail(pinboard->window == NULL);
2119 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2120 gtk_widget_set_style(win, gtk_widget_get_default_style());
2122 gtk_widget_set_double_buffered(win, FALSE);
2123 gtk_widget_set_app_paintable(win, TRUE);
2124 gtk_widget_set_name(win, "rox-pinboard");
2125 pinboard->window = win;
2126 pinboard->fixed = gtk_fixed_new();
2127 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2129 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2131 gtk_widget_set_size_request(win, screen_width, screen_height);
2132 gtk_widget_realize(win);
2133 gtk_window_move(GTK_WINDOW(win), 0, 0);
2134 make_panel_window(win);
2136 /* TODO: Use gdk function when it supports this type */
2138 GdkAtom desktop_type;
2140 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2141 FALSE);
2142 gdk_property_change(win->window,
2143 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2144 gdk_atom_intern("ATOM", FALSE), 32,
2145 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2148 gtk_widget_add_events(win,
2149 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2150 GDK_EXPOSURE_MASK |
2151 GDK_BUTTON1_MOTION_MASK |
2152 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2153 g_signal_connect(win, "button-press-event",
2154 G_CALLBACK(button_press_event), NULL);
2155 g_signal_connect(win, "button-release-event",
2156 G_CALLBACK(button_release_event), NULL);
2157 g_signal_connect(win, "motion-notify-event",
2158 G_CALLBACK(lasso_motion), NULL);
2159 g_signal_connect(pinboard->fixed, "expose_event",
2160 G_CALLBACK(bg_expose), NULL);
2162 /* Some window managers use scroll events on the root to switch
2163 * desktops, but don't cope with our pinboard window, so we forward
2164 * them manually in that case.
2166 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2168 /* Drag and drop handlers */
2169 drag_set_pinboard_dest(win);
2170 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2171 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2173 pinboard->shadow_gc = gdk_gc_new(win->window);
2174 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2176 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2178 gtk_widget_show_all(win);
2179 gdk_window_lower(win->window);
2182 /* Load image 'path' and scale according to 'style' */
2183 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2185 GdkPixmap *pixmap;
2186 GdkPixbuf *pixbuf;
2187 GError *error = NULL;
2189 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2190 if (error)
2192 delayed_error(_("Error loading backdrop image:\n%s\n"
2193 "Backdrop removed."),
2194 error->message);
2195 g_error_free(error);
2196 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2197 return NULL;
2200 if (style == BACKDROP_STRETCH)
2202 GdkPixbuf *old = pixbuf;
2204 pixbuf = gdk_pixbuf_scale_simple(old,
2205 screen_width, screen_height,
2206 GDK_INTERP_HYPER);
2208 g_object_unref(old);
2210 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2212 GdkPixbuf *old = pixbuf;
2213 int x, y, width, height;
2214 float scale;
2216 width = gdk_pixbuf_get_width(pixbuf);
2217 height = gdk_pixbuf_get_height(pixbuf);
2219 if (style == BACKDROP_SCALE)
2221 float scale_x, scale_y;
2222 scale_x = screen_width / ((float) width);
2223 scale_y = screen_height / ((float) height);
2224 scale = MIN(scale_x, scale_y);
2226 else
2227 scale = 1;
2229 pixbuf = gdk_pixbuf_new(
2230 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2231 8, screen_width, screen_height);
2232 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2233 ((pin_text_bg_col.green & 0xff00) << 8) |
2234 ((pin_text_bg_col.blue & 0xff00)));
2236 x = (screen_width - width * scale) / 2;
2237 y = (screen_height - height * scale) / 2;
2238 x = MAX(x, 0);
2239 y = MAX(y, 0);
2241 gdk_pixbuf_composite(old, pixbuf,
2242 x, y,
2243 MIN(screen_width, width * scale),
2244 MIN(screen_height, height * scale),
2245 x, y, scale, scale,
2246 o_pinboard_image_scaling.int_value?
2247 GDK_INTERP_BILINEAR: GDK_INTERP_HYPER,
2248 255);
2249 g_object_unref(old);
2252 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2253 &pixmap, NULL, 0);
2254 g_object_unref(pixbuf);
2256 return pixmap;
2259 static void abandon_backdrop_app(Pinboard *pinboard)
2261 g_return_if_fail(pinboard != NULL);
2263 if (pinboard->to_backdrop_app != -1)
2265 close(pinboard->to_backdrop_app);
2266 close(pinboard->from_backdrop_app);
2267 g_source_remove(pinboard->input_tag);
2268 g_string_free(pinboard->input_buffer, TRUE);
2269 pinboard->to_backdrop_app = -1;
2270 pinboard->from_backdrop_app = -1;
2271 pinboard->input_tag = -1;
2272 pinboard->input_buffer = NULL;
2275 g_return_if_fail(pinboard->to_backdrop_app == -1);
2276 g_return_if_fail(pinboard->from_backdrop_app == -1);
2277 g_return_if_fail(pinboard->input_tag == -1);
2278 g_return_if_fail(pinboard->input_buffer == NULL);
2281 /* A single line has been read from the child.
2282 * Processes the command, and replies 'ok' (or abandons the child on error).
2284 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2286 BackdropStyle style;
2287 const char *ok = "ok\n";
2289 if (strncmp(command, "tile ", 5) == 0)
2291 style = BACKDROP_TILE;
2292 command += 5;
2294 else if (strncmp(command, "scale ", 6) == 0)
2296 style = BACKDROP_SCALE;
2297 command += 6;
2299 else if (strncmp(command, "stretch ", 8) == 0)
2301 style = BACKDROP_STRETCH;
2302 command += 8;
2304 else if (strncmp(command, "centre ", 7) == 0)
2306 style = BACKDROP_CENTRE;
2307 command += 7;
2309 else
2311 g_warning("Invalid command '%s' from backdrop app\n",
2312 command);
2313 abandon_backdrop_app(pinboard);
2314 return;
2317 /* Load the backdrop. May abandon the program if loading fails. */
2318 reload_backdrop(pinboard, command, style);
2320 if (pinboard->to_backdrop_app == -1)
2321 return;
2323 while (*ok)
2325 int sent;
2327 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2328 if (sent <= 0)
2330 /* Remote app quit? Not an error. */
2331 abandon_backdrop_app(pinboard);
2332 break;
2334 ok += sent;
2338 static void backdrop_from_child(Pinboard *pinboard,
2339 int src, GdkInputCondition cond)
2341 char buf[256];
2342 int got;
2344 got = read(src, buf, sizeof(buf));
2346 if (got <= 0)
2348 if (got < 0)
2349 g_warning("backdrop_from_child: %s\n",
2350 g_strerror(errno));
2351 abandon_backdrop_app(pinboard);
2352 return;
2355 g_string_append_len(pinboard->input_buffer, buf, got);
2357 while (pinboard->from_backdrop_app != -1)
2359 int len;
2360 char *nl, *command;
2362 nl = strchr(pinboard->input_buffer->str, '\n');
2363 if (!nl)
2364 return; /* Haven't got a whole line yet */
2366 len = nl - pinboard->input_buffer->str;
2367 command = g_strndup(pinboard->input_buffer->str, len);
2368 g_string_erase(pinboard->input_buffer, 0, len + 1);
2370 command_from_backdrop_app(pinboard, command);
2372 g_free(command);
2376 static void reload_backdrop(Pinboard *pinboard,
2377 const gchar *backdrop,
2378 BackdropStyle backdrop_style)
2380 GtkStyle *style;
2382 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2384 const char *argv[] = {NULL, "--backdrop", NULL};
2385 GError *error = NULL;
2387 g_return_if_fail(pinboard->to_backdrop_app == -1);
2388 g_return_if_fail(pinboard->from_backdrop_app == -1);
2389 g_return_if_fail(pinboard->input_tag == -1);
2390 g_return_if_fail(pinboard->input_buffer == NULL);
2392 argv[0] = make_path(backdrop, "AppRun");
2394 /* Run the program. It'll send us a SOAP message and we'll
2395 * get back here with a different style and image.
2398 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2399 G_SPAWN_DO_NOT_REAP_CHILD |
2400 G_SPAWN_SEARCH_PATH,
2401 NULL, NULL, /* Child setup fn */
2402 NULL, /* Child PID */
2403 &pinboard->to_backdrop_app,
2404 &pinboard->from_backdrop_app,
2405 NULL, /* Standard error */
2406 &error))
2408 pinboard->input_buffer = g_string_new(NULL);
2409 pinboard->input_tag = gdk_input_add_full(
2410 pinboard->from_backdrop_app,
2411 GDK_INPUT_READ,
2412 (GdkInputFunction) backdrop_from_child,
2413 pinboard, NULL);
2415 else
2417 delayed_error("%s", error ? error->message : "(null)");
2418 g_error_free(error);
2420 return;
2423 /* Note: Copying a style does not ref the pixmaps! */
2425 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2426 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2428 if (backdrop)
2429 style->bg_pixmap[GTK_STATE_NORMAL] =
2430 load_backdrop(backdrop, backdrop_style);
2432 gdk_color_parse(o_pinboard_bg_colour.value,
2433 &style->bg[GTK_STATE_NORMAL]);
2435 gtk_widget_set_style(pinboard->window, style);
2437 g_object_unref(style);
2439 gtk_widget_queue_draw(pinboard->window);
2441 /* Also update root window property (for transparent xterms, etc) */
2442 if (style->bg_pixmap[GTK_STATE_NORMAL])
2444 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2445 gdk_property_change(gdk_get_default_root_window(),
2446 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2447 gdk_atom_intern("PIXMAP", FALSE),
2448 32, GDK_PROP_MODE_REPLACE,
2449 (guchar *) &id, 1);
2451 else
2453 gdk_property_delete(gdk_get_default_root_window(),
2454 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2458 #define SEARCH_STEP 32
2460 static void search_free(GdkRectangle *rect, GdkRegion *used,
2461 int *outer, int od, int omax,
2462 int *inner, int id, int imax)
2464 *outer = od > 0 ? 0 : omax;
2465 while (*outer >= 0 && *outer <= omax)
2467 *inner = id > 0 ? 0 : imax;
2468 while (*inner >= 0 && *inner <= imax)
2470 if (gdk_region_rect_in(used, rect) ==
2471 GDK_OVERLAP_RECTANGLE_OUT)
2472 return;
2473 *inner += id;
2476 *outer += od;
2479 rect->x = -1;
2480 rect->y = -1;
2483 /* Finds a free area on the pinboard large enough for the width and height
2484 * of the given rectangle, by filling in the x and y fields of 'rect'.
2485 * If 'old' is true, 'rect' has a previous position and we first check
2486 * if it is viable.
2487 * The search order respects user preferences.
2488 * If no area is free, returns any old area.
2490 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2491 gboolean old)
2493 GdkRegion *used;
2494 GList *next;
2495 GdkRectangle used_rect;
2496 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2498 used = gdk_region_new();
2500 panel_mark_used(used);
2502 /* Subtract the no-go areas... */
2504 if (o_top_margin.int_value > 0)
2506 used_rect.x = 0;
2507 used_rect.y = 0;
2508 used_rect.width = gdk_screen_width();
2509 used_rect.height = o_top_margin.int_value;
2510 gdk_region_union_with_rect(used, &used_rect);
2513 if (o_bottom_margin.int_value > 0)
2515 used_rect.x = 0;
2516 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2517 used_rect.width = gdk_screen_width();
2518 used_rect.height = o_bottom_margin.int_value;
2519 gdk_region_union_with_rect(used, &used_rect);
2522 /* Subtract the used areas... */
2524 next = GTK_FIXED(pinboard->fixed)->children;
2525 for (; next; next = next->next)
2527 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2529 if (!GTK_WIDGET_VISIBLE(fix->widget))
2530 continue;
2532 used_rect.x = fix->x;
2533 used_rect.y = fix->y;
2534 used_rect.width = fix->widget->requisition.width;
2535 used_rect.height = fix->widget->requisition.height;
2537 gdk_region_union_with_rect(used, &used_rect);
2540 /* Check the previous area */
2541 if(old) {
2542 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2543 gdk_region_destroy(used);
2544 return;
2548 /* Find the first free area (yes, this isn't exactly pretty, but
2549 * it works). If you know a better (fast!) algorithm, let me know!
2553 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2554 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2555 dx = -SEARCH_STEP;
2557 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2558 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2559 dy = -SEARCH_STEP;
2561 if (o_iconify_dir.int_value == DIR_VERT)
2563 search_free(rect, used,
2564 &rect->x, dx, screen_width - rect->width,
2565 &rect->y, dy, screen_height - rect->height);
2567 else
2569 search_free(rect, used,
2570 &rect->y, dy, screen_height - rect->height,
2571 &rect->x, dx, screen_width - rect->width);
2574 gdk_region_destroy(used);
2576 if (rect->x == -1)
2578 rect->x = 0;
2579 rect->y = 0;
2583 /* Icon's size, shape or appearance has changed - update the display */
2584 static void pinboard_reshape_icon(Icon *icon)
2586 PinIcon *pi = (PinIcon *) icon;
2587 int x = pi->x, y = pi->y;
2589 set_size_and_style(pi);
2590 offset_from_centre(pi, &x, &y);
2592 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2594 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2595 pi->win, x, y);
2599 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2600 static void update_pinboard_font(void)
2602 if (pinboard_font)
2603 pango_font_description_free(pinboard_font);
2604 pinboard_font = o_label_font.value[0] != '\0'
2605 ? pango_font_description_from_string(o_label_font.value)
2606 : NULL;
2609 static void radios_changed(gpointer data)
2611 GObject *dialog = G_OBJECT(data);
2612 DropBox *drop_box;
2613 const guchar *path;
2614 Radios *radios;
2616 g_return_if_fail(dialog != NULL);
2618 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2619 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2621 g_return_if_fail(radios != NULL);
2622 g_return_if_fail(drop_box != NULL);
2623 g_return_if_fail(current_pinboard != NULL);
2625 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2627 path = drop_box_get_path(drop_box);
2628 if (path)
2629 pinboard_set_backdrop(path, radios_get_value(radios));
2633 static void update_radios(GtkWidget *dialog)
2635 Radios *radios;
2636 GtkWidget *hbox;
2638 g_return_if_fail(dialog != NULL);
2640 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2641 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2643 g_return_if_fail(current_pinboard != NULL);
2644 g_return_if_fail(radios != NULL);
2645 g_return_if_fail(hbox != NULL);
2647 switch (current_pinboard->backdrop_style)
2649 case BACKDROP_TILE:
2650 case BACKDROP_STRETCH:
2651 case BACKDROP_SCALE:
2652 case BACKDROP_CENTRE:
2653 radios_set_value(radios,
2654 current_pinboard->backdrop_style);
2655 gtk_widget_set_sensitive(hbox, TRUE);
2656 break;
2657 default:
2658 gtk_widget_set_sensitive(hbox, FALSE);
2659 radios_set_value(radios, BACKDROP_TILE);
2660 break;