4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* panel.c - code for dealing with panel windows */
42 #include "gui_support.h"
53 Panel
*current_panel
[PANEL_NUMBER_OF_SIDES
];
55 /* NULL => Not loading a panel */
56 static Panel
*loading_panel
= NULL
;
58 /* Static prototypes */
59 static int panel_delete(GtkWidget
*widget
, GdkEvent
*event
, Panel
*panel
);
60 static void panel_destroyed(GtkWidget
*widget
, Panel
*panel
);
61 static char *pan_from_file(guchar
*line
);
62 static gint
icon_button_release(GtkWidget
*widget
,
63 GdkEventButton
*event
,
65 static gint
icon_button_press(GtkWidget
*widget
,
66 GdkEventButton
*event
,
68 static void reposition_panel(Panel
*panel
, gboolean force_resize
);
69 static gint
expose_icon(GtkWidget
*widget
,
70 GdkEventExpose
*event
,
72 static gint
draw_icon(GtkWidget
*widget
,
73 GdkRectangle
*badarea
,
75 static gint
panel_button_release(GtkWidget
*widget
,
76 GdkEventButton
*event
,
78 static gint
panel_button_press(GtkWidget
*widget
,
79 GdkEventButton
*event
,
81 static void panel_post_resize(GtkWidget
*box
,
82 GtkRequisition
*req
, Panel
*panel
);
83 static void box_resized(GtkWidget
*box
, GtkAllocation
*alloc
, Panel
*panel
);
84 static void drag_set_panel_dest(Icon
*icon
);
85 static void add_uri_list(GtkWidget
*widget
,
86 GdkDragContext
*context
,
89 GtkSelectionData
*selection_data
,
93 static void panel_add_item(Panel
*panel
,
97 static gboolean
drag_motion(GtkWidget
*widget
,
98 GdkDragContext
*context
,
103 static void drag_leave(GtkWidget
*widget
,
104 GdkDragContext
*context
,
107 static GList
*get_widget_list(Panel
*panel
);
108 static GtkWidget
*make_insert_frame(Panel
*panel
);
109 static gboolean
enter_icon(GtkWidget
*widget
,
110 GdkEventCrossing
*event
,
112 static gint
icon_motion_event(GtkWidget
*widget
,
113 GdkEventMotion
*event
,
115 static gint
panel_motion_event(GtkWidget
*widget
,
116 GdkEventMotion
*event
,
118 static void reposition_icon(Icon
*icon
, int index
);
119 static void start_drag(Icon
*icon
, GdkEventMotion
*event
);
120 static guchar
*create_uri_list(GList
*list
);
121 static void drag_end(GtkWidget
*widget
,
122 GdkDragContext
*context
,
124 static void perform_action(Panel
*panel
,
126 GdkEventButton
*event
);
127 static void run_applet(Icon
*icon
);
128 static void panel_set_style(guchar
*new);
129 static void size_request(GtkWidget
*widget
, GtkRequisition
*req
, Icon
*icon
);
132 static GtkWidget
*dnd_highlight
= NULL
; /* (stops flickering) */
134 /* When sliding the panel, records where the panel was before */
135 static gint slide_from_value
= 0;
138 #define SHOW_APPS_SMALL 1
140 static int panel_style
= SHOW_APPS_SMALL
;
142 static int closing_panel
= 0; /* Don't panel_save; destroying! */
144 /****************************************************************
145 * EXTERNAL INTERFACE *
146 ****************************************************************/
148 void panel_init(void)
150 option_add_int("panel_style", panel_style
, panel_set_style
);
153 /* 'name' may be NULL or "" to remove the panel */
154 Panel
*panel_new(guchar
*name
, PanelSide side
)
158 GtkWidget
*vp
, *box
, *frame
;
160 g_return_val_if_fail(side
>= 0 && side
< PANEL_NUMBER_OF_SIDES
, NULL
);
161 g_return_val_if_fail(loading_panel
== NULL
, NULL
);
163 if (name
&& *name
== '\0')
166 if (current_panel
[side
])
171 gtk_widget_destroy(current_panel
[side
]->window
);
177 if (name
== NULL
|| *name
== '\0')
180 panel
= g_new(Panel
, 1);
181 panel
->name
= g_strdup(name
);
184 panel
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
185 gtk_window_set_wmclass(GTK_WINDOW(panel
->window
), "ROX-Panel", PROJECT
);
186 gtk_widget_set_events(panel
->window
,
187 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
188 GDK_BUTTON1_MOTION_MASK
| GDK_BUTTON2_MOTION_MASK
|
189 GDK_BUTTON2_MOTION_MASK
);
191 gtk_signal_connect(GTK_OBJECT(panel
->window
), "delete-event",
192 GTK_SIGNAL_FUNC(panel_delete
), panel
);
193 gtk_signal_connect(GTK_OBJECT(panel
->window
), "destroy",
194 GTK_SIGNAL_FUNC(panel_destroyed
), panel
);
195 gtk_signal_connect(GTK_OBJECT(panel
->window
), "button_press_event",
196 GTK_SIGNAL_FUNC(panel_button_press
), panel
);
197 gtk_signal_connect(GTK_OBJECT(panel
->window
), "button_release_event",
198 GTK_SIGNAL_FUNC(panel_button_release
), panel
);
199 gtk_signal_connect(GTK_OBJECT(panel
->window
), "motion-notify-event",
200 GTK_SIGNAL_FUNC(panel_motion_event
), panel
);
202 if (strchr(name
, '/'))
203 load_path
= g_strdup(name
);
208 leaf
= g_strconcat("pan_", name
, NULL
);
209 load_path
= choices_find_path_load(leaf
, PROJECT
);
213 vp
= gtk_viewport_new(NULL
, NULL
);
214 gtk_viewport_set_shadow_type(GTK_VIEWPORT(vp
), GTK_SHADOW_OUT
);
215 gtk_container_add(GTK_CONTAINER(panel
->window
), vp
);
217 if (side
== PANEL_TOP
|| side
== PANEL_BOTTOM
)
219 panel
->adj
= gtk_viewport_get_hadjustment(GTK_VIEWPORT(vp
));
220 box
= gtk_hbox_new(FALSE
, 0);
221 panel
->before
= gtk_hbox_new(FALSE
, 0);
222 panel
->after
= gtk_hbox_new(FALSE
, 0);
226 panel
->adj
= gtk_viewport_get_vadjustment(GTK_VIEWPORT(vp
));
227 box
= gtk_vbox_new(FALSE
, 0);
228 panel
->before
= gtk_vbox_new(FALSE
, 0);
229 panel
->after
= gtk_vbox_new(FALSE
, 0);
232 gtk_container_add(GTK_CONTAINER(vp
), box
);
233 gtk_box_pack_start(GTK_BOX(box
), panel
->before
, FALSE
, TRUE
, 0);
234 gtk_box_pack_end(GTK_BOX(box
), panel
->after
, FALSE
, TRUE
, 0);
236 frame
= make_insert_frame(panel
);
237 gtk_box_pack_start(GTK_BOX(box
), frame
, TRUE
, TRUE
, 4);
239 /* This is used so that we can find the middle easily! */
240 panel
->gap
= gtk_event_box_new();
241 gtk_box_pack_start(GTK_BOX(box
), panel
->gap
, FALSE
, FALSE
, 0);
243 frame
= make_insert_frame(panel
);
244 gtk_object_set_data(GTK_OBJECT(frame
), "after", "yes");
245 gtk_box_pack_start(GTK_BOX(box
), frame
, TRUE
, TRUE
, 4);
247 gtk_widget_realize(panel
->window
);
248 make_panel_window(panel
->window
->window
);
250 gtk_widget_show_all(vp
);
252 loading_panel
= panel
;
253 if (load_path
&& access(load_path
, F_OK
) == 0)
254 parse_file(load_path
, pan_from_file
);
257 /* Don't scare users with an empty panel... */
260 panel_add_item(panel
, "~", "Home", FALSE
);
262 apps
= pathdup(make_path(app_dir
, "..")->str
);
265 panel_add_item(panel
, apps
, "Apps", FALSE
);
269 loading_panel
= NULL
;
272 current_panel
[side
] = panel
;
274 gtk_widget_queue_resize(box
);
275 gtk_signal_connect_after(GTK_OBJECT(panel
->window
), "size-request",
276 GTK_SIGNAL_FUNC(panel_post_resize
), (GtkObject
*) panel
);
277 gtk_signal_connect_after(GTK_OBJECT(box
), "size-allocate",
278 GTK_SIGNAL_FUNC(box_resized
), (GtkObject
*) panel
);
281 gtk_widget_show(panel
->window
);
286 gboolean
panel_want_show_text(Icon
*icon
)
288 if (panel_style
== SHOW_BOTH
)
290 if (panel_style
== SHOW_ICON
)
293 if (icon
->item
.flags
& ITEM_FLAG_APPDIR
)
299 void panel_icon_renamed(Icon
*icon
)
301 GtkLabel
*label
= GTK_LABEL(icon
->label
);
303 gtk_label_set_text(label
, icon
->item
.leafname
);
307 /****************************************************************
308 * INTERNAL FUNCTIONS *
309 ****************************************************************/
311 /* User has tried to close the panel via the window manager - confirm */
312 static int panel_delete(GtkWidget
*widget
, GdkEvent
*event
, Panel
*panel
)
314 return get_choice(_("Close panel?"),
315 _("You have tried to close a panel via the window "
316 "manager - I usually find that this is accidental... "
318 2, _("Remove"), _("Cancel")) != 0;
321 static void panel_destroyed(GtkWidget
*widget
, Panel
*panel
)
323 if (current_panel
[panel
->side
] == panel
)
324 current_panel
[panel
->side
] = NULL
;
326 if (panel
->side
== PANEL_TOP
|| panel
->side
== PANEL_BOTTOM
)
328 if (current_panel
[PANEL_RIGHT
])
329 reposition_panel(current_panel
[PANEL_RIGHT
], FALSE
);
330 if (current_panel
[PANEL_LEFT
])
331 reposition_panel(current_panel
[PANEL_LEFT
], FALSE
);
337 if (--number_of_windows
< 1)
341 /* Called for each line in the config file while loading a new panel */
342 static char *pan_from_file(guchar
*line
)
346 g_return_val_if_fail(line
!= NULL
, NULL
);
347 g_return_val_if_fail(loading_panel
!= NULL
, NULL
);
352 sep
= strpbrk(line
, "<>");
354 return _("Missing < or > in panel config file");
357 leaf
= g_strndup(line
, sep
- line
);
361 panel_add_item(loading_panel
,
371 /* Add an icon with this path to the panel. If after is TRUE then the
372 * icon is added to the right/bottom end of the panel.
374 * If name is NULL a suitable name is taken from path.
376 static void panel_add_item(Panel
*panel
,
384 widget
= gtk_event_box_new();
385 gtk_widget_set_events(widget
,
386 GDK_BUTTON1_MOTION_MASK
| GDK_BUTTON2_MOTION_MASK
|
387 GDK_BUTTON3_MOTION_MASK
|
388 GDK_EXPOSURE_MASK
| GDK_BUTTON_PRESS_MASK
|
389 GDK_BUTTON_RELEASE_MASK
);
391 gtk_box_pack_start(GTK_BOX(after
? panel
->after
: panel
->before
),
392 widget
, FALSE
, TRUE
, 4);
394 gtk_box_reorder_child(GTK_BOX(panel
->after
), widget
, 0);
396 gtk_widget_realize(widget
);
398 icon
= g_new(Icon
, 1);
400 icon
->src_path
= g_strdup(path
);
401 icon
->path
= icon_convert_path(path
);
408 gtk_object_set_data(GTK_OBJECT(widget
), "icon", icon
);
410 icon_hash_path(icon
);
412 icon
->widget
= widget
;
413 gtk_widget_set_name(icon
->widget
, "panel-icon");
414 icon
->selected
= FALSE
;
415 diritem_stat(icon
->path
, &icon
->item
, FALSE
);
418 icon
->item
.leafname
= g_strdup(name
);
423 slash
= strrchr(icon
->path
, '/');
424 icon
->item
.leafname
= g_strdup(slash
&& slash
[1] ? slash
+ 1
428 gtk_signal_connect_object(GTK_OBJECT(widget
), "destroy",
429 GTK_SIGNAL_FUNC(icon_destroyed
), (gpointer
) icon
);
431 if (icon
->item
.base_type
== TYPE_DIRECTORY
)
434 gtk_signal_connect(GTK_OBJECT(widget
), "button_release_event",
435 GTK_SIGNAL_FUNC(icon_button_release
), icon
);
436 gtk_signal_connect(GTK_OBJECT(widget
), "button_press_event",
437 GTK_SIGNAL_FUNC(icon_button_press
), icon
);
438 gtk_signal_connect(GTK_OBJECT(icon
->widget
), "motion-notify-event",
439 GTK_SIGNAL_FUNC(icon_motion_event
), icon
);
443 gtk_signal_connect_after(GTK_OBJECT(widget
),
444 "enter-notify-event",
445 GTK_SIGNAL_FUNC(enter_icon
), icon
);
447 gtk_signal_connect_after(GTK_OBJECT(widget
), "draw",
448 GTK_SIGNAL_FUNC(draw_icon
), icon
);
450 gtk_signal_connect_after(GTK_OBJECT(widget
), "expose_event",
451 GTK_SIGNAL_FUNC(expose_icon
), icon
);
452 gtk_signal_connect(GTK_OBJECT(widget
), "drag_data_get",
453 GTK_SIGNAL_FUNC(drag_data_get
), NULL
);
455 gtk_signal_connect(GTK_OBJECT(widget
), "size_request",
456 GTK_SIGNAL_FUNC(size_request
), icon
);
458 drag_set_panel_dest(icon
);
460 icon
->label
= gtk_label_new(icon
->item
.leafname
);
461 gtk_container_add(GTK_CONTAINER(icon
->widget
), icon
->label
);
462 gtk_misc_set_alignment(GTK_MISC(icon
->label
), 0.5, 1);
463 gtk_misc_set_padding(GTK_MISC(icon
->label
), 1, 2);
470 gtk_widget_show(widget
);
473 /* Called when Gtk+ wants to know how much space an icon needs.
474 * 'req' is already big enough for the label, if shown.
476 static void size_request(GtkWidget
*widget
, GtkRequisition
*req
, Icon
*icon
)
478 int im_width
, im_height
;
480 im_width
= icon
->item
.image
->width
;
481 im_height
= MIN(icon
->item
.image
->height
, ICON_HEIGHT
);
483 req
->height
+= im_height
;
484 req
->width
= MAX(req
->width
, im_width
);
487 static gint
expose_icon(GtkWidget
*widget
,
488 GdkEventExpose
*event
,
491 return draw_icon(widget
, &event
->area
, icon
);
494 static gint
draw_icon(GtkWidget
*widget
, GdkRectangle
*badarea
, Icon
*icon
)
499 gdk_window_get_size(widget
->window
, &width
, &height
);
503 area
.height
= icon
->item
.image
->height
;
505 if (panel_want_show_text(icon
))
507 int text_height
= icon
->label
->requisition
.height
;
509 area
.y
= height
- text_height
- area
.height
;
511 draw_large_icon(widget
, &area
, &icon
->item
, icon
->selected
);
515 area
.y
= (height
- area
.height
) >> 1;
516 draw_large_icon(widget
, &area
, &icon
->item
, icon
->selected
);
522 /* icon may be NULL if the event is on the background */
523 static void perform_action(Panel
*panel
, Icon
*icon
, GdkEventButton
*event
)
527 action
= bind_lookup_bev(icon
? BIND_PANEL_ICON
: BIND_PANEL
, event
);
529 if (icon
&& icon
->socket
)
530 if (action
!= ACT_POPUP_MENU
&& action
!= ACT_MOVE_ICON
)
537 wink_widget(icon
->widget
);
538 run_diritem(icon
->path
, &icon
->item
, NULL
, NULL
, FALSE
);
542 wink_widget(icon
->widget
);
543 run_diritem(icon
->path
, &icon
->item
, NULL
, NULL
, TRUE
);
547 icon_show_menu(event
, icon
, panel
);
550 dnd_motion_start(MOTION_REPOSITION
);
552 case ACT_PRIME_AND_SELECT
:
554 icon_select_only(icon
);
555 dnd_motion_start(MOTION_READY_FOR_DND
);
557 case ACT_PRIME_AND_TOGGLE
:
558 icon_set_selected(icon
, !icon
->selected
);
559 dnd_motion_start(MOTION_READY_FOR_DND
);
561 case ACT_PRIME_FOR_DND
:
562 dnd_motion_start(MOTION_READY_FOR_DND
);
564 case ACT_TOGGLE_SELECTED
:
565 icon_set_selected(icon
, !icon
->selected
);
567 case ACT_SELECT_EXCL
:
568 icon_set_selected(icon
, TRUE
);
570 case ACT_SLIDE_CLEAR_PANEL
:
571 icon_select_only(NULL
);
573 case ACT_SLIDE_PANEL
:
574 dnd_motion_grab_pointer();
575 slide_from_value
= panel
->adj
->value
;
576 dnd_motion_start(MOTION_REPOSITION
);
580 case ACT_CLEAR_SELECTION
:
581 icon_select_only(NULL
);
584 g_warning("Unsupported action : %d\n", action
);
589 static gint
panel_button_release(GtkWidget
*widget
,
590 GdkEventButton
*event
,
593 if (dnd_motion_release(event
))
596 perform_action(panel
, NULL
, event
);
601 static gint
panel_button_press(GtkWidget
*widget
,
602 GdkEventButton
*event
,
605 if (dnd_motion_press(panel
->window
, event
))
606 perform_action(panel
, NULL
, event
);
611 static gint
icon_button_release(GtkWidget
*widget
,
612 GdkEventButton
*event
,
615 if (icon
->socket
&& event
->button
== 1)
616 return FALSE
; /* Restart button */
618 if (dnd_motion_release(event
))
621 perform_action(icon
->panel
, icon
, event
);
626 static gint
icon_button_press(GtkWidget
*widget
,
627 GdkEventButton
*event
,
630 if (icon
->socket
&& event
->button
== 1)
631 return FALSE
; /* Restart button */
633 if (dnd_motion_press(widget
, event
))
634 perform_action(icon
->panel
, icon
, event
);
639 /* Difference between height of an icon and height of panel (or width) */
642 static void reposition_panel(Panel
*panel
, gboolean force_resize
)
646 GList
*next
, *children
;
647 PanelSide side
= panel
->side
;
649 children
= get_widget_list(panel
);
651 for (next
= children
; next
; next
= next
->next
)
653 GtkWidget
*widget
= (GtkWidget
*) next
->data
;
656 gtk_widget_get_child_requisition(widget
, &req
);
664 g_list_free(children
);
666 if (side
== PANEL_TOP
|| side
== PANEL_BOTTOM
)
674 if (side
== PANEL_RIGHT
)
675 x
= screen_width
- w
;
679 if (current_panel
[PANEL_TOP
])
683 ph
= current_panel
[PANEL_TOP
]->height
;
688 if (current_panel
[PANEL_BOTTOM
])
689 h
-= current_panel
[PANEL_BOTTOM
]->height
;
692 if (side
== PANEL_BOTTOM
)
693 y
= screen_height
- h
;
695 gtk_widget_set_uposition(panel
->window
, x
, y
);
697 gdk_window_move_resize(panel
->window
->window
, x
, y
, w
, h
);
699 if (side
== PANEL_BOTTOM
|| side
== PANEL_TOP
)
701 if (current_panel
[PANEL_RIGHT
])
702 reposition_panel(current_panel
[PANEL_RIGHT
], FALSE
);
703 if (current_panel
[PANEL_LEFT
])
704 reposition_panel(current_panel
[PANEL_LEFT
], FALSE
);
708 /* Same as drag_set_dest(), but for panel icons */
709 static void drag_set_panel_dest(Icon
*icon
)
711 GtkObject
*obj
= GTK_OBJECT(icon
->widget
);
713 make_drop_target(icon
->widget
, 0);
715 gtk_signal_connect(obj
, "drag_motion",
716 GTK_SIGNAL_FUNC(drag_motion
), icon
);
717 gtk_signal_connect(obj
, "drag_leave",
718 GTK_SIGNAL_FUNC(drag_leave
), icon
);
719 gtk_signal_connect(obj
, "drag_end",
720 GTK_SIGNAL_FUNC(drag_end
), icon
);
723 static gboolean
drag_motion(GtkWidget
*widget
,
724 GdkDragContext
*context
,
730 GdkDragAction action
= context
->suggested_action
;
732 DirItem
*item
= &icon
->item
;
735 goto out
; /* Can't drag a selection to itself */
737 type
= dnd_motion_item(context
, &item
);
742 /* We actually must pretend to accept the drop, even if the
743 * directory isn't writeable, so that the spring-opening
747 /* Don't allow drops to non-writeable directories */
748 if (option_get_int("dnd_spring_open") == FALSE
&&
749 type
== drop_dest_dir
&&
750 access(icon
->path
, W_OK
) != 0)
755 g_dataset_set_data(context
, "drop_dest_type", type
);
758 gdk_drag_status(context
, action
, time
);
759 g_dataset_set_data_full(context
, "drop_dest_path",
760 g_strdup(icon
->path
), g_free
);
761 if (type
== drop_dest_dir
)
762 dnd_spring_load(context
, NULL
);
764 if (dnd_highlight
&& dnd_highlight
!= icon
->widget
)
766 gtk_drag_unhighlight(dnd_highlight
);
767 dnd_highlight
= NULL
;
770 if (dnd_highlight
== NULL
)
772 gtk_drag_highlight(icon
->widget
);
773 dnd_highlight
= icon
->widget
;
781 static void add_uri_list(GtkWidget
*widget
,
782 GdkDragContext
*context
,
785 GtkSelectionData
*selection_data
,
790 gboolean after
= FALSE
;
793 if (!selection_data
->data
)
796 g_return_if_fail(selection_data
->data
[selection_data
->length
] == '\0');
798 if (gtk_object_get_data(GTK_OBJECT(widget
), "after"))
801 uris
= uri_list_to_glist(selection_data
->data
);
803 for (next
= uris
; next
; next
= next
->next
)
807 path
= get_local_path((guchar
*) next
->data
);
810 panel_add_item(panel
, path
, NULL
, after
);
816 static void drag_end(GtkWidget
*widget
,
817 GdkDragContext
*context
,
820 if (tmp_icon_selected
)
822 icon_select_only(NULL
);
823 tmp_icon_selected
= FALSE
;
827 static void drag_leave(GtkWidget
*widget
,
828 GdkDragContext
*context
,
832 if (dnd_highlight
&& dnd_highlight
== widget
)
834 gtk_drag_unhighlight(dnd_highlight
);
835 dnd_highlight
= NULL
;
841 /* Writes lines to the file, one for each widget, prefixed by 'side'.
842 * Returns TRUE on success, or FALSE on error (and sets errno).
843 * Always frees the widgets list.
845 static gboolean
write_widgets(FILE *file
, GList
*widgets
, guchar side
)
850 tmp
= g_string_new(NULL
);
852 for (next
= widgets
; next
; next
= next
->next
)
856 icon
= gtk_object_get_data(GTK_OBJECT(next
->data
), "icon");
860 g_warning("Can't find Icon from widget\n");
864 g_string_sprintf(tmp
, "%s%c%s\n",
866 side
, icon
->src_path
);
867 if (fwrite(tmp
->str
, 1, tmp
->len
, file
) < tmp
->len
)
869 g_list_free(widgets
);
874 g_string_free(tmp
, TRUE
);
877 g_list_free(widgets
);
882 void panel_save(Panel
*panel
)
886 guchar
*save_new
= NULL
;
888 g_return_if_fail(panel
!= NULL
);
890 if (strchr(panel
->name
, '/'))
891 save
= g_strdup(panel
->name
);
896 leaf
= g_strconcat("pan_", panel
->name
, NULL
);
897 save
= choices_find_path_save(leaf
, PROJECT
, TRUE
);
904 save_new
= g_strconcat(save
, ".new", NULL
);
905 file
= fopen(save_new
, "wb");
909 if (!write_widgets(file
,
910 gtk_container_children(GTK_CONTAINER(panel
->before
)),
914 if (!write_widgets(file
,
915 g_list_reverse(gtk_container_children(
916 GTK_CONTAINER(panel
->after
))),
928 if (rename(save_new
, save
))
933 delayed_rox_error(_("Could not save panel: %s"), g_strerror(errno
));
941 static GList
*get_widget_list(Panel
*panel
)
945 list
= gtk_container_children(GTK_CONTAINER(panel
->before
));
946 list
= g_list_concat(list
,
947 gtk_container_children(GTK_CONTAINER(panel
->after
)));
952 /* Create a frame widget which can be used to add icons to the panel */
953 static GtkWidget
*make_insert_frame(Panel
*panel
)
956 GtkTargetEntry target_table
[] = {
957 {"text/uri-list", 0, TARGET_URI_LIST
},
960 frame
= gtk_frame_new(NULL
);
961 gtk_frame_set_shadow_type(GTK_FRAME(frame
), GTK_SHADOW_NONE
);
962 gtk_widget_set_usize(frame
, 16, 16);
964 gtk_signal_connect(GTK_OBJECT(frame
), "drag-data-received",
965 GTK_SIGNAL_FUNC(add_uri_list
), panel
);
966 gtk_drag_dest_set(frame
,
967 GTK_DEST_DEFAULT_ALL
,
969 sizeof(target_table
) / sizeof(*target_table
),
975 static gboolean
enter_icon(GtkWidget
*widget
,
976 GdkEventCrossing
*event
,
979 icon_may_update(icon
);
984 static gint
panel_motion_event(GtkWidget
*widget
,
985 GdkEventMotion
*event
,
989 gboolean horz
= panel
->side
== PANEL_TOP
|| panel
->side
== PANEL_BOTTOM
;
991 if (motion_state
!= MOTION_REPOSITION
)
995 delta
= event
->x_root
- drag_start_x
;
997 delta
= event
->y_root
- drag_start_y
;
999 new = slide_from_value
- delta
;
1000 new = CLAMP(new, 0, panel
->adj
->upper
- panel
->adj
->page_size
);
1002 gtk_adjustment_set_value(panel
->adj
, new);
1007 static gint
icon_motion_event(GtkWidget
*widget
,
1008 GdkEventMotion
*event
,
1011 Panel
*panel
= icon
->panel
;
1013 gboolean horz
= panel
->side
== PANEL_TOP
|| panel
->side
== PANEL_BOTTOM
;
1017 if (motion_state
== MOTION_READY_FOR_DND
)
1019 if (dnd_motion_moved(event
))
1020 start_drag(icon
, event
);
1023 else if (motion_state
!= MOTION_REPOSITION
)
1026 list
= gtk_container_children(GTK_CONTAINER(panel
->before
));
1027 list
= g_list_append(list
, NULL
); /* The gap in the middle */
1028 list
= g_list_concat(list
,
1029 gtk_container_children(GTK_CONTAINER(panel
->after
)));
1030 me
= g_list_find(list
, widget
);
1032 g_return_val_if_fail(me
!= NULL
, TRUE
);
1034 val
= horz
? event
->x_root
: event
->y_root
;
1042 prev
= GTK_WIDGET(me
->prev
->data
);
1046 gdk_window_get_deskrelative_origin(prev
->window
, &x
, &y
);
1048 if (val
<= (horz
? x
: y
))
1052 if (dir
== 0 && me
->next
)
1058 next
= GTK_WIDGET(me
->next
->data
);
1062 gdk_window_get_deskrelative_origin(next
->window
, &x
, &y
);
1064 gdk_window_get_size(next
->window
, &w
, &h
);
1069 if (val
>= (horz
? x
: y
))
1071 if (next
== panel
->gap
)
1079 reposition_icon(icon
, g_list_index(list
, widget
) + dir
);
1084 /* Move icon to this index in the complete widget list.
1085 * 0 makes the icon the left-most icon. The gap in the middle has
1086 * an index number, which allows you to specify that the icon should
1087 * go on the left or right side.
1089 static void reposition_icon(Icon
*icon
, int index
)
1091 Panel
*panel
= icon
->panel
;
1092 GtkWidget
*widget
= icon
->widget
;
1096 list
= gtk_container_children(GTK_CONTAINER(panel
->before
));
1097 before_len
= g_list_length(list
);
1099 if (index
<= before_len
)
1101 /* Want to move icon to the 'before' list. Is it there
1105 if (!g_list_find(list
, widget
))
1108 gtk_grab_remove(widget
);
1109 gtk_widget_reparent(widget
, panel
->before
);
1110 dnd_motion_grab_pointer();
1111 gtk_grab_add(widget
);
1114 gtk_box_reorder_child(GTK_BOX(panel
->before
), widget
, index
);
1118 /* Else, we need it in the 'after' list. */
1120 index
-= before_len
+ 1;
1124 list
= gtk_container_children(GTK_CONTAINER(panel
->after
));
1126 if (!g_list_find(list
, widget
))
1128 /* Not already there, reparent */
1129 gtk_grab_remove(widget
);
1130 gtk_widget_reparent(widget
, panel
->after
);
1131 dnd_motion_grab_pointer();
1132 gtk_grab_add(widget
);
1135 gtk_box_reorder_child(GTK_BOX(panel
->after
), widget
, index
);
1143 static void start_drag(Icon
*icon
, GdkEventMotion
*event
)
1145 GtkWidget
*widget
= icon
->widget
;
1147 if (!icon
->selected
)
1149 if (event
->state
& GDK_BUTTON1_MASK
)
1151 /* Select just this one */
1152 icon_select_only(icon
);
1153 tmp_icon_selected
= TRUE
;
1156 icon_set_selected(icon
, TRUE
);
1159 g_return_if_fail(icon_selection
!= NULL
);
1161 if (icon_selection
->next
== NULL
)
1162 drag_one_item(widget
, event
, icon
->path
, &icon
->item
);
1167 uri_list
= create_uri_list(icon_selection
);
1168 drag_selection(widget
, event
, uri_list
);
1173 /* Return a text/uri-list of all the icons in the list */
1174 static guchar
*create_uri_list(GList
*list
)
1180 tmp
= g_string_new(NULL
);
1181 leader
= g_strdup_printf("file://%s", our_host_name_for_dnd());
1183 for (; list
; list
= list
->next
)
1185 Icon
*icon
= (Icon
*) list
->data
;
1187 g_string_append(tmp
, leader
);
1188 g_string_append(tmp
, icon
->path
);
1189 g_string_append(tmp
, "\r\n");
1194 g_string_free(tmp
, FALSE
);
1199 static void applet_died(GtkWidget
*socket
)
1201 gboolean never_plugged
;
1203 never_plugged
= (!gtk_object_get_data(GTK_OBJECT(socket
), "lost_plug"))
1204 && !GTK_SOCKET(socket
)->plug_window
;
1209 _("Applet quit without ever creating a widget!"));
1210 gtk_widget_destroy(socket
);
1213 gtk_widget_unref(socket
);
1216 static void socket_destroyed(GtkWidget
*socket
, GtkWidget
*widget
)
1218 gtk_object_set_data(GTK_OBJECT(socket
), "lost_plug", "yes");
1220 gtk_widget_unref(socket
);
1222 gtk_widget_destroy(widget
); /* Remove from panel */
1225 panel_save(gtk_object_get_data(GTK_OBJECT(socket
), "panel"));
1228 /* Try to run this applet.
1231 * - No executable AppletRun:
1232 * icon->socket == NULL (unchanged) on return.
1234 * Otherwise, create socket (setting icon->socket) and ref it twice.
1236 * - AppletRun quits without connecting a plug:
1237 * On child death lost_plug is unset and socket is empty.
1239 * Report error and destroy widget (to 'socket destroyed').
1241 * - AppletRun quits while plug is in socket:
1242 * Unref socket once. Socket will be destroyed later.
1244 * - Socket is destroyed.
1245 * Set lost_plug = "yes" and remove widget from panel.
1248 static void run_applet(Icon
*icon
)
1253 argv
[0] = make_path(icon
->path
, "AppletRun")->str
;
1255 if (access(argv
[0], X_OK
) != 0)
1258 icon
->socket
= gtk_socket_new();
1259 /* Two refs held: one for child death, one for socket destroyed */
1260 gtk_widget_ref(icon
->socket
);
1261 gtk_widget_ref(icon
->socket
);
1263 gtk_container_add(GTK_CONTAINER(icon
->widget
), icon
->socket
);
1264 gtk_widget_show_all(icon
->socket
);
1265 gtk_widget_realize(icon
->socket
);
1267 gtk_object_set_data(GTK_OBJECT(icon
->widget
), "icon", icon
);
1268 gtk_object_set_data(GTK_OBJECT(icon
->socket
), "panel", icon
->panel
);
1270 gtk_signal_connect(GTK_OBJECT(icon
->socket
), "destroy",
1271 GTK_SIGNAL_FUNC(socket_destroyed
), icon
->widget
);
1273 argv
[1] = g_strdup_printf("%ld",
1274 GDK_WINDOW_XWINDOW(icon
->socket
->window
));
1279 on_child_death(pid
, (CallbackFn
) applet_died
, icon
->socket
);
1284 /* When one of the panel icons resizes it will cause it's container box
1285 * to resize. This will cause the packing box inside the viewport to resize -
1286 * we get here right after that.
1288 static void box_resized(GtkWidget
*box
, GtkAllocation
*alloc
, Panel
*panel
)
1290 reposition_panel(panel
, FALSE
);
1293 static void panel_post_resize(GtkWidget
*win
, GtkRequisition
*req
, Panel
*panel
)
1295 if (panel
->side
== PANEL_TOP
|| panel
->side
== PANEL_BOTTOM
)
1297 if (req
->width
< screen_width
)
1298 req
->width
= screen_width
;
1302 int h
= screen_height
;
1304 if (current_panel
[PANEL_TOP
])
1305 h
-= current_panel
[PANEL_TOP
]->height
;
1307 if (current_panel
[PANEL_BOTTOM
])
1308 h
-= current_panel
[PANEL_BOTTOM
]->height
;
1310 if (req
->height
< h
)
1315 /* The style setting has been changed -- update all panels */
1316 static void panel_set_style(guchar
*new)
1318 int os
= panel_style
;
1320 panel_style
= option_get_int("panel_style");
1322 if (os
!= panel_style
)
1328 for (i
= 0; i
< PANEL_NUMBER_OF_SIDES
; i
++)
1330 Panel
*panel
= current_panel
[i
];
1335 gtk_widget_queue_resize(panel
->window
);