r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / pinboard.c
blobc0fa0245ac1d7963a4d494a2a125560440918f30
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, 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 if (!current_pinboard)
807 return;
809 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
810 &width, &height);
812 /* Only update the pinboard's size if the screen gets bigger,
813 * not smaller. Not sure what to do about icons that end up
814 * offscreen if the screen shrinks, but perhaps a good policy
815 * is to leave them there until the screen size is increased
816 * again rather than mess them around. */
817 width = MAX(width, screen_width);
818 height = MAX(height, screen_height);
820 gtk_widget_set_size_request(current_pinboard->window, width, height);
823 /****************************************************************
824 * INTERNAL FUNCTIONS *
825 ****************************************************************/
827 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
829 gtk_widget_destroy(dialog);
832 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
834 pinboard_set_backdrop(NULL, BACKDROP_NONE);
837 static void drag_backdrop_dropped(GtkWidget *drop_box,
838 const guchar *path,
839 GtkWidget *dialog)
841 struct stat info;
842 Radios *radios;
844 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
845 g_return_if_fail(radios != NULL);
847 if (mc_stat(path, &info))
849 delayed_error(
850 _("Can't access '%s':\n%s"), path,
851 g_strerror(errno));
852 return;
855 if (S_ISDIR(info.st_mode))
857 /* Use this program to set the backdrop */
858 pinboard_set_backdrop_app(path);
860 else if (S_ISREG(info.st_mode))
861 pinboard_set_backdrop(path, radios_get_value(radios));
862 else
863 delayed_error(_("Only files (and certain applications) can be "
864 "used to set the background image."));
867 /* Do this in the idle loop so that we don't try to put an unmanaged
868 * pinboard behind a managed panel (crashes some WMs).
870 static gboolean recreate_pinboard(gchar *name)
872 pinboard_activate(name);
873 g_free(name);
875 return FALSE;
878 static void pinboard_check_options(void)
880 GdkColor n_fg, n_bg, n_shadow;
882 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
883 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
884 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
886 if (o_override_redirect.has_changed && current_pinboard)
888 gchar *name;
889 name = g_strdup(current_pinboard->name);
890 pinboard_activate(NULL);
891 g_idle_add((GtkFunction) recreate_pinboard, name);
894 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
896 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
897 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
898 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
899 o_pinboard_shadow_labels.has_changed ||
900 o_label_font.has_changed)
902 pin_text_fg_col = n_fg;
903 pin_text_bg_col = n_bg;
904 pin_text_shadow_col = n_shadow;
905 update_pinboard_font();
907 if (current_pinboard)
909 GtkWidget *w = current_pinboard->window;
910 GdkColormap *cm;
912 cm = gtk_widget_get_colormap(w);
914 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
915 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
917 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
918 &n_shadow);
920 abandon_backdrop_app(current_pinboard);
921 reload_backdrop(current_pinboard,
922 current_pinboard->backdrop,
923 current_pinboard->backdrop_style);
925 reshape_all();
928 tasklist_style_changed();
932 static gint end_wink(gpointer data)
934 pinboard_wink_item(NULL, FALSE);
935 return FALSE;
938 /* Sets the appearance from the options and updates the size request of
939 * the image.
941 static void set_size_and_style(PinIcon *pi)
943 Icon *icon = (Icon *) pi;
944 MaskedPixmap *image = di_image(icon->item);
945 int iwidth = image->width;
946 int iheight = image->height;
948 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
949 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
950 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
951 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
952 widget_modify_font(pi->label, pinboard_font);
954 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
956 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
959 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
961 static GtkWidgetClass *parent_class = NULL;
962 Icon *icon = (Icon *) pi;
963 DirItem *item = icon->item;
964 MaskedPixmap *image = di_image(item);
965 int iwidth = image->width;
966 int iheight = image->height;
967 int x, y;
969 if (!parent_class)
971 gpointer c = ((GTypeInstance *) widget)->g_class;
972 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
975 x = pi->widget->allocation.x;
976 y = pi->widget->allocation.y;
978 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
980 render_pixbuf(icon->selected ? image->pixbuf_lit : image->pixbuf,
981 pi->widget->window,
982 pi->widget->style->black_gc,
983 x, y, iwidth, iheight);
985 if (item->flags & ITEM_FLAG_SYMLINK)
987 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
988 pi->widget->style->black_gc,
989 x, y, -1, -1);
991 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
993 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
994 ? im_mounted
995 : im_unmounted;
997 render_pixbuf(mp->pixbuf, pi->widget->window,
998 pi->widget->style->black_gc,
999 x, y, -1, -1);
1002 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
1004 if (icon->selected)
1006 GtkStyle *style = pi->label->style;
1007 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
1009 gdk_gc_set_clip_region(gc, event->region);
1010 gdk_draw_rectangle(pi->label->window, gc, TRUE,
1011 pi->label->allocation.x,
1012 pi->label->allocation.y,
1013 pi->label->allocation.width,
1014 pi->label->allocation.height);
1015 gdk_gc_set_clip_region(gc, NULL);
1017 else if (o_pinboard_shadow_labels.int_value)
1018 draw_label_shadow((WrappedLabel *) pi->label, event->region);
1020 /* Draw children */
1021 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
1022 event->region);
1023 (parent_class->expose_event)(widget, event);
1024 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
1026 /* Stop the button effect */
1027 return TRUE;
1030 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
1032 gint x, y, width, height;
1034 if (current_wink_icon != pi)
1035 return FALSE;
1037 x = widget->allocation.x;
1038 y = widget->allocation.y;
1039 width = widget->allocation.width;
1040 height = widget->allocation.height;
1042 gdk_draw_rectangle(widget->window,
1043 pi->widget->style->white_gc,
1044 FALSE,
1045 x, y, width - 1, height - 1);
1046 gdk_draw_rectangle(widget->window,
1047 pi->widget->style->black_gc,
1048 FALSE,
1049 x + 1, y + 1, width - 3, height - 3);
1051 return FALSE;
1054 static gboolean enter_notify(GtkWidget *widget,
1055 GdkEventCrossing *event,
1056 PinIcon *pi)
1058 icon_may_update((Icon *) pi);
1059 pin_icon_set_tip(pi);
1060 return TRUE;
1063 static gint leave_notify(GtkWidget *widget,
1064 GdkEventCrossing *event,
1065 PinIcon *pi)
1067 return TRUE;
1070 static void select_lasso(void)
1072 GList *next;
1073 int minx, miny, maxx, maxy;
1075 g_return_if_fail(lasso_in_progress == TRUE);
1077 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1078 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1079 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1080 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1082 for (next = current_pinboard->icons; next; next = next->next)
1084 PinIcon *pi = (PinIcon *) next->data;
1085 GtkAllocation *alloc = &pi->win->allocation;
1086 int cx = alloc->x + alloc->width / 2;
1087 int cy = alloc->y + alloc->height / 2;
1089 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1090 icon_set_selected((Icon *) pi, TRUE);
1094 static void cancel_lasso(void)
1096 draw_lasso();
1097 lasso_in_progress = FALSE;
1100 static void pinboard_lasso_box(int start_x, int start_y)
1102 if (lasso_in_progress)
1103 cancel_lasso();
1104 lasso_in_progress = TRUE;
1105 lasso_rect_x1 = lasso_rect_x2 = start_x;
1106 lasso_rect_y1 = lasso_rect_y2 = start_y;
1108 draw_lasso();
1111 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1113 if (!lasso_in_progress)
1114 return FALSE;
1116 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1118 draw_lasso();
1119 lasso_rect_x2 = event->x;
1120 lasso_rect_y2 = event->y;
1121 draw_lasso();
1124 return FALSE;
1127 /* Mark the area of the screen covered by the lasso box for redraw */
1128 static void draw_lasso(void)
1130 GdkRectangle area, edge;
1132 if (!lasso_in_progress)
1133 return;
1135 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1136 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1137 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1138 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1140 edge.x = area.x;
1141 edge.y = area.y;
1142 edge.width = area.width;
1144 edge.height = 2; /* Top */
1145 gdk_window_invalidate_rect(current_pinboard->window->window,
1146 &edge, TRUE);
1148 edge.y += area.height - 2; /* Bottom */
1149 gdk_window_invalidate_rect(current_pinboard->window->window,
1150 &edge, TRUE);
1152 edge.y = area.y;
1153 edge.height = area.height;
1154 edge.width = 2; /* Left */
1155 gdk_window_invalidate_rect(current_pinboard->window->window,
1156 &edge, TRUE);
1158 edge.x += area.width - 2; /* Right */
1159 gdk_window_invalidate_rect(current_pinboard->window->window,
1160 &edge, TRUE);
1163 static void perform_action(PinIcon *pi, GdkEventButton *event)
1165 BindAction action;
1166 Icon *icon = (Icon *) pi;
1168 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1169 event);
1171 /* Actions that can happen with or without an icon */
1172 switch (action)
1174 case ACT_LASSO_CLEAR:
1175 icon_select_only(NULL);
1176 /* (no break) */
1177 case ACT_LASSO_MODIFY:
1178 pinboard_lasso_box(event->x, event->y);
1179 return;
1180 case ACT_CLEAR_SELECTION:
1181 icon_select_only(NULL);
1182 return;
1183 case ACT_POPUP_MENU:
1184 dnd_motion_ungrab();
1185 pinboard_show_menu(event, pi);
1186 return;
1187 case ACT_IGNORE:
1188 return;
1189 default:
1190 break;
1193 g_return_if_fail(pi != NULL);
1195 switch (action)
1197 case ACT_OPEN_ITEM:
1198 dnd_motion_ungrab();
1199 pinboard_wink_item(pi, TRUE);
1200 if (event->type == GDK_2BUTTON_PRESS)
1201 icon_set_selected(icon, FALSE);
1202 icon_run(icon);
1203 break;
1204 case ACT_EDIT_ITEM:
1205 dnd_motion_ungrab();
1206 pinboard_wink_item(pi, TRUE);
1207 if (event->type == GDK_2BUTTON_PRESS)
1208 icon_set_selected(icon, FALSE);
1209 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1210 break;
1211 case ACT_PRIME_AND_SELECT:
1212 if (!icon->selected)
1213 icon_select_only(icon);
1214 dnd_motion_start(MOTION_READY_FOR_DND);
1215 break;
1216 case ACT_PRIME_AND_TOGGLE:
1217 icon_set_selected(icon, !icon->selected);
1218 dnd_motion_start(MOTION_READY_FOR_DND);
1219 break;
1220 case ACT_PRIME_FOR_DND:
1221 dnd_motion_start(MOTION_READY_FOR_DND);
1222 break;
1223 case ACT_TOGGLE_SELECTED:
1224 icon_set_selected(icon, !icon->selected);
1225 break;
1226 case ACT_SELECT_EXCL:
1227 icon_select_only(icon);
1228 break;
1229 default:
1230 g_warning("Unsupported action : %d\n", action);
1231 break;
1235 static void forward_to_root(GdkEventButton *event)
1237 XButtonEvent xev;
1239 if (event->type == GDK_BUTTON_PRESS)
1241 xev.type = ButtonPress;
1242 if (!o_blackbox_hack.int_value)
1243 XUngrabPointer(gdk_display, event->time);
1245 else
1246 xev.type = ButtonRelease;
1248 xev.window = gdk_x11_get_default_root_xwindow();
1249 xev.root = xev.window;
1250 xev.subwindow = None;
1251 xev.time = event->time;
1252 xev.x = event->x_root; /* Needed for icewm */
1253 xev.y = event->y_root;
1254 xev.x_root = event->x_root;
1255 xev.y_root = event->y_root;
1256 xev.state = event->state;
1257 xev.button = event->button;
1258 xev.same_screen = True;
1260 XSendEvent(gdk_display, xev.window, False,
1261 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1264 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1265 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1267 /* pi is NULL if this is a root event */
1268 static gboolean button_release_event(GtkWidget *widget,
1269 GdkEventButton *event,
1270 PinIcon *pi)
1272 if (FORWARDED_BUTTON(pi, event->button))
1273 forward_to_root(event);
1274 else if (dnd_motion_release(event))
1276 if (motion_buttons_pressed == 0 && lasso_in_progress)
1278 select_lasso();
1279 cancel_lasso();
1281 return FALSE;
1284 perform_action(pi, event);
1286 return TRUE;
1289 /* pi is NULL if this is a root event */
1290 static gboolean button_press_event(GtkWidget *widget,
1291 GdkEventButton *event,
1292 PinIcon *pi)
1294 /* Just in case we've jumped in front of everything... */
1295 gdk_window_lower(current_pinboard->window->window);
1297 if (FORWARDED_BUTTON(pi, event->button))
1298 forward_to_root(event);
1299 else if (dnd_motion_press(widget, event))
1300 perform_action(pi, event);
1302 return TRUE;
1305 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1306 * (for old window managers that don't catch the buttons themselves)
1308 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1310 XButtonEvent xev;
1312 xev.type = ButtonPress;
1313 xev.window = gdk_x11_get_default_root_xwindow();
1314 xev.root = xev.window;
1315 xev.subwindow = None;
1316 xev.time = event->time;
1317 xev.x = event->x_root; /* Needed for icewm */
1318 xev.y = event->y_root;
1319 xev.x_root = event->x_root;
1320 xev.y_root = event->y_root;
1321 xev.state = event->state;
1322 xev.same_screen = True;
1324 if (event->direction == GDK_SCROLL_UP)
1325 xev.button = 4;
1326 else if (event->direction == GDK_SCROLL_DOWN)
1327 xev.button = 5;
1328 else
1329 return FALSE;
1331 XSendEvent(gdk_display, xev.window, False,
1332 ButtonPressMask, (XEvent *) &xev);
1334 return TRUE;
1337 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1339 GtkWidget *widget = pi->win;
1340 Icon *icon = (Icon *) pi;
1342 if (!icon->selected)
1344 tmp_icon_selected = TRUE;
1345 icon_select_only(icon);
1348 g_return_if_fail(icon_selection != NULL);
1350 pinboard_drag_in_progress = icon;
1352 if (icon_selection->next == NULL)
1353 drag_one_item(widget, event, icon->path, icon->item, NULL);
1354 else
1356 guchar *uri_list;
1358 uri_list = icon_create_uri_list();
1359 drag_selection(widget, event, uri_list);
1360 g_free(uri_list);
1364 /* An icon is being dragged around... */
1365 static gint icon_motion_notify(GtkWidget *widget,
1366 GdkEventMotion *event,
1367 PinIcon *pi)
1369 if (motion_state == MOTION_READY_FOR_DND)
1371 if (dnd_motion_moved(event))
1372 start_drag(pi, event);
1373 return TRUE;
1376 return FALSE;
1379 static void backdrop_from_xml(xmlNode *node)
1381 gchar *style;
1383 g_free(current_pinboard->backdrop);
1384 current_pinboard->backdrop = xmlNodeGetContent(node);
1386 style = xmlGetProp(node, "style");
1388 if (style)
1390 current_pinboard->backdrop_style =
1391 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1392 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1393 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1394 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1395 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1396 BACKDROP_NONE;
1397 g_free(style);
1399 else
1400 current_pinboard->backdrop_style = BACKDROP_TILE;
1403 /* Create one pinboard icon for each icon in the doc */
1404 static void pinboard_load_from_xml(xmlDocPtr doc)
1406 xmlNodePtr node, root;
1407 char *tmp, *label, *path, *shortcut, *args;
1408 int x, y;
1410 root = xmlDocGetRootElement(doc);
1412 for (node = root->xmlChildrenNode; node; node = node->next)
1414 if (node->type != XML_ELEMENT_NODE)
1415 continue;
1416 if (strcmp(node->name, "backdrop") == 0)
1418 backdrop_from_xml(node);
1419 continue;
1421 if (strcmp(node->name, "icon") != 0)
1422 continue;
1424 tmp = xmlGetProp(node, "x");
1425 if (!tmp)
1426 continue;
1427 x = atoi(tmp);
1428 g_free(tmp);
1430 tmp = xmlGetProp(node, "y");
1431 if (!tmp)
1432 continue;
1433 y = atoi(tmp);
1434 g_free(tmp);
1436 label = xmlGetProp(node, "label");
1437 if (!label)
1438 label = g_strdup("<missing label>");
1439 path = xmlNodeGetContent(node);
1440 if (!path)
1441 path = g_strdup("<missing path>");
1442 shortcut = xmlGetProp(node, "shortcut");
1443 args = xmlGetProp(node, "args");
1445 pinboard_pin_with_args(path, label, x, y, shortcut, args);
1447 g_free(path);
1448 g_free(label);
1449 g_free(shortcut);
1450 g_free(args);
1454 /* Called for each line in the pinboard file while loading a new board.
1455 * Only used for old-format files when converting to XML.
1457 static const char *pin_from_file(gchar *line)
1459 gchar *leaf = NULL;
1460 int x, y, n;
1462 if (*line == '<')
1464 gchar *end;
1466 end = strchr(line + 1, '>');
1467 if (!end)
1468 return _("Missing '>' in icon label");
1470 leaf = g_strndup(line + 1, end - line - 1);
1472 line = end + 1;
1474 while (g_ascii_isspace(*line))
1475 line++;
1476 if (*line != ',')
1477 return _("Missing ',' after icon label");
1478 line++;
1481 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1482 return NULL; /* Ignore format errors */
1484 pinboard_pin(line + n, leaf, x, y, NULL);
1486 g_free(leaf);
1488 return NULL;
1491 /* Write the current state of the pinboard to the current pinboard file */
1492 static void pinboard_save(void)
1494 guchar *save = NULL;
1495 guchar *save_new = NULL;
1496 GList *next;
1497 xmlDocPtr doc = NULL;
1498 xmlNodePtr root;
1500 g_return_if_fail(current_pinboard != NULL);
1502 if (strchr(current_pinboard->name, '/'))
1503 save = g_strdup(current_pinboard->name);
1504 else
1506 guchar *leaf;
1508 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1509 save = choices_find_path_save(leaf, PROJECT, TRUE);
1510 g_free(leaf);
1513 if (!save)
1514 return;
1516 doc = xmlNewDoc("1.0");
1517 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1519 root = xmlDocGetRootElement(doc);
1521 if (current_pinboard->backdrop)
1523 BackdropStyle style = current_pinboard->backdrop_style;
1524 xmlNodePtr tree;
1526 tree = xmlNewTextChild(root, NULL, "backdrop",
1527 current_pinboard->backdrop);
1528 xmlSetProp(tree, "style",
1529 style == BACKDROP_TILE ? "Tiled" :
1530 style == BACKDROP_CENTRE ? "Centred" :
1531 style == BACKDROP_SCALE ? "Scaled" :
1532 style == BACKDROP_STRETCH ? "Stretched" :
1533 "Program");
1536 for (next = current_pinboard->icons; next; next = next->next)
1538 xmlNodePtr tree;
1539 PinIcon *pi = (PinIcon *) next->data;
1540 Icon *icon = (Icon *) pi;
1541 char *tmp;
1543 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1545 tmp = g_strdup_printf("%d", pi->x);
1546 xmlSetProp(tree, "x", tmp);
1547 g_free(tmp);
1549 tmp = g_strdup_printf("%d", pi->y);
1550 xmlSetProp(tree, "y", tmp);
1551 g_free(tmp);
1553 xmlSetProp(tree, "label", icon->item->leafname);
1554 if (icon->shortcut)
1555 xmlSetProp(tree, "shortcut", icon->shortcut);
1556 if (icon->args)
1557 xmlSetProp(tree, "args", icon->args);
1560 save_new = g_strconcat(save, ".new", NULL);
1561 if (save_xml_file(doc, save_new) || rename(save_new, save))
1562 delayed_error(_("Error saving pinboard %s: %s"),
1563 save, g_strerror(errno));
1564 g_free(save_new);
1566 g_free(save);
1567 if (doc)
1568 xmlFreeDoc(doc);
1571 static void snap_to_grid(int *x, int *y)
1573 int step = o_pinboard_grid_step.int_value;
1575 *x = ((*x + step / 2) / step) * step;
1576 *y = ((*y + step / 2) / step) * step;
1579 /* Convert (x,y) from a centre point to a window position */
1580 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1582 gboolean clamp = o_pinboard_clamp_icons.int_value;
1583 GtkRequisition req;
1585 gtk_widget_size_request(pi->win, &req);
1587 *x -= req.width >> 1;
1588 *y -= req.height >> 1;
1589 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1590 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1593 /* Same as drag_set_dest(), but for pinboard icons */
1594 static void drag_set_pinicon_dest(PinIcon *pi)
1596 GtkObject *obj = GTK_OBJECT(pi->win);
1598 make_drop_target(pi->win, 0);
1600 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1601 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1602 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1605 /* Called during the drag when the mouse is in a widget registered
1606 * as a drop target. Returns TRUE if we can accept the drop.
1608 static gboolean drag_motion(GtkWidget *widget,
1609 GdkDragContext *context,
1610 gint x,
1611 gint y,
1612 guint time,
1613 PinIcon *pi)
1615 GdkDragAction action = context->suggested_action;
1616 const char *type = NULL;
1617 Icon *icon = (Icon *) pi;
1618 DirItem *item = icon->item;
1620 if (gtk_drag_get_source_widget(context) == widget)
1622 g_dataset_set_data(context, "drop_dest_type",
1623 (gpointer) drop_dest_pass_through);
1624 return FALSE; /* Can't drag something to itself! */
1627 if (icon->selected)
1628 goto out; /* Can't drag a selection to itself */
1630 type = dnd_motion_item(context, &item);
1632 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1633 && type != drop_dest_prog)
1635 guint state;
1636 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1637 if (state & GDK_BUTTON1_MASK)
1638 action = GDK_ACTION_ASK;
1641 if (!item)
1642 type = NULL;
1643 out:
1644 /* We actually must pretend to accept the drop, even if the
1645 * directory isn't writeable, so that the spring-opening
1646 * thing works.
1649 /* Don't allow drops to non-writeable directories */
1650 if (o_dnd_spring_open.int_value == FALSE &&
1651 type == drop_dest_dir &&
1652 access(icon->path, W_OK) != 0)
1654 type = NULL;
1657 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1658 if (type)
1660 gdk_drag_status(context, action, time);
1661 g_dataset_set_data_full(context, "drop_dest_path",
1662 g_strdup(icon->path), g_free);
1663 if (type == drop_dest_dir)
1664 dnd_spring_load(context, NULL);
1666 pinboard_wink_item(pi, FALSE);
1668 else
1669 gdk_drag_status(context, 0, time);
1671 /* Always return TRUE to stop the pinboard getting the events */
1672 return TRUE;
1675 static gboolean pinboard_shadow = FALSE;
1676 static gint shadow_x, shadow_y;
1677 #define SHADOW_SIZE (ICON_WIDTH)
1679 static gboolean bg_expose(GtkWidget *widget,
1680 GdkEventExpose *event, gpointer data)
1682 GdkRectangle clipbox;
1683 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1684 gboolean double_buffer;
1686 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1687 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1689 gdk_region_get_clipbox(event->region, &clipbox);
1691 double_buffer = (clipbox.width * clipbox.height) < 20000;
1692 if (double_buffer)
1693 gdk_window_begin_paint_region(widget->window, event->region);
1695 /* Clear the area to the background image */
1697 GtkStyle *style = current_pinboard->window->style;
1698 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1700 gdk_gc_set_clip_region(gc, event->region);
1701 if (style->bg_pixmap[GTK_STATE_NORMAL])
1703 gdk_gc_set_ts_origin(gc, 0, 0);
1704 gdk_gc_set_fill(gc, GDK_TILED);
1705 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1708 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1709 clipbox.x, clipbox.y,
1710 clipbox.width, clipbox.height);
1711 if (style->bg_pixmap[GTK_STATE_NORMAL])
1712 gdk_gc_set_fill(gc, GDK_SOLID);
1714 gdk_gc_set_clip_region(gc, NULL);
1717 if (pinboard_shadow)
1719 gdk_draw_rectangle(widget->window,
1720 widget->style->white_gc, FALSE,
1721 shadow_x, shadow_y,
1722 SHADOW_SIZE, SHADOW_SIZE);
1723 gdk_draw_rectangle(widget->window,
1724 widget->style->black_gc, FALSE,
1725 shadow_x + 1, shadow_y + 1,
1726 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1729 if (lasso_in_progress)
1731 GdkRectangle area;
1733 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1734 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1735 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1736 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1738 if (area.width > 4 && area.height > 4)
1740 gdk_draw_rectangle(widget->window,
1741 widget->style->white_gc, FALSE,
1742 area.x, area.y,
1743 area.width - 1, area.height - 1);
1744 gdk_draw_rectangle(widget->window,
1745 widget->style->black_gc, FALSE,
1746 area.x + 1, area.y + 1,
1747 area.width - 3, area.height - 3);
1751 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1752 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1754 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1756 if (double_buffer)
1757 gdk_window_end_paint(widget->window);
1759 return TRUE;
1762 /* Draw a 'shadow' under an icon being dragged, showing where
1763 * it will land.
1765 static void pinboard_set_shadow(gboolean on)
1767 GdkRectangle area;
1769 if (pinboard_shadow)
1771 area.x = shadow_x;
1772 area.y = shadow_y;
1773 area.width = SHADOW_SIZE + 1;
1774 area.height = SHADOW_SIZE + 1;
1776 gdk_window_invalidate_rect(current_pinboard->window->window,
1777 &area, TRUE);
1780 if (on)
1782 int old_x = shadow_x, old_y = shadow_y;
1784 gdk_window_get_pointer(current_pinboard->fixed->window,
1785 &shadow_x, &shadow_y, NULL);
1786 snap_to_grid(&shadow_x, &shadow_y);
1787 shadow_x -= SHADOW_SIZE / 2;
1788 shadow_y -= SHADOW_SIZE / 2;
1791 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1792 return;
1794 area.x = shadow_x;
1795 area.y = shadow_y;
1796 area.width = SHADOW_SIZE + 1;
1797 area.height = SHADOW_SIZE + 1;
1799 gdk_window_invalidate_rect(current_pinboard->window->window,
1800 &area, TRUE);
1803 pinboard_shadow = on;
1806 /* Called when dragging some pinboard icons finishes */
1807 void pinboard_move_icons(void)
1809 int x = shadow_x, y = shadow_y;
1810 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1811 int width, height;
1812 int dx, dy;
1813 GList *next;
1815 g_return_if_fail(pi != NULL);
1817 x += SHADOW_SIZE / 2;
1818 y += SHADOW_SIZE / 2;
1819 snap_to_grid(&x, &y);
1821 if (pi->x == x && pi->y == y)
1822 return;
1824 /* Find out how much the dragged icon moved (after snapping).
1825 * Move all selected icons by the same amount.
1827 dx = x - pi->x;
1828 dy = y - pi->y;
1830 /* Move the other selected icons to keep the same relative
1831 * position.
1833 for (next = icon_selection; next; next = next->next)
1835 PinIcon *pi = (PinIcon *) next->data;
1836 int nx, ny;
1838 g_return_if_fail(IS_PIN_ICON(pi));
1840 pi->x += dx;
1841 pi->y += dy;
1842 nx = pi->x;
1843 ny = pi->y;
1845 gdk_drawable_get_size(pi->win->window, &width, &height);
1846 offset_from_centre(pi, &nx, &ny);
1848 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1849 pi->win, nx, ny);
1852 pinboard_save();
1855 static void drag_leave(GtkWidget *widget,
1856 GdkDragContext *context,
1857 guint32 time,
1858 PinIcon *pi)
1860 pinboard_wink_item(NULL, FALSE);
1861 dnd_spring_abort();
1864 static gboolean bg_drag_leave(GtkWidget *widget,
1865 GdkDragContext *context,
1866 guint32 time,
1867 gpointer data)
1869 pinboard_set_shadow(FALSE);
1870 return TRUE;
1873 static gboolean bg_drag_motion(GtkWidget *widget,
1874 GdkDragContext *context,
1875 gint x,
1876 gint y,
1877 guint time,
1878 gpointer data)
1880 /* Dragging from the pinboard to the pinboard is not allowed */
1882 if (!provides(context, text_uri_list))
1883 return FALSE;
1885 pinboard_set_shadow(TRUE);
1887 gdk_drag_status(context,
1888 context->suggested_action == GDK_ACTION_ASK
1889 ? GDK_ACTION_LINK : context->suggested_action,
1890 time);
1891 return TRUE;
1894 static void drag_end(GtkWidget *widget,
1895 GdkDragContext *context,
1896 PinIcon *pi)
1898 pinboard_drag_in_progress = NULL;
1899 if (tmp_icon_selected)
1901 icon_select_only(NULL);
1902 tmp_icon_selected = FALSE;
1906 /* Something which affects all the icons has changed - reshape
1907 * and redraw all of them.
1909 static void reshape_all(void)
1911 GList *next;
1913 g_return_if_fail(current_pinboard != NULL);
1915 for (next = current_pinboard->icons; next; next = next->next)
1917 Icon *icon = (Icon *) next->data;
1918 pinboard_reshape_icon(icon);
1922 /* Turns off the pinboard. Does not call gtk_main_quit. */
1923 static void pinboard_clear(void)
1925 GList *next;
1927 g_return_if_fail(current_pinboard != NULL);
1929 tasklist_set_active(FALSE);
1931 next = current_pinboard->icons;
1932 while (next)
1934 PinIcon *pi = (PinIcon *) next->data;
1936 next = next->next;
1938 gtk_widget_destroy(pi->win);
1941 gtk_widget_destroy(current_pinboard->window);
1943 abandon_backdrop_app(current_pinboard);
1945 g_object_unref(current_pinboard->shadow_gc);
1946 current_pinboard->shadow_gc = NULL;
1948 g_free(current_pinboard->name);
1949 null_g_free(&current_pinboard);
1951 number_of_windows--;
1954 static gpointer parent_class;
1956 static void pin_icon_destroy(Icon *icon)
1958 PinIcon *pi = (PinIcon *) icon;
1960 g_return_if_fail(pi->win != NULL);
1962 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1963 gtk_widget_destroy(pi->win);
1966 static void pinboard_remove_items(void)
1968 g_return_if_fail(icon_selection != NULL);
1970 while (icon_selection)
1971 icon_destroy((Icon *) icon_selection->data);
1973 pinboard_save();
1976 static void pin_icon_update(Icon *icon)
1978 pinboard_reshape_icon(icon);
1979 pinboard_save();
1982 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1984 return IS_PIN_ICON(other);
1987 static void pin_wink_icon(Icon *icon)
1989 pinboard_wink_item((PinIcon *) icon, TRUE);
1992 static void pin_icon_class_init(gpointer gclass, gpointer data)
1994 IconClass *icon = (IconClass *) gclass;
1996 parent_class = g_type_class_peek_parent(gclass);
1998 icon->destroy = pin_icon_destroy;
1999 icon->redraw = pinboard_reshape_icon;
2000 icon->update = pin_icon_update;
2001 icon->wink = pin_wink_icon;
2002 icon->remove_items = pinboard_remove_items;
2003 icon->same_group = pin_icon_same_group;
2006 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
2010 static GType pin_icon_get_type(void)
2012 static GType type = 0;
2014 if (!type)
2016 static const GTypeInfo info =
2018 sizeof (PinIconClass),
2019 NULL, /* base_init */
2020 NULL, /* base_finalise */
2021 pin_icon_class_init,
2022 NULL, /* class_finalise */
2023 NULL, /* class_data */
2024 sizeof(PinIcon),
2025 0, /* n_preallocs */
2026 pin_icon_init
2029 type = g_type_register_static(icon_get_type(),
2030 "PinIcon", &info, 0);
2033 return type;
2036 static PinIcon *pin_icon_new(const char *pathname, const char *name)
2038 PinIcon *pi;
2039 Icon *icon;
2041 pi = g_object_new(pin_icon_get_type(), NULL);
2042 icon = (Icon *) pi;
2044 icon_set_path(icon, pathname, name);
2046 return pi;
2049 /* Called when the window widget is somehow destroyed */
2050 static void pin_icon_destroyed(PinIcon *pi)
2052 g_return_if_fail(pi->win != NULL);
2054 pi->win = NULL;
2056 pinboard_wink_item(NULL, FALSE);
2058 if (pinboard_drag_in_progress == (Icon *) pi)
2059 pinboard_drag_in_progress = NULL;
2061 if (current_pinboard)
2062 current_pinboard->icons =
2063 g_list_remove(current_pinboard->icons, pi);
2065 g_object_unref(pi);
2068 /* Set the tooltip */
2069 static void pin_icon_set_tip(PinIcon *pi)
2071 XMLwrapper *ai;
2072 xmlNode *node;
2073 Icon *icon = (Icon *) pi;
2075 g_return_if_fail(pi != NULL);
2077 ai = appinfo_get(icon->path, icon->item);
2079 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2081 guchar *str;
2082 str = xmlNodeListGetString(node->doc,
2083 node->xmlChildrenNode, 1);
2084 if (str)
2086 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2087 g_free(str);
2090 else
2091 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2093 if (ai)
2094 g_object_unref(ai);
2097 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2099 int pos[3];
2100 GList *list;
2102 pos[0] = event->x_root;
2103 pos[1] = event->y_root;
2105 icon_prepare_menu((Icon *) pi, TRUE);
2107 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2108 pos[2] = g_list_length(list) - 6;
2109 g_list_free(list);
2111 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2112 position_menu,
2113 (gpointer) pos, event->button, event->time);
2116 static void create_pinboard_window(Pinboard *pinboard)
2118 GtkWidget *win;
2120 g_return_if_fail(pinboard->window == NULL);
2122 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2123 gtk_widget_set_style(win, gtk_widget_get_default_style());
2125 gtk_widget_set_double_buffered(win, FALSE);
2126 gtk_widget_set_app_paintable(win, TRUE);
2127 gtk_widget_set_name(win, "rox-pinboard");
2128 pinboard->window = win;
2129 pinboard->fixed = gtk_fixed_new();
2130 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2132 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2134 gtk_widget_set_size_request(win, screen_width, screen_height);
2135 gtk_widget_realize(win);
2136 gtk_window_move(GTK_WINDOW(win), 0, 0);
2137 make_panel_window(win);
2139 /* TODO: Use gdk function when it supports this type */
2141 GdkAtom desktop_type;
2143 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2144 FALSE);
2145 gdk_property_change(win->window,
2146 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2147 gdk_atom_intern("ATOM", FALSE), 32,
2148 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2151 gtk_widget_add_events(win,
2152 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2153 GDK_EXPOSURE_MASK |
2154 GDK_BUTTON1_MOTION_MASK |
2155 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2156 g_signal_connect(win, "button-press-event",
2157 G_CALLBACK(button_press_event), NULL);
2158 g_signal_connect(win, "button-release-event",
2159 G_CALLBACK(button_release_event), NULL);
2160 g_signal_connect(win, "motion-notify-event",
2161 G_CALLBACK(lasso_motion), NULL);
2162 g_signal_connect(pinboard->fixed, "expose_event",
2163 G_CALLBACK(bg_expose), NULL);
2165 /* Some window managers use scroll events on the root to switch
2166 * desktops, but don't cope with our pinboard window, so we forward
2167 * them manually in that case.
2169 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2171 /* Drag and drop handlers */
2172 drag_set_pinboard_dest(win);
2173 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2174 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2176 pinboard->shadow_gc = gdk_gc_new(win->window);
2177 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2179 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2181 gtk_widget_show_all(win);
2182 gdk_window_lower(win->window);
2185 /* Load image 'path' and scale according to 'style' */
2186 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2188 GdkPixmap *pixmap;
2189 GdkPixbuf *pixbuf;
2190 GError *error = NULL;
2192 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2193 if (error)
2195 delayed_error(_("Error loading backdrop image:\n%s\n"
2196 "Backdrop removed."),
2197 error->message);
2198 g_error_free(error);
2199 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2200 return NULL;
2203 if (style == BACKDROP_STRETCH)
2205 GdkPixbuf *old = pixbuf;
2207 pixbuf = gdk_pixbuf_scale_simple(old,
2208 screen_width, screen_height,
2209 GDK_INTERP_HYPER);
2211 g_object_unref(old);
2213 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2215 GdkPixbuf *old = pixbuf;
2216 int x, y, width, height;
2217 float scale;
2219 width = gdk_pixbuf_get_width(pixbuf);
2220 height = gdk_pixbuf_get_height(pixbuf);
2222 if (style == BACKDROP_SCALE)
2224 float scale_x, scale_y;
2225 scale_x = screen_width / ((float) width);
2226 scale_y = screen_height / ((float) height);
2227 scale = MIN(scale_x, scale_y);
2229 else
2230 scale = 1;
2232 pixbuf = gdk_pixbuf_new(
2233 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2234 8, screen_width, screen_height);
2235 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2236 ((pin_text_bg_col.green & 0xff00) << 8) |
2237 ((pin_text_bg_col.blue & 0xff00)));
2239 x = (screen_width - width * scale) / 2;
2240 y = (screen_height - height * scale) / 2;
2241 x = MAX(x, 0);
2242 y = MAX(y, 0);
2244 gdk_pixbuf_composite(old, pixbuf,
2245 x, y,
2246 MIN(screen_width, width * scale),
2247 MIN(screen_height, height * scale),
2248 x, y, scale, scale,
2249 o_pinboard_image_scaling.int_value?
2250 GDK_INTERP_BILINEAR: GDK_INTERP_HYPER,
2251 255);
2252 g_object_unref(old);
2255 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2256 &pixmap, NULL, 0);
2257 g_object_unref(pixbuf);
2259 return pixmap;
2262 static void abandon_backdrop_app(Pinboard *pinboard)
2264 g_return_if_fail(pinboard != NULL);
2266 if (pinboard->to_backdrop_app != -1)
2268 close(pinboard->to_backdrop_app);
2269 close(pinboard->from_backdrop_app);
2270 g_source_remove(pinboard->input_tag);
2271 g_string_free(pinboard->input_buffer, TRUE);
2272 pinboard->to_backdrop_app = -1;
2273 pinboard->from_backdrop_app = -1;
2274 pinboard->input_tag = -1;
2275 pinboard->input_buffer = NULL;
2278 g_return_if_fail(pinboard->to_backdrop_app == -1);
2279 g_return_if_fail(pinboard->from_backdrop_app == -1);
2280 g_return_if_fail(pinboard->input_tag == -1);
2281 g_return_if_fail(pinboard->input_buffer == NULL);
2284 /* A single line has been read from the child.
2285 * Processes the command, and replies 'ok' (or abandons the child on error).
2287 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2289 BackdropStyle style;
2290 const char *ok = "ok\n";
2292 if (strncmp(command, "tile ", 5) == 0)
2294 style = BACKDROP_TILE;
2295 command += 5;
2297 else if (strncmp(command, "scale ", 6) == 0)
2299 style = BACKDROP_SCALE;
2300 command += 6;
2302 else if (strncmp(command, "stretch ", 8) == 0)
2304 style = BACKDROP_STRETCH;
2305 command += 8;
2307 else if (strncmp(command, "centre ", 7) == 0)
2309 style = BACKDROP_CENTRE;
2310 command += 7;
2312 else
2314 g_warning("Invalid command '%s' from backdrop app\n",
2315 command);
2316 abandon_backdrop_app(pinboard);
2317 return;
2320 /* Load the backdrop. May abandon the program if loading fails. */
2321 reload_backdrop(pinboard, command, style);
2323 if (pinboard->to_backdrop_app == -1)
2324 return;
2326 while (*ok)
2328 int sent;
2330 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2331 if (sent <= 0)
2333 /* Remote app quit? Not an error. */
2334 abandon_backdrop_app(pinboard);
2335 break;
2337 ok += sent;
2341 static void backdrop_from_child(Pinboard *pinboard,
2342 int src, GdkInputCondition cond)
2344 char buf[256];
2345 int got;
2347 got = read(src, buf, sizeof(buf));
2349 if (got <= 0)
2351 if (got < 0)
2352 g_warning("backdrop_from_child: %s\n",
2353 g_strerror(errno));
2354 abandon_backdrop_app(pinboard);
2355 return;
2358 g_string_append_len(pinboard->input_buffer, buf, got);
2360 while (pinboard->from_backdrop_app != -1)
2362 int len;
2363 char *nl, *command;
2365 nl = strchr(pinboard->input_buffer->str, '\n');
2366 if (!nl)
2367 return; /* Haven't got a whole line yet */
2369 len = nl - pinboard->input_buffer->str;
2370 command = g_strndup(pinboard->input_buffer->str, len);
2371 g_string_erase(pinboard->input_buffer, 0, len + 1);
2373 command_from_backdrop_app(pinboard, command);
2375 g_free(command);
2379 static void reload_backdrop(Pinboard *pinboard,
2380 const gchar *backdrop,
2381 BackdropStyle backdrop_style)
2383 GtkStyle *style;
2385 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2387 const char *argv[] = {NULL, "--backdrop", NULL};
2388 GError *error = NULL;
2390 g_return_if_fail(pinboard->to_backdrop_app == -1);
2391 g_return_if_fail(pinboard->from_backdrop_app == -1);
2392 g_return_if_fail(pinboard->input_tag == -1);
2393 g_return_if_fail(pinboard->input_buffer == NULL);
2395 argv[0] = make_path(backdrop, "AppRun");
2397 /* Run the program. It'll send us a SOAP message and we'll
2398 * get back here with a different style and image.
2401 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2402 G_SPAWN_DO_NOT_REAP_CHILD |
2403 G_SPAWN_SEARCH_PATH,
2404 NULL, NULL, /* Child setup fn */
2405 NULL, /* Child PID */
2406 &pinboard->to_backdrop_app,
2407 &pinboard->from_backdrop_app,
2408 NULL, /* Standard error */
2409 &error))
2411 pinboard->input_buffer = g_string_new(NULL);
2412 pinboard->input_tag = gdk_input_add_full(
2413 pinboard->from_backdrop_app,
2414 GDK_INPUT_READ,
2415 (GdkInputFunction) backdrop_from_child,
2416 pinboard, NULL);
2418 else
2420 delayed_error("%s", error ? error->message : "(null)");
2421 g_error_free(error);
2423 return;
2426 /* Note: Copying a style does not ref the pixmaps! */
2428 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2429 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2431 if (backdrop)
2432 style->bg_pixmap[GTK_STATE_NORMAL] =
2433 load_backdrop(backdrop, backdrop_style);
2435 gdk_color_parse(o_pinboard_bg_colour.value,
2436 &style->bg[GTK_STATE_NORMAL]);
2438 gtk_widget_set_style(pinboard->window, style);
2440 g_object_unref(style);
2442 gtk_widget_queue_draw(pinboard->window);
2444 /* Also update root window property (for transparent xterms, etc) */
2445 if (style->bg_pixmap[GTK_STATE_NORMAL])
2447 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2448 gdk_property_change(gdk_get_default_root_window(),
2449 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2450 gdk_atom_intern("PIXMAP", FALSE),
2451 32, GDK_PROP_MODE_REPLACE,
2452 (guchar *) &id, 1);
2454 else
2456 gdk_property_delete(gdk_get_default_root_window(),
2457 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2461 #define SEARCH_STEP 32
2463 static void search_free(GdkRectangle *rect, GdkRegion *used,
2464 int *outer, int od, int omax,
2465 int *inner, int id, int imax)
2467 *outer = od > 0 ? 0 : omax;
2468 while (*outer >= 0 && *outer <= omax)
2470 *inner = id > 0 ? 0 : imax;
2471 while (*inner >= 0 && *inner <= imax)
2473 if (gdk_region_rect_in(used, rect) ==
2474 GDK_OVERLAP_RECTANGLE_OUT)
2475 return;
2476 *inner += id;
2479 *outer += od;
2482 rect->x = -1;
2483 rect->y = -1;
2486 /* Finds a free area on the pinboard large enough for the width and height
2487 * of the given rectangle, by filling in the x and y fields of 'rect'.
2488 * If 'old' is true, 'rect' has a previous position and we first check
2489 * if it is viable.
2490 * The search order respects user preferences.
2491 * If no area is free, returns any old area.
2493 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2494 gboolean old)
2496 GdkRegion *used;
2497 GList *next;
2498 GdkRectangle used_rect;
2499 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2501 used = gdk_region_new();
2503 panel_mark_used(used);
2505 /* Subtract the no-go areas... */
2507 if (o_top_margin.int_value > 0)
2509 used_rect.x = 0;
2510 used_rect.y = 0;
2511 used_rect.width = gdk_screen_width();
2512 used_rect.height = o_top_margin.int_value;
2513 gdk_region_union_with_rect(used, &used_rect);
2516 if (o_bottom_margin.int_value > 0)
2518 used_rect.x = 0;
2519 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2520 used_rect.width = gdk_screen_width();
2521 used_rect.height = o_bottom_margin.int_value;
2522 gdk_region_union_with_rect(used, &used_rect);
2525 /* Subtract the used areas... */
2527 next = GTK_FIXED(pinboard->fixed)->children;
2528 for (; next; next = next->next)
2530 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2532 if (!GTK_WIDGET_VISIBLE(fix->widget))
2533 continue;
2535 used_rect.x = fix->x;
2536 used_rect.y = fix->y;
2537 used_rect.width = fix->widget->requisition.width;
2538 used_rect.height = fix->widget->requisition.height;
2540 gdk_region_union_with_rect(used, &used_rect);
2543 /* Check the previous area */
2544 if(old) {
2545 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2546 gdk_region_destroy(used);
2547 return;
2551 /* Find the first free area (yes, this isn't exactly pretty, but
2552 * it works). If you know a better (fast!) algorithm, let me know!
2556 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2557 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2558 dx = -SEARCH_STEP;
2560 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2561 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2562 dy = -SEARCH_STEP;
2564 if (o_iconify_dir.int_value == DIR_VERT)
2566 search_free(rect, used,
2567 &rect->x, dx, screen_width - rect->width,
2568 &rect->y, dy, screen_height - rect->height);
2570 else
2572 search_free(rect, used,
2573 &rect->y, dy, screen_height - rect->height,
2574 &rect->x, dx, screen_width - rect->width);
2577 gdk_region_destroy(used);
2579 if (rect->x == -1)
2581 rect->x = 0;
2582 rect->y = 0;
2586 /* Icon's size, shape or appearance has changed - update the display */
2587 static void pinboard_reshape_icon(Icon *icon)
2589 PinIcon *pi = (PinIcon *) icon;
2590 int x = pi->x, y = pi->y;
2592 set_size_and_style(pi);
2593 offset_from_centre(pi, &x, &y);
2595 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2597 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2598 pi->win, x, y);
2601 /* Newer versions of GTK seem to need this, or the icon doesn't
2602 * get redrawn.
2604 gtk_widget_queue_draw(pi->win);
2607 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2608 static void update_pinboard_font(void)
2610 if (pinboard_font)
2611 pango_font_description_free(pinboard_font);
2612 pinboard_font = o_label_font.value[0] != '\0'
2613 ? pango_font_description_from_string(o_label_font.value)
2614 : NULL;
2617 static void radios_changed(gpointer data)
2619 GObject *dialog = G_OBJECT(data);
2620 DropBox *drop_box;
2621 const guchar *path;
2622 Radios *radios;
2624 g_return_if_fail(dialog != NULL);
2626 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2627 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2629 g_return_if_fail(radios != NULL);
2630 g_return_if_fail(drop_box != NULL);
2631 g_return_if_fail(current_pinboard != NULL);
2633 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2635 path = drop_box_get_path(drop_box);
2636 if (path)
2637 pinboard_set_backdrop(path, radios_get_value(radios));
2641 static void update_radios(GtkWidget *dialog)
2643 Radios *radios;
2644 GtkWidget *hbox;
2646 g_return_if_fail(dialog != NULL);
2648 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2649 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2651 g_return_if_fail(current_pinboard != NULL);
2652 g_return_if_fail(radios != NULL);
2653 g_return_if_fail(hbox != NULL);
2655 switch (current_pinboard->backdrop_style)
2657 case BACKDROP_TILE:
2658 case BACKDROP_STRETCH:
2659 case BACKDROP_SCALE:
2660 case BACKDROP_CENTRE:
2661 radios_set_value(radios,
2662 current_pinboard->backdrop_style);
2663 gtk_widget_set_sensitive(hbox, TRUE);
2664 break;
2665 default:
2666 gtk_widget_set_sensitive(hbox, FALSE);
2667 radios_set_value(radios, BACKDROP_TILE);
2668 break;