r3711: PinboardRemove SOAP call
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob78f26d86c542c9d86c5983fa1228a8037ed046f8
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* pinboard.c - icons on the desktop background */
24 #include "config.h"
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <gtk/gtk.h>
31 #include <gdk/gdkx.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <libxml/parser.h>
35 #include <signal.h>
37 #include "global.h"
39 #include "pinboard.h"
40 #include "main.h"
41 #include "dnd.h"
42 #include "pixmaps.h"
43 #include "type.h"
44 #include "choices.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "diritem.h"
49 #include "bind.h"
50 #include "icon.h"
51 #include "run.h"
52 #include "appinfo.h"
53 #include "menu.h"
54 #include "xml.h"
55 #include "tasklist.h"
56 #include "panel.h" /* For panel_mark_used() */
57 #include "wrapped.h"
58 #include "dropbox.h"
60 static gboolean tmp_icon_selected = FALSE; /* When dragging */
62 static GtkWidget *set_backdrop_dialog = NULL;
64 struct _Pinboard {
65 guchar *name; /* Leaf name */
66 GList *icons;
67 GtkStyle *style;
69 gchar *backdrop; /* Pathname */
70 BackdropStyle backdrop_style;
71 gint to_backdrop_app; /* pipe FD, or -1 */
72 gint from_backdrop_app; /* pipe FD, or -1 */
73 gint input_tag;
74 GString *input_buffer;
76 GtkWidget *window; /* Screen-sized window */
77 GtkWidget *fixed;
78 GdkGC *shadow_gc;
81 #define IS_PIN_ICON(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), pin_icon_get_type())
83 typedef struct _PinIconClass PinIconClass;
84 typedef struct _PinIcon PinIcon;
86 struct _PinIconClass {
87 IconClass parent;
90 struct _PinIcon {
91 Icon icon;
93 int x, y;
94 GtkWidget *win;
95 GtkWidget *widget; /* The drawing area for the icon */
96 GtkWidget *label;
99 /* The number of pixels between the bottom of the image and the top
100 * of the text.
102 #define GAP 4
104 /* The size of the border around the icon which is used when winking */
105 #define WINK_FRAME 2
107 /* Grid sizes */
108 #define GRID_STEP_FINE 2
109 #define GRID_STEP_MED 16
110 #define GRID_STEP_COARSE 32
112 /* Used in options */
113 #define CORNER_TOP_LEFT 0
114 #define CORNER_TOP_RIGHT 1
115 #define CORNER_BOTTOM_LEFT 2
116 #define CORNER_BOTTOM_RIGHT 3
118 #define DIR_HORZ 0
119 #define DIR_VERT 1
121 static PinIcon *current_wink_icon = NULL;
122 static gint wink_timeout;
124 /* Used for the text colours (only) in the icons */
125 GdkColor pin_text_fg_col, pin_text_bg_col;
126 PangoFontDescription *pinboard_font = NULL; /* NULL => Gtk default */
128 static GdkColor pin_text_shadow_col;
130 Pinboard *current_pinboard = NULL;
131 static gint loading_pinboard = 0; /* Non-zero => loading */
133 /* The Icon that was used to start the current drag, if any */
134 Icon *pinboard_drag_in_progress = NULL;
136 /* For selecting groups of icons */
137 static gboolean lasso_in_progress = FALSE;
138 static int lasso_rect_x1, lasso_rect_x2;
139 static int lasso_rect_y1, lasso_rect_y2;
141 /* Tracking icon positions */
142 static GHashTable *placed_icons=NULL;
144 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
145 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
146 static Option o_pinboard_tasklist, o_forward_buttons_13;
147 static Option o_iconify_start, o_iconify_dir;
148 static Option o_label_font, o_pinboard_shadow_colour;
149 static Option o_pinboard_shadow_labels;
150 static Option o_blackbox_hack;
152 static Option o_top_margin, o_bottom_margin;
154 /* Static prototypes */
155 static GType pin_icon_get_type(void);
156 static void set_size_and_style(PinIcon *pi);
157 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
158 static gint end_wink(gpointer data);
159 static gboolean button_release_event(GtkWidget *widget,
160 GdkEventButton *event,
161 PinIcon *pi);
162 static gboolean enter_notify(GtkWidget *widget,
163 GdkEventCrossing *event,
164 PinIcon *pi);
165 static gint leave_notify(GtkWidget *widget,
166 GdkEventCrossing *event,
167 PinIcon *pi);
168 static gboolean button_press_event(GtkWidget *widget,
169 GdkEventButton *event,
170 PinIcon *pi);
171 static gboolean scroll_event(GtkWidget *widget,
172 GdkEventScroll *event);
173 static gint icon_motion_notify(GtkWidget *widget,
174 GdkEventMotion *event,
175 PinIcon *pi);
176 static const char *pin_from_file(gchar *line);
177 static void snap_to_grid(int *x, int *y);
178 static void offset_from_centre(PinIcon *pi, int *x, int *y);
179 static gboolean drag_motion(GtkWidget *widget,
180 GdkDragContext *context,
181 gint x,
182 gint y,
183 guint time,
184 PinIcon *pi);
185 static void drag_set_pinicon_dest(PinIcon *pi);
186 static void drag_leave(GtkWidget *widget,
187 GdkDragContext *context,
188 guint32 time,
189 PinIcon *pi);
190 static gboolean bg_drag_motion(GtkWidget *widget,
191 GdkDragContext *context,
192 gint x,
193 gint y,
194 guint time,
195 gpointer data);
196 static gboolean bg_drag_leave(GtkWidget *widget,
197 GdkDragContext *context,
198 guint32 time,
199 gpointer data);
200 static gboolean bg_expose(GtkWidget *window,
201 GdkEventExpose *event, gpointer data);
202 static void drag_end(GtkWidget *widget,
203 GdkDragContext *context,
204 PinIcon *pi);
205 static void reshape_all(void);
206 static void pinboard_check_options(void);
207 static void pinboard_load_from_xml(xmlDocPtr doc);
208 static void pinboard_clear(void);
209 static void pinboard_save(void);
210 static PinIcon *pin_icon_new(const char *pathname, const char *name);
211 static void pin_icon_destroyed(PinIcon *pi);
212 static void pin_icon_set_tip(PinIcon *pi);
213 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
214 static void create_pinboard_window(Pinboard *pinboard);
215 static void reload_backdrop(Pinboard *pinboard,
216 const gchar *backdrop,
217 BackdropStyle backdrop_style);
218 static void pinboard_reshape_icon(Icon *icon);
219 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
220 static void abandon_backdrop_app(Pinboard *pinboard);
221 static void drag_backdrop_dropped(GtkWidget *drop_box,
222 const guchar *path,
223 GtkWidget *dialog);
224 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
225 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
226 gboolean old);
227 static void update_pinboard_font(void);
228 static void draw_lasso(void);
229 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d);
230 static void clear_backdrop(GtkWidget *drop_box, gpointer data);
231 static void radios_changed(gpointer data);
232 static void update_radios(GtkWidget *dialog);
234 /****************************************************************
235 * EXTERNAL INTERFACE *
236 ****************************************************************/
238 void pinboard_init(void)
240 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
241 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
242 option_add_string(&o_pinboard_shadow_colour, "pinboard_shadow_colour",
243 "#000");
244 option_add_string(&o_label_font, "label_font", "");
245 option_add_int(&o_pinboard_shadow_labels, "pinboard_shadow_labels", 1);
247 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
248 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
249 GRID_STEP_COARSE);
250 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
251 option_add_int(&o_forward_buttons_13, "pinboard_forward_buttons_13",
252 FALSE);
254 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
255 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
257 option_add_int(&o_blackbox_hack, "blackbox_hack", FALSE);
259 option_add_int(&o_top_margin, "pinboard_top_margin", 0);
260 option_add_int(&o_bottom_margin, "pinboard_bottom_margin", 0);
262 option_add_notify(pinboard_check_options);
264 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
265 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
266 gdk_color_parse(o_pinboard_shadow_colour.value, &pin_text_shadow_col);
267 update_pinboard_font();
269 placed_icons=g_hash_table_new(g_str_hash, g_str_equal);
272 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
273 * and make it the current pinboard.
274 * Any existing pinned items are removed. You must call this
275 * at least once before using the pinboard. NULL disables the
276 * pinboard.
278 void pinboard_activate(const gchar *name)
280 Pinboard *old_board = current_pinboard;
281 guchar *path, *slash;
283 /* Treat an empty name the same as NULL */
284 if (name && !*name)
285 name = NULL;
287 if (old_board)
288 pinboard_clear();
290 if (!name)
292 if (number_of_windows < 1 && gtk_main_level() > 0)
293 gtk_main_quit();
295 gdk_property_delete(gdk_get_default_root_window(),
296 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
297 return;
300 number_of_windows++;
302 slash = strchr(name, '/');
303 if (slash)
305 if (access(name, F_OK))
306 path = NULL; /* File does not (yet) exist */
307 else
308 path = g_strdup(name);
310 else
312 guchar *leaf;
314 leaf = g_strconcat("pb_", name, NULL);
315 path = choices_find_path_load(leaf, PROJECT);
316 g_free(leaf);
319 current_pinboard = g_new(Pinboard, 1);
320 current_pinboard->name = g_strdup(name);
321 current_pinboard->icons = NULL;
322 current_pinboard->window = NULL;
323 current_pinboard->backdrop = NULL;
324 current_pinboard->backdrop_style = BACKDROP_NONE;
325 current_pinboard->to_backdrop_app = -1;
326 current_pinboard->from_backdrop_app = -1;
327 current_pinboard->input_tag = -1;
328 current_pinboard->input_buffer = NULL;
330 create_pinboard_window(current_pinboard);
332 loading_pinboard++;
333 if (path)
335 xmlDocPtr doc;
336 doc = xmlParseFile(path);
337 if (doc)
339 pinboard_load_from_xml(doc);
340 xmlFreeDoc(doc);
341 reload_backdrop(current_pinboard,
342 current_pinboard->backdrop,
343 current_pinboard->backdrop_style);
345 else
347 parse_file(path, pin_from_file);
348 info_message(_("Your old pinboard file has been "
349 "converted to the new XML format."));
350 pinboard_save();
352 g_free(path);
354 else
355 pinboard_pin(home_dir, "Home",
356 4 + ICON_WIDTH / 2,
357 4 + ICON_HEIGHT / 2,
358 NULL);
359 loading_pinboard--;
361 if (o_pinboard_tasklist.int_value)
362 tasklist_set_active(TRUE);
365 /* Return the window of the current pinboard, or NULL.
366 * Used to make sure lowering the panels doesn't lose them...
368 GdkWindow *pinboard_get_window(void)
370 if (current_pinboard)
371 return current_pinboard->window->window;
372 return NULL;
375 const char *pinboard_get_name(void)
377 g_return_val_if_fail(current_pinboard != NULL, NULL);
379 return current_pinboard->name;
382 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
383 * being cleared.
385 void pinboard_add_widget(GtkWidget *widget, const gchar *name)
387 GtkRequisition req;
388 GdkRectangle *rect=NULL;
389 gboolean found=FALSE;
391 g_return_if_fail(current_pinboard != NULL);
393 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, 0, 0);
395 gtk_widget_size_request(widget, &req);
397 if(name) {
398 rect=g_hash_table_lookup(placed_icons, name);
399 if(rect)
400 found=TRUE;
402 /*printf("%s at %p %d\n", name? name: "(nil)", rect, found);*/
404 if(rect) {
405 if(rect->width<req.width || rect->height<req.height) {
406 found=FALSE;
408 } else {
409 rect=g_new(GdkRectangle, 1);
410 rect->width = req.width;
411 rect->height = req.height;
413 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
414 rect->y, found);*/
415 find_free_rect(current_pinboard, rect, found);
416 /*printf("%s at %d,%d %d\n", name? name: "(nil)", rect->x,
417 rect->y, found);*/
419 gtk_fixed_move(GTK_FIXED(current_pinboard->fixed),
420 widget, rect->x, rect->y);
422 /* Store the new position (key and value are never freed) */
423 if(name)
424 g_hash_table_insert(placed_icons, g_strdup(name),
425 rect);
428 void pinboard_moved_widget(GtkWidget *widget, const gchar *name,
429 int x, int y)
431 GdkRectangle *rect;
433 if(!name)
434 return;
435 rect=g_hash_table_lookup(placed_icons, name);
436 if(!rect)
437 return;
439 rect->x=x;
440 rect->y=y;
443 /* Add a new icon to the background.
444 * 'path' should be an absolute pathname.
445 * 'x' and 'y' are the coordinates of the point in the middle of the text.
446 * 'name' is the name to use. If NULL then the leafname of path is used.
448 static void pinboard_pin_with_args(const gchar *path, const gchar *name,
449 int x, int y, const gchar *shortcut,
450 const gchar *args)
452 GtkWidget *align, *vbox;
453 GdkWindow *events;
454 PinIcon *pi;
455 Icon *icon;
457 g_return_if_fail(path != NULL);
458 g_return_if_fail(current_pinboard != NULL);
460 pi = pin_icon_new(path, name);
461 icon = (Icon *) pi;
462 pi->x = x;
463 pi->y = y;
465 /* This is a bit complicated...
467 * An icon needs to be a NO_WINDOW widget so that the image can
468 * blend with the background (A ParentRelative window also works, but
469 * is slow, causes the xfree86's memory consumption to grow without
470 * bound, and doesn't even get freed when the filer quits!).
472 * However, the icon also needs to have a window, so we get events
473 * delivered correctly. The solution is to float an InputOnly window
474 * over the icon. Since GtkButton works the same way, we just use
475 * that :-)
478 /* Button takes the initial ref of Icon */
479 pi->win = gtk_button_new();
480 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
481 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
482 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
484 vbox = gtk_vbox_new(FALSE, 0);
485 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
487 align = gtk_alignment_new(0.5, 0.5, 0, 0);
488 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
489 gtk_container_add(GTK_CONTAINER(align), pi->widget);
491 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
492 drag_set_pinicon_dest(pi);
493 g_signal_connect(pi->win, "drag_data_get",
494 G_CALLBACK(drag_data_get), NULL);
496 pi->label = wrapped_label_new(icon->item->leafname, 180);
497 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
499 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
501 snap_to_grid(&x, &y);
502 pi->x = x;
503 pi->y = y;
504 gtk_widget_show_all(pi->win);
505 pinboard_reshape_icon((Icon *) pi);
507 gtk_widget_realize(pi->win);
508 events = GTK_BUTTON(pi->win)->event_window;
509 gdk_window_set_events(events,
510 GDK_EXPOSURE_MASK |
511 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
512 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
513 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
514 GDK_BUTTON3_MOTION_MASK);
515 g_signal_connect(pi->win, "enter-notify-event",
516 G_CALLBACK(enter_notify), pi);
517 g_signal_connect(pi->win, "leave-notify-event",
518 G_CALLBACK(leave_notify), pi);
519 g_signal_connect(pi->win, "button-press-event",
520 G_CALLBACK(button_press_event), pi);
521 g_signal_connect(pi->win, "button-release-event",
522 G_CALLBACK(button_release_event), pi);
523 g_signal_connect(pi->win, "motion-notify-event",
524 G_CALLBACK(icon_motion_notify), pi);
525 g_signal_connect(pi->win, "expose-event",
526 G_CALLBACK(draw_icon), pi);
527 g_signal_connect_swapped(pi->win, "style-set",
528 G_CALLBACK(pinboard_reshape_icon), pi);
529 g_signal_connect_swapped(pi->win, "destroy",
530 G_CALLBACK(pin_icon_destroyed), pi);
532 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
533 pin_icon_set_tip(pi);
535 icon_set_shortcut(icon, shortcut);
536 icon_set_arguments(icon, args);
538 if (!loading_pinboard)
539 pinboard_save();
542 void pinboard_pin(const gchar *path, const gchar *name, int x, int y,
543 const gchar *shortcut)
545 pinboard_pin_with_args(path, name, x, y, shortcut, NULL);
549 * Remove an icon from the background. The first icon with a matching
550 * path is removed. If name is not NULL then that also must match
552 gboolean pinboard_remove(const gchar *path, const gchar *name)
554 GList *item;
555 PinIcon *pi;
556 Icon *icon;
558 g_return_if_fail(current_pinboard != NULL);
560 for(item=current_pinboard->icons; item; item=g_list_next(item)) {
561 pi=(PinIcon *) item->data;
562 icon=(Icon *) pi;
564 if(strcmp(icon->path, path)!=0)
565 continue;
567 if(name && strcmp(name, icon->item->leafname)!=0)
568 continue;
570 icon_destroy(icon);
572 pinboard_save();
574 return TRUE;
577 return FALSE;
580 /* Put a border around the icon, briefly.
581 * If icon is NULL then cancel any existing wink.
582 * The icon will automatically unhighlight unless timeout is FALSE,
583 * in which case you must call this function again (with NULL or another
584 * icon) to remove the highlight.
586 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
588 PinIcon *old = current_wink_icon;
590 if (old == pi)
591 return;
593 current_wink_icon = pi;
595 if (old)
597 gtk_widget_queue_draw(old->win);
598 gdk_window_process_updates(old->widget->window, TRUE);
600 if (wink_timeout != -1)
601 g_source_remove(wink_timeout);
604 if (pi)
606 gtk_widget_queue_draw(pi->win);
607 gdk_window_process_updates(pi->widget->window, TRUE);
609 if (timeout)
610 wink_timeout = g_timeout_add(300, end_wink, NULL);
611 else
612 wink_timeout = -1;
616 /* 'app' is saved as the new application to set the backdrop. It will then be
617 * run, and should communicate with the filer as described in the manual.
619 void pinboard_set_backdrop_app(const gchar *app)
621 XMLwrapper *ai;
622 DirItem *item;
623 gboolean can_set;
625 item = diritem_new("");
626 diritem_restat(app, item, NULL);
627 if (!(item->flags & ITEM_FLAG_APPDIR))
629 delayed_error(_("The backdrop handler must be an application "
630 "directory. Drag an application directory "
631 "into the Set Backdrop dialog box, or (for "
632 "programmers) pass it to the SOAP "
633 "SetBackdropApp method."));
634 diritem_free(item);
635 return;
638 ai = appinfo_get(app, item);
639 diritem_free(item);
641 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
642 if (ai)
643 g_object_unref(ai);
645 if (can_set)
646 pinboard_set_backdrop(app, BACKDROP_PROGRAM);
647 else
648 delayed_error(_("You can only set the backdrop to an image "
649 "or to a program which knows how to "
650 "manage ROX-Filer's backdrop.\n\n"
651 "Programmers: the application's AppInfo.xml "
652 "must contain the CanSetBackdrop element, as "
653 "described in ROX-Filer's manual."));
656 /* Open a dialog box allowing the user to set the backdrop */
657 void pinboard_set_backdrop_box(void)
659 GtkWidget *dialog, *frame, *label, *hbox;
660 GtkBox *vbox;
661 Radios *radios;
663 g_return_if_fail(current_pinboard != NULL);
665 if (set_backdrop_dialog)
666 gtk_widget_destroy(set_backdrop_dialog);
668 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
669 GTK_DIALOG_NO_SEPARATOR,
670 GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
671 NULL);
672 set_backdrop_dialog = dialog;
673 g_signal_connect(dialog, "destroy",
674 G_CALLBACK(gtk_widget_destroyed), &set_backdrop_dialog);
675 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
677 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
679 label = gtk_label_new(_("Choose a style and drag an image in:"));
680 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
681 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
682 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
684 /* The Centred, Scaled, Tiled radios... */
685 hbox = gtk_hbox_new(TRUE, 2);
686 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
688 radios = radios_new(radios_changed, dialog);
689 g_object_set_data(G_OBJECT(dialog), "rox-radios", radios);
690 g_object_set_data(G_OBJECT(dialog), "rox-radios-hbox", hbox);
692 radios_add(radios, _("Centre the image without scaling it"),
693 BACKDROP_CENTRE, _("Centre"));
694 radios_add(radios, _("Scale the image to fit the backdrop area, "
695 "without distorting it"),
696 BACKDROP_SCALE, _("Scale"));
697 radios_add(radios, _("Stretch the image to fill the backdrop area"),
698 BACKDROP_STRETCH, _("Stretch"));
699 radios_add(radios, _("Tile the image over the backdrop area"),
700 BACKDROP_TILE, _("Tile"));
702 update_radios(dialog);
704 /* The drop area... */
705 frame = drop_box_new(_("Drop an image here"));
706 g_object_set_data(G_OBJECT(dialog), "rox-dropbox", frame);
708 radios_pack(radios, GTK_BOX(hbox));
709 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
711 drop_box_set_path(DROP_BOX(frame), current_pinboard->backdrop);
713 g_signal_connect(frame, "path_dropped",
714 G_CALLBACK(drag_backdrop_dropped), dialog);
715 g_signal_connect(frame, "clear",
716 G_CALLBACK(clear_backdrop), dialog);
718 g_signal_connect(dialog, "response",
719 G_CALLBACK(backdrop_response), NULL);
720 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
722 gtk_widget_show_all(dialog);
725 /* Also used by tasklist.c */
726 void draw_label_shadow(WrappedLabel *wl, GdkRegion *region)
728 GtkWidget *widget;
729 gint x, y;
731 if (!o_pinboard_shadow_labels.int_value)
732 return;
734 gdk_gc_set_clip_region(current_pinboard->shadow_gc, region);
736 widget = GTK_WIDGET(wl);
738 y = widget->allocation.y - wl->y_off;
739 x = widget->allocation.x - wl->x_off -
740 ((wl->text_width - widget->allocation.width) >> 1);
742 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
743 x + 1, y + 1, wl->layout);
745 if (o_pinboard_shadow_labels.int_value > 1)
746 gdk_draw_layout(widget->window, current_pinboard->shadow_gc,
747 x + 2, y + 2, wl->layout);
749 gdk_gc_set_clip_region(current_pinboard->shadow_gc, NULL);
752 /* Set and save (path, style) as the new backdrop.
753 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
754 * Otherwise, the image is displayed now.
756 void pinboard_set_backdrop(const gchar *path, BackdropStyle style)
758 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
759 (path != NULL && style != BACKDROP_NONE));
761 if (!current_pinboard)
763 if (!path)
764 return;
765 pinboard_activate("Default");
766 delayed_error(_("No pinboard was in use... "
767 "the 'Default' pinboard has been selected. "
768 "Use 'rox -p=Default' to turn it on in "
769 "future."));
770 g_return_if_fail(current_pinboard != NULL);
773 /* We might have just run the old backdrop program and now
774 * we're going to set a new one! Seems a bit mean...
777 abandon_backdrop_app(current_pinboard);
779 g_free(current_pinboard->backdrop);
780 current_pinboard->backdrop = g_strdup(path);
781 current_pinboard->backdrop_style = style;
782 reload_backdrop(current_pinboard,
783 current_pinboard->backdrop,
784 current_pinboard->backdrop_style);
786 pinboard_save();
788 if (set_backdrop_dialog)
790 DropBox *box = g_object_get_data(G_OBJECT(set_backdrop_dialog),
791 "rox-dropbox");
792 g_return_if_fail(box != NULL);
793 drop_box_set_path(box, current_pinboard->backdrop);
794 update_radios(set_backdrop_dialog);
798 /* Called on xrandr screen resizes */
799 void pinboard_update_size(void)
801 int width, height;
803 gtk_window_get_size(GTK_WINDOW(current_pinboard->window),
804 &width, &height);
806 /* Only update the pinboard's size if the screen gets bigger,
807 * not smaller. Not sure what to do about icons that end up
808 * offscreen if the screen shrinks, but perhaps a good policy
809 * is to leave them there until the screen size is increased
810 * again rather than mess them around. */
811 width = MAX(width, screen_width);
812 height = MAX(height, screen_height);
814 gtk_widget_set_size_request(current_pinboard->window, width, height);
817 /****************************************************************
818 * INTERNAL FUNCTIONS *
819 ****************************************************************/
821 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
823 gtk_widget_destroy(dialog);
826 static void clear_backdrop(GtkWidget *drop_box, gpointer data)
828 pinboard_set_backdrop(NULL, BACKDROP_NONE);
831 static void drag_backdrop_dropped(GtkWidget *drop_box,
832 const guchar *path,
833 GtkWidget *dialog)
835 struct stat info;
836 Radios *radios;
838 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
839 g_return_if_fail(radios != NULL);
841 if (mc_stat(path, &info))
843 delayed_error(
844 _("Can't access '%s':\n%s"), path,
845 g_strerror(errno));
846 return;
849 if (S_ISDIR(info.st_mode))
851 /* Use this program to set the backdrop */
852 pinboard_set_backdrop_app(path);
854 else if (S_ISREG(info.st_mode))
855 pinboard_set_backdrop(path, radios_get_value(radios));
856 else
857 delayed_error(_("Only files (and certain applications) can be "
858 "used to set the background image."));
861 /* Do this in the idle loop so that we don't try to put an unmanaged
862 * pinboard behind a managed panel (crashes some WMs).
864 static gboolean recreate_pinboard(gchar *name)
866 pinboard_activate(name);
867 g_free(name);
869 return FALSE;
872 static void pinboard_check_options(void)
874 GdkColor n_fg, n_bg, n_shadow;
876 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
877 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
878 gdk_color_parse(o_pinboard_shadow_colour.value, &n_shadow);
880 if (o_override_redirect.has_changed && current_pinboard)
882 gchar *name;
883 name = g_strdup(current_pinboard->name);
884 pinboard_activate(NULL);
885 g_idle_add((GtkFunction) recreate_pinboard, name);
888 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
890 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
891 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0 ||
892 gdk_color_equal(&n_shadow, &pin_text_shadow_col) == 0 ||
893 o_pinboard_shadow_labels.has_changed ||
894 o_label_font.has_changed)
896 pin_text_fg_col = n_fg;
897 pin_text_bg_col = n_bg;
898 pin_text_shadow_col = n_shadow;
899 update_pinboard_font();
901 if (current_pinboard)
903 GtkWidget *w = current_pinboard->window;
904 GdkColormap *cm;
906 cm = gtk_widget_get_colormap(w);
908 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
909 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
911 gdk_gc_set_rgb_fg_color(current_pinboard->shadow_gc,
912 &n_shadow);
914 abandon_backdrop_app(current_pinboard);
915 reload_backdrop(current_pinboard,
916 current_pinboard->backdrop,
917 current_pinboard->backdrop_style);
919 reshape_all();
922 tasklist_style_changed();
926 static gint end_wink(gpointer data)
928 pinboard_wink_item(NULL, FALSE);
929 return FALSE;
932 /* Sets the appearance from the options and updates the size request of
933 * the image.
935 static void set_size_and_style(PinIcon *pi)
937 Icon *icon = (Icon *) pi;
938 MaskedPixmap *image = di_image(icon->item);
939 int iwidth = image->width;
940 int iheight = image->height;
942 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
943 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
944 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
945 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
946 widget_modify_font(pi->label, pinboard_font);
948 wrapped_label_set_text(WRAPPED_LABEL(pi->label), icon->item->leafname);
950 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
953 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
955 static GtkWidgetClass *parent_class = NULL;
956 Icon *icon = (Icon *) pi;
957 DirItem *item = icon->item;
958 MaskedPixmap *image = di_image(item);
959 int iwidth = image->width;
960 int iheight = image->height;
961 int x, y;
963 if (!parent_class)
965 gpointer c = ((GTypeInstance *) widget)->g_class;
966 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
969 x = pi->widget->allocation.x;
970 y = pi->widget->allocation.y;
972 gdk_gc_set_clip_region(pi->widget->style->black_gc, event->region);
974 render_pixbuf(icon->selected ? image->pixbuf_lit : image->pixbuf,
975 pi->widget->window,
976 pi->widget->style->black_gc,
977 x, y, iwidth, iheight);
979 if (item->flags & ITEM_FLAG_SYMLINK)
981 render_pixbuf(im_symlink->pixbuf, pi->widget->window,
982 pi->widget->style->black_gc,
983 x, y, -1, -1);
985 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
987 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
988 ? im_mounted
989 : im_unmounted;
991 render_pixbuf(mp->pixbuf, pi->widget->window,
992 pi->widget->style->black_gc,
993 x, y, -1, -1);
996 gdk_gc_set_clip_region(pi->widget->style->black_gc, NULL);
998 if (icon->selected)
1000 GtkStyle *style = pi->label->style;
1001 GdkGC *gc = style->bg_gc[GTK_STATE_SELECTED];
1003 gdk_gc_set_clip_region(gc, event->region);
1004 gdk_draw_rectangle(pi->label->window, gc, TRUE,
1005 pi->label->allocation.x,
1006 pi->label->allocation.y,
1007 pi->label->allocation.width,
1008 pi->label->allocation.height);
1009 gdk_gc_set_clip_region(gc, NULL);
1011 else if (o_pinboard_shadow_labels.int_value)
1012 draw_label_shadow((WrappedLabel *) pi->label, event->region);
1014 /* Draw children */
1015 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL],
1016 event->region);
1017 (parent_class->expose_event)(widget, event);
1018 gdk_gc_set_clip_region(pi->label->style->fg_gc[GTK_STATE_NORMAL], NULL);
1020 /* Stop the button effect */
1021 return TRUE;
1024 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
1026 gint x, y, width, height;
1028 if (current_wink_icon != pi)
1029 return FALSE;
1031 x = widget->allocation.x;
1032 y = widget->allocation.y;
1033 width = widget->allocation.width;
1034 height = widget->allocation.height;
1036 gdk_draw_rectangle(widget->window,
1037 pi->widget->style->white_gc,
1038 FALSE,
1039 x, y, width - 1, height - 1);
1040 gdk_draw_rectangle(widget->window,
1041 pi->widget->style->black_gc,
1042 FALSE,
1043 x + 1, y + 1, width - 3, height - 3);
1045 return FALSE;
1048 static gboolean enter_notify(GtkWidget *widget,
1049 GdkEventCrossing *event,
1050 PinIcon *pi)
1052 icon_may_update((Icon *) pi);
1053 pin_icon_set_tip(pi);
1054 return TRUE;
1057 static gint leave_notify(GtkWidget *widget,
1058 GdkEventCrossing *event,
1059 PinIcon *pi)
1061 return TRUE;
1064 static void select_lasso(void)
1066 GList *next;
1067 int minx, miny, maxx, maxy;
1069 g_return_if_fail(lasso_in_progress == TRUE);
1071 minx = MIN(lasso_rect_x1, lasso_rect_x2);
1072 miny = MIN(lasso_rect_y1, lasso_rect_y2);
1073 maxx = MAX(lasso_rect_x1, lasso_rect_x2);
1074 maxy = MAX(lasso_rect_y1, lasso_rect_y2);
1076 for (next = current_pinboard->icons; next; next = next->next)
1078 PinIcon *pi = (PinIcon *) next->data;
1079 GtkAllocation *alloc = &pi->win->allocation;
1080 int cx = alloc->x + alloc->width / 2;
1081 int cy = alloc->y + alloc->height / 2;
1083 if (cx > minx && cx < maxx && cy > miny && cy < maxy)
1084 icon_set_selected((Icon *) pi, TRUE);
1088 static void cancel_lasso(void)
1090 draw_lasso();
1091 lasso_in_progress = FALSE;
1094 static void pinboard_lasso_box(int start_x, int start_y)
1096 if (lasso_in_progress)
1097 cancel_lasso();
1098 lasso_in_progress = TRUE;
1099 lasso_rect_x1 = lasso_rect_x2 = start_x;
1100 lasso_rect_y1 = lasso_rect_y2 = start_y;
1102 draw_lasso();
1105 static gint lasso_motion(GtkWidget *widget, GdkEventMotion *event, gpointer d)
1107 if (!lasso_in_progress)
1108 return FALSE;
1110 if (lasso_rect_x2 != event->x || lasso_rect_y2 != event->y)
1112 draw_lasso();
1113 lasso_rect_x2 = event->x;
1114 lasso_rect_y2 = event->y;
1115 draw_lasso();
1118 return FALSE;
1121 /* Mark the area of the screen covered by the lasso box for redraw */
1122 static void draw_lasso(void)
1124 GdkRectangle area, edge;
1126 if (!lasso_in_progress)
1127 return;
1129 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1130 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1131 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1132 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1134 edge.x = area.x;
1135 edge.y = area.y;
1136 edge.width = area.width;
1138 edge.height = 2; /* Top */
1139 gdk_window_invalidate_rect(current_pinboard->window->window,
1140 &edge, TRUE);
1142 edge.y += area.height - 2; /* Bottom */
1143 gdk_window_invalidate_rect(current_pinboard->window->window,
1144 &edge, TRUE);
1146 edge.y = area.y;
1147 edge.height = area.height;
1148 edge.width = 2; /* Left */
1149 gdk_window_invalidate_rect(current_pinboard->window->window,
1150 &edge, TRUE);
1152 edge.x += area.width - 2; /* Right */
1153 gdk_window_invalidate_rect(current_pinboard->window->window,
1154 &edge, TRUE);
1157 static void perform_action(PinIcon *pi, GdkEventButton *event)
1159 BindAction action;
1160 Icon *icon = (Icon *) pi;
1162 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
1163 event);
1165 /* Actions that can happen with or without an icon */
1166 switch (action)
1168 case ACT_LASSO_CLEAR:
1169 icon_select_only(NULL);
1170 /* (no break) */
1171 case ACT_LASSO_MODIFY:
1172 pinboard_lasso_box(event->x, event->y);
1173 return;
1174 case ACT_CLEAR_SELECTION:
1175 icon_select_only(NULL);
1176 return;
1177 case ACT_POPUP_MENU:
1178 dnd_motion_ungrab();
1179 pinboard_show_menu(event, pi);
1180 return;
1181 case ACT_IGNORE:
1182 return;
1183 default:
1184 break;
1187 g_return_if_fail(pi != NULL);
1189 switch (action)
1191 case ACT_OPEN_ITEM:
1192 dnd_motion_ungrab();
1193 pinboard_wink_item(pi, TRUE);
1194 if (event->type == GDK_2BUTTON_PRESS)
1195 icon_set_selected(icon, FALSE);
1196 icon_run(icon);
1197 break;
1198 case ACT_EDIT_ITEM:
1199 dnd_motion_ungrab();
1200 pinboard_wink_item(pi, TRUE);
1201 if (event->type == GDK_2BUTTON_PRESS)
1202 icon_set_selected(icon, FALSE);
1203 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1204 break;
1205 case ACT_PRIME_AND_SELECT:
1206 if (!icon->selected)
1207 icon_select_only(icon);
1208 dnd_motion_start(MOTION_READY_FOR_DND);
1209 break;
1210 case ACT_PRIME_AND_TOGGLE:
1211 icon_set_selected(icon, !icon->selected);
1212 dnd_motion_start(MOTION_READY_FOR_DND);
1213 break;
1214 case ACT_PRIME_FOR_DND:
1215 dnd_motion_start(MOTION_READY_FOR_DND);
1216 break;
1217 case ACT_TOGGLE_SELECTED:
1218 icon_set_selected(icon, !icon->selected);
1219 break;
1220 case ACT_SELECT_EXCL:
1221 icon_select_only(icon);
1222 break;
1223 default:
1224 g_warning("Unsupported action : %d\n", action);
1225 break;
1229 static void forward_to_root(GdkEventButton *event)
1231 XButtonEvent xev;
1233 if (event->type == GDK_BUTTON_PRESS)
1235 xev.type = ButtonPress;
1236 if (!o_blackbox_hack.int_value)
1237 XUngrabPointer(gdk_display, event->time);
1239 else
1240 xev.type = ButtonRelease;
1242 xev.window = gdk_x11_get_default_root_xwindow();
1243 xev.root = xev.window;
1244 xev.subwindow = None;
1245 xev.time = event->time;
1246 xev.x = event->x_root; /* Needed for icewm */
1247 xev.y = event->y_root;
1248 xev.x_root = event->x_root;
1249 xev.y_root = event->y_root;
1250 xev.state = event->state;
1251 xev.button = event->button;
1252 xev.same_screen = True;
1254 XSendEvent(gdk_display, xev.window, False,
1255 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
1258 #define FORWARDED_BUTTON(pi, b) ((b) == 2 || \
1259 (((b) == 3 || (b) == 1) && o_forward_buttons_13.int_value && !pi))
1261 /* pi is NULL if this is a root event */
1262 static gboolean button_release_event(GtkWidget *widget,
1263 GdkEventButton *event,
1264 PinIcon *pi)
1266 if (FORWARDED_BUTTON(pi, event->button))
1267 forward_to_root(event);
1268 else if (dnd_motion_release(event))
1270 if (motion_buttons_pressed == 0 && lasso_in_progress)
1272 select_lasso();
1273 cancel_lasso();
1275 return FALSE;
1278 perform_action(pi, event);
1280 return TRUE;
1283 /* pi is NULL if this is a root event */
1284 static gboolean button_press_event(GtkWidget *widget,
1285 GdkEventButton *event,
1286 PinIcon *pi)
1288 /* Just in case we've jumped in front of everything... */
1289 gdk_window_lower(current_pinboard->window->window);
1291 if (FORWARDED_BUTTON(pi, event->button))
1292 forward_to_root(event);
1293 else if (dnd_motion_press(widget, event))
1294 perform_action(pi, event);
1296 return TRUE;
1299 /* Forward mouse scroll events as buttons 4 and 5 to the window manager
1300 * (for old window managers that don't catch the buttons themselves)
1302 static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event)
1304 XButtonEvent xev;
1306 xev.type = ButtonPress;
1307 xev.window = gdk_x11_get_default_root_xwindow();
1308 xev.root = xev.window;
1309 xev.subwindow = None;
1310 xev.time = event->time;
1311 xev.x = event->x_root; /* Needed for icewm */
1312 xev.y = event->y_root;
1313 xev.x_root = event->x_root;
1314 xev.y_root = event->y_root;
1315 xev.state = event->state;
1316 xev.same_screen = True;
1318 if (event->direction == GDK_SCROLL_UP)
1319 xev.button = 4;
1320 else if (event->direction == GDK_SCROLL_DOWN)
1321 xev.button = 5;
1322 else
1323 return FALSE;
1325 XSendEvent(gdk_display, xev.window, False,
1326 ButtonPressMask, (XEvent *) &xev);
1328 return TRUE;
1331 static void start_drag(PinIcon *pi, GdkEventMotion *event)
1333 GtkWidget *widget = pi->win;
1334 Icon *icon = (Icon *) pi;
1336 if (!icon->selected)
1338 tmp_icon_selected = TRUE;
1339 icon_select_only(icon);
1342 g_return_if_fail(icon_selection != NULL);
1344 pinboard_drag_in_progress = icon;
1346 if (icon_selection->next == NULL)
1347 drag_one_item(widget, event, icon->path, icon->item, NULL);
1348 else
1350 guchar *uri_list;
1352 uri_list = icon_create_uri_list();
1353 drag_selection(widget, event, uri_list);
1354 g_free(uri_list);
1358 /* An icon is being dragged around... */
1359 static gint icon_motion_notify(GtkWidget *widget,
1360 GdkEventMotion *event,
1361 PinIcon *pi)
1363 if (motion_state == MOTION_READY_FOR_DND)
1365 if (dnd_motion_moved(event))
1366 start_drag(pi, event);
1367 return TRUE;
1370 return FALSE;
1373 static void backdrop_from_xml(xmlNode *node)
1375 gchar *style;
1377 g_free(current_pinboard->backdrop);
1378 current_pinboard->backdrop = xmlNodeGetContent(node);
1380 style = xmlGetProp(node, "style");
1382 if (style)
1384 current_pinboard->backdrop_style =
1385 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1386 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1387 g_strcasecmp(style, "Stretched") == 0 ? BACKDROP_STRETCH :
1388 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1389 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1390 BACKDROP_NONE;
1391 g_free(style);
1393 else
1394 current_pinboard->backdrop_style = BACKDROP_TILE;
1397 /* Create one pinboard icon for each icon in the doc */
1398 static void pinboard_load_from_xml(xmlDocPtr doc)
1400 xmlNodePtr node, root;
1401 char *tmp, *label, *path, *shortcut, *args;
1402 int x, y;
1404 root = xmlDocGetRootElement(doc);
1406 for (node = root->xmlChildrenNode; node; node = node->next)
1408 if (node->type != XML_ELEMENT_NODE)
1409 continue;
1410 if (strcmp(node->name, "backdrop") == 0)
1412 backdrop_from_xml(node);
1413 continue;
1415 if (strcmp(node->name, "icon") != 0)
1416 continue;
1418 tmp = xmlGetProp(node, "x");
1419 if (!tmp)
1420 continue;
1421 x = atoi(tmp);
1422 g_free(tmp);
1424 tmp = xmlGetProp(node, "y");
1425 if (!tmp)
1426 continue;
1427 y = atoi(tmp);
1428 g_free(tmp);
1430 label = xmlGetProp(node, "label");
1431 if (!label)
1432 label = g_strdup("<missing label>");
1433 path = xmlNodeGetContent(node);
1434 if (!path)
1435 path = g_strdup("<missing path>");
1436 shortcut = xmlGetProp(node, "shortcut");
1437 args = xmlGetProp(node, "args");
1439 pinboard_pin_with_args(path, label, x, y, shortcut, args);
1441 g_free(path);
1442 g_free(label);
1443 g_free(shortcut);
1444 g_free(args);
1448 /* Called for each line in the pinboard file while loading a new board.
1449 * Only used for old-format files when converting to XML.
1451 static const char *pin_from_file(gchar *line)
1453 gchar *leaf = NULL;
1454 int x, y, n;
1456 if (*line == '<')
1458 gchar *end;
1460 end = strchr(line + 1, '>');
1461 if (!end)
1462 return _("Missing '>' in icon label");
1464 leaf = g_strndup(line + 1, end - line - 1);
1466 line = end + 1;
1468 while (g_ascii_isspace(*line))
1469 line++;
1470 if (*line != ',')
1471 return _("Missing ',' after icon label");
1472 line++;
1475 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1476 return NULL; /* Ignore format errors */
1478 pinboard_pin(line + n, leaf, x, y, NULL);
1480 g_free(leaf);
1482 return NULL;
1485 /* Write the current state of the pinboard to the current pinboard file */
1486 static void pinboard_save(void)
1488 guchar *save = NULL;
1489 guchar *save_new = NULL;
1490 GList *next;
1491 xmlDocPtr doc = NULL;
1492 xmlNodePtr root;
1494 g_return_if_fail(current_pinboard != NULL);
1496 if (strchr(current_pinboard->name, '/'))
1497 save = g_strdup(current_pinboard->name);
1498 else
1500 guchar *leaf;
1502 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1503 save = choices_find_path_save(leaf, PROJECT, TRUE);
1504 g_free(leaf);
1507 if (!save)
1508 return;
1510 doc = xmlNewDoc("1.0");
1511 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1513 root = xmlDocGetRootElement(doc);
1515 if (current_pinboard->backdrop)
1517 BackdropStyle style = current_pinboard->backdrop_style;
1518 xmlNodePtr tree;
1520 tree = xmlNewTextChild(root, NULL, "backdrop",
1521 current_pinboard->backdrop);
1522 xmlSetProp(tree, "style",
1523 style == BACKDROP_TILE ? "Tiled" :
1524 style == BACKDROP_CENTRE ? "Centred" :
1525 style == BACKDROP_SCALE ? "Scaled" :
1526 style == BACKDROP_STRETCH ? "Stretched" :
1527 "Program");
1530 for (next = current_pinboard->icons; next; next = next->next)
1532 xmlNodePtr tree;
1533 PinIcon *pi = (PinIcon *) next->data;
1534 Icon *icon = (Icon *) pi;
1535 char *tmp;
1537 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1539 tmp = g_strdup_printf("%d", pi->x);
1540 xmlSetProp(tree, "x", tmp);
1541 g_free(tmp);
1543 tmp = g_strdup_printf("%d", pi->y);
1544 xmlSetProp(tree, "y", tmp);
1545 g_free(tmp);
1547 xmlSetProp(tree, "label", icon->item->leafname);
1548 if (icon->shortcut)
1549 xmlSetProp(tree, "shortcut", icon->shortcut);
1550 if (icon->args)
1551 xmlSetProp(tree, "args", icon->args);
1554 save_new = g_strconcat(save, ".new", NULL);
1555 if (save_xml_file(doc, save_new) || rename(save_new, save))
1556 delayed_error(_("Error saving pinboard %s: %s"),
1557 save, g_strerror(errno));
1558 g_free(save_new);
1560 g_free(save);
1561 if (doc)
1562 xmlFreeDoc(doc);
1565 static void snap_to_grid(int *x, int *y)
1567 int step = o_pinboard_grid_step.int_value;
1569 *x = ((*x + step / 2) / step) * step;
1570 *y = ((*y + step / 2) / step) * step;
1573 /* Convert (x,y) from a centre point to a window position */
1574 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1576 gboolean clamp = o_pinboard_clamp_icons.int_value;
1577 GtkRequisition req;
1579 gtk_widget_size_request(pi->win, &req);
1581 *x -= req.width >> 1;
1582 *y -= req.height >> 1;
1583 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1584 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1587 /* Same as drag_set_dest(), but for pinboard icons */
1588 static void drag_set_pinicon_dest(PinIcon *pi)
1590 GtkObject *obj = GTK_OBJECT(pi->win);
1592 make_drop_target(pi->win, 0);
1594 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1595 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1596 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1599 /* Called during the drag when the mouse is in a widget registered
1600 * as a drop target. Returns TRUE if we can accept the drop.
1602 static gboolean drag_motion(GtkWidget *widget,
1603 GdkDragContext *context,
1604 gint x,
1605 gint y,
1606 guint time,
1607 PinIcon *pi)
1609 GdkDragAction action = context->suggested_action;
1610 const char *type = NULL;
1611 Icon *icon = (Icon *) pi;
1612 DirItem *item = icon->item;
1614 if (gtk_drag_get_source_widget(context) == widget)
1616 g_dataset_set_data(context, "drop_dest_type",
1617 (gpointer) drop_dest_pass_through);
1618 return FALSE; /* Can't drag something to itself! */
1621 if (icon->selected)
1622 goto out; /* Can't drag a selection to itself */
1624 type = dnd_motion_item(context, &item);
1626 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1627 && type != drop_dest_prog)
1629 guint state;
1630 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1631 if (state & GDK_BUTTON1_MASK)
1632 action = GDK_ACTION_ASK;
1635 if (!item)
1636 type = NULL;
1637 out:
1638 /* We actually must pretend to accept the drop, even if the
1639 * directory isn't writeable, so that the spring-opening
1640 * thing works.
1643 /* Don't allow drops to non-writeable directories */
1644 if (o_dnd_spring_open.int_value == FALSE &&
1645 type == drop_dest_dir &&
1646 access(icon->path, W_OK) != 0)
1648 type = NULL;
1651 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1652 if (type)
1654 gdk_drag_status(context, action, time);
1655 g_dataset_set_data_full(context, "drop_dest_path",
1656 g_strdup(icon->path), g_free);
1657 if (type == drop_dest_dir)
1658 dnd_spring_load(context, NULL);
1660 pinboard_wink_item(pi, FALSE);
1662 else
1663 gdk_drag_status(context, 0, time);
1665 /* Always return TRUE to stop the pinboard getting the events */
1666 return TRUE;
1669 static gboolean pinboard_shadow = FALSE;
1670 static gint shadow_x, shadow_y;
1671 #define SHADOW_SIZE (ICON_WIDTH)
1673 static gboolean bg_expose(GtkWidget *widget,
1674 GdkEventExpose *event, gpointer data)
1676 GdkRectangle clipbox;
1677 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1678 gboolean double_buffer;
1680 gdk_gc_set_clip_region(widget->style->white_gc, event->region);
1681 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1683 gdk_region_get_clipbox(event->region, &clipbox);
1685 double_buffer = (clipbox.width * clipbox.height) < 20000;
1686 if (double_buffer)
1687 gdk_window_begin_paint_region(widget->window, event->region);
1689 /* Clear the area to the background image */
1691 GtkStyle *style = current_pinboard->window->style;
1692 GdkGC *gc = style->bg_gc[GTK_STATE_NORMAL];
1694 gdk_gc_set_clip_region(gc, event->region);
1695 if (style->bg_pixmap[GTK_STATE_NORMAL])
1697 gdk_gc_set_ts_origin(gc, 0, 0);
1698 gdk_gc_set_fill(gc, GDK_TILED);
1699 gdk_gc_set_tile(gc, style->bg_pixmap[GTK_STATE_NORMAL]);
1702 gdk_draw_rectangle(current_pinboard->window->window, gc, TRUE,
1703 clipbox.x, clipbox.y,
1704 clipbox.width, clipbox.height);
1705 if (style->bg_pixmap[GTK_STATE_NORMAL])
1706 gdk_gc_set_fill(gc, GDK_SOLID);
1708 gdk_gc_set_clip_region(gc, NULL);
1711 if (pinboard_shadow)
1713 gdk_draw_rectangle(widget->window,
1714 widget->style->white_gc, FALSE,
1715 shadow_x, shadow_y,
1716 SHADOW_SIZE, SHADOW_SIZE);
1717 gdk_draw_rectangle(widget->window,
1718 widget->style->black_gc, FALSE,
1719 shadow_x + 1, shadow_y + 1,
1720 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1723 if (lasso_in_progress)
1725 GdkRectangle area;
1727 area.x = MIN(lasso_rect_x1, lasso_rect_x2);
1728 area.y = MIN(lasso_rect_y1, lasso_rect_y2);
1729 area.width = ABS(lasso_rect_x1 - lasso_rect_x2);
1730 area.height = ABS(lasso_rect_y1 - lasso_rect_y2);
1732 if (area.width > 4 && area.height > 4)
1734 gdk_draw_rectangle(widget->window,
1735 widget->style->white_gc, FALSE,
1736 area.x, area.y,
1737 area.width - 1, area.height - 1);
1738 gdk_draw_rectangle(widget->window,
1739 widget->style->black_gc, FALSE,
1740 area.x + 1, area.y + 1,
1741 area.width - 3, area.height - 3);
1745 gdk_gc_set_clip_region(widget->style->white_gc, NULL);
1746 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1748 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1750 if (double_buffer)
1751 gdk_window_end_paint(widget->window);
1753 return TRUE;
1756 /* Draw a 'shadow' under an icon being dragged, showing where
1757 * it will land.
1759 static void pinboard_set_shadow(gboolean on)
1761 GdkRectangle area;
1763 if (pinboard_shadow)
1765 area.x = shadow_x;
1766 area.y = shadow_y;
1767 area.width = SHADOW_SIZE + 1;
1768 area.height = SHADOW_SIZE + 1;
1770 gdk_window_invalidate_rect(current_pinboard->window->window,
1771 &area, TRUE);
1774 if (on)
1776 int old_x = shadow_x, old_y = shadow_y;
1778 gdk_window_get_pointer(current_pinboard->fixed->window,
1779 &shadow_x, &shadow_y, NULL);
1780 snap_to_grid(&shadow_x, &shadow_y);
1781 shadow_x -= SHADOW_SIZE / 2;
1782 shadow_y -= SHADOW_SIZE / 2;
1785 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1786 return;
1788 area.x = shadow_x;
1789 area.y = shadow_y;
1790 area.width = SHADOW_SIZE + 1;
1791 area.height = SHADOW_SIZE + 1;
1793 gdk_window_invalidate_rect(current_pinboard->window->window,
1794 &area, TRUE);
1797 pinboard_shadow = on;
1800 /* Called when dragging some pinboard icons finishes */
1801 void pinboard_move_icons(void)
1803 int x = shadow_x, y = shadow_y;
1804 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1805 int width, height;
1806 int dx, dy;
1807 GList *next;
1809 g_return_if_fail(pi != NULL);
1811 x += SHADOW_SIZE / 2;
1812 y += SHADOW_SIZE / 2;
1813 snap_to_grid(&x, &y);
1815 if (pi->x == x && pi->y == y)
1816 return;
1818 /* Find out how much the dragged icon moved (after snapping).
1819 * Move all selected icons by the same amount.
1821 dx = x - pi->x;
1822 dy = y - pi->y;
1824 /* Move the other selected icons to keep the same relative
1825 * position.
1827 for (next = icon_selection; next; next = next->next)
1829 PinIcon *pi = (PinIcon *) next->data;
1830 int nx, ny;
1832 g_return_if_fail(IS_PIN_ICON(pi));
1834 pi->x += dx;
1835 pi->y += dy;
1836 nx = pi->x;
1837 ny = pi->y;
1839 gdk_drawable_get_size(pi->win->window, &width, &height);
1840 offset_from_centre(pi, &nx, &ny);
1842 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
1843 pi->win, nx, ny);
1846 pinboard_save();
1849 static void drag_leave(GtkWidget *widget,
1850 GdkDragContext *context,
1851 guint32 time,
1852 PinIcon *pi)
1854 pinboard_wink_item(NULL, FALSE);
1855 dnd_spring_abort();
1858 static gboolean bg_drag_leave(GtkWidget *widget,
1859 GdkDragContext *context,
1860 guint32 time,
1861 gpointer data)
1863 pinboard_set_shadow(FALSE);
1864 return TRUE;
1867 static gboolean bg_drag_motion(GtkWidget *widget,
1868 GdkDragContext *context,
1869 gint x,
1870 gint y,
1871 guint time,
1872 gpointer data)
1874 /* Dragging from the pinboard to the pinboard is not allowed */
1876 if (!provides(context, text_uri_list))
1877 return FALSE;
1879 pinboard_set_shadow(TRUE);
1881 gdk_drag_status(context,
1882 context->suggested_action == GDK_ACTION_ASK
1883 ? GDK_ACTION_LINK : context->suggested_action,
1884 time);
1885 return TRUE;
1888 static void drag_end(GtkWidget *widget,
1889 GdkDragContext *context,
1890 PinIcon *pi)
1892 pinboard_drag_in_progress = NULL;
1893 if (tmp_icon_selected)
1895 icon_select_only(NULL);
1896 tmp_icon_selected = FALSE;
1900 /* Something which affects all the icons has changed - reshape
1901 * and redraw all of them.
1903 static void reshape_all(void)
1905 GList *next;
1907 g_return_if_fail(current_pinboard != NULL);
1909 for (next = current_pinboard->icons; next; next = next->next)
1911 Icon *icon = (Icon *) next->data;
1912 pinboard_reshape_icon(icon);
1916 /* Turns off the pinboard. Does not call gtk_main_quit. */
1917 static void pinboard_clear(void)
1919 GList *next;
1921 g_return_if_fail(current_pinboard != NULL);
1923 tasklist_set_active(FALSE);
1925 next = current_pinboard->icons;
1926 while (next)
1928 PinIcon *pi = (PinIcon *) next->data;
1930 next = next->next;
1932 gtk_widget_destroy(pi->win);
1935 gtk_widget_destroy(current_pinboard->window);
1937 abandon_backdrop_app(current_pinboard);
1939 g_object_unref(current_pinboard->shadow_gc);
1940 current_pinboard->shadow_gc = NULL;
1942 g_free(current_pinboard->name);
1943 null_g_free(&current_pinboard);
1945 number_of_windows--;
1948 static gpointer parent_class;
1950 static void pin_icon_destroy(Icon *icon)
1952 PinIcon *pi = (PinIcon *) icon;
1954 g_return_if_fail(pi->win != NULL);
1956 gtk_widget_hide(pi->win); /* Stops flicker - stupid GtkFixed! */
1957 gtk_widget_destroy(pi->win);
1960 static void pinboard_remove_items(void)
1962 g_return_if_fail(icon_selection != NULL);
1964 while (icon_selection)
1965 icon_destroy((Icon *) icon_selection->data);
1967 pinboard_save();
1970 static void pin_icon_update(Icon *icon)
1972 pinboard_reshape_icon(icon);
1973 pinboard_save();
1976 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1978 return IS_PIN_ICON(other);
1981 static void pin_wink_icon(Icon *icon)
1983 pinboard_wink_item((PinIcon *) icon, TRUE);
1986 static void pin_icon_class_init(gpointer gclass, gpointer data)
1988 IconClass *icon = (IconClass *) gclass;
1990 parent_class = g_type_class_peek_parent(gclass);
1992 icon->destroy = pin_icon_destroy;
1993 icon->redraw = pinboard_reshape_icon;
1994 icon->update = pin_icon_update;
1995 icon->wink = pin_wink_icon;
1996 icon->remove_items = pinboard_remove_items;
1997 icon->same_group = pin_icon_same_group;
2000 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
2004 static GType pin_icon_get_type(void)
2006 static GType type = 0;
2008 if (!type)
2010 static const GTypeInfo info =
2012 sizeof (PinIconClass),
2013 NULL, /* base_init */
2014 NULL, /* base_finalise */
2015 pin_icon_class_init,
2016 NULL, /* class_finalise */
2017 NULL, /* class_data */
2018 sizeof(PinIcon),
2019 0, /* n_preallocs */
2020 pin_icon_init
2023 type = g_type_register_static(icon_get_type(),
2024 "PinIcon", &info, 0);
2027 return type;
2030 static PinIcon *pin_icon_new(const char *pathname, const char *name)
2032 PinIcon *pi;
2033 Icon *icon;
2035 pi = g_object_new(pin_icon_get_type(), NULL);
2036 icon = (Icon *) pi;
2038 icon_set_path(icon, pathname, name);
2040 return pi;
2043 /* Called when the window widget is somehow destroyed */
2044 static void pin_icon_destroyed(PinIcon *pi)
2046 g_return_if_fail(pi->win != NULL);
2048 pi->win = NULL;
2050 pinboard_wink_item(NULL, FALSE);
2052 if (pinboard_drag_in_progress == (Icon *) pi)
2053 pinboard_drag_in_progress = NULL;
2055 if (current_pinboard)
2056 current_pinboard->icons =
2057 g_list_remove(current_pinboard->icons, pi);
2059 g_object_unref(pi);
2062 /* Set the tooltip */
2063 static void pin_icon_set_tip(PinIcon *pi)
2065 XMLwrapper *ai;
2066 xmlNode *node;
2067 Icon *icon = (Icon *) pi;
2069 g_return_if_fail(pi != NULL);
2071 ai = appinfo_get(icon->path, icon->item);
2073 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
2075 guchar *str;
2076 str = xmlNodeListGetString(node->doc,
2077 node->xmlChildrenNode, 1);
2078 if (str)
2080 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
2081 g_free(str);
2084 else
2085 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
2087 if (ai)
2088 g_object_unref(ai);
2091 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
2093 int pos[3];
2094 GList *list;
2096 pos[0] = event->x_root;
2097 pos[1] = event->y_root;
2099 icon_prepare_menu((Icon *) pi, TRUE);
2101 list = gtk_container_get_children(GTK_CONTAINER(icon_menu));
2102 pos[2] = g_list_length(list) - 6;
2103 g_list_free(list);
2105 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2106 position_menu,
2107 (gpointer) pos, event->button, event->time);
2110 static void create_pinboard_window(Pinboard *pinboard)
2112 GtkWidget *win;
2114 g_return_if_fail(pinboard->window == NULL);
2116 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2117 gtk_widget_set_style(win, gtk_widget_get_default_style());
2119 gtk_widget_set_double_buffered(win, FALSE);
2120 gtk_widget_set_app_paintable(win, TRUE);
2121 gtk_widget_set_name(win, "rox-pinboard");
2122 pinboard->window = win;
2123 pinboard->fixed = gtk_fixed_new();
2124 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
2126 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
2128 gtk_widget_set_size_request(win, screen_width, screen_height);
2129 gtk_widget_realize(win);
2130 gtk_window_move(GTK_WINDOW(win), 0, 0);
2131 make_panel_window(win);
2133 /* TODO: Use gdk function when it supports this type */
2135 GdkAtom desktop_type;
2137 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
2138 FALSE);
2139 gdk_property_change(win->window,
2140 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
2141 gdk_atom_intern("ATOM", FALSE), 32,
2142 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
2145 gtk_widget_add_events(win,
2146 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2147 GDK_EXPOSURE_MASK |
2148 GDK_BUTTON1_MOTION_MASK |
2149 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
2150 g_signal_connect(win, "button-press-event",
2151 G_CALLBACK(button_press_event), NULL);
2152 g_signal_connect(win, "button-release-event",
2153 G_CALLBACK(button_release_event), NULL);
2154 g_signal_connect(win, "motion-notify-event",
2155 G_CALLBACK(lasso_motion), NULL);
2156 g_signal_connect(pinboard->fixed, "expose_event",
2157 G_CALLBACK(bg_expose), NULL);
2159 /* Some window managers use scroll events on the root to switch
2160 * desktops, but don't cope with our pinboard window, so we forward
2161 * them manually in that case.
2163 g_signal_connect(win, "scroll-event", G_CALLBACK(scroll_event), NULL);
2165 /* Drag and drop handlers */
2166 drag_set_pinboard_dest(win);
2167 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
2168 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
2170 pinboard->shadow_gc = gdk_gc_new(win->window);
2171 gdk_gc_set_rgb_fg_color(pinboard->shadow_gc, &pin_text_shadow_col);
2173 reload_backdrop(current_pinboard, NULL, BACKDROP_NONE);
2175 gtk_widget_show_all(win);
2176 gdk_window_lower(win->window);
2179 /* Load image 'path' and scale according to 'style' */
2180 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
2182 GdkPixmap *pixmap;
2183 GdkPixbuf *pixbuf;
2184 GError *error = NULL;
2186 pixbuf = gdk_pixbuf_new_from_file(path, &error);
2187 if (error)
2189 delayed_error(_("Error loading backdrop image:\n%s\n"
2190 "Backdrop removed."),
2191 error->message);
2192 g_error_free(error);
2193 pinboard_set_backdrop(NULL, BACKDROP_NONE);
2194 return NULL;
2197 if (style == BACKDROP_STRETCH)
2199 GdkPixbuf *old = pixbuf;
2201 pixbuf = gdk_pixbuf_scale_simple(old,
2202 screen_width, screen_height,
2203 GDK_INTERP_HYPER);
2205 g_object_unref(old);
2207 else if (style == BACKDROP_CENTRE || style == BACKDROP_SCALE)
2209 GdkPixbuf *old = pixbuf;
2210 int x, y, width, height;
2211 float scale;
2213 width = gdk_pixbuf_get_width(pixbuf);
2214 height = gdk_pixbuf_get_height(pixbuf);
2216 if (style == BACKDROP_SCALE)
2218 float scale_x, scale_y;
2219 scale_x = screen_width / ((float) width);
2220 scale_y = screen_height / ((float) height);
2221 scale = MIN(scale_x, scale_y);
2223 else
2224 scale = 1;
2226 pixbuf = gdk_pixbuf_new(
2227 gdk_pixbuf_get_colorspace(pixbuf), FALSE,
2228 8, screen_width, screen_height);
2229 gdk_pixbuf_fill(pixbuf, ((pin_text_bg_col.red & 0xff00) << 16) |
2230 ((pin_text_bg_col.green & 0xff00) << 8) |
2231 ((pin_text_bg_col.blue & 0xff00)));
2233 x = (screen_width - width * scale) / 2;
2234 y = (screen_height - height * scale) / 2;
2235 x = MAX(x, 0);
2236 y = MAX(y, 0);
2238 gdk_pixbuf_composite(old, pixbuf,
2239 x, y,
2240 MIN(screen_width, width * scale),
2241 MIN(screen_height, height * scale),
2242 x, y, scale, scale,
2243 GDK_INTERP_HYPER, 255);
2244 g_object_unref(old);
2247 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
2248 &pixmap, NULL, 0);
2249 g_object_unref(pixbuf);
2251 return pixmap;
2254 static void abandon_backdrop_app(Pinboard *pinboard)
2256 g_return_if_fail(pinboard != NULL);
2258 if (pinboard->to_backdrop_app != -1)
2260 close(pinboard->to_backdrop_app);
2261 close(pinboard->from_backdrop_app);
2262 g_source_remove(pinboard->input_tag);
2263 g_string_free(pinboard->input_buffer, TRUE);
2264 pinboard->to_backdrop_app = -1;
2265 pinboard->from_backdrop_app = -1;
2266 pinboard->input_tag = -1;
2267 pinboard->input_buffer = NULL;
2270 g_return_if_fail(pinboard->to_backdrop_app == -1);
2271 g_return_if_fail(pinboard->from_backdrop_app == -1);
2272 g_return_if_fail(pinboard->input_tag == -1);
2273 g_return_if_fail(pinboard->input_buffer == NULL);
2276 /* A single line has been read from the child.
2277 * Processes the command, and replies 'ok' (or abandons the child on error).
2279 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
2281 BackdropStyle style;
2282 const char *ok = "ok\n";
2284 if (strncmp(command, "tile ", 5) == 0)
2286 style = BACKDROP_TILE;
2287 command += 5;
2289 else if (strncmp(command, "scale ", 6) == 0)
2291 style = BACKDROP_SCALE;
2292 command += 6;
2294 else if (strncmp(command, "stretch ", 8) == 0)
2296 style = BACKDROP_STRETCH;
2297 command += 8;
2299 else if (strncmp(command, "centre ", 7) == 0)
2301 style = BACKDROP_CENTRE;
2302 command += 7;
2304 else
2306 g_warning("Invalid command '%s' from backdrop app\n",
2307 command);
2308 abandon_backdrop_app(pinboard);
2309 return;
2312 /* Load the backdrop. May abandon the program if loading fails. */
2313 reload_backdrop(pinboard, command, style);
2315 if (pinboard->to_backdrop_app == -1)
2316 return;
2318 while (*ok)
2320 int sent;
2322 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
2323 if (sent <= 0)
2325 /* Remote app quit? Not an error. */
2326 abandon_backdrop_app(pinboard);
2327 break;
2329 ok += sent;
2333 static void backdrop_from_child(Pinboard *pinboard,
2334 int src, GdkInputCondition cond)
2336 char buf[256];
2337 int got;
2339 got = read(src, buf, sizeof(buf));
2341 if (got <= 0)
2343 if (got < 0)
2344 g_warning("backdrop_from_child: %s\n",
2345 g_strerror(errno));
2346 abandon_backdrop_app(pinboard);
2347 return;
2350 g_string_append_len(pinboard->input_buffer, buf, got);
2352 while (pinboard->from_backdrop_app != -1)
2354 int len;
2355 char *nl, *command;
2357 nl = strchr(pinboard->input_buffer->str, '\n');
2358 if (!nl)
2359 return; /* Haven't got a whole line yet */
2361 len = nl - pinboard->input_buffer->str;
2362 command = g_strndup(pinboard->input_buffer->str, len);
2363 g_string_erase(pinboard->input_buffer, 0, len + 1);
2365 command_from_backdrop_app(pinboard, command);
2367 g_free(command);
2371 static void reload_backdrop(Pinboard *pinboard,
2372 const gchar *backdrop,
2373 BackdropStyle backdrop_style)
2375 GtkStyle *style;
2377 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
2379 const char *argv[] = {NULL, "--backdrop", NULL};
2380 GError *error = NULL;
2382 g_return_if_fail(pinboard->to_backdrop_app == -1);
2383 g_return_if_fail(pinboard->from_backdrop_app == -1);
2384 g_return_if_fail(pinboard->input_tag == -1);
2385 g_return_if_fail(pinboard->input_buffer == NULL);
2387 argv[0] = make_path(backdrop, "AppRun");
2389 /* Run the program. It'll send us a SOAP message and we'll
2390 * get back here with a different style and image.
2393 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
2394 G_SPAWN_DO_NOT_REAP_CHILD |
2395 G_SPAWN_SEARCH_PATH,
2396 NULL, NULL, /* Child setup fn */
2397 NULL, /* Child PID */
2398 &pinboard->to_backdrop_app,
2399 &pinboard->from_backdrop_app,
2400 NULL, /* Standard error */
2401 &error))
2403 pinboard->input_buffer = g_string_new(NULL);
2404 pinboard->input_tag = gdk_input_add_full(
2405 pinboard->from_backdrop_app,
2406 GDK_INPUT_READ,
2407 (GdkInputFunction) backdrop_from_child,
2408 pinboard, NULL);
2410 else
2412 delayed_error("%s", error ? error->message : "(null)");
2413 g_error_free(error);
2415 return;
2418 /* Note: Copying a style does not ref the pixmaps! */
2420 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
2421 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
2423 if (backdrop)
2424 style->bg_pixmap[GTK_STATE_NORMAL] =
2425 load_backdrop(backdrop, backdrop_style);
2427 gdk_color_parse(o_pinboard_bg_colour.value,
2428 &style->bg[GTK_STATE_NORMAL]);
2430 gtk_widget_set_style(pinboard->window, style);
2432 g_object_unref(style);
2434 gtk_widget_queue_draw(pinboard->window);
2436 /* Also update root window property (for transparent xterms, etc) */
2437 if (style->bg_pixmap[GTK_STATE_NORMAL])
2439 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
2440 gdk_property_change(gdk_get_default_root_window(),
2441 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
2442 gdk_atom_intern("PIXMAP", FALSE),
2443 32, GDK_PROP_MODE_REPLACE,
2444 (guchar *) &id, 1);
2446 else
2448 gdk_property_delete(gdk_get_default_root_window(),
2449 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
2453 #define SEARCH_STEP 32
2455 static void search_free(GdkRectangle *rect, GdkRegion *used,
2456 int *outer, int od, int omax,
2457 int *inner, int id, int imax)
2459 *outer = od > 0 ? 0 : omax;
2460 while (*outer >= 0 && *outer <= omax)
2462 *inner = id > 0 ? 0 : imax;
2463 while (*inner >= 0 && *inner <= imax)
2465 if (gdk_region_rect_in(used, rect) ==
2466 GDK_OVERLAP_RECTANGLE_OUT)
2467 return;
2468 *inner += id;
2471 *outer += od;
2474 rect->x = -1;
2475 rect->y = -1;
2478 /* Finds a free area on the pinboard large enough for the width and height
2479 * of the given rectangle, by filling in the x and y fields of 'rect'.
2480 * If 'old' is true, 'rect' has a previous position and we first check
2481 * if it is viable.
2482 * The search order respects user preferences.
2483 * If no area is free, returns any old area.
2485 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect,
2486 gboolean old)
2488 GdkRegion *used;
2489 GList *next;
2490 GdkRectangle used_rect;
2491 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2493 used = gdk_region_new();
2495 panel_mark_used(used);
2497 /* Subtract the no-go areas... */
2499 if (o_top_margin.int_value > 0)
2501 used_rect.x = 0;
2502 used_rect.y = 0;
2503 used_rect.width = gdk_screen_width();
2504 used_rect.height = o_top_margin.int_value;
2505 gdk_region_union_with_rect(used, &used_rect);
2508 if (o_bottom_margin.int_value > 0)
2510 used_rect.x = 0;
2511 used_rect.y = gdk_screen_height() - o_bottom_margin.int_value;
2512 used_rect.width = gdk_screen_width();
2513 used_rect.height = o_bottom_margin.int_value;
2514 gdk_region_union_with_rect(used, &used_rect);
2517 /* Subtract the used areas... */
2519 next = GTK_FIXED(pinboard->fixed)->children;
2520 for (; next; next = next->next)
2522 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2524 if (!GTK_WIDGET_VISIBLE(fix->widget))
2525 continue;
2527 used_rect.x = fix->x;
2528 used_rect.y = fix->y;
2529 used_rect.width = fix->widget->requisition.width;
2530 used_rect.height = fix->widget->requisition.height;
2532 gdk_region_union_with_rect(used, &used_rect);
2535 /* Check the previous area */
2536 if(old) {
2537 if(gdk_region_rect_in(used, rect)==GDK_OVERLAP_RECTANGLE_OUT) {
2538 gdk_region_destroy(used);
2539 return;
2543 /* Find the first free area (yes, this isn't exactly pretty, but
2544 * it works). If you know a better (fast!) algorithm, let me know!
2548 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2549 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2550 dx = -SEARCH_STEP;
2552 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2553 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2554 dy = -SEARCH_STEP;
2556 if (o_iconify_dir.int_value == DIR_VERT)
2558 search_free(rect, used,
2559 &rect->x, dx, screen_width - rect->width,
2560 &rect->y, dy, screen_height - rect->height);
2562 else
2564 search_free(rect, used,
2565 &rect->y, dy, screen_height - rect->height,
2566 &rect->x, dx, screen_width - rect->width);
2569 gdk_region_destroy(used);
2571 if (rect->x == -1)
2573 rect->x = 0;
2574 rect->y = 0;
2578 /* Icon's size, shape or appearance has changed - update the display */
2579 static void pinboard_reshape_icon(Icon *icon)
2581 PinIcon *pi = (PinIcon *) icon;
2582 int x = pi->x, y = pi->y;
2584 set_size_and_style(pi);
2585 offset_from_centre(pi, &x, &y);
2587 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2589 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2590 pi->win, x, y);
2594 /* Sets the pinboard_font global from the option. Doesn't do anything else. */
2595 static void update_pinboard_font(void)
2597 if (pinboard_font)
2598 pango_font_description_free(pinboard_font);
2599 pinboard_font = o_label_font.value[0] != '\0'
2600 ? pango_font_description_from_string(o_label_font.value)
2601 : NULL;
2604 static void radios_changed(gpointer data)
2606 GObject *dialog = G_OBJECT(data);
2607 DropBox *drop_box;
2608 const guchar *path;
2609 Radios *radios;
2611 g_return_if_fail(dialog != NULL);
2613 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2614 drop_box = g_object_get_data(G_OBJECT(dialog), "rox-dropbox");
2616 g_return_if_fail(radios != NULL);
2617 g_return_if_fail(drop_box != NULL);
2618 g_return_if_fail(current_pinboard != NULL);
2620 if (current_pinboard->backdrop_style != BACKDROP_PROGRAM)
2622 path = drop_box_get_path(drop_box);
2623 if (path)
2624 pinboard_set_backdrop(path, radios_get_value(radios));
2628 static void update_radios(GtkWidget *dialog)
2630 Radios *radios;
2631 GtkWidget *hbox;
2633 g_return_if_fail(dialog != NULL);
2635 radios = g_object_get_data(G_OBJECT(dialog), "rox-radios");
2636 hbox = g_object_get_data(G_OBJECT(dialog), "rox-radios-hbox");
2638 g_return_if_fail(current_pinboard != NULL);
2639 g_return_if_fail(radios != NULL);
2640 g_return_if_fail(hbox != NULL);
2642 switch (current_pinboard->backdrop_style)
2644 case BACKDROP_TILE:
2645 case BACKDROP_STRETCH:
2646 case BACKDROP_SCALE:
2647 case BACKDROP_CENTRE:
2648 radios_set_value(radios,
2649 current_pinboard->backdrop_style);
2650 gtk_widget_set_sensitive(hbox, TRUE);
2651 break;
2652 default:
2653 gtk_widget_set_sensitive(hbox, FALSE);
2654 radios_set_value(radios, BACKDROP_TILE);
2655 break;