r4053: Added option to only show iconified windows belonging to the current workspace
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob27cb14519a034bce7ee0b725c64580adebc93c6a
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 Option o_pinboard_tasklist_per_workspace;
145 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
146 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
147 static Option o_pinboard_tasklist, o_forward_buttons_13;
148 static Option o_iconify_start, o_iconify_dir;
149 static Option o_label_font, o_pinboard_shadow_colour;
150 static Option o_pinboard_shadow_labels;
151 static Option o_blackbox_hack;
153 static Option o_top_margin, o_bottom_margin;
154 static Option o_pinboard_image_scaling;
156 /* Static prototypes */
157 static GType pin_icon_get_type(void);
158 static void set_size_and_style(PinIcon *pi);
159 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
160 static gint end_wink(gpointer data);
161 static gboolean button_release_event(GtkWidget *widget,
162 GdkEventButton *event,
163 PinIcon *pi);
164 static gboolean enter_notify(GtkWidget *widget,
165 GdkEventCrossing *event,
166 PinIcon *pi);
167 static gint leave_notify(GtkWidget *widget,
168 GdkEventCrossing *event,
169 PinIcon *pi);
170 static gboolean button_press_event(GtkWidget *widget,
171 GdkEventButton *event,
172 PinIcon *pi);
173 static gboolean scroll_event(GtkWidget *widget,
174 GdkEventScroll *event);
175 static gint icon_motion_notify(GtkWidget *widget,
176 GdkEventMotion *event,
177 PinIcon *pi);
178 static const char *pin_from_file(gchar *line);
179 static void snap_to_grid(int *x, int *y);
180 static void offset_from_centre(PinIcon *pi, int *x, int *y);
181 static gboolean drag_motion(GtkWidget *widget,
182 GdkDragContext *context,
183 gint x,
184 gint y,
185 guint time,
186 PinIcon *pi);
187 static void drag_set_pinicon_dest(PinIcon *pi);
188 static void drag_leave(GtkWidget *widget,
189 GdkDragContext *context,
190 guint32 time,
191 PinIcon *pi);
192 static gboolean bg_drag_motion(GtkWidget *widget,
193 GdkDragContext *context,
194 gint x,
195 gint y,
196 guint time,
197 gpointer data);
198 static gboolean bg_drag_leave(GtkWidget *widget,
199 GdkDragContext *context,
200 guint32 time,
201 gpointer data);
202 static gboolean bg_expose(GtkWidget *window,
203 GdkEventExpose *event, gpointer data);
204 static void drag_end(GtkWidget *widget,
205 GdkDragContext *context,
206 PinIcon *pi);
207 static void reshape_all(void);
208 static void pinboard_check_options(void);
209 static void pinboard_load_from_xml(xmlDocPtr doc);
210 static void pinboard_clear(void);
211 static void pinboard_save(void);
212 static PinIcon *pin_icon_new(const char *pathname, const char *name);
213 static void pin_icon_destroyed(PinIcon *pi);
214 static void pin_icon_set_tip(PinIcon *pi);
215 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
216 static void create_pinboard_window(Pinboard *pinboard);
217 static void reload_backdrop(Pinboard *pinboard,
218 const gchar *backdrop,
219 BackdropStyle backdrop_style);
220 static void pinboard_reshape_icon(Icon *icon);
221 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
222 static void abandon_backdrop_app(Pinboard *pinboard);
223 static void drag_backdrop_dropped(GtkWidget *drop_box,
224 const guchar *path,
225 GtkWidget *dialog);
226 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
227 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
228 gboolean old);
229 static void update_pinboard_font(void);
230 static void draw_lasso(void);
231 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d);
232 static void clear_backdrop(GtkWidget *drop_box, gpointer data);
233 static void radios_changed(Radios *radios, gpointer data);
234 static void update_radios(GtkWidget *dialog);
235 static void pinboard_set_backdrop_box(void);
237 /****************************************************************
238 * EXTERNAL INTERFACE *
239 ****************************************************************/
241 void pinboard_init(void)
243 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
244 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
245 option_add_string(&o_pinboard_shadow_colour, "pinboard_shadow_colour",
246 "#000");
247 option_add_string(&o_label_font, "label_font", "");
248 option_add_int(&o_pinboard_shadow_labels, "pinboard_shadow_labels", 1);
250 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
251 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
252 GRID_STEP_COARSE);
253 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
254 option_add_int(&o_pinboard_tasklist_per_workspace, "pinboard_tasklist_per_workspace", FALSE);
255 option_add_int(&o_forward_buttons_13, "pinboard_forward_buttons_13",
256 FALSE);
258 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
259 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
261 option_add_int(&o_blackbox_hack, "blackbox_hack", FALSE);
263 option_add_int(&o_top_margin, "pinboard_top_margin", 0);
264 option_add_int(&o_bottom_margin, "pinboard_bottom_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)
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);
544 if (!loading_pinboard)
545 pinboard_save();
548 void pinboard_pin(const gchar *path, const gchar *name, int x, int y,
549 const gchar *shortcut)
551 pinboard_pin_with_args(path, name, x, y, shortcut, NULL);
555 * Remove an icon from the background. The first icon with a matching
556 * path is removed. If name is not NULL then that also must match
558 gboolean pinboard_remove(const gchar *path, const gchar *name)
560 GList *item;
561 PinIcon *pi;
562 Icon *icon;
564 g_return_val_if_fail(current_pinboard != NULL, FALSE);
566 for(item=current_pinboard->icons; item; item=g_list_next(item)) {
567 pi=(PinIcon *) item->data;
568 icon=(Icon *) pi;
570 if(strcmp(icon->path, path)!=0)
571 continue;
573 if(name && strcmp(name, icon->item->leafname)!=0)
574 continue;
576 icon_destroy(icon);
578 pinboard_save();
580 return TRUE;
583 return FALSE;
586 /* Put a border around the icon, briefly.
587 * If icon is NULL then cancel any existing wink.
588 * The icon will automatically unhighlight unless timeout is FALSE,
589 * in which case you must call this function again (with NULL or another
590 * icon) to remove the highlight.
592 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
594 PinIcon *old = current_wink_icon;
596 if (old == pi)
597 return;
599 current_wink_icon = pi;
601 if (old)
603 gtk_widget_queue_draw(old->win);
604 gdk_window_process_updates(old->widget->window, TRUE);
606 if (wink_timeout != -1)
607 g_source_remove(wink_timeout);
610 if (pi)
612 gtk_widget_queue_draw(pi->win);
613 gdk_window_process_updates(pi->widget->window, TRUE);
615 if (timeout)
616 wink_timeout = g_timeout_add(300, end_wink, NULL);
617 else
618 wink_timeout = -1;
622 /* 'app' is saved as the new application to set the backdrop. It will then be
623 * run, and should communicate with the filer as described in the manual.
625 void pinboard_set_backdrop_app(const gchar *app)
627 XMLwrapper *ai;
628 DirItem *item;
629 gboolean can_set;
631 item = diritem_new("");
632 diritem_restat(app, item, NULL);
633 if (!(item->flags & ITEM_FLAG_APPDIR))
635 delayed_error(_("The backdrop handler must be an application "
636 "directory. Drag an application directory "
637 "into the Set Backdrop dialog box, or (for "
638 "programmers) pass it to the SOAP "
639 "SetBackdropApp method."));
640 diritem_free(item);
641 return;
644 ai = appinfo_get(app, item);
645 diritem_free(item);
647 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
648 if (ai)
649 g_object_unref(ai);
651 if (can_set)
652 pinboard_set_backdrop(app, BACKDROP_PROGRAM);
653 else
654 delayed_error(_("You can only set the backdrop to an image "
655 "or to a program which knows how to "
656 "manage ROX-Filer's backdrop.\n\n"
657 "Programmers: the application's AppInfo.xml "
658 "must contain the CanSetBackdrop element, as "
659 "described in ROX-Filer's manual."));
662 /* Open a dialog box allowing the user to set the backdrop */
663 static void pinboard_set_backdrop_box(void)
665 GtkWidget *dialog, *frame, *label, *hbox;
666 GtkBox *vbox;
667 Radios *radios;
669 g_return_if_fail(current_pinboard != NULL);
671 if (set_backdrop_dialog)
672 gtk_widget_destroy(set_backdrop_dialog);
674 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
675 GTK_DIALOG_NO_SEPARATOR,
676 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
677 NULL);
678 set_backdrop_dialog = dialog;
679 g_signal_connect(dialog, "destroy",
680 G_CALLBACK(gtk_widget_destroyed), &set_backdrop_dialog);
681 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
683 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
685 label = gtk_label_new(_("Choose a style and drag an image in:"));
686 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
687 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
688 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
690 /* The Centred, Scaled, Tiled radios... */
691 hbox = gtk_hbox_new(TRUE, 2);
692 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
694 radios = radios_new(radios_changed, dialog);
695 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
696 g_object_set_data(G_OBJECT(dialog), "rox-radios-hbox", hbox);
698 radios_add(radios, _("Centre the image without scaling it"),
699 BACKDROP_CENTRE, _("Centre"));
700 radios_add(radios, _("Scale the image to fit the backdrop area, "
701 "without distorting it"),
702 BACKDROP_SCALE, _("Scale"));
703 radios_add(radios, _("Stretch the image to fill the backdrop area"),
704 BACKDROP_STRETCH, _("Stretch"));
705 radios_add(radios, _("Tile the image over the backdrop area"),
706 BACKDROP_TILE, _("Tile"));
708 update_radios(dialog);
710 /* The drop area... */
711 frame = drop_box_new(_("Drop an image here"));
712 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
714 radios_pack(radios, GTK_BOX(hbox));
715 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
717 drop_box_set_path(DROP_BOX(frame), current_pinboard->backdrop);
719 g_signal_connect(frame, "path_dropped",
720 G_CALLBACK(drag_backdrop_dropped), dialog);
721 g_signal_connect(frame, "clear",
722 G_CALLBACK(clear_backdrop), dialog);
724 g_signal_connect(dialog, "response",
725 G_CALLBACK(backdrop_response), NULL);
726 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
728 gtk_widget_show_all(dialog);
731 /* Also used by tasklist.c */
732 void draw_label_shadow(WrappedLabel *wl, GdkRegion *region)
734 GtkWidget *widget;
735 gint x, y;
737 if (!o_pinboard_shadow_labels.int_value)
738 return;
740 gdk_gc_set_clip_region(current_pinboard->shadow_gc, region);
742 widget = GTK_WIDGET(wl);
744 y = widget->allocation.y - wl->y_off;
745 x = widget->allocation.x - wl->x_off -
746 ((wl->text_width - widget->allocation.width) >> 1);
748 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
749 x + 1, y + 1, wl->layout);
751 if (o_pinboard_shadow_labels.int_value > 1)
752 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
753 x + 2, y + 2, wl->layout);
755 gdk_gc_set_clip_region(current_pinboard->shadow_gc, NULL);
758 /* Set and save (path, style) as the new backdrop.
759 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
760 * Otherwise, the image is displayed now.
762 void pinboard_set_backdrop(const gchar *path, BackdropStyle style)
764 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
765 (path != NULL && style != BACKDROP_NONE));
767 if (!current_pinboard)
769 if (!path)
770 return;
771 pinboard_activate("Default");
772 delayed_error(_("No pinboard was in use... "
773 "the 'Default' pinboard has been selected. "
774 "Use 'rox -p=Default' to turn it on in "
775 "future."));
776 g_return_if_fail(current_pinboard != NULL);
779 /* We might have just run the old backdrop program and now
780 * we're going to set a new one! Seems a bit mean...
783 abandon_backdrop_app(current_pinboard);
785 g_free(current_pinboard->backdrop);
786 current_pinboard->backdrop = g_strdup(path);
787 current_pinboard->backdrop_style = style;
788 reload_backdrop(current_pinboard,
789 current_pinboard->backdrop,
790 current_pinboard->backdrop_style);
792 pinboard_save();
794 if (set_backdrop_dialog)
796 DropBox *box = g_object_get_data(G_OBJECT(set_backdrop_dialog),
797 "rox-dropbox");
798 g_return_if_fail(box != NULL);
799 drop_box_set_path(box, current_pinboard->backdrop);
800 update_radios(set_backdrop_dialog);
804 /* Called on xrandr screen resizes */
805 void pinboard_update_size(void)
807 int width, height;
809 if (!current_pinboard)
810 return;
812 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
813 &width, &height);
815 /* Only update the pinboard's size if the screen gets bigger,
816 * not smaller. Not sure what to do about icons that end up
817 * offscreen if the screen shrinks, but perhaps a good policy
818 * is to leave them there until the screen size is increased
819 * again rather than mess them around. */
820 width = MAX(width, screen_width);
821 height = MAX(height, screen_height);
823 gtk_widget_set_size_request(current_pinboard->window, width, height);
826 /****************************************************************
827 * INTERNAL FUNCTIONS *
828 ****************************************************************/
830 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
832 gtk_widget_destroy(dialog);
835 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
837 pinboard_set_backdrop(NULL, BACKDROP_NONE);
840 static void drag_backdrop_dropped(GtkWidget *drop_box,
841 const guchar *path,
842 GtkWidget *dialog)
844 struct stat info;
845 Radios *radios;
847 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
848 g_return_if_fail(radios != NULL);
850 if (mc_stat(path, &info))
852 delayed_error(
853 _("Can't access '%s':\n%s"), path,
854 g_strerror(errno));
855 return;
858 if (S_ISDIR(info.st_mode))
860 /* Use this program to set the backdrop */
861 pinboard_set_backdrop_app(path);
863 else if (S_ISREG(info.st_mode))
864 pinboard_set_backdrop(path, radios_get_value(radios));
865 else
866 delayed_error(_("Only files (and certain applications) can be "
867 "used to set the background image."));
870 /* Do this in the idle loop so that we don't try to put an unmanaged
871 * pinboard behind a managed panel (crashes some WMs).
873 static gboolean recreate_pinboard(gchar *name)
875 pinboard_activate(name);
876 g_free(name);
878 return FALSE;
881 static void pinboard_check_options(void)
883 GdkColor n_fg, n_bg, n_shadow;
885 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
886 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
887 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
889 if (o_override_redirect.has_changed && current_pinboard)
891 gchar *name;
892 name = g_strdup(current_pinboard->name);
893 pinboard_activate(NULL);
894 g_idle_add((GtkFunction) recreate_pinboard, name);
897 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
899 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
900 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
901 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
902 o_pinboard_shadow_labels.has_changed ||
903 o_label_font.has_changed)
905 pin_text_fg_col = n_fg;
906 pin_text_bg_col = n_bg;
907 pin_text_shadow_col = n_shadow;
908 update_pinboard_font();
910 if (current_pinboard)
912 GtkWidget *w = current_pinboard->window;
913 GdkColormap *cm;
915 cm = gtk_widget_get_colormap(w);
917 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
918 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
920 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
921 &n_shadow);
923 abandon_backdrop_app(current_pinboard);
924 reload_backdrop(current_pinboard,
925 current_pinboard->backdrop,
926 current_pinboard->backdrop_style);
928 reshape_all();
931 tasklist_style_changed();
935 static gint end_wink(gpointer data)
937 pinboard_wink_item(NULL, FALSE);
938 return FALSE;
941 /* Sets the appearance from the options and updates the size request of
942 * the image.
944 static void set_size_and_style(PinIcon *pi)
946 Icon *icon = (Icon *) pi;
947 MaskedPixmap *image = di_image(icon->item);
948 int iwidth = image->width;
949 int iheight = image->height;
951 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
952 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
953 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
954 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
955 widget_modify_font(pi->label, pinboard_font);
957 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
959 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
962 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
964 static GtkWidgetClass *parent_class = NULL;
965 Icon *icon = (Icon *) pi;
966 DirItem *item = icon->item;
967 MaskedPixmap *image = di_image(item);
968 int iwidth = image->width;
969 int iheight = image->height;
970 int x, y;
972 if (!parent_class)
974 gpointer c = ((GTypeInstance *) widget)->g_class;
975 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
978 x = pi->widget->allocation.x;
979 y = pi->widget->allocation.y;
981 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
983 render_pixbuf(icon->selected ? image->pixbuf_lit : image->pixbuf,
984 pi->widget->window,
985 pi->widget->style->black_gc,
986 x, y, iwidth, iheight);
988 if (item->flags & ITEM_FLAG_SYMLINK)
990 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
991 pi->widget->style->black_gc,
992 x, y, -1, -1);
994 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
996 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
997 ? im_mounted
998 : im_unmounted;
1000 render_pixbuf(mp->pixbuf, pi->widget->window,
1001 pi->widget->style->black_gc,
1002 x, y, -1, -1);
1005 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
1007 if (icon->selected)
1009 GtkStyle *style = pi->label->style;
1010 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
1012 gdk_gc_set_clip_region(gc, event->region);
1013 gdk_draw_rectangle(pi->label->window, gc, TRUE,
1014 pi->label->allocation.x,
1015 pi->label->allocation.y,
1016 pi->label->allocation.width,
1017 pi->label->allocation.height);
1018 gdk_gc_set_clip_region(gc, NULL);
1020 else if (o_pinboard_shadow_labels.int_value)
1021 draw_label_shadow((WrappedLabel *) pi->label, event->region);
1023 /* Draw children */
1024 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
1025 event->region);
1026 (parent_class->expose_event)(widget, event);
1027 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
1029 /* Stop the button effect */
1030 return TRUE;
1033 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
1035 gint x, y, width, height;
1037 if (current_wink_icon != pi)
1038 return FALSE;
1040 x = widget->allocation.x;
1041 y = widget->allocation.y;
1042 width = widget->allocation.width;
1043 height = widget->allocation.height;
1045 gdk_draw_rectangle(widget->window,
1046 pi->widget->style->white_gc,
1047 FALSE,
1048 x, y, width - 1, height - 1);
1049 gdk_draw_rectangle(widget->window,
1050 pi->widget->style->black_gc,
1051 FALSE,
1052 x + 1, y + 1, width - 3, height - 3);
1054 return FALSE;
1057 static gboolean enter_notify(GtkWidget *widget,
1058 GdkEventCrossing *event,
1059 PinIcon *pi)
1061 icon_may_update((Icon *) pi);
1062 pin_icon_set_tip(pi);
1063 return TRUE;
1066 static gint leave_notify(GtkWidget *widget,
1067 GdkEventCrossing *event,
1068 PinIcon *pi)
1070 return TRUE;
1073 static void select_lasso(void)
1075 GList *next;
1076 int minx, miny, maxx, maxy;
1078 g_return_if_fail(lasso_in_progress == TRUE);
1080 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1081 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1082 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1083 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1085 for (next = current_pinboard->icons; next; next = next->next)
1087 PinIcon *pi = (PinIcon *) next->data;
1088 GtkAllocation *alloc = &pi->win->allocation;
1089 int cx = alloc->x + alloc->width / 2;
1090 int cy = alloc->y + alloc->height / 2;
1092 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1093 icon_set_selected((Icon *) pi, TRUE);
1097 static void cancel_lasso(void)
1099 draw_lasso();
1100 lasso_in_progress = FALSE;
1103 static void pinboard_lasso_box(int start_x, int start_y)
1105 if (lasso_in_progress)
1106 cancel_lasso();
1107 lasso_in_progress = TRUE;
1108 lasso_rect_x1 = lasso_rect_x2 = start_x;
1109 lasso_rect_y1 = lasso_rect_y2 = start_y;
1111 draw_lasso();
1114 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1116 if (!lasso_in_progress)
1117 return FALSE;
1119 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1121 draw_lasso();
1122 lasso_rect_x2 = event->x;
1123 lasso_rect_y2 = event->y;
1124 draw_lasso();
1127 return FALSE;
1130 /* Mark the area of the screen covered by the lasso box for redraw */
1131 static void draw_lasso(void)
1133 GdkRectangle area, edge;
1135 if (!lasso_in_progress)
1136 return;
1138 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1139 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1140 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1141 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1143 edge.x = area.x;
1144 edge.y = area.y;
1145 edge.width = area.width;
1147 edge.height = 2; /* Top */
1148 gdk_window_invalidate_rect(current_pinboard->window->window,
1149 &edge, TRUE);
1151 edge.y += area.height - 2; /* Bottom */
1152 gdk_window_invalidate_rect(current_pinboard->window->window,
1153 &edge, TRUE);
1155 edge.y = area.y;
1156 edge.height = area.height;
1157 edge.width = 2; /* Left */
1158 gdk_window_invalidate_rect(current_pinboard->window->window,
1159 &edge, TRUE);
1161 edge.x += area.width - 2; /* Right */
1162 gdk_window_invalidate_rect(current_pinboard->window->window,
1163 &edge, TRUE);
1166 static void perform_action(PinIcon *pi, GdkEventButton *event)
1168 BindAction action;
1169 Icon *icon = (Icon *) pi;
1171 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1172 event);
1174 /* Actions that can happen with or without an icon */
1175 switch (action)
1177 case ACT_LASSO_CLEAR:
1178 icon_select_only(NULL);
1179 /* (no break) */
1180 case ACT_LASSO_MODIFY:
1181 pinboard_lasso_box(event->x, event->y);
1182 return;
1183 case ACT_CLEAR_SELECTION:
1184 icon_select_only(NULL);
1185 return;
1186 case ACT_POPUP_MENU:
1187 dnd_motion_ungrab();
1188 pinboard_show_menu(event, pi);
1189 return;
1190 case ACT_IGNORE:
1191 return;
1192 default:
1193 break;
1196 g_return_if_fail(pi != NULL);
1198 switch (action)
1200 case ACT_OPEN_ITEM:
1201 dnd_motion_ungrab();
1202 pinboard_wink_item(pi, TRUE);
1203 if (event->type == GDK_2BUTTON_PRESS)
1204 icon_set_selected(icon, FALSE);
1205 icon_run(icon);
1206 break;
1207 case ACT_EDIT_ITEM:
1208 dnd_motion_ungrab();
1209 pinboard_wink_item(pi, TRUE);
1210 if (event->type == GDK_2BUTTON_PRESS)
1211 icon_set_selected(icon, FALSE);
1212 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1213 break;
1214 case ACT_PRIME_AND_SELECT:
1215 if (!icon->selected)
1216 icon_select_only(icon);
1217 dnd_motion_start(MOTION_READY_FOR_DND);
1218 break;
1219 case ACT_PRIME_AND_TOGGLE:
1220 icon_set_selected(icon, !icon->selected);
1221 dnd_motion_start(MOTION_READY_FOR_DND);
1222 break;
1223 case ACT_PRIME_FOR_DND:
1224 dnd_motion_start(MOTION_READY_FOR_DND);
1225 break;
1226 case ACT_TOGGLE_SELECTED:
1227 icon_set_selected(icon, !icon->selected);
1228 break;
1229 case ACT_SELECT_EXCL:
1230 icon_select_only(icon);
1231 break;
1232 default:
1233 g_warning("Unsupported action : %d\n", action);
1234 break;
1238 static void forward_to_root(GdkEventButton *event)
1240 XButtonEvent xev;
1242 if (event->type == GDK_BUTTON_PRESS)
1244 xev.type = ButtonPress;
1245 if (!o_blackbox_hack.int_value)
1246 XUngrabPointer(gdk_display, event->time);
1248 else
1249 xev.type = ButtonRelease;
1251 xev.window = gdk_x11_get_default_root_xwindow();
1252 xev.root = xev.window;
1253 xev.subwindow = None;
1254 xev.time = event->time;
1255 xev.x = event->x_root; /* Needed for icewm */
1256 xev.y = event->y_root;
1257 xev.x_root = event->x_root;
1258 xev.y_root = event->y_root;
1259 xev.state = event->state;
1260 xev.button = event->button;
1261 xev.same_screen = True;
1263 XSendEvent(gdk_display, xev.window, False,
1264 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1267 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1268 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1270 /* pi is NULL if this is a root event */
1271 static gboolean button_release_event(GtkWidget *widget,
1272 GdkEventButton *event,
1273 PinIcon *pi)
1275 if (FORWARDED_BUTTON(pi, event->button))
1276 forward_to_root(event);
1277 else if (dnd_motion_release(event))
1279 if (motion_buttons_pressed == 0 && lasso_in_progress)
1281 select_lasso();
1282 cancel_lasso();
1284 return FALSE;
1287 perform_action(pi, event);
1289 return TRUE;
1292 /* pi is NULL if this is a root event */
1293 static gboolean button_press_event(GtkWidget *widget,
1294 GdkEventButton *event,
1295 PinIcon *pi)
1297 /* Just in case we've jumped in front of everything... */
1298 gdk_window_lower(current_pinboard->window->window);
1300 if (FORWARDED_BUTTON(pi, event->button))
1301 forward_to_root(event);
1302 else if (dnd_motion_press(widget, event))
1303 perform_action(pi, event);
1305 return TRUE;
1308 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1309 * (for old window managers that don't catch the buttons themselves)
1311 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1313 XButtonEvent xev;
1315 xev.type = ButtonPress;
1316 xev.window = gdk_x11_get_default_root_xwindow();
1317 xev.root = xev.window;
1318 xev.subwindow = None;
1319 xev.time = event->time;
1320 xev.x = event->x_root; /* Needed for icewm */
1321 xev.y = event->y_root;
1322 xev.x_root = event->x_root;
1323 xev.y_root = event->y_root;
1324 xev.state = event->state;
1325 xev.same_screen = True;
1327 if (event->direction == GDK_SCROLL_UP)
1328 xev.button = 4;
1329 else if (event->direction == GDK_SCROLL_DOWN)
1330 xev.button = 5;
1331 else
1332 return FALSE;
1334 XSendEvent(gdk_display, xev.window, False,
1335 ButtonPressMask, (XEvent *) &xev);
1337 return TRUE;
1340 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1342 GtkWidget *widget = pi->win;
1343 Icon *icon = (Icon *) pi;
1345 if (!icon->selected)
1347 tmp_icon_selected = TRUE;
1348 icon_select_only(icon);
1351 g_return_if_fail(icon_selection != NULL);
1353 pinboard_drag_in_progress = icon;
1355 if (icon_selection->next == NULL)
1356 drag_one_item(widget, event, icon->path, icon->item, NULL);
1357 else
1359 guchar *uri_list;
1361 uri_list = icon_create_uri_list();
1362 drag_selection(widget, event, uri_list);
1363 g_free(uri_list);
1367 /* An icon is being dragged around... */
1368 static gint icon_motion_notify(GtkWidget *widget,
1369 GdkEventMotion *event,
1370 PinIcon *pi)
1372 if (motion_state == MOTION_READY_FOR_DND)
1374 if (dnd_motion_moved(event))
1375 start_drag(pi, event);
1376 return TRUE;
1379 return FALSE;
1382 static void backdrop_from_xml(xmlNode *node)
1384 gchar *style;
1386 g_free(current_pinboard->backdrop);
1387 current_pinboard->backdrop = xmlNodeGetContent(node);
1389 style = xmlGetProp(node, "style");
1391 if (style)
1393 current_pinboard->backdrop_style =
1394 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1395 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1396 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1397 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1398 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1399 BACKDROP_NONE;
1400 g_free(style);
1402 else
1403 current_pinboard->backdrop_style = BACKDROP_TILE;
1406 /* Create one pinboard icon for each icon in the doc */
1407 static void pinboard_load_from_xml(xmlDocPtr doc)
1409 xmlNodePtr node, root;
1410 char *tmp, *label, *path, *shortcut, *args;
1411 int x, y;
1413 root = xmlDocGetRootElement(doc);
1415 for (node = root->xmlChildrenNode; node; node = node->next)
1417 if (node->type != XML_ELEMENT_NODE)
1418 continue;
1419 if (strcmp(node->name, "backdrop") == 0)
1421 backdrop_from_xml(node);
1422 continue;
1424 if (strcmp(node->name, "icon") != 0)
1425 continue;
1427 tmp = xmlGetProp(node, "x");
1428 if (!tmp)
1429 continue;
1430 x = atoi(tmp);
1431 g_free(tmp);
1433 tmp = xmlGetProp(node, "y");
1434 if (!tmp)
1435 continue;
1436 y = atoi(tmp);
1437 g_free(tmp);
1439 label = xmlGetProp(node, "label");
1440 if (!label)
1441 label = g_strdup("<missing label>");
1442 path = xmlNodeGetContent(node);
1443 if (!path)
1444 path = g_strdup("<missing path>");
1445 shortcut = xmlGetProp(node, "shortcut");
1446 args = xmlGetProp(node, "args");
1448 pinboard_pin_with_args(path, label, x, y, shortcut, args);
1450 g_free(path);
1451 g_free(label);
1452 g_free(shortcut);
1453 g_free(args);
1457 /* Called for each line in the pinboard file while loading a new board.
1458 * Only used for old-format files when converting to XML.
1460 static const char *pin_from_file(gchar *line)
1462 gchar *leaf = NULL;
1463 int x, y, n;
1465 if (*line == '<')
1467 gchar *end;
1469 end = strchr(line + 1, '>');
1470 if (!end)
1471 return _("Missing '>' in icon label");
1473 leaf = g_strndup(line + 1, end - line - 1);
1475 line = end + 1;
1477 while (g_ascii_isspace(*line))
1478 line++;
1479 if (*line != ',')
1480 return _("Missing ',' after icon label");
1481 line++;
1484 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1485 return NULL; /* Ignore format errors */
1487 pinboard_pin(line + n, leaf, x, y, NULL);
1489 g_free(leaf);
1491 return NULL;
1494 /* Write the current state of the pinboard to the current pinboard file */
1495 static void pinboard_save(void)
1497 guchar *save = NULL;
1498 guchar *save_new = NULL;
1499 GList *next;
1500 xmlDocPtr doc = NULL;
1501 xmlNodePtr root;
1503 g_return_if_fail(current_pinboard != NULL);
1505 if (strchr(current_pinboard->name, '/'))
1506 save = g_strdup(current_pinboard->name);
1507 else
1509 guchar *leaf;
1511 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1512 save = choices_find_xdg_path_save(leaf, PROJECT, SITE, TRUE);
1513 g_free(leaf);
1516 if (!save)
1517 return;
1519 doc = xmlNewDoc("1.0");
1520 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1522 root = xmlDocGetRootElement(doc);
1524 if (current_pinboard->backdrop)
1526 BackdropStyle style = current_pinboard->backdrop_style;
1527 xmlNodePtr tree;
1529 tree = xmlNewTextChild(root, NULL, "backdrop",
1530 current_pinboard->backdrop);
1531 xmlSetProp(tree, "style",
1532 style == BACKDROP_TILE ? "Tiled" :
1533 style == BACKDROP_CENTRE ? "Centred" :
1534 style == BACKDROP_SCALE ? "Scaled" :
1535 style == BACKDROP_STRETCH ? "Stretched" :
1536 "Program");
1539 for (next = current_pinboard->icons; next; next = next->next)
1541 xmlNodePtr tree;
1542 PinIcon *pi = (PinIcon *) next->data;
1543 Icon *icon = (Icon *) pi;
1544 char *tmp;
1546 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1548 tmp = g_strdup_printf("%d", pi->x);
1549 xmlSetProp(tree, "x", tmp);
1550 g_free(tmp);
1552 tmp = g_strdup_printf("%d", pi->y);
1553 xmlSetProp(tree, "y", tmp);
1554 g_free(tmp);
1556 xmlSetProp(tree, "label", icon->item->leafname);
1557 if (icon->shortcut)
1558 xmlSetProp(tree, "shortcut", icon->shortcut);
1559 if (icon->args)
1560 xmlSetProp(tree, "args", icon->args);
1563 save_new = g_strconcat(save, ".new", NULL);
1564 if (save_xml_file(doc, save_new) || rename(save_new, save))
1565 delayed_error(_("Error saving pinboard %s: %s"),
1566 save, g_strerror(errno));
1567 g_free(save_new);
1569 g_free(save);
1570 if (doc)
1571 xmlFreeDoc(doc);
1574 static void snap_to_grid(int *x, int *y)
1576 int step = o_pinboard_grid_step.int_value;
1578 *x = ((*x + step / 2) / step) * step;
1579 *y = ((*y + step / 2) / step) * step;
1582 /* Convert (x,y) from a centre point to a window position */
1583 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1585 gboolean clamp = o_pinboard_clamp_icons.int_value;
1586 GtkRequisition req;
1588 gtk_widget_size_request(pi->win, &req);
1590 *x -= req.width >> 1;
1591 *y -= req.height >> 1;
1592 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1593 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1596 /* Same as drag_set_dest(), but for pinboard icons */
1597 static void drag_set_pinicon_dest(PinIcon *pi)
1599 GtkObject *obj = GTK_OBJECT(pi->win);
1601 make_drop_target(pi->win, 0);
1603 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1604 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1605 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1608 /* Called during the drag when the mouse is in a widget registered
1609 * as a drop target. Returns TRUE if we can accept the drop.
1611 static gboolean drag_motion(GtkWidget *widget,
1612 GdkDragContext *context,
1613 gint x,
1614 gint y,
1615 guint time,
1616 PinIcon *pi)
1618 GdkDragAction action = context->suggested_action;
1619 const char *type = NULL;
1620 Icon *icon = (Icon *) pi;
1621 DirItem *item = icon->item;
1623 if (gtk_drag_get_source_widget(context) == widget)
1625 g_dataset_set_data(context, "drop_dest_type",
1626 (gpointer) drop_dest_pass_through);
1627 return FALSE; /* Can't drag something to itself! */
1630 if (icon->selected)
1631 goto out; /* Can't drag a selection to itself */
1633 type = dnd_motion_item(context, &item);
1635 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1636 && type != drop_dest_prog)
1638 guint state;
1639 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1640 if (state & GDK_BUTTON1_MASK)
1641 action = GDK_ACTION_ASK;
1644 if (!item)
1645 type = NULL;
1646 out:
1647 /* We actually must pretend to accept the drop, even if the
1648 * directory isn't writeable, so that the spring-opening
1649 * thing works.
1652 /* Don't allow drops to non-writeable directories */
1653 if (o_dnd_spring_open.int_value == FALSE &&
1654 type == drop_dest_dir &&
1655 access(icon->path, W_OK) != 0)
1657 type = NULL;
1660 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1661 if (type)
1663 gdk_drag_status(context, action, time);
1664 g_dataset_set_data_full(context, "drop_dest_path",
1665 g_strdup(icon->path), g_free);
1666 if (type == drop_dest_dir)
1667 dnd_spring_load(context, NULL);
1669 pinboard_wink_item(pi, FALSE);
1671 else
1672 gdk_drag_status(context, 0, time);
1674 /* Always return TRUE to stop the pinboard getting the events */
1675 return TRUE;
1678 static gboolean pinboard_shadow = FALSE;
1679 static gint shadow_x, shadow_y;
1680 #define SHADOW_SIZE (ICON_WIDTH)
1682 static gboolean bg_expose(GtkWidget *widget,
1683 GdkEventExpose *event, gpointer data)
1685 GdkRectangle clipbox;
1686 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1687 gboolean double_buffer;
1689 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1690 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1692 gdk_region_get_clipbox(event->region, &clipbox);
1694 double_buffer = (clipbox.width * clipbox.height) < 20000;
1695 if (double_buffer)
1696 gdk_window_begin_paint_region(widget->window, event->region);
1698 /* Clear the area to the background image */
1700 GtkStyle *style = current_pinboard->window->style;
1701 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1703 gdk_gc_set_clip_region(gc, event->region);
1704 if (style->bg_pixmap[GTK_STATE_NORMAL])
1706 gdk_gc_set_ts_origin(gc, 0, 0);
1707 gdk_gc_set_fill(gc, GDK_TILED);
1708 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1711 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1712 clipbox.x, clipbox.y,
1713 clipbox.width, clipbox.height);
1714 if (style->bg_pixmap[GTK_STATE_NORMAL])
1715 gdk_gc_set_fill(gc, GDK_SOLID);
1717 gdk_gc_set_clip_region(gc, NULL);
1720 if (pinboard_shadow)
1722 gdk_draw_rectangle(widget->window,
1723 widget->style->white_gc, FALSE,
1724 shadow_x, shadow_y,
1725 SHADOW_SIZE, SHADOW_SIZE);
1726 gdk_draw_rectangle(widget->window,
1727 widget->style->black_gc, FALSE,
1728 shadow_x + 1, shadow_y + 1,
1729 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1732 if (lasso_in_progress)
1734 GdkRectangle area;
1736 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1737 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1738 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1739 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1741 if (area.width > 4 && area.height > 4)
1743 gdk_draw_rectangle(widget->window,
1744 widget->style->white_gc, FALSE,
1745 area.x, area.y,
1746 area.width - 1, area.height - 1);
1747 gdk_draw_rectangle(widget->window,
1748 widget->style->black_gc, FALSE,
1749 area.x + 1, area.y + 1,
1750 area.width - 3, area.height - 3);
1754 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1755 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1757 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1759 if (double_buffer)
1760 gdk_window_end_paint(widget->window);
1762 return TRUE;
1765 /* Draw a 'shadow' under an icon being dragged, showing where
1766 * it will land.
1768 static void pinboard_set_shadow(gboolean on)
1770 GdkRectangle area;
1772 if (pinboard_shadow)
1774 area.x = shadow_x;
1775 area.y = shadow_y;
1776 area.width = SHADOW_SIZE + 1;
1777 area.height = SHADOW_SIZE + 1;
1779 gdk_window_invalidate_rect(current_pinboard->window->window,
1780 &area, TRUE);
1783 if (on)
1785 int old_x = shadow_x, old_y = shadow_y;
1787 gdk_window_get_pointer(current_pinboard->fixed->window,
1788 &shadow_x, &shadow_y, NULL);
1789 snap_to_grid(&shadow_x, &shadow_y);
1790 shadow_x -= SHADOW_SIZE / 2;
1791 shadow_y -= SHADOW_SIZE / 2;
1794 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1795 return;
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 pinboard_shadow = on;
1809 /* Called when dragging some pinboard icons finishes */
1810 void pinboard_move_icons(void)
1812 int x = shadow_x, y = shadow_y;
1813 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1814 int width, height;
1815 int dx, dy;
1816 GList *next;
1818 g_return_if_fail(pi != NULL);
1820 x += SHADOW_SIZE / 2;
1821 y += SHADOW_SIZE / 2;
1822 snap_to_grid(&x, &y);
1824 if (pi->x == x && pi->y == y)
1825 return;
1827 /* Find out how much the dragged icon moved (after snapping).
1828 * Move all selected icons by the same amount.
1830 dx = x - pi->x;
1831 dy = y - pi->y;
1833 /* Move the other selected icons to keep the same relative
1834 * position.
1836 for (next = icon_selection; next; next = next->next)
1838 PinIcon *pi = (PinIcon *) next->data;
1839 int nx, ny;
1841 g_return_if_fail(IS_PIN_ICON(pi));
1843 pi->x += dx;
1844 pi->y += dy;
1845 nx = pi->x;
1846 ny = pi->y;
1848 gdk_drawable_get_size(pi->win->window, &width, &height);
1849 offset_from_centre(pi, &nx, &ny);
1851 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1852 pi->win, nx, ny);
1855 pinboard_save();
1858 static void drag_leave(GtkWidget *widget,
1859 GdkDragContext *context,
1860 guint32 time,
1861 PinIcon *pi)
1863 pinboard_wink_item(NULL, FALSE);
1864 dnd_spring_abort();
1867 static gboolean bg_drag_leave(GtkWidget *widget,
1868 GdkDragContext *context,
1869 guint32 time,
1870 gpointer data)
1872 pinboard_set_shadow(FALSE);
1873 return TRUE;
1876 static gboolean bg_drag_motion(GtkWidget *widget,
1877 GdkDragContext *context,
1878 gint x,
1879 gint y,
1880 guint time,
1881 gpointer data)
1883 /* Dragging from the pinboard to the pinboard is not allowed */
1885 if (!provides(context, text_uri_list))
1886 return FALSE;
1888 pinboard_set_shadow(TRUE);
1890 gdk_drag_status(context,
1891 context->suggested_action == GDK_ACTION_ASK
1892 ? GDK_ACTION_LINK : context->suggested_action,
1893 time);
1894 return TRUE;
1897 static void drag_end(GtkWidget *widget,
1898 GdkDragContext *context,
1899 PinIcon *pi)
1901 pinboard_drag_in_progress = NULL;
1902 if (tmp_icon_selected)
1904 icon_select_only(NULL);
1905 tmp_icon_selected = FALSE;
1909 /* Something which affects all the icons has changed - reshape
1910 * and redraw all of them.
1912 static void reshape_all(void)
1914 GList *next;
1916 g_return_if_fail(current_pinboard != NULL);
1918 for (next = current_pinboard->icons; next; next = next->next)
1920 Icon *icon = (Icon *) next->data;
1921 pinboard_reshape_icon(icon);
1925 /* Turns off the pinboard. Does not call gtk_main_quit. */
1926 static void pinboard_clear(void)
1928 GList *next;
1930 g_return_if_fail(current_pinboard != NULL);
1932 tasklist_set_active(FALSE);
1934 next = current_pinboard->icons;
1935 while (next)
1937 PinIcon *pi = (PinIcon *) next->data;
1939 next = next->next;
1941 gtk_widget_destroy(pi->win);
1944 gtk_widget_destroy(current_pinboard->window);
1946 abandon_backdrop_app(current_pinboard);
1948 g_object_unref(current_pinboard->shadow_gc);
1949 current_pinboard->shadow_gc = NULL;
1951 g_free(current_pinboard->name);
1952 null_g_free(&current_pinboard);
1954 number_of_windows--;
1957 static gpointer parent_class;
1959 static void pin_icon_destroy(Icon *icon)
1961 PinIcon *pi = (PinIcon *) icon;
1963 g_return_if_fail(pi->win != NULL);
1965 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1966 gtk_widget_destroy(pi->win);
1969 static void pinboard_remove_items(void)
1971 g_return_if_fail(icon_selection != NULL);
1973 while (icon_selection)
1974 icon_destroy((Icon *) icon_selection->data);
1976 pinboard_save();
1979 static void pin_icon_update(Icon *icon)
1981 pinboard_reshape_icon(icon);
1982 pinboard_save();
1985 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1987 return IS_PIN_ICON(other);
1990 static void pin_wink_icon(Icon *icon)
1992 pinboard_wink_item((PinIcon *) icon, TRUE);
1995 static void pin_icon_class_init(gpointer gclass, gpointer data)
1997 IconClass *icon = (IconClass *) gclass;
1999 parent_class = g_type_class_peek_parent(gclass);
2001 icon->destroy = pin_icon_destroy;
2002 icon->redraw = pinboard_reshape_icon;
2003 icon->update = pin_icon_update;
2004 icon->wink = pin_wink_icon;
2005 icon->remove_items = pinboard_remove_items;
2006 icon->same_group = pin_icon_same_group;
2009 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
2013 static GType pin_icon_get_type(void)
2015 static GType type = 0;
2017 if (!type)
2019 static const GTypeInfo info =
2021 sizeof (PinIconClass),
2022 NULL, /* base_init */
2023 NULL, /* base_finalise */
2024 pin_icon_class_init,
2025 NULL, /* class_finalise */
2026 NULL, /* class_data */
2027 sizeof(PinIcon),
2028 0, /* n_preallocs */
2029 pin_icon_init
2032 type = g_type_register_static(icon_get_type(),
2033 "PinIcon", &info, 0);
2036 return type;
2039 static PinIcon *pin_icon_new(const char *pathname, const char *name)
2041 PinIcon *pi;
2042 Icon *icon;
2044 pi = g_object_new(pin_icon_get_type(), NULL);
2045 icon = (Icon *) pi;
2047 icon_set_path(icon, pathname, name);
2049 return pi;
2052 /* Called when the window widget is somehow destroyed */
2053 static void pin_icon_destroyed(PinIcon *pi)
2055 g_return_if_fail(pi->win != NULL);
2057 pi->win = NULL;
2059 pinboard_wink_item(NULL, FALSE);
2061 if (pinboard_drag_in_progress == (Icon *) pi)
2062 pinboard_drag_in_progress = NULL;
2064 if (current_pinboard)
2065 current_pinboard->icons =
2066 g_list_remove(current_pinboard->icons, pi);
2068 g_object_unref(pi);
2071 /* Set the tooltip */
2072 static void pin_icon_set_tip(PinIcon *pi)
2074 XMLwrapper *ai;
2075 xmlNode *node;
2076 Icon *icon = (Icon *) pi;
2078 g_return_if_fail(pi != NULL);
2080 ai = appinfo_get(icon->path, icon->item);
2082 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2084 guchar *str;
2085 str = xmlNodeListGetString(node->doc,
2086 node->xmlChildrenNode, 1);
2087 if (str)
2089 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2090 g_free(str);
2093 else
2094 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2096 if (ai)
2097 g_object_unref(ai);
2100 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2102 GtkWidget *option_item;
2103 int pos[3];
2104 GList *list;
2106 pos[0] = event->x_root;
2107 pos[1] = event->y_root;
2109 option_item = gtk_image_menu_item_new_with_label(_("Backdrop..."));
2110 g_signal_connect(option_item, "activate",
2111 G_CALLBACK(pinboard_set_backdrop_box), NULL);
2112 icon_prepare_menu((Icon *) pi, option_item);
2114 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2115 pos[2] = g_list_length(list) - 6;
2116 g_list_free(list);
2118 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2119 position_menu,
2120 (gpointer) pos, event->button, event->time);
2123 static void create_pinboard_window(Pinboard *pinboard)
2125 GtkWidget *win;
2127 g_return_if_fail(pinboard->window == NULL);
2129 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2130 gtk_widget_set_style(win, gtk_widget_get_default_style());
2132 gtk_widget_set_double_buffered(win, FALSE);
2133 gtk_widget_set_app_paintable(win, TRUE);
2134 gtk_widget_set_name(win, "rox-pinboard");
2135 pinboard->window = win;
2136 pinboard->fixed = gtk_fixed_new();
2137 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2139 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2141 gtk_widget_set_size_request(win, screen_width, screen_height);
2142 gtk_widget_realize(win);
2143 gtk_window_move(GTK_WINDOW(win), 0, 0);
2144 make_panel_window(win);
2146 /* TODO: Use gdk function when it supports this type */
2148 GdkAtom desktop_type;
2150 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2151 FALSE);
2152 gdk_property_change(win->window,
2153 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2154 gdk_atom_intern("ATOM", FALSE), 32,
2155 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2158 gtk_widget_add_events(win,
2159 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2160 GDK_EXPOSURE_MASK |
2161 GDK_BUTTON1_MOTION_MASK |
2162 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2163 g_signal_connect(win, "button-press-event",
2164 G_CALLBACK(button_press_event), NULL);
2165 g_signal_connect(win, "button-release-event",
2166 G_CALLBACK(button_release_event), NULL);
2167 g_signal_connect(win, "motion-notify-event",
2168 G_CALLBACK(lasso_motion), NULL);
2169 g_signal_connect(pinboard->fixed, "expose_event",
2170 G_CALLBACK(bg_expose), NULL);
2172 /* Some window managers use scroll events on the root to switch
2173 * desktops, but don't cope with our pinboard window, so we forward
2174 * them manually in that case.
2176 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2178 /* Drag and drop handlers */
2179 drag_set_pinboard_dest(win);
2180 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2181 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2183 pinboard->shadow_gc = gdk_gc_new(win->window);
2184 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2186 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2188 gtk_widget_show_all(win);
2189 gdk_window_lower(win->window);
2192 /* Load image 'path' and scale according to 'style' */
2193 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2195 GdkPixmap *pixmap;
2196 GdkPixbuf *pixbuf;
2197 GError *error = NULL;
2199 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2200 if (error)
2202 delayed_error(_("Error loading backdrop image:\n%s\n"
2203 "Backdrop removed."),
2204 error->message);
2205 g_error_free(error);
2206 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2207 return NULL;
2210 if (style == BACKDROP_STRETCH)
2212 GdkPixbuf *old = pixbuf;
2214 pixbuf = gdk_pixbuf_scale_simple(old,
2215 screen_width, screen_height,
2216 GDK_INTERP_HYPER);
2218 g_object_unref(old);
2220 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2222 GdkPixbuf *old = pixbuf;
2223 int x, y, width, height;
2224 float scale;
2226 width = gdk_pixbuf_get_width(pixbuf);
2227 height = gdk_pixbuf_get_height(pixbuf);
2229 if (style == BACKDROP_SCALE)
2231 float scale_x, scale_y;
2232 scale_x = screen_width / ((float) width);
2233 scale_y = screen_height / ((float) height);
2234 scale = MIN(scale_x, scale_y);
2236 else
2237 scale = 1;
2239 pixbuf = gdk_pixbuf_new(
2240 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2241 8, screen_width, screen_height);
2242 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2243 ((pin_text_bg_col.green & 0xff00) << 8) |
2244 ((pin_text_bg_col.blue & 0xff00)));
2246 x = (screen_width - width * scale) / 2;
2247 y = (screen_height - height * scale) / 2;
2248 x = MAX(x, 0);
2249 y = MAX(y, 0);
2251 gdk_pixbuf_composite(old, pixbuf,
2252 x, y,
2253 MIN(screen_width, width * scale),
2254 MIN(screen_height, height * scale),
2255 x, y, scale, scale,
2256 o_pinboard_image_scaling.int_value?
2257 GDK_INTERP_BILINEAR: GDK_INTERP_HYPER,
2258 255);
2259 g_object_unref(old);
2262 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2263 &pixmap, NULL, 0);
2264 g_object_unref(pixbuf);
2266 return pixmap;
2269 static void abandon_backdrop_app(Pinboard *pinboard)
2271 g_return_if_fail(pinboard != NULL);
2273 if (pinboard->to_backdrop_app != -1)
2275 close(pinboard->to_backdrop_app);
2276 close(pinboard->from_backdrop_app);
2277 g_source_remove(pinboard->input_tag);
2278 g_string_free(pinboard->input_buffer, TRUE);
2279 pinboard->to_backdrop_app = -1;
2280 pinboard->from_backdrop_app = -1;
2281 pinboard->input_tag = -1;
2282 pinboard->input_buffer = NULL;
2285 g_return_if_fail(pinboard->to_backdrop_app == -1);
2286 g_return_if_fail(pinboard->from_backdrop_app == -1);
2287 g_return_if_fail(pinboard->input_tag == -1);
2288 g_return_if_fail(pinboard->input_buffer == NULL);
2291 /* A single line has been read from the child.
2292 * Processes the command, and replies 'ok' (or abandons the child on error).
2294 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2296 BackdropStyle style;
2297 const char *ok = "ok\n";
2299 if (strncmp(command, "tile ", 5) == 0)
2301 style = BACKDROP_TILE;
2302 command += 5;
2304 else if (strncmp(command, "scale ", 6) == 0)
2306 style = BACKDROP_SCALE;
2307 command += 6;
2309 else if (strncmp(command, "stretch ", 8) == 0)
2311 style = BACKDROP_STRETCH;
2312 command += 8;
2314 else if (strncmp(command, "centre ", 7) == 0)
2316 style = BACKDROP_CENTRE;
2317 command += 7;
2319 else
2321 g_warning("Invalid command '%s' from backdrop app\n",
2322 command);
2323 abandon_backdrop_app(pinboard);
2324 return;
2327 /* Load the backdrop. May abandon the program if loading fails. */
2328 reload_backdrop(pinboard, command, style);
2330 if (pinboard->to_backdrop_app == -1)
2331 return;
2333 while (*ok)
2335 int sent;
2337 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2338 if (sent <= 0)
2340 /* Remote app quit? Not an error. */
2341 abandon_backdrop_app(pinboard);
2342 break;
2344 ok += sent;
2348 static void backdrop_from_child(Pinboard *pinboard,
2349 int src, GdkInputCondition cond)
2351 char buf[256];
2352 int got;
2354 got = read(src, buf, sizeof(buf));
2356 if (got <= 0)
2358 if (got < 0)
2359 g_warning("backdrop_from_child: %s\n",
2360 g_strerror(errno));
2361 abandon_backdrop_app(pinboard);
2362 return;
2365 g_string_append_len(pinboard->input_buffer, buf, got);
2367 while (pinboard->from_backdrop_app != -1)
2369 int len;
2370 char *nl, *command;
2372 nl = strchr(pinboard->input_buffer->str, '\n');
2373 if (!nl)
2374 return; /* Haven't got a whole line yet */
2376 len = nl - pinboard->input_buffer->str;
2377 command = g_strndup(pinboard->input_buffer->str, len);
2378 g_string_erase(pinboard->input_buffer, 0, len + 1);
2380 command_from_backdrop_app(pinboard, command);
2382 g_free(command);
2386 static void reload_backdrop(Pinboard *pinboard,
2387 const gchar *backdrop,
2388 BackdropStyle backdrop_style)
2390 GtkStyle *style;
2392 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2394 const char *argv[] = {NULL, "--backdrop", NULL};
2395 GError *error = NULL;
2397 g_return_if_fail(pinboard->to_backdrop_app == -1);
2398 g_return_if_fail(pinboard->from_backdrop_app == -1);
2399 g_return_if_fail(pinboard->input_tag == -1);
2400 g_return_if_fail(pinboard->input_buffer == NULL);
2402 argv[0] = make_path(backdrop, "AppRun");
2404 /* Run the program. It'll send us a SOAP message and we'll
2405 * get back here with a different style and image.
2408 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2409 G_SPAWN_DO_NOT_REAP_CHILD |
2410 G_SPAWN_SEARCH_PATH,
2411 NULL, NULL, /* Child setup fn */
2412 NULL, /* Child PID */
2413 &pinboard->to_backdrop_app,
2414 &pinboard->from_backdrop_app,
2415 NULL, /* Standard error */
2416 &error))
2418 pinboard->input_buffer = g_string_new(NULL);
2419 pinboard->input_tag = gdk_input_add_full(
2420 pinboard->from_backdrop_app,
2421 GDK_INPUT_READ,
2422 (GdkInputFunction) backdrop_from_child,
2423 pinboard, NULL);
2425 else
2427 delayed_error("%s", error ? error->message : "(null)");
2428 g_error_free(error);
2430 return;
2433 /* Note: Copying a style does not ref the pixmaps! */
2435 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2436 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2438 if (backdrop)
2439 style->bg_pixmap[GTK_STATE_NORMAL] =
2440 load_backdrop(backdrop, backdrop_style);
2442 gdk_color_parse(o_pinboard_bg_colour.value,
2443 &style->bg[GTK_STATE_NORMAL]);
2445 gtk_widget_set_style(pinboard->window, style);
2447 g_object_unref(style);
2449 gtk_widget_queue_draw(pinboard->window);
2451 /* Also update root window property (for transparent xterms, etc) */
2452 if (style->bg_pixmap[GTK_STATE_NORMAL])
2454 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2455 gdk_property_change(gdk_get_default_root_window(),
2456 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2457 gdk_atom_intern("PIXMAP", FALSE),
2458 32, GDK_PROP_MODE_REPLACE,
2459 (guchar *) &id, 1);
2461 else
2463 gdk_property_delete(gdk_get_default_root_window(),
2464 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2468 #define SEARCH_STEP 32
2470 static void search_free(GdkRectangle *rect, GdkRegion *used,
2471 int *outer, int od, int omax,
2472 int *inner, int id, int imax)
2474 *outer = od > 0 ? 0 : omax;
2475 while (*outer >= 0 && *outer <= omax)
2477 *inner = id > 0 ? 0 : imax;
2478 while (*inner >= 0 && *inner <= imax)
2480 if (gdk_region_rect_in(used, rect) ==
2481 GDK_OVERLAP_RECTANGLE_OUT)
2482 return;
2483 *inner += id;
2486 *outer += od;
2489 rect->x = -1;
2490 rect->y = -1;
2493 /* Finds a free area on the pinboard large enough for the width and height
2494 * of the given rectangle, by filling in the x and y fields of 'rect'.
2495 * If 'old' is true, 'rect' has a previous position and we first check
2496 * if it is viable.
2497 * The search order respects user preferences.
2498 * If no area is free, returns any old area.
2500 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2501 gboolean old)
2503 GdkRegion *used;
2504 GList *next;
2505 GdkRectangle used_rect;
2506 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2508 used = gdk_region_new();
2510 panel_mark_used(used);
2512 /* Subtract the no-go areas... */
2514 if (o_top_margin.int_value > 0)
2516 used_rect.x = 0;
2517 used_rect.y = 0;
2518 used_rect.width = gdk_screen_width();
2519 used_rect.height = o_top_margin.int_value;
2520 gdk_region_union_with_rect(used, &used_rect);
2523 if (o_bottom_margin.int_value > 0)
2525 used_rect.x = 0;
2526 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2527 used_rect.width = gdk_screen_width();
2528 used_rect.height = o_bottom_margin.int_value;
2529 gdk_region_union_with_rect(used, &used_rect);
2532 /* Subtract the used areas... */
2534 next = GTK_FIXED(pinboard->fixed)->children;
2535 for (; next; next = next->next)
2537 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2539 if (!GTK_WIDGET_VISIBLE(fix->widget))
2540 continue;
2542 used_rect.x = fix->x;
2543 used_rect.y = fix->y;
2544 used_rect.width = fix->widget->requisition.width;
2545 used_rect.height = fix->widget->requisition.height;
2547 gdk_region_union_with_rect(used, &used_rect);
2550 /* Check the previous area */
2551 if(old) {
2552 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2553 gdk_region_destroy(used);
2554 return;
2558 /* Find the first free area (yes, this isn't exactly pretty, but
2559 * it works). If you know a better (fast!) algorithm, let me know!
2563 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2564 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2565 dx = -SEARCH_STEP;
2567 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2568 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2569 dy = -SEARCH_STEP;
2571 if (o_iconify_dir.int_value == DIR_VERT)
2573 search_free(rect, used,
2574 &rect->x, dx, screen_width - rect->width,
2575 &rect->y, dy, screen_height - rect->height);
2577 else
2579 search_free(rect, used,
2580 &rect->y, dy, screen_height - rect->height,
2581 &rect->x, dx, screen_width - rect->width);
2584 gdk_region_destroy(used);
2586 if (rect->x == -1)
2588 rect->x = 0;
2589 rect->y = 0;
2593 /* Icon's size, shape or appearance has changed - update the display */
2594 static void pinboard_reshape_icon(Icon *icon)
2596 PinIcon *pi = (PinIcon *) icon;
2597 int x = pi->x, y = pi->y;
2599 set_size_and_style(pi);
2600 offset_from_centre(pi, &x, &y);
2602 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2604 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2605 pi->win, x, y);
2608 /* Newer versions of GTK seem to need this, or the icon doesn't
2609 * get redrawn.
2611 gtk_widget_queue_draw(pi->win);
2614 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2615 static void update_pinboard_font(void)
2617 if (pinboard_font)
2618 pango_font_description_free(pinboard_font);
2619 pinboard_font = o_label_font.value[0] != '\0'
2620 ? pango_font_description_from_string(o_label_font.value)
2621 : NULL;
2624 static void radios_changed(Radios *radios, gpointer data)
2626 GObject *dialog = G_OBJECT(data);
2627 DropBox *drop_box;
2628 const guchar *path;
2630 g_return_if_fail(dialog != NULL);
2632 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2634 g_return_if_fail(radios != NULL);
2635 g_return_if_fail(drop_box != NULL);
2636 g_return_if_fail(current_pinboard != NULL);
2638 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2640 path = drop_box_get_path(drop_box);
2641 if (path)
2642 pinboard_set_backdrop(path, radios_get_value(radios));
2646 static void update_radios(GtkWidget *dialog)
2648 Radios *radios;
2649 GtkWidget *hbox;
2651 g_return_if_fail(dialog != NULL);
2653 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2654 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2656 g_return_if_fail(current_pinboard != NULL);
2657 g_return_if_fail(radios != NULL);
2658 g_return_if_fail(hbox != NULL);
2660 switch (current_pinboard->backdrop_style)
2662 case BACKDROP_TILE:
2663 case BACKDROP_STRETCH:
2664 case BACKDROP_SCALE:
2665 case BACKDROP_CENTRE:
2666 radios_set_value(radios,
2667 current_pinboard->backdrop_style);
2668 gtk_widget_set_sensitive(hbox, TRUE);
2669 break;
2670 default:
2671 gtk_widget_set_sensitive(hbox, FALSE);
2672 radios_set_value(radios, BACKDROP_TILE);
2673 break;