r1601: Turned on more compiler warnings, and fixed some minor issues it threw up.
[rox-filer.git] / ROX-Filer / src / pinboard.c
blobd679a797785a5d09b14adf9fb4633b29c1318b9a
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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 <libxml/parser.h>
34 #include <signal.h>
36 #include "global.h"
38 #include "pinboard.h"
39 #include "main.h"
40 #include "dnd.h"
41 #include "pixmaps.h"
42 #include "type.h"
43 #include "choices.h"
44 #include "support.h"
45 #include "gui_support.h"
46 #include "options.h"
47 #include "diritem.h"
48 #include "bind.h"
49 #include "icon.h"
50 #include "run.h"
51 #include "appinfo.h"
52 #include "menu.h"
53 #include "xml.h"
54 #include "tasklist.h"
55 #include "panel.h" /* For panel_mark_used() */
57 static gboolean tmp_icon_selected = FALSE; /* When dragging */
59 struct _Pinboard {
60 guchar *name; /* Leaf name */
61 GList *icons;
62 GtkStyle *style;
64 gchar *backdrop; /* Pathname */
65 BackdropStyle backdrop_style;
66 gint to_backdrop_app; /* pipe FD, or -1 */
67 gint from_backdrop_app; /* pipe FD, or -1 */
68 gint input_tag;
69 GString *input_buffer;
71 GtkWidget *window; /* Screen-sized window */
72 GtkWidget *fixed;
75 #define IS_PIN_ICON(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), pin_icon_get_type())
77 typedef struct _PinIconClass PinIconClass;
78 typedef struct _PinIcon PinIcon;
80 struct _PinIconClass {
81 IconClass parent;
84 struct _PinIcon {
85 Icon icon;
87 int x, y;
88 GtkWidget *win;
89 GtkWidget *widget; /* The drawing area for the icon */
90 GtkWidget *label;
93 /* The number of pixels between the bottom of the image and the top
94 * of the text.
96 #define GAP 4
98 /* The size of the border around the icon which is used when winking */
99 #define WINK_FRAME 2
101 /* Grid sizes */
102 #define GRID_STEP_FINE 2
103 #define GRID_STEP_MED 16
104 #define GRID_STEP_COARSE 32
106 /* Used in options */
107 #define CORNER_TOP_LEFT 0
108 #define CORNER_TOP_RIGHT 1
109 #define CORNER_BOTTOM_LEFT 2
110 #define CORNER_BOTTOM_RIGHT 3
112 #define DIR_HORZ 0
113 #define DIR_VERT 1
115 static PinIcon *current_wink_icon = NULL;
116 static gint wink_timeout;
118 /* Used for the text colours (only) in the icons (and tasklist windows) */
119 GdkColor pin_text_fg_col, pin_text_bg_col;
121 /* Style that all the icons should use. NULL => regenerate from text_fg/bg */
122 static GtkStyle *pinicon_style = NULL;
124 Pinboard *current_pinboard = NULL;
125 static gint loading_pinboard = 0; /* Non-zero => loading */
127 /* The Icon that was used to start the current drag, if any */
128 Icon *pinboard_drag_in_progress = NULL;
130 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
131 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
132 static Option o_pinboard_tasklist, o_forward_button_3;
133 static Option o_iconify_start, o_iconify_dir;
135 /* Static prototypes */
136 static GType pin_icon_get_type(void);
137 static void set_size_and_style(PinIcon *pi);
138 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
139 static gint end_wink(gpointer data);
140 static gboolean button_release_event(GtkWidget *widget,
141 GdkEventButton *event,
142 PinIcon *pi);
143 static gboolean enter_notify(GtkWidget *widget,
144 GdkEventCrossing *event,
145 PinIcon *pi);
146 static gboolean button_press_event(GtkWidget *widget,
147 GdkEventButton *event,
148 PinIcon *pi);
149 static gint icon_motion_notify(GtkWidget *widget,
150 GdkEventMotion *event,
151 PinIcon *pi);
152 static const char *pin_from_file(gchar *line);
153 static void snap_to_grid(int *x, int *y);
154 static void offset_from_centre(PinIcon *pi, int *x, int *y);
155 static gboolean drag_motion(GtkWidget *widget,
156 GdkDragContext *context,
157 gint x,
158 gint y,
159 guint time,
160 PinIcon *pi);
161 static void drag_set_pinicon_dest(PinIcon *pi);
162 static void drag_leave(GtkWidget *widget,
163 GdkDragContext *context,
164 guint32 time,
165 PinIcon *pi);
166 static gboolean bg_drag_motion(GtkWidget *widget,
167 GdkDragContext *context,
168 gint x,
169 gint y,
170 guint time,
171 gpointer data);
172 static gboolean bg_drag_leave(GtkWidget *widget,
173 GdkDragContext *context,
174 guint32 time,
175 gpointer data);
176 static gboolean bg_expose(GtkWidget *window,
177 GdkEventExpose *event, gpointer data);
178 static void drag_end(GtkWidget *widget,
179 GdkDragContext *context,
180 PinIcon *pi);
181 static void reshape_all(void);
182 static void pinboard_check_options(void);
183 static void pinboard_load_from_xml(xmlDocPtr doc);
184 static void pinboard_clear(void);
185 static void pinboard_save(void);
186 static PinIcon *pin_icon_new(const char *pathname, const char *name);
187 static void pin_icon_destroyed(PinIcon *pi);
188 static void pin_icon_set_tip(PinIcon *pi);
189 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
190 static void create_pinboard_window(Pinboard *pinboard);
191 static void reload_backdrop(Pinboard *pinboard,
192 const gchar *backdrop,
193 BackdropStyle backdrop_style);
194 static void set_backdrop(const gchar *path, BackdropStyle style);
195 static void pinboard_reshape_icon(Icon *icon);
196 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
197 static void abandon_backdrop_app(Pinboard *pinboard);
198 static void drag_backdrop_dropped(GtkWidget *frame,
199 GdkDragContext *context,
200 gint x,
201 gint y,
202 GtkSelectionData *selection_data,
203 guint drag_info,
204 guint32 time,
205 GtkWidget *dialog);
206 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
207 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect);
210 /****************************************************************
211 * EXTERNAL INTERFACE *
212 ****************************************************************/
214 void pinboard_init(void)
216 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
217 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
219 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
220 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
221 GRID_STEP_COARSE);
222 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
223 option_add_int(&o_forward_button_3, "pinboard_forward_button_3", FALSE);
225 option_add_int(&o_iconify_start, "iconify_start", CORNER_TOP_RIGHT);
226 option_add_int(&o_iconify_dir, "iconify_dir", DIR_VERT);
228 option_add_notify(pinboard_check_options);
230 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
231 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
234 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
235 * and make it the current pinboard.
236 * Any existing pinned items are removed. You must call this
237 * at least once before using the pinboard. NULL disables the
238 * pinboard.
240 void pinboard_activate(const gchar *name)
242 Pinboard *old_board = current_pinboard;
243 guchar *path, *slash;
245 /* Treat an empty name the same as NULL */
246 if (name && !*name)
247 name = NULL;
249 if (old_board)
250 pinboard_clear();
252 if (!name)
254 if (number_of_windows < 1 && gtk_main_level() > 0)
255 gtk_main_quit();
257 gdk_property_delete(gdk_get_default_root_window(),
258 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
259 return;
262 number_of_windows++;
264 slash = strchr(name, '/');
265 if (slash)
267 if (access(name, F_OK))
268 path = NULL; /* File does not (yet) exist */
269 else
270 path = g_strdup(name);
272 else
274 guchar *leaf;
276 leaf = g_strconcat("pb_", name, NULL);
277 path = choices_find_path_load(leaf, PROJECT);
278 g_free(leaf);
281 current_pinboard = g_new(Pinboard, 1);
282 current_pinboard->name = g_strdup(name);
283 current_pinboard->icons = NULL;
284 current_pinboard->window = NULL;
285 current_pinboard->backdrop = NULL;
286 current_pinboard->backdrop_style = BACKDROP_NONE;
287 current_pinboard->to_backdrop_app = -1;
288 current_pinboard->from_backdrop_app = -1;
289 current_pinboard->input_tag = -1;
290 current_pinboard->input_buffer = NULL;
292 create_pinboard_window(current_pinboard);
294 loading_pinboard++;
295 if (path)
297 xmlDocPtr doc;
298 doc = xmlParseFile(path);
299 if (doc)
301 pinboard_load_from_xml(doc);
302 xmlFreeDoc(doc);
303 reload_backdrop(current_pinboard,
304 current_pinboard->backdrop,
305 current_pinboard->backdrop_style);
307 else
309 parse_file(path, pin_from_file);
310 info_message(_("Your old pinboard file has been "
311 "converted to the new XML format."));
312 pinboard_save();
314 g_free(path);
316 else
317 pinboard_pin(home_dir, "Home",
318 4 + ICON_WIDTH / 2,
319 4 + ICON_HEIGHT / 2);
320 loading_pinboard--;
322 if (o_pinboard_tasklist.int_value)
323 tasklist_set_active(TRUE);
326 /* Return the window of the current pinboard, or NULL.
327 * Used to make sure lowering the panels doesn't lose them...
329 GdkWindow *pinboard_get_window(void)
331 if (current_pinboard)
332 return current_pinboard->window->window;
333 return NULL;
336 const char *pinboard_get_name(void)
338 g_return_val_if_fail(current_pinboard != NULL, NULL);
340 return current_pinboard->name;
343 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
344 * being cleared.
346 void pinboard_add_widget(GtkWidget *widget)
348 GtkRequisition req;
349 GdkRectangle rect;
351 g_return_if_fail(current_pinboard != NULL);
353 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, 0, 0);
355 gtk_widget_size_request(widget, &req);
357 rect.width = req.width;
358 rect.height = req.height;
359 find_free_rect(current_pinboard, &rect);
361 gtk_fixed_move(GTK_FIXED(current_pinboard->fixed),
362 widget, rect.x, rect.y);
365 /* Add a new icon to the background.
366 * 'path' should be an absolute pathname.
367 * 'x' and 'y' are the coordinates of the point in the middle of the text.
368 * 'name' is the name to use. If NULL then the leafname of path is used.
370 * name and path are in UTF-8 for Gtk+-2.0 only.
372 void pinboard_pin(const gchar *path, const gchar *name, int x, int y)
374 GtkWidget *align, *vbox;
375 GdkWindow *events;
376 PinIcon *pi;
377 Icon *icon;
379 g_return_if_fail(path != NULL);
380 g_return_if_fail(current_pinboard != NULL);
382 pi = pin_icon_new(path, name);
383 icon = (Icon *) pi;
384 pi->x = x;
385 pi->y = y;
387 /* This is a bit complicated...
389 * An icon needs to be a NO_WINDOW widget so that the image can
390 * blend with the background (A ParentRelative window also works, but
391 * is slow, causes the xfree86's memory consumption to grow without
392 * bound, and doesn't even get freed when the filer quits!).
394 * However, the icon also needs to have a window, so we get events
395 * delivered correctly. The solution is to float an InputOnly window
396 * over the icon. Since GtkButton works the same way, we just use
397 * that :-)
400 /* Button takes the initial ref of Icon */
401 pi->win = gtk_button_new();
402 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
403 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
404 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
406 vbox = gtk_vbox_new(FALSE, 0);
407 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
409 align = gtk_alignment_new(0.5, 0.5, 0, 0);
410 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
411 gtk_container_add(GTK_CONTAINER(align), pi->widget);
413 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
414 drag_set_pinicon_dest(pi);
415 g_signal_connect(pi->win, "drag_data_get",
416 G_CALLBACK(drag_data_get), NULL);
418 pi->label = gtk_label_new(icon->item->leafname);
419 gtk_label_set_line_wrap(GTK_LABEL(pi->label), TRUE);
420 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
422 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
424 snap_to_grid(&x, &y);
425 pi->x = x;
426 pi->y = y;
427 gtk_widget_show_all(pi->win);
428 pinboard_reshape_icon((Icon *) pi);
430 gtk_widget_realize(pi->win);
431 events = GTK_BUTTON(pi->win)->event_window;
432 gdk_window_set_events(events,
433 GDK_EXPOSURE_MASK |
434 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
435 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
436 GDK_BUTTON1_MOTION_MASK | GDK_ENTER_NOTIFY_MASK |
437 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
438 g_signal_connect(pi->win, "enter-notify-event",
439 G_CALLBACK(enter_notify), pi);
440 g_signal_connect(pi->win, "button-press-event",
441 G_CALLBACK(button_press_event), pi);
442 g_signal_connect(pi->win, "button-release-event",
443 G_CALLBACK(button_release_event), pi);
444 g_signal_connect(pi->win, "motion-notify-event",
445 G_CALLBACK(icon_motion_notify), pi);
446 g_signal_connect(pi->win, "expose-event",
447 G_CALLBACK(draw_icon), pi);
448 g_signal_connect_swapped(pi->win, "style-set",
449 G_CALLBACK(pinboard_reshape_icon), pi);
450 g_signal_connect_swapped(pi->win, "destroy",
451 G_CALLBACK(pin_icon_destroyed), pi);
453 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
454 pin_icon_set_tip(pi);
456 if (!loading_pinboard)
457 pinboard_save();
460 /* Put a border around the icon, briefly.
461 * If icon is NULL then cancel any existing wink.
462 * The icon will automatically unhighlight unless timeout is FALSE,
463 * in which case you must call this function again (with NULL or another
464 * icon) to remove the highlight.
466 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
468 PinIcon *old = current_wink_icon;
470 if (old == pi)
471 return;
473 current_wink_icon = pi;
475 if (old)
477 gtk_widget_queue_draw(old->win);
478 gdk_window_process_updates(old->widget->window, TRUE);
480 if (wink_timeout != -1)
481 gtk_timeout_remove(wink_timeout);
484 if (pi)
486 gtk_widget_queue_draw(pi->win);
487 gdk_window_process_updates(pi->widget->window, TRUE);
489 if (timeout)
490 wink_timeout = gtk_timeout_add(300, end_wink, NULL);
491 else
492 wink_timeout = -1;
496 /* 'app' is saved as the new application to set the backdrop. It will then be
497 * run, and should communicate with the filer as described in the manual.
499 void pinboard_set_backdrop_app(const gchar *app)
501 XMLwrapper *ai;
502 DirItem *item;
503 gboolean can_set;
505 item = diritem_new("");
506 diritem_restat(app, item, NULL);
507 if (!(item->flags & ITEM_FLAG_APPDIR))
509 delayed_error(_("The backdrop handler must be an application "
510 "directory. Drag an application directory "
511 "into the Set Backdrop dialog box, or (for "
512 "programmers) pass it to the SOAP "
513 "SetBackdropApp method."));
514 diritem_free(item);
515 return;
518 ai = appinfo_get(app, item);
519 diritem_free(item);
521 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
522 if (ai)
523 g_object_unref(ai);
525 if (can_set)
526 set_backdrop(app, BACKDROP_PROGRAM);
527 else
528 delayed_error(_("You can only set the backdrop to an image "
529 "or to a program which knows how to "
530 "manage ROX-Filer's backdrop.\n\n"
531 "Programmers: the application's AppInfo.xml "
532 "must contain the CanSetBackdrop element, as "
533 "described in ROX-Filer's manual."));
536 /* Open a dialog box allowing the user to set the backdrop */
537 void pinboard_set_backdrop(void)
539 GtkWidget *dialog, *frame, *label, *radio, *hbox;
540 GtkBox *vbox;
541 GtkTargetEntry targets[] = {
542 {"text/uri-list", 0, TARGET_URI_LIST},
545 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
546 GTK_DIALOG_NO_SEPARATOR,
547 GTK_STOCK_CLEAR, GTK_RESPONSE_NO,
548 GTK_STOCK_OK, GTK_RESPONSE_OK,
549 NULL);
550 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
552 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
554 label = gtk_label_new(_("Display backdrop image:"));
555 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
556 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
557 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
559 /* The Centred, Scaled, Tiled radios... */
560 hbox = gtk_hbox_new(TRUE, 2);
561 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
563 radio = gtk_radio_button_new_with_label(NULL, _("Centred"));
564 g_object_set_data(G_OBJECT(dialog), "radio_centred", radio);
565 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
567 radio = gtk_radio_button_new_with_label(
568 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio)),
569 _("Scaled"));
570 g_object_set_data(G_OBJECT(dialog), "radio_scaled", radio);
571 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
573 radio = gtk_radio_button_new_with_label(
574 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio)),
575 _("Tiled"));
576 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
578 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUE);
580 /* The drop area... */
581 frame = gtk_frame_new(NULL);
582 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
583 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
584 gtk_container_set_border_width(GTK_CONTAINER(frame), 4);
586 gtk_drag_dest_set(frame, GTK_DEST_DEFAULT_ALL,
587 targets, sizeof(targets) / sizeof(*targets),
588 GDK_ACTION_COPY);
589 g_signal_connect(frame, "drag_data_received",
590 G_CALLBACK(drag_backdrop_dropped), dialog);
592 label = gtk_label_new(_("Drop an image here"));
593 gtk_misc_set_padding(GTK_MISC(label), 10, 20);
594 gtk_container_add(GTK_CONTAINER(frame), label);
596 g_signal_connect(dialog, "response",
597 G_CALLBACK(backdrop_response), NULL);
598 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
600 gtk_widget_show_all(dialog);
603 /****************************************************************
604 * INTERNAL FUNCTIONS *
605 ****************************************************************/
607 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
609 if (response == GTK_RESPONSE_NO)
610 set_backdrop(NULL, BACKDROP_NONE);
612 gtk_widget_destroy(dialog);
615 static void drag_backdrop_dropped(GtkWidget *frame,
616 GdkDragContext *context,
617 gint x,
618 gint y,
619 GtkSelectionData *selection_data,
620 guint drag_info,
621 guint32 time,
622 GtkWidget *dialog)
624 struct stat info;
625 const gchar *path = NULL;
626 GList *uris;
628 if (!selection_data->data)
629 return; /* Timeout? */
631 uris = uri_list_to_glist(selection_data->data);
633 if (g_list_length(uris) == 1)
634 path = get_local_path((guchar *) uris->data);
635 g_list_free(uris);
637 if (!path)
639 delayed_error(
640 _("You should drop a single (local) image file "
641 "onto the drop box - that image will be "
642 "used for the desktop background. You can also "
643 "drag certain applications onto this box."));
644 return;
647 if (mc_stat(path, &info))
649 delayed_error(
650 _("Can't access '%s':\n%s"), path,
651 g_strerror(errno));
652 return;
655 if (S_ISDIR(info.st_mode))
657 /* Use this program to set the backdrop */
658 pinboard_set_backdrop_app(path);
660 else if (S_ISREG(info.st_mode))
662 GtkWidget *radio;
664 radio = g_object_get_data(G_OBJECT(dialog), "radio_scaled");
665 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)))
667 set_backdrop(path, BACKDROP_SCALE);
668 return;
671 radio = g_object_get_data(G_OBJECT(dialog), "radio_centred");
672 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)))
674 set_backdrop(path, BACKDROP_CENTRE);
675 return;
678 set_backdrop(path, BACKDROP_TILE);
680 else
681 delayed_error(_("Only files (and certain applications) can be "
682 "used to set the background image."));
685 static void pinboard_check_options(void)
687 GdkColor n_fg, n_bg;
689 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
690 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
692 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
694 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
695 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0)
697 pin_text_fg_col = n_fg;
698 pin_text_bg_col = n_bg;
700 if (pinicon_style)
702 g_object_unref(G_OBJECT(pinicon_style));
703 pinicon_style = NULL;
706 if (current_pinboard)
708 GtkWidget *w = current_pinboard->window;
709 GdkColormap *cm;
711 cm = gtk_widget_get_colormap(w);
713 gdk_colormap_alloc_color(cm, &n_bg, FALSE, TRUE);
714 gtk_widget_modify_bg(w, GTK_STATE_NORMAL, &n_bg);
716 /* Only redraw the background if there is no image */
717 if (!current_pinboard->backdrop)
718 reload_backdrop(current_pinboard,
719 NULL, BACKDROP_NONE);
721 reshape_all();
724 tasklist_style_changed();
728 static gint end_wink(gpointer data)
730 pinboard_wink_item(NULL, FALSE);
731 return FALSE;
734 /* Updates the width, height, name_width and label fields, and resizes the
735 * window. Also sets the style to pinicon_style, generating it if needed.
737 static void set_size_and_style(PinIcon *pi)
739 Icon *icon = (Icon *) pi;
740 MaskedPixmap *image = icon->item->image;
741 int iwidth = image->width;
742 int iheight = image->height;
744 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
745 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
746 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
747 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
749 gtk_label_set_text(GTK_LABEL(pi->label), icon->item->leafname);
751 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
754 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
756 static GtkWidgetClass *parent_class = NULL;
757 Icon *icon = (Icon *) pi;
758 DirItem *item = icon->item;
759 MaskedPixmap *image = item->image;
760 int iwidth = image->width;
761 int iheight = image->height;
762 int x, y;
763 GtkStateType state = icon->selected ? GTK_STATE_SELECTED
764 : GTK_STATE_NORMAL;
766 if (!parent_class)
768 gpointer c = ((GTypeInstance *) widget)->g_class;
769 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
772 x = pi->widget->allocation.x;
773 y = pi->widget->allocation.y;
775 gdk_pixbuf_render_to_drawable_alpha(
776 icon->selected ? image->pixbuf_lit : image->pixbuf,
777 pi->widget->window,
778 0, 0, /* src */
779 x, y, /* dest */
780 iwidth, iheight,
781 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
782 GDK_RGB_DITHER_NORMAL, 0, 0);
784 if (item->flags & ITEM_FLAG_SYMLINK)
786 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
787 pi->widget->window,
788 0, 0, /* src */
789 x, y,
790 -1, -1,
791 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
792 GDK_RGB_DITHER_NORMAL, 0, 0);
794 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
796 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
797 ? im_mounted
798 : im_unmounted;
800 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
801 pi->widget->window,
802 0, 0, /* src */
803 x, y,
804 -1, -1,
805 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
806 GDK_RGB_DITHER_NORMAL, 0, 0);
809 if (icon->selected)
811 gtk_paint_flat_box(pi->label->style, pi->label->window,
812 state,
813 GTK_SHADOW_NONE,
814 NULL, pi->label, "text",
815 pi->label->allocation.x,
816 pi->label->allocation.y,
817 pi->label->allocation.width,
818 pi->label->allocation.height);
821 /* Draw children */
822 (parent_class->expose_event)(widget, event);
824 /* Stop the button effect */
825 return TRUE;
828 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
830 gint x, y, width, height;
832 if (current_wink_icon != pi)
833 return FALSE;
835 x = widget->allocation.x;
836 y = widget->allocation.y;
837 width = widget->allocation.width;
838 height = widget->allocation.height;
840 gdk_draw_rectangle(widget->window,
841 pi->widget->style->white_gc,
842 FALSE,
843 x, y, width - 1, height - 1);
844 gdk_draw_rectangle(widget->window,
845 pi->widget->style->black_gc,
846 FALSE,
847 x + 1, y + 1, width - 3, height - 3);
849 return FALSE;
852 static gboolean enter_notify(GtkWidget *widget,
853 GdkEventCrossing *event,
854 PinIcon *pi)
856 icon_may_update((Icon *) pi);
858 return FALSE;
861 static void perform_action(PinIcon *pi, GdkEventButton *event)
863 BindAction action;
864 Icon *icon = (Icon *) pi;
866 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
867 event);
869 /* Actions that can happen with or without an icon */
870 switch (action)
872 case ACT_CLEAR_SELECTION:
873 icon_select_only(NULL);
874 return;
875 case ACT_POPUP_MENU:
876 dnd_motion_ungrab();
877 pinboard_show_menu(event, pi);
878 return;
879 case ACT_IGNORE:
880 return;
881 default:
882 break;
885 g_return_if_fail(pi != NULL);
887 switch (action)
889 case ACT_OPEN_ITEM:
890 dnd_motion_ungrab();
891 pinboard_wink_item(pi, TRUE);
892 if (event->type == GDK_2BUTTON_PRESS)
893 icon_set_selected(icon, FALSE);
894 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
895 break;
896 case ACT_EDIT_ITEM:
897 dnd_motion_ungrab();
898 pinboard_wink_item(pi, TRUE);
899 if (event->type == GDK_2BUTTON_PRESS)
900 icon_set_selected(icon, FALSE);
901 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
902 break;
903 case ACT_PRIME_AND_SELECT:
904 if (!icon->selected)
905 icon_select_only(icon);
906 dnd_motion_start(MOTION_READY_FOR_DND);
907 break;
908 case ACT_PRIME_AND_TOGGLE:
909 icon_set_selected(icon, !icon->selected);
910 dnd_motion_start(MOTION_READY_FOR_DND);
911 break;
912 case ACT_PRIME_FOR_DND:
913 dnd_motion_start(MOTION_READY_FOR_DND);
914 break;
915 case ACT_TOGGLE_SELECTED:
916 icon_set_selected(icon, !icon->selected);
917 break;
918 case ACT_SELECT_EXCL:
919 icon_select_only(icon);
920 break;
921 default:
922 g_warning("Unsupported action : %d\n", action);
923 break;
927 static void forward_to_root(GdkEventButton *event)
929 XButtonEvent xev;
931 if (event->type == GDK_BUTTON_PRESS)
933 xev.type = ButtonPress;
934 XUngrabPointer(gdk_display, event->time);
936 else
937 xev.type = ButtonRelease;
939 xev.window = gdk_x11_get_default_root_xwindow();
940 xev.root = xev.window;
941 xev.subwindow = None;
942 xev.time = event->time;
943 xev.x = event->x_root; /* Needed for icewm */
944 xev.y = event->y_root;
945 xev.x_root = event->x_root;
946 xev.y_root = event->y_root;
947 xev.state = event->state;
948 xev.button = event->button;
949 xev.same_screen = True;
951 XSendEvent(gdk_display, xev.window, False,
952 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
955 #define FORWARDED_BUTTON(pi, button) ((button) == 2 || \
956 ((button) == 3 && o_forward_button_3.int_value && !pi))
958 /* pi is NULL if this is a root event */
959 static gboolean button_release_event(GtkWidget *widget,
960 GdkEventButton *event,
961 PinIcon *pi)
963 if (FORWARDED_BUTTON(pi, event->button))
964 forward_to_root(event);
965 else if (dnd_motion_release(event))
966 return TRUE;
968 perform_action(pi, event);
970 return TRUE;
973 /* pi is NULL if this is a root event */
974 static gboolean button_press_event(GtkWidget *widget,
975 GdkEventButton *event,
976 PinIcon *pi)
978 /* Just in case we've jumped in front of everything... */
979 gdk_window_lower(current_pinboard->window->window);
981 if (FORWARDED_BUTTON(pi, event->button))
982 forward_to_root(event);
983 else if (dnd_motion_press(widget, event))
984 perform_action(pi, event);
986 return TRUE;
989 static void start_drag(PinIcon *pi, GdkEventMotion *event)
991 GtkWidget *widget = pi->win;
992 Icon *icon = (Icon *) pi;
994 if (!icon->selected)
996 tmp_icon_selected = TRUE;
997 icon_select_only(icon);
1000 g_return_if_fail(icon_selection != NULL);
1002 pinboard_drag_in_progress = icon;
1004 if (icon_selection->next == NULL)
1005 drag_one_item(widget, event, icon->path, icon->item, NULL);
1006 else
1008 guchar *uri_list;
1010 uri_list = icon_create_uri_list();
1011 drag_selection(widget, event, uri_list);
1012 g_free(uri_list);
1016 /* An icon is being dragged around... */
1017 static gint icon_motion_notify(GtkWidget *widget,
1018 GdkEventMotion *event,
1019 PinIcon *pi)
1021 if (motion_state == MOTION_READY_FOR_DND)
1023 if (dnd_motion_moved(event))
1024 start_drag(pi, event);
1025 return TRUE;
1028 return FALSE;
1031 static void backdrop_from_xml(xmlNode *node)
1033 gchar *style;
1035 g_free(current_pinboard->backdrop);
1036 current_pinboard->backdrop = xmlNodeGetContent(node);
1038 style = xmlGetProp(node, "style");
1040 if (style)
1042 current_pinboard->backdrop_style =
1043 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1044 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1045 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1046 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1047 BACKDROP_NONE;
1048 g_free(style);
1050 else
1051 current_pinboard->backdrop_style = BACKDROP_TILE;
1054 /* Create one pinboard icon for each icon in the doc */
1055 static void pinboard_load_from_xml(xmlDocPtr doc)
1057 xmlNodePtr node, root;
1058 char *tmp, *label, *path;
1059 int x, y;
1061 root = xmlDocGetRootElement(doc);
1063 for (node = root->xmlChildrenNode; node; node = node->next)
1065 if (node->type != XML_ELEMENT_NODE)
1066 continue;
1067 if (strcmp(node->name, "backdrop") == 0)
1069 backdrop_from_xml(node);
1070 continue;
1072 if (strcmp(node->name, "icon") != 0)
1073 continue;
1075 tmp = xmlGetProp(node, "x");
1076 if (!tmp)
1077 continue;
1078 x = atoi(tmp);
1079 g_free(tmp);
1081 tmp = xmlGetProp(node, "y");
1082 if (!tmp)
1083 continue;
1084 y = atoi(tmp);
1085 g_free(tmp);
1087 label = xmlGetProp(node, "label");
1088 if (!label)
1089 label = g_strdup("<missing label>");
1090 path = xmlNodeGetContent(node);
1091 if (!path)
1092 path = g_strdup("<missing path>");
1094 pinboard_pin(path, label, x, y);
1096 g_free(path);
1097 g_free(label);
1101 /* Called for each line in the pinboard file while loading a new board.
1102 * Only used for old-format files when converting to XML.
1104 static const char *pin_from_file(gchar *line)
1106 gchar *leaf = NULL;
1107 int x, y, n;
1109 if (*line == '<')
1111 gchar *end;
1113 end = strchr(line + 1, '>');
1114 if (!end)
1115 return _("Missing '>' in icon label");
1117 leaf = g_strndup(line + 1, end - line - 1);
1119 line = end + 1;
1121 while (isspace(*line))
1122 line++;
1123 if (*line != ',')
1124 return _("Missing ',' after icon label");
1125 line++;
1128 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1129 return NULL; /* Ignore format errors */
1131 pinboard_pin(line + n, leaf, x, y);
1133 g_free(leaf);
1135 return NULL;
1138 /* Write the current state of the pinboard to the current pinboard file */
1139 static void pinboard_save(void)
1141 guchar *save = NULL;
1142 guchar *save_new = NULL;
1143 GList *next;
1144 xmlDocPtr doc = NULL;
1145 xmlNodePtr root;
1147 g_return_if_fail(current_pinboard != NULL);
1149 if (strchr(current_pinboard->name, '/'))
1150 save = g_strdup(current_pinboard->name);
1151 else
1153 guchar *leaf;
1155 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1156 save = choices_find_path_save(leaf, PROJECT, TRUE);
1157 g_free(leaf);
1160 if (!save)
1161 return;
1163 doc = xmlNewDoc("1.0");
1164 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1166 root = xmlDocGetRootElement(doc);
1168 if (current_pinboard->backdrop)
1170 BackdropStyle style = current_pinboard->backdrop_style;
1171 xmlNodePtr tree;
1173 tree = xmlNewTextChild(root, NULL, "backdrop",
1174 current_pinboard->backdrop);
1175 xmlSetProp(tree, "style",
1176 style == BACKDROP_TILE ? "Tiled" :
1177 style == BACKDROP_CENTRE ? "Centred" :
1178 style == BACKDROP_SCALE ? "Scaled" :
1179 "Program");
1182 for (next = current_pinboard->icons; next; next = next->next)
1184 xmlNodePtr tree;
1185 PinIcon *pi = (PinIcon *) next->data;
1186 Icon *icon = (Icon *) pi;
1187 char *tmp;
1189 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1191 tmp = g_strdup_printf("%d", pi->x);
1192 xmlSetProp(tree, "x", tmp);
1193 g_free(tmp);
1195 tmp = g_strdup_printf("%d", pi->y);
1196 xmlSetProp(tree, "y", tmp);
1197 g_free(tmp);
1199 xmlSetProp(tree, "label", icon->item->leafname);
1202 save_new = g_strconcat(save, ".new", NULL);
1203 if (save_xml_file(doc, save_new) || rename(save_new, save))
1204 delayed_error(_("Error saving pinboard %s: %s"),
1205 save, g_strerror(errno));
1206 g_free(save_new);
1208 g_free(save);
1209 if (doc)
1210 xmlFreeDoc(doc);
1213 static void snap_to_grid(int *x, int *y)
1215 int step = o_pinboard_grid_step.int_value;
1217 *x = ((*x + step / 2) / step) * step;
1218 *y = ((*y + step / 2) / step) * step;
1221 /* Convert (x,y) from a centre point to a window position */
1222 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1224 gboolean clamp = o_pinboard_clamp_icons.int_value;
1225 GtkRequisition req;
1227 gtk_widget_size_request(pi->win, &req);
1229 *x -= req.width >> 1;
1230 *y -= req.height >> 1;
1231 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1232 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1235 /* Same as drag_set_dest(), but for pinboard icons */
1236 static void drag_set_pinicon_dest(PinIcon *pi)
1238 GtkObject *obj = GTK_OBJECT(pi->win);
1240 make_drop_target(pi->win, 0);
1242 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1243 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1244 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1247 /* Called during the drag when the mouse is in a widget registered
1248 * as a drop target. Returns TRUE if we can accept the drop.
1250 static gboolean drag_motion(GtkWidget *widget,
1251 GdkDragContext *context,
1252 gint x,
1253 gint y,
1254 guint time,
1255 PinIcon *pi)
1257 GdkDragAction action = context->suggested_action;
1258 const char *type = NULL;
1259 Icon *icon = (Icon *) pi;
1260 DirItem *item = icon->item;
1262 if (gtk_drag_get_source_widget(context) == widget)
1263 goto out; /* Can't drag something to itself! */
1265 if (icon->selected)
1266 goto out; /* Can't drag a selection to itself */
1268 type = dnd_motion_item(context, &item);
1270 if (!item)
1271 type = NULL;
1272 out:
1273 /* We actually must pretend to accept the drop, even if the
1274 * directory isn't writeable, so that the spring-opening
1275 * thing works.
1278 /* Don't allow drops to non-writeable directories */
1279 if (o_dnd_spring_open.int_value == FALSE &&
1280 type == drop_dest_dir &&
1281 access(icon->path, W_OK) != 0)
1283 type = NULL;
1286 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1287 if (type)
1289 gdk_drag_status(context, action, time);
1290 g_dataset_set_data_full(context, "drop_dest_path",
1291 g_strdup(icon->path), g_free);
1292 if (type == drop_dest_dir)
1293 dnd_spring_load(context, NULL);
1295 pinboard_wink_item(pi, FALSE);
1297 else
1298 gdk_drag_status(context, 0, time);
1300 /* Always return TRUE to stop the pinboard getting the events */
1301 return TRUE;
1304 static gboolean pinboard_shadow = FALSE;
1305 static gint shadow_x, shadow_y;
1306 #define SHADOW_SIZE (ICON_WIDTH)
1308 static gboolean bg_expose(GtkWidget *widget,
1309 GdkEventExpose *event, gpointer data)
1311 gpointer gclass = ((GTypeInstance *) widget)->g_class;
1313 /* TODO: Split large regions into smaller chunks... */
1315 gdk_window_begin_paint_region(widget->window, event->region);
1317 if (pinboard_shadow)
1319 gdk_draw_rectangle(widget->window,
1320 widget->style->white_gc, FALSE,
1321 shadow_x, shadow_y,
1322 SHADOW_SIZE, SHADOW_SIZE);
1323 gdk_draw_rectangle(widget->window,
1324 widget->style->black_gc, FALSE,
1325 shadow_x + 1, shadow_y + 1,
1326 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1329 ((GtkWidgetClass *) gclass)->expose_event(widget, event);
1331 gdk_window_end_paint(widget->window);
1333 return TRUE;
1336 /* Draw a 'shadow' under an icon being dragged, showing where
1337 * it will land.
1339 static void pinboard_set_shadow(gboolean on)
1341 GdkRectangle area;
1343 if (pinboard_shadow)
1345 area.x = shadow_x;
1346 area.y = shadow_y;
1347 area.width = SHADOW_SIZE + 1;
1348 area.height = SHADOW_SIZE + 1;
1350 gdk_window_invalidate_rect(current_pinboard->window->window,
1351 &area, TRUE);
1354 if (on)
1356 int old_x = shadow_x, old_y = shadow_y;
1358 gdk_window_get_pointer(current_pinboard->fixed->window,
1359 &shadow_x, &shadow_y, NULL);
1360 snap_to_grid(&shadow_x, &shadow_y);
1361 shadow_x -= SHADOW_SIZE / 2;
1362 shadow_y -= SHADOW_SIZE / 2;
1365 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1366 return;
1368 area.x = shadow_x;
1369 area.y = shadow_y;
1370 area.width = SHADOW_SIZE + 1;
1371 area.height = SHADOW_SIZE + 1;
1373 gdk_window_invalidate_rect(current_pinboard->window->window,
1374 &area, TRUE);
1377 pinboard_shadow = on;
1380 /* Called when dragging some pinboard icons finishes */
1381 void pinboard_move_icons(void)
1383 int x = shadow_x, y = shadow_y;
1384 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1385 int width, height;
1387 g_return_if_fail(pi != NULL);
1389 x += SHADOW_SIZE / 2;
1390 y += SHADOW_SIZE / 2;
1391 snap_to_grid(&x, &y);
1393 if (pi->x == x && pi->y == y)
1394 return;
1396 pi->x = x;
1397 pi->y = y;
1398 gdk_drawable_get_size(pi->win->window, &width, &height);
1399 offset_from_centre(pi, &x, &y);
1401 fixed_move_fast(GTK_FIXED(current_pinboard->fixed), pi->win, x, y);
1403 pinboard_save();
1406 static void drag_leave(GtkWidget *widget,
1407 GdkDragContext *context,
1408 guint32 time,
1409 PinIcon *pi)
1411 pinboard_wink_item(NULL, FALSE);
1412 dnd_spring_abort();
1415 static gboolean bg_drag_leave(GtkWidget *widget,
1416 GdkDragContext *context,
1417 guint32 time,
1418 gpointer data)
1420 pinboard_set_shadow(FALSE);
1421 return TRUE;
1424 static gboolean bg_drag_motion(GtkWidget *widget,
1425 GdkDragContext *context,
1426 gint x,
1427 gint y,
1428 guint time,
1429 gpointer data)
1431 /* Dragging from the pinboard to the pinboard is not allowed */
1433 if (!provides(context, text_uri_list))
1434 return FALSE;
1436 pinboard_set_shadow(TRUE);
1438 gdk_drag_status(context,
1439 context->suggested_action == GDK_ACTION_ASK
1440 ? GDK_ACTION_LINK : context->suggested_action,
1441 time);
1442 return TRUE;
1445 static void drag_end(GtkWidget *widget,
1446 GdkDragContext *context,
1447 PinIcon *pi)
1449 pinboard_drag_in_progress = NULL;
1450 if (tmp_icon_selected)
1452 icon_select_only(NULL);
1453 tmp_icon_selected = FALSE;
1457 /* Something which affects all the icons has changed - reshape
1458 * and redraw all of them.
1460 static void reshape_all(void)
1462 GList *next;
1464 g_return_if_fail(current_pinboard != NULL);
1466 for (next = current_pinboard->icons; next; next = next->next)
1468 Icon *icon = (Icon *) next->data;
1469 pinboard_reshape_icon(icon);
1473 /* Turns off the pinboard. Does not call gtk_main_quit. */
1474 static void pinboard_clear(void)
1476 GList *next;
1478 g_return_if_fail(current_pinboard != NULL);
1480 tasklist_set_active(FALSE);
1482 next = current_pinboard->icons;
1483 while (next)
1485 PinIcon *pi = (PinIcon *) next->data;
1487 next = next->next;
1489 gtk_widget_destroy(pi->win);
1492 gtk_widget_destroy(current_pinboard->window);
1494 abandon_backdrop_app(current_pinboard);
1496 g_free(current_pinboard->name);
1497 g_free(current_pinboard);
1498 current_pinboard = NULL;
1500 number_of_windows--;
1503 static gpointer parent_class;
1505 static void pin_icon_destroy(Icon *icon)
1507 PinIcon *pi = (PinIcon *) icon;
1509 g_return_if_fail(pi->win != NULL);
1511 gtk_widget_destroy(pi->win);
1514 static void pinboard_remove_items(void)
1516 g_return_if_fail(icon_selection != NULL);
1518 while (icon_selection)
1519 icon_destroy((Icon *) icon_selection->data);
1521 pinboard_save();
1524 static void pin_icon_update(Icon *icon)
1526 pinboard_reshape_icon(icon);
1527 pinboard_save();
1530 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1532 return IS_PIN_ICON(other);
1535 static void pin_icon_class_init(gpointer gclass, gpointer data)
1537 IconClass *icon = (IconClass *) gclass;
1539 parent_class = g_type_class_peek_parent(gclass);
1541 icon->destroy = pin_icon_destroy;
1542 icon->redraw = pinboard_reshape_icon;
1543 icon->update = pin_icon_update;
1544 icon->remove_items = pinboard_remove_items;
1545 icon->same_group = pin_icon_same_group;
1548 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
1552 static GType pin_icon_get_type(void)
1554 static GType type = 0;
1556 if (!type)
1558 static const GTypeInfo info =
1560 sizeof (PinIconClass),
1561 NULL, /* base_init */
1562 NULL, /* base_finalise */
1563 pin_icon_class_init,
1564 NULL, /* class_finalise */
1565 NULL, /* class_data */
1566 sizeof(PinIcon),
1567 0, /* n_preallocs */
1568 pin_icon_init
1571 type = g_type_register_static(icon_get_type(),
1572 "PinIcon", &info, 0);
1575 return type;
1578 static PinIcon *pin_icon_new(const char *pathname, const char *name)
1580 PinIcon *pi;
1581 Icon *icon;
1583 pi = g_object_new(pin_icon_get_type(), NULL);
1584 icon = (Icon *) pi;
1586 icon_set_path(icon, pathname, name);
1588 return pi;
1591 /* Called when the window widget is somehow destroyed */
1592 static void pin_icon_destroyed(PinIcon *pi)
1594 g_return_if_fail(pi->win != NULL);
1596 pi->win = NULL;
1598 pinboard_wink_item(NULL, FALSE);
1600 if (pinboard_drag_in_progress == (Icon *) pi)
1601 pinboard_drag_in_progress = NULL;
1603 if (current_pinboard)
1604 current_pinboard->icons =
1605 g_list_remove(current_pinboard->icons, pi);
1607 g_object_unref(pi);
1610 /* Set the tooltip */
1611 static void pin_icon_set_tip(PinIcon *pi)
1613 XMLwrapper *ai;
1614 xmlNode *node;
1615 Icon *icon = (Icon *) pi;
1617 g_return_if_fail(pi != NULL);
1619 ai = appinfo_get(icon->path, icon->item);
1621 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
1623 guchar *str;
1624 str = xmlNodeListGetString(node->doc,
1625 node->xmlChildrenNode, 1);
1626 if (str)
1628 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
1629 g_free(str);
1632 else
1633 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
1635 if (ai)
1636 g_object_unref(ai);
1639 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
1641 int pos[3];
1643 pos[0] = event->x_root;
1644 pos[1] = event->y_root;
1645 pos[2] = 1;
1647 icon_prepare_menu((Icon *) pi, TRUE);
1649 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
1650 position_menu,
1651 (gpointer) pos, event->button, event->time);
1654 static void create_pinboard_window(Pinboard *pinboard)
1656 GtkWidget *win;
1658 g_return_if_fail(pinboard->window == NULL);
1660 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1662 gtk_widget_modify_bg(win, GTK_STATE_NORMAL, &pin_text_bg_col);
1664 gtk_widget_set_double_buffered(win, FALSE);
1665 gtk_widget_set_app_paintable(win, TRUE);
1666 gtk_widget_set_name(win, "rox-pinboard");
1667 pinboard->window = win;
1668 pinboard->fixed = gtk_fixed_new();
1669 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
1671 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
1673 gtk_widget_set_size_request(win, screen_width, screen_height);
1674 gtk_widget_realize(win);
1675 gtk_window_move(GTK_WINDOW(win), 0, 0);
1676 make_panel_window(win);
1678 /* TODO: Use gdk function when it supports this type */
1680 GdkAtom desktop_type;
1682 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
1683 FALSE);
1684 gdk_property_change(win->window,
1685 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
1686 gdk_atom_intern("ATOM", FALSE), 32,
1687 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
1690 gtk_widget_add_events(win,
1691 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1692 GDK_EXPOSURE_MASK);
1693 g_signal_connect(win, "button-press-event",
1694 G_CALLBACK(button_press_event), NULL);
1695 g_signal_connect(win, "button-release-event",
1696 G_CALLBACK(button_release_event), NULL);
1697 g_signal_connect(pinboard->fixed, "expose_event",
1698 G_CALLBACK(bg_expose), NULL);
1700 /* Drag and drop handlers */
1701 drag_set_pinboard_dest(win);
1702 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
1703 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
1705 gtk_widget_show_all(win);
1706 gdk_window_lower(win->window);
1709 /* Load image 'path' and scale according to 'style' */
1710 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
1712 GdkPixmap *pixmap;
1713 GdkPixbuf *pixbuf;
1714 GError *error = NULL;
1716 pixbuf = gdk_pixbuf_new_from_file(path, &error);
1717 if (error)
1719 delayed_error(_("Error loading backdrop image:\n%s\n"
1720 "Backdrop removed."),
1721 error->message);
1722 g_error_free(error);
1723 set_backdrop(NULL, BACKDROP_NONE);
1724 return NULL;
1727 if (style == BACKDROP_SCALE)
1729 GdkPixbuf *old = pixbuf;
1731 pixbuf = gdk_pixbuf_scale_simple(old,
1732 screen_width, screen_height,
1733 GDK_INTERP_HYPER);
1735 g_object_unref(old);
1737 else if (style == BACKDROP_CENTRE)
1739 GdkPixbuf *old = pixbuf;
1740 int x, y, width, height;
1742 width = gdk_pixbuf_get_width(pixbuf);
1743 height = gdk_pixbuf_get_height(pixbuf);
1745 pixbuf = gdk_pixbuf_new(
1746 gdk_pixbuf_get_colorspace(pixbuf), 0,
1747 8, screen_width, screen_height);
1748 gdk_pixbuf_fill(pixbuf, 0);
1750 x = (screen_width - width) / 2;
1751 y = (screen_height - height) / 2;
1752 x = MAX(x, 0);
1753 y = MAX(y, 0);
1755 gdk_pixbuf_composite(old, pixbuf,
1756 x, y,
1757 MIN(screen_width, width),
1758 MIN(screen_height, height),
1759 x, y, 1, 1,
1760 GDK_INTERP_NEAREST, 255);
1761 g_object_unref(old);
1764 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
1765 &pixmap, NULL, 0);
1766 g_object_unref(pixbuf);
1768 return pixmap;
1771 static void abandon_backdrop_app(Pinboard *pinboard)
1773 g_return_if_fail(pinboard != NULL);
1775 if (pinboard->to_backdrop_app != -1)
1777 close(pinboard->to_backdrop_app);
1778 close(pinboard->from_backdrop_app);
1779 gtk_input_remove(pinboard->input_tag);
1780 g_string_free(pinboard->input_buffer, TRUE);
1781 pinboard->to_backdrop_app = -1;
1782 pinboard->from_backdrop_app = -1;
1783 pinboard->input_tag = -1;
1784 pinboard->input_buffer = NULL;
1787 g_return_if_fail(pinboard->to_backdrop_app == -1);
1788 g_return_if_fail(pinboard->from_backdrop_app == -1);
1789 g_return_if_fail(pinboard->input_tag == -1);
1790 g_return_if_fail(pinboard->input_buffer == NULL);
1793 /* A single line has been read from the child.
1794 * Processes the command, and replies 'ok' (or abandons the child on error).
1796 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
1798 BackdropStyle style;
1799 const char *ok = "ok\n";
1801 if (strncmp(command, "tile ", 5) == 0)
1803 style = BACKDROP_TILE;
1804 command += 5;
1806 else if (strncmp(command, "scale ", 6) == 0)
1808 style = BACKDROP_SCALE;
1809 command += 6;
1811 else if (strncmp(command, "centre ", 7) == 0)
1813 style = BACKDROP_CENTRE;
1814 command += 7;
1816 else
1818 g_warning("Invalid command '%s' from backdrop app\n",
1819 command);
1820 abandon_backdrop_app(pinboard);
1821 return;
1824 /* Load the backdrop. May abandon the program if loading fails. */
1825 reload_backdrop(pinboard, command, style);
1827 if (pinboard->to_backdrop_app == -1)
1828 return;
1830 while (*ok)
1832 int sent;
1834 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
1835 if (sent <= 0)
1837 /* Remote app quit? Not an error. */
1838 abandon_backdrop_app(pinboard);
1839 break;
1841 ok += sent;
1845 static void backdrop_from_child(Pinboard *pinboard,
1846 int src, GdkInputCondition cond)
1848 char buf[256];
1849 int got;
1851 got = read(src, buf, sizeof(buf));
1853 if (got <= 0)
1855 if (got < 0)
1856 g_warning("backdrop_from_child: %s\n",
1857 g_strerror(errno));
1858 abandon_backdrop_app(pinboard);
1859 return;
1862 g_string_append_len(pinboard->input_buffer, buf, got);
1864 while (pinboard->from_backdrop_app != -1)
1866 int len;
1867 char *nl, *command;
1869 nl = strchr(pinboard->input_buffer->str, '\n');
1870 if (!nl)
1871 return; /* Haven't got a whole line yet */
1873 len = nl - pinboard->input_buffer->str;
1874 command = g_strndup(pinboard->input_buffer->str, len);
1875 g_string_erase(pinboard->input_buffer, 0, len + 1);
1877 command_from_backdrop_app(pinboard, command);
1879 g_free(command);
1883 static void reload_backdrop(Pinboard *pinboard,
1884 const gchar *backdrop,
1885 BackdropStyle backdrop_style)
1887 GtkStyle *style;
1889 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
1891 const char *argv[] = {NULL, "--backdrop", NULL};
1892 GError *error = NULL;
1894 g_return_if_fail(pinboard->to_backdrop_app == -1);
1895 g_return_if_fail(pinboard->from_backdrop_app == -1);
1896 g_return_if_fail(pinboard->input_tag == -1);
1897 g_return_if_fail(pinboard->input_buffer == NULL);
1899 argv[0] = make_path(backdrop, "AppRun")->str;
1901 /* Run the program. It'll send us a SOAP message and we'll
1902 * get back here with a different style and image.
1905 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
1906 G_SPAWN_DO_NOT_REAP_CHILD |
1907 G_SPAWN_SEARCH_PATH,
1908 NULL, NULL, /* Child setup fn */
1909 NULL, /* Child PID */
1910 &pinboard->to_backdrop_app,
1911 &pinboard->from_backdrop_app,
1912 NULL, /* Standard error */
1913 &error))
1915 pinboard->input_buffer = g_string_new(NULL);
1916 pinboard->input_tag = gtk_input_add_full(
1917 pinboard->from_backdrop_app,
1918 GDK_INPUT_READ,
1919 (GdkInputFunction) backdrop_from_child,
1920 NULL, pinboard, NULL);
1922 else
1924 delayed_error("%s", error ? error->message : "(null)");
1925 g_error_free(error);
1927 return;
1930 /* Note: Copying a style does not ref the pixmaps! */
1932 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
1933 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
1935 if (backdrop)
1936 style->bg_pixmap[GTK_STATE_NORMAL] =
1937 load_backdrop(backdrop, backdrop_style);
1939 gdk_color_parse(o_pinboard_bg_colour.value,
1940 &style->bg[GTK_STATE_NORMAL]);
1942 gtk_widget_set_style(pinboard->window, style);
1944 g_object_unref(style);
1946 gtk_widget_queue_draw(pinboard->window);
1948 /* Also update root window property (for transparent xterms, etc) */
1949 if (style->bg_pixmap[GTK_STATE_NORMAL])
1951 XID id = GDK_DRAWABLE_XID(style->bg_pixmap[GTK_STATE_NORMAL]);
1952 gdk_property_change(gdk_get_default_root_window(),
1953 gdk_atom_intern("_XROOTPMAP_ID", FALSE),
1954 gdk_atom_intern("PIXMAP", FALSE),
1955 32, GDK_PROP_MODE_REPLACE,
1956 (guchar *) &id, 1);
1958 else
1960 gdk_property_delete(gdk_get_default_root_window(),
1961 gdk_atom_intern("_XROOTPMAP_ID", FALSE));
1965 /* Set and save (path, style) as the new backdrop.
1966 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
1967 * Otherwise, the image is displayed now.
1969 static void set_backdrop(const gchar *path, BackdropStyle style)
1971 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
1972 (path != NULL && style != BACKDROP_NONE));
1974 if (!current_pinboard)
1976 if (!path)
1977 return;
1978 pinboard_activate("Default");
1979 delayed_error(_("No pinboard was in use... "
1980 "the 'Default' pinboard has been selected. "
1981 "Use 'rox -p=Default' to turn it on in "
1982 "future."));
1983 g_return_if_fail(current_pinboard != NULL);
1986 /* We might have just run the old backdrop program and now
1987 * we're going to set a new one! Seems a bit mean...
1990 abandon_backdrop_app(current_pinboard);
1992 g_free(current_pinboard->backdrop);
1993 current_pinboard->backdrop = g_strdup(path);
1994 current_pinboard->backdrop_style = style;
1995 reload_backdrop(current_pinboard,
1996 current_pinboard->backdrop,
1997 current_pinboard->backdrop_style);
1999 pinboard_save();
2002 #define SEARCH_STEP 32
2004 static void search_free(GdkRectangle *rect, GdkRegion *used,
2005 int *outer, int od, int omax,
2006 int *inner, int id, int imax)
2008 *outer = od > 0 ? 0 : omax;
2009 while (*outer >= 0 && *outer <= omax)
2011 *inner = id > 0 ? 0 : imax;
2012 while (*inner >= 0 && *inner <= imax)
2014 if (gdk_region_rect_in(used, rect) ==
2015 GDK_OVERLAP_RECTANGLE_OUT)
2016 return;
2017 *inner += id;
2020 *outer += od;
2023 rect->x = -1;
2024 rect->y = -1;
2027 /* Finds a free area on the pinboard large enough for the width and height
2028 * of the given rectangle, by filling in the x and y fields of 'rect'.
2029 * The search order respects user preferences.
2030 * If no area is free, returns any old area.
2032 static void find_free_rect(Pinboard *pinboard, GdkRectangle *rect)
2034 GdkRegion *used;
2035 GList *next;
2036 GdkRectangle used_rect;
2037 int dx = SEARCH_STEP, dy = SEARCH_STEP;
2039 used = gdk_region_new();
2041 panel_mark_used(used);
2043 /* Subtract the used areas... */
2045 next = GTK_FIXED(pinboard->fixed)->children;
2046 for (; next; next = next->next)
2048 GtkFixedChild *fix = (GtkFixedChild *) next->data;
2050 if (!GTK_WIDGET_VISIBLE(fix->widget))
2051 continue;
2053 used_rect.x = fix->x;
2054 used_rect.y = fix->y;
2055 used_rect.width = fix->widget->requisition.width;
2056 used_rect.height = fix->widget->requisition.height;
2058 gdk_region_union_with_rect(used, &used_rect);
2061 /* Find the first free area (yes, this isn't exactly pretty, but
2062 * it works). If you know a better (fast!) algorithm, let me know!
2066 if (o_iconify_start.int_value == CORNER_TOP_RIGHT ||
2067 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2068 dx = -SEARCH_STEP;
2070 if (o_iconify_start.int_value == CORNER_BOTTOM_LEFT ||
2071 o_iconify_start.int_value == CORNER_BOTTOM_RIGHT)
2072 dy = -SEARCH_STEP;
2074 if (o_iconify_dir.int_value == DIR_VERT)
2076 search_free(rect, used,
2077 &rect->x, dx, screen_width - rect->width,
2078 &rect->y, dy, screen_height - rect->height);
2080 else
2082 search_free(rect, used,
2083 &rect->y, dy, screen_height - rect->height,
2084 &rect->x, dx, screen_width - rect->width);
2087 gdk_region_destroy(used);
2089 if (rect->x == -1)
2091 rect->x = 0;
2092 rect->y = 0;
2096 /* Icon's size, shape or appearance has changed - update the display */
2097 static void pinboard_reshape_icon(Icon *icon)
2099 PinIcon *pi = (PinIcon *) icon;
2100 int x = pi->x, y = pi->y;
2102 set_size_and_style(pi);
2103 offset_from_centre(pi, &x, &y);
2105 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
2107 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
2108 pi->win, x, y);