r4654: Allow pinboard and panel icons to be locked (Dennis Tomas, suggested by
[rox-filer.git] / ROX-Filer / src / pinboard.c
bloba8ad9eb195ebf49209e3c2c4695116201623cbd3
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* pinboard.c - icons on the desktop background */
22 #include "config.h"
24 #include <ctype.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <gtk/gtk.h>
29 #include <gdk/gdkx.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <libxml/parser.h>
33 #include <signal.h>
35 #include "global.h"
37 #include "pinboard.h"
38 #include "main.h"
39 #include "dnd.h"
40 #include "pixmaps.h"
41 #include "type.h"
42 #include "choices.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "diritem.h"
47 #include "bind.h"
48 #include "icon.h"
49 #include "run.h"
50 #include "appinfo.h"
51 #include "menu.h"
52 #include "xml.h"
53 #include "tasklist.h"
54 #include "panel.h" /* For panel_mark_used() */
55 #include "wrapped.h"
56 #include "dropbox.h"
58 static gboolean tmp_icon_selected = FALSE; /* When dragging */
60 static GtkWidget *set_backdrop_dialog = NULL;
62 struct _Pinboard {
63 guchar *name; /* Leaf name */
64 GList *icons;
65 GtkStyle *style;
67 gchar *backdrop; /* Pathname */
68 BackdropStyle backdrop_style;
69 gint to_backdrop_app; /* pipe FD, or -1 */
70 gint from_backdrop_app; /* pipe FD, or -1 */
71 gint input_tag;
72 GString *input_buffer;
74 GtkWidget *window; /* Screen-sized window */
75 GtkWidget *fixed;
76 GdkGC *shadow_gc;
79 #define IS_PIN_ICON(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), pin_icon_get_type())
81 typedef struct _PinIconClass PinIconClass;
82 typedef struct _PinIcon PinIcon;
84 struct _PinIconClass {
85 IconClass parent;
88 struct _PinIcon {
89 Icon icon;
91 int x, y;
92 GtkWidget *win;
93 GtkWidget *widget; /* The drawing area for the icon */
94 GtkWidget *label;
97 /* The number of pixels between the bottom of the image and the top
98 * of the text.
100 #define GAP 4
102 /* The size of the border around the icon which is used when winking */
103 #define WINK_FRAME 2
105 /* Grid sizes */
106 #define GRID_STEP_FINE 2
107 #define GRID_STEP_MED 16
108 #define GRID_STEP_COARSE 32
110 /* Used in options */
111 #define CORNER_TOP_LEFT 0
112 #define CORNER_TOP_RIGHT 1
113 #define CORNER_BOTTOM_LEFT 2
114 #define CORNER_BOTTOM_RIGHT 3
116 #define DIR_HORZ 0
117 #define DIR_VERT 1
119 static PinIcon *current_wink_icon = NULL;
120 static gint wink_timeout;
122 /* Used for the text colours (only) in the icons */
123 GdkColor pin_text_fg_col, pin_text_bg_col;
124 PangoFontDescription *pinboard_font = NULL; /* NULL => Gtk default */
126 static GdkColor pin_text_shadow_col;
128 Pinboard *current_pinboard = NULL;
129 static gint loading_pinboard = 0; /* Non-zero => loading */
131 /* The Icon that was used to start the current drag, if any */
132 Icon *pinboard_drag_in_progress = NULL;
134 /* For selecting groups of icons */
135 static gboolean lasso_in_progress = FALSE;
136 static int lasso_rect_x1, lasso_rect_x2;
137 static int lasso_rect_y1, lasso_rect_y2;
139 /* Tracking icon positions */
140 static GHashTable *placed_icons=NULL;
142 Option o_pinboard_tasklist_per_workspace;
143 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
144 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
145 static Option o_pinboard_tasklist, o_forward_buttons_13;
146 static Option o_iconify_start, o_iconify_dir;
147 static Option o_label_font, o_pinboard_shadow_colour;
148 static Option o_pinboard_shadow_labels;
149 static Option o_blackbox_hack;
151 static Option o_top_margin, o_bottom_margin, o_left_margin, o_right_margin;
152 static Option o_pinboard_image_scaling;
154 /* Static prototypes */
155 static GType pin_icon_get_type(void);
156 static void set_size_and_style(PinIcon *pi);
157 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
158 static gint end_wink(gpointer data);
159 static gboolean button_release_event(GtkWidget *widget,
160 GdkEventButton *event,
161 PinIcon *pi);
162 static gboolean enter_notify(GtkWidget *widget,
163 GdkEventCrossing *event,
164 PinIcon *pi);
165 static gint leave_notify(GtkWidget *widget,
166 GdkEventCrossing *event,
167 PinIcon *pi);
168 static gboolean button_press_event(GtkWidget *widget,
169 GdkEventButton *event,
170 PinIcon *pi);
171 static gboolean scroll_event(GtkWidget *widget,
172 GdkEventScroll *event);
173 static gint icon_motion_notify(GtkWidget *widget,
174 GdkEventMotion *event,
175 PinIcon *pi);
176 static const char *pin_from_file(gchar *line);
177 static void snap_to_grid(int *x, int *y);
178 static void offset_from_centre(PinIcon *pi, int *x, int *y);
179 static gboolean drag_motion(GtkWidget *widget,
180 GdkDragContext *context,
181 gint x,
182 gint y,
183 guint time,
184 PinIcon *pi);
185 static void drag_set_pinicon_dest(PinIcon *pi);
186 static void drag_leave(GtkWidget *widget,
187 GdkDragContext *context,
188 guint32 time,
189 PinIcon *pi);
190 static gboolean bg_drag_motion(GtkWidget *widget,
191 GdkDragContext *context,
192 gint x,
193 gint y,
194 guint time,
195 gpointer data);
196 static gboolean bg_drag_leave(GtkWidget *widget,
197 GdkDragContext *context,
198 guint32 time,
199 gpointer data);
200 static gboolean bg_expose(GtkWidget *window,
201 GdkEventExpose *event, gpointer data);
202 static void drag_end(GtkWidget *widget,
203 GdkDragContext *context,
204 PinIcon *pi);
205 static void reshape_all(void);
206 static void pinboard_check_options(void);
207 static void pinboard_load_from_xml(xmlDocPtr doc);
208 static void pinboard_clear(void);
209 static void pinboard_save(void);
210 static PinIcon *pin_icon_new(const char *pathname, const char *name);
211 static void pin_icon_destroyed(PinIcon *pi);
212 static void pin_icon_set_tip(PinIcon *pi);
213 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
214 static void create_pinboard_window(Pinboard *pinboard);
215 static void reload_backdrop(Pinboard *pinboard,
216 const gchar *backdrop,
217 BackdropStyle backdrop_style);
218 static void pinboard_reshape_icon(Icon *icon);
219 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
220 static void abandon_backdrop_app(Pinboard *pinboard);
221 static void drag_backdrop_dropped(GtkWidget *drop_box,
222 const guchar *path,
223 GtkWidget *dialog);
224 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
225 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
226 gboolean old);
227 static void update_pinboard_font(void);
228 static void draw_lasso(void);
229 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d);
230 static void clear_backdrop(GtkWidget *drop_box, gpointer data);
231 static void radios_changed(Radios *radios, gpointer data);
232 static void update_radios(GtkWidget *dialog);
233 static void pinboard_set_backdrop_box(void);
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_pinboard_tasklist_per_workspace, "pinboard_tasklist_per_workspace", FALSE);
253 option_add_int(&o_forward_buttons_13, "pinboard_forward_buttons_13",
254 FALSE);
256 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
257 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
259 option_add_int(&o_blackbox_hack, "blackbox_hack", FALSE);
261 option_add_int(&o_top_margin, "pinboard_top_margin", 0);
262 option_add_int(&o_bottom_margin, "pinboard_bottom_margin", 0);
263 option_add_int(&o_left_margin, "pinboard_left_margin", 0);
264 option_add_int(&o_right_margin, "pinboard_right_margin", 0);
266 option_add_int(&o_pinboard_image_scaling, "pinboard_image_scaling", 0);
268 option_add_notify(pinboard_check_options);
270 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
271 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
272 gdk_color_parse(o_pinboard_shadow_colour.value, &pin_text_shadow_col);
273 update_pinboard_font();
275 placed_icons=g_hash_table_new(g_str_hash, g_str_equal);
278 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
279 * and make it the current pinboard.
280 * Any existing pinned items are removed. You must call this
281 * at least once before using the pinboard. NULL disables the
282 * pinboard.
284 void pinboard_activate(const gchar *name)
286 Pinboard *old_board = current_pinboard;
287 guchar *path, *slash;
289 /* Treat an empty name the same as NULL */
290 if (name && !*name)
291 name = NULL;
293 if (old_board)
294 pinboard_clear();
296 if (!name)
298 if (number_of_windows < 1 && gtk_main_level() > 0)
299 gtk_main_quit();
301 gdk_property_delete(gdk_get_default_root_window(),
302 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
303 return;
306 number_of_windows++;
308 slash = strchr(name, '/');
309 if (slash)
311 if (access(name, F_OK))
312 path = NULL; /* File does not (yet) exist */
313 else
314 path = g_strdup(name);
316 else
318 guchar *leaf;
320 leaf = g_strconcat("pb_", name, NULL);
321 path = choices_find_xdg_path_load(leaf, PROJECT, SITE);
322 g_free(leaf);
325 current_pinboard = g_new(Pinboard, 1);
326 current_pinboard->name = g_strdup(name);
327 current_pinboard->icons = NULL;
328 current_pinboard->window = NULL;
329 current_pinboard->backdrop = NULL;
330 current_pinboard->backdrop_style = BACKDROP_NONE;
331 current_pinboard->to_backdrop_app = -1;
332 current_pinboard->from_backdrop_app = -1;
333 current_pinboard->input_tag = -1;
334 current_pinboard->input_buffer = NULL;
336 create_pinboard_window(current_pinboard);
338 loading_pinboard++;
339 if (path)
341 xmlDocPtr doc;
342 doc = xmlParseFile(path);
343 if (doc)
345 pinboard_load_from_xml(doc);
346 xmlFreeDoc(doc);
347 reload_backdrop(current_pinboard,
348 current_pinboard->backdrop,
349 current_pinboard->backdrop_style);
351 else
353 parse_file(path, pin_from_file);
354 info_message(_("Your old pinboard file has been "
355 "converted to the new XML format."));
356 pinboard_save();
358 g_free(path);
360 else
361 pinboard_pin(home_dir, "Home",
362 4 + ICON_WIDTH / 2,
363 4 + ICON_HEIGHT / 2,
364 NULL);
365 loading_pinboard--;
367 if (o_pinboard_tasklist.int_value)
368 tasklist_set_active(TRUE);
371 /* Return the window of the current pinboard, or NULL.
372 * Used to make sure lowering the panels doesn't lose them...
374 GdkWindow *pinboard_get_window(void)
376 if (current_pinboard)
377 return current_pinboard->window->window;
378 return NULL;
381 const char *pinboard_get_name(void)
383 g_return_val_if_fail(current_pinboard != NULL, NULL);
385 return current_pinboard->name;
388 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
389 * being cleared.
391 void pinboard_add_widget(GtkWidget *widget, const gchar *name)
393 GtkRequisition req;
394 GdkRectangle *rect=NULL;
395 gboolean found=FALSE;
397 g_return_if_fail(current_pinboard != NULL);
399 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, 0, 0);
401 gtk_widget_size_request(widget, &req);
403 if(name) {
404 rect=g_hash_table_lookup(placed_icons, name);
405 if(rect)
406 found=TRUE;
408 /*printf("%s at %p %d\n", name? name: "(nil)", rect, found);*/
410 if(rect) {
411 if(rect->width<req.width || rect->height<req.height) {
412 found=FALSE;
414 } else {
415 rect=g_new(GdkRectangle, 1);
416 rect->width = req.width;
417 rect->height = req.height;
419 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
420 rect->y, found);*/
421 find_free_rect(current_pinboard, rect, found);
422 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
423 rect->y, found);*/
425 gtk_fixed_move(GTK_FIXED(current_pinboard->fixed),
426 widget, rect->x, rect->y);
428 /* Store the new position (key and value are never freed) */
429 if(name)
430 g_hash_table_insert(placed_icons, g_strdup(name),
431 rect);
434 void pinboard_moved_widget(GtkWidget *widget, const gchar *name,
435 int x, int y)
437 GdkRectangle *rect;
439 if(!name)
440 return;
441 rect=g_hash_table_lookup(placed_icons, name);
442 if(!rect)
443 return;
445 rect->x=x;
446 rect->y=y;
449 /* Add a new icon to the background.
450 * 'path' should be an absolute pathname.
451 * 'x' and 'y' are the coordinates of the point in the middle of the text.
452 * 'name' is the name to use. If NULL then the leafname of path is used.
454 void pinboard_pin_with_args(const gchar *path, const gchar *name,
455 int x, int y, const gchar *shortcut,
456 const gchar *args, gboolean locked)
458 GtkWidget *align, *vbox;
459 GdkWindow *events;
460 PinIcon *pi;
461 Icon *icon;
463 g_return_if_fail(path != NULL);
464 g_return_if_fail(current_pinboard != NULL);
466 pi = pin_icon_new(path, name);
467 icon = (Icon *) pi;
468 pi->x = x;
469 pi->y = y;
471 /* This is a bit complicated...
473 * An icon needs to be a NO_WINDOW widget so that the image can
474 * blend with the background (A ParentRelative window also works, but
475 * is slow, causes the xfree86's memory consumption to grow without
476 * bound, and doesn't even get freed when the filer quits!).
478 * However, the icon also needs to have a window, so we get events
479 * delivered correctly. The solution is to float an InputOnly window
480 * over the icon. Since GtkButton works the same way, we just use
481 * that :-)
484 /* Button takes the initial ref of Icon */
485 pi->win = gtk_button_new();
486 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
487 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
488 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
490 vbox = gtk_vbox_new(FALSE, 0);
491 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
493 align = gtk_alignment_new(0.5, 0.5, 0, 0);
494 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
495 gtk_container_add(GTK_CONTAINER(align), pi->widget);
497 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
498 drag_set_pinicon_dest(pi);
499 g_signal_connect(pi->win, "drag_data_get",
500 G_CALLBACK(drag_data_get), NULL);
502 pi->label = wrapped_label_new(icon->item->leafname, 180);
503 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
505 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
507 snap_to_grid(&x, &y);
508 pi->x = x;
509 pi->y = y;
510 gtk_widget_show_all(pi->win);
511 pinboard_reshape_icon((Icon *) pi);
513 gtk_widget_realize(pi->win);
514 events = GTK_BUTTON(pi->win)->event_window;
515 gdk_window_set_events(events,
516 GDK_EXPOSURE_MASK |
517 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
518 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
519 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
520 GDK_BUTTON3_MOTION_MASK);
521 g_signal_connect(pi->win, "enter-notify-event",
522 G_CALLBACK(enter_notify), pi);
523 g_signal_connect(pi->win, "leave-notify-event",
524 G_CALLBACK(leave_notify), pi);
525 g_signal_connect(pi->win, "button-press-event",
526 G_CALLBACK(button_press_event), pi);
527 g_signal_connect(pi->win, "button-release-event",
528 G_CALLBACK(button_release_event), pi);
529 g_signal_connect(pi->win, "motion-notify-event",
530 G_CALLBACK(icon_motion_notify), pi);
531 g_signal_connect(pi->win, "expose-event",
532 G_CALLBACK(draw_icon), pi);
533 g_signal_connect_swapped(pi->win, "style-set",
534 G_CALLBACK(pinboard_reshape_icon), pi);
535 g_signal_connect_swapped(pi->win, "destroy",
536 G_CALLBACK(pin_icon_destroyed), pi);
538 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
539 pin_icon_set_tip(pi);
541 icon_set_shortcut(icon, shortcut);
542 icon_set_arguments(icon, args);
543 icon->locked = locked;
545 if (!loading_pinboard)
546 pinboard_save();
549 void pinboard_pin(const gchar *path, const gchar *name, int x, int y,
550 const gchar *shortcut)
552 pinboard_pin_with_args(path, name, x, y, shortcut, NULL, FALSE);
556 * Remove an icon from the background. The first icon with a matching
557 * path is removed. If name is not NULL then that also must match
559 gboolean pinboard_remove(const gchar *path, const gchar *name)
561 GList *item;
562 PinIcon *pi;
563 Icon *icon;
565 g_return_val_if_fail(current_pinboard != NULL, FALSE);
567 for(item=current_pinboard->icons; item; item=g_list_next(item)) {
568 pi=(PinIcon *) item->data;
569 icon=(Icon *) pi;
571 if(strcmp(icon->path, path)!=0)
572 continue;
574 if(name && strcmp(name, icon->item->leafname)!=0)
575 continue;
577 icon->locked = FALSE;
578 icon_destroy(icon);
580 pinboard_save();
582 return TRUE;
585 return FALSE;
588 /* Put a border around the icon, briefly.
589 * If icon is NULL then cancel any existing wink.
590 * The icon will automatically unhighlight unless timeout is FALSE,
591 * in which case you must call this function again (with NULL or another
592 * icon) to remove the highlight.
594 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
596 PinIcon *old = current_wink_icon;
598 if (old == pi)
599 return;
601 current_wink_icon = pi;
603 if (old)
605 gtk_widget_queue_draw(old->win);
606 gdk_window_process_updates(old->widget->window, TRUE);
608 if (wink_timeout != -1)
609 g_source_remove(wink_timeout);
612 if (pi)
614 gtk_widget_queue_draw(pi->win);
615 gdk_window_process_updates(pi->widget->window, TRUE);
617 if (timeout)
618 wink_timeout = g_timeout_add(300, end_wink, NULL);
619 else
620 wink_timeout = -1;
624 /* 'app' is saved as the new application to set the backdrop. It will then be
625 * run, and should communicate with the filer as described in the manual.
627 void pinboard_set_backdrop_app(const gchar *app)
629 XMLwrapper *ai;
630 DirItem *item;
631 gboolean can_set;
633 item = diritem_new("");
634 diritem_restat(app, item, NULL);
635 if (!(item->flags & ITEM_FLAG_APPDIR))
637 delayed_error(_("The backdrop handler must be an application "
638 "directory. Drag an application directory "
639 "into the Set Backdrop dialog box, or (for "
640 "programmers) pass it to the SOAP "
641 "SetBackdropApp method."));
642 diritem_free(item);
643 return;
646 ai = appinfo_get(app, item);
647 diritem_free(item);
649 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
650 if (ai)
651 g_object_unref(ai);
653 if (can_set)
654 pinboard_set_backdrop(app, BACKDROP_PROGRAM);
655 else
656 delayed_error(_("You can only set the backdrop to an image "
657 "or to a program which knows how to "
658 "manage ROX-Filer's backdrop.\n\n"
659 "Programmers: the application's AppInfo.xml "
660 "must contain the CanSetBackdrop element, as "
661 "described in ROX-Filer's manual."));
664 /* Open a dialog box allowing the user to set the backdrop */
665 static void pinboard_set_backdrop_box(void)
667 GtkWidget *dialog, *frame, *label, *hbox;
668 GtkBox *vbox;
669 Radios *radios;
671 g_return_if_fail(current_pinboard != NULL);
673 if (set_backdrop_dialog)
674 gtk_widget_destroy(set_backdrop_dialog);
676 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
677 GTK_DIALOG_NO_SEPARATOR,
678 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
679 NULL);
680 set_backdrop_dialog = dialog;
681 g_signal_connect(dialog, "destroy",
682 G_CALLBACK(gtk_widget_destroyed), &set_backdrop_dialog);
683 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
685 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
687 label = gtk_label_new(_("Choose a style and drag an image in:"));
688 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
689 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
690 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
692 /* The Centred, Scaled, Tiled radios... */
693 hbox = gtk_hbox_new(TRUE, 2);
694 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
696 radios = radios_new(radios_changed, dialog);
697 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
698 g_object_set_data(G_OBJECT(dialog), "rox-radios-hbox", hbox);
700 radios_add(radios, _("Centre the image without scaling it"),
701 BACKDROP_CENTRE, _("Centre"));
702 radios_add(radios, _("Scale the image to fit the backdrop area, "
703 "without distorting it"),
704 BACKDROP_SCALE, _("Scale"));
705 radios_add(radios, _("Stretch the image to fill the backdrop area"),
706 BACKDROP_STRETCH, _("Stretch"));
707 radios_add(radios, _("Tile the image over the backdrop area"),
708 BACKDROP_TILE, _("Tile"));
710 update_radios(dialog);
712 /* The drop area... */
713 frame = drop_box_new(_("Drop an image here"));
714 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
716 radios_pack(radios, GTK_BOX(hbox));
717 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
719 drop_box_set_path(DROP_BOX(frame), current_pinboard->backdrop);
721 g_signal_connect(frame, "path_dropped",
722 G_CALLBACK(drag_backdrop_dropped), dialog);
723 g_signal_connect(frame, "clear",
724 G_CALLBACK(clear_backdrop), dialog);
726 g_signal_connect(dialog, "response",
727 G_CALLBACK(backdrop_response), NULL);
728 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
730 gtk_widget_show_all(dialog);
733 /* Also used by tasklist.c */
734 void draw_label_shadow(WrappedLabel *wl, GdkRegion *region)
736 GtkWidget *widget;
737 gint x, y;
739 if (!o_pinboard_shadow_labels.int_value)
740 return;
742 gdk_gc_set_clip_region(current_pinboard->shadow_gc, region);
744 widget = GTK_WIDGET(wl);
746 y = widget->allocation.y - wl->y_off;
747 x = widget->allocation.x - wl->x_off -
748 ((wl->text_width - widget->allocation.width) >> 1);
750 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
751 x + 1, y + 1, wl->layout);
753 if (o_pinboard_shadow_labels.int_value > 1)
754 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
755 x + 2, y + 2, wl->layout);
757 gdk_gc_set_clip_region(current_pinboard->shadow_gc, NULL);
760 /* Set and save (path, style) as the new backdrop.
761 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
762 * Otherwise, the image is displayed now.
764 void pinboard_set_backdrop(const gchar *path, BackdropStyle style)
766 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
767 (path != NULL && style != BACKDROP_NONE));
769 if (!current_pinboard)
771 if (!path)
772 return;
773 pinboard_activate("Default");
774 delayed_error(_("No pinboard was in use... "
775 "the 'Default' pinboard has been selected. "
776 "Use 'rox -p=Default' to turn it on in "
777 "future."));
778 g_return_if_fail(current_pinboard != NULL);
781 /* We might have just run the old backdrop program and now
782 * we're going to set a new one! Seems a bit mean...
785 abandon_backdrop_app(current_pinboard);
787 g_free(current_pinboard->backdrop);
788 current_pinboard->backdrop = g_strdup(path);
789 current_pinboard->backdrop_style = style;
790 reload_backdrop(current_pinboard,
791 current_pinboard->backdrop,
792 current_pinboard->backdrop_style);
794 pinboard_save();
796 if (set_backdrop_dialog)
798 DropBox *box = g_object_get_data(G_OBJECT(set_backdrop_dialog),
799 "rox-dropbox");
800 g_return_if_fail(box != NULL);
801 drop_box_set_path(box, current_pinboard->backdrop);
802 update_radios(set_backdrop_dialog);
806 /* Called on xrandr screen resizes */
807 void pinboard_update_size(void)
809 int width, height;
811 if (!current_pinboard)
812 return;
814 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
815 &width, &height);
817 /* Only update the pinboard's size if the screen gets bigger,
818 * not smaller. Not sure what to do about icons that end up
819 * offscreen if the screen shrinks, but perhaps a good policy
820 * is to leave them there until the screen size is increased
821 * again rather than mess them around. */
822 width = MAX(width, screen_width);
823 height = MAX(height, screen_height);
825 gtk_widget_set_size_request(current_pinboard->window, width, height);
828 /****************************************************************
829 * INTERNAL FUNCTIONS *
830 ****************************************************************/
832 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
834 gtk_widget_destroy(dialog);
837 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
839 pinboard_set_backdrop(NULL, BACKDROP_NONE);
842 static void drag_backdrop_dropped(GtkWidget *drop_box,
843 const guchar *path,
844 GtkWidget *dialog)
846 struct stat info;
847 Radios *radios;
849 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
850 g_return_if_fail(radios != NULL);
852 if (mc_stat(path, &info))
854 delayed_error(
855 _("Can't access '%s':\n%s"), path,
856 g_strerror(errno));
857 return;
860 if (S_ISDIR(info.st_mode))
862 /* Use this program to set the backdrop */
863 pinboard_set_backdrop_app(path);
865 else if (S_ISREG(info.st_mode))
866 pinboard_set_backdrop(path, radios_get_value(radios));
867 else
868 delayed_error(_("Only files (and certain applications) can be "
869 "used to set the background image."));
872 /* Do this in the idle loop so that we don't try to put an unmanaged
873 * pinboard behind a managed panel (crashes some WMs).
875 static gboolean recreate_pinboard(gchar *name)
877 pinboard_activate(name);
878 g_free(name);
880 return FALSE;
883 static void pinboard_check_options(void)
885 GdkColor n_fg, n_bg, n_shadow;
887 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
888 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
889 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
891 if (o_override_redirect.has_changed && current_pinboard)
893 gchar *name;
894 name = g_strdup(current_pinboard->name);
895 pinboard_activate(NULL);
896 g_idle_add((GtkFunction) recreate_pinboard, name);
899 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
901 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
902 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
903 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
904 o_pinboard_shadow_labels.has_changed ||
905 o_label_font.has_changed)
907 pin_text_fg_col = n_fg;
908 pin_text_bg_col = n_bg;
909 pin_text_shadow_col = n_shadow;
910 update_pinboard_font();
912 if (current_pinboard)
914 GtkWidget *w = current_pinboard->window;
915 GdkColormap *cm;
917 cm = gtk_widget_get_colormap(w);
919 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
920 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
922 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
923 &n_shadow);
925 abandon_backdrop_app(current_pinboard);
926 reload_backdrop(current_pinboard,
927 current_pinboard->backdrop,
928 current_pinboard->backdrop_style);
930 reshape_all();
933 tasklist_style_changed();
937 static gint end_wink(gpointer data)
939 pinboard_wink_item(NULL, FALSE);
940 return FALSE;
943 /* Sets the appearance from the options and updates the size request of
944 * the image.
946 static void set_size_and_style(PinIcon *pi)
948 Icon *icon = (Icon *) pi;
949 MaskedPixmap *image = di_image(icon->item);
950 int iwidth = image->width;
951 int iheight = image->height;
953 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
954 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
955 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
956 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
957 widget_modify_font(pi->label, pinboard_font);
959 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
961 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
964 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
966 static GtkWidgetClass *parent_class = NULL;
967 Icon *icon = (Icon *) pi;
968 DirItem *item = icon->item;
969 MaskedPixmap *image = di_image(item);
970 int iwidth = image->width;
971 int iheight = image->height;
972 int x, y;
973 GdkPixbuf *pixbuf;
975 if (!parent_class)
977 gpointer c = ((GTypeInstance *) widget)->g_class;
978 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
981 x = pi->widget->allocation.x;
982 y = pi->widget->allocation.y;
984 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
986 pixbuf = icon->selected
987 ? create_spotlight_pixbuf(image->pixbuf,
988 &pi->widget->style->base[GTK_STATE_SELECTED])
989 : image->pixbuf;
991 render_pixbuf(pixbuf,
992 pi->widget->window,
993 pi->widget->style->black_gc,
994 x, y, iwidth, iheight);
996 if (icon->selected)
997 g_object_unref(pixbuf);
999 if (item->flags & ITEM_FLAG_SYMLINK)
1001 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
1002 pi->widget->style->black_gc,
1003 x, y, -1, -1);
1005 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1007 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
1008 ? im_mounted
1009 : im_unmounted;
1011 render_pixbuf(mp->pixbuf, pi->widget->window,
1012 pi->widget->style->black_gc,
1013 x, y, -1, -1);
1016 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
1018 if (icon->selected)
1020 GtkStyle *style = pi->label->style;
1021 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
1023 gdk_gc_set_clip_region(gc, event->region);
1024 gdk_draw_rectangle(pi->label->window, gc, TRUE,
1025 pi->label->allocation.x,
1026 pi->label->allocation.y,
1027 pi->label->allocation.width,
1028 pi->label->allocation.height);
1029 gdk_gc_set_clip_region(gc, NULL);
1031 else if (o_pinboard_shadow_labels.int_value)
1032 draw_label_shadow((WrappedLabel *) pi->label, event->region);
1034 /* Draw children */
1035 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
1036 event->region);
1037 (parent_class->expose_event)(widget, event);
1038 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
1040 /* Stop the button effect */
1041 return TRUE;
1044 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
1046 gint x, y, width, height;
1048 if (current_wink_icon != pi)
1049 return FALSE;
1051 x = widget->allocation.x;
1052 y = widget->allocation.y;
1053 width = widget->allocation.width;
1054 height = widget->allocation.height;
1056 gdk_draw_rectangle(widget->window,
1057 pi->widget->style->white_gc,
1058 FALSE,
1059 x, y, width - 1, height - 1);
1060 gdk_draw_rectangle(widget->window,
1061 pi->widget->style->black_gc,
1062 FALSE,
1063 x + 1, y + 1, width - 3, height - 3);
1065 return FALSE;
1068 static gboolean enter_notify(GtkWidget *widget,
1069 GdkEventCrossing *event,
1070 PinIcon *pi)
1072 icon_may_update((Icon *) pi);
1073 pin_icon_set_tip(pi);
1074 return TRUE;
1077 static gint leave_notify(GtkWidget *widget,
1078 GdkEventCrossing *event,
1079 PinIcon *pi)
1081 return TRUE;
1084 static void select_lasso(void)
1086 GList *next;
1087 int minx, miny, maxx, maxy;
1089 g_return_if_fail(lasso_in_progress == TRUE);
1091 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1092 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1093 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1094 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1096 for (next = current_pinboard->icons; next; next = next->next)
1098 PinIcon *pi = (PinIcon *) next->data;
1099 GtkAllocation *alloc = &pi->win->allocation;
1100 int cx = alloc->x + alloc->width / 2;
1101 int cy = alloc->y + alloc->height / 2;
1103 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1104 icon_set_selected((Icon *) pi, TRUE);
1108 static void cancel_lasso(void)
1110 draw_lasso();
1111 lasso_in_progress = FALSE;
1114 static void pinboard_lasso_box(int start_x, int start_y)
1116 if (lasso_in_progress)
1117 cancel_lasso();
1118 lasso_in_progress = TRUE;
1119 lasso_rect_x1 = lasso_rect_x2 = start_x;
1120 lasso_rect_y1 = lasso_rect_y2 = start_y;
1122 draw_lasso();
1125 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1127 if (!lasso_in_progress)
1128 return FALSE;
1130 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1132 draw_lasso();
1133 lasso_rect_x2 = event->x;
1134 lasso_rect_y2 = event->y;
1135 draw_lasso();
1138 return FALSE;
1141 /* Mark the area of the screen covered by the lasso box for redraw */
1142 static void draw_lasso(void)
1144 GdkRectangle area, edge;
1146 if (!lasso_in_progress)
1147 return;
1149 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1150 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1151 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1152 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1154 edge.x = area.x;
1155 edge.y = area.y;
1156 edge.width = area.width;
1158 edge.height = 2; /* Top */
1159 gdk_window_invalidate_rect(current_pinboard->window->window,
1160 &edge, TRUE);
1162 edge.y += area.height - 2; /* Bottom */
1163 gdk_window_invalidate_rect(current_pinboard->window->window,
1164 &edge, TRUE);
1166 edge.y = area.y;
1167 edge.height = area.height;
1168 edge.width = 2; /* Left */
1169 gdk_window_invalidate_rect(current_pinboard->window->window,
1170 &edge, TRUE);
1172 edge.x += area.width - 2; /* Right */
1173 gdk_window_invalidate_rect(current_pinboard->window->window,
1174 &edge, TRUE);
1177 static void perform_action(PinIcon *pi, GdkEventButton *event)
1179 BindAction action;
1180 Icon *icon = (Icon *) pi;
1182 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1183 event);
1185 /* Actions that can happen with or without an icon */
1186 switch (action)
1188 case ACT_LASSO_CLEAR:
1189 icon_select_only(NULL);
1190 /* (no break) */
1191 case ACT_LASSO_MODIFY:
1192 pinboard_lasso_box(event->x, event->y);
1193 return;
1194 case ACT_CLEAR_SELECTION:
1195 icon_select_only(NULL);
1196 return;
1197 case ACT_POPUP_MENU:
1198 dnd_motion_ungrab();
1199 pinboard_show_menu(event, pi);
1200 return;
1201 case ACT_IGNORE:
1202 return;
1203 default:
1204 break;
1207 g_return_if_fail(pi != NULL);
1209 switch (action)
1211 case ACT_OPEN_ITEM:
1212 dnd_motion_ungrab();
1213 pinboard_wink_item(pi, TRUE);
1214 if (event->type == GDK_2BUTTON_PRESS)
1215 icon_set_selected(icon, FALSE);
1216 icon_run(icon);
1217 break;
1218 case ACT_EDIT_ITEM:
1219 dnd_motion_ungrab();
1220 pinboard_wink_item(pi, TRUE);
1221 if (event->type == GDK_2BUTTON_PRESS)
1222 icon_set_selected(icon, FALSE);
1223 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1224 break;
1225 case ACT_PRIME_AND_SELECT:
1226 if (!icon->selected)
1227 icon_select_only(icon);
1228 dnd_motion_start(MOTION_READY_FOR_DND);
1229 break;
1230 case ACT_PRIME_AND_TOGGLE:
1231 icon_set_selected(icon, !icon->selected);
1232 dnd_motion_start(MOTION_READY_FOR_DND);
1233 break;
1234 case ACT_PRIME_FOR_DND:
1235 dnd_motion_start(MOTION_READY_FOR_DND);
1236 break;
1237 case ACT_TOGGLE_SELECTED:
1238 icon_set_selected(icon, !icon->selected);
1239 break;
1240 case ACT_SELECT_EXCL:
1241 icon_select_only(icon);
1242 break;
1243 default:
1244 g_warning("Unsupported action : %d\n", action);
1245 break;
1249 static void forward_to_root(GdkEventButton *event)
1251 XButtonEvent xev;
1253 if (event->type == GDK_BUTTON_PRESS)
1255 xev.type = ButtonPress;
1256 if (!o_blackbox_hack.int_value)
1257 XUngrabPointer(gdk_display, event->time);
1259 else
1260 xev.type = ButtonRelease;
1262 xev.window = gdk_x11_get_default_root_xwindow();
1263 xev.root = xev.window;
1264 xev.subwindow = None;
1265 xev.time = event->time;
1266 xev.x = event->x_root; /* Needed for icewm */
1267 xev.y = event->y_root;
1268 xev.x_root = event->x_root;
1269 xev.y_root = event->y_root;
1270 xev.state = event->state;
1271 xev.button = event->button;
1272 xev.same_screen = True;
1274 XSendEvent(gdk_display, xev.window, False,
1275 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1278 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1279 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1281 /* pi is NULL if this is a root event */
1282 static gboolean button_release_event(GtkWidget *widget,
1283 GdkEventButton *event,
1284 PinIcon *pi)
1286 if (FORWARDED_BUTTON(pi, event->button))
1287 forward_to_root(event);
1288 else if (dnd_motion_release(event))
1290 if (motion_buttons_pressed == 0 && lasso_in_progress)
1292 select_lasso();
1293 cancel_lasso();
1295 return FALSE;
1298 perform_action(pi, event);
1300 return TRUE;
1303 /* pi is NULL if this is a root event */
1304 static gboolean button_press_event(GtkWidget *widget,
1305 GdkEventButton *event,
1306 PinIcon *pi)
1308 /* Just in case we've jumped in front of everything... */
1309 gdk_window_lower(current_pinboard->window->window);
1311 if (FORWARDED_BUTTON(pi, event->button))
1312 forward_to_root(event);
1313 else if (dnd_motion_press(widget, event))
1314 perform_action(pi, event);
1316 return TRUE;
1319 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1320 * (for old window managers that don't catch the buttons themselves)
1322 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1324 XButtonEvent xev;
1326 xev.type = ButtonPress;
1327 xev.window = gdk_x11_get_default_root_xwindow();
1328 xev.root = xev.window;
1329 xev.subwindow = None;
1330 xev.time = event->time;
1331 xev.x = event->x_root; /* Needed for icewm */
1332 xev.y = event->y_root;
1333 xev.x_root = event->x_root;
1334 xev.y_root = event->y_root;
1335 xev.state = event->state;
1336 xev.same_screen = True;
1338 if (event->direction == GDK_SCROLL_UP)
1339 xev.button = 4;
1340 else if (event->direction == GDK_SCROLL_DOWN)
1341 xev.button = 5;
1342 else
1343 return FALSE;
1345 XSendEvent(gdk_display, xev.window, False,
1346 ButtonPressMask, (XEvent *) &xev);
1348 return TRUE;
1351 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1353 GtkWidget *widget = pi->win;
1354 Icon *icon = (Icon *) pi;
1356 if (!icon->selected)
1358 tmp_icon_selected = TRUE;
1359 icon_select_only(icon);
1362 g_return_if_fail(icon_selection != NULL);
1364 pinboard_drag_in_progress = icon;
1366 if (icon_selection->next == NULL)
1367 drag_one_item(widget, event, icon->path, icon->item, NULL);
1368 else
1370 guchar *uri_list;
1372 uri_list = icon_create_uri_list();
1373 drag_selection(widget, event, uri_list);
1374 g_free(uri_list);
1378 /* An icon is being dragged around... */
1379 static gint icon_motion_notify(GtkWidget *widget,
1380 GdkEventMotion *event,
1381 PinIcon *pi)
1383 if (motion_state == MOTION_READY_FOR_DND)
1385 if (dnd_motion_moved(event))
1386 start_drag(pi, event);
1387 return TRUE;
1390 return FALSE;
1393 static void backdrop_from_xml(xmlNode *node)
1395 gchar *style;
1397 g_free(current_pinboard->backdrop);
1398 current_pinboard->backdrop = xmlNodeGetContent(node);
1400 style = xmlGetProp(node, "style");
1402 if (style)
1404 current_pinboard->backdrop_style =
1405 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1406 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1407 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1408 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1409 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1410 BACKDROP_NONE;
1411 g_free(style);
1413 else
1414 current_pinboard->backdrop_style = BACKDROP_TILE;
1417 /* Create one pinboard icon for each icon in the doc */
1418 static void pinboard_load_from_xml(xmlDocPtr doc)
1420 xmlNodePtr node, root;
1421 char *tmp, *label, *path, *shortcut, *args;
1422 int x, y;
1423 gboolean locked;
1425 root = xmlDocGetRootElement(doc);
1427 for (node = root->xmlChildrenNode; node; node = node->next)
1429 if (node->type != XML_ELEMENT_NODE)
1430 continue;
1431 if (strcmp(node->name, "backdrop") == 0)
1433 backdrop_from_xml(node);
1434 continue;
1436 if (strcmp(node->name, "icon") != 0)
1437 continue;
1439 tmp = xmlGetProp(node, "x");
1440 if (!tmp)
1441 continue;
1442 x = atoi(tmp);
1443 g_free(tmp);
1445 tmp = xmlGetProp(node, "y");
1446 if (!tmp)
1447 continue;
1448 y = atoi(tmp);
1449 g_free(tmp);
1451 label = xmlGetProp(node, "label");
1452 if (!label)
1453 label = g_strdup("<missing label>");
1454 path = xmlNodeGetContent(node);
1455 if (!path)
1456 path = g_strdup("<missing path>");
1457 shortcut = xmlGetProp(node, "shortcut");
1458 args = xmlGetProp(node, "args");
1460 tmp = xmlGetProp(node, "locked");
1461 if (tmp)
1463 locked = text_to_boolean(tmp, FALSE);
1464 g_free(tmp);
1466 else
1467 locked = FALSE;
1469 pinboard_pin_with_args(path, label, x, y, shortcut, args, locked);
1471 g_free(path);
1472 g_free(label);
1473 g_free(shortcut);
1474 g_free(args);
1478 /* Called for each line in the pinboard file while loading a new board.
1479 * Only used for old-format files when converting to XML.
1481 static const char *pin_from_file(gchar *line)
1483 gchar *leaf = NULL;
1484 int x, y, n;
1486 if (*line == '<')
1488 gchar *end;
1490 end = strchr(line + 1, '>');
1491 if (!end)
1492 return _("Missing '>' in icon label");
1494 leaf = g_strndup(line + 1, end - line - 1);
1496 line = end + 1;
1498 while (g_ascii_isspace(*line))
1499 line++;
1500 if (*line != ',')
1501 return _("Missing ',' after icon label");
1502 line++;
1505 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1506 return NULL; /* Ignore format errors */
1508 pinboard_pin(line + n, leaf, x, y, NULL);
1510 g_free(leaf);
1512 return NULL;
1515 /* Write the current state of the pinboard to the current pinboard file */
1516 static void pinboard_save(void)
1518 guchar *save = NULL;
1519 guchar *save_new = NULL;
1520 GList *next;
1521 xmlDocPtr doc = NULL;
1522 xmlNodePtr root;
1524 g_return_if_fail(current_pinboard != NULL);
1526 if (strchr(current_pinboard->name, '/'))
1527 save = g_strdup(current_pinboard->name);
1528 else
1530 guchar *leaf;
1532 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1533 save = choices_find_xdg_path_save(leaf, PROJECT, SITE, TRUE);
1534 g_free(leaf);
1537 if (!save)
1538 return;
1540 doc = xmlNewDoc("1.0");
1541 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1543 root = xmlDocGetRootElement(doc);
1545 if (current_pinboard->backdrop)
1547 BackdropStyle style = current_pinboard->backdrop_style;
1548 xmlNodePtr tree;
1550 tree = xmlNewTextChild(root, NULL, "backdrop",
1551 current_pinboard->backdrop);
1552 xmlSetProp(tree, "style",
1553 style == BACKDROP_TILE ? "Tiled" :
1554 style == BACKDROP_CENTRE ? "Centred" :
1555 style == BACKDROP_SCALE ? "Scaled" :
1556 style == BACKDROP_STRETCH ? "Stretched" :
1557 "Program");
1560 for (next = current_pinboard->icons; next; next = next->next)
1562 xmlNodePtr tree;
1563 PinIcon *pi = (PinIcon *) next->data;
1564 Icon *icon = (Icon *) pi;
1565 char *tmp;
1567 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1569 tmp = g_strdup_printf("%d", pi->x);
1570 xmlSetProp(tree, "x", tmp);
1571 g_free(tmp);
1573 tmp = g_strdup_printf("%d", pi->y);
1574 xmlSetProp(tree, "y", tmp);
1575 g_free(tmp);
1577 xmlSetProp(tree, "label", icon->item->leafname);
1578 if (icon->shortcut)
1579 xmlSetProp(tree, "shortcut", icon->shortcut);
1580 if (icon->args)
1581 xmlSetProp(tree, "args", icon->args);
1582 if (icon->locked)
1583 xmlSetProp(tree, "locked", "true");
1586 save_new = g_strconcat(save, ".new", NULL);
1587 if (save_xml_file(doc, save_new) || rename(save_new, save))
1588 delayed_error(_("Error saving pinboard %s: %s"),
1589 save, g_strerror(errno));
1590 g_free(save_new);
1592 g_free(save);
1593 if (doc)
1594 xmlFreeDoc(doc);
1597 static void snap_to_grid(int *x, int *y)
1599 int step = o_pinboard_grid_step.int_value;
1601 *x = ((*x + step / 2) / step) * step;
1602 *y = ((*y + step / 2) / step) * step;
1605 /* Convert (x,y) from a centre point to a window position */
1606 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1608 gboolean clamp = o_pinboard_clamp_icons.int_value;
1609 GtkRequisition req;
1611 gtk_widget_size_request(pi->win, &req);
1613 *x -= req.width >> 1;
1614 *y -= req.height >> 1;
1615 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1616 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1619 /* Same as drag_set_dest(), but for pinboard icons */
1620 static void drag_set_pinicon_dest(PinIcon *pi)
1622 GtkObject *obj = GTK_OBJECT(pi->win);
1624 make_drop_target(pi->win, 0);
1626 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1627 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1628 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1631 /* Called during the drag when the mouse is in a widget registered
1632 * as a drop target. Returns TRUE if we can accept the drop.
1634 static gboolean drag_motion(GtkWidget *widget,
1635 GdkDragContext *context,
1636 gint x,
1637 gint y,
1638 guint time,
1639 PinIcon *pi)
1641 GdkDragAction action = context->suggested_action;
1642 const char *type = NULL;
1643 Icon *icon = (Icon *) pi;
1644 DirItem *item = icon->item;
1646 if (gtk_drag_get_source_widget(context) == widget)
1648 g_dataset_set_data(context, "drop_dest_type",
1649 (gpointer) drop_dest_pass_through);
1650 return FALSE; /* Can't drag something to itself! */
1653 if (icon->selected)
1654 goto out; /* Can't drag a selection to itself */
1656 type = dnd_motion_item(context, &item);
1658 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1659 && type != drop_dest_prog)
1661 guint state;
1662 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1663 if (state & GDK_BUTTON1_MASK)
1664 action = GDK_ACTION_ASK;
1667 if (!item)
1668 type = NULL;
1669 out:
1670 /* We actually must pretend to accept the drop, even if the
1671 * directory isn't writeable, so that the spring-opening
1672 * thing works.
1675 /* Don't allow drops to non-writeable directories */
1676 if (o_dnd_spring_open.int_value == FALSE &&
1677 type == drop_dest_dir &&
1678 access(icon->path, W_OK) != 0)
1680 type = NULL;
1683 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1684 if (type)
1686 gdk_drag_status(context, action, time);
1687 g_dataset_set_data_full(context, "drop_dest_path",
1688 g_strdup(icon->path), g_free);
1689 if (type == drop_dest_dir)
1690 dnd_spring_load(context, NULL);
1692 pinboard_wink_item(pi, FALSE);
1694 else
1695 gdk_drag_status(context, 0, time);
1697 /* Always return TRUE to stop the pinboard getting the events */
1698 return TRUE;
1701 static gboolean pinboard_shadow = FALSE;
1702 static gint shadow_x, shadow_y;
1703 #define SHADOW_SIZE (ICON_WIDTH)
1705 static gboolean bg_expose(GtkWidget *widget,
1706 GdkEventExpose *event, gpointer data)
1708 GdkRectangle clipbox;
1709 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1710 gboolean double_buffer;
1712 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1713 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1715 gdk_region_get_clipbox(event->region, &clipbox);
1717 double_buffer = (clipbox.width * clipbox.height) < 20000;
1718 if (double_buffer)
1719 gdk_window_begin_paint_region(widget->window, event->region);
1721 /* Clear the area to the background image */
1723 GtkStyle *style = current_pinboard->window->style;
1724 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1726 gdk_gc_set_clip_region(gc, event->region);
1727 if (style->bg_pixmap[GTK_STATE_NORMAL])
1729 gdk_gc_set_ts_origin(gc, 0, 0);
1730 gdk_gc_set_fill(gc, GDK_TILED);
1731 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1734 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1735 clipbox.x, clipbox.y,
1736 clipbox.width, clipbox.height);
1737 if (style->bg_pixmap[GTK_STATE_NORMAL])
1738 gdk_gc_set_fill(gc, GDK_SOLID);
1740 gdk_gc_set_clip_region(gc, NULL);
1743 if (pinboard_shadow)
1745 gdk_draw_rectangle(widget->window,
1746 widget->style->white_gc, FALSE,
1747 shadow_x, shadow_y,
1748 SHADOW_SIZE, SHADOW_SIZE);
1749 gdk_draw_rectangle(widget->window,
1750 widget->style->black_gc, FALSE,
1751 shadow_x + 1, shadow_y + 1,
1752 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1755 if (lasso_in_progress)
1757 GdkRectangle area;
1759 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1760 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1761 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1762 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1764 if (area.width > 4 && area.height > 4)
1766 gdk_draw_rectangle(widget->window,
1767 widget->style->white_gc, FALSE,
1768 area.x, area.y,
1769 area.width - 1, area.height - 1);
1770 gdk_draw_rectangle(widget->window,
1771 widget->style->black_gc, FALSE,
1772 area.x + 1, area.y + 1,
1773 area.width - 3, area.height - 3);
1777 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1778 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1780 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1782 if (double_buffer)
1783 gdk_window_end_paint(widget->window);
1785 return TRUE;
1788 /* Draw a 'shadow' under an icon being dragged, showing where
1789 * it will land.
1791 static void pinboard_set_shadow(gboolean on)
1793 GdkRectangle area;
1795 if (pinboard_shadow)
1797 area.x = shadow_x;
1798 area.y = shadow_y;
1799 area.width = SHADOW_SIZE + 1;
1800 area.height = SHADOW_SIZE + 1;
1802 gdk_window_invalidate_rect(current_pinboard->window->window,
1803 &area, TRUE);
1806 if (on)
1808 int old_x = shadow_x, old_y = shadow_y;
1810 gdk_window_get_pointer(current_pinboard->fixed->window,
1811 &shadow_x, &shadow_y, NULL);
1812 snap_to_grid(&shadow_x, &shadow_y);
1813 shadow_x -= SHADOW_SIZE / 2;
1814 shadow_y -= SHADOW_SIZE / 2;
1817 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1818 return;
1820 area.x = shadow_x;
1821 area.y = shadow_y;
1822 area.width = SHADOW_SIZE + 1;
1823 area.height = SHADOW_SIZE + 1;
1825 gdk_window_invalidate_rect(current_pinboard->window->window,
1826 &area, TRUE);
1829 pinboard_shadow = on;
1832 /* Called when dragging some pinboard icons finishes */
1833 void pinboard_move_icons(void)
1835 int x = shadow_x, y = shadow_y;
1836 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1837 int width, height;
1838 int dx, dy;
1839 GList *next;
1841 g_return_if_fail(pi != NULL);
1843 x += SHADOW_SIZE / 2;
1844 y += SHADOW_SIZE / 2;
1845 snap_to_grid(&x, &y);
1847 if (pi->x == x && pi->y == y)
1848 return;
1850 /* Find out how much the dragged icon moved (after snapping).
1851 * Move all selected icons by the same amount.
1853 dx = x - pi->x;
1854 dy = y - pi->y;
1856 /* Move the other selected icons to keep the same relative
1857 * position.
1859 for (next = icon_selection; next; next = next->next)
1861 PinIcon *pi = (PinIcon *) next->data;
1862 int nx, ny;
1864 g_return_if_fail(IS_PIN_ICON(pi));
1866 pi->x += dx;
1867 pi->y += dy;
1868 nx = pi->x;
1869 ny = pi->y;
1871 gdk_drawable_get_size(pi->win->window, &width, &height);
1872 offset_from_centre(pi, &nx, &ny);
1874 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1875 pi->win, nx, ny);
1878 pinboard_save();
1881 static void drag_leave(GtkWidget *widget,
1882 GdkDragContext *context,
1883 guint32 time,
1884 PinIcon *pi)
1886 pinboard_wink_item(NULL, FALSE);
1887 dnd_spring_abort();
1890 static gboolean bg_drag_leave(GtkWidget *widget,
1891 GdkDragContext *context,
1892 guint32 time,
1893 gpointer data)
1895 pinboard_set_shadow(FALSE);
1896 return TRUE;
1899 static gboolean bg_drag_motion(GtkWidget *widget,
1900 GdkDragContext *context,
1901 gint x,
1902 gint y,
1903 guint time,
1904 gpointer data)
1906 /* Dragging from the pinboard to the pinboard is not allowed */
1908 if (!provides(context, text_uri_list))
1909 return FALSE;
1911 pinboard_set_shadow(TRUE);
1913 gdk_drag_status(context,
1914 context->suggested_action == GDK_ACTION_ASK
1915 ? GDK_ACTION_LINK : context->suggested_action,
1916 time);
1917 return TRUE;
1920 static void drag_end(GtkWidget *widget,
1921 GdkDragContext *context,
1922 PinIcon *pi)
1924 pinboard_drag_in_progress = NULL;
1925 if (tmp_icon_selected)
1927 icon_select_only(NULL);
1928 tmp_icon_selected = FALSE;
1932 /* Something which affects all the icons has changed - reshape
1933 * and redraw all of them.
1935 static void reshape_all(void)
1937 GList *next;
1939 g_return_if_fail(current_pinboard != NULL);
1941 for (next = current_pinboard->icons; next; next = next->next)
1943 Icon *icon = (Icon *) next->data;
1944 pinboard_reshape_icon(icon);
1948 /* Turns off the pinboard. Does not call gtk_main_quit. */
1949 static void pinboard_clear(void)
1951 GList *next;
1953 g_return_if_fail(current_pinboard != NULL);
1955 tasklist_set_active(FALSE);
1957 next = current_pinboard->icons;
1958 while (next)
1960 PinIcon *pi = (PinIcon *) next->data;
1962 next = next->next;
1964 gtk_widget_destroy(pi->win);
1967 gtk_widget_destroy(current_pinboard->window);
1969 abandon_backdrop_app(current_pinboard);
1971 g_object_unref(current_pinboard->shadow_gc);
1972 current_pinboard->shadow_gc = NULL;
1974 g_free(current_pinboard->name);
1975 null_g_free(&current_pinboard);
1977 number_of_windows--;
1980 static gpointer parent_class;
1982 static void pin_icon_destroy(Icon *icon)
1984 PinIcon *pi = (PinIcon *) icon;
1986 g_return_if_fail(pi->win != NULL);
1988 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1989 gtk_widget_destroy(pi->win);
1992 static void pinboard_remove_items(void)
1994 g_return_if_fail(icon_selection != NULL);
1996 while (icon_selection)
1997 icon_destroy((Icon *) icon_selection->data);
1999 pinboard_save();
2002 static void pin_icon_update(Icon *icon)
2004 pinboard_reshape_icon(icon);
2005 pinboard_save();
2008 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
2010 return IS_PIN_ICON(other);
2013 static void pin_wink_icon(Icon *icon)
2015 pinboard_wink_item((PinIcon *) icon, TRUE);
2018 static void pin_icon_class_init(gpointer gclass, gpointer data)
2020 IconClass *icon = (IconClass *) gclass;
2022 parent_class = g_type_class_peek_parent(gclass);
2024 icon->destroy = pin_icon_destroy;
2025 icon->redraw = pinboard_reshape_icon;
2026 icon->update = pin_icon_update;
2027 icon->wink = pin_wink_icon;
2028 icon->remove_items = pinboard_remove_items;
2029 icon->same_group = pin_icon_same_group;
2032 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
2036 static GType pin_icon_get_type(void)
2038 static GType type = 0;
2040 if (!type)
2042 static const GTypeInfo info =
2044 sizeof (PinIconClass),
2045 NULL, /* base_init */
2046 NULL, /* base_finalise */
2047 pin_icon_class_init,
2048 NULL, /* class_finalise */
2049 NULL, /* class_data */
2050 sizeof(PinIcon),
2051 0, /* n_preallocs */
2052 pin_icon_init
2055 type = g_type_register_static(icon_get_type(),
2056 "PinIcon", &info, 0);
2059 return type;
2062 static PinIcon *pin_icon_new(const char *pathname, const char *name)
2064 PinIcon *pi;
2065 Icon *icon;
2067 pi = g_object_new(pin_icon_get_type(), NULL);
2068 icon = (Icon *) pi;
2070 icon_set_path(icon, pathname, name);
2072 return pi;
2075 /* Called when the window widget is somehow destroyed */
2076 static void pin_icon_destroyed(PinIcon *pi)
2078 g_return_if_fail(pi->win != NULL);
2080 pi->win = NULL;
2082 pinboard_wink_item(NULL, FALSE);
2084 if (pinboard_drag_in_progress == (Icon *) pi)
2085 pinboard_drag_in_progress = NULL;
2087 if (current_pinboard)
2088 current_pinboard->icons =
2089 g_list_remove(current_pinboard->icons, pi);
2091 g_object_unref(pi);
2094 /* Set the tooltip */
2095 static void pin_icon_set_tip(PinIcon *pi)
2097 XMLwrapper *ai;
2098 xmlNode *node;
2099 Icon *icon = (Icon *) pi;
2101 g_return_if_fail(pi != NULL);
2103 ai = appinfo_get(icon->path, icon->item);
2105 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2107 guchar *str;
2108 str = xmlNodeListGetString(node->doc,
2109 node->xmlChildrenNode, 1);
2110 if (str)
2112 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2113 g_free(str);
2116 else
2117 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2119 if (ai)
2120 g_object_unref(ai);
2123 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2125 GtkWidget *option_item;
2126 int pos[3];
2127 GList *list;
2129 pos[0] = event->x_root;
2130 pos[1] = event->y_root;
2132 option_item = gtk_image_menu_item_new_with_label(_("Backdrop..."));
2133 g_signal_connect(option_item, "activate",
2134 G_CALLBACK(pinboard_set_backdrop_box), NULL);
2135 icon_prepare_menu((Icon *) pi, option_item);
2137 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2138 pos[2] = g_list_length(list) - 6;
2139 g_list_free(list);
2141 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2142 position_menu,
2143 (gpointer) pos, event->button, event->time);
2146 static void create_pinboard_window(Pinboard *pinboard)
2148 GtkWidget *win;
2150 g_return_if_fail(pinboard->window == NULL);
2152 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2153 gtk_widget_set_style(win, gtk_widget_get_default_style());
2155 gtk_widget_set_double_buffered(win, FALSE);
2156 gtk_widget_set_app_paintable(win, TRUE);
2157 gtk_widget_set_name(win, "rox-pinboard");
2158 pinboard->window = win;
2159 pinboard->fixed = gtk_fixed_new();
2160 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2162 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2164 gtk_widget_set_size_request(win, screen_width, screen_height);
2165 gtk_widget_realize(win);
2166 gtk_window_move(GTK_WINDOW(win), 0, 0);
2167 make_panel_window(win);
2169 /* TODO: Use gdk function when it supports this type */
2171 GdkAtom desktop_type;
2173 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2174 FALSE);
2175 gdk_property_change(win->window,
2176 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2177 gdk_atom_intern("ATOM", FALSE), 32,
2178 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2181 gtk_widget_add_events(win,
2182 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2183 GDK_EXPOSURE_MASK |
2184 GDK_BUTTON1_MOTION_MASK |
2185 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2186 g_signal_connect(win, "button-press-event",
2187 G_CALLBACK(button_press_event), NULL);
2188 g_signal_connect(win, "button-release-event",
2189 G_CALLBACK(button_release_event), NULL);
2190 g_signal_connect(win, "motion-notify-event",
2191 G_CALLBACK(lasso_motion), NULL);
2192 g_signal_connect(pinboard->fixed, "expose_event",
2193 G_CALLBACK(bg_expose), NULL);
2195 /* Some window managers use scroll events on the root to switch
2196 * desktops, but don't cope with our pinboard window, so we forward
2197 * them manually in that case.
2199 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2201 /* Drag and drop handlers */
2202 drag_set_pinboard_dest(win);
2203 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2204 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2206 pinboard->shadow_gc = gdk_gc_new(win->window);
2207 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2209 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2211 gtk_widget_show_all(win);
2212 gdk_window_lower(win->window);
2215 /* Load image 'path' and scale according to 'style' */
2216 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2218 GdkPixmap *pixmap;
2219 GdkPixbuf *pixbuf;
2220 GError *error = NULL;
2222 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2223 if (error)
2225 delayed_error(_("Error loading backdrop image:\n%s\n"
2226 "Backdrop removed."),
2227 error->message);
2228 g_error_free(error);
2229 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2230 return NULL;
2233 if (style == BACKDROP_STRETCH)
2235 GdkPixbuf *old = pixbuf;
2237 pixbuf = gdk_pixbuf_scale_simple(old,
2238 screen_width, screen_height,
2239 GDK_INTERP_HYPER);
2241 g_object_unref(old);
2243 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2245 GdkPixbuf *old = pixbuf;
2246 int x, y, width, height;
2247 float scale;
2249 width = gdk_pixbuf_get_width(pixbuf);
2250 height = gdk_pixbuf_get_height(pixbuf);
2252 if (style == BACKDROP_SCALE)
2254 float scale_x, scale_y;
2255 scale_x = screen_width / ((float) width);
2256 scale_y = screen_height / ((float) height);
2257 scale = MIN(scale_x, scale_y);
2259 else
2260 scale = 1;
2262 pixbuf = gdk_pixbuf_new(
2263 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2264 8, screen_width, screen_height);
2265 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2266 ((pin_text_bg_col.green & 0xff00) << 8) |
2267 ((pin_text_bg_col.blue & 0xff00)));
2269 x = (screen_width - width * scale) / 2;
2270 y = (screen_height - height * scale) / 2;
2271 x = MAX(x, 0);
2272 y = MAX(y, 0);
2274 gdk_pixbuf_composite(old, pixbuf,
2275 x, y,
2276 MIN(screen_width, width * scale),
2277 MIN(screen_height, height * scale),
2278 x, y, scale, scale,
2279 o_pinboard_image_scaling.int_value?
2280 GDK_INTERP_BILINEAR: GDK_INTERP_HYPER,
2281 255);
2282 g_object_unref(old);
2285 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2286 &pixmap, NULL, 0);
2287 g_object_unref(pixbuf);
2289 return pixmap;
2292 static void abandon_backdrop_app(Pinboard *pinboard)
2294 g_return_if_fail(pinboard != NULL);
2296 if (pinboard->to_backdrop_app != -1)
2298 close(pinboard->to_backdrop_app);
2299 close(pinboard->from_backdrop_app);
2300 g_source_remove(pinboard->input_tag);
2301 g_string_free(pinboard->input_buffer, TRUE);
2302 pinboard->to_backdrop_app = -1;
2303 pinboard->from_backdrop_app = -1;
2304 pinboard->input_tag = -1;
2305 pinboard->input_buffer = NULL;
2308 g_return_if_fail(pinboard->to_backdrop_app == -1);
2309 g_return_if_fail(pinboard->from_backdrop_app == -1);
2310 g_return_if_fail(pinboard->input_tag == -1);
2311 g_return_if_fail(pinboard->input_buffer == NULL);
2314 /* A single line has been read from the child.
2315 * Processes the command, and replies 'ok' (or abandons the child on error).
2317 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2319 BackdropStyle style;
2320 const char *ok = "ok\n";
2322 if (strncmp(command, "tile ", 5) == 0)
2324 style = BACKDROP_TILE;
2325 command += 5;
2327 else if (strncmp(command, "scale ", 6) == 0)
2329 style = BACKDROP_SCALE;
2330 command += 6;
2332 else if (strncmp(command, "stretch ", 8) == 0)
2334 style = BACKDROP_STRETCH;
2335 command += 8;
2337 else if (strncmp(command, "centre ", 7) == 0)
2339 style = BACKDROP_CENTRE;
2340 command += 7;
2342 else
2344 g_warning("Invalid command '%s' from backdrop app\n",
2345 command);
2346 abandon_backdrop_app(pinboard);
2347 return;
2350 /* Load the backdrop. May abandon the program if loading fails. */
2351 reload_backdrop(pinboard, command, style);
2353 if (pinboard->to_backdrop_app == -1)
2354 return;
2356 while (*ok)
2358 int sent;
2360 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2361 if (sent <= 0)
2363 /* Remote app quit? Not an error. */
2364 abandon_backdrop_app(pinboard);
2365 break;
2367 ok += sent;
2371 static void backdrop_from_child(Pinboard *pinboard,
2372 int src, GdkInputCondition cond)
2374 char buf[256];
2375 int got;
2377 got = read(src, buf, sizeof(buf));
2379 if (got <= 0)
2381 if (got < 0)
2382 g_warning("backdrop_from_child: %s\n",
2383 g_strerror(errno));
2384 abandon_backdrop_app(pinboard);
2385 return;
2388 g_string_append_len(pinboard->input_buffer, buf, got);
2390 while (pinboard->from_backdrop_app != -1)
2392 int len;
2393 char *nl, *command;
2395 nl = strchr(pinboard->input_buffer->str, '\n');
2396 if (!nl)
2397 return; /* Haven't got a whole line yet */
2399 len = nl - pinboard->input_buffer->str;
2400 command = g_strndup(pinboard->input_buffer->str, len);
2401 g_string_erase(pinboard->input_buffer, 0, len + 1);
2403 command_from_backdrop_app(pinboard, command);
2405 g_free(command);
2409 static void reload_backdrop(Pinboard *pinboard,
2410 const gchar *backdrop,
2411 BackdropStyle backdrop_style)
2413 GtkStyle *style;
2415 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2417 const char *argv[] = {NULL, "--backdrop", NULL};
2418 GError *error = NULL;
2420 g_return_if_fail(pinboard->to_backdrop_app == -1);
2421 g_return_if_fail(pinboard->from_backdrop_app == -1);
2422 g_return_if_fail(pinboard->input_tag == -1);
2423 g_return_if_fail(pinboard->input_buffer == NULL);
2425 argv[0] = make_path(backdrop, "AppRun");
2427 /* Run the program. It'll send us a SOAP message and we'll
2428 * get back here with a different style and image.
2431 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2432 G_SPAWN_DO_NOT_REAP_CHILD |
2433 G_SPAWN_SEARCH_PATH,
2434 NULL, NULL, /* Child setup fn */
2435 NULL, /* Child PID */
2436 &pinboard->to_backdrop_app,
2437 &pinboard->from_backdrop_app,
2438 NULL, /* Standard error */
2439 &error))
2441 pinboard->input_buffer = g_string_new(NULL);
2442 pinboard->input_tag = gdk_input_add_full(
2443 pinboard->from_backdrop_app,
2444 GDK_INPUT_READ,
2445 (GdkInputFunction) backdrop_from_child,
2446 pinboard, NULL);
2448 else
2450 delayed_error("%s", error ? error->message : "(null)");
2451 g_error_free(error);
2453 return;
2456 /* Note: Copying a style does not ref the pixmaps! */
2458 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2459 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2461 if (backdrop)
2462 style->bg_pixmap[GTK_STATE_NORMAL] =
2463 load_backdrop(backdrop, backdrop_style);
2465 gdk_color_parse(o_pinboard_bg_colour.value,
2466 &style->bg[GTK_STATE_NORMAL]);
2468 gtk_widget_set_style(pinboard->window, style);
2470 g_object_unref(style);
2472 gtk_widget_queue_draw(pinboard->window);
2474 /* Also update root window property (for transparent xterms, etc) */
2475 if (style->bg_pixmap[GTK_STATE_NORMAL])
2477 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2478 gdk_property_change(gdk_get_default_root_window(),
2479 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2480 gdk_atom_intern("PIXMAP", FALSE),
2481 32, GDK_PROP_MODE_REPLACE,
2482 (guchar *) &id, 1);
2484 else
2486 gdk_property_delete(gdk_get_default_root_window(),
2487 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2491 #define SEARCH_STEP 32
2493 /* Search the area (omin, imin) to (omax, imax) for a free region the size of
2494 * 'rect' that doesn't overlap 'used'. Which of inner and outer is the
2495 * vertical axis depends on the configuration.
2497 * id and od give the direction of the search (step size).
2499 * Returns the start of the found region in inner/outer, or -1 if there is no
2500 * free space.
2502 static void search_free(GdkRectangle *rect, GdkRegion *used,
2503 int *outer, int od, int omin, int omax,
2504 int *inner, int id, int imin, int imax)
2506 *outer = od > 0 ? omin : omax;
2507 while (*outer >= omin && *outer <= omax)
2509 *inner = id > 0 ? imin : imax;
2510 while (*inner >= imin && *inner <= imax)
2512 if (gdk_region_rect_in(used, rect) ==
2513 GDK_OVERLAP_RECTANGLE_OUT)
2514 return;
2515 *inner += id;
2518 *outer += od;
2521 rect->x = -1;
2522 rect->y = -1;
2525 /* Search the width x height area from (x0, y0) for a free region of size
2526 * 'rect'. direction indicates whether to search rows or columns. dx, dy gives
2527 * the direction of the search.
2529 static void search_free_area(GdkRectangle *rect, GdkRegion *used,
2530 int direction, int dx, int dy, int x0, int y0, int width, int height)
2532 if (direction == DIR_VERT)
2534 search_free(rect, used,
2535 &rect->x, dx, x0, width,
2536 &rect->y, dy, y0, height);
2538 else
2540 search_free(rect, used,
2541 &rect->y, dy, x0, height,
2542 &rect->x, dx, y0, width);
2546 static gboolean search_free_xinerama(GdkRectangle *rect, GdkRegion *used,
2547 int direction, int dx, int dy, int rwidth, int rheight)
2549 GdkRectangle *geom = &monitor_geom[get_monitor_under_pointer()];
2551 search_free_area(rect, used, direction, dx, dy,
2552 geom->x, geom->y, geom->width - rwidth, geom->height - rheight);
2553 return rect->x != -1;
2556 /* Finds a free area on the pinboard large enough for the width and height
2557 * of the given rectangle, by filling in the x and y fields of 'rect'.
2558 * If 'old' is true, 'rect' has a previous position and we first check
2559 * if it is viable.
2560 * The search order respects user preferences.
2561 * If no area is free, returns any old area.
2563 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2564 gboolean old)
2566 GdkRegion *used;
2567 GList *next;
2568 GdkRectangle used_rect;
2569 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2571 used = gdk_region_new();
2573 panel_mark_used(used);
2575 /* Subtract the no-go areas... */
2577 if (o_top_margin.int_value > 0)
2579 used_rect.x = 0;
2580 used_rect.y = 0;
2581 used_rect.width = gdk_screen_width();
2582 used_rect.height = o_top_margin.int_value;
2583 gdk_region_union_with_rect(used, &used_rect);
2586 if (o_bottom_margin.int_value > 0)
2588 used_rect.x = 0;
2589 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2590 used_rect.width = gdk_screen_width();
2591 used_rect.height = o_bottom_margin.int_value;
2592 gdk_region_union_with_rect(used, &used_rect);
2595 if (o_left_margin.int_value > 0)
2597 used_rect.x = 0;
2598 used_rect.y = 0;
2599 used_rect.width = o_left_margin.int_value;
2600 used_rect.height = gdk_screen_height();
2601 gdk_region_union_with_rect(used, &used_rect);
2604 if (o_right_margin.int_value > 0)
2606 used_rect.x = gdk_screen_width() - o_right_margin.int_value;
2607 used_rect.y = 0;
2608 used_rect.width = o_right_margin.int_value;
2609 used_rect.height = gdk_screen_height();
2610 gdk_region_union_with_rect(used, &used_rect);
2613 /* Subtract the used areas... */
2615 next = GTK_FIXED(pinboard->fixed)->children;
2616 for (; next; next = next->next)
2618 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2620 if (!GTK_WIDGET_VISIBLE(fix->widget))
2621 continue;
2623 used_rect.x = fix->x;
2624 used_rect.y = fix->y;
2625 used_rect.width = fix->widget->requisition.width;
2626 used_rect.height = fix->widget->requisition.height;
2628 gdk_region_union_with_rect(used, &used_rect);
2631 /* Check the previous area */
2632 if(old) {
2633 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2634 gdk_region_destroy(used);
2635 return;
2639 /* Find the first free area (yes, this isn't exactly pretty, but
2640 * it works). If you know a better (fast!) algorithm, let me know!
2643 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2644 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2645 dx = -SEARCH_STEP;
2647 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2648 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2649 dy = -SEARCH_STEP;
2651 /* If pinboard covers more than one monitor, try to find free space on
2652 * monitor under pointer first, then whole screen if that fails */
2653 if (n_monitors == 1 || !search_free_xinerama(rect, used,
2654 o_iconify_dir.int_value, dx, dy, rect->width, rect->height))
2656 search_free_area(rect, used, o_iconify_dir.int_value, dx, dy,
2657 0, 0, screen_width - rect->width, screen_height - rect->height);
2660 gdk_region_destroy(used);
2662 if (rect->x == -1)
2664 rect->x = 0;
2665 rect->y = 0;
2669 /* Icon's size, shape or appearance has changed - update the display */
2670 static void pinboard_reshape_icon(Icon *icon)
2672 PinIcon *pi = (PinIcon *) icon;
2673 int x = pi->x, y = pi->y;
2675 set_size_and_style(pi);
2676 offset_from_centre(pi, &x, &y);
2678 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2680 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2681 pi->win, x, y);
2684 /* Newer versions of GTK seem to need this, or the icon doesn't
2685 * get redrawn.
2687 gtk_widget_queue_draw(pi->win);
2690 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2691 static void update_pinboard_font(void)
2693 if (pinboard_font)
2694 pango_font_description_free(pinboard_font);
2695 pinboard_font = o_label_font.value[0] != '\0'
2696 ? pango_font_description_from_string(o_label_font.value)
2697 : NULL;
2700 static void radios_changed(Radios *radios, gpointer data)
2702 GObject *dialog = G_OBJECT(data);
2703 DropBox *drop_box;
2704 const guchar *path;
2706 g_return_if_fail(dialog != NULL);
2708 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2710 g_return_if_fail(radios != NULL);
2711 g_return_if_fail(drop_box != NULL);
2712 g_return_if_fail(current_pinboard != NULL);
2714 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2716 path = drop_box_get_path(drop_box);
2717 if (path)
2718 pinboard_set_backdrop(path, radios_get_value(radios));
2722 static void update_radios(GtkWidget *dialog)
2724 Radios *radios;
2725 GtkWidget *hbox;
2727 g_return_if_fail(dialog != NULL);
2729 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2730 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2732 g_return_if_fail(current_pinboard != NULL);
2733 g_return_if_fail(radios != NULL);
2734 g_return_if_fail(hbox != NULL);
2736 switch (current_pinboard->backdrop_style)
2738 case BACKDROP_TILE:
2739 case BACKDROP_STRETCH:
2740 case BACKDROP_SCALE:
2741 case BACKDROP_CENTRE:
2742 radios_set_value(radios,
2743 current_pinboard->backdrop_style);
2744 gtk_widget_set_sensitive(hbox, TRUE);
2745 break;
2746 default:
2747 gtk_widget_set_sensitive(hbox, FALSE);
2748 radios_set_value(radios, BACKDROP_TILE);
2749 break;