r1476: Options to forward button-3 clicks on the pinboard to the window manager
[rox-filer.git] / ROX-Filer / src / pinboard.c
blob65e949e185bb5aef39e0bcc6384f0db55b61e2b3
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 <gtk/gtkinvisible.h>
33 #include <stdlib.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"
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 static PinIcon *current_wink_icon = NULL;
107 static gint wink_timeout;
109 /* Used for the text colours (only) in the icons (and tasklist windows) */
110 GdkColor pin_text_fg_col, pin_text_bg_col;
112 /* Style that all the icons should use. NULL => regenerate from text_fg/bg */
113 static GtkStyle *pinicon_style = NULL;
115 Pinboard *current_pinboard = NULL;
116 static gint loading_pinboard = 0; /* Non-zero => loading */
118 /* The Icon that was used to start the current drag, if any */
119 Icon *pinboard_drag_in_progress = NULL;
121 typedef enum {
122 TEXT_BG_NONE = 0,
123 TEXT_BG_OUTLINE = 1,
124 TEXT_BG_SOLID = 2,
125 } TextBgType;
127 static Option o_pinboard_clamp_icons, o_pinboard_grid_step;
128 static Option o_pinboard_fg_colour, o_pinboard_bg_colour;
129 static Option o_pinboard_tasklist, o_forward_button_3;
131 /* Static prototypes */
132 static GType pin_icon_get_type(void);
133 static void set_size_and_style(PinIcon *pi);
134 static gint stop_expose(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
135 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
136 static gint end_wink(gpointer data);
137 static gboolean button_release_event(GtkWidget *widget,
138 GdkEventButton *event,
139 PinIcon *pi);
140 static gboolean enter_notify(GtkWidget *widget,
141 GdkEventCrossing *event,
142 PinIcon *pi);
143 static gboolean button_press_event(GtkWidget *widget,
144 GdkEventButton *event,
145 PinIcon *pi);
146 static gint icon_motion_notify(GtkWidget *widget,
147 GdkEventMotion *event,
148 PinIcon *pi);
149 static const char *pin_from_file(gchar *line);
150 static void snap_to_grid(int *x, int *y);
151 static void offset_from_centre(PinIcon *pi, int *x, int *y);
152 static gboolean drag_motion(GtkWidget *widget,
153 GdkDragContext *context,
154 gint x,
155 gint y,
156 guint time,
157 PinIcon *pi);
158 static void drag_set_pinicon_dest(PinIcon *pi);
159 static void drag_leave(GtkWidget *widget,
160 GdkDragContext *context,
161 guint32 time,
162 PinIcon *pi);
163 static gboolean bg_drag_motion(GtkWidget *widget,
164 GdkDragContext *context,
165 gint x,
166 gint y,
167 guint time,
168 gpointer data);
169 static gboolean bg_drag_leave(GtkWidget *widget,
170 GdkDragContext *context,
171 guint32 time,
172 gpointer data);
173 static gboolean bg_expose(GtkWidget *window,
174 GdkEventExpose *event, gpointer data);
175 static void drag_end(GtkWidget *widget,
176 GdkDragContext *context,
177 PinIcon *pi);
178 static void reshape_all(void);
179 static void pinboard_check_options(void);
180 static void pinboard_load_from_xml(xmlDocPtr doc);
181 static void pinboard_clear(void);
182 static void pinboard_save(void);
183 static PinIcon *pin_icon_new(const char *pathname, const char *name);
184 static void pin_icon_destroyed(PinIcon *pi);
185 static void pin_icon_set_tip(PinIcon *pi);
186 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi);
187 static void create_pinboard_window(Pinboard *pinboard);
188 static void reload_backdrop(Pinboard *pinboard,
189 const gchar *backdrop,
190 BackdropStyle backdrop_style);
191 static void set_backdrop(const gchar *path, BackdropStyle style);
192 void pinboard_reshape_icon(Icon *icon);
193 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi);
194 static void abandon_backdrop_app(Pinboard *pinboard);
195 static void drag_backdrop_dropped(GtkWidget *frame,
196 GdkDragContext *context,
197 gint x,
198 gint y,
199 GtkSelectionData *selection_data,
200 guint drag_info,
201 guint32 time,
202 GtkWidget *dialog);
203 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data);
206 /****************************************************************
207 * EXTERNAL INTERFACE *
208 ****************************************************************/
210 void pinboard_init(void)
212 option_add_string(&o_pinboard_fg_colour, "pinboard_fg_colour", "#fff");
213 option_add_string(&o_pinboard_bg_colour, "pinboard_bg_colour", "#888");
215 option_add_int(&o_pinboard_clamp_icons, "pinboard_clamp_icons", 1);
216 option_add_int(&o_pinboard_grid_step, "pinboard_grid_step",
217 GRID_STEP_COARSE);
218 option_add_int(&o_pinboard_tasklist, "pinboard_tasklist", TRUE);
219 option_add_int(&o_forward_button_3, "pinboard_forward_button_3", FALSE);
221 option_add_notify(pinboard_check_options);
223 gdk_color_parse(o_pinboard_fg_colour.value, &pin_text_fg_col);
224 gdk_color_parse(o_pinboard_bg_colour.value, &pin_text_bg_col);
227 /* Load 'pb_<pinboard>' config file from Choices (if it exists)
228 * and make it the current pinboard.
229 * Any existing pinned items are removed. You must call this
230 * at least once before using the pinboard. NULL disables the
231 * pinboard.
233 void pinboard_activate(const gchar *name)
235 Pinboard *old_board = current_pinboard;
236 guchar *path, *slash;
238 /* Treat an empty name the same as NULL */
239 if (name && !*name)
240 name = NULL;
242 if (old_board)
243 pinboard_clear();
245 if (!name)
247 if (number_of_windows < 1 && gtk_main_level() > 0)
248 gtk_main_quit();
249 return;
252 number_of_windows++;
254 slash = strchr(name, '/');
255 if (slash)
257 if (access(name, F_OK))
258 path = NULL; /* File does not (yet) exist */
259 else
260 path = g_strdup(name);
262 else
264 guchar *leaf;
266 leaf = g_strconcat("pb_", name, NULL);
267 path = choices_find_path_load(leaf, PROJECT);
268 g_free(leaf);
271 current_pinboard = g_new(Pinboard, 1);
272 current_pinboard->name = g_strdup(name);
273 current_pinboard->icons = NULL;
274 current_pinboard->window = NULL;
275 current_pinboard->backdrop = NULL;
276 current_pinboard->backdrop_style = BACKDROP_NONE;
277 current_pinboard->to_backdrop_app = -1;
278 current_pinboard->from_backdrop_app = -1;
279 current_pinboard->input_tag = -1;
280 current_pinboard->input_buffer = NULL;
282 create_pinboard_window(current_pinboard);
284 loading_pinboard++;
285 if (path)
287 xmlDocPtr doc;
288 doc = xmlParseFile(path);
289 if (doc)
291 pinboard_load_from_xml(doc);
292 xmlFreeDoc(doc);
293 reload_backdrop(current_pinboard,
294 current_pinboard->backdrop,
295 current_pinboard->backdrop_style);
297 else
299 parse_file(path, pin_from_file);
300 info_message(_("Your old pinboard file has been "
301 "converted to the new XML format."));
302 pinboard_save();
304 g_free(path);
306 else
307 pinboard_pin(home_dir, "Home",
308 4 + ICON_WIDTH / 2,
309 4 + ICON_HEIGHT / 2);
310 loading_pinboard--;
312 if (o_pinboard_tasklist.int_value)
313 tasklist_set_active(TRUE);
316 /* Return the window of the current pinboard, or NULL.
317 * Used to make sure lowering the panels doesn't lose them...
319 GdkWindow *pinboard_get_window(void)
321 if (current_pinboard)
322 return current_pinboard->window->window;
323 return NULL;
326 const char *pinboard_get_name(void)
328 g_return_val_if_fail(current_pinboard != NULL, NULL);
330 return current_pinboard->name;
333 /* Add widget to the pinboard. Caller is responsible for coping with pinboard
334 * being cleared.
336 void pinboard_add_widget(GtkWidget *widget, int x, int y)
338 g_return_if_fail(current_pinboard != NULL);
340 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), widget, x, y);
343 /* Add a new icon to the background.
344 * 'path' should be an absolute pathname.
345 * 'x' and 'y' are the coordinates of the point in the middle of the text.
346 * 'name' is the name to use. If NULL then the leafname of path is used.
348 * name and path are in UTF-8 for Gtk+-2.0 only.
350 void pinboard_pin(const gchar *path, const gchar *name, int x, int y)
352 GtkWidget *align, *vbox;
353 GdkWindow *events;
354 PinIcon *pi;
355 Icon *icon;
357 g_return_if_fail(path != NULL);
358 g_return_if_fail(current_pinboard != NULL);
360 pi = pin_icon_new(path, name);
361 icon = (Icon *) pi;
362 pi->x = x;
363 pi->y = y;
365 /* This is a bit complicated...
367 * An icon needs to be a NO_WINDOW widget so that the image can
368 * blend with the background (A ParentRelative window also works, but
369 * is slow, causes the xfree86's memory consumption to grow without
370 * bound, and doesn't even get freed when the filer quits!).
372 * However, the icon also needs to have a window, so we get events
373 * delivered correctly. The solution is to float an InputOnly window
374 * over the icon. Since GtkButton works the same way, we just use
375 * that :-)
378 /* Button takes the initial ref of Icon */
379 pi->win = gtk_button_new();
380 gtk_container_set_border_width(GTK_CONTAINER(pi->win), WINK_FRAME);
381 g_signal_connect(pi->win, "expose-event", G_CALLBACK(draw_wink), pi);
382 gtk_button_set_relief(GTK_BUTTON(pi->win), GTK_RELIEF_NONE);
384 vbox = gtk_vbox_new(FALSE, 0);
385 gtk_container_add(GTK_CONTAINER(pi->win), vbox);
387 align = gtk_alignment_new(0.5, 0.5, 0, 0);
388 pi->widget = gtk_hbox_new(FALSE, 0); /* Placeholder */
389 gtk_container_add(GTK_CONTAINER(align), pi->widget);
391 gtk_box_pack_start(GTK_BOX(vbox), align, FALSE, TRUE, 0);
392 drag_set_pinicon_dest(pi);
393 g_signal_connect(pi->win, "drag_data_get",
394 G_CALLBACK(drag_data_get), NULL);
396 pi->label = gtk_label_new(icon->item->leafname);
397 gtk_label_set_line_wrap(GTK_LABEL(pi->label), TRUE);
398 gtk_box_pack_start(GTK_BOX(vbox), pi->label, TRUE, TRUE, 0);
400 gtk_fixed_put(GTK_FIXED(current_pinboard->fixed), pi->win, 0, 0);
402 snap_to_grid(&x, &y);
403 pi->x = x;
404 pi->y = y;
405 gtk_widget_show_all(pi->win);
406 pinboard_reshape_icon((Icon *) pi);
408 gtk_widget_realize(pi->win);
409 events = GTK_BUTTON(pi->win)->event_window;
410 gdk_window_set_events(events,
411 GDK_EXPOSURE_MASK |
412 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
413 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
414 GDK_BUTTON1_MOTION_MASK | GDK_ENTER_NOTIFY_MASK |
415 GDK_BUTTON2_MOTION_MASK | GDK_BUTTON3_MOTION_MASK);
416 g_signal_connect(pi->win, "enter-notify-event",
417 G_CALLBACK(enter_notify), pi);
418 g_signal_connect(pi->win, "button-press-event",
419 G_CALLBACK(button_press_event), pi);
420 g_signal_connect(pi->win, "button-release-event",
421 G_CALLBACK(button_release_event), pi);
422 g_signal_connect(pi->win, "motion-notify-event",
423 G_CALLBACK(icon_motion_notify), pi);
424 g_signal_connect(pi->win, "expose-event",
425 G_CALLBACK(stop_expose), pi);
426 g_signal_connect(pi->widget, "expose-event",
427 G_CALLBACK(draw_icon), pi);
428 g_signal_connect_swapped(pi->win, "destroy",
429 G_CALLBACK(pin_icon_destroyed), pi);
431 current_pinboard->icons = g_list_prepend(current_pinboard->icons, pi);
432 pin_icon_set_tip(pi);
434 if (!loading_pinboard)
435 pinboard_save();
438 /* Remove an icon from the pinboard */
439 /* XXX: use destroy */
440 void pinboard_unpin(PinIcon *pi)
442 g_return_if_fail(pi != NULL);
444 gtk_widget_destroy(pi->win);
445 pinboard_save();
448 /* Put a border around the icon, briefly.
449 * If icon is NULL then cancel any existing wink.
450 * The icon will automatically unhighlight unless timeout is FALSE,
451 * in which case you must call this function again (with NULL or another
452 * icon) to remove the highlight.
454 static void pinboard_wink_item(PinIcon *pi, gboolean timeout)
456 PinIcon *old = current_wink_icon;
458 if (old == pi)
459 return;
461 current_wink_icon = pi;
463 if (old)
465 gtk_widget_queue_draw(old->win);
466 gdk_window_process_updates(old->widget->window, TRUE);
468 if (wink_timeout != -1)
469 gtk_timeout_remove(wink_timeout);
472 if (pi)
474 gtk_widget_queue_draw(pi->win);
475 gdk_window_process_updates(pi->widget->window, TRUE);
477 if (timeout)
478 wink_timeout = gtk_timeout_add(300, end_wink, NULL);
479 else
480 wink_timeout = -1;
484 /* Icon's size, shape or appearance has changed - update the display */
485 void pinboard_reshape_icon(Icon *icon)
487 PinIcon *pi = (PinIcon *) icon;
488 int x = pi->x, y = pi->y;
490 set_size_and_style(pi);
491 offset_from_centre(pi, &x, &y);
493 if (pi->win->allocation.x != x || pi->win->allocation.y != y)
495 fixed_move_fast(GTK_FIXED(current_pinboard->fixed),
496 pi->win, x, y);
500 /* 'app' is saved as the new application to set the backdrop. It will then be
501 * run, and should communicate with the filer as described in the manual.
503 void pinboard_set_backdrop_app(const gchar *app)
505 XMLwrapper *ai;
506 DirItem *item;
507 gboolean can_set;
509 item = diritem_new("");
510 diritem_restat(app, item, NULL);
511 ai = appinfo_get(app, item);
512 diritem_free(item);
514 can_set = ai && xml_get_section(ai, ROX_NS, "CanSetBackdrop") != NULL;
515 if (ai)
516 g_object_unref(ai);
518 if (can_set)
519 set_backdrop(app, BACKDROP_PROGRAM);
520 else
521 delayed_error(_("You can only set the backdrop to an image "
522 "or to a program which knows how to "
523 "manage ROX-Filer's backdrop."));
526 /* Open a dialog box allowing the user to set the backdrop */
527 void pinboard_set_backdrop(void)
529 GtkWidget *dialog, *frame, *label, *radio, *hbox;
530 GtkBox *vbox;
531 GtkTargetEntry targets[] = {
532 {"text/uri-list", 0, TARGET_URI_LIST},
535 dialog = gtk_dialog_new_with_buttons(_("Set backdrop"), NULL,
536 GTK_DIALOG_NO_SEPARATOR,
537 GTK_STOCK_CLEAR, GTK_RESPONSE_NO,
538 GTK_STOCK_OK, GTK_RESPONSE_OK,
539 NULL);
540 vbox = GTK_BOX(GTK_DIALOG(dialog)->vbox);
542 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
544 label = gtk_label_new("Display backdrop image:");
545 gtk_misc_set_padding(GTK_MISC(label), 4, 0);
546 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
547 gtk_box_pack_start(vbox, label, TRUE, TRUE, 4);
549 /* The Centred, Scaled, Tiled radios... */
550 hbox = gtk_hbox_new(TRUE, 2);
551 gtk_box_pack_start(vbox, hbox, TRUE, TRUE, 4);
553 radio = gtk_radio_button_new_with_label(NULL, _("Centred"));
554 g_object_set_data(G_OBJECT(dialog), "radio_centred", radio);
555 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
557 radio = gtk_radio_button_new_with_label(
558 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio)),
559 _("Scaled"));
560 g_object_set_data(G_OBJECT(dialog), "radio_scaled", radio);
561 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
563 radio = gtk_radio_button_new_with_label(
564 gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio)),
565 _("Tiled"));
566 gtk_box_pack_start(GTK_BOX(hbox), radio, FALSE, TRUE, 0);
568 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUE);
570 /* The drop area... */
571 frame = gtk_frame_new(NULL);
572 gtk_box_pack_start(vbox, frame, TRUE, TRUE, 4);
573 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
574 gtk_container_set_border_width(GTK_CONTAINER(frame), 4);
576 gtk_drag_dest_set(frame, GTK_DEST_DEFAULT_ALL,
577 targets, sizeof(targets) / sizeof(*targets),
578 GDK_ACTION_COPY);
579 g_signal_connect(frame, "drag_data_received",
580 G_CALLBACK(drag_backdrop_dropped), dialog);
582 label = gtk_label_new(_("Drop an image here"));
583 gtk_misc_set_padding(GTK_MISC(label), 10, 20);
584 gtk_container_add(GTK_CONTAINER(frame), label);
586 g_signal_connect(dialog, "response",
587 G_CALLBACK(backdrop_response), NULL);
588 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
590 gtk_widget_show_all(dialog);
593 /****************************************************************
594 * INTERNAL FUNCTIONS *
595 ****************************************************************/
597 static void backdrop_response(GtkWidget *dialog, gint response, gpointer data)
599 if (response == GTK_RESPONSE_NO)
600 set_backdrop(NULL, BACKDROP_NONE);
602 gtk_widget_destroy(dialog);
605 static void drag_backdrop_dropped(GtkWidget *frame,
606 GdkDragContext *context,
607 gint x,
608 gint y,
609 GtkSelectionData *selection_data,
610 guint drag_info,
611 guint32 time,
612 GtkWidget *dialog)
614 struct stat info;
615 const gchar *path = NULL;
616 GList *uris;
618 if (!selection_data->data)
619 return; /* Timeout? */
621 uris = uri_list_to_glist(selection_data->data);
623 if (g_list_length(uris) == 1)
624 path = get_local_path((guchar *) uris->data);
625 g_list_free(uris);
627 if (!path)
629 delayed_error(
630 _("You should drop a single (local) image file "
631 "onto the drop box - that image will be "
632 "used for the desktop background. You can also "
633 "drag certain applications onto this box."));
634 return;
637 if (mc_stat(path, &info))
639 delayed_error(
640 _("Can't access '%s':\n%s"), path,
641 g_strerror(errno));
642 return;
645 if (S_ISDIR(info.st_mode))
647 /* Use this program to set the backdrop */
648 pinboard_set_backdrop_app(path);
650 else if (S_ISREG(info.st_mode))
652 GtkWidget *radio;
654 radio = g_object_get_data(G_OBJECT(dialog), "radio_scaled");
655 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)))
657 set_backdrop(path, BACKDROP_SCALE);
658 return;
661 radio = g_object_get_data(G_OBJECT(dialog), "radio_centred");
662 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)))
664 set_backdrop(path, BACKDROP_CENTRE);
665 return;
668 set_backdrop(path, BACKDROP_TILE);
670 else
671 delayed_error(_("Only files (and certain applications) can be "
672 "used to set the background image."));
675 static void pinboard_check_options(void)
677 GdkColor n_fg, n_bg;
679 gdk_color_parse(o_pinboard_fg_colour.value, &n_fg);
680 gdk_color_parse(o_pinboard_bg_colour.value, &n_bg);
682 tasklist_set_active(o_pinboard_tasklist.int_value && current_pinboard);
684 if (gdk_color_equal(&n_fg, &pin_text_fg_col) == 0 ||
685 gdk_color_equal(&n_bg, &pin_text_bg_col) == 0)
687 pin_text_fg_col = n_fg;
688 pin_text_bg_col = n_bg;
690 if (pinicon_style)
692 g_object_unref(G_OBJECT(pinicon_style));
693 pinicon_style = NULL;
696 if (current_pinboard)
697 reshape_all();
699 tasklist_style_changed();
703 static gint end_wink(gpointer data)
705 pinboard_wink_item(NULL, FALSE);
706 return FALSE;
709 /* Updates the width, height, name_width and label fields, and resizes the
710 * window. Also sets the style to pinicon_style, generating it if needed.
712 static void set_size_and_style(PinIcon *pi)
714 Icon *icon = (Icon *) pi;
715 MaskedPixmap *image = icon->item->image;
716 int iwidth = image->width;
717 int iheight = image->height;
719 gtk_widget_modify_fg(pi->label, GTK_STATE_PRELIGHT, &pin_text_fg_col);
720 gtk_widget_modify_bg(pi->label, GTK_STATE_PRELIGHT, &pin_text_bg_col);
721 gtk_widget_modify_fg(pi->label, GTK_STATE_NORMAL, &pin_text_fg_col);
722 gtk_widget_modify_bg(pi->label, GTK_STATE_NORMAL, &pin_text_bg_col);
724 gtk_label_set_text(GTK_LABEL(pi->label), icon->item->leafname);
726 gtk_widget_set_size_request(pi->widget, iwidth, iheight);
729 /* Don't draw the normal button effect */
730 static gint stop_expose(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
732 static GtkWidgetClass *parent_class = NULL;
734 if (!parent_class)
736 gpointer c = ((GTypeInstance *) widget)->g_class;
737 parent_class = (GtkWidgetClass *) g_type_class_peek_parent(c);
740 (parent_class->expose_event)(widget, event);
741 return TRUE;
744 static gint draw_icon(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
746 Icon *icon = (Icon *) pi;
747 DirItem *item = icon->item;
748 MaskedPixmap *image = item->image;
749 int iwidth = image->width;
750 int iheight = image->height;
751 int x, y;
752 GtkStateType state = icon->selected ? GTK_STATE_SELECTED
753 : GTK_STATE_NORMAL;
756 x = widget->allocation.x;
757 y = widget->allocation.y;
759 gdk_pixbuf_render_to_drawable_alpha(
760 icon->selected ? image->pixbuf_lit : image->pixbuf,
761 widget->window,
762 0, 0, /* src */
763 x, y, /* dest */
764 iwidth, iheight,
765 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
766 GDK_RGB_DITHER_NORMAL, 0, 0);
768 if (item->flags & ITEM_FLAG_SYMLINK)
770 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
771 widget->window,
772 0, 0, /* src */
773 x, y,
774 -1, -1,
775 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
776 GDK_RGB_DITHER_NORMAL, 0, 0);
778 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
780 MaskedPixmap *mp = item->flags & ITEM_FLAG_MOUNTED
781 ? im_mounted
782 : im_unmounted;
784 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
785 widget->window,
786 0, 0, /* src */
787 x, y,
788 -1, -1,
789 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
790 GDK_RGB_DITHER_NORMAL, 0, 0);
793 if (icon->selected)
795 gtk_paint_flat_box(pi->label->style, pi->label->window,
796 state,
797 GTK_SHADOW_NONE,
798 NULL, pi->label, "text",
799 pi->label->allocation.x,
800 pi->label->allocation.y,
801 pi->label->allocation.width,
802 pi->label->allocation.height);
805 return FALSE;
808 static gint draw_wink(GtkWidget *widget, GdkEventExpose *event, PinIcon *pi)
810 gint x, y, width, height;
812 if (current_wink_icon != pi)
813 return FALSE;
815 x = widget->allocation.x;
816 y = widget->allocation.y;
817 width = widget->allocation.width;
818 height = widget->allocation.height;
820 gdk_draw_rectangle(widget->window,
821 pi->widget->style->white_gc,
822 FALSE,
823 x, y, width - 1, height - 1);
824 gdk_draw_rectangle(widget->window,
825 pi->widget->style->black_gc,
826 FALSE,
827 x + 1, y + 1, width - 3, height - 3);
829 return FALSE;
832 static gboolean enter_notify(GtkWidget *widget,
833 GdkEventCrossing *event,
834 PinIcon *pi)
836 icon_may_update((Icon *) pi);
838 return FALSE;
841 static void perform_action(PinIcon *pi, GdkEventButton *event)
843 BindAction action;
844 Icon *icon = (Icon *) pi;
846 action = bind_lookup_bev(pi ? BIND_PINBOARD_ICON : BIND_PINBOARD,
847 event);
849 /* Actions that can happen with or without an icon */
850 switch (action)
852 case ACT_CLEAR_SELECTION:
853 icon_select_only(NULL);
854 return;
855 case ACT_POPUP_MENU:
856 dnd_motion_ungrab();
857 pinboard_show_menu(event, pi);
858 return;
859 case ACT_IGNORE:
860 return;
861 default:
862 break;
865 g_return_if_fail(pi != NULL);
867 switch (action)
869 case ACT_OPEN_ITEM:
870 dnd_motion_ungrab();
871 pinboard_wink_item(pi, TRUE);
872 if (event->type == GDK_2BUTTON_PRESS)
873 icon_set_selected(icon, FALSE);
874 run_diritem(icon->path, icon->item, NULL, NULL, FALSE);
875 break;
876 case ACT_EDIT_ITEM:
877 dnd_motion_ungrab();
878 pinboard_wink_item(pi, TRUE);
879 if (event->type == GDK_2BUTTON_PRESS)
880 icon_set_selected(icon, FALSE);
881 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
882 break;
883 case ACT_PRIME_AND_SELECT:
884 if (!icon->selected)
885 icon_select_only(icon);
886 dnd_motion_start(MOTION_READY_FOR_DND);
887 break;
888 case ACT_PRIME_AND_TOGGLE:
889 icon_set_selected(icon, !icon->selected);
890 dnd_motion_start(MOTION_READY_FOR_DND);
891 break;
892 case ACT_PRIME_FOR_DND:
893 dnd_motion_start(MOTION_READY_FOR_DND);
894 break;
895 case ACT_TOGGLE_SELECTED:
896 icon_set_selected(icon, !icon->selected);
897 break;
898 case ACT_SELECT_EXCL:
899 icon_select_only(icon);
900 break;
901 default:
902 g_warning("Unsupported action : %d\n", action);
903 break;
907 static void forward_to_root(GdkEventButton *event)
909 XButtonEvent xev;
911 if (event->type == GDK_BUTTON_PRESS)
913 xev.type = ButtonPress;
914 XUngrabPointer(gdk_display, event->time);
916 else
917 xev.type = ButtonRelease;
919 xev.window = gdk_x11_get_default_root_xwindow();
920 xev.root = xev.window;
921 xev.subwindow = None;
922 xev.time = event->time;
923 xev.x = event->x;
924 xev.y = event->y;
925 xev.x_root = event->x_root;
926 xev.y_root = event->y_root;
927 xev.state = event->state;
928 xev.button = event->button;
929 xev.same_screen = True;
931 XSendEvent(gdk_display, xev.window, False,
932 ButtonPressMask | ButtonReleaseMask, (XEvent *) &xev);
935 #define FORWARDED_BUTTON(pi, button) ((button) == 2 || \
936 ((button) == 3 && o_forward_button_3.int_value && !pi))
938 /* pi is NULL if this is a root event */
939 static gboolean button_release_event(GtkWidget *widget,
940 GdkEventButton *event,
941 PinIcon *pi)
943 if (FORWARDED_BUTTON(pi, event->button))
944 forward_to_root(event);
945 else if (dnd_motion_release(event))
946 return TRUE;
948 perform_action(pi, event);
950 return TRUE;
953 /* pi is NULL if this is a root event */
954 static gboolean button_press_event(GtkWidget *widget,
955 GdkEventButton *event,
956 PinIcon *pi)
958 /* Just in case we've jumped in front of everything... */
959 gdk_window_lower(current_pinboard->window->window);
961 if (FORWARDED_BUTTON(pi, event->button))
962 forward_to_root(event);
963 else if (dnd_motion_press(widget, event))
964 perform_action(pi, event);
966 return TRUE;
969 static void start_drag(PinIcon *pi, GdkEventMotion *event)
971 GtkWidget *widget = pi->win;
972 Icon *icon = (Icon *) pi;
974 if (!icon->selected)
976 tmp_icon_selected = TRUE;
977 icon_select_only(icon);
980 g_return_if_fail(icon_selection != NULL);
982 pinboard_drag_in_progress = icon;
984 if (icon_selection->next == NULL)
985 drag_one_item(widget, event, icon->path, icon->item, NULL);
986 else
988 guchar *uri_list;
990 uri_list = icon_create_uri_list();
991 drag_selection(widget, event, uri_list);
992 g_free(uri_list);
996 /* An icon is being dragged around... */
997 static gint icon_motion_notify(GtkWidget *widget,
998 GdkEventMotion *event,
999 PinIcon *pi)
1001 if (motion_state == MOTION_READY_FOR_DND)
1003 if (dnd_motion_moved(event))
1004 start_drag(pi, event);
1005 return TRUE;
1008 return FALSE;
1011 static void backdrop_from_xml(xmlNode *node)
1013 gchar *style;
1015 g_free(current_pinboard->backdrop);
1016 current_pinboard->backdrop = xmlNodeGetContent(node);
1018 style = xmlGetProp(node, "style");
1020 if (style)
1022 current_pinboard->backdrop_style =
1023 g_strcasecmp(style, "Tiled") == 0 ? BACKDROP_TILE :
1024 g_strcasecmp(style, "Scaled") == 0 ? BACKDROP_SCALE :
1025 g_strcasecmp(style, "Centred") == 0 ? BACKDROP_CENTRE :
1026 g_strcasecmp(style, "Program") == 0 ? BACKDROP_PROGRAM :
1027 BACKDROP_NONE;
1028 g_free(style);
1030 else
1031 current_pinboard->backdrop_style = BACKDROP_TILE;
1034 /* Create one pinboard icon for each icon in the doc */
1035 static void pinboard_load_from_xml(xmlDocPtr doc)
1037 xmlNodePtr node, root;
1038 char *tmp, *label, *path;
1039 int x, y;
1041 root = xmlDocGetRootElement(doc);
1043 for (node = root->xmlChildrenNode; node; node = node->next)
1045 if (node->type != XML_ELEMENT_NODE)
1046 continue;
1047 if (strcmp(node->name, "backdrop") == 0)
1049 backdrop_from_xml(node);
1050 continue;
1052 if (strcmp(node->name, "icon") != 0)
1053 continue;
1055 tmp = xmlGetProp(node, "x");
1056 if (!tmp)
1057 continue;
1058 x = atoi(tmp);
1059 g_free(tmp);
1061 tmp = xmlGetProp(node, "y");
1062 if (!tmp)
1063 continue;
1064 y = atoi(tmp);
1065 g_free(tmp);
1067 label = xmlGetProp(node, "label");
1068 if (!label)
1069 label = g_strdup("<missing label>");
1070 path = xmlNodeGetContent(node);
1071 if (!path)
1072 path = g_strdup("<missing path>");
1074 pinboard_pin(path, label, x, y);
1076 g_free(path);
1077 g_free(label);
1081 /* Called for each line in the pinboard file while loading a new board.
1082 * Only used for old-format files when converting to XML.
1084 static const char *pin_from_file(gchar *line)
1086 gchar *leaf = NULL;
1087 int x, y, n;
1089 if (*line == '<')
1091 gchar *end;
1093 end = strchr(line + 1, '>');
1094 if (!end)
1095 return _("Missing '>' in icon label");
1097 leaf = g_strndup(line + 1, end - line - 1);
1099 line = end + 1;
1101 while (isspace(*line))
1102 line++;
1103 if (*line != ',')
1104 return _("Missing ',' after icon label");
1105 line++;
1108 if (sscanf(line, " %d , %d , %n", &x, &y, &n) < 2)
1109 return NULL; /* Ignore format errors */
1111 pinboard_pin(line + n, leaf, x, y);
1113 g_free(leaf);
1115 return NULL;
1118 /* Write the current state of the pinboard to the current pinboard file */
1119 static void pinboard_save(void)
1121 guchar *save = NULL;
1122 guchar *save_new = NULL;
1123 GList *next;
1124 xmlDocPtr doc = NULL;
1125 xmlNodePtr root;
1127 g_return_if_fail(current_pinboard != NULL);
1129 if (strchr(current_pinboard->name, '/'))
1130 save = g_strdup(current_pinboard->name);
1131 else
1133 guchar *leaf;
1135 leaf = g_strconcat("pb_", current_pinboard->name, NULL);
1136 save = choices_find_path_save(leaf, PROJECT, TRUE);
1137 g_free(leaf);
1140 if (!save)
1141 return;
1143 doc = xmlNewDoc("1.0");
1144 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "pinboard", NULL));
1146 root = xmlDocGetRootElement(doc);
1148 if (current_pinboard->backdrop)
1150 BackdropStyle style = current_pinboard->backdrop_style;
1151 xmlNodePtr tree;
1153 tree = xmlNewTextChild(root, NULL, "backdrop",
1154 current_pinboard->backdrop);
1155 xmlSetProp(tree, "style",
1156 style == BACKDROP_TILE ? "Tiled" :
1157 style == BACKDROP_CENTRE ? "Centred" :
1158 style == BACKDROP_SCALE ? "Scaled" :
1159 "Program");
1162 for (next = current_pinboard->icons; next; next = next->next)
1164 xmlNodePtr tree;
1165 PinIcon *pi = (PinIcon *) next->data;
1166 Icon *icon = (Icon *) pi;
1167 char *tmp;
1169 tree = xmlNewTextChild(root, NULL, "icon", icon->src_path);
1171 tmp = g_strdup_printf("%d", pi->x);
1172 xmlSetProp(tree, "x", tmp);
1173 g_free(tmp);
1175 tmp = g_strdup_printf("%d", pi->y);
1176 xmlSetProp(tree, "y", tmp);
1177 g_free(tmp);
1179 xmlSetProp(tree, "label", icon->item->leafname);
1182 save_new = g_strconcat(save, ".new", NULL);
1183 if (save_xml_file(doc, save_new) || rename(save_new, save))
1184 delayed_error(_("Error saving pinboard %s: %s"),
1185 save, g_strerror(errno));
1186 g_free(save_new);
1188 g_free(save);
1189 if (doc)
1190 xmlFreeDoc(doc);
1193 static void snap_to_grid(int *x, int *y)
1195 int step = o_pinboard_grid_step.int_value;
1197 *x = ((*x + step / 2) / step) * step;
1198 *y = ((*y + step / 2) / step) * step;
1201 /* Convert (x,y) from a centre point to a window position */
1202 static void offset_from_centre(PinIcon *pi, int *x, int *y)
1204 gboolean clamp = o_pinboard_clamp_icons.int_value;
1205 GtkRequisition req;
1207 gtk_widget_size_request(pi->win, &req);
1209 *x -= req.width >> 1;
1210 *y -= req.height >> 1;
1211 *x = CLAMP(*x, 0, screen_width - (clamp ? req.width : 0));
1212 *y = CLAMP(*y, 0, screen_height - (clamp ? req.height : 0));
1215 /* Same as drag_set_dest(), but for pinboard icons */
1216 static void drag_set_pinicon_dest(PinIcon *pi)
1218 GtkObject *obj = GTK_OBJECT(pi->win);
1220 make_drop_target(pi->win, 0);
1222 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1223 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1224 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1227 /* Called during the drag when the mouse is in a widget registered
1228 * as a drop target. Returns TRUE if we can accept the drop.
1230 static gboolean drag_motion(GtkWidget *widget,
1231 GdkDragContext *context,
1232 gint x,
1233 gint y,
1234 guint time,
1235 PinIcon *pi)
1237 GdkDragAction action = context->suggested_action;
1238 char *type = NULL;
1239 Icon *icon = (Icon *) pi;
1240 DirItem *item = icon->item;
1242 if (gtk_drag_get_source_widget(context) == widget)
1243 goto out; /* Can't drag something to itself! */
1245 if (icon->selected)
1246 goto out; /* Can't drag a selection to itself */
1248 type = dnd_motion_item(context, &item);
1250 if (!item)
1251 type = NULL;
1252 out:
1253 /* We actually must pretend to accept the drop, even if the
1254 * directory isn't writeable, so that the spring-opening
1255 * thing works.
1258 /* Don't allow drops to non-writeable directories */
1259 if (o_dnd_spring_open.int_value == FALSE &&
1260 type == drop_dest_dir &&
1261 access(icon->path, W_OK) != 0)
1263 type = NULL;
1266 g_dataset_set_data(context, "drop_dest_type", type);
1267 if (type)
1269 gdk_drag_status(context, action, time);
1270 g_dataset_set_data_full(context, "drop_dest_path",
1271 g_strdup(icon->path), g_free);
1272 if (type == drop_dest_dir)
1273 dnd_spring_load(context, NULL);
1275 pinboard_wink_item(pi, FALSE);
1277 else
1278 gdk_drag_status(context, 0, time);
1280 /* Always return TRUE to stop the pinboard getting the events */
1281 return TRUE;
1284 static gboolean pinboard_shadow = FALSE;
1285 static gint shadow_x, shadow_y;
1286 #define SHADOW_SIZE (ICON_WIDTH)
1288 static gboolean bg_expose(GtkWidget *widget,
1289 GdkEventExpose *event, gpointer data)
1291 if (!pinboard_shadow)
1292 return FALSE;
1294 gdk_draw_rectangle(widget->window,
1295 widget->style->white_gc, FALSE,
1296 shadow_x, shadow_y,
1297 SHADOW_SIZE, SHADOW_SIZE);
1298 gdk_draw_rectangle(widget->window,
1299 widget->style->black_gc, FALSE,
1300 shadow_x + 1, shadow_y + 1,
1301 SHADOW_SIZE - 2, SHADOW_SIZE - 2);
1303 return FALSE;
1306 /* Draw a 'shadow' under an icon being dragged, showing where
1307 * it will land.
1309 static void pinboard_set_shadow(gboolean on)
1311 GdkRectangle area;
1313 if (pinboard_shadow)
1315 area.x = shadow_x;
1316 area.y = shadow_y;
1317 area.width = SHADOW_SIZE + 1;
1318 area.height = SHADOW_SIZE + 1;
1320 gdk_window_invalidate_rect(current_pinboard->window->window,
1321 &area, TRUE);
1324 if (on)
1326 int old_x = shadow_x, old_y = shadow_y;
1328 gdk_window_get_pointer(current_pinboard->fixed->window,
1329 &shadow_x, &shadow_y, NULL);
1330 snap_to_grid(&shadow_x, &shadow_y);
1331 shadow_x -= SHADOW_SIZE / 2;
1332 shadow_y -= SHADOW_SIZE / 2;
1335 if (pinboard_shadow && shadow_x == old_x && shadow_y == old_y)
1336 return;
1338 area.x = shadow_x;
1339 area.y = shadow_y;
1340 area.width = SHADOW_SIZE + 1;
1341 area.height = SHADOW_SIZE + 1;
1343 gdk_window_invalidate_rect(current_pinboard->window->window,
1344 &area, TRUE);
1347 pinboard_shadow = on;
1350 /* Called when dragging some pinboard icons finishes */
1351 void pinboard_move_icons(void)
1353 int x = shadow_x, y = shadow_y;
1354 PinIcon *pi = (PinIcon *) pinboard_drag_in_progress;
1355 int width, height;
1357 g_return_if_fail(pi != NULL);
1359 x += SHADOW_SIZE / 2;
1360 y += SHADOW_SIZE / 2;
1361 snap_to_grid(&x, &y);
1363 if (pi->x == x && pi->y == y)
1364 return;
1366 pi->x = x;
1367 pi->y = y;
1368 gdk_drawable_get_size(pi->win->window, &width, &height);
1369 offset_from_centre(pi, &x, &y);
1371 fixed_move_fast(GTK_FIXED(current_pinboard->fixed), pi->win, x, y);
1373 pinboard_save();
1376 static void drag_leave(GtkWidget *widget,
1377 GdkDragContext *context,
1378 guint32 time,
1379 PinIcon *pi)
1381 pinboard_wink_item(NULL, FALSE);
1382 dnd_spring_abort();
1385 static gboolean bg_drag_leave(GtkWidget *widget,
1386 GdkDragContext *context,
1387 guint32 time,
1388 gpointer data)
1390 pinboard_set_shadow(FALSE);
1391 return TRUE;
1394 static gboolean bg_drag_motion(GtkWidget *widget,
1395 GdkDragContext *context,
1396 gint x,
1397 gint y,
1398 guint time,
1399 gpointer data)
1401 /* Dragging from the pinboard to the pinboard is not allowed */
1403 if (!provides(context, text_uri_list))
1404 return FALSE;
1406 pinboard_set_shadow(TRUE);
1408 gdk_drag_status(context,
1409 context->suggested_action == GDK_ACTION_ASK
1410 ? GDK_ACTION_LINK : context->suggested_action,
1411 time);
1412 return TRUE;
1415 static void drag_end(GtkWidget *widget,
1416 GdkDragContext *context,
1417 PinIcon *pi)
1419 pinboard_drag_in_progress = NULL;
1420 if (tmp_icon_selected)
1422 icon_select_only(NULL);
1423 tmp_icon_selected = FALSE;
1427 /* Something which affects all the icons has changed - reshape
1428 * and redraw all of them.
1430 static void reshape_all(void)
1432 GList *next;
1434 g_return_if_fail(current_pinboard != NULL);
1436 for (next = current_pinboard->icons; next; next = next->next)
1438 Icon *icon = (Icon *) next->data;
1439 pinboard_reshape_icon(icon);
1443 /* Turns off the pinboard. Does not call gtk_main_quit. */
1444 static void pinboard_clear(void)
1446 GList *next;
1448 g_return_if_fail(current_pinboard != NULL);
1450 tasklist_set_active(FALSE);
1452 next = current_pinboard->icons;
1453 while (next)
1455 PinIcon *pi = (PinIcon *) next->data;
1457 next = next->next;
1459 gtk_widget_destroy(pi->win);
1462 gtk_widget_destroy(current_pinboard->window);
1464 abandon_backdrop_app(current_pinboard);
1466 g_free(current_pinboard->name);
1467 g_free(current_pinboard);
1468 current_pinboard = NULL;
1470 number_of_windows--;
1473 static gpointer parent_class;
1475 static void pin_icon_destroy(Icon *icon)
1477 PinIcon *pi = (PinIcon *) icon;
1479 g_return_if_fail(pi->win != NULL);
1481 gtk_widget_destroy(pi->win);
1484 static void pinboard_remove_items(void)
1486 g_return_if_fail(icon_selection != NULL);
1488 while (icon_selection)
1489 icon_destroy((Icon *) icon_selection->data);
1491 pinboard_save();
1494 static void pin_icon_update(Icon *icon)
1496 pinboard_reshape_icon(icon);
1497 pinboard_save();
1500 static gboolean pin_icon_same_group(Icon *icon, Icon *other)
1502 return IS_PIN_ICON(other);
1505 static void pin_icon_class_init(gpointer gclass, gpointer data)
1507 IconClass *icon = (IconClass *) gclass;
1509 parent_class = g_type_class_peek_parent(gclass);
1511 icon->destroy = pin_icon_destroy;
1512 icon->redraw = pinboard_reshape_icon;
1513 icon->update = pin_icon_update;
1514 icon->remove_items = pinboard_remove_items;
1515 icon->same_group = pin_icon_same_group;
1518 static void pin_icon_init(GTypeInstance *object, gpointer gclass)
1522 static GType pin_icon_get_type(void)
1524 static GType type = 0;
1526 if (!type)
1528 static const GTypeInfo info =
1530 sizeof (PinIconClass),
1531 NULL, /* base_init */
1532 NULL, /* base_finalise */
1533 pin_icon_class_init,
1534 NULL, /* class_finalise */
1535 NULL, /* class_data */
1536 sizeof(PinIcon),
1537 0, /* n_preallocs */
1538 pin_icon_init
1541 type = g_type_register_static(icon_get_type(),
1542 "PinIcon", &info, 0);
1545 return type;
1548 static PinIcon *pin_icon_new(const char *pathname, const char *name)
1550 PinIcon *pi;
1551 Icon *icon;
1553 pi = g_object_new(pin_icon_get_type(), NULL);
1554 icon = (Icon *) pi;
1556 icon_set_path(icon, pathname, name);
1558 return pi;
1561 /* Called when the window widget is somehow destroyed */
1562 static void pin_icon_destroyed(PinIcon *pi)
1564 g_return_if_fail(pi->win != NULL);
1566 pi->win = NULL;
1568 pinboard_wink_item(NULL, FALSE);
1570 if (pinboard_drag_in_progress == (Icon *) pi)
1571 pinboard_drag_in_progress = NULL;
1573 if (current_pinboard)
1574 current_pinboard->icons =
1575 g_list_remove(current_pinboard->icons, pi);
1577 g_object_unref(pi);
1580 /* Set the tooltip */
1581 static void pin_icon_set_tip(PinIcon *pi)
1583 XMLwrapper *ai;
1584 xmlNode *node;
1585 Icon *icon = (Icon *) pi;
1587 g_return_if_fail(pi != NULL);
1589 ai = appinfo_get(icon->path, icon->item);
1591 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
1593 guchar *str;
1594 str = xmlNodeListGetString(node->doc,
1595 node->xmlChildrenNode, 1);
1596 if (str)
1598 gtk_tooltips_set_tip(tooltips, pi->win, str, NULL);
1599 g_free(str);
1602 else
1603 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
1605 if (ai)
1606 g_object_unref(ai);
1609 static void pinboard_show_menu(GdkEventButton *event, PinIcon *pi)
1611 int pos[3];
1613 pos[0] = event->x_root;
1614 pos[1] = event->y_root;
1615 pos[2] = 1;
1617 icon_prepare_menu((Icon *) pi, TRUE);
1619 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
1620 position_menu,
1621 (gpointer) pos, event->button, event->time);
1624 static void create_pinboard_window(Pinboard *pinboard)
1626 GtkWidget *win;
1628 g_return_if_fail(pinboard->window == NULL);
1630 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1631 gtk_widget_set_app_paintable(win, TRUE);
1632 gtk_widget_set_name(win, "rox-pinboard");
1633 pinboard->window = win;
1634 pinboard->fixed = gtk_fixed_new();
1635 gtk_container_add(GTK_CONTAINER(win), pinboard->fixed);
1637 gtk_window_set_wmclass(GTK_WINDOW(win), "ROX-Pinboard", PROJECT);
1639 gtk_widget_set_size_request(win, screen_width, screen_height);
1640 gtk_widget_realize(win);
1641 gtk_window_move(GTK_WINDOW(win), 0, 0);
1642 make_panel_window(win);
1644 /* TODO: Use gdk function when it supports this type */
1646 GdkAtom desktop_type;
1648 desktop_type = gdk_atom_intern("_NET_WM_WINDOW_TYPE_DESKTOP",
1649 FALSE);
1650 gdk_property_change(win->window,
1651 gdk_atom_intern("_NET_WM_WINDOW_TYPE", FALSE),
1652 gdk_atom_intern("ATOM", FALSE), 32,
1653 GDK_PROP_MODE_REPLACE, (guchar *) &desktop_type, 1);
1656 gtk_widget_add_events(win,
1657 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1658 GDK_EXPOSURE_MASK);
1659 g_signal_connect(win, "button-press-event",
1660 G_CALLBACK(button_press_event), NULL);
1661 g_signal_connect(win, "button-release-event",
1662 G_CALLBACK(button_release_event), NULL);
1663 g_signal_connect(pinboard->fixed, "expose_event",
1664 G_CALLBACK(bg_expose), NULL);
1666 /* Drag and drop handlers */
1667 drag_set_pinboard_dest(win);
1668 g_signal_connect(win, "drag_motion", G_CALLBACK(bg_drag_motion), NULL);
1669 g_signal_connect(win, "drag_leave", G_CALLBACK(bg_drag_leave), NULL);
1671 gtk_widget_show_all(win);
1672 gdk_window_lower(win->window);
1675 /* Load image 'path' and scale according to 'style' */
1676 static GdkPixmap *load_backdrop(const gchar *path, BackdropStyle style)
1678 GdkPixmap *pixmap;
1679 GdkPixbuf *pixbuf;
1680 GError *error = NULL;
1682 pixbuf = gdk_pixbuf_new_from_file(path, &error);
1683 if (error)
1685 delayed_error(_("Error loading backdrop image:\n%s\n"
1686 "Backdrop removed."),
1687 error->message);
1688 g_error_free(error);
1689 set_backdrop(NULL, BACKDROP_NONE);
1690 return NULL;
1693 if (style == BACKDROP_SCALE)
1695 GdkPixbuf *old = pixbuf;
1697 pixbuf = gdk_pixbuf_scale_simple(old,
1698 screen_width, screen_height,
1699 GDK_INTERP_HYPER);
1701 g_object_unref(old);
1703 else if (style == BACKDROP_CENTRE)
1705 GdkPixbuf *old = pixbuf;
1706 int x, y, width, height;
1708 width = gdk_pixbuf_get_width(pixbuf);
1709 height = gdk_pixbuf_get_height(pixbuf);
1711 pixbuf = gdk_pixbuf_new(
1712 gdk_pixbuf_get_colorspace(pixbuf), 0,
1713 8, screen_width, screen_height);
1714 gdk_pixbuf_fill(pixbuf, 0);
1716 x = (screen_width - width) / 2;
1717 y = (screen_height - height) / 2;
1718 x = MAX(x, 0);
1719 y = MAX(y, 0);
1721 gdk_pixbuf_composite(old, pixbuf,
1722 x, y,
1723 MIN(screen_width, width),
1724 MIN(screen_height, height),
1725 x, y, 1, 1,
1726 GDK_INTERP_NEAREST, 255);
1727 g_object_unref(old);
1730 gdk_pixbuf_render_pixmap_and_mask(pixbuf,
1731 &pixmap, NULL, 0);
1732 g_object_unref(pixbuf);
1734 return pixmap;
1737 static void abandon_backdrop_app(Pinboard *pinboard)
1739 g_return_if_fail(pinboard != NULL);
1741 if (pinboard->to_backdrop_app != -1)
1743 close(pinboard->to_backdrop_app);
1744 close(pinboard->from_backdrop_app);
1745 gtk_input_remove(pinboard->input_tag);
1746 g_string_free(pinboard->input_buffer, TRUE);
1747 pinboard->to_backdrop_app = -1;
1748 pinboard->from_backdrop_app = -1;
1749 pinboard->input_tag = -1;
1750 pinboard->input_buffer = NULL;
1753 g_return_if_fail(pinboard->to_backdrop_app == -1);
1754 g_return_if_fail(pinboard->from_backdrop_app == -1);
1755 g_return_if_fail(pinboard->input_tag == -1);
1756 g_return_if_fail(pinboard->input_buffer == NULL);
1759 /* A single line has been read from the child.
1760 * Processes the command, and replies 'ok' (or abandons the child on error).
1762 static void command_from_backdrop_app(Pinboard *pinboard, const gchar *command)
1764 BackdropStyle style;
1765 const char *ok = "ok\n";
1767 if (strncmp(command, "tile ", 5) == 0)
1769 style = BACKDROP_TILE;
1770 command += 5;
1772 else if (strncmp(command, "scale ", 6) == 0)
1774 style = BACKDROP_SCALE;
1775 command += 6;
1777 else if (strncmp(command, "centre ", 7) == 0)
1779 style = BACKDROP_CENTRE;
1780 command += 7;
1782 else
1784 g_warning("Invalid command '%s' from backdrop app\n",
1785 command);
1786 abandon_backdrop_app(pinboard);
1787 return;
1790 reload_backdrop(pinboard, command, style);
1792 while (*ok)
1794 int sent;
1796 sent = write(pinboard->to_backdrop_app, ok, strlen(ok));
1797 if (sent <= 0)
1799 g_warning("command_from_backdrop_app: %s\n",
1800 g_strerror(errno));
1801 abandon_backdrop_app(pinboard);
1802 return;
1804 ok += sent;
1808 static void backdrop_from_child(Pinboard *pinboard,
1809 int src, GdkInputCondition cond)
1811 char buf[256];
1812 int got;
1814 got = read(src, buf, sizeof(buf));
1816 if (got <= 0)
1818 if (got < 0)
1819 g_warning("backdrop_from_child: %s\n",
1820 g_strerror(errno));
1821 abandon_backdrop_app(pinboard);
1822 return;
1825 g_string_append_len(pinboard->input_buffer, buf, got);
1827 while (pinboard->from_backdrop_app != -1)
1829 int len;
1830 char *nl, *command;
1832 nl = strchr(pinboard->input_buffer->str, '\n');
1833 if (!nl)
1834 return; /* Haven't got a whole line yet */
1836 len = nl - pinboard->input_buffer->str;
1837 command = g_strndup(pinboard->input_buffer->str, len);
1838 g_string_erase(pinboard->input_buffer, 0, len + 1);
1840 command_from_backdrop_app(pinboard, command);
1842 g_free(command);
1846 static void reload_backdrop(Pinboard *pinboard,
1847 const gchar *backdrop,
1848 BackdropStyle backdrop_style)
1850 GtkStyle *style;
1852 if (backdrop && backdrop_style == BACKDROP_PROGRAM)
1854 const char *argv[] = {NULL, "--backdrop", NULL};
1855 GError *error = NULL;
1857 g_return_if_fail(pinboard->to_backdrop_app == -1);
1858 g_return_if_fail(pinboard->from_backdrop_app == -1);
1859 g_return_if_fail(pinboard->input_tag == -1);
1860 g_return_if_fail(pinboard->input_buffer == NULL);
1862 argv[0] = make_path(backdrop, "AppRun")->str;
1864 /* Run the program. It'll send us a SOAP message and we'll
1865 * get back here with a different style and image.
1868 if (g_spawn_async_with_pipes(NULL, (gchar **) argv, NULL,
1869 G_SPAWN_DO_NOT_REAP_CHILD |
1870 G_SPAWN_SEARCH_PATH,
1871 NULL, NULL, /* Child setup fn */
1872 NULL, /* Child PID */
1873 &pinboard->to_backdrop_app,
1874 &pinboard->from_backdrop_app,
1875 NULL, /* Standard error */
1876 &error))
1878 pinboard->input_buffer = g_string_new(NULL);
1879 pinboard->input_tag = gtk_input_add_full(
1880 pinboard->from_backdrop_app,
1881 GDK_INPUT_READ,
1882 (GdkInputFunction) backdrop_from_child,
1883 NULL, pinboard, NULL);
1885 else
1887 delayed_error("%s", error ? error->message : "(null)");
1888 g_error_free(error);
1890 return;
1893 /* Note: Copying a style does not ref the pixmaps! */
1895 style = gtk_style_copy(gtk_widget_get_style(pinboard->window));
1896 style->bg_pixmap[GTK_STATE_NORMAL] = NULL;
1898 if (backdrop)
1899 style->bg_pixmap[GTK_STATE_NORMAL] =
1900 load_backdrop(backdrop, backdrop_style);
1902 gtk_widget_set_style(pinboard->window, style);
1904 g_object_unref(style);
1906 gtk_widget_queue_draw(pinboard->window);
1909 /* Set and save (path, style) as the new backdrop.
1910 * If style is BACKDROP_PROGRAM, the program is run to get the backdrop.
1911 * Otherwise, the image is displayed now.
1913 static void set_backdrop(const gchar *path, BackdropStyle style)
1915 g_return_if_fail((path == NULL && style == BACKDROP_NONE) ||
1916 (path != NULL && style != BACKDROP_NONE));
1918 if (!current_pinboard)
1920 if (!path)
1921 return;
1922 pinboard_activate("Default");
1923 delayed_error(_("No pinboard was in use... "
1924 "the 'Default' pinboard has been selected. "
1925 "Use 'rox -p=Default' to turn it on in "
1926 "future."));
1927 g_return_if_fail(current_pinboard != NULL);
1930 abandon_backdrop_app(current_pinboard);
1932 g_free(current_pinboard->backdrop);
1933 current_pinboard->backdrop = g_strdup(path);
1934 current_pinboard->backdrop_style = style;
1935 reload_backdrop(current_pinboard,
1936 current_pinboard->backdrop,
1937 current_pinboard->backdrop_style);
1939 pinboard_save();