r4332: When iconfiying a window, try to find space on the pinboard of the current
[rox-filer/translations.git] / ROX-Filer / src / gui_support.c
blob99c5dbeae4b281bd499a0c6e2316552ed0f851d4
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* gui_support.c - general (GUI) support routines */
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/param.h>
30 #include <stdarg.h>
31 #include <errno.h>
32 #include <time.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <gdk/gdkx.h>
37 #include <gdk/gdk.h>
38 #include <gdk/gdkkeysyms.h>
40 #include "global.h"
42 #include "main.h"
43 #include "gui_support.h"
44 #include "support.h"
45 #include "pixmaps.h"
46 #include "choices.h"
47 #include "options.h"
49 gint screen_width, screen_height;
51 gint n_monitors;
52 GdkRectangle *monitor_geom = NULL;
53 gint monitor_width, monitor_height;
54 MonitorAdjacent *monitor_adjacent;
56 static GdkAtom xa_cardinal;
58 static GtkWidget *current_dialog = NULL;
60 static GtkWidget *tip_widget = NULL;
61 static time_t tip_time = 0; /* Time tip widget last closed */
62 static gint tip_timeout = 0; /* When primed */
64 /* Static prototypes */
65 static void run_error_info_dialog(GtkMessageType type, const char *message,
66 va_list args);
67 static GType simple_image_get_type(void);
68 static void gui_get_monitor_adjacent(int monitor, MonitorAdjacent *adj);
70 void gui_store_screen_geometry(GdkScreen *screen)
72 gint mon;
74 screen_width = gdk_screen_get_width(screen);
75 screen_height = gdk_screen_get_height(screen);
77 if (monitor_adjacent)
78 g_free(monitor_adjacent);
80 monitor_width = monitor_height = G_MAXINT;
81 n_monitors = gdk_screen_get_n_monitors(screen);
82 if (monitor_geom)
83 g_free(monitor_geom);
84 monitor_geom = g_new(GdkRectangle, n_monitors ? n_monitors : 1);
86 if (n_monitors)
88 for (mon = 0; mon < n_monitors; ++mon)
90 gdk_screen_get_monitor_geometry(screen, mon,
91 &monitor_geom[mon]);
92 if (monitor_geom[mon].width < monitor_width)
93 monitor_width = monitor_geom[mon].width;
94 if (monitor_geom[mon].height < monitor_height)
95 monitor_height = monitor_geom[mon].height;
97 monitor_adjacent = g_new(MonitorAdjacent, n_monitors);
98 for (mon = 0; mon < n_monitors; ++mon)
100 gui_get_monitor_adjacent(mon, &monitor_adjacent[mon]);
103 else
105 n_monitors = 1;
106 monitor_geom[0].x = monitor_geom[0].y = 0;
107 monitor_width = monitor_geom[0].width = screen_width;
108 monitor_height = monitor_geom[0].height = screen_height;
109 monitor_adjacent = g_new0(MonitorAdjacent, 1);
114 void gui_support_init()
116 gpointer klass;
118 xa_cardinal = gdk_atom_intern("CARDINAL", FALSE);
120 gui_store_screen_geometry(gdk_screen_get_default());
122 /* Work around the scrollbar placement bug */
123 klass = g_type_class_ref(gtk_scrolled_window_get_type());
124 ((GtkScrolledWindowClass *) klass)->scrollbar_spacing = 0;
125 /* (don't unref, ever) */
128 /* Open a modal dialog box showing a message.
129 * The user can choose from a selection of buttons at the bottom.
130 * Returns -1 if the window is destroyed, or the number of the button
131 * if one is clicked (starting from zero).
133 * If a dialog is already open, returns -1 without waiting AND
134 * brings the current dialog to the front.
136 * Each button has two arguments, a GTK_STOCK icon and some text. If the
137 * text is NULL, the stock's text is used.
139 int get_choice(const char *title,
140 const char *message,
141 int number_of_buttons, ...)
143 GtkWidget *dialog;
144 GtkWidget *button = NULL;
145 int i, retval;
146 va_list ap;
148 if (current_dialog)
150 gtk_widget_hide(current_dialog);
151 gtk_widget_show(current_dialog);
152 return -1;
155 current_dialog = dialog = gtk_message_dialog_new(NULL,
156 GTK_DIALOG_MODAL,
157 GTK_MESSAGE_QUESTION,
158 GTK_BUTTONS_NONE,
159 "%s", message);
160 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
162 va_start(ap, number_of_buttons);
164 for (i = 0; i < number_of_buttons; i++)
166 const char *stock = va_arg(ap, char *);
167 const char *text = va_arg(ap, char *);
169 if (text)
170 button = button_new_mixed(stock, text);
171 else
172 button = gtk_button_new_from_stock(stock);
174 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
175 gtk_widget_show(button);
177 gtk_dialog_add_action_widget(GTK_DIALOG(current_dialog),
178 button, i);
181 gtk_window_set_title(GTK_WINDOW(dialog), title);
182 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
184 gtk_dialog_set_default_response(GTK_DIALOG(dialog), i - 1);
186 va_end(ap);
188 retval = gtk_dialog_run(GTK_DIALOG(dialog));
189 if (retval == GTK_RESPONSE_NONE)
190 retval = -1;
191 gtk_widget_destroy(dialog);
193 current_dialog = NULL;
195 return retval;
198 void info_message(const char *message, ...)
200 va_list args;
202 va_start(args, message);
204 run_error_info_dialog(GTK_MESSAGE_INFO, message, args);
207 /* Display a message in a window with "ROX-Filer" as title */
208 void report_error(const char *message, ...)
210 va_list args;
212 va_start(args, message);
214 run_error_info_dialog(GTK_MESSAGE_ERROR, message, args);
217 void set_cardinal_property(GdkWindow *window, GdkAtom prop, gulong value)
219 gdk_property_change(window, prop, xa_cardinal, 32,
220 GDK_PROP_MODE_REPLACE, (gchar *) &value, 1);
223 /* NB: Also used for pinned icons.
224 * TODO: Set the level here too.
226 void make_panel_window(GtkWidget *widget)
228 static gboolean need_init = TRUE;
229 static GdkAtom xa_state, xa_atom, xa_hints, xa_win_hints;
230 static GdkAtom xa_NET_WM_DESKTOP;
231 GdkWindow *window = widget->window;
232 long wm_hints_values[] = {1, False, 0, 0, 0, 0, 0, 0};
233 GdkAtom wm_protocols[2];
235 g_return_if_fail(window != NULL);
237 if (o_override_redirect.int_value)
239 gdk_window_set_override_redirect(window, TRUE);
240 return;
243 if (need_init)
245 xa_win_hints = gdk_atom_intern("_WIN_HINTS", FALSE);
246 xa_state = gdk_atom_intern("_WIN_STATE", FALSE);
247 xa_atom = gdk_atom_intern("ATOM", FALSE);
248 xa_hints = gdk_atom_intern("WM_HINTS", FALSE);
249 xa_NET_WM_DESKTOP = gdk_atom_intern("_NET_WM_DESKTOP", FALSE);
251 need_init = FALSE;
254 gdk_window_set_decorations(window, 0);
255 gdk_window_set_functions(window, 0);
256 gtk_window_set_resizable(GTK_WINDOW(widget), FALSE);
258 /* Don't hide panel/pinboard windows initially (WIN_STATE_HIDDEN).
259 * Needed for IceWM - Christopher Arndt <chris.arndt@web.de>
261 set_cardinal_property(window, xa_state,
262 WIN_STATE_STICKY |
263 WIN_STATE_FIXED_POSITION | WIN_STATE_ARRANGE_IGNORE);
265 set_cardinal_property(window, xa_win_hints,
266 WIN_HINTS_SKIP_FOCUS | WIN_HINTS_SKIP_WINLIST |
267 WIN_HINTS_SKIP_TASKBAR);
269 /* Appear on all workspaces */
270 set_cardinal_property(window, xa_NET_WM_DESKTOP, 0xffffffff);
272 gdk_property_change(window, xa_hints, xa_hints, 32,
273 GDK_PROP_MODE_REPLACE, (guchar *) wm_hints_values,
274 sizeof(wm_hints_values) / sizeof(long));
276 wm_protocols[0] = gdk_atom_intern("WM_DELETE_WINDOW", FALSE);
277 wm_protocols[1] = gdk_atom_intern("_NET_WM_PING", FALSE);
278 gdk_property_change(window,
279 gdk_atom_intern("WM_PROTOCOLS", FALSE), xa_atom, 32,
280 GDK_PROP_MODE_REPLACE, (guchar *) wm_protocols,
281 sizeof(wm_protocols) / sizeof(GdkAtom));
283 gdk_window_set_skip_taskbar_hint(window, TRUE);
284 gdk_window_set_skip_pager_hint(window, TRUE);
286 if (g_object_class_find_property(G_OBJECT_GET_CLASS(widget),
287 "accept_focus"))
289 GValue vfalse = { 0, };
290 g_value_init(&vfalse, G_TYPE_BOOLEAN);
291 g_value_set_boolean(&vfalse, FALSE);
292 g_object_set_property(G_OBJECT(widget),
293 "accept_focus", &vfalse);
294 g_value_unset(&vfalse);
298 static gboolean error_idle_cb(gpointer data)
300 char **error = (char **) data;
302 report_error("%s", *error);
303 null_g_free(error);
305 one_less_window();
306 return FALSE;
309 /* Display an error with "ROX-Filer" as title next time we are idle.
310 * If multiple errors are reported this way before the window is opened,
311 * all are displayed in a single window.
312 * If an error is reported while the error window is open, it is discarded.
314 void delayed_error(const char *error, ...)
316 static char *delayed_error_data = NULL;
317 char *old, *new;
318 va_list args;
320 g_return_if_fail(error != NULL);
322 old = delayed_error_data;
324 va_start(args, error);
325 new = g_strdup_vprintf(error, args);
326 va_end(args);
328 if (old)
330 delayed_error_data = g_strconcat(old,
331 _("\n---\n"),
332 new, NULL);
333 g_free(old);
334 g_free(new);
336 else
338 delayed_error_data = new;
339 g_idle_add(error_idle_cb, &delayed_error_data);
341 number_of_windows++;
345 /* Load the file into memory. Return TRUE on success.
346 * Block is zero terminated (but this is not included in the length).
348 gboolean load_file(const char *pathname, char **data_out, long *length_out)
350 gsize len;
351 GError *error = NULL;
353 if (!g_file_get_contents(pathname, data_out, &len, &error))
355 delayed_error("%s", error->message);
356 g_error_free(error);
357 return FALSE;
360 if (length_out)
361 *length_out = len;
362 return TRUE;
365 GtkWidget *new_help_button(HelpFunc show_help, gpointer data)
367 GtkWidget *b, *icon;
369 b = gtk_button_new();
370 gtk_button_set_relief(GTK_BUTTON(b), GTK_RELIEF_NONE);
371 icon = gtk_image_new_from_stock(GTK_STOCK_HELP,
372 GTK_ICON_SIZE_SMALL_TOOLBAR);
373 gtk_container_add(GTK_CONTAINER(b), icon);
374 g_signal_connect_swapped(b, "clicked", G_CALLBACK(show_help), data);
376 GTK_WIDGET_UNSET_FLAGS(b, GTK_CAN_FOCUS);
378 return b;
381 /* Read file into memory. Call parse_line(guchar *line) for each line
382 * in the file. Callback returns NULL on success, or an error message
383 * if something went wrong. Only the first error is displayed to the user.
385 void parse_file(const char *path, ParseFunc *parse_line)
387 char *data;
388 long length;
389 gboolean seen_error = FALSE;
391 if (load_file(path, &data, &length))
393 char *eol;
394 const char *error;
395 char *line = data;
396 int line_number = 1;
398 if (strncmp(data, "<?xml ", 6) == 0)
400 delayed_error(_("Attempt to read an XML file as "
401 "a text file. File '%s' may be "
402 "corrupted."), path);
403 return;
406 while (line && *line)
408 eol = strchr(line, '\n');
409 if (eol)
410 *eol = '\0';
412 error = parse_line(line);
414 if (error && !seen_error)
416 delayed_error(
417 _("Error in '%s' file at line %d: "
418 "\n\"%s\"\n"
419 "This may be due to upgrading from a previous version of "
420 "ROX-Filer. Open the Options window and try changing something "
421 "and then changing it back (causing the file to be resaved).\n"
422 "Further errors will be ignored."),
423 path,
424 line_number,
425 error);
426 seen_error = TRUE;
429 if (!eol)
430 break;
431 line = eol + 1;
432 line_number++;
434 g_free(data);
438 /* Returns the position of the pointer.
439 * TRUE if any modifier keys or mouse buttons are pressed.
441 gboolean get_pointer_xy(int *x, int *y)
443 unsigned int mask;
445 gdk_window_get_pointer(NULL, x, y, &mask);
447 return mask != 0;
450 int get_monitor_under_pointer(void)
452 int x, y;
454 get_pointer_xy(&x, &y);
455 return gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
458 #define DECOR_BORDER 32
460 /* Centre the window at these coords */
461 void centre_window(GdkWindow *window, int x, int y)
463 int w, h;
464 int m;
466 g_return_if_fail(window != NULL);
468 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
470 gdk_drawable_get_size(window, &w, &h);
472 x -= w / 2;
473 y -= h / 2;
475 gdk_window_move(window,
476 CLAMP(x, DECOR_BORDER + monitor_geom[m].x,
477 monitor_geom[m].x + monitor_geom[m].width
478 - w - DECOR_BORDER),
479 CLAMP(y, DECOR_BORDER + monitor_geom[m].y,
480 monitor_geom[m].y + monitor_geom[m].height
481 - h - DECOR_BORDER));
484 static void run_error_info_dialog(GtkMessageType type, const char *message,
485 va_list args)
487 GtkWidget *dialog;
488 gchar *s;
490 g_return_if_fail(message != NULL);
492 s = g_strdup_vprintf(message, args);
493 va_end(args);
495 dialog = gtk_message_dialog_new(NULL,
496 GTK_DIALOG_MODAL,
497 type,
498 GTK_BUTTONS_OK,
499 "%s", s);
500 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
501 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
502 gtk_dialog_run(GTK_DIALOG(dialog));
503 gtk_widget_destroy(dialog);
505 g_free(s);
508 static GtkWidget *current_wink_widget = NULL;
509 static gint wink_timeout = -1; /* Called when it's time to stop */
510 static gulong wink_destroy; /* Called if the widget dies first */
512 static gboolean end_wink(gpointer data)
514 gtk_drag_unhighlight(current_wink_widget);
516 g_signal_handler_disconnect(current_wink_widget, wink_destroy);
518 current_wink_widget = NULL;
520 return FALSE;
523 static void cancel_wink(void)
525 g_source_remove(wink_timeout);
526 end_wink(NULL);
529 static void wink_widget_died(gpointer data)
531 current_wink_widget = NULL;
532 g_source_remove(wink_timeout);
535 /* Draw a black box around this widget, briefly.
536 * Note: uses the drag highlighting code for now.
538 void wink_widget(GtkWidget *widget)
540 g_return_if_fail(widget != NULL);
542 if (current_wink_widget)
543 cancel_wink();
545 current_wink_widget = widget;
546 gtk_drag_highlight(current_wink_widget);
548 wink_timeout = g_timeout_add(300, (GSourceFunc) end_wink, NULL);
550 wink_destroy = g_signal_connect_swapped(widget, "destroy",
551 G_CALLBACK(wink_widget_died), NULL);
554 static gboolean idle_destroy_cb(GtkWidget *widget)
556 gtk_widget_unref(widget);
557 gtk_widget_destroy(widget);
558 return FALSE;
561 /* Destroy the widget in an idle callback */
562 void destroy_on_idle(GtkWidget *widget)
564 gtk_widget_ref(widget);
565 g_idle_add((GSourceFunc) idle_destroy_cb, widget);
568 /* Spawn a child process (as spawn_full), and report errors.
569 * Returns the child's PID on succes, or 0 on failure.
571 gint rox_spawn(const gchar *dir, const gchar **argv)
573 GError *error = NULL;
574 gint pid = 0;
576 if (!g_spawn_async_with_pipes(dir, (gchar **) argv, NULL,
577 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL |
578 G_SPAWN_SEARCH_PATH,
579 NULL, NULL, /* Child setup fn */
580 &pid, /* Child PID */
581 NULL, NULL, NULL, /* Standard pipes */
582 &error))
584 delayed_error("%s", error ? error->message : "(null)");
585 g_error_free(error);
587 return 0;
590 return pid;
593 GtkWidget *button_new_image_text(GtkWidget *image, const char *message)
595 GtkWidget *button, *align, *hbox, *label;
597 button = gtk_button_new();
598 label = gtk_label_new_with_mnemonic(message);
599 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
601 hbox = gtk_hbox_new(FALSE, 2);
603 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
605 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
606 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
608 gtk_container_add(GTK_CONTAINER(button), align);
609 gtk_container_add(GTK_CONTAINER(align), hbox);
610 gtk_widget_show_all(align);
612 return button;
615 GtkWidget *button_new_mixed(const char *stock, const char *message)
617 return button_new_image_text(gtk_image_new_from_stock(stock,
618 GTK_ICON_SIZE_BUTTON),
619 message);
622 /* Highlight entry in red if 'error' is TRUE */
623 void entry_set_error(GtkWidget *entry, gboolean error)
625 const GdkColor red = {0, 0xffff, 0, 0};
626 const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
628 gtk_widget_modify_text(entry, GTK_STATE_NORMAL, error ? &red : NULL);
629 gtk_widget_modify_base(entry, GTK_STATE_NORMAL, error ? &white : NULL);
632 /* Change stacking position of higher to be just above lower.
633 * If lower is NULL, put higher at the bottom of the stack.
635 void window_put_just_above(GdkWindow *higher, GdkWindow *lower)
637 if (o_override_redirect.int_value && lower)
639 XWindowChanges restack;
641 gdk_error_trap_push();
643 restack.stack_mode = Above;
645 restack.sibling = GDK_WINDOW_XWINDOW(lower);
647 XConfigureWindow(gdk_display, GDK_WINDOW_XWINDOW(higher),
648 CWSibling | CWStackMode, &restack);
650 gdk_flush();
651 if (gdk_error_trap_pop())
652 g_warning("window_put_just_above()\n");
654 else
655 gdk_window_lower(higher); /* To bottom of stack */
658 /* Copied from Gtk */
659 static GtkFixedChild* fixed_get_child(GtkFixed *fixed, GtkWidget *widget)
661 GList *children;
663 children = fixed->children;
664 while (children)
666 GtkFixedChild *child;
668 child = children->data;
669 children = children->next;
671 if (child->widget == widget)
672 return child;
675 return NULL;
678 /* Like gtk_fixed_move(), except not insanely slow */
679 void fixed_move_fast(GtkFixed *fixed, GtkWidget *widget, int x, int y)
681 GtkFixedChild *child;
683 child = fixed_get_child(fixed, widget);
685 g_assert(child);
687 gtk_widget_freeze_child_notify(widget);
689 child->x = x;
690 gtk_widget_child_notify(widget, "x");
692 child->y = y;
693 gtk_widget_child_notify(widget, "y");
695 gtk_widget_thaw_child_notify(widget);
697 if (GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_VISIBLE(fixed))
699 int border_width = GTK_CONTAINER(fixed)->border_width;
700 GtkAllocation child_allocation;
701 GtkRequisition child_requisition;
703 gtk_widget_get_child_requisition(child->widget,
704 &child_requisition);
705 child_allocation.x = child->x + border_width;
706 child_allocation.y = child->y + border_width;
708 child_allocation.x += GTK_WIDGET(fixed)->allocation.x;
709 child_allocation.y += GTK_WIDGET(fixed)->allocation.y;
711 child_allocation.width = child_requisition.width;
712 child_allocation.height = child_requisition.height;
713 gtk_widget_size_allocate(child->widget, &child_allocation);
717 /* Draw the black border */
718 static gint tooltip_draw(GtkWidget *w)
720 gdk_draw_rectangle(w->window, w->style->fg_gc[w->state], FALSE, 0, 0,
721 w->allocation.width - 1, w->allocation.height - 1);
723 return FALSE;
726 /* When the tips window closed, record the time. If we try to open another
727 * tip soon, it will appear more quickly.
729 static void tooltip_destroyed(gpointer data)
731 time(&tip_time);
734 /* Display a tooltip-like widget near the pointer with 'text'. If 'text' is
735 * NULL, close any current tooltip.
737 void tooltip_show(guchar *text)
739 GtkWidget *label;
740 int x, y, py;
741 int w, h;
742 int m;
744 if (tip_timeout)
746 g_source_remove(tip_timeout);
747 tip_timeout = 0;
750 if (tip_widget)
752 gtk_widget_destroy(tip_widget);
753 tip_widget = NULL;
756 if (!text)
757 return;
759 /* Show the tip */
760 tip_widget = gtk_window_new(GTK_WINDOW_POPUP);
761 gtk_widget_set_app_paintable(tip_widget, TRUE);
762 gtk_widget_set_name(tip_widget, "gtk-tooltips");
764 g_signal_connect_swapped(tip_widget, "expose_event",
765 G_CALLBACK(tooltip_draw), tip_widget);
767 label = gtk_label_new(text);
768 gtk_misc_set_padding(GTK_MISC(label), 4, 2);
769 gtk_container_add(GTK_CONTAINER(tip_widget), label);
770 gtk_widget_show(label);
771 gtk_widget_realize(tip_widget);
773 w = tip_widget->allocation.width;
774 h = tip_widget->allocation.height;
775 gdk_window_get_pointer(NULL, &x, &py, NULL);
777 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, py);
779 x -= w / 2;
780 y = py + 12; /* I don't know the pointer height so I use a constant */
782 /* Now check for screen boundaries */
783 x = CLAMP(x, monitor_geom[m].x,
784 monitor_geom[m].x + monitor_geom[m].width - w);
785 y = CLAMP(y, monitor_geom[m].y,
786 monitor_geom[m].y + monitor_geom[m].height - h);
788 /* And again test if pointer is over the tooltip window */
789 if (py >= y && py <= y + h)
790 y = py - h - 2;
791 gtk_window_move(GTK_WINDOW(tip_widget), x, y);
792 gtk_widget_show(tip_widget);
794 g_signal_connect_swapped(tip_widget, "destroy",
795 G_CALLBACK(tooltip_destroyed), NULL);
796 time(&tip_time);
799 /* Call callback(user_data) after a while, unless cancelled.
800 * Object is refd now and unref when cancelled / after callback called.
802 void tooltip_prime(GtkFunction callback, GObject *object)
804 time_t now;
805 int delay;
807 g_return_if_fail(tip_timeout == 0);
809 time(&now);
810 delay = now - tip_time > 2 ? 1000 : 200;
812 g_object_ref(object);
813 tip_timeout = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,
814 delay,
815 (GSourceFunc) callback,
816 object,
817 g_object_unref);
820 /* Like gtk_widget_modify_font, but copes with font_desc == NULL */
821 void widget_modify_font(GtkWidget *widget, PangoFontDescription *font_desc)
823 GtkRcStyle *rc_style;
825 g_return_if_fail(GTK_IS_WIDGET(widget));
827 rc_style = gtk_widget_get_modifier_style(widget);
829 if (rc_style->font_desc)
830 pango_font_description_free(rc_style->font_desc);
832 rc_style->font_desc = font_desc
833 ? pango_font_description_copy(font_desc)
834 : NULL;
836 gtk_widget_modify_style(widget, rc_style);
839 /* Confirm the action with the user. If action is NULL, the text from stock
840 * is used.
842 gboolean confirm(const gchar *message, const gchar *stock, const gchar *action)
844 return get_choice(PROJECT, message, 2,
845 GTK_STOCK_CANCEL, NULL,
846 stock, action) == 1;
849 struct _Radios {
850 GList *widgets;
852 void (*changed)(Radios *, gpointer data);
853 gpointer changed_data;
856 /* Create a new set of radio buttons.
857 * Use radios_add to add options, then radios_pack to put them into something.
858 * The radios object will self-destruct with the first widget it contains.
859 * changed(data) is called (if not NULL) when pack is called, and on any
860 * change after that.
862 Radios *radios_new(void (*changed)(Radios *, gpointer data), gpointer data)
864 Radios *radios;
866 radios = g_new(Radios, 1);
868 radios->widgets = NULL;
869 radios->changed = changed;
870 radios->changed_data = data;
872 return radios;
875 static void radios_free(GtkWidget *radio, Radios *radios)
877 g_return_if_fail(radios != NULL);
879 g_list_free(radios->widgets);
880 g_free(radios);
883 void radios_add(Radios *radios, const gchar *tip, gint value,
884 const gchar *label, ...)
886 GtkWidget *radio;
887 GSList *group = NULL;
888 gchar *s;
889 va_list args;
891 g_return_if_fail(radios != NULL);
892 g_return_if_fail(label != NULL);
894 va_start(args, label);
895 s = g_strdup_vprintf(label, args);
896 va_end(args);
898 if (radios->widgets)
900 GtkRadioButton *first = GTK_RADIO_BUTTON(radios->widgets->data);
901 group = gtk_radio_button_get_group(first);
904 radio = gtk_radio_button_new_with_label(group, s);
905 gtk_label_set_line_wrap(GTK_LABEL(GTK_BIN(radio)->child), TRUE);
906 gtk_widget_show(radio);
907 if (tip)
908 gtk_tooltips_set_tip(tooltips, radio, tip, NULL);
909 if (!group)
910 g_signal_connect(G_OBJECT(radio), "destroy",
911 G_CALLBACK(radios_free), radios);
913 radios->widgets = g_list_prepend(radios->widgets, radio);
914 g_object_set_data(G_OBJECT(radio), "rox-radios-value",
915 GINT_TO_POINTER(value));
918 static void radio_toggled(GtkToggleButton *button, Radios *radios)
920 g_return_if_fail(radios != NULL);
922 if (button && !gtk_toggle_button_get_active(button))
923 return; /* Stop double-notifies */
925 if (radios->changed)
926 radios->changed(radios, radios->changed_data);
929 void radios_pack(Radios *radios, GtkBox *box)
931 GList *next;
933 g_return_if_fail(radios != NULL);
935 for (next = g_list_last(radios->widgets); next; next = next->prev)
937 GtkWidget *button = GTK_WIDGET(next->data);
939 gtk_box_pack_start(box, button, FALSE, TRUE, 0);
940 g_signal_connect(button, "toggled",
941 G_CALLBACK(radio_toggled), radios);
943 radio_toggled(NULL, radios);
946 void radios_set_value(Radios *radios, gint value)
948 GList *next;
950 g_return_if_fail(radios != NULL);
952 for (next = radios->widgets; next; next = next->next)
954 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
955 int radio_value;
957 radio_value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(radio),
958 "rox-radios-value"));
960 if (radio_value == value)
962 gtk_toggle_button_set_active(radio, TRUE);
963 return;
967 g_warning("Value %d not in radio group!", value);
970 gint radios_get_value(Radios *radios)
972 GList *next;
974 g_return_val_if_fail(radios != NULL, -1);
976 for (next = radios->widgets; next; next = next->next)
978 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
980 if (gtk_toggle_button_get_active(radio))
981 return GPOINTER_TO_INT(g_object_get_data(
982 G_OBJECT(radio), "rox-radios-value"));
985 g_warning("Nothing in the radio group is selected!");
987 return -1;
990 /* Convert a list of URIs as a string into a GList of EscapedPath URIs.
991 * No unescaping is done.
992 * Lines beginning with # are skipped.
993 * The text block passed in is zero terminated (after the final CRLF)
995 GList *uri_list_to_glist(const char *uri_list)
997 GList *list = NULL;
999 while (*uri_list)
1001 char *linebreak;
1002 int length;
1004 linebreak = strchr(uri_list, 13);
1006 if (!linebreak || linebreak[1] != 10)
1008 delayed_error("uri_list_to_glist: %s",
1009 _("Incorrect or missing line "
1010 "break in text/uri-list data"));
1011 return list;
1014 length = linebreak - uri_list;
1016 if (length && uri_list[0] != '#')
1017 list = g_list_append(list, g_strndup(uri_list, length));
1019 uri_list = linebreak + 2;
1022 return list;
1025 typedef struct _SimpleImageClass SimpleImageClass;
1026 typedef struct _SimpleImage SimpleImage;
1028 struct _SimpleImageClass {
1029 GtkWidgetClass parent;
1032 struct _SimpleImage {
1033 GtkWidget widget;
1035 GdkPixbuf *pixbuf;
1036 int width, height;
1039 #define SIMPLE_IMAGE(obj) (GTK_CHECK_CAST((obj), \
1040 simple_image_get_type(), SimpleImage))
1042 static void simple_image_finialize(GObject *object)
1044 SimpleImage *image = SIMPLE_IMAGE(object);
1046 g_object_unref(G_OBJECT(image->pixbuf));
1047 image->pixbuf = NULL;
1050 static void simple_image_size_request(GtkWidget *widget,
1051 GtkRequisition *requisition)
1053 SimpleImage *image = (SimpleImage *) widget;
1055 requisition->width = image->width;
1056 requisition->height = image->height;
1059 /* Render a pixbuf without messing up the clipping */
1060 void render_pixbuf(GdkPixbuf *pixbuf, GdkDrawable *target, GdkGC *gc,
1061 int x, int y, int width, int height)
1063 gdk_draw_pixbuf(target, gc, pixbuf, 0, 0, x, y, width, height,
1064 GDK_RGB_DITHER_NORMAL, 0, 0);
1068 static gint simple_image_expose(GtkWidget *widget, GdkEventExpose *event)
1070 SimpleImage *image = (SimpleImage *) widget;
1071 int x;
1073 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1075 x = widget->allocation.x +
1076 (widget->allocation.width - image->width) / 2;
1078 render_pixbuf(image->pixbuf, widget->window, widget->style->black_gc,
1079 x, widget->allocation.y,
1080 image->width, image->height);
1082 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1083 return FALSE;
1086 static void simple_image_class_init(gpointer gclass, gpointer data)
1088 GObjectClass *object = (GObjectClass *) gclass;
1089 GtkWidgetClass *widget = (GtkWidgetClass *) gclass;
1091 object->finalize = simple_image_finialize;
1092 widget->size_request = simple_image_size_request;
1093 widget->expose_event = simple_image_expose;
1096 static void simple_image_init(GTypeInstance *object, gpointer gclass)
1098 GTK_WIDGET_SET_FLAGS(object, GTK_NO_WINDOW);
1101 static GType simple_image_get_type(void)
1103 static GType type = 0;
1105 if (!type)
1107 static const GTypeInfo info =
1109 sizeof (SimpleImageClass),
1110 NULL, /* base_init */
1111 NULL, /* base_finalise */
1112 simple_image_class_init,
1113 NULL, /* class_finalise */
1114 NULL, /* class_data */
1115 sizeof(SimpleImage),
1116 0, /* n_preallocs */
1117 simple_image_init,
1120 type = g_type_register_static(gtk_widget_get_type(),
1121 "SimpleImage", &info, 0);
1124 return type;
1127 GtkWidget *simple_image_new(GdkPixbuf *pixbuf)
1129 SimpleImage *image;
1131 g_return_val_if_fail(pixbuf != NULL, NULL);
1133 image = g_object_new(simple_image_get_type(), NULL);
1135 image->pixbuf = pixbuf;
1136 g_object_ref(G_OBJECT(pixbuf));
1138 image->width = gdk_pixbuf_get_width(pixbuf);
1139 image->height = gdk_pixbuf_get_height(pixbuf);
1141 return GTK_WIDGET(image);
1144 /* Whether a line l1 long starting from n1 overlaps a line l2 from n2 */
1145 inline static gboolean gui_ranges_overlap(int n1, int l1, int n2, int l2)
1147 return (n1 > n2 && n1 < n2 + l2) ||
1148 (n1 + l1 > n2 && n1 + l1 < n2 + l2) ||
1149 (n1 <= n2 && n1 + l1 >= n2 + l2);
1152 static void gui_get_monitor_adjacent(int monitor, MonitorAdjacent *adj)
1154 int m;
1156 adj->left = adj->right = adj->top = adj->bottom = FALSE;
1158 for (m = 0; m < n_monitors; ++m)
1160 if (m == monitor)
1161 continue;
1162 if (gui_ranges_overlap(monitor_geom[m].y,
1163 monitor_geom[m].height,
1164 monitor_geom[monitor].y,
1165 monitor_geom[monitor].height))
1167 if (monitor_geom[m].x < monitor_geom[monitor].x)
1169 adj->left = TRUE;
1171 else if (monitor_geom[m].x > monitor_geom[monitor].x)
1173 adj->right = TRUE;
1176 if (gui_ranges_overlap(monitor_geom[m].x,
1177 monitor_geom[m].width,
1178 monitor_geom[monitor].x,
1179 monitor_geom[monitor].width))
1181 if (monitor_geom[m].y < monitor_geom[monitor].y)
1183 adj->top = TRUE;
1185 else if (monitor_geom[m].y > monitor_geom[monitor].y)
1187 adj->bottom = TRUE;
1193 static void rox_wmspec_change_state(gboolean add, GdkWindow *window,
1194 GdkAtom state1, GdkAtom state2)
1196 GdkDisplay *display = gdk_drawable_get_display(GDK_DRAWABLE(window));
1197 XEvent xev;
1199 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1200 #define _NET_WM_STATE_ADD 1 /* add/set property */
1201 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1203 xev.xclient.type = ClientMessage;
1204 xev.xclient.serial = 0;
1205 xev.xclient.send_event = True;
1206 xev.xclient.window = GDK_WINDOW_XID(window);
1207 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display(
1208 display, "_NET_WM_STATE");
1209 xev.xclient.format = 32;
1210 xev.xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1211 xev.xclient.data.l[1] = gdk_x11_atom_to_xatom_for_display(display,
1212 state1);
1213 xev.xclient.data.l[2] = gdk_x11_atom_to_xatom_for_display(display,
1214 state2);
1215 xev.xclient.data.l[3] = 0;
1216 xev.xclient.data.l[4] = 0;
1218 XSendEvent(GDK_DISPLAY_XDISPLAY(display),
1219 GDK_WINDOW_XID(
1220 gdk_screen_get_root_window(
1221 gdk_drawable_get_screen(GDK_DRAWABLE(window)))),
1222 False,
1223 SubstructureRedirectMask | SubstructureNotifyMask,
1224 &xev);
1227 void keep_below(GdkWindow *window, gboolean setting)
1229 g_return_if_fail(GDK_IS_WINDOW(window));
1231 if (GDK_WINDOW_DESTROYED(window))
1232 return;
1234 if (gdk_window_is_visible(window))
1236 if (setting)
1238 rox_wmspec_change_state(FALSE, window,
1239 gdk_atom_intern("_NET_WM_STATE_ABOVE", FALSE),
1240 GDK_NONE);
1242 rox_wmspec_change_state(setting, window,
1243 gdk_atom_intern("_NET_WM_STATE_BELOW", FALSE),
1244 GDK_NONE);
1246 #if 0
1247 else
1249 #if GTK_CHECK_VERSION(2,4,0)
1250 gdk_synthesize_window_state(window,
1251 setting ? GDK_WINDOW_STATE_ABOVE :
1252 GDK_WINDOW_STATE_BELOW,
1253 setting ? GDK_WINDOW_STATE_BELOW : 0);
1254 #endif
1256 #endif
1259 static void
1260 size_prepared_cb (GdkPixbufLoader *loader,
1261 int width,
1262 int height,
1263 gpointer data)
1265 struct {
1266 gint width;
1267 gint height;
1268 gboolean preserve_aspect_ratio;
1269 } *info = data;
1271 g_return_if_fail (width > 0 && height > 0);
1273 if(info->preserve_aspect_ratio) {
1274 if ((double)height * (double)info->width >
1275 (double)width * (double)info->height) {
1276 width = 0.5 + (double)width * (double)info->height / (double)height;
1277 height = info->height;
1278 } else {
1279 height = 0.5 + (double)height * (double)info->width / (double)width;
1280 width = info->width;
1282 } else {
1283 width = info->width;
1284 height = info->height;
1287 gdk_pixbuf_loader_set_size (loader, width, height);
1291 * rox_pixbuf_new_from_file_at_scale:
1292 * @filename: Name of file to load.
1293 * @width: The width the image should have
1294 * @height: The height the image should have
1295 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1296 * @error: Return location for an error
1298 * Creates a new pixbuf by loading an image from a file. The file format is
1299 * detected automatically. If %NULL is returned, then @error will be set.
1300 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1301 * The image will be scaled to fit in the requested size, optionally preserving
1302 * the image's aspect ratio.
1304 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1305 * if any of several error conditions occurred: the file could not be opened,
1306 * there was no loader for the file's format, there was not enough memory to
1307 * allocate the image buffer, or the image file contained invalid data.
1309 * Taken from GTK 2.6.
1311 GdkPixbuf *
1312 rox_pixbuf_new_from_file_at_scale (const char *filename,
1313 int width,
1314 int height,
1315 gboolean preserve_aspect_ratio,
1316 GError **error)
1319 GdkPixbufLoader *loader;
1320 GdkPixbuf *pixbuf;
1322 guchar buffer [4096];
1323 int length;
1324 FILE *f;
1325 struct {
1326 gint width;
1327 gint height;
1328 gboolean preserve_aspect_ratio;
1329 } info;
1331 g_return_val_if_fail (filename != NULL, NULL);
1332 g_return_val_if_fail (width > 0 && height > 0, NULL);
1334 f = fopen (filename, "rb");
1335 if (!f) {
1336 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1337 NULL, NULL, NULL);
1338 g_set_error (error,
1339 G_FILE_ERROR,
1340 g_file_error_from_errno (errno),
1341 _("Failed to open file '%s': %s"),
1342 utf8_filename ? utf8_filename : "???",
1343 g_strerror (errno));
1344 g_free (utf8_filename);
1345 return NULL;
1348 loader = gdk_pixbuf_loader_new ();
1350 info.width = width;
1351 info.height = height;
1352 info.preserve_aspect_ratio = preserve_aspect_ratio;
1354 g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), &info);
1356 while (!feof (f) && !ferror (f)) {
1357 length = fread (buffer, 1, sizeof (buffer), f);
1358 if (length > 0)
1359 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1360 gdk_pixbuf_loader_close (loader, NULL);
1361 fclose (f);
1362 g_object_unref (loader);
1363 return NULL;
1367 fclose (f);
1369 if (!gdk_pixbuf_loader_close (loader, error)) {
1370 g_object_unref (loader);
1371 return NULL;
1374 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1376 if (!pixbuf) {
1377 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1378 NULL, NULL, NULL);
1380 g_object_unref (loader);
1382 g_set_error (error,
1383 GDK_PIXBUF_ERROR,
1384 GDK_PIXBUF_ERROR_FAILED,
1385 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1386 utf8_filename ? utf8_filename : "???");
1387 g_free (utf8_filename);
1388 return NULL;
1391 g_object_ref (pixbuf);
1393 g_object_unref (loader);
1395 return pixbuf;
1398 /* Make the name bolder and larger.
1399 * scale_factor can be PANGO_SCALE_X_LARGE, etc.
1401 void make_heading(GtkWidget *label, double scale_factor)
1403 PangoAttribute *attr;
1404 PangoAttrList *list;
1406 list = pango_attr_list_new();
1408 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
1409 attr->start_index = 0;
1410 attr->end_index = -1;
1411 pango_attr_list_insert(list, attr);
1413 attr = pango_attr_scale_new(scale_factor);
1414 attr->start_index = 0;
1415 attr->end_index = -1;
1416 pango_attr_list_insert(list, attr);
1418 gtk_label_set_attributes(GTK_LABEL(label), list);
1421 /* Launch a program using 0launch.
1422 * If button-3 is used, open the GUI with -g.
1424 void launch_uri(const char *uri)
1426 const char *argv[] = {"0launch", NULL, NULL, NULL};
1427 const char *uri_0launch = "/uri/0install/zero-install.sourceforge.net"
1428 "/bin/0launch";
1430 if (!available_in_path(argv[0]))
1432 if (access(uri_0launch, X_OK) == 0)
1433 argv[0] = uri_0launch;
1434 else
1436 delayed_error(_("This program cannot be run, as the "
1437 "0launch command is not available. "
1438 "It can be downloaded from here:\n\n"
1439 "http://0install.net/injector.html"));
1440 return;
1444 if (current_event_button() == 3)
1446 argv[1] = "-g";
1447 argv[2] = uri;
1449 else
1450 argv[1] = uri;
1452 rox_spawn(NULL, argv);
1455 static gint button3_button_pressed(GtkButton *button,
1456 GdkEventButton *event,
1457 gpointer date)
1459 if (event->button == 3)
1461 gtk_grab_add(GTK_WIDGET(button));
1462 gtk_button_pressed(button);
1464 return TRUE;
1467 return FALSE;
1470 static gint button3_button_released(GtkButton *button,
1471 GdkEventButton *event,
1472 FilerWindow *filer_window)
1474 if (event->button == 3)
1476 gtk_grab_remove(GTK_WIDGET(button));
1477 gtk_button_released(button);
1479 return TRUE;
1482 return FALSE;
1485 void allow_right_click(GtkWidget *button)
1487 g_signal_connect(button, "button_press_event",
1488 G_CALLBACK(button3_button_pressed), NULL);
1489 g_signal_connect(button, "button_release_event",
1490 G_CALLBACK(button3_button_released), NULL);
1493 /* Return mouse button used in the current event, or -1 if none (no event,
1494 * or not a click).
1496 gint current_event_button(void)
1498 GdkEventButton *bev;
1499 gint button = -1;
1501 bev = (GdkEventButton *) gtk_get_current_event();
1503 if (bev &&
1504 (bev->type == GDK_BUTTON_PRESS || bev->type == GDK_BUTTON_RELEASE))
1505 button = bev->button;
1507 gdk_event_free((GdkEvent *) bev);
1509 return button;
1512 /* Create a new pixbuf by colourizing 'src' to 'color'. If the function fails,
1513 * 'src' will be returned (with an increased reference count, so it is safe to
1514 * g_object_unref() the return value whether the function fails or not).
1516 GdkPixbuf *create_spotlight_pixbuf(GdkPixbuf *src, GdkColor *color)
1518 guchar opacity = 192;
1519 guchar alpha = 255 - opacity;
1520 GdkPixbuf *dst;
1521 GdkColorspace colorspace;
1522 int width, height, src_rowstride, dst_rowstride, x, y;
1523 int n_channels, bps;
1524 int r, g, b;
1525 guchar *spixels, *dpixels, *src_pixels, *dst_pixels;
1526 gboolean has_alpha;
1528 has_alpha = gdk_pixbuf_get_has_alpha(src);
1529 colorspace = gdk_pixbuf_get_colorspace(src);
1530 n_channels = gdk_pixbuf_get_n_channels(src);
1531 bps = gdk_pixbuf_get_bits_per_sample(src);
1533 if ((colorspace != GDK_COLORSPACE_RGB) ||
1534 (!has_alpha && n_channels != 3) ||
1535 (has_alpha && n_channels != 4) ||
1536 (bps != 8))
1537 goto error;
1539 width = gdk_pixbuf_get_width(src);
1540 height = gdk_pixbuf_get_height(src);
1542 dst = gdk_pixbuf_new(colorspace, has_alpha, bps, width, height);
1543 if (dst == NULL)
1544 goto error;
1546 src_pixels = gdk_pixbuf_get_pixels(src);
1547 dst_pixels = gdk_pixbuf_get_pixels(dst);
1548 src_rowstride = gdk_pixbuf_get_rowstride(src);
1549 dst_rowstride = gdk_pixbuf_get_rowstride(dst);
1551 r = opacity * (color->red >> 8);
1552 g = opacity * (color->green >> 8);
1553 b = opacity * (color->blue >> 8);
1555 for (y = 0; y < height; y++)
1557 spixels = src_pixels + y * src_rowstride;
1558 dpixels = dst_pixels + y * dst_rowstride;
1559 for (x = 0; x < width; x++)
1561 *dpixels++ = (*spixels++ * alpha + r) >> 8;
1562 *dpixels++ = (*spixels++ * alpha + g) >> 8;
1563 *dpixels++ = (*spixels++ * alpha + b) >> 8;
1564 if (has_alpha)
1565 *dpixels++ = *spixels++;
1569 return dst;
1571 error:
1572 g_object_ref(src);
1573 return src;