New options in mount point Properties dialog:
[rox-filer/th.git] / ROX-Filer / src / panel.c
blob22c9a97c6618253c1d73e6cda5f6f2af30254ed0
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* panel.c - code for dealing with panel windows */
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <libxml/parser.h>
32 #include <gtk/gtk.h>
33 #include <gdk/gdkx.h>
35 #include "global.h"
37 #include "panel.h"
38 #include "options.h"
39 #include "choices.h"
40 #include "main.h"
41 #include "type.h"
42 #include "gui_support.h"
43 #include "diritem.h"
44 #include "pixmaps.h"
45 #include "filer.h"
46 #include "display.h"
47 #include "bind.h"
48 #include "dnd.h"
49 #include "support.h"
50 #include "icon.h"
51 #include "run.h"
52 #include "appinfo.h"
53 #include "pixmaps.h"
54 #include "xml.h"
55 #include "pinboard.h" /* For pinboard_get_window() */
57 /* The width of the separator at the inner edge of the panel */
58 #define EDGE_WIDTH 1
60 /* The gap between panel icons */
61 #define PANEL_ICON_SPACING 8
63 enum {TEXT_BESIDE_ICON, TEXT_UNDER_ICON};
65 static gboolean tmp_icon_selected = FALSE; /* When dragging */
67 typedef struct _PanelIconClass PanelIconClass;
68 typedef struct _PanelIcon PanelIcon;
70 struct _PanelIconClass {
71 IconClass parent;
74 struct _PanelIcon {
75 Icon icon;
76 GdkPixbuf *image;
78 Panel *panel;
79 GtkWidget *widget; /* The drawing area for the icon */
80 GtkWidget *label;
81 GtkWidget *socket; /* For applets */
83 int style;
86 #define PANEL_ICON(obj) GTK_CHECK_CAST((obj), panel_icon_get_type(), PanelIcon)
87 #define IS_PANEL_ICON(obj) \
88 G_TYPE_CHECK_INSTANCE_TYPE((obj), panel_icon_get_type())
90 Panel *current_panel[PANEL_NUMBER_OF_SIDES];
92 /* NULL => Not loading a panel */
93 static Panel *loading_panel = NULL;
95 static GtkWidget *panel_options_dialog = NULL;
97 /* Static prototypes */
98 static int panel_delete(GtkWidget *widget, GdkEvent *event, Panel *panel);
99 static void panel_destroyed(GtkWidget *widget, Panel *panel);
100 static const char *pan_from_file(gchar *line);
101 static gint icon_button_release(GtkWidget *widget,
102 GdkEventButton *event,
103 PanelIcon *pi);
104 static gint icon_button_press(GtkWidget *widget,
105 GdkEventButton *event,
106 PanelIcon *pi);
107 static void reposition_panel(GtkWidget *window,
108 GtkAllocation *alloc, Panel *panel);
109 static gint expose_icon(GtkWidget *widget,
110 GdkEventExpose *event,
111 PanelIcon *pi);
112 static gint draw_icon(GtkWidget *widget,
113 GdkRectangle *badarea,
114 PanelIcon *pi);
115 static gint panel_button_release(GtkWidget *widget,
116 GdkEventButton *event,
117 Panel *panel);
118 static gint panel_button_press(GtkWidget *widget,
119 GdkEventButton *event,
120 Panel *panel);
121 static void panel_post_resize(GtkWidget *box,
122 GtkRequisition *req, Panel *panel);
123 static void drag_set_panel_dest(PanelIcon *pi);
124 static void add_uri_list(GtkWidget *widget,
125 GdkDragContext *context,
126 gint x,
127 gint y,
128 GtkSelectionData *selection_data,
129 guint info,
130 guint32 time,
131 Panel *panel);
132 static void panel_add_item(Panel *panel,
133 const gchar *path,
134 const gchar *name,
135 gboolean after,
136 const gchar *shortcut,
137 const gchar *args,
138 gboolean locked);
139 static gboolean panel_drag_motion(GtkWidget *widget,
140 GdkDragContext *context,
141 gint x,
142 gint y,
143 guint time,
144 Panel *panel);
145 static gboolean insert_drag_motion(GtkWidget *widget,
146 GdkDragContext *context,
147 gint x,
148 gint y,
149 guint time,
150 Panel *panel);
151 static gboolean drag_motion(GtkWidget *widget,
152 GdkDragContext *context,
153 gint x,
154 gint y,
155 guint time,
156 PanelIcon *pi);
157 static void panel_drag_leave(GtkWidget *widget,
158 GdkDragContext *context,
159 guint32 time,
160 Panel *panel);
161 static void drag_leave(GtkWidget *widget,
162 GdkDragContext *context,
163 guint32 time,
164 Icon *icon);
165 static GtkWidget *make_insert_frame(Panel *panel);
166 static gboolean enter_icon(GtkWidget *widget,
167 GdkEventCrossing *event,
168 Icon *icon);
169 static gint icon_motion_event(GtkWidget *widget,
170 GdkEventMotion *event,
171 PanelIcon *pi);
172 static gint panel_leave_event(GtkWidget *widget,
173 GdkEventCrossing *event,
174 Panel *panel);
175 static gint panel_motion_event(GtkWidget *widget,
176 GdkEventMotion *event,
177 Panel *panel);
178 static void reposition_icon(PanelIcon *pi, int index);
179 static void start_drag(PanelIcon *pi, GdkEventMotion *event);
180 static void drag_end(GtkWidget *widget,
181 GdkDragContext *context,
182 Icon *icon);
183 static void perform_action(Panel *panel,
184 PanelIcon *pi,
185 GdkEventButton *event);
186 static void run_applet(PanelIcon *pi);
187 static void size_request(GtkWidget *widget, GtkRequisition *req, PanelIcon *pi);
188 static void panel_load_from_xml(Panel *panel, xmlDocPtr doc);
189 static gboolean draw_panel_edge(GtkWidget *widget, GdkEventExpose *event,
190 Panel *panel);
191 static PanelIcon *panel_icon_new(Panel *panel,
192 const char *pathname,
193 const char *name);
194 static GType panel_icon_get_type(void);
195 static gboolean panel_want_show_text(PanelIcon *pi);
196 static void panel_show_menu(GdkEventButton *event, PanelIcon *pi, Panel *panel);
197 static void panel_style_changed(void);
198 static void motion_may_raise(Panel *panel, int x, int y);
199 static void panel_update(Panel *panel);
200 static GList *build_monitor_number(Option *option,
201 xmlNode *node, guchar *label);
202 static gboolean may_autoscroll(Panel *panel);
203 static void panel_update_geometry(Panel *panel);
204 static gboolean panel_keep_below(Panel *panel, gboolean setting);
207 static GtkWidget *dnd_highlight = NULL; /* (stops flickering) */
209 #define SHOW_BOTH 0
210 #define SHOW_APPS_SMALL 1
211 #define SHOW_ICON 2
212 static Option o_panel_style;
213 static Option o_panel_width;
214 static Option o_panel_xinerama;
215 static Option o_panel_monitor;
216 static Option o_panel_avoid;
217 static Option o_panel_is_dock;
218 static Option o_panel_on_top;
220 static gint panel_monitor = -1;
222 static int closing_panel = 0; /* Don't panel_save; destroying! */
224 /****************************************************************
225 * EXTERNAL INTERFACE *
226 ****************************************************************/
228 void panel_init(void)
230 option_add_int(&o_panel_style, "panel_style", SHOW_APPS_SMALL);
231 option_add_int(&o_panel_width, "panel_width", 52);
233 option_add_int(&o_panel_xinerama, "panel_xinerama", 0);
234 option_add_int(&o_panel_monitor, "panel_monitor", 0);
236 option_add_int(&o_panel_avoid, "panel_avoid", TRUE);
237 option_add_int(&o_panel_is_dock, "panel_is_dock", TRUE);
238 option_add_int(&o_panel_on_top, "panel_on_top", FALSE);
240 option_add_notify(panel_style_changed);
242 option_register_widget("monitor-number", build_monitor_number);
245 /* Return a free edge for a new panel.
246 * If no edge is free, returns PANEL_BOTTOM.
248 static PanelSide find_free_side()
250 if (!current_panel[PANEL_BOTTOM])
251 return PANEL_BOTTOM;
253 if (!current_panel[PANEL_TOP])
254 return PANEL_TOP;
256 if (!current_panel[PANEL_LEFT])
257 return PANEL_LEFT;
259 if (!current_panel[PANEL_RIGHT])
260 return PANEL_RIGHT;
262 return PANEL_BOTTOM;
265 /* Returns TRUE and sets *target if the property exists, otherwise returns
266 * FALSE and leaves *target unchanged */
267 static gboolean get_int_prop(xmlNodePtr node, const char *name, int *target)
269 char *prop = xmlGetProp(node, name);
271 if (prop)
273 *target = atoi(prop);
274 g_free(prop);
275 return TRUE;
277 return FALSE;
280 static void set_int_prop(xmlNodePtr node, const char *name, int value)
282 char prop[16];
284 sprintf(prop, "%d", value);
285 xmlSetProp(node, name, prop);
288 static void panel_load_options_from_xml(Panel *panel, xmlDocPtr doc)
290 xmlNodePtr root;
291 xmlNodePtr options;
293 root = xmlDocGetRootElement(doc);
294 options = get_subnode(root, NULL, "options");
295 if (!options)
296 return;
297 get_int_prop(options, "style", &panel->style);
298 get_int_prop(options, "width", &panel->width);
299 get_int_prop(options, "avoid", &panel->avoid);
300 get_int_prop(options, "xinerama", &panel->xinerama);
301 get_int_prop(options, "monitor", &panel->monitor);
304 static void save_panels(void)
306 char *filename = choices_find_xdg_path_save("panels",
307 "ROX-Filer", "rox.sourceforge.net", TRUE);
308 char *tmp = g_strconcat(filename, ".new", NULL);
309 FILE *fp = fopen(tmp, "w");
311 if (fp)
313 PanelSide n;
315 for (n = 0; n < PANEL_NUMBER_OF_SIDES; ++n)
317 if (current_panel[n])
318 fprintf(fp, "%s\n", current_panel[n]->name);
320 fclose(fp);
321 if (rename(tmp, filename))
322 g_critical(_("Unable to replace '%s'"), filename);
324 else
326 g_critical(_("Unable to save '%s'"), tmp);
328 g_free(tmp);
329 g_free(filename);
332 /* 'name' may be NULL or "" to remove the panel */
333 Panel *panel_new(const gchar *name, PanelSide side)
335 guchar *load_path;
336 Panel *panel;
337 GtkWidget *vp, *box, *frame, *align;
338 xmlDocPtr panel_doc = NULL;
339 gboolean need_resave = FALSE;
341 g_return_val_if_fail(side == PANEL_DEFAULT_SIDE ||
342 (side >= 0 && side < PANEL_NUMBER_OF_SIDES), NULL);
343 g_return_val_if_fail(loading_panel == NULL, NULL);
345 if (name && *name == '\0')
346 name = NULL;
348 if (!name)
349 load_path = NULL;
350 else if (strchr(name, '/'))
351 load_path = g_strdup(name);
352 else
354 guchar *leaf;
356 leaf = g_strconcat("pan_", name, NULL);
357 load_path = choices_find_xdg_path_load(leaf, PROJECT, SITE);
358 g_free(leaf);
361 if (load_path && access(load_path, F_OK) == 0)
363 char *saved_side;
364 xmlNodePtr root;
366 panel_doc = xmlParseFile(load_path);
367 root = xmlDocGetRootElement(panel_doc);
369 saved_side = xmlGetProp(root, "side");
370 if (saved_side)
372 PanelSide old_side;
373 old_side = panel_name_to_side(saved_side);
374 g_free(saved_side);
376 if (side == PANEL_DEFAULT_SIDE)
377 side = old_side;
378 else if (side != old_side)
379 need_resave = TRUE;
381 else
382 need_resave = TRUE;
385 if (side == PANEL_DEFAULT_SIDE)
386 side = find_free_side();
388 if (current_panel[side])
390 if (name)
391 number_of_windows++;
392 closing_panel++;
393 gtk_widget_destroy(current_panel[side]->window);
394 closing_panel--;
395 if (name)
396 number_of_windows--;
399 if (name == NULL || *name == '\0')
401 save_panels();
402 return NULL;
405 panel = g_new(Panel, 1);
406 panel->name = g_strdup(name);
407 panel->side = side;
408 panel->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
409 panel->autoscroll_speed = 0;
411 /* These are fallbacks from legacy global options */
412 panel->style = o_panel_style.int_value;
413 panel->width = o_panel_width.int_value;
414 panel->xinerama = o_panel_xinerama.int_value;
415 panel->monitor = o_panel_monitor.int_value;
416 panel->avoid = o_panel_avoid.int_value;
418 /* Now try to load options from this panel's XML */
419 if (panel_doc)
421 panel_load_options_from_xml(panel, panel_doc);
423 else
425 /* Otherwise ensure old settings are migrated */
426 need_resave = TRUE;
429 panel_update_geometry(panel);
431 gtk_window_set_resizable(GTK_WINDOW(panel->window), FALSE);
432 gtk_window_set_wmclass(GTK_WINDOW(panel->window), "ROX-Panel", PROJECT);
433 gtk_widget_set_name(panel->window, "rox-panel");
434 gtk_widget_set_events(panel->window,
435 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
436 GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
438 /* We make the panel a drop target only so that we can auto-raise! */
439 gtk_drag_dest_set(panel->window, 0, NULL, 0, GDK_ACTION_PRIVATE);
440 g_signal_connect(panel->window, "drag_leave",
441 G_CALLBACK(panel_drag_leave), panel);
442 g_signal_connect(panel->window, "drag_motion",
443 G_CALLBACK(panel_drag_motion), panel);
445 g_signal_connect(panel->window, "delete-event",
446 G_CALLBACK(panel_delete), panel);
447 g_signal_connect(panel->window, "destroy",
448 G_CALLBACK(panel_destroyed), panel);
449 g_signal_connect(panel->window, "button_press_event",
450 G_CALLBACK(panel_button_press), panel);
451 g_signal_connect(panel->window, "button_release_event",
452 G_CALLBACK(panel_button_release), panel);
453 g_signal_connect(panel->window, "motion-notify-event",
454 G_CALLBACK(panel_motion_event), panel);
455 g_signal_connect(panel->window, "leave-notify-event",
456 G_CALLBACK(panel_leave_event), panel);
458 if (panel->side == PANEL_RIGHT)
459 align = gtk_alignment_new(1.0, 0.0, 0.0, 1.0);
460 else if (panel->side == PANEL_BOTTOM)
461 align = gtk_alignment_new(0.0, 1.0, 1.0, 0.0);
462 else if (panel->side == PANEL_TOP)
463 align = gtk_alignment_new(0.0, 0.0, 1.0, 0.0);
464 else
465 align = gtk_alignment_new(0.0, 0.0, 0.0, 1.0);
467 gtk_container_add(GTK_CONTAINER(panel->window), align);
469 vp = gtk_viewport_new(NULL, NULL);
470 gtk_container_set_resize_mode(GTK_CONTAINER(vp), GTK_RESIZE_PARENT);
471 gtk_viewport_set_shadow_type(GTK_VIEWPORT(vp), GTK_SHADOW_NONE);
472 gtk_container_add(GTK_CONTAINER(align), vp);
474 g_signal_connect(align, "expose-event",
475 G_CALLBACK(draw_panel_edge), panel);
477 if (side == PANEL_TOP || side == PANEL_BOTTOM)
479 panel->adj = gtk_viewport_get_hadjustment(GTK_VIEWPORT(vp));
480 box = gtk_hbox_new(FALSE, 0);
481 panel->before = gtk_hbox_new(FALSE, 0);
482 panel->after = gtk_hbox_new(FALSE, 0);
484 else
486 panel->adj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(vp));
487 box = gtk_vbox_new(FALSE, 0);
488 panel->before = gtk_vbox_new(FALSE, 0);
489 panel->after = gtk_vbox_new(FALSE, 0);
492 gtk_container_add(GTK_CONTAINER(vp), box);
493 gtk_box_pack_start(GTK_BOX(box), panel->before, FALSE, TRUE, 0);
494 gtk_box_pack_end(GTK_BOX(box), panel->after, FALSE, TRUE, 0);
496 frame = make_insert_frame(panel);
497 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 4);
499 /* This is used so that we can find the middle easily! */
500 panel->gap = gtk_event_box_new();
501 gtk_box_pack_start(GTK_BOX(box), panel->gap, FALSE, FALSE, 0);
503 frame = make_insert_frame(panel);
504 g_object_set_data(G_OBJECT(frame), "after", "yes");
505 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 4);
507 if (o_panel_is_dock.int_value)
508 gtk_window_set_type_hint(GTK_WINDOW(panel->window),
509 GDK_WINDOW_TYPE_HINT_DOCK);
511 gtk_widget_realize(panel->window);
512 make_panel_window(panel->window);
513 gtk_window_stick(GTK_WINDOW(panel->window));
515 gtk_widget_show_all(align);
517 loading_panel = panel;
518 if (panel_doc)
520 panel_load_from_xml(panel, panel_doc);
521 xmlFreeDoc(panel_doc);
523 if (need_resave)
524 panel_save(panel);
526 else if (load_path)
528 parse_file(load_path, pan_from_file);
529 info_message(_("Your old panel file has been "
530 "converted to the new XML format."));
531 panel_save(panel);
533 else
535 /* Don't scare users with an empty panel... */
536 guchar *apps;
538 panel_add_item(panel, "~", "Home", FALSE, NULL, NULL, FALSE);
540 apps = pathdup(make_path(app_dir, ".."));
541 if (apps)
543 panel_add_item(panel, apps, "Apps", FALSE, NULL, NULL, FALSE);
544 g_free(apps);
547 loading_panel = NULL;
548 g_free(load_path);
550 current_panel[side] = panel;
552 gtk_widget_queue_resize(box);
553 g_signal_connect(panel->window, "size-request",
554 G_CALLBACK(panel_post_resize), panel);
555 g_signal_connect(panel->window, "size-allocate",
556 G_CALLBACK(reposition_panel), panel);
558 number_of_windows++;
559 gdk_window_lower(panel->window->window);
560 gtk_widget_show(panel->window);
561 /* This has no effect until after window is showing; GTK+ bug? */
562 if (panel_keep_below(panel, TRUE))
564 GdkWindow *pinboard;
566 pinboard = pinboard_get_window();
567 /* (if pinboard is NULL, will go right to the back) */
568 window_put_just_above(panel->window->window, pinboard);
571 save_panels();
573 return panel;
576 /* Externally visible function to add an item to a panel */
577 gboolean panel_add(PanelSide side,
578 const gchar *path, const gchar *label, gboolean after, const gchar *shortcut, const gchar *args,
579 gboolean locked)
581 g_return_val_if_fail(side >= 0 && side < PANEL_NUMBER_OF_SIDES, FALSE);
583 g_return_val_if_fail(current_panel[side] != NULL, FALSE);
585 panel_add_item(current_panel[side], path, label, after, shortcut, args, locked);
587 return TRUE;
590 /* Add the area covered by the panels to the region */
591 void panel_mark_used(GdkRegion *used)
593 int i;
595 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
597 Panel *panel = current_panel[i];
598 GdkRectangle rect;
600 if (!panel)
601 continue;
603 gdk_window_get_root_origin(panel->window->window,
604 &rect.x, &rect.y);
605 rect.width = panel->window->allocation.width;
606 rect.height = panel->window->allocation.height;
608 gdk_region_union_with_rect(used, &rect);
612 /* On xrandr screen size changes, update all panels */
613 void panel_update_size(void)
615 int i;
617 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
619 if (current_panel[i])
621 panel_update_geometry(current_panel[i]);
622 reposition_panel(current_panel[i]->window,
623 &current_panel[i]->window->allocation,
624 current_panel[i]);
625 gtk_widget_queue_resize(current_panel[i]->window);
630 /****************************************************************
631 * INTERNAL FUNCTIONS *
632 ****************************************************************/
634 /* User has tried to close the panel via the window manager - confirm */
635 static int panel_delete(GtkWidget *widget, GdkEvent *event, Panel *panel)
637 return !confirm(_("You have tried to close a panel via the window "
638 "manager - I usually find that this is accidental... "
639 "really close?"),
640 GTK_STOCK_CLOSE, NULL);
643 static void panel_destroyed(GtkWidget *widget, Panel *panel)
645 if (panel_options_dialog)
647 Panel *dlg_panel = g_object_get_data(G_OBJECT(panel_options_dialog),
648 "rox-panel");
650 if (dlg_panel == panel)
651 gtk_widget_destroy(panel_options_dialog);
654 if (current_panel[panel->side] == panel)
655 current_panel[panel->side] = NULL;
657 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
659 if (current_panel[PANEL_RIGHT])
660 gtk_widget_queue_resize(
661 current_panel[PANEL_RIGHT]->window);
662 if (current_panel[PANEL_LEFT])
663 gtk_widget_queue_resize(
664 current_panel[PANEL_LEFT]->window);
667 if (panel->autoscroll_speed)
668 g_source_remove(panel->autoscroll_to);
670 g_free(panel->name);
671 g_free(panel);
673 one_less_window();
676 static void panel_load_side(Panel *panel, xmlNodePtr side, gboolean after)
678 xmlNodePtr node;
679 char *label, *path, *shortcut, *args, *tmp;
680 gboolean locked;
682 for (node = side->xmlChildrenNode; node; node = node->next)
684 if (node->type != XML_ELEMENT_NODE)
685 continue;
686 if (strcmp(node->name, "icon") != 0)
687 continue;
689 label = xmlGetProp(node, "label");
690 if (!label)
691 label = g_strdup("<missing label>");
692 path = xmlNodeGetContent(node);
693 if (!path)
694 path = g_strdup("<missing path>");
695 shortcut = xmlGetProp(node, "shortcut");
696 args = xmlGetProp(node, "args");
697 tmp = xmlGetProp(node, "locked");
698 if (tmp)
700 locked = text_to_boolean(tmp, FALSE);
701 g_free(tmp);
703 else
704 locked = FALSE;
706 panel_add_item(panel, path, label, after, shortcut, args, locked);
708 g_free(path);
709 g_free(label);
710 g_free(shortcut);
711 g_free(args);
715 /* Create one panel icon for each icon in the doc */
716 static void panel_load_from_xml(Panel *panel, xmlDocPtr doc)
718 xmlNodePtr root;
720 root = xmlDocGetRootElement(doc);
721 panel_load_side(panel, get_subnode(root, NULL, "start"), FALSE);
722 panel_load_side(panel, get_subnode(root, NULL, "end"), TRUE);
725 /* Called for each line in the config file while loading a new panel */
726 static const char *pan_from_file(gchar *line)
728 gchar *sep, *leaf;
730 g_return_val_if_fail(line != NULL, NULL);
731 g_return_val_if_fail(loading_panel != NULL, NULL);
733 if (*line == '\0')
734 return NULL;
736 sep = strpbrk(line, "<>");
737 if (!sep)
738 return _("Missing < or > in panel config file");
740 if (sep != line)
741 leaf = g_strndup(line, sep - line);
742 else
743 leaf = NULL;
745 panel_add_item(loading_panel, sep + 1, leaf, sep[0] == '>',
746 NULL, NULL, FALSE);
748 g_free(leaf);
750 return NULL;
753 static gboolean icon_pointer_in(GtkWidget *widget,
754 GdkEventCrossing *event,
755 Icon *icon)
757 gtk_widget_set_state(widget,
758 icon->selected ? GTK_STATE_SELECTED : GTK_STATE_PRELIGHT);
760 return 0;
763 static gboolean icon_pointer_out(GtkWidget *widget,
764 GdkEventCrossing *event,
765 Icon *icon)
767 gtk_widget_set_state(widget,
768 icon->selected ? GTK_STATE_SELECTED : GTK_STATE_NORMAL);
770 return 0;
773 static void panel_icon_destroyed(PanelIcon *pi)
775 g_return_if_fail(pi->widget != NULL);
777 pi->widget = NULL;
779 g_object_unref(pi);
782 /* Set the tooltip AND hide/show the label */
783 static void panel_icon_set_tip(PanelIcon *pi)
785 XMLwrapper *ai;
786 xmlNode *node;
787 Icon *icon = (Icon *) pi;
789 g_return_if_fail(pi != NULL);
791 if (pi->label)
793 if (panel_want_show_text(pi))
794 gtk_widget_show(pi->label);
795 else
796 gtk_widget_hide(pi->label);
799 if (pi->socket)
800 ai = NULL;
801 else
802 ai = appinfo_get(icon->path, icon->item);
804 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
806 guchar *str;
807 str = xmlNodeListGetString(node->doc,
808 node->xmlChildrenNode, 1);
809 if (str)
811 gtk_tooltips_set_tip(tooltips, pi->widget, str, NULL);
812 g_free(str);
815 else if ((!panel_want_show_text(pi)) && !pi->socket)
817 if (icon->item->leafname && icon->item->leafname[0])
818 gtk_tooltips_set_tip(tooltips, pi->widget,
819 icon->item->leafname, NULL);
821 else
822 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
824 if (ai)
825 g_object_unref(ai);
828 /* Add an icon with this path to the panel. If after is TRUE then the
829 * icon is added to the right/bottom end of the panel.
831 * If name is NULL a suitable name is taken from path.
833 static void panel_add_item(Panel *panel,
834 const gchar *path,
835 const gchar *name,
836 gboolean after,
837 const gchar *shortcut,
838 const gchar *args,
839 gboolean locked)
841 GtkWidget *widget;
842 PanelIcon *pi;
843 Icon *icon;
845 g_return_if_fail(panel != NULL);
846 g_return_if_fail(path != NULL);
848 widget = gtk_event_box_new();
849 gtk_widget_set_events(widget,
850 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
851 GDK_BUTTON3_MOTION_MASK |
852 GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
853 GDK_BUTTON_RELEASE_MASK);
855 gtk_box_pack_start(GTK_BOX(after ? panel->after : panel->before),
856 widget, FALSE, TRUE, 0);
857 if (after)
858 gtk_box_reorder_child(GTK_BOX(panel->after), widget, 0);
860 gtk_widget_realize(widget);
862 pi = panel_icon_new(panel, path, name);
863 icon = (Icon *) pi;
865 /* Widget takes the initial ref of Icon */
866 g_object_set_data(G_OBJECT(widget), "icon", pi);
868 pi->widget = widget;
869 g_object_ref(widget);
871 gtk_widget_set_name(pi->widget, "panel-icon");
873 g_signal_connect_swapped(widget, "destroy",
874 G_CALLBACK(panel_icon_destroyed), pi);
876 if (icon->item->base_type == TYPE_DIRECTORY)
877 run_applet(pi);
879 g_signal_connect(widget, "button_release_event",
880 G_CALLBACK(icon_button_release), pi);
881 g_signal_connect(widget, "button_press_event",
882 G_CALLBACK(icon_button_press), pi);
883 g_signal_connect(widget, "motion-notify-event",
884 G_CALLBACK(icon_motion_event), pi);
885 g_signal_connect(widget, "enter-notify-event",
886 G_CALLBACK(icon_pointer_in), pi);
887 g_signal_connect(widget, "leave-notify-event",
888 G_CALLBACK(icon_pointer_out), pi);
890 if (!pi->socket)
892 g_signal_connect(widget, "enter-notify-event",
893 G_CALLBACK(enter_icon), pi);
894 g_signal_connect_after(widget, "expose_event",
895 G_CALLBACK(expose_icon), pi);
896 g_signal_connect(widget, "drag_data_get",
897 G_CALLBACK(drag_data_get), NULL);
899 g_signal_connect(widget, "size_request",
900 G_CALLBACK(size_request), pi);
902 drag_set_panel_dest(pi);
904 pi->label = gtk_label_new(icon->item->leafname);
905 gtk_container_add(GTK_CONTAINER(pi->widget), pi->label);
906 gtk_misc_set_alignment(GTK_MISC(pi->label), 0.5, 1);
907 gtk_misc_set_padding(GTK_MISC(pi->label), 1, 2);
910 icon_set_shortcut(icon, shortcut);
911 icon_set_arguments(icon, args);
912 icon->locked = locked;
914 if (!loading_panel)
915 panel_save(panel);
917 panel_icon_set_tip(pi);
918 gtk_widget_show(widget);
921 static gboolean remove_item_from_side(GtkWidget *container, const gchar *path,
922 const gchar *label)
924 GList *kids, *next;
925 gboolean found = FALSE;
927 kids = gtk_container_get_children(GTK_CONTAINER(container));
929 for (next = kids; next; next = next->next)
931 Icon *icon;
932 icon = g_object_get_data(G_OBJECT(next->data), "icon");
933 if (!icon)
934 continue;
936 if ((!path || strcmp(path, icon->src_path) == 0) &&
937 (!label || strcmp(label, icon->item->leafname)==0))
939 icon->locked = FALSE;
940 icon_destroy(icon);
941 found = TRUE;
942 break;
946 g_list_free(kids);
948 return found;
951 /* Remove an item with this path. If more than one item matches, only
952 * one is removed. If label is not NULL then it must also match the item.
953 * Returns TRUE if an item was successfully removed.
955 gboolean panel_remove_item(PanelSide side, const gchar *path,
956 const gchar *label)
958 Panel *panel;
960 g_return_val_if_fail(side >= 0 && side < PANEL_NUMBER_OF_SIDES, FALSE);
962 g_return_val_if_fail(path != NULL || label != NULL, FALSE);
964 panel = current_panel[side];
965 if (!panel)
967 g_warning("No panel on this side of the screen!");
968 return FALSE;
971 if (remove_item_from_side(panel->before, path, label) ||
972 remove_item_from_side(panel->after, path, label))
974 panel_save(panel);
975 panel_update(panel);
976 return TRUE;
979 g_warning("Panel item path='%s', label='%s' not found", path, label);
980 return FALSE;
983 /* Called when Gtk+ wants to know how much space an icon needs.
984 * 'req' is already big enough for the label, if shown.
986 static void size_request(GtkWidget *widget, GtkRequisition *req, PanelIcon *pi)
988 Icon *icon = (Icon *) pi;
989 gboolean horz = (pi->panel->side == PANEL_TOP ||
990 pi->panel->side == PANEL_BOTTOM);
991 int max_width = 100;
992 int max_height = 100;
993 int image_width, image_height;
994 Panel *panel = pi->panel;
996 if (horz)
997 max_height = panel->width - req->height;
998 else
999 max_width = MAX(panel->width, req->width);
1001 /* TODO: really need to recreate? */
1002 if (pi->image)
1003 g_object_unref(pi->image);
1005 pi->image = scale_pixbuf(di_image(icon->item)->src_pixbuf,
1006 MAX(20, max_width), MAX(20, max_height));
1008 image_width = gdk_pixbuf_get_width(pi->image);
1009 image_height = gdk_pixbuf_get_height(pi->image);
1011 if (req->height > 0 && max_height < req->height)
1013 pi->style = TEXT_BESIDE_ICON;
1014 req->width += image_width;
1015 req->height = MAX(req->height, image_height);
1016 gtk_misc_set_alignment(GTK_MISC(pi->label), 1, 0.5);
1018 else
1020 pi->style = TEXT_UNDER_ICON;
1021 req->width = MAX(req->width, image_width);
1022 req->height += image_height;
1023 gtk_misc_set_alignment(GTK_MISC(pi->label), 0.5, 1);
1026 if (horz)
1027 req->width += PANEL_ICON_SPACING;
1028 else
1029 req->height += PANEL_ICON_SPACING;
1032 static gint expose_icon(GtkWidget *widget,
1033 GdkEventExpose *event,
1034 PanelIcon *pi)
1036 return draw_icon(widget, &event->area, pi);
1039 static gint draw_icon(GtkWidget *widget, GdkRectangle *badarea, PanelIcon *pi)
1041 GdkRectangle area;
1042 int width, height;
1043 Icon *icon = (Icon *) pi;
1044 int image_x;
1045 int image_y;
1046 GdkPixbuf *image;
1047 int text_height = 0;
1049 gdk_drawable_get_size(widget->window, &area.width, &area.height);
1051 if (panel_want_show_text(pi))
1052 text_height = pi->label->requisition.height;
1054 g_return_val_if_fail(pi->image != NULL, FALSE);
1056 image = pi->image;
1058 width = gdk_pixbuf_get_width(image);
1059 height = gdk_pixbuf_get_height(image);
1061 if (pi->style == TEXT_UNDER_ICON)
1063 image_x = (area.width - width) >> 1;
1064 image_y = (area.height - height - text_height) >> 1;
1066 else
1068 image_x = PANEL_ICON_SPACING - 2;
1069 image_y = (area.height - height) >> 1;
1072 gdk_pixbuf_render_to_drawable_alpha(
1073 image,
1074 widget->window,
1075 0, 0, /* src */
1076 image_x, image_y, /* dest */
1077 width, height,
1078 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
1079 GDK_RGB_DITHER_NORMAL, 0, 0);
1081 if (icon->item->flags & ITEM_FLAG_SYMLINK)
1083 draw_emblem_on_icon(widget->window, widget->style,
1084 ROX_STOCK_SYMLINK, &image_x, image_y+2);
1086 if (icon->item->flags & ITEM_FLAG_MOUNT_POINT)
1088 draw_emblem_on_icon(widget->window, widget->style,
1089 icon->item->flags & ITEM_FLAG_MOUNTED
1090 ? ROX_STOCK_MOUNTED
1091 : ROX_STOCK_MOUNT,
1092 &image_x, image_y+2);
1094 return FALSE;
1097 static void panel_icon_wink(Icon *icon)
1099 PanelIcon *pi = (PanelIcon *) icon;
1101 wink_widget(pi->widget);
1104 /* icon may be NULL if the event is on the background */
1105 static void perform_action(Panel *panel, PanelIcon *pi, GdkEventButton *event)
1107 BindAction action;
1108 Icon *icon = (Icon *) pi;
1110 action = bind_lookup_bev(icon ? BIND_PANEL_ICON : BIND_PANEL, event);
1112 if (pi && pi->socket)
1113 if (action != ACT_POPUP_MENU && action != ACT_MOVE_ICON)
1114 return;
1116 switch (action)
1118 case ACT_OPEN_ITEM:
1119 dnd_motion_ungrab();
1120 wink_widget(pi->widget);
1121 icon_run(icon);
1122 break;
1123 case ACT_EDIT_ITEM:
1124 dnd_motion_ungrab();
1125 wink_widget(pi->widget);
1126 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1127 break;
1128 case ACT_POPUP_MENU:
1129 dnd_motion_ungrab();
1130 panel_show_menu(event, pi, panel);
1131 break;
1132 case ACT_MOVE_ICON:
1133 dnd_motion_start(MOTION_REPOSITION);
1134 break;
1135 case ACT_PRIME_AND_SELECT:
1136 if (!icon->selected)
1137 icon_select_only(icon);
1138 dnd_motion_start(MOTION_READY_FOR_DND);
1139 break;
1140 case ACT_PRIME_AND_TOGGLE:
1141 icon_set_selected(icon, !icon->selected);
1142 dnd_motion_start(MOTION_READY_FOR_DND);
1143 break;
1144 case ACT_PRIME_FOR_DND:
1145 dnd_motion_start(MOTION_READY_FOR_DND);
1146 break;
1147 case ACT_TOGGLE_SELECTED:
1148 icon_set_selected(icon, !icon->selected);
1149 break;
1150 case ACT_SELECT_EXCL:
1151 icon_set_selected(icon, TRUE);
1152 break;
1153 case ACT_IGNORE:
1154 break;
1155 case ACT_CLEAR_SELECTION:
1156 dnd_motion_ungrab();
1157 icon_select_only(NULL);
1158 break;
1159 default:
1160 g_warning("Unsupported action : %d\n", action);
1161 break;
1165 static gint panel_button_release(GtkWidget *widget,
1166 GdkEventButton *event,
1167 Panel *panel)
1169 if (dnd_motion_release(event))
1170 return TRUE;
1172 perform_action(panel, NULL, event);
1174 return TRUE;
1177 static gint panel_button_press(GtkWidget *widget,
1178 GdkEventButton *event,
1179 Panel *panel)
1181 if (dnd_motion_press(panel->window, event))
1182 perform_action(panel, NULL, event);
1184 return TRUE;
1187 static gint icon_button_release(GtkWidget *widget,
1188 GdkEventButton *event,
1189 PanelIcon *pi)
1191 if (pi->socket && event->button == 1)
1192 return FALSE; /* Restart button */
1194 if (dnd_motion_release(event))
1195 return TRUE;
1197 perform_action(pi->panel, pi, event);
1199 return TRUE;
1202 static gint icon_button_press(GtkWidget *widget,
1203 GdkEventButton *event,
1204 PanelIcon *pi)
1206 if (pi->socket && event->button == 1)
1207 return FALSE; /* Restart button */
1209 if (dnd_motion_press(widget, event))
1210 perform_action(pi->panel, pi, event);
1212 return TRUE;
1215 /* Stop windows from maximising over all/part of us */
1216 static void panel_setup_struts(Panel *panel, GtkAllocation *alloc)
1218 int thickness;
1219 struct {
1220 gulong left, right, top, bottom;
1221 gulong left_start_y, left_end_y;
1222 gulong right_start_y, right_end_y;
1223 gulong top_start_x, top_end_x;
1224 gulong bottom_start_x, bottom_end_x;
1225 } strut = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1227 if (panel->avoid == FALSE)
1228 thickness = 2;
1229 else if (panel->side == PANEL_TOP ||
1230 panel->side == PANEL_BOTTOM)
1231 thickness = alloc->height;
1232 else
1233 thickness = alloc->width;
1235 switch (panel->side)
1237 case PANEL_LEFT:
1238 if (!panel->xinerama || !monitor_adjacent[panel->monitor].left)
1240 strut.left = panel->geometry.x + thickness;
1241 strut.left_start_y = panel->geometry.y;
1242 strut.left_end_y = panel->geometry.y +
1243 panel->geometry.height - 1;
1245 /* else there is (part of) a monitor
1246 * to the left */
1247 else
1249 thickness = 0;
1251 break;
1252 case PANEL_RIGHT:
1253 if (!panel->xinerama || !monitor_adjacent[panel->monitor].right)
1255 /* RHS of monitor might not abut edge
1256 * of total virtual screen */
1257 strut.right = screen_width -
1258 panel->geometry.x -
1259 panel->geometry.width +
1260 thickness;
1261 strut.right_start_y = panel->geometry.y;
1262 strut.right_end_y = panel->geometry.y +
1263 panel->geometry.height - 1;
1265 /* else there is (part of) a monitor
1266 * to the right */
1267 else
1269 thickness = 0;
1271 break;
1272 case PANEL_TOP:
1273 if (!panel->xinerama || !monitor_adjacent[panel->monitor].top)
1275 strut.top = panel->geometry.y + thickness;
1276 strut.top_start_x = panel->geometry.x;
1277 strut.top_end_x = panel->geometry.x +
1278 panel->geometry.width - 1;
1280 /* else there is (part of) a monitor above */
1281 else
1283 thickness = 0;
1285 break;
1286 default: /* PANEL_BOTTOM */
1287 if (!panel->xinerama ||
1288 !monitor_adjacent[panel->monitor].bottom)
1290 /* Bottom of monitor might not abut
1291 * edge of total virtual screen */
1292 strut.bottom = screen_height -
1293 panel->geometry.y -
1294 panel->geometry.height +
1295 thickness;
1296 strut.bottom_start_x = panel->geometry.x;
1297 strut.bottom_end_x = panel->geometry.x +
1298 panel->geometry.width - 1;
1300 /* else there is (part of) a monitor below */
1301 else
1303 thickness = 0;
1305 break;
1308 if (thickness)
1310 /* Set full-width strut as well as partial in case
1311 * partial isn't supported by wm */
1312 gdk_property_change(panel->window->window,
1313 gdk_atom_intern("_NET_WM_STRUT",
1314 FALSE),
1315 gdk_atom_intern("CARDINAL", FALSE),
1316 32, GDK_PROP_MODE_REPLACE,
1317 (gchar *) &strut, 4);
1318 gdk_property_change(panel->window->window,
1319 gdk_atom_intern("_NET_WM_STRUT_PARTIAL",
1320 FALSE),
1321 gdk_atom_intern("CARDINAL", FALSE),
1322 32, GDK_PROP_MODE_REPLACE,
1323 (gchar *) &strut, 12);
1325 else
1327 gdk_property_delete(panel->window->window,
1328 gdk_atom_intern("_NET_WM_STRUT_PARTIAL",
1329 FALSE));
1330 gdk_property_delete(panel->window->window,
1331 gdk_atom_intern("_NET_WM_STRUT",
1332 FALSE));
1336 static void reposition_panel(GtkWidget *window,
1337 GtkAllocation *alloc, Panel *panel)
1339 int x = panel->geometry.x;
1340 int y = panel->geometry.y;
1341 PanelSide side = panel->side;
1343 if (side == PANEL_LEFT || side == PANEL_RIGHT)
1345 if (side == PANEL_RIGHT)
1346 x += panel->geometry.width - alloc->width;
1348 if (current_panel[PANEL_TOP])
1350 GtkWidget *win = current_panel[PANEL_TOP]->window;
1351 y += win->allocation.height;
1355 if (side == PANEL_BOTTOM)
1356 y += panel->geometry.height - alloc->height;
1358 gtk_window_move(GTK_WINDOW(panel->window), x, y);
1359 gdk_window_move(panel->window->window, x, y);
1361 if (side == PANEL_BOTTOM || side == PANEL_TOP)
1363 if (current_panel[PANEL_RIGHT])
1364 gtk_widget_queue_resize(
1365 current_panel[PANEL_RIGHT]->window);
1366 if (current_panel[PANEL_LEFT])
1367 gtk_widget_queue_resize(
1368 current_panel[PANEL_LEFT]->window);
1371 panel_setup_struts(panel, alloc);
1374 /* Same as drag_set_dest(), but for panel icons */
1375 static void drag_set_panel_dest(PanelIcon *pi)
1377 GtkWidget *obj = pi->widget;
1379 make_drop_target(pi->widget, 0);
1381 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1382 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1383 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1386 static gboolean drag_motion(GtkWidget *widget,
1387 GdkDragContext *context,
1388 gint x,
1389 gint y,
1390 guint time,
1391 PanelIcon *pi)
1393 GdkDragAction action = context->suggested_action;
1394 const char *type = NULL;
1395 Icon *icon = (Icon *) pi;
1396 DirItem *item = icon->item;
1397 int panel_x, panel_y;
1399 gdk_window_get_pointer(pi->panel->window->window,
1400 &panel_x, &panel_y, NULL);
1401 motion_may_raise(pi->panel, panel_x, panel_y);
1403 /* Should we scroll the panel when dragging? */
1404 if (motion_state != MOTION_REPOSITION)
1405 if (pi->panel->autoscroll_speed == 0)
1406 may_autoscroll(pi->panel);
1408 if (icon->selected)
1409 goto out; /* Can't drag a selection to itself */
1411 type = dnd_motion_item(context, &item);
1413 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1414 && type != drop_dest_prog)
1416 guint state;
1417 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1418 if (state & GDK_BUTTON1_MASK)
1419 action = GDK_ACTION_ASK;
1422 if (!item)
1423 type = NULL;
1424 out:
1425 /* We actually must pretend to accept the drop, even if the
1426 * directory isn't writeable, so that the spring-opening
1427 * thing works.
1430 /* Don't allow drops to non-writeable directories */
1431 if (o_dnd_spring_open.int_value == FALSE &&
1432 type == drop_dest_dir &&
1433 access(icon->path, W_OK) != 0)
1435 type = NULL;
1438 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1439 if (type)
1441 gdk_drag_status(context, action, time);
1442 g_dataset_set_data_full(context, "drop_dest_path",
1443 g_strdup(icon->path), g_free);
1444 if (type == drop_dest_dir)
1445 dnd_spring_load(context, NULL);
1447 if (dnd_highlight && dnd_highlight != pi->widget)
1449 gtk_drag_unhighlight(dnd_highlight);
1450 dnd_highlight = NULL;
1453 if (dnd_highlight == NULL)
1455 gtk_drag_highlight(pi->widget);
1456 dnd_highlight = pi->widget;
1460 return type != NULL;
1464 static void add_uri_list(GtkWidget *widget,
1465 GdkDragContext *context,
1466 gint x,
1467 gint y,
1468 GtkSelectionData *selection_data,
1469 guint info,
1470 guint32 time,
1471 Panel *panel)
1473 gboolean after = FALSE;
1474 GList *uris, *next;
1476 if (!selection_data->data)
1477 return;
1479 g_return_if_fail(selection_data->data[selection_data->length] == '\0');
1481 if (g_object_get_data(G_OBJECT(widget), "after"))
1482 after = TRUE;
1484 uris = uri_list_to_glist(selection_data->data);
1486 for (next = uris; next; next = next->next)
1488 guchar *path;
1490 path = get_local_path((EscapedPath *) next->data);
1492 if (path) {
1493 panel_add_item(panel, path, NULL, after, NULL, NULL, FALSE);
1494 g_free(path);
1498 g_list_free(uris);
1501 static void drag_end(GtkWidget *widget,
1502 GdkDragContext *context,
1503 Icon *icon)
1505 if (tmp_icon_selected)
1507 icon_select_only(NULL);
1508 tmp_icon_selected = FALSE;
1512 static void drag_leave(GtkWidget *widget,
1513 GdkDragContext *context,
1514 guint32 time,
1515 Icon *icon)
1517 panel_drag_leave(widget, context, time, ((PanelIcon *) icon)->panel);
1519 if (dnd_highlight && dnd_highlight == widget)
1521 gtk_drag_unhighlight(dnd_highlight);
1522 dnd_highlight = NULL;
1525 dnd_spring_abort();
1528 /* Create XML icon nodes for these widgets.
1529 * Always frees the widgets list.
1531 static void make_widgets(xmlNodePtr side, GList *widgets)
1533 GList *next;
1535 for (next = widgets; next; next = next->next)
1537 Icon *icon;
1538 xmlNodePtr tree;
1540 icon = g_object_get_data(G_OBJECT(next->data), "icon");
1542 if (!icon)
1544 g_warning("Can't find Icon from widget\n");
1545 continue;
1548 tree = xmlNewTextChild(side, NULL, "icon", icon->src_path);
1550 xmlSetProp(tree, "label", icon->item->leafname);
1551 if (icon->shortcut)
1552 xmlSetProp(tree, "shortcut", icon->shortcut);
1553 if (icon->args)
1554 xmlSetProp(tree, "args", icon->args);
1555 if (icon->locked)
1556 xmlSetProp(tree, "locked", "true");
1559 if (widgets)
1560 g_list_free(widgets);
1563 void panel_save(Panel *panel)
1565 xmlDocPtr doc;
1566 xmlNodePtr root;
1567 xmlNodePtr options;
1568 guchar *save = NULL;
1569 guchar *save_new = NULL;
1571 g_return_if_fail(panel != NULL);
1573 if (strchr(panel->name, '/'))
1574 save = g_strdup(panel->name);
1575 else
1577 guchar *leaf;
1579 leaf = g_strconcat("pan_", panel->name, NULL);
1580 save = choices_find_xdg_path_save(leaf, PROJECT, SITE, TRUE);
1581 g_free(leaf);
1584 if (!save)
1585 return;
1587 doc = xmlNewDoc("1.0");
1588 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "panel", NULL));
1590 root = xmlDocGetRootElement(doc);
1592 xmlSetProp(root, "side", panel_side_to_name(panel->side));
1594 options = xmlNewChild(root, NULL, "options", NULL);
1595 set_int_prop(options, "style", panel->style);
1596 set_int_prop(options, "width", panel->width);
1597 set_int_prop(options, "avoid", panel->avoid);
1598 set_int_prop(options, "xinerama", panel->xinerama);
1599 set_int_prop(options, "monitor", panel->monitor);
1601 make_widgets(xmlNewChild(root, NULL, "start", NULL),
1602 gtk_container_get_children(GTK_CONTAINER(panel->before)));
1604 make_widgets(xmlNewChild(root, NULL, "end", NULL),
1605 g_list_reverse(gtk_container_get_children(
1606 GTK_CONTAINER(panel->after))));
1608 save_new = g_strconcat(save, ".new", NULL);
1609 if (save_xml_file(doc, save_new) || rename(save_new, save))
1610 delayed_error(_("Error saving panel %s: %s"),
1611 save, g_strerror(errno));
1612 g_free(save_new);
1614 g_free(save);
1615 if (doc)
1616 xmlFreeDoc(doc);
1619 /* Create a frame widget which can be used to add icons to the panel */
1620 static GtkWidget *make_insert_frame(Panel *panel)
1622 GtkWidget *frame;
1623 GtkTargetEntry target_table[] = {
1624 {"text/uri-list", 0, TARGET_URI_LIST},
1627 frame = gtk_frame_new(NULL);
1628 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1629 gtk_widget_set_size_request(frame, 16, 16);
1631 g_signal_connect(frame, "drag-motion",
1632 G_CALLBACK(insert_drag_motion), panel);
1633 g_signal_connect(frame, "drag-leave",
1634 G_CALLBACK(panel_drag_leave), panel);
1636 g_signal_connect(frame, "drag-data-received",
1637 G_CALLBACK(add_uri_list), panel);
1638 gtk_drag_dest_set(frame,
1639 GTK_DEST_DEFAULT_ALL,
1640 target_table,
1641 sizeof(target_table) / sizeof(*target_table),
1642 GDK_ACTION_COPY);
1644 return frame;
1647 static gboolean enter_icon(GtkWidget *widget,
1648 GdkEventCrossing *event,
1649 Icon *icon)
1651 icon_may_update(icon);
1652 panel_icon_set_tip((PanelIcon *) icon);
1654 return FALSE;
1657 static gint panel_leave_event(GtkWidget *widget,
1658 GdkEventCrossing *event,
1659 Panel *panel)
1661 GdkWindow *pinboard;
1663 if (event->mode != GDK_CROSSING_NORMAL)
1664 return FALSE; /* Grab for menu, DnD, etc */
1666 if (panel_keep_below(panel, TRUE))
1668 /* Shouldn't need this as well as keep_below but some WMs don't
1669 * automatically lower as soon as the hint is set */
1670 pinboard = pinboard_get_window();
1671 window_put_just_above(panel->window->window, pinboard);
1674 return FALSE;
1677 /* If (x, y) is at the edge of the panel then raise */
1678 static void motion_may_raise(Panel *panel, int x, int y)
1680 gboolean raise;
1682 if (panel->side == PANEL_TOP)
1683 raise = y == 0;
1684 else if (panel->side == PANEL_BOTTOM)
1685 raise = y == panel->window->allocation.height - 1;
1686 else if (panel->side == PANEL_LEFT)
1687 raise = x == 0;
1688 else
1689 raise = x == panel->window->allocation.width - 1;
1691 if (raise && panel_keep_below(panel, FALSE))
1693 /* Shouldn't need this as well as keep_below but some WMs don't
1694 * automatically raise as soon as the hint is set */
1695 gdk_window_raise(panel->window->window);
1699 static gboolean may_autoscroll(Panel *panel)
1701 gboolean horz = panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM;
1702 gint max, panel_x, panel_y, delta, new;
1704 if (panel->adj->upper <= panel->adj->page_size)
1705 goto stop_scrolling; /* Can see everything already */
1707 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
1709 if (horz)
1711 delta = panel_x;
1712 max = panel->window->allocation.width;
1713 if (panel_y < 0 || panel_y > panel->window->allocation.height)
1714 goto stop_scrolling; /* Not over the panel */
1716 else
1718 delta = panel_y;
1719 max = panel->window->allocation.height;
1720 if (panel_x < 0 || panel_x > panel->window->allocation.width)
1721 goto stop_scrolling; /* Not over the panel */
1724 if (delta >= 20 && delta <= max - 20)
1725 goto stop_scrolling; /* Not at either end */
1727 panel->autoscroll_speed = MIN(panel->autoscroll_speed + 2, 200);
1729 new = panel->adj->value - ((delta < 20) ? panel->autoscroll_speed
1730 : -panel->autoscroll_speed);
1731 new = CLAMP(new, 0, panel->adj->upper - panel->adj->page_size);
1732 gtk_adjustment_set_value(panel->adj, new);
1734 panel->autoscroll_to = g_timeout_add(40,
1735 (GSourceFunc) may_autoscroll, panel);
1737 return FALSE;
1739 stop_scrolling:
1740 panel->autoscroll_speed = 0;
1741 return FALSE;
1744 static gint panel_motion_event(GtkWidget *widget,
1745 GdkEventMotion *event,
1746 Panel *panel)
1748 motion_may_raise(panel, event->x, event->y);
1750 if (motion_state != MOTION_REPOSITION)
1751 if (panel->autoscroll_speed == 0)
1752 may_autoscroll(panel);
1754 return FALSE;
1757 static gint icon_motion_event(GtkWidget *widget,
1758 GdkEventMotion *event,
1759 PanelIcon *pi)
1761 Panel *panel = pi->panel;
1762 GList *list, *me;
1763 gboolean horz = panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM;
1764 int val;
1765 int dir = 0;
1767 if (motion_state == MOTION_READY_FOR_DND)
1769 if (dnd_motion_moved(event))
1770 start_drag(pi, event);
1771 return TRUE;
1773 else if (motion_state != MOTION_REPOSITION)
1774 return FALSE;
1776 list = gtk_container_get_children(GTK_CONTAINER(panel->before));
1777 list = g_list_append(list, NULL); /* The gap in the middle */
1778 list = g_list_concat(list,
1779 gtk_container_get_children(GTK_CONTAINER(panel->after)));
1780 me = g_list_find(list, widget);
1782 g_return_val_if_fail(me != NULL, TRUE);
1784 val = horz ? event->x_root : event->y_root;
1786 if (me->prev)
1788 GtkWidget *prev;
1789 int x, y;
1791 if (me->prev->data)
1792 prev = GTK_WIDGET(me->prev->data);
1793 else
1794 prev = panel->gap;
1796 gdk_window_get_origin(prev->window, &x, &y);
1798 if (val <= (horz ? x : y))
1799 dir = -1;
1802 if (dir == 0 && me->next)
1804 GtkWidget *next;
1805 int x, y, w, h;
1807 if (me->next->data)
1808 next = GTK_WIDGET(me->next->data);
1809 else
1810 next = panel->gap;
1812 gdk_window_get_origin(next->window, &x, &y);
1814 gdk_drawable_get_size(next->window, &w, &h);
1816 x += w;
1817 y += h;
1819 if (val >= (horz ? x : y)-1)
1821 if (next == panel->gap)
1822 dir = +2;
1823 else
1824 dir = +1;
1828 if (dir)
1829 reposition_icon(pi, g_list_index(list, widget) + dir);
1831 return TRUE;
1834 static void reposition_icon_on_side(GtkWidget *side, GtkWidget *widget,
1835 int index)
1837 GList *list;
1839 list = gtk_container_get_children(GTK_CONTAINER(side));
1841 /* Want to move icon to the list in the given 'side'. Is it there
1842 * already?
1845 if (!g_list_find(list, widget))
1847 /* No, reparent */
1848 gtk_grab_remove(widget);
1849 gtk_widget_reparent(widget, side);
1850 dnd_motion_grab_pointer();
1851 gtk_grab_add(widget);
1854 gtk_box_reorder_child(GTK_BOX(side), widget, index);
1856 g_list_free(list);
1859 /* Move icon to this index in the complete widget list.
1860 * 0 makes the icon the left-most icon. The gap in the middle has
1861 * an index number, which allows you to specify that the icon should
1862 * go on the left or right side.
1864 static void reposition_icon(PanelIcon *pi, int index)
1866 Panel *panel = pi->panel;
1867 GtkWidget *widget = pi->widget;
1868 GList *list;
1869 int before_len;
1871 list = gtk_container_get_children(GTK_CONTAINER(panel->before));
1872 before_len = g_list_length(list);
1873 g_list_free(list);
1875 if (index <= before_len)
1876 reposition_icon_on_side(panel->before, widget, index);
1877 else
1878 reposition_icon_on_side(panel->after, widget,
1879 index - (before_len + 1));
1881 panel_save(panel);
1884 static void start_drag(PanelIcon *pi, GdkEventMotion *event)
1886 GtkWidget *widget = pi->widget;
1887 Icon *icon = (Icon *) pi;
1889 if (!icon->selected)
1891 if (event->state & GDK_BUTTON1_MASK)
1893 /* Select just this one */
1894 icon_select_only(icon);
1895 tmp_icon_selected = TRUE;
1897 else
1898 icon_set_selected(icon, TRUE);
1901 g_return_if_fail(icon_selection != NULL);
1903 if (icon_selection->next == NULL)
1904 drag_one_item(widget, event, icon->path, icon->item, NULL);
1905 else
1907 guchar *uri_list;
1909 uri_list = icon_create_uri_list();
1910 drag_selection(widget, event, uri_list);
1911 g_free(uri_list);
1915 static void applet_died(GtkWidget *socket)
1917 gboolean never_plugged;
1919 never_plugged = (!g_object_get_data(G_OBJECT(socket), "lost_plug"))
1920 && !GTK_SOCKET(socket)->plug_window;
1922 if (never_plugged)
1924 report_error(
1925 _("Applet quit without ever creating a widget!"));
1926 gtk_widget_destroy(socket);
1929 gtk_widget_unref(socket);
1932 static void socket_destroyed(GtkWidget *socket, GtkWidget *widget)
1934 g_object_set_data(G_OBJECT(socket), "lost_plug", "yes");
1936 gtk_widget_unref(socket);
1938 gtk_widget_destroy(widget); /* Remove from panel */
1940 if (!closing_panel)
1941 panel_save(g_object_get_data(G_OBJECT(socket), "panel"));
1944 /* Try to run this applet.
1945 * Cases:
1947 * - No executable AppletRun:
1948 * icon->socket == NULL (unchanged) on return.
1950 * Otherwise, create socket (setting icon->socket) and ref it twice.
1952 * - AppletRun quits without connecting a plug:
1953 * On child death lost_plug is unset and socket is empty.
1954 * Unref socket.
1955 * Report error and destroy widget (to 'socket destroyed').
1957 * - AppletRun quits while plug is in socket:
1958 * Unref socket once. Socket will be destroyed later.
1960 * - Socket is destroyed.
1961 * Set lost_plug = "yes" and remove widget from panel.
1962 * Unref socket.
1964 static void run_applet(PanelIcon *pi)
1966 GError *error = NULL;
1967 char *argv[3];
1968 gint pid;
1969 Icon *icon = (Icon *) pi;
1971 argv[0] = (char *) make_path(icon->path, "AppletRun");
1973 if (access(argv[0], X_OK) != 0)
1974 return;
1976 pi->socket = gtk_socket_new();
1978 gtk_container_add(GTK_CONTAINER(pi->widget), pi->socket);
1979 gtk_widget_show_all(pi->socket);
1980 gtk_widget_realize(pi->socket);
1982 /* Always get button-2 events so we can drag */
1983 XGrabButton(gdk_display, Button2, AnyModifier,
1984 GDK_WINDOW_XWINDOW(pi->socket->window),
1985 False,
1986 ButtonPressMask | ButtonReleaseMask | Button2MotionMask,
1987 GrabModeAsync, /* Pointer */
1988 GrabModeAsync, /* Keyboard */
1989 None, None);
1992 gchar *pos;
1993 PanelSide side = pi->panel->side;
1995 /* Set a hint to let applets position their menus correctly */
1996 pos = g_strdup_printf("%s,%d",
1997 panel_side_to_name(side), MENU_MARGIN(side));
1998 gdk_property_change(pi->socket->window,
1999 gdk_atom_intern("_ROX_PANEL_MENU_POS", FALSE),
2000 gdk_atom_intern("STRING", FALSE),
2001 8, GDK_PROP_MODE_REPLACE,
2002 pos, strlen(pos));
2003 g_free(pos);
2005 /* Ensure that the properties are set before starting the
2006 * applet.
2008 gdk_flush();
2011 g_object_set_data(G_OBJECT(pi->widget), "icon", pi);
2012 g_object_set_data(G_OBJECT(pi->socket), "panel", pi->panel);
2014 argv[1] = g_strdup_printf("%ld",
2015 GDK_WINDOW_XWINDOW(pi->socket->window));
2016 argv[2] = NULL;
2018 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
2019 NULL, NULL, &pid, &error))
2021 delayed_error(_("Error running applet:\n%s"), error->message);
2022 g_error_free(error);
2023 gtk_widget_destroy(pi->socket);
2024 pi->socket = NULL;
2026 else
2028 gtk_widget_ref(pi->socket);
2029 on_child_death(pid, (CallbackFn) applet_died, pi->socket);
2031 gtk_widget_ref(pi->socket);
2032 g_signal_connect(pi->socket, "destroy",
2033 G_CALLBACK(socket_destroyed), pi->widget);
2036 g_free(argv[1]);
2039 static void panel_post_resize(GtkWidget *win, GtkRequisition *req, Panel *panel)
2041 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
2043 req->width = panel->geometry.width;
2044 req->height += EDGE_WIDTH;
2046 else
2048 int h = panel->geometry.height;
2050 if (current_panel[PANEL_TOP])
2052 GtkWidget *win = current_panel[PANEL_TOP]->window;
2053 h -= win->allocation.height;
2056 if (current_panel[PANEL_BOTTOM])
2058 GtkWidget *win = current_panel[PANEL_BOTTOM]->window;
2059 h -= win->allocation.height;
2062 req->height = h;
2063 req->width += EDGE_WIDTH;
2067 static void update_side(GtkWidget *side)
2069 GList *kids, *next;
2071 kids = gtk_container_get_children(GTK_CONTAINER(side));
2072 for (next = kids; next; next = next->next)
2074 PanelIcon *pi;
2075 pi = g_object_get_data(next->data, "icon");
2076 panel_icon_set_tip(pi);
2078 g_list_free(kids);
2081 /* Tips or style has changed -- update everything on this panel */
2082 static void panel_set_style(Panel *panel)
2084 update_side(panel->before);
2085 update_side(panel->after);
2086 gtk_widget_queue_resize(panel->window);
2089 static gboolean recreate_panels(char **names)
2091 int i;
2093 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2095 if (names[i])
2097 panel_new(names[i], i);
2098 g_free(names[i]);
2102 g_free(names);
2104 return FALSE;
2107 static void update_side_size(GtkWidget *side)
2109 GList *kids, *next;
2111 kids = gtk_container_get_children(GTK_CONTAINER(side));
2112 for (next = kids; next; next = next->next)
2114 PanelIcon *pi;
2115 pi = g_object_get_data(next->data, "icon");
2116 gtk_widget_queue_resize(pi->widget);
2118 g_list_free(kids);
2121 /* Update panel size and redraw */
2122 static void panel_update(Panel *panel)
2124 update_side_size(panel->before);
2125 update_side_size(panel->after);
2126 gtk_widget_queue_resize(panel->window);
2127 gtk_widget_queue_draw(panel->window);
2130 static void panel_style_changed(void)
2132 int i;
2134 if (o_override_redirect.has_changed)
2136 gchar **names;
2138 names = g_new(char *, PANEL_NUMBER_OF_SIDES);
2140 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2142 Panel *panel = current_panel[i];
2143 names[i] = panel ? g_strdup(panel->name) : NULL;
2144 panel_new(NULL, i);
2147 g_idle_add((GtkFunction) recreate_panels, names);
2151 static gboolean draw_panel_edge(GtkWidget *widget, GdkEventExpose *event,
2152 Panel *panel)
2154 int x, y, width, height;
2156 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
2158 width = panel->geometry.width;
2159 height = EDGE_WIDTH;
2161 x = 0;
2162 if (panel->side == PANEL_BOTTOM)
2163 y = 0;
2164 else
2165 y = widget->allocation.height - EDGE_WIDTH;
2167 else
2169 width = EDGE_WIDTH;
2170 height = panel->geometry.height;
2172 y = 0;
2173 if (panel->side == PANEL_RIGHT)
2174 x = 0;
2175 else
2176 x = widget->allocation.width - EDGE_WIDTH;
2179 gdk_draw_rectangle(widget->window,
2180 widget->style->fg_gc[GTK_STATE_NORMAL], TRUE,
2181 x, y, width, height);
2183 return FALSE;
2186 static gpointer parent_class;
2188 static void panel_icon_destroy(Icon *icon)
2190 PanelIcon *pi = (PanelIcon *) icon;
2192 g_return_if_fail(pi != NULL);
2194 if (pi->image)
2195 g_object_unref(pi->image);
2197 g_return_if_fail(pi->widget != NULL);
2199 gtk_widget_destroy(pi->widget);
2202 static void panel_remove_items(void)
2204 Panel *panel;
2206 g_return_if_fail(icon_selection != NULL);
2208 panel = ((PanelIcon *) icon_selection->data)->panel;
2210 while (icon_selection)
2211 icon_destroy((Icon *) icon_selection->data);
2213 panel_save(panel);
2216 /* Icon's size, shape or appearance has changed - update the display */
2217 static void panel_icon_redraw(Icon *icon)
2219 PanelIcon *pi = (PanelIcon *) icon;
2221 gtk_widget_set_state(pi->widget,
2222 icon->selected ? GTK_STATE_SELECTED
2223 : GTK_STATE_NORMAL);
2225 /* Will regenerate the scaled icon from the new image */
2226 gtk_widget_queue_resize(pi->widget);
2228 panel_icon_set_tip((PanelIcon *) icon);
2231 static void panel_icon_update(Icon *icon)
2233 PanelIcon *pi = (PanelIcon *) icon;
2235 gtk_widget_queue_draw(pi->widget);
2236 gtk_label_set_text(GTK_LABEL(pi->label), icon->item->leafname);
2237 panel_save(pi->panel);
2240 /* The point of this is to clear the selection if the existing icons
2241 * aren't from the same panel...
2243 static gboolean panel_icon_same_group(Icon *icon, Icon *other)
2245 if (IS_PANEL_ICON(other))
2247 PanelIcon *a = (PanelIcon *) icon;
2248 PanelIcon *b = (PanelIcon *) other;
2250 return a->panel == b->panel;
2252 else
2253 return FALSE;
2256 static void panel_icon_class_init(gpointer gclass, gpointer data)
2258 IconClass *icon = (IconClass *) gclass;
2260 parent_class = g_type_class_peek_parent(gclass);
2262 icon->destroy = panel_icon_destroy;
2263 icon->redraw = panel_icon_redraw;
2264 icon->update = panel_icon_update;
2265 icon->remove_items = panel_remove_items;
2266 icon->same_group = panel_icon_same_group;
2267 icon->wink = panel_icon_wink;
2270 static void panel_icon_init(GTypeInstance *object, gpointer gclass)
2272 PanelIcon *pi = (PanelIcon *) object;
2274 pi->widget = NULL;
2275 pi->image = NULL;
2276 pi->label = NULL;
2277 pi->socket = NULL;
2278 pi->style = TEXT_UNDER_ICON;
2281 static GType panel_icon_get_type(void)
2283 static GType type = 0;
2285 if (!type)
2287 static const GTypeInfo info =
2289 sizeof (PanelIconClass),
2290 NULL, /* base_init */
2291 NULL, /* base_finalise */
2292 panel_icon_class_init,
2293 NULL, /* class_finalise */
2294 NULL, /* class_data */
2295 sizeof(PanelIcon),
2296 0, /* n_preallocs */
2297 panel_icon_init
2300 type = g_type_register_static(icon_get_type(),
2301 "PanelIcon", &info, 0);
2304 return type;
2307 static PanelIcon *panel_icon_new(Panel *panel,
2308 const char *pathname,
2309 const char *name)
2311 PanelIcon *pi;
2312 Icon *icon;
2314 pi = g_object_new(panel_icon_get_type(), NULL);
2315 icon = (Icon *) pi;
2317 icon_set_path(icon, pathname, name);
2318 pi->panel = panel;
2320 return pi;
2323 static gboolean panel_want_show_text(PanelIcon *pi)
2325 Icon *icon = (Icon *) pi;
2326 Panel *panel = pi->panel;
2328 if (!icon->item->leafname[0])
2329 return FALSE;
2331 if (panel->style == SHOW_BOTH)
2332 return TRUE;
2333 if (panel->style == SHOW_ICON)
2334 return FALSE;
2336 if (icon->item->flags & ITEM_FLAG_APPDIR)
2337 return FALSE;
2339 if (EXECUTABLE_FILE(icon->item))
2340 return FALSE;
2342 return TRUE;
2345 static void xinerama_sensitive(GladeXML *glade, gboolean sensitive)
2347 gtk_widget_set_sensitive(
2348 glade_xml_get_widget(glade, "panel_xinerama_monitor"),
2349 sensitive);
2352 inline static Panel *panel_from_opts_widget(GtkWidget *widget)
2354 return g_object_get_data(G_OBJECT(gtk_widget_get_toplevel(widget)),
2355 "rox-panel");
2358 static void panel_style_radio_toggled(GtkToggleButton *widget, int style)
2360 Panel *panel;
2362 if (!gtk_toggle_button_get_active(widget))
2363 return;
2364 panel = panel_from_opts_widget(GTK_WIDGET(widget));
2365 if (style != panel->style)
2367 panel->style = style;
2368 panel_set_style(panel);
2369 panel_save(panel);
2373 static void panel_xinerama_changed(Panel *panel)
2375 panel_update_geometry(panel);
2376 reposition_panel(panel->window, &panel->window->allocation, panel);
2377 gtk_widget_queue_resize(panel->window);
2378 panel_save(panel);
2381 static void panel_side_radio_toggled(GtkWidget *widget, PanelSide new_side)
2383 Panel *panel;
2384 PanelSide old_side;
2385 char *name, *other_side_name;
2387 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
2388 return;
2390 panel = panel_from_opts_widget(widget);
2391 old_side = panel->side;
2392 if (new_side == old_side)
2393 return;
2395 name = g_strdup(panel->name);
2396 other_side_name = current_panel[new_side]
2397 ? g_strdup(current_panel[new_side]->name)
2398 : NULL;
2400 panel_new(name, new_side);
2401 g_object_set_data(G_OBJECT(gtk_widget_get_toplevel(widget)),
2402 "rox-panel", current_panel[new_side]);
2403 panel_new(other_side_name, old_side);
2405 g_free(name);
2406 g_free(other_side_name);
2409 static void panel_style_radio_0_toggled_cb(GtkToggleButton *widget)
2411 panel_style_radio_toggled(widget, 0);
2414 static void panel_style_radio_1_toggled_cb(GtkToggleButton *widget)
2416 panel_style_radio_toggled(widget, 1);
2419 static void panel_style_radio_2_toggled_cb(GtkToggleButton *widget)
2421 panel_style_radio_toggled(widget, 2);
2424 static void panel_width_changed_cb(GtkSpinButton *widget)
2426 Panel *panel = panel_from_opts_widget(GTK_WIDGET(widget));
2427 int width = gtk_spin_button_get_value_as_int(widget);
2429 if (width != panel->width)
2431 panel->width = width;
2432 panel_update(panel);
2433 panel_save(panel);
2437 static void panel_avoid_toggled_cb(GtkToggleButton *widget)
2439 Panel *panel = panel_from_opts_widget(GTK_WIDGET(widget));
2440 gboolean avoid = gtk_toggle_button_get_active(widget);
2442 if (avoid != panel->avoid)
2444 panel->avoid = avoid;
2445 panel_setup_struts(panel, &panel->window->allocation);
2446 panel_save(panel);
2450 static void panel_xinerama_confine_toggled_cb(GtkWidget *widget)
2452 Panel *panel = panel_from_opts_widget(widget);
2453 gboolean xinerama = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
2455 xinerama_sensitive(glade_get_widget_tree(gtk_widget_get_toplevel(widget)),
2456 xinerama);
2457 if (xinerama != panel->xinerama)
2459 panel->xinerama = xinerama;
2460 panel_xinerama_changed(panel);
2464 static void panel_xinerama_monitor_changed_cb(GtkSpinButton *widget)
2466 Panel *panel = panel_from_opts_widget(GTK_WIDGET(widget));
2467 int monitor = gtk_spin_button_get_value_as_int(widget);
2469 if (monitor != panel->monitor)
2471 panel->monitor = monitor;
2472 panel_xinerama_changed(panel);
2476 static void panel_pos_top_toggled_cb(GtkWidget *widget)
2478 panel_side_radio_toggled(widget, PANEL_TOP);
2481 static void panel_pos_bottom_toggled_cb(GtkWidget *widget)
2483 panel_side_radio_toggled(widget, PANEL_BOTTOM);
2486 static void panel_pos_left_toggled_cb(GtkWidget *widget)
2488 panel_side_radio_toggled(widget, PANEL_LEFT);
2491 static void panel_pos_right_toggled_cb(GtkWidget *widget)
2493 panel_side_radio_toggled(widget, PANEL_RIGHT);
2496 #define CONNECT_GLADE_CB(glade, handler) \
2497 glade_xml_signal_connect(glade, #handler, G_CALLBACK(handler))
2499 static void panel_connect_dialog_signal_handlers(GladeXML *glade)
2501 CONNECT_GLADE_CB(glade, gtk_widget_destroy);
2502 CONNECT_GLADE_CB(glade, panel_style_radio_0_toggled_cb);
2503 CONNECT_GLADE_CB(glade, panel_style_radio_1_toggled_cb);
2504 CONNECT_GLADE_CB(glade, panel_style_radio_2_toggled_cb);
2505 CONNECT_GLADE_CB(glade, panel_width_changed_cb);
2506 CONNECT_GLADE_CB(glade, panel_avoid_toggled_cb);
2507 CONNECT_GLADE_CB(glade, panel_xinerama_confine_toggled_cb);
2508 CONNECT_GLADE_CB(glade, panel_xinerama_monitor_changed_cb);
2509 CONNECT_GLADE_CB(glade, panel_pos_top_toggled_cb);
2510 CONNECT_GLADE_CB(glade, panel_pos_bottom_toggled_cb);
2511 CONNECT_GLADE_CB(glade, panel_pos_left_toggled_cb);
2512 CONNECT_GLADE_CB(glade, panel_pos_right_toggled_cb);
2515 static void panel_setup_options_dialog(GladeXML *glade, Panel *panel)
2517 char *wnm;
2518 const char *pos_radio;
2520 wnm = g_strdup_printf("panel_style_radio_%d", panel->style);
2521 gtk_toggle_button_set_active(
2522 GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade, wnm)),
2523 TRUE);
2524 g_free(wnm);
2525 gtk_spin_button_set_value(
2526 GTK_SPIN_BUTTON(glade_xml_get_widget(glade, "panel_width")),
2527 panel->width);
2528 gtk_toggle_button_set_active(
2529 GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade, "panel_avoid")),
2530 panel->avoid);
2531 gtk_toggle_button_set_active(
2532 GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade,
2533 "panel_xinerama_confine")),
2534 panel->xinerama);
2535 gtk_spin_button_set_adjustment(
2536 GTK_SPIN_BUTTON(glade_xml_get_widget(glade, "panel_xinerama_monitor")),
2537 GTK_ADJUSTMENT(gtk_adjustment_new(MAX(0, panel->monitor),
2538 0, n_monitors - 1, 1, 10, 1)));
2539 xinerama_sensitive(glade, panel->xinerama);
2540 switch (panel->side)
2542 case PANEL_TOP:
2543 pos_radio = "panel_pos_top";
2544 break;
2545 case PANEL_BOTTOM:
2546 pos_radio = "panel_pos_bottom";
2547 break;
2548 case PANEL_LEFT:
2549 pos_radio = "panel_pos_left";
2550 break;
2551 case PANEL_RIGHT:
2552 pos_radio = "panel_pos_right";
2553 break;
2554 default:
2555 pos_radio = NULL;
2556 break;
2558 g_return_if_fail(pos_radio != NULL);
2559 gtk_toggle_button_set_active(
2560 GTK_TOGGLE_BUTTON(glade_xml_get_widget(glade, pos_radio)),
2561 TRUE);
2564 static void panel_show_options(Panel *panel)
2566 GtkWidget *dialog;
2567 gboolean already_showing = FALSE;
2568 GladeXML *glade = NULL;
2570 if (panel_options_dialog)
2572 dialog = panel_options_dialog;
2573 already_showing = TRUE;
2574 glade = glade_get_widget_tree(dialog);
2576 else
2578 glade = get_glade_xml("Panel Options");
2579 dialog = glade_xml_get_widget(glade, "Panel Options");
2580 panel_options_dialog = dialog;
2581 g_signal_connect(dialog, "destroy",
2582 G_CALLBACK(gtk_widget_destroyed),
2583 &panel_options_dialog);
2584 panel_connect_dialog_signal_handlers(glade);
2586 g_object_set_data(G_OBJECT(panel_options_dialog), "rox-panel", panel);
2588 panel_setup_options_dialog(glade, panel);
2590 if (already_showing)
2592 GtkWindow *win = GTK_WINDOW(dialog);
2594 gtk_widget_hide(dialog);
2595 /* This extra set_position() should ensure it moves to new position
2596 * under pointer */
2597 gtk_window_set_position(win, GTK_WIN_POS_CENTER_ALWAYS);
2598 gtk_window_set_position(win, GTK_WIN_POS_MOUSE);
2599 gtk_window_present(win);
2601 else
2603 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
2604 gtk_widget_show_all(dialog);
2608 static void panel_position_menu(GtkMenu *menu, gint *x, gint *y,
2609 gboolean *push_in, gpointer data)
2611 int *pos = (int *) data;
2612 GtkRequisition requisition;
2613 int margin = pos[2];
2614 int mon = pos[3];
2615 int mon_right = monitor_geom[mon].x +
2616 monitor_geom[mon].width;
2617 int mon_bottom = monitor_geom[mon].y +
2618 monitor_geom[mon].height;
2620 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
2622 if (pos[0] == -1)
2623 *x = mon_right - margin - requisition.width;
2624 else if (pos[0] == -2)
2625 *x = monitor_geom[mon].x + margin;
2626 else
2627 *x = pos[0] - (requisition.width >> 2);
2629 if (pos[1] == -1)
2630 *y = mon_bottom - margin - requisition.height;
2631 else if (pos[1] == -2)
2632 *y = monitor_geom[mon].y + margin;
2633 else
2634 *y = pos[1] - (requisition.height >> 2);
2636 *x = CLAMP(*x, 0, mon_right - requisition.width);
2637 *y = CLAMP(*y, 0, mon_bottom - requisition.height);
2639 *push_in = FALSE;
2642 static void panel_remove_callback(PanelSide side)
2644 GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
2645 GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL,
2646 _("Are you sure you want to remove this panel from the desktop?"));
2648 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
2649 gtk_window_set_title(GTK_WINDOW(dialog), _("Remove Panel"));
2650 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
2651 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
2652 panel_new(NULL, side);
2653 gtk_widget_destroy(dialog);
2656 static void panel_show_menu(GdkEventButton *event, PanelIcon *pi, Panel *panel)
2658 GtkWidget *option_item;
2659 GtkWidget *del_item;
2660 PanelSide side = panel->side;
2661 int pos[4];
2663 pos[0] = event->x_root;
2664 pos[1] = event->y_root;
2665 pos[2] = MENU_MARGIN(side);
2666 /* FIXME: Should we read screen from event's window rather than
2667 * using default? */
2668 pos[3] = gdk_screen_get_monitor_at_point(
2669 gdk_screen_get_default(),
2670 event->x_root, event->y_root);
2672 option_item = gtk_image_menu_item_new_with_label(_("Panel Options..."));
2673 g_signal_connect_swapped(option_item, "activate",
2674 G_CALLBACK(panel_show_options), panel);
2676 del_item = gtk_image_menu_item_new_with_label(_("Remove Panel"));
2677 add_stock_to_menu_item(del_item, GTK_STOCK_REMOVE);
2678 g_signal_connect_swapped(del_item, "activate",
2679 G_CALLBACK(panel_remove_callback), GINT_TO_POINTER(side));
2681 icon_prepare_menu((Icon *) pi, option_item, del_item, NULL);
2683 if (side == PANEL_LEFT)
2684 pos[0] = -2;
2685 else if (side == PANEL_RIGHT)
2686 pos[0] = -1;
2688 if (side == PANEL_TOP)
2689 pos[1] = -2;
2690 else if (side == PANEL_BOTTOM)
2691 pos[1] = -1;
2693 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2694 panel_position_menu,
2695 (gpointer) pos, event->button, event->time);
2698 /* Note: also called from icon handler */
2699 static gboolean panel_drag_motion(GtkWidget *widget,
2700 GdkDragContext *context,
2701 gint x,
2702 gint y,
2703 guint time,
2704 Panel *panel)
2706 int panel_x, panel_y;
2708 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
2710 motion_may_raise(panel, panel_x, panel_y);
2711 gdk_drag_status(context, 0, time);
2713 return TRUE;
2716 static gboolean insert_drag_motion(GtkWidget *widget,
2717 GdkDragContext *context,
2718 gint x,
2719 gint y,
2720 guint time,
2721 Panel *panel)
2723 int panel_x, panel_y;
2725 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
2726 motion_may_raise(panel, panel_x, panel_y);
2728 return FALSE;
2731 /* Note: also called from icon handler */
2732 static void panel_drag_leave(GtkWidget *widget,
2733 GdkDragContext *context,
2734 guint32 time,
2735 Panel *panel)
2737 GdkWindow *pinboard, *window;
2738 GtkAllocation *alloc = &panel->window->allocation;
2739 int x, y;
2741 window = panel->window->window;
2742 gdk_window_get_pointer(window, &x, &y, NULL);
2743 if ((x < 0 || y < 0 || x > alloc->width || y > alloc->height) &&
2744 panel_keep_below(panel, TRUE))
2746 /* Shouldn't need this as well as keep_below but some WMs don't
2747 * automatically lower as soon as the hint is set */
2748 pinboard = pinboard_get_window();
2749 window_put_just_above(panel->window->window, pinboard);
2753 static void panel_update_geometry(Panel *panel)
2755 if (panel->xinerama && panel->monitor >= n_monitors)
2757 g_warning(_("Xinerama monitor %d unavailable"), panel->monitor);
2758 panel->xinerama = FALSE;
2761 if (panel->xinerama)
2763 panel->geometry = monitor_geom[panel->monitor];
2765 else
2767 panel->geometry.x = panel->geometry.y = 0;
2768 panel->geometry.width = screen_width;
2769 panel->geometry.height = screen_height;
2773 static GList *build_monitor_number(Option *option, xmlNode *node, guchar *label)
2775 GtkObject *adj;
2777 adj = gtk_adjustment_new(MAX(0, panel_monitor),
2778 0, n_monitors - 1, 1, 10, 1);
2779 return build_numentry_base(option, node, label, GTK_ADJUSTMENT(adj));
2782 static const char *panel_side_to_translated_name(PanelSide side)
2784 switch (side)
2786 case PANEL_TOP:
2787 return _("Top");
2788 case PANEL_BOTTOM:
2789 return _("Bottom");
2790 case PANEL_LEFT:
2791 return _("Left");
2792 case PANEL_RIGHT:
2793 return _("Right");
2794 case PANEL_DEFAULT_SIDE:
2795 return _("Default");
2796 default:
2797 break;
2799 return _("Unknown side");
2802 const char *panel_side_to_name(PanelSide side)
2804 switch (side)
2806 case PANEL_TOP:
2807 return "Top";
2808 case PANEL_BOTTOM:
2809 return "Bottom";
2810 case PANEL_LEFT:
2811 return "Left";
2812 case PANEL_RIGHT:
2813 return "Right";
2814 case PANEL_DEFAULT_SIDE:
2815 return "Default";
2816 default:
2817 break;
2819 return "UnknownSide";
2822 /* Returns PANEL_NUMBER_OF_SIDES if name is invalid */
2823 PanelSide panel_name_to_side(gchar *side)
2825 if (strcmp(side, "Top") == 0)
2826 return PANEL_TOP;
2827 else if (strcmp(side, "Bottom") == 0)
2828 return PANEL_BOTTOM;
2829 else if (strcmp(side, "Left") == 0)
2830 return PANEL_LEFT;
2831 else if (strcmp(side, "Right") == 0)
2832 return PANEL_RIGHT;
2833 else
2834 g_warning("Unknown panel side '%s'", side);
2835 return PANEL_NUMBER_OF_SIDES;
2838 static void panel_add_callback(PanelSide side)
2840 g_return_if_fail(current_panel[side] == NULL);
2841 panel_new(panel_side_to_name(side), side);
2844 GtkWidget *panel_new_panel_submenu(void)
2846 GtkWidget *menu = gtk_menu_new();
2847 PanelSide side;
2849 for (side = 0; side < PANEL_NUMBER_OF_SIDES; ++side)
2851 GtkWidget *item = gtk_menu_item_new_with_label(
2852 panel_side_to_translated_name(side));
2854 g_signal_connect_swapped(item, "activate",
2855 G_CALLBACK(panel_add_callback), GINT_TO_POINTER(side));
2856 gtk_widget_set_sensitive(item, current_panel[side] == NULL);
2857 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
2858 gtk_widget_show(item);
2860 return menu;
2863 /* Set the panel to be above/below other windows (setting = TRUE for below),
2864 * and return TRUE.
2866 * If o_panel_on_top is set then do nothing and return FALSE.
2868 static gboolean panel_keep_below(Panel *panel, gboolean setting)
2870 if (!o_panel_on_top.int_value)
2872 keep_below(panel->window->window, setting);
2873 return TRUE;
2875 return FALSE;