panel_show_options() deals gracefully with dialog already existing.
[rox-filer/translations.git] / ROX-Filer / src / panel.c
blobc378710f01f6db0f43144d114ca1ab5399f0debd
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 <string.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <unistd.h>
29 #include <libxml/parser.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkx.h>
34 #include "global.h"
36 #include "panel.h"
37 #include "options.h"
38 #include "choices.h"
39 #include "main.h"
40 #include "type.h"
41 #include "gui_support.h"
42 #include "diritem.h"
43 #include "pixmaps.h"
44 #include "filer.h"
45 #include "display.h"
46 #include "bind.h"
47 #include "dnd.h"
48 #include "support.h"
49 #include "icon.h"
50 #include "run.h"
51 #include "appinfo.h"
52 #include "pixmaps.h"
53 #include "xml.h"
54 #include "pinboard.h" /* For pinboard_get_window() */
56 /* The width of the separator at the inner edge of the panel */
57 #define EDGE_WIDTH 1
59 /* The gap between panel icons */
60 #define PANEL_ICON_SPACING 8
62 enum {TEXT_BESIDE_ICON, TEXT_UNDER_ICON};
64 static gboolean tmp_icon_selected = FALSE; /* When dragging */
66 typedef struct _PanelIconClass PanelIconClass;
67 typedef struct _PanelIcon PanelIcon;
69 struct _PanelIconClass {
70 IconClass parent;
73 struct _PanelIcon {
74 Icon icon;
75 GdkPixbuf *image;
77 Panel *panel;
78 GtkWidget *widget; /* The drawing area for the icon */
79 GtkWidget *label;
80 GtkWidget *socket; /* For applets */
82 int style;
85 #define PANEL_ICON(obj) GTK_CHECK_CAST((obj), panel_icon_get_type(), PanelIcon)
86 #define IS_PANEL_ICON(obj) \
87 G_TYPE_CHECK_INSTANCE_TYPE((obj), panel_icon_get_type())
89 Panel *current_panel[PANEL_NUMBER_OF_SIDES];
91 /* NULL => Not loading a panel */
92 static Panel *loading_panel = NULL;
94 static GtkWidget *panel_options_dialog = NULL;
96 /* Static prototypes */
97 static int panel_delete(GtkWidget *widget, GdkEvent *event, Panel *panel);
98 static void panel_destroyed(GtkWidget *widget, Panel *panel);
99 static const char *pan_from_file(gchar *line);
100 static gint icon_button_release(GtkWidget *widget,
101 GdkEventButton *event,
102 PanelIcon *pi);
103 static gint icon_button_press(GtkWidget *widget,
104 GdkEventButton *event,
105 PanelIcon *pi);
106 static void reposition_panel(GtkWidget *window,
107 GtkAllocation *alloc, Panel *panel);
108 static gint expose_icon(GtkWidget *widget,
109 GdkEventExpose *event,
110 PanelIcon *pi);
111 static gint draw_icon(GtkWidget *widget,
112 GdkRectangle *badarea,
113 PanelIcon *pi);
114 static gint panel_button_release(GtkWidget *widget,
115 GdkEventButton *event,
116 Panel *panel);
117 static gint panel_button_press(GtkWidget *widget,
118 GdkEventButton *event,
119 Panel *panel);
120 static void panel_post_resize(GtkWidget *box,
121 GtkRequisition *req, Panel *panel);
122 static void drag_set_panel_dest(PanelIcon *pi);
123 static void add_uri_list(GtkWidget *widget,
124 GdkDragContext *context,
125 gint x,
126 gint y,
127 GtkSelectionData *selection_data,
128 guint info,
129 guint32 time,
130 Panel *panel);
131 static void panel_add_item(Panel *panel,
132 const gchar *path,
133 const gchar *name,
134 gboolean after,
135 const gchar *shortcut,
136 const gchar *args,
137 gboolean locked);
138 static gboolean panel_drag_motion(GtkWidget *widget,
139 GdkDragContext *context,
140 gint x,
141 gint y,
142 guint time,
143 Panel *panel);
144 static gboolean insert_drag_motion(GtkWidget *widget,
145 GdkDragContext *context,
146 gint x,
147 gint y,
148 guint time,
149 Panel *panel);
150 static gboolean drag_motion(GtkWidget *widget,
151 GdkDragContext *context,
152 gint x,
153 gint y,
154 guint time,
155 PanelIcon *pi);
156 static void panel_drag_leave(GtkWidget *widget,
157 GdkDragContext *context,
158 guint32 time,
159 Panel *panel);
160 static void drag_leave(GtkWidget *widget,
161 GdkDragContext *context,
162 guint32 time,
163 Icon *icon);
164 static GtkWidget *make_insert_frame(Panel *panel);
165 static gboolean enter_icon(GtkWidget *widget,
166 GdkEventCrossing *event,
167 Icon *icon);
168 static gint icon_motion_event(GtkWidget *widget,
169 GdkEventMotion *event,
170 PanelIcon *pi);
171 static gint panel_leave_event(GtkWidget *widget,
172 GdkEventCrossing *event,
173 Panel *panel);
174 static gint panel_motion_event(GtkWidget *widget,
175 GdkEventMotion *event,
176 Panel *panel);
177 static void reposition_icon(PanelIcon *pi, int index);
178 static void start_drag(PanelIcon *pi, GdkEventMotion *event);
179 static void drag_end(GtkWidget *widget,
180 GdkDragContext *context,
181 Icon *icon);
182 static void perform_action(Panel *panel,
183 PanelIcon *pi,
184 GdkEventButton *event);
185 static void run_applet(PanelIcon *pi);
186 static void size_request(GtkWidget *widget, GtkRequisition *req, PanelIcon *pi);
187 static void panel_load_from_xml(Panel *panel, xmlDocPtr doc);
188 static gboolean draw_panel_edge(GtkWidget *widget, GdkEventExpose *event,
189 Panel *panel);
190 static PanelIcon *panel_icon_new(Panel *panel,
191 const char *pathname,
192 const char *name);
193 static GType panel_icon_get_type(void);
194 static gboolean panel_want_show_text(PanelIcon *pi);
195 static void panel_show_menu(GdkEventButton *event, PanelIcon *pi, Panel *panel);
196 static void panel_style_changed(void);
197 static void motion_may_raise(Panel *panel, int x, int y);
198 static void panel_update(Panel *panel);
199 static gboolean panel_check_xinerama(void);
200 static GList *build_monitor_number(Option *option,
201 xmlNode *node, guchar *label);
202 static gboolean may_autoscroll(Panel *panel);
205 static GtkWidget *dnd_highlight = NULL; /* (stops flickering) */
207 #define SHOW_BOTH 0
208 #define SHOW_APPS_SMALL 1
209 #define SHOW_ICON 2
210 static Option o_panel_style;
211 static Option o_panel_width;
212 static Option o_panel_xinerama;
213 static Option o_panel_monitor;
214 static Option o_panel_avoid;
215 static Option o_panel_is_dock;
217 static gint panel_monitor = -1;
218 GdkRectangle panel_geometry;
220 static int closing_panel = 0; /* Don't panel_save; destroying! */
222 /****************************************************************
223 * EXTERNAL INTERFACE *
224 ****************************************************************/
226 void panel_init(void)
228 option_add_int(&o_panel_style, "panel_style", SHOW_APPS_SMALL);
229 option_add_int(&o_panel_width, "panel_width", 52);
231 option_add_int(&o_panel_xinerama, "panel_xinerama", 0);
232 option_add_int(&o_panel_monitor, "panel_monitor", 0);
234 option_add_int(&o_panel_avoid, "panel_avoid", TRUE);
235 option_add_int(&o_panel_is_dock, "panel_is_dock", FALSE);
237 option_add_notify(panel_style_changed);
239 option_register_widget("monitor-number", build_monitor_number);
241 panel_check_xinerama();
244 /* Return a free edge for a new panel.
245 * If no edge is free, returns PANEL_BOTTOM.
247 static PanelSide find_free_side()
249 if (!current_panel[PANEL_BOTTOM])
250 return PANEL_BOTTOM;
252 if (!current_panel[PANEL_TOP])
253 return PANEL_TOP;
255 if (!current_panel[PANEL_LEFT])
256 return PANEL_LEFT;
258 if (!current_panel[PANEL_RIGHT])
259 return PANEL_RIGHT;
261 return PANEL_BOTTOM;
264 /* 'name' may be NULL or "" to remove the panel */
265 Panel *panel_new(const gchar *name, PanelSide side)
267 guchar *load_path;
268 Panel *panel;
269 GtkWidget *vp, *box, *frame, *align;
270 xmlDocPtr panel_doc = NULL;
271 gboolean need_resave = FALSE;
273 g_return_val_if_fail(side == PANEL_DEFAULT_SIDE ||
274 (side >= 0 && side < PANEL_NUMBER_OF_SIDES), NULL);
275 g_return_val_if_fail(loading_panel == NULL, NULL);
277 if (name && *name == '\0')
278 name = NULL;
280 if (!name)
281 load_path = NULL;
282 else if (strchr(name, '/'))
283 load_path = g_strdup(name);
284 else
286 guchar *leaf;
288 leaf = g_strconcat("pan_", name, NULL);
289 load_path = choices_find_xdg_path_load(leaf, PROJECT, SITE);
290 g_free(leaf);
293 if (load_path && access(load_path, F_OK) == 0)
295 char *saved_side;
296 xmlNodePtr root;
298 panel_doc = xmlParseFile(load_path);
299 root = xmlDocGetRootElement(panel_doc);
301 saved_side = xmlGetProp(root, "side");
302 if (saved_side)
304 PanelSide old_side;
305 old_side = panel_name_to_side(saved_side);
306 g_free(saved_side);
308 if (side == PANEL_DEFAULT_SIDE)
309 side = old_side;
310 else if (side != old_side)
311 need_resave = TRUE;
313 else
314 need_resave = TRUE;
317 if (side == PANEL_DEFAULT_SIDE)
318 side = find_free_side();
320 if (current_panel[side])
322 if (name)
323 number_of_windows++;
324 closing_panel++;
325 gtk_widget_destroy(current_panel[side]->window);
326 closing_panel--;
327 if (name)
328 number_of_windows--;
331 if (name == NULL || *name == '\0')
332 return NULL;
334 panel = g_new(Panel, 1);
335 panel->name = g_strdup(name);
336 panel->side = side;
337 panel->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
338 panel->autoscroll_speed = 0;
339 gtk_window_set_resizable(GTK_WINDOW(panel->window), FALSE);
340 gtk_window_set_wmclass(GTK_WINDOW(panel->window), "ROX-Panel", PROJECT);
341 gtk_widget_set_name(panel->window, "rox-panel");
342 gtk_widget_set_events(panel->window,
343 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
344 GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
346 /* We make the panel a drop target only so that we can auto-raise! */
347 gtk_drag_dest_set(panel->window, 0, NULL, 0, GDK_ACTION_PRIVATE);
348 g_signal_connect(panel->window, "drag_leave",
349 G_CALLBACK(panel_drag_leave), panel);
350 g_signal_connect(panel->window, "drag_motion",
351 G_CALLBACK(panel_drag_motion), panel);
353 g_signal_connect(panel->window, "delete-event",
354 G_CALLBACK(panel_delete), panel);
355 g_signal_connect(panel->window, "destroy",
356 G_CALLBACK(panel_destroyed), panel);
357 g_signal_connect(panel->window, "button_press_event",
358 G_CALLBACK(panel_button_press), panel);
359 g_signal_connect(panel->window, "button_release_event",
360 G_CALLBACK(panel_button_release), panel);
361 g_signal_connect(panel->window, "motion-notify-event",
362 G_CALLBACK(panel_motion_event), panel);
363 g_signal_connect(panel->window, "leave-notify-event",
364 G_CALLBACK(panel_leave_event), panel);
366 if (panel->side == PANEL_RIGHT)
367 align = gtk_alignment_new(1.0, 0.0, 0.0, 1.0);
368 else if (panel->side == PANEL_BOTTOM)
369 align = gtk_alignment_new(0.0, 1.0, 1.0, 0.0);
370 else if (panel->side == PANEL_TOP)
371 align = gtk_alignment_new(0.0, 0.0, 1.0, 0.0);
372 else
373 align = gtk_alignment_new(0.0, 0.0, 0.0, 1.0);
375 gtk_container_add(GTK_CONTAINER(panel->window), align);
377 vp = gtk_viewport_new(NULL, NULL);
378 gtk_container_set_resize_mode(GTK_CONTAINER(vp), GTK_RESIZE_PARENT);
379 gtk_viewport_set_shadow_type(GTK_VIEWPORT(vp), GTK_SHADOW_NONE);
380 gtk_container_add(GTK_CONTAINER(align), vp);
382 g_signal_connect(align, "expose-event",
383 G_CALLBACK(draw_panel_edge), panel);
385 if (side == PANEL_TOP || side == PANEL_BOTTOM)
387 panel->adj = gtk_viewport_get_hadjustment(GTK_VIEWPORT(vp));
388 box = gtk_hbox_new(FALSE, 0);
389 panel->before = gtk_hbox_new(FALSE, 0);
390 panel->after = gtk_hbox_new(FALSE, 0);
392 else
394 panel->adj = gtk_viewport_get_vadjustment(GTK_VIEWPORT(vp));
395 box = gtk_vbox_new(FALSE, 0);
396 panel->before = gtk_vbox_new(FALSE, 0);
397 panel->after = gtk_vbox_new(FALSE, 0);
400 gtk_container_add(GTK_CONTAINER(vp), box);
401 gtk_box_pack_start(GTK_BOX(box), panel->before, FALSE, TRUE, 0);
402 gtk_box_pack_end(GTK_BOX(box), panel->after, FALSE, TRUE, 0);
404 frame = make_insert_frame(panel);
405 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 4);
407 /* This is used so that we can find the middle easily! */
408 panel->gap = gtk_event_box_new();
409 gtk_box_pack_start(GTK_BOX(box), panel->gap, FALSE, FALSE, 0);
411 frame = make_insert_frame(panel);
412 g_object_set_data(G_OBJECT(frame), "after", "yes");
413 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 4);
415 if (o_panel_is_dock.int_value)
416 gtk_window_set_type_hint(GTK_WINDOW(panel->window),
417 GDK_WINDOW_TYPE_HINT_DOCK);
419 gtk_widget_realize(panel->window);
420 make_panel_window(panel->window);
421 gtk_window_stick(GTK_WINDOW(panel->window));
423 gtk_widget_show_all(align);
425 loading_panel = panel;
426 if (panel_doc)
428 panel_load_from_xml(panel, panel_doc);
429 xmlFreeDoc(panel_doc);
431 if (need_resave)
432 panel_save(panel);
434 else if (load_path)
436 parse_file(load_path, pan_from_file);
437 info_message(_("Your old panel file has been "
438 "converted to the new XML format."));
439 panel_save(panel);
441 else
443 /* Don't scare users with an empty panel... */
444 guchar *apps;
446 panel_add_item(panel, "~", "Home", FALSE, NULL, NULL, FALSE);
448 apps = pathdup(make_path(app_dir, ".."));
449 if (apps)
451 panel_add_item(panel, apps, "Apps", FALSE, NULL, NULL, FALSE);
452 g_free(apps);
455 loading_panel = NULL;
456 g_free(load_path);
458 current_panel[side] = panel;
460 gtk_widget_queue_resize(box);
461 g_signal_connect(panel->window, "size-request",
462 G_CALLBACK(panel_post_resize), panel);
463 g_signal_connect(panel->window, "size-allocate",
464 G_CALLBACK(reposition_panel), panel);
466 number_of_windows++;
467 gdk_window_lower(panel->window->window);
468 gtk_widget_show(panel->window);
469 /* This has no effect until after window is showing; GTK+ bug? */
470 keep_below(panel->window->window, TRUE);
473 GdkWindow *pinboard;
475 pinboard = pinboard_get_window();
476 /* (if pinboard is NULL, will go right to the back) */
477 window_put_just_above(panel->window->window, pinboard);
480 return panel;
483 /* Externally visible function to add an item to a panel */
484 gboolean panel_add(PanelSide side,
485 const gchar *path, const gchar *label, gboolean after, const gchar *shortcut, const gchar *args,
486 gboolean locked)
488 g_return_val_if_fail(side >= 0 && side < PANEL_NUMBER_OF_SIDES, FALSE);
490 g_return_val_if_fail(current_panel[side] != NULL, FALSE);
492 panel_add_item(current_panel[side], path, label, after, shortcut, args, locked);
494 return TRUE;
497 /* Add the area covered by the panels to the region */
498 void panel_mark_used(GdkRegion *used)
500 int i;
502 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
504 Panel *panel = current_panel[i];
505 GdkRectangle rect;
507 if (!panel)
508 continue;
510 gdk_window_get_root_origin(panel->window->window,
511 &rect.x, &rect.y);
512 rect.width = panel->window->allocation.width;
513 rect.height = panel->window->allocation.height;
515 gdk_region_union_with_rect(used, &rect);
519 /* On xrandr screen size changes, update all panels */
520 void panel_update_size(void)
522 int i;
524 panel_check_xinerama();
526 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
528 if (current_panel[i])
530 reposition_panel(current_panel[i]->window,
531 &current_panel[i]->window->allocation,
532 current_panel[i]);
533 gtk_widget_queue_resize(current_panel[i]->window);
538 /****************************************************************
539 * INTERNAL FUNCTIONS *
540 ****************************************************************/
542 /* User has tried to close the panel via the window manager - confirm */
543 static int panel_delete(GtkWidget *widget, GdkEvent *event, Panel *panel)
545 return !confirm(_("You have tried to close a panel via the window "
546 "manager - I usually find that this is accidental... "
547 "really close?"),
548 GTK_STOCK_CLOSE, NULL);
551 static void panel_destroyed(GtkWidget *widget, Panel *panel)
553 if (panel_options_dialog)
555 Panel *dlg_panel = g_object_get_data(G_OBJECT(panel_options_dialog),
556 "rox-panel");
558 if (dlg_panel == panel)
559 gtk_widget_destroy(panel_options_dialog);
562 if (current_panel[panel->side] == panel)
563 current_panel[panel->side] = NULL;
565 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
567 if (current_panel[PANEL_RIGHT])
568 gtk_widget_queue_resize(
569 current_panel[PANEL_RIGHT]->window);
570 if (current_panel[PANEL_LEFT])
571 gtk_widget_queue_resize(
572 current_panel[PANEL_LEFT]->window);
575 if (panel->autoscroll_speed)
576 g_source_remove(panel->autoscroll_to);
578 g_free(panel->name);
579 g_free(panel);
581 one_less_window();
584 static void panel_load_side(Panel *panel, xmlNodePtr side, gboolean after)
586 xmlNodePtr node;
587 char *label, *path, *shortcut, *args, *tmp;
588 gboolean locked;
590 for (node = side->xmlChildrenNode; node; node = node->next)
592 if (node->type != XML_ELEMENT_NODE)
593 continue;
594 if (strcmp(node->name, "icon") != 0)
595 continue;
597 label = xmlGetProp(node, "label");
598 if (!label)
599 label = g_strdup("<missing label>");
600 path = xmlNodeGetContent(node);
601 if (!path)
602 path = g_strdup("<missing path>");
603 shortcut = xmlGetProp(node, "shortcut");
604 args = xmlGetProp(node, "args");
605 tmp = xmlGetProp(node, "locked");
606 if (tmp)
608 locked = text_to_boolean(tmp, FALSE);
609 g_free(tmp);
611 else
612 locked = FALSE;
614 panel_add_item(panel, path, label, after, shortcut, args, locked);
616 g_free(path);
617 g_free(label);
618 g_free(shortcut);
619 g_free(args);
623 /* Create one panel icon for each icon in the doc */
624 static void panel_load_from_xml(Panel *panel, xmlDocPtr doc)
626 xmlNodePtr root;
628 root = xmlDocGetRootElement(doc);
629 panel_load_side(panel, get_subnode(root, NULL, "start"), FALSE);
630 panel_load_side(panel, get_subnode(root, NULL, "end"), TRUE);
633 /* Called for each line in the config file while loading a new panel */
634 static const char *pan_from_file(gchar *line)
636 gchar *sep, *leaf;
638 g_return_val_if_fail(line != NULL, NULL);
639 g_return_val_if_fail(loading_panel != NULL, NULL);
641 if (*line == '\0')
642 return NULL;
644 sep = strpbrk(line, "<>");
645 if (!sep)
646 return _("Missing < or > in panel config file");
648 if (sep != line)
649 leaf = g_strndup(line, sep - line);
650 else
651 leaf = NULL;
653 panel_add_item(loading_panel, sep + 1, leaf, sep[0] == '>',
654 NULL, NULL, FALSE);
656 g_free(leaf);
658 return NULL;
661 static gboolean icon_pointer_in(GtkWidget *widget,
662 GdkEventCrossing *event,
663 Icon *icon)
665 gtk_widget_set_state(widget,
666 icon->selected ? GTK_STATE_SELECTED : GTK_STATE_PRELIGHT);
668 return 0;
671 static gboolean icon_pointer_out(GtkWidget *widget,
672 GdkEventCrossing *event,
673 Icon *icon)
675 gtk_widget_set_state(widget,
676 icon->selected ? GTK_STATE_SELECTED : GTK_STATE_NORMAL);
678 return 0;
681 static void panel_icon_destroyed(PanelIcon *pi)
683 g_return_if_fail(pi->widget != NULL);
685 pi->widget = NULL;
687 g_object_unref(pi);
690 /* Set the tooltip AND hide/show the label */
691 static void panel_icon_set_tip(PanelIcon *pi)
693 XMLwrapper *ai;
694 xmlNode *node;
695 Icon *icon = (Icon *) pi;
697 g_return_if_fail(pi != NULL);
699 if (pi->label)
701 if (panel_want_show_text(pi))
702 gtk_widget_show(pi->label);
703 else
704 gtk_widget_hide(pi->label);
707 if (pi->socket)
708 ai = NULL;
709 else
710 ai = appinfo_get(icon->path, icon->item);
712 if (ai && ((node = xml_get_section(ai, NULL, "Summary"))))
714 guchar *str;
715 str = xmlNodeListGetString(node->doc,
716 node->xmlChildrenNode, 1);
717 if (str)
719 gtk_tooltips_set_tip(tooltips, pi->widget, str, NULL);
720 g_free(str);
723 else if ((!panel_want_show_text(pi)) && !pi->socket)
725 if (icon->item->leafname && icon->item->leafname[0])
726 gtk_tooltips_set_tip(tooltips, pi->widget,
727 icon->item->leafname, NULL);
729 else
730 gtk_tooltips_set_tip(tooltips, pi->widget, NULL, NULL);
732 if (ai)
733 g_object_unref(ai);
736 /* Add an icon with this path to the panel. If after is TRUE then the
737 * icon is added to the right/bottom end of the panel.
739 * If name is NULL a suitable name is taken from path.
741 static void panel_add_item(Panel *panel,
742 const gchar *path,
743 const gchar *name,
744 gboolean after,
745 const gchar *shortcut,
746 const gchar *args,
747 gboolean locked)
749 GtkWidget *widget;
750 PanelIcon *pi;
751 Icon *icon;
753 g_return_if_fail(panel != NULL);
754 g_return_if_fail(path != NULL);
756 widget = gtk_event_box_new();
757 gtk_widget_set_events(widget,
758 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
759 GDK_BUTTON3_MOTION_MASK |
760 GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
761 GDK_BUTTON_RELEASE_MASK);
763 gtk_box_pack_start(GTK_BOX(after ? panel->after : panel->before),
764 widget, FALSE, TRUE, 0);
765 if (after)
766 gtk_box_reorder_child(GTK_BOX(panel->after), widget, 0);
768 gtk_widget_realize(widget);
770 pi = panel_icon_new(panel, path, name);
771 icon = (Icon *) pi;
773 /* Widget takes the initial ref of Icon */
774 g_object_set_data(G_OBJECT(widget), "icon", pi);
776 pi->widget = widget;
777 g_object_ref(widget);
779 gtk_widget_set_name(pi->widget, "panel-icon");
781 g_signal_connect_swapped(widget, "destroy",
782 G_CALLBACK(panel_icon_destroyed), pi);
784 if (icon->item->base_type == TYPE_DIRECTORY)
785 run_applet(pi);
787 g_signal_connect(widget, "button_release_event",
788 G_CALLBACK(icon_button_release), pi);
789 g_signal_connect(widget, "button_press_event",
790 G_CALLBACK(icon_button_press), pi);
791 g_signal_connect(widget, "motion-notify-event",
792 G_CALLBACK(icon_motion_event), pi);
793 g_signal_connect(widget, "enter-notify-event",
794 G_CALLBACK(icon_pointer_in), pi);
795 g_signal_connect(widget, "leave-notify-event",
796 G_CALLBACK(icon_pointer_out), pi);
798 if (!pi->socket)
800 g_signal_connect(widget, "enter-notify-event",
801 G_CALLBACK(enter_icon), pi);
802 g_signal_connect_after(widget, "expose_event",
803 G_CALLBACK(expose_icon), pi);
804 g_signal_connect(widget, "drag_data_get",
805 G_CALLBACK(drag_data_get), NULL);
807 g_signal_connect(widget, "size_request",
808 G_CALLBACK(size_request), pi);
810 drag_set_panel_dest(pi);
812 pi->label = gtk_label_new(icon->item->leafname);
813 gtk_container_add(GTK_CONTAINER(pi->widget), pi->label);
814 gtk_misc_set_alignment(GTK_MISC(pi->label), 0.5, 1);
815 gtk_misc_set_padding(GTK_MISC(pi->label), 1, 2);
818 icon_set_shortcut(icon, shortcut);
819 icon_set_arguments(icon, args);
820 icon->locked = locked;
822 if (!loading_panel)
823 panel_save(panel);
825 panel_icon_set_tip(pi);
826 gtk_widget_show(widget);
829 static gboolean remove_item_from_side(GtkWidget *container, const gchar *path,
830 const gchar *label)
832 GList *kids, *next;
833 gboolean found = FALSE;
835 kids = gtk_container_get_children(GTK_CONTAINER(container));
837 for (next = kids; next; next = next->next)
839 Icon *icon;
840 icon = g_object_get_data(G_OBJECT(next->data), "icon");
841 if (!icon)
842 continue;
844 if ((!path || strcmp(path, icon->src_path) == 0) &&
845 (!label || strcmp(label, icon->item->leafname)==0))
847 icon->locked = FALSE;
848 icon_destroy(icon);
849 found = TRUE;
850 break;
854 g_list_free(kids);
856 return found;
859 /* Remove an item with this path. If more than one item matches, only
860 * one is removed. If label is not NULL then it must also match the item.
861 * Returns TRUE if an item was successfully removed.
863 gboolean panel_remove_item(PanelSide side, const gchar *path,
864 const gchar *label)
866 Panel *panel;
868 g_return_val_if_fail(side >= 0 && side < PANEL_NUMBER_OF_SIDES, FALSE);
870 g_return_val_if_fail(path != NULL || label != NULL, FALSE);
872 panel = current_panel[side];
873 if (!panel)
875 g_warning("No panel on this side of the screen!");
876 return FALSE;
879 if (remove_item_from_side(panel->before, path, label) ||
880 remove_item_from_side(panel->after, path, label))
882 panel_save(panel);
883 panel_update(panel);
884 return TRUE;
887 g_warning("Panel item path='%s', label='%s' not found", path, label);
888 return FALSE;
891 /* Called when Gtk+ wants to know how much space an icon needs.
892 * 'req' is already big enough for the label, if shown.
894 static void size_request(GtkWidget *widget, GtkRequisition *req, PanelIcon *pi)
896 Icon *icon = (Icon *) pi;
897 gboolean horz = (pi->panel->side == PANEL_TOP ||
898 pi->panel->side == PANEL_BOTTOM);
899 int max_width = 100;
900 int max_height = 100;
901 int image_width, image_height;
903 if (horz)
904 max_height = o_panel_width.int_value - req->height;
905 else
906 max_width = MAX(o_panel_width.int_value, req->width);
908 /* TODO: really need to recreate? */
909 if (pi->image)
910 g_object_unref(pi->image);
912 pi->image = scale_pixbuf(di_image(icon->item)->src_pixbuf,
913 MAX(20, max_width), MAX(20, max_height));
915 image_width = gdk_pixbuf_get_width(pi->image);
916 image_height = gdk_pixbuf_get_height(pi->image);
918 if (req->height > 0 && max_height < req->height)
920 pi->style = TEXT_BESIDE_ICON;
921 req->width += image_width;
922 req->height = MAX(req->height, image_height);
923 gtk_misc_set_alignment(GTK_MISC(pi->label), 1, 0.5);
925 else
927 pi->style = TEXT_UNDER_ICON;
928 req->width = MAX(req->width, image_width);
929 req->height += image_height;
930 gtk_misc_set_alignment(GTK_MISC(pi->label), 0.5, 1);
933 if (horz)
934 req->width += PANEL_ICON_SPACING;
935 else
936 req->height += PANEL_ICON_SPACING;
939 static gint expose_icon(GtkWidget *widget,
940 GdkEventExpose *event,
941 PanelIcon *pi)
943 return draw_icon(widget, &event->area, pi);
946 static gint draw_icon(GtkWidget *widget, GdkRectangle *badarea, PanelIcon *pi)
948 GdkRectangle area;
949 int width, height;
950 Icon *icon = (Icon *) pi;
951 int image_x;
952 int image_y;
953 GdkPixbuf *image;
954 int text_height = 0;
956 gdk_drawable_get_size(widget->window, &area.width, &area.height);
958 if (panel_want_show_text(pi))
959 text_height = pi->label->requisition.height;
961 g_return_val_if_fail(pi->image != NULL, FALSE);
963 image = pi->image;
965 width = gdk_pixbuf_get_width(image);
966 height = gdk_pixbuf_get_height(image);
968 if (pi->style == TEXT_UNDER_ICON)
970 image_x = (area.width - width) >> 1;
971 image_y = (area.height - height - text_height) >> 1;
973 else
975 image_x = PANEL_ICON_SPACING - 2;
976 image_y = (area.height - height) >> 1;
979 gdk_pixbuf_render_to_drawable_alpha(
980 image,
981 widget->window,
982 0, 0, /* src */
983 image_x, image_y, /* dest */
984 width, height,
985 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
986 GDK_RGB_DITHER_NORMAL, 0, 0);
988 if (icon->item->flags & ITEM_FLAG_SYMLINK)
990 gdk_pixbuf_render_to_drawable_alpha(im_symlink->pixbuf,
991 widget->window,
992 0, 0, /* src */
993 image_x, image_y + 2, /* dest */
994 -1, -1,
995 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
996 GDK_RGB_DITHER_NORMAL, 0, 0);
998 if (icon->item->flags & ITEM_FLAG_MOUNT_POINT)
1000 MaskedPixmap *mp = icon->item->flags & ITEM_FLAG_MOUNTED
1001 ? im_mounted
1002 : im_unmounted;
1004 gdk_pixbuf_render_to_drawable_alpha(mp->pixbuf,
1005 widget->window,
1006 0, 0, /* src */
1007 image_x, image_y + 2, /* dest */
1008 -1, -1,
1009 GDK_PIXBUF_ALPHA_FULL, 128, /* (unused) */
1010 GDK_RGB_DITHER_NORMAL, 0, 0);
1012 return FALSE;
1015 static void panel_icon_wink(Icon *icon)
1017 PanelIcon *pi = (PanelIcon *) icon;
1019 wink_widget(pi->widget);
1022 /* icon may be NULL if the event is on the background */
1023 static void perform_action(Panel *panel, PanelIcon *pi, GdkEventButton *event)
1025 BindAction action;
1026 Icon *icon = (Icon *) pi;
1028 action = bind_lookup_bev(icon ? BIND_PANEL_ICON : BIND_PANEL, event);
1030 if (pi && pi->socket)
1031 if (action != ACT_POPUP_MENU && action != ACT_MOVE_ICON)
1032 return;
1034 switch (action)
1036 case ACT_OPEN_ITEM:
1037 dnd_motion_ungrab();
1038 wink_widget(pi->widget);
1039 icon_run(icon);
1040 break;
1041 case ACT_EDIT_ITEM:
1042 dnd_motion_ungrab();
1043 wink_widget(pi->widget);
1044 run_diritem(icon->path, icon->item, NULL, NULL, TRUE);
1045 break;
1046 case ACT_POPUP_MENU:
1047 dnd_motion_ungrab();
1048 panel_show_menu(event, pi, panel);
1049 break;
1050 case ACT_MOVE_ICON:
1051 dnd_motion_start(MOTION_REPOSITION);
1052 break;
1053 case ACT_PRIME_AND_SELECT:
1054 if (!icon->selected)
1055 icon_select_only(icon);
1056 dnd_motion_start(MOTION_READY_FOR_DND);
1057 break;
1058 case ACT_PRIME_AND_TOGGLE:
1059 icon_set_selected(icon, !icon->selected);
1060 dnd_motion_start(MOTION_READY_FOR_DND);
1061 break;
1062 case ACT_PRIME_FOR_DND:
1063 dnd_motion_start(MOTION_READY_FOR_DND);
1064 break;
1065 case ACT_TOGGLE_SELECTED:
1066 icon_set_selected(icon, !icon->selected);
1067 break;
1068 case ACT_SELECT_EXCL:
1069 icon_set_selected(icon, TRUE);
1070 break;
1071 case ACT_IGNORE:
1072 break;
1073 case ACT_CLEAR_SELECTION:
1074 dnd_motion_ungrab();
1075 icon_select_only(NULL);
1076 break;
1077 default:
1078 g_warning("Unsupported action : %d\n", action);
1079 break;
1083 static gint panel_button_release(GtkWidget *widget,
1084 GdkEventButton *event,
1085 Panel *panel)
1087 if (dnd_motion_release(event))
1088 return TRUE;
1090 perform_action(panel, NULL, event);
1092 return TRUE;
1095 static gint panel_button_press(GtkWidget *widget,
1096 GdkEventButton *event,
1097 Panel *panel)
1099 if (dnd_motion_press(panel->window, event))
1100 perform_action(panel, NULL, event);
1102 return TRUE;
1105 static gint icon_button_release(GtkWidget *widget,
1106 GdkEventButton *event,
1107 PanelIcon *pi)
1109 if (pi->socket && event->button == 1)
1110 return FALSE; /* Restart button */
1112 if (dnd_motion_release(event))
1113 return TRUE;
1115 perform_action(pi->panel, pi, event);
1117 return TRUE;
1120 static gint icon_button_press(GtkWidget *widget,
1121 GdkEventButton *event,
1122 PanelIcon *pi)
1124 if (pi->socket && event->button == 1)
1125 return FALSE; /* Restart button */
1127 if (dnd_motion_press(widget, event))
1128 perform_action(pi->panel, pi, event);
1130 return TRUE;
1133 static void reposition_panel(GtkWidget *window,
1134 GtkAllocation *alloc, Panel *panel)
1136 int x = panel_geometry.x;
1137 int y = panel_geometry.y;
1138 int thickness;
1139 PanelSide side = panel->side;
1141 if (side == PANEL_LEFT || side == PANEL_RIGHT)
1143 if (side == PANEL_RIGHT)
1144 x += panel_geometry.width - alloc->width;
1146 if (current_panel[PANEL_TOP])
1148 GtkWidget *win = current_panel[PANEL_TOP]->window;
1149 y += win->allocation.height;
1153 if (side == PANEL_BOTTOM)
1154 y += panel_geometry.height - alloc->height;
1156 gtk_window_move(GTK_WINDOW(panel->window), x, y);
1157 gdk_window_move(panel->window->window, x, y);
1159 if (side == PANEL_BOTTOM || side == PANEL_TOP)
1161 if (current_panel[PANEL_RIGHT])
1162 gtk_widget_queue_resize(
1163 current_panel[PANEL_RIGHT]->window);
1164 if (current_panel[PANEL_LEFT])
1165 gtk_widget_queue_resize(
1166 current_panel[PANEL_LEFT]->window);
1169 /* Stop windows from maximising over all/part of us */
1171 struct {
1172 gulong left, right, top, bottom;
1173 gulong left_start_y, left_end_y;
1174 gulong right_start_y, right_end_y;
1175 gulong top_start_x, top_end_x;
1176 gulong bottom_start_x, bottom_end_x;
1177 } strut = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1179 if (o_panel_avoid.int_value == FALSE)
1180 thickness = 2;
1181 else if (panel->side == PANEL_TOP ||
1182 panel->side == PANEL_BOTTOM)
1183 thickness = alloc->height;
1184 else
1185 thickness = alloc->width;
1187 switch (panel->side)
1189 case PANEL_LEFT:
1190 if (!o_panel_xinerama.int_value ||
1191 !monitor_adjacent
1192 [o_panel_monitor.int_value].left)
1194 strut.left = panel_geometry.x +
1195 thickness;
1196 strut.left_start_y = panel_geometry.y;
1197 strut.left_end_y = panel_geometry.y +
1198 panel_geometry.height - 1;
1200 /* else there is (part of) a monitor
1201 * to the left */
1202 else
1204 thickness = 0;
1206 break;
1207 case PANEL_RIGHT:
1208 if (!o_panel_xinerama.int_value ||
1209 !monitor_adjacent
1210 [o_panel_monitor.int_value].right)
1212 /* RHS of monitor might not abut edge
1213 * of total virtual screen */
1214 strut.right = screen_width -
1215 panel_geometry.x -
1216 panel_geometry.width +
1217 thickness;
1218 strut.right_start_y = panel_geometry.y;
1219 strut.right_end_y = panel_geometry.y +
1220 panel_geometry.height - 1;
1222 /* else there is (part of) a monitor
1223 * to the right */
1224 else
1226 thickness = 0;
1228 break;
1229 case PANEL_TOP:
1230 if (!o_panel_xinerama.int_value ||
1231 !monitor_adjacent
1232 [o_panel_monitor.int_value].top)
1234 strut.top = panel_geometry.y +
1235 thickness;
1236 strut.top_start_x = panel_geometry.x;
1237 strut.top_end_x = panel_geometry.x +
1238 panel_geometry.width - 1;
1240 /* else there is (part of) a monitor above */
1241 else
1243 thickness = 0;
1245 break;
1246 default: /* PANEL_BOTTOM */
1247 if (!o_panel_xinerama.int_value ||
1248 !monitor_adjacent
1249 [o_panel_monitor.int_value].bottom)
1251 /* Bottom of monitor might not abut
1252 * edge of total virtual screen */
1253 strut.bottom = screen_height -
1254 panel_geometry.y -
1255 panel_geometry.height +
1256 thickness;
1257 strut.bottom_start_x = panel_geometry.x;
1258 strut.bottom_end_x = panel_geometry.x +
1259 panel_geometry.width - 1;
1261 /* else there is (part of) a monitor below */
1262 else
1264 thickness = 0;
1266 break;
1269 if (thickness)
1271 /* Set full-width strut as well as partial in case
1272 * partial isn't supported by wm */
1273 gdk_property_change(panel->window->window,
1274 gdk_atom_intern("_NET_WM_STRUT",
1275 FALSE),
1276 gdk_atom_intern("CARDINAL", FALSE),
1277 32, GDK_PROP_MODE_REPLACE,
1278 (gchar *) &strut, 4);
1279 gdk_property_change(panel->window->window,
1280 gdk_atom_intern("_NET_WM_STRUT_PARTIAL",
1281 FALSE),
1282 gdk_atom_intern("CARDINAL", FALSE),
1283 32, GDK_PROP_MODE_REPLACE,
1284 (gchar *) &strut, 12);
1286 else
1288 gdk_property_delete(panel->window->window,
1289 gdk_atom_intern("_NET_WM_STRUT_PARTIAL",
1290 FALSE));
1291 gdk_property_delete(panel->window->window,
1292 gdk_atom_intern("_NET_WM_STRUT",
1293 FALSE));
1299 /* Same as drag_set_dest(), but for panel icons */
1300 static void drag_set_panel_dest(PanelIcon *pi)
1302 GtkWidget *obj = pi->widget;
1304 make_drop_target(pi->widget, 0);
1306 g_signal_connect(obj, "drag_motion", G_CALLBACK(drag_motion), pi);
1307 g_signal_connect(obj, "drag_leave", G_CALLBACK(drag_leave), pi);
1308 g_signal_connect(obj, "drag_end", G_CALLBACK(drag_end), pi);
1311 static gboolean drag_motion(GtkWidget *widget,
1312 GdkDragContext *context,
1313 gint x,
1314 gint y,
1315 guint time,
1316 PanelIcon *pi)
1318 GdkDragAction action = context->suggested_action;
1319 const char *type = NULL;
1320 Icon *icon = (Icon *) pi;
1321 DirItem *item = icon->item;
1322 int panel_x, panel_y;
1324 gdk_window_get_pointer(pi->panel->window->window,
1325 &panel_x, &panel_y, NULL);
1326 motion_may_raise(pi->panel, panel_x, panel_y);
1328 /* Should we scroll the panel when dragging? */
1329 if (motion_state != MOTION_REPOSITION)
1330 if (pi->panel->autoscroll_speed == 0)
1331 may_autoscroll(pi->panel);
1333 if (icon->selected)
1334 goto out; /* Can't drag a selection to itself */
1336 type = dnd_motion_item(context, &item);
1338 if ((context->actions & GDK_ACTION_ASK) && o_dnd_left_menu.int_value
1339 && type != drop_dest_prog)
1341 guint state;
1342 gdk_window_get_pointer(NULL, NULL, NULL, &state);
1343 if (state & GDK_BUTTON1_MASK)
1344 action = GDK_ACTION_ASK;
1347 if (!item)
1348 type = NULL;
1349 out:
1350 /* We actually must pretend to accept the drop, even if the
1351 * directory isn't writeable, so that the spring-opening
1352 * thing works.
1355 /* Don't allow drops to non-writeable directories */
1356 if (o_dnd_spring_open.int_value == FALSE &&
1357 type == drop_dest_dir &&
1358 access(icon->path, W_OK) != 0)
1360 type = NULL;
1363 g_dataset_set_data(context, "drop_dest_type", (gpointer) type);
1364 if (type)
1366 gdk_drag_status(context, action, time);
1367 g_dataset_set_data_full(context, "drop_dest_path",
1368 g_strdup(icon->path), g_free);
1369 if (type == drop_dest_dir)
1370 dnd_spring_load(context, NULL);
1372 if (dnd_highlight && dnd_highlight != pi->widget)
1374 gtk_drag_unhighlight(dnd_highlight);
1375 dnd_highlight = NULL;
1378 if (dnd_highlight == NULL)
1380 gtk_drag_highlight(pi->widget);
1381 dnd_highlight = pi->widget;
1385 return type != NULL;
1389 static void add_uri_list(GtkWidget *widget,
1390 GdkDragContext *context,
1391 gint x,
1392 gint y,
1393 GtkSelectionData *selection_data,
1394 guint info,
1395 guint32 time,
1396 Panel *panel)
1398 gboolean after = FALSE;
1399 GList *uris, *next;
1401 if (!selection_data->data)
1402 return;
1404 g_return_if_fail(selection_data->data[selection_data->length] == '\0');
1406 if (g_object_get_data(G_OBJECT(widget), "after"))
1407 after = TRUE;
1409 uris = uri_list_to_glist(selection_data->data);
1411 for (next = uris; next; next = next->next)
1413 guchar *path;
1415 path = get_local_path((EscapedPath *) next->data);
1417 if (path) {
1418 panel_add_item(panel, path, NULL, after, NULL, NULL, FALSE);
1419 g_free(path);
1423 g_list_free(uris);
1426 static void drag_end(GtkWidget *widget,
1427 GdkDragContext *context,
1428 Icon *icon)
1430 if (tmp_icon_selected)
1432 icon_select_only(NULL);
1433 tmp_icon_selected = FALSE;
1437 static void drag_leave(GtkWidget *widget,
1438 GdkDragContext *context,
1439 guint32 time,
1440 Icon *icon)
1442 panel_drag_leave(widget, context, time, ((PanelIcon *) icon)->panel);
1444 if (dnd_highlight && dnd_highlight == widget)
1446 gtk_drag_unhighlight(dnd_highlight);
1447 dnd_highlight = NULL;
1450 dnd_spring_abort();
1453 /* Create XML icon nodes for these widgets.
1454 * Always frees the widgets list.
1456 static void make_widgets(xmlNodePtr side, GList *widgets)
1458 GList *next;
1460 for (next = widgets; next; next = next->next)
1462 Icon *icon;
1463 xmlNodePtr tree;
1465 icon = g_object_get_data(G_OBJECT(next->data), "icon");
1467 if (!icon)
1469 g_warning("Can't find Icon from widget\n");
1470 continue;
1473 tree = xmlNewTextChild(side, NULL, "icon", icon->src_path);
1475 xmlSetProp(tree, "label", icon->item->leafname);
1476 if (icon->shortcut)
1477 xmlSetProp(tree, "shortcut", icon->shortcut);
1478 if (icon->args)
1479 xmlSetProp(tree, "args", icon->args);
1480 if (icon->locked)
1481 xmlSetProp(tree, "locked", "true");
1484 if (widgets)
1485 g_list_free(widgets);
1488 void panel_save(Panel *panel)
1490 xmlDocPtr doc;
1491 xmlNodePtr root;
1492 guchar *save = NULL;
1493 guchar *save_new = NULL;
1495 g_return_if_fail(panel != NULL);
1497 if (strchr(panel->name, '/'))
1498 save = g_strdup(panel->name);
1499 else
1501 guchar *leaf;
1503 leaf = g_strconcat("pan_", panel->name, NULL);
1504 save = choices_find_xdg_path_save(leaf, PROJECT, SITE, TRUE);
1505 g_free(leaf);
1508 if (!save)
1509 return;
1511 doc = xmlNewDoc("1.0");
1512 xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "panel", NULL));
1514 root = xmlDocGetRootElement(doc);
1516 xmlSetProp(root, "side",
1517 panel->side == PANEL_TOP ? "Top" :
1518 panel->side == PANEL_BOTTOM ? "Bottom" :
1519 panel->side == PANEL_LEFT ? "Left" :
1520 "Right");
1522 make_widgets(xmlNewChild(root, NULL, "start", NULL),
1523 gtk_container_get_children(GTK_CONTAINER(panel->before)));
1525 make_widgets(xmlNewChild(root, NULL, "end", NULL),
1526 g_list_reverse(gtk_container_get_children(
1527 GTK_CONTAINER(panel->after))));
1529 save_new = g_strconcat(save, ".new", NULL);
1530 if (save_xml_file(doc, save_new) || rename(save_new, save))
1531 delayed_error(_("Error saving panel %s: %s"),
1532 save, g_strerror(errno));
1533 g_free(save_new);
1535 g_free(save);
1536 if (doc)
1537 xmlFreeDoc(doc);
1540 /* Create a frame widget which can be used to add icons to the panel */
1541 static GtkWidget *make_insert_frame(Panel *panel)
1543 GtkWidget *frame;
1544 GtkTargetEntry target_table[] = {
1545 {"text/uri-list", 0, TARGET_URI_LIST},
1548 frame = gtk_frame_new(NULL);
1549 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
1550 gtk_widget_set_size_request(frame, 16, 16);
1552 g_signal_connect(frame, "drag-motion",
1553 G_CALLBACK(insert_drag_motion), panel);
1554 g_signal_connect(frame, "drag-leave",
1555 G_CALLBACK(panel_drag_leave), panel);
1557 g_signal_connect(frame, "drag-data-received",
1558 G_CALLBACK(add_uri_list), panel);
1559 gtk_drag_dest_set(frame,
1560 GTK_DEST_DEFAULT_ALL,
1561 target_table,
1562 sizeof(target_table) / sizeof(*target_table),
1563 GDK_ACTION_COPY);
1565 return frame;
1568 static gboolean enter_icon(GtkWidget *widget,
1569 GdkEventCrossing *event,
1570 Icon *icon)
1572 icon_may_update(icon);
1573 panel_icon_set_tip((PanelIcon *) icon);
1575 return FALSE;
1578 static gint panel_leave_event(GtkWidget *widget,
1579 GdkEventCrossing *event,
1580 Panel *panel)
1582 GdkWindow *pinboard;
1584 if (event->mode != GDK_CROSSING_NORMAL)
1585 return FALSE; /* Grab for menu, DnD, etc */
1587 keep_below(panel->window->window, TRUE);
1589 /* Shouldn't need this as well as keep_below but some WMs don't
1590 * automatically lower as soon as the hint is set */
1591 pinboard = pinboard_get_window();
1592 window_put_just_above(panel->window->window, pinboard);
1594 return FALSE;
1597 /* If (x, y) is at the edge of the panel then raise */
1598 static void motion_may_raise(Panel *panel, int x, int y)
1600 gboolean raise;
1602 if (panel->side == PANEL_TOP)
1603 raise = y == 0;
1604 else if (panel->side == PANEL_BOTTOM)
1605 raise = y == panel->window->allocation.height - 1;
1606 else if (panel->side == PANEL_LEFT)
1607 raise = x == 0;
1608 else
1609 raise = x == panel->window->allocation.width - 1;
1611 if (raise)
1613 keep_below(panel->window->window, FALSE);
1615 /* Shouldn't need this as well as keep_below but some WMs don't
1616 * automatically raise as soon as the hint is set */
1617 gdk_window_raise(panel->window->window);
1621 static gboolean may_autoscroll(Panel *panel)
1623 gboolean horz = panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM;
1624 gint max, panel_x, panel_y, delta, new;
1626 if (panel->adj->upper <= panel->adj->page_size)
1627 goto stop_scrolling; /* Can see everything already */
1629 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
1631 if (horz)
1633 delta = panel_x;
1634 max = panel->window->allocation.width;
1635 if (panel_y < 0 || panel_y > panel->window->allocation.height)
1636 goto stop_scrolling; /* Not over the panel */
1638 else
1640 delta = panel_y;
1641 max = panel->window->allocation.height;
1642 if (panel_x < 0 || panel_x > panel->window->allocation.width)
1643 goto stop_scrolling; /* Not over the panel */
1646 if (delta >= 20 && delta <= max - 20)
1647 goto stop_scrolling; /* Not at either end */
1649 panel->autoscroll_speed = MIN(panel->autoscroll_speed + 2, 200);
1651 new = panel->adj->value - ((delta < 20) ? panel->autoscroll_speed
1652 : -panel->autoscroll_speed);
1653 new = CLAMP(new, 0, panel->adj->upper - panel->adj->page_size);
1654 gtk_adjustment_set_value(panel->adj, new);
1656 panel->autoscroll_to = g_timeout_add(40,
1657 (GSourceFunc) may_autoscroll, panel);
1659 return FALSE;
1661 stop_scrolling:
1662 panel->autoscroll_speed = 0;
1663 return FALSE;
1666 static gint panel_motion_event(GtkWidget *widget,
1667 GdkEventMotion *event,
1668 Panel *panel)
1670 motion_may_raise(panel, event->x, event->y);
1672 if (motion_state != MOTION_REPOSITION)
1673 if (panel->autoscroll_speed == 0)
1674 may_autoscroll(panel);
1676 return FALSE;
1679 static gint icon_motion_event(GtkWidget *widget,
1680 GdkEventMotion *event,
1681 PanelIcon *pi)
1683 Panel *panel = pi->panel;
1684 GList *list, *me;
1685 gboolean horz = panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM;
1686 int val;
1687 int dir = 0;
1689 if (motion_state == MOTION_READY_FOR_DND)
1691 if (dnd_motion_moved(event))
1692 start_drag(pi, event);
1693 return TRUE;
1695 else if (motion_state != MOTION_REPOSITION)
1696 return FALSE;
1698 list = gtk_container_get_children(GTK_CONTAINER(panel->before));
1699 list = g_list_append(list, NULL); /* The gap in the middle */
1700 list = g_list_concat(list,
1701 gtk_container_get_children(GTK_CONTAINER(panel->after)));
1702 me = g_list_find(list, widget);
1704 g_return_val_if_fail(me != NULL, TRUE);
1706 val = horz ? event->x_root : event->y_root;
1708 if (me->prev)
1710 GtkWidget *prev;
1711 int x, y;
1713 if (me->prev->data)
1714 prev = GTK_WIDGET(me->prev->data);
1715 else
1716 prev = panel->gap;
1718 gdk_window_get_origin(prev->window, &x, &y);
1720 if (val <= (horz ? x : y))
1721 dir = -1;
1724 if (dir == 0 && me->next)
1726 GtkWidget *next;
1727 int x, y, w, h;
1729 if (me->next->data)
1730 next = GTK_WIDGET(me->next->data);
1731 else
1732 next = panel->gap;
1734 gdk_window_get_origin(next->window, &x, &y);
1736 gdk_drawable_get_size(next->window, &w, &h);
1738 x += w;
1739 y += h;
1741 if (val >= (horz ? x : y)-1)
1743 if (next == panel->gap)
1744 dir = +2;
1745 else
1746 dir = +1;
1750 if (dir)
1751 reposition_icon(pi, g_list_index(list, widget) + dir);
1753 return TRUE;
1756 static void reposition_icon_on_side(GtkWidget *side, GtkWidget *widget,
1757 int index)
1759 GList *list;
1761 list = gtk_container_get_children(GTK_CONTAINER(side));
1763 /* Want to move icon to the list in the given 'side'. Is it there
1764 * already?
1767 if (!g_list_find(list, widget))
1769 /* No, reparent */
1770 gtk_grab_remove(widget);
1771 gtk_widget_reparent(widget, side);
1772 dnd_motion_grab_pointer();
1773 gtk_grab_add(widget);
1776 gtk_box_reorder_child(GTK_BOX(side), widget, index);
1778 g_list_free(list);
1781 /* Move icon to this index in the complete widget list.
1782 * 0 makes the icon the left-most icon. The gap in the middle has
1783 * an index number, which allows you to specify that the icon should
1784 * go on the left or right side.
1786 static void reposition_icon(PanelIcon *pi, int index)
1788 Panel *panel = pi->panel;
1789 GtkWidget *widget = pi->widget;
1790 GList *list;
1791 int before_len;
1793 list = gtk_container_get_children(GTK_CONTAINER(panel->before));
1794 before_len = g_list_length(list);
1795 g_list_free(list);
1797 if (index <= before_len)
1798 reposition_icon_on_side(panel->before, widget, index);
1799 else
1800 reposition_icon_on_side(panel->after, widget,
1801 index - (before_len + 1));
1803 panel_save(panel);
1806 static void start_drag(PanelIcon *pi, GdkEventMotion *event)
1808 GtkWidget *widget = pi->widget;
1809 Icon *icon = (Icon *) pi;
1811 if (!icon->selected)
1813 if (event->state & GDK_BUTTON1_MASK)
1815 /* Select just this one */
1816 icon_select_only(icon);
1817 tmp_icon_selected = TRUE;
1819 else
1820 icon_set_selected(icon, TRUE);
1823 g_return_if_fail(icon_selection != NULL);
1825 if (icon_selection->next == NULL)
1826 drag_one_item(widget, event, icon->path, icon->item, NULL);
1827 else
1829 guchar *uri_list;
1831 uri_list = icon_create_uri_list();
1832 drag_selection(widget, event, uri_list);
1833 g_free(uri_list);
1837 static void applet_died(GtkWidget *socket)
1839 gboolean never_plugged;
1841 never_plugged = (!g_object_get_data(G_OBJECT(socket), "lost_plug"))
1842 && !GTK_SOCKET(socket)->plug_window;
1844 if (never_plugged)
1846 report_error(
1847 _("Applet quit without ever creating a widget!"));
1848 gtk_widget_destroy(socket);
1851 gtk_widget_unref(socket);
1854 static void socket_destroyed(GtkWidget *socket, GtkWidget *widget)
1856 g_object_set_data(G_OBJECT(socket), "lost_plug", "yes");
1858 gtk_widget_unref(socket);
1860 gtk_widget_destroy(widget); /* Remove from panel */
1862 if (!closing_panel)
1863 panel_save(g_object_get_data(G_OBJECT(socket), "panel"));
1866 /* Try to run this applet.
1867 * Cases:
1869 * - No executable AppletRun:
1870 * icon->socket == NULL (unchanged) on return.
1872 * Otherwise, create socket (setting icon->socket) and ref it twice.
1874 * - AppletRun quits without connecting a plug:
1875 * On child death lost_plug is unset and socket is empty.
1876 * Unref socket.
1877 * Report error and destroy widget (to 'socket destroyed').
1879 * - AppletRun quits while plug is in socket:
1880 * Unref socket once. Socket will be destroyed later.
1882 * - Socket is destroyed.
1883 * Set lost_plug = "yes" and remove widget from panel.
1884 * Unref socket.
1886 static void run_applet(PanelIcon *pi)
1888 GError *error = NULL;
1889 char *argv[3];
1890 gint pid;
1891 Icon *icon = (Icon *) pi;
1893 argv[0] = (char *) make_path(icon->path, "AppletRun");
1895 if (access(argv[0], X_OK) != 0)
1896 return;
1898 pi->socket = gtk_socket_new();
1900 gtk_container_add(GTK_CONTAINER(pi->widget), pi->socket);
1901 gtk_widget_show_all(pi->socket);
1902 gtk_widget_realize(pi->socket);
1904 /* Always get button-2 events so we can drag */
1905 XGrabButton(gdk_display, Button2, AnyModifier,
1906 GDK_WINDOW_XWINDOW(pi->socket->window),
1907 False,
1908 ButtonPressMask | ButtonReleaseMask | Button2MotionMask,
1909 GrabModeAsync, /* Pointer */
1910 GrabModeAsync, /* Keyboard */
1911 None, None);
1914 gchar *pos;
1915 PanelSide side = pi->panel->side;
1917 /* Set a hint to let applets position their menus correctly */
1918 pos = g_strdup_printf("%s,%d",
1919 side == PANEL_TOP ? "Top" :
1920 side == PANEL_BOTTOM ? "Bottom" :
1921 side == PANEL_LEFT ? "Left" :
1922 "Right", MENU_MARGIN(side));
1923 gdk_property_change(pi->socket->window,
1924 gdk_atom_intern("_ROX_PANEL_MENU_POS", FALSE),
1925 gdk_atom_intern("STRING", FALSE),
1926 8, GDK_PROP_MODE_REPLACE,
1927 pos, strlen(pos));
1928 g_free(pos);
1930 /* Ensure that the properties are set before starting the
1931 * applet.
1933 gdk_flush();
1936 g_object_set_data(G_OBJECT(pi->widget), "icon", pi);
1937 g_object_set_data(G_OBJECT(pi->socket), "panel", pi->panel);
1939 argv[1] = g_strdup_printf("%ld",
1940 GDK_WINDOW_XWINDOW(pi->socket->window));
1941 argv[2] = NULL;
1943 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
1944 NULL, NULL, &pid, &error))
1946 delayed_error(_("Error running applet:\n%s"), error->message);
1947 g_error_free(error);
1948 gtk_widget_destroy(pi->socket);
1949 pi->socket = NULL;
1951 else
1953 gtk_widget_ref(pi->socket);
1954 on_child_death(pid, (CallbackFn) applet_died, pi->socket);
1956 gtk_widget_ref(pi->socket);
1957 g_signal_connect(pi->socket, "destroy",
1958 G_CALLBACK(socket_destroyed), pi->widget);
1961 g_free(argv[1]);
1964 static void panel_post_resize(GtkWidget *win, GtkRequisition *req, Panel *panel)
1966 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
1968 req->width = panel_geometry.width;
1969 req->height += EDGE_WIDTH;
1971 else
1973 int h = panel_geometry.height;
1975 if (current_panel[PANEL_TOP])
1977 GtkWidget *win = current_panel[PANEL_TOP]->window;
1978 h -= win->allocation.height;
1981 if (current_panel[PANEL_BOTTOM])
1983 GtkWidget *win = current_panel[PANEL_BOTTOM]->window;
1984 h -= win->allocation.height;
1987 req->height = h;
1988 req->width += EDGE_WIDTH;
1992 static void update_side(GtkWidget *side)
1994 GList *kids, *next;
1996 kids = gtk_container_get_children(GTK_CONTAINER(side));
1997 for (next = kids; next; next = next->next)
1999 PanelIcon *pi;
2000 pi = g_object_get_data(next->data, "icon");
2001 panel_icon_set_tip(pi);
2003 g_list_free(kids);
2006 /* Tips or style has changed -- update everything on this panel */
2007 static void panel_set_style(Panel *panel)
2009 update_side(panel->before);
2010 update_side(panel->after);
2011 gtk_widget_queue_resize(panel->window);
2014 static gboolean recreate_panels(char **names)
2016 int i;
2018 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2020 if (names[i])
2022 panel_new(names[i], i);
2023 g_free(names[i]);
2027 g_free(names);
2029 return FALSE;
2032 static void update_side_size(GtkWidget *side)
2034 GList *kids, *next;
2036 kids = gtk_container_get_children(GTK_CONTAINER(side));
2037 for (next = kids; next; next = next->next)
2039 PanelIcon *pi;
2040 pi = g_object_get_data(next->data, "icon");
2041 gtk_widget_queue_resize(pi->widget);
2043 g_list_free(kids);
2046 /* Update panel size and redraw */
2047 static void panel_update(Panel *panel)
2049 update_side_size(panel->before);
2050 update_side_size(panel->after);
2051 gtk_widget_queue_resize(panel->window);
2052 gtk_widget_queue_draw(panel->window);
2055 static void panel_style_changed(void)
2057 int i;
2059 if (o_override_redirect.has_changed)
2061 gchar **names;
2063 names = g_new(char *, PANEL_NUMBER_OF_SIDES);
2065 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2067 Panel *panel = current_panel[i];
2068 names[i] = panel ? g_strdup(panel->name) : NULL;
2069 panel_new(NULL, i);
2072 g_idle_add((GtkFunction) recreate_panels, names);
2075 if (o_panel_style.has_changed)
2077 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2079 if (current_panel[i])
2080 panel_set_style(current_panel[i]);
2083 if (o_panel_width.has_changed)
2085 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2087 if (current_panel[i])
2088 panel_update(current_panel[i]);
2092 if (o_panel_xinerama.has_changed || o_panel_monitor.has_changed ||
2093 o_panel_avoid.has_changed)
2095 if (panel_check_xinerama() || o_panel_avoid.has_changed)
2097 for (i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
2099 if (current_panel[i])
2101 reposition_panel(
2102 current_panel[i]->window,
2103 &current_panel[i]->
2104 window->allocation,
2105 current_panel[i]);
2106 gtk_widget_queue_resize(
2107 current_panel[i]->window);
2114 static gboolean draw_panel_edge(GtkWidget *widget, GdkEventExpose *event,
2115 Panel *panel)
2117 int x, y, width, height;
2119 if (panel->side == PANEL_TOP || panel->side == PANEL_BOTTOM)
2121 width = panel_geometry.width;
2122 height = EDGE_WIDTH;
2124 x = 0;
2125 if (panel->side == PANEL_BOTTOM)
2126 y = 0;
2127 else
2128 y = widget->allocation.height - EDGE_WIDTH;
2130 else
2132 width = EDGE_WIDTH;
2133 height = panel_geometry.height;
2135 y = 0;
2136 if (panel->side == PANEL_RIGHT)
2137 x = 0;
2138 else
2139 x = widget->allocation.width - EDGE_WIDTH;
2142 gdk_draw_rectangle(widget->window,
2143 widget->style->fg_gc[GTK_STATE_NORMAL], TRUE,
2144 x, y, width, height);
2146 return FALSE;
2149 static gpointer parent_class;
2151 static void panel_icon_destroy(Icon *icon)
2153 PanelIcon *pi = (PanelIcon *) icon;
2155 g_return_if_fail(pi != NULL);
2157 if (pi->image)
2158 g_object_unref(pi->image);
2160 g_return_if_fail(pi->widget != NULL);
2162 gtk_widget_destroy(pi->widget);
2165 static void panel_remove_items(void)
2167 Panel *panel;
2169 g_return_if_fail(icon_selection != NULL);
2171 panel = ((PanelIcon *) icon_selection->data)->panel;
2173 while (icon_selection)
2174 icon_destroy((Icon *) icon_selection->data);
2176 panel_save(panel);
2179 /* Icon's size, shape or appearance has changed - update the display */
2180 static void panel_icon_redraw(Icon *icon)
2182 PanelIcon *pi = (PanelIcon *) icon;
2184 gtk_widget_set_state(pi->widget,
2185 icon->selected ? GTK_STATE_SELECTED
2186 : GTK_STATE_NORMAL);
2188 /* Will regenerate the scaled icon from the new image */
2189 gtk_widget_queue_resize(pi->widget);
2191 panel_icon_set_tip((PanelIcon *) icon);
2194 static void panel_icon_update(Icon *icon)
2196 PanelIcon *pi = (PanelIcon *) icon;
2198 gtk_widget_queue_draw(pi->widget);
2199 gtk_label_set_text(GTK_LABEL(pi->label), icon->item->leafname);
2200 panel_save(pi->panel);
2203 /* The point of this is to clear the selection if the existing icons
2204 * aren't from the same panel...
2206 static gboolean panel_icon_same_group(Icon *icon, Icon *other)
2208 if (IS_PANEL_ICON(other))
2210 PanelIcon *a = (PanelIcon *) icon;
2211 PanelIcon *b = (PanelIcon *) other;
2213 return a->panel == b->panel;
2215 else
2216 return FALSE;
2219 static void panel_icon_class_init(gpointer gclass, gpointer data)
2221 IconClass *icon = (IconClass *) gclass;
2223 parent_class = g_type_class_peek_parent(gclass);
2225 icon->destroy = panel_icon_destroy;
2226 icon->redraw = panel_icon_redraw;
2227 icon->update = panel_icon_update;
2228 icon->remove_items = panel_remove_items;
2229 icon->same_group = panel_icon_same_group;
2230 icon->wink = panel_icon_wink;
2233 static void panel_icon_init(GTypeInstance *object, gpointer gclass)
2235 PanelIcon *pi = (PanelIcon *) object;
2237 pi->widget = NULL;
2238 pi->image = NULL;
2239 pi->label = NULL;
2240 pi->socket = NULL;
2241 pi->style = TEXT_UNDER_ICON;
2244 static GType panel_icon_get_type(void)
2246 static GType type = 0;
2248 if (!type)
2250 static const GTypeInfo info =
2252 sizeof (PanelIconClass),
2253 NULL, /* base_init */
2254 NULL, /* base_finalise */
2255 panel_icon_class_init,
2256 NULL, /* class_finalise */
2257 NULL, /* class_data */
2258 sizeof(PanelIcon),
2259 0, /* n_preallocs */
2260 panel_icon_init
2263 type = g_type_register_static(icon_get_type(),
2264 "PanelIcon", &info, 0);
2267 return type;
2270 static PanelIcon *panel_icon_new(Panel *panel,
2271 const char *pathname,
2272 const char *name)
2274 PanelIcon *pi;
2275 Icon *icon;
2277 pi = g_object_new(panel_icon_get_type(), NULL);
2278 icon = (Icon *) pi;
2280 icon_set_path(icon, pathname, name);
2281 pi->panel = panel;
2283 return pi;
2286 static gboolean panel_want_show_text(PanelIcon *pi)
2288 Icon *icon = (Icon *) pi;
2290 if (!icon->item->leafname[0])
2291 return FALSE;
2293 if (o_panel_style.int_value == SHOW_BOTH)
2294 return TRUE;
2295 if (o_panel_style.int_value == SHOW_ICON)
2296 return FALSE;
2298 if (icon->item->flags & ITEM_FLAG_APPDIR)
2299 return FALSE;
2301 if (EXECUTABLE_FILE(icon->item))
2302 return FALSE;
2304 return TRUE;
2307 static void panel_show_options(Panel *panel)
2309 GtkWidget *dialog;
2310 gboolean already_showing = FALSE;
2312 if (panel_options_dialog)
2314 dialog = panel_options_dialog;
2315 already_showing = TRUE;
2317 else
2319 GladeXML *glade = get_glade_xml("Panel Options");
2321 dialog = glade_xml_get_widget(glade, "Panel Options");
2322 panel_options_dialog = dialog;
2323 g_signal_connect(dialog, "destroy",
2324 G_CALLBACK(gtk_widget_destroyed),
2325 &panel_options_dialog);
2326 g_signal_connect(dialog, "response",
2327 G_CALLBACK(gtk_widget_destroy), NULL);
2328 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
2331 g_object_set_data(G_OBJECT(panel_options_dialog), "rox-panel", panel);
2333 if (already_showing)
2335 GtkWindow *win = GTK_WINDOW(dialog);
2337 gtk_widget_hide(dialog);
2338 /* This extra set_position() should ensure it moves to new position
2339 * under pointer */
2340 gtk_window_set_position(win, GTK_WIN_POS_CENTER_ALWAYS);
2341 gtk_window_set_position(win, GTK_WIN_POS_MOUSE);
2342 gtk_window_present(win);
2344 else
2346 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
2347 gtk_widget_show_all(dialog);
2351 static void panel_position_menu(GtkMenu *menu, gint *x, gint *y,
2352 gboolean *push_in, gpointer data)
2354 int *pos = (int *) data;
2355 GtkRequisition requisition;
2356 int margin = pos[2];
2357 int mon = pos[3];
2358 int mon_right = monitor_geom[mon].x +
2359 monitor_geom[mon].width;
2360 int mon_bottom = monitor_geom[mon].y +
2361 monitor_geom[mon].height;
2363 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
2365 if (pos[0] == -1)
2366 *x = mon_right - margin - requisition.width;
2367 else if (pos[0] == -2)
2368 *x = monitor_geom[mon].x + margin;
2369 else
2370 *x = pos[0] - (requisition.width >> 2);
2372 if (pos[1] == -1)
2373 *y = mon_bottom - margin - requisition.height;
2374 else if (pos[1] == -2)
2375 *y = monitor_geom[mon].y + margin;
2376 else
2377 *y = pos[1] - (requisition.height >> 2);
2379 *x = CLAMP(*x, 0, mon_right - requisition.width);
2380 *y = CLAMP(*y, 0, mon_bottom - requisition.height);
2382 *push_in = FALSE;
2385 static void side_radio_toggled(GtkCheckMenuItem *item, Panel *panel)
2387 PanelSide new_side;
2388 PanelSide old_side;
2389 char *name, *other_side_name;
2391 if (!gtk_check_menu_item_get_active(item))
2392 return;
2393 new_side = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
2394 "rox-panel-side"));
2395 old_side = panel->side;
2396 if (new_side == old_side)
2397 return;
2399 name = g_strdup(panel->name);
2400 other_side_name = current_panel[new_side]
2401 ? g_strdup(current_panel[new_side]->name)
2402 : NULL;
2404 panel_new(name, new_side);
2405 panel_new(other_side_name, old_side);
2407 g_free(name);
2408 g_free(other_side_name);
2411 static void append_pos_to_menu(GtkWidget *menu, const char *label,
2412 PanelSide side, PanelSide current_side, GSList **pgroup, Panel *panel)
2414 GtkWidget *item = gtk_radio_menu_item_new_with_label(*pgroup, label);
2416 *pgroup = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item));
2417 if (side == current_side)
2418 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2419 g_object_set_data(G_OBJECT(item), "rox-panel-side", GINT_TO_POINTER(side));
2420 g_signal_connect(item, "toggled", G_CALLBACK(side_radio_toggled), panel);
2421 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
2422 gtk_widget_show(item);
2425 static void panel_show_menu(GdkEventButton *event, PanelIcon *pi, Panel *panel)
2427 GtkWidget *option_item;
2428 PanelSide side = panel->side;
2429 int pos[4];
2430 GtkWidget *pos_submenu;
2431 GtkWidget *change_side_item;
2432 GSList *pos_radio_group = NULL;
2434 pos[0] = event->x_root;
2435 pos[1] = event->y_root;
2436 pos[2] = MENU_MARGIN(side);
2437 /* FIXME: Should we read screen from event's window rather than
2438 * using default? */
2439 pos[3] = gdk_screen_get_monitor_at_point(
2440 gdk_screen_get_default(),
2441 event->x_root, event->y_root);
2443 option_item = gtk_image_menu_item_new_with_label(_("Panel Options..."));
2444 g_signal_connect_swapped(option_item, "activate",
2445 G_CALLBACK(panel_show_options), panel);
2447 pos_submenu = gtk_menu_new();
2448 append_pos_to_menu(pos_submenu, _("Top Edge"), PANEL_TOP, side,
2449 &pos_radio_group, panel);
2450 append_pos_to_menu(pos_submenu, _("Bottom Edge"), PANEL_BOTTOM, side,
2451 &pos_radio_group, panel);
2452 append_pos_to_menu(pos_submenu, _("Left Edge"), PANEL_LEFT, side,
2453 &pos_radio_group, panel);
2454 append_pos_to_menu(pos_submenu, _("Right Edge"), PANEL_RIGHT, side,
2455 &pos_radio_group, panel);
2456 change_side_item = gtk_menu_item_new_with_label(_("Change panel side"));
2457 gtk_menu_item_set_submenu(GTK_MENU_ITEM(change_side_item), pos_submenu);
2459 icon_prepare_menu((Icon *) pi, option_item, change_side_item, NULL);
2461 if (side == PANEL_LEFT)
2462 pos[0] = -2;
2463 else if (side == PANEL_RIGHT)
2464 pos[0] = -1;
2466 if (side == PANEL_TOP)
2467 pos[1] = -2;
2468 else if (side == PANEL_BOTTOM)
2469 pos[1] = -1;
2471 gtk_menu_popup(GTK_MENU(icon_menu), NULL, NULL,
2472 panel_position_menu,
2473 (gpointer) pos, event->button, event->time);
2476 /* Note: also called from icon handler */
2477 static gboolean panel_drag_motion(GtkWidget *widget,
2478 GdkDragContext *context,
2479 gint x,
2480 gint y,
2481 guint time,
2482 Panel *panel)
2484 int panel_x, panel_y;
2486 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
2488 motion_may_raise(panel, panel_x, panel_y);
2489 gdk_drag_status(context, 0, time);
2491 return TRUE;
2494 static gboolean insert_drag_motion(GtkWidget *widget,
2495 GdkDragContext *context,
2496 gint x,
2497 gint y,
2498 guint time,
2499 Panel *panel)
2501 int panel_x, panel_y;
2503 gdk_window_get_pointer(panel->window->window, &panel_x, &panel_y, NULL);
2504 motion_may_raise(panel, panel_x, panel_y);
2506 return FALSE;
2509 /* Note: also called from icon handler */
2510 static void panel_drag_leave(GtkWidget *widget,
2511 GdkDragContext *context,
2512 guint32 time,
2513 Panel *panel)
2515 GdkWindow *pinboard, *window;
2516 GtkAllocation *alloc = &panel->window->allocation;
2517 int x, y;
2519 window = panel->window->window;
2520 gdk_window_get_pointer(window, &x, &y, NULL);
2521 if (x < 0 || y < 0 || x > alloc->width || y > alloc->height)
2523 keep_below(panel->window->window, TRUE);
2525 /* Shouldn't need this as well as keep_below but some WMs don't
2526 * automatically lower as soon as the hint is set */
2527 pinboard = pinboard_get_window();
2528 window_put_just_above(panel->window->window, pinboard);
2532 static gboolean panel_check_xinerama(void)
2534 gint old_monitor = panel_monitor;
2536 panel_monitor = -1;
2538 if (o_panel_xinerama.int_value)
2540 if (o_panel_monitor.int_value < n_monitors)
2542 panel_monitor = o_panel_monitor.int_value;
2544 else
2546 g_warning(_("Xinerama monitor %d unavailable"),
2547 o_panel_monitor.int_value);
2551 if (panel_monitor == -1)
2553 panel_geometry.x = panel_geometry.y = 0;
2554 panel_geometry.width = screen_width;
2555 panel_geometry.height = screen_height;
2557 else
2559 panel_geometry = monitor_geom[panel_monitor];
2562 return old_monitor != panel_monitor;
2565 static GList *build_monitor_number(Option *option, xmlNode *node, guchar *label)
2567 GtkObject *adj;
2569 adj = gtk_adjustment_new(MAX(0, panel_monitor),
2570 0, n_monitors - 1, 1, 10, 1);
2571 return build_numentry_base(option, node, label, GTK_ADJUSTMENT(adj));
2574 /* Returns PANEL_NUMBER_OF_SIDES if name is invalid */
2575 PanelSide panel_name_to_side(gchar *side)
2577 if (strcmp(side, "Top") == 0)
2578 return PANEL_TOP;
2579 else if (strcmp(side, "Bottom") == 0)
2580 return PANEL_BOTTOM;
2581 else if (strcmp(side, "Left") == 0)
2582 return PANEL_LEFT;
2583 else if (strcmp(side, "Right") == 0)
2584 return PANEL_RIGHT;
2585 else
2586 g_warning("Unknown panel side '%s'", side);
2587 return PANEL_NUMBER_OF_SIDES;