r4269: Bugfix: New detail crashes cleanice theme. Try another value... (reported
[rox-filer/translations.git] / ROX-Filer / src / gui_support.c
blobb67b8de78c510101007aa446b298de9d780a69a2
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 #define DECOR_BORDER 32
452 /* Centre the window at these coords */
453 void centre_window(GdkWindow *window, int x, int y)
455 int w, h;
456 int m;
458 g_return_if_fail(window != NULL);
460 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
462 gdk_drawable_get_size(window, &w, &h);
464 x -= w / 2;
465 y -= h / 2;
467 gdk_window_move(window,
468 CLAMP(x, DECOR_BORDER + monitor_geom[m].x,
469 monitor_geom[m].x + monitor_geom[m].width
470 - w - DECOR_BORDER),
471 CLAMP(y, DECOR_BORDER + monitor_geom[m].y,
472 monitor_geom[m].y + monitor_geom[m].height
473 - h - DECOR_BORDER));
476 static void run_error_info_dialog(GtkMessageType type, const char *message,
477 va_list args)
479 GtkWidget *dialog;
480 gchar *s;
482 g_return_if_fail(message != NULL);
484 s = g_strdup_vprintf(message, args);
485 va_end(args);
487 dialog = gtk_message_dialog_new(NULL,
488 GTK_DIALOG_MODAL,
489 type,
490 GTK_BUTTONS_OK,
491 "%s", s);
492 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
493 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
494 gtk_dialog_run(GTK_DIALOG(dialog));
495 gtk_widget_destroy(dialog);
497 g_free(s);
500 static GtkWidget *current_wink_widget = NULL;
501 static gint wink_timeout = -1; /* Called when it's time to stop */
502 static gulong wink_destroy; /* Called if the widget dies first */
504 static gboolean end_wink(gpointer data)
506 gtk_drag_unhighlight(current_wink_widget);
508 g_signal_handler_disconnect(current_wink_widget, wink_destroy);
510 current_wink_widget = NULL;
512 return FALSE;
515 static void cancel_wink(void)
517 g_source_remove(wink_timeout);
518 end_wink(NULL);
521 static void wink_widget_died(gpointer data)
523 current_wink_widget = NULL;
524 g_source_remove(wink_timeout);
527 /* Draw a black box around this widget, briefly.
528 * Note: uses the drag highlighting code for now.
530 void wink_widget(GtkWidget *widget)
532 g_return_if_fail(widget != NULL);
534 if (current_wink_widget)
535 cancel_wink();
537 current_wink_widget = widget;
538 gtk_drag_highlight(current_wink_widget);
540 wink_timeout = g_timeout_add(300, (GSourceFunc) end_wink, NULL);
542 wink_destroy = g_signal_connect_swapped(widget, "destroy",
543 G_CALLBACK(wink_widget_died), NULL);
546 static gboolean idle_destroy_cb(GtkWidget *widget)
548 gtk_widget_unref(widget);
549 gtk_widget_destroy(widget);
550 return FALSE;
553 /* Destroy the widget in an idle callback */
554 void destroy_on_idle(GtkWidget *widget)
556 gtk_widget_ref(widget);
557 g_idle_add((GSourceFunc) idle_destroy_cb, widget);
560 /* Spawn a child process (as spawn_full), and report errors.
561 * Returns the child's PID on succes, or 0 on failure.
563 gint rox_spawn(const gchar *dir, const gchar **argv)
565 GError *error = NULL;
566 gint pid = 0;
568 if (!g_spawn_async_with_pipes(dir, (gchar **) argv, NULL,
569 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL |
570 G_SPAWN_SEARCH_PATH,
571 NULL, NULL, /* Child setup fn */
572 &pid, /* Child PID */
573 NULL, NULL, NULL, /* Standard pipes */
574 &error))
576 delayed_error("%s", error ? error->message : "(null)");
577 g_error_free(error);
579 return 0;
582 return pid;
585 GtkWidget *button_new_image_text(GtkWidget *image, const char *message)
587 GtkWidget *button, *align, *hbox, *label;
589 button = gtk_button_new();
590 label = gtk_label_new_with_mnemonic(message);
591 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
593 hbox = gtk_hbox_new(FALSE, 2);
595 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
597 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
598 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
600 gtk_container_add(GTK_CONTAINER(button), align);
601 gtk_container_add(GTK_CONTAINER(align), hbox);
602 gtk_widget_show_all(align);
604 return button;
607 GtkWidget *button_new_mixed(const char *stock, const char *message)
609 return button_new_image_text(gtk_image_new_from_stock(stock,
610 GTK_ICON_SIZE_BUTTON),
611 message);
614 /* Highlight entry in red if 'error' is TRUE */
615 void entry_set_error(GtkWidget *entry, gboolean error)
617 const GdkColor red = {0, 0xffff, 0, 0};
618 const GdkColor white = {0, 0xffff, 0xffff, 0xffff};
620 gtk_widget_modify_text(entry, GTK_STATE_NORMAL, error ? &red : NULL);
621 gtk_widget_modify_base(entry, GTK_STATE_NORMAL, error ? &white : NULL);
624 /* Change stacking position of higher to be just above lower.
625 * If lower is NULL, put higher at the bottom of the stack.
627 void window_put_just_above(GdkWindow *higher, GdkWindow *lower)
629 if (o_override_redirect.int_value && lower)
631 XWindowChanges restack;
633 gdk_error_trap_push();
635 restack.stack_mode = Above;
637 restack.sibling = GDK_WINDOW_XWINDOW(lower);
639 XConfigureWindow(gdk_display, GDK_WINDOW_XWINDOW(higher),
640 CWSibling | CWStackMode, &restack);
642 gdk_flush();
643 if (gdk_error_trap_pop())
644 g_warning("window_put_just_above()\n");
646 else
647 gdk_window_lower(higher); /* To bottom of stack */
650 /* Copied from Gtk */
651 static GtkFixedChild* fixed_get_child(GtkFixed *fixed, GtkWidget *widget)
653 GList *children;
655 children = fixed->children;
656 while (children)
658 GtkFixedChild *child;
660 child = children->data;
661 children = children->next;
663 if (child->widget == widget)
664 return child;
667 return NULL;
670 /* Like gtk_fixed_move(), except not insanely slow */
671 void fixed_move_fast(GtkFixed *fixed, GtkWidget *widget, int x, int y)
673 GtkFixedChild *child;
675 child = fixed_get_child(fixed, widget);
677 g_assert(child);
679 gtk_widget_freeze_child_notify(widget);
681 child->x = x;
682 gtk_widget_child_notify(widget, "x");
684 child->y = y;
685 gtk_widget_child_notify(widget, "y");
687 gtk_widget_thaw_child_notify(widget);
689 if (GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_VISIBLE(fixed))
691 int border_width = GTK_CONTAINER(fixed)->border_width;
692 GtkAllocation child_allocation;
693 GtkRequisition child_requisition;
695 gtk_widget_get_child_requisition(child->widget,
696 &child_requisition);
697 child_allocation.x = child->x + border_width;
698 child_allocation.y = child->y + border_width;
700 child_allocation.x += GTK_WIDGET(fixed)->allocation.x;
701 child_allocation.y += GTK_WIDGET(fixed)->allocation.y;
703 child_allocation.width = child_requisition.width;
704 child_allocation.height = child_requisition.height;
705 gtk_widget_size_allocate(child->widget, &child_allocation);
709 /* Draw the black border */
710 static gint tooltip_draw(GtkWidget *w)
712 gdk_draw_rectangle(w->window, w->style->fg_gc[w->state], FALSE, 0, 0,
713 w->allocation.width - 1, w->allocation.height - 1);
715 return FALSE;
718 /* When the tips window closed, record the time. If we try to open another
719 * tip soon, it will appear more quickly.
721 static void tooltip_destroyed(gpointer data)
723 time(&tip_time);
726 /* Display a tooltip-like widget near the pointer with 'text'. If 'text' is
727 * NULL, close any current tooltip.
729 void tooltip_show(guchar *text)
731 GtkWidget *label;
732 int x, y, py;
733 int w, h;
734 int m;
736 if (tip_timeout)
738 g_source_remove(tip_timeout);
739 tip_timeout = 0;
742 if (tip_widget)
744 gtk_widget_destroy(tip_widget);
745 tip_widget = NULL;
748 if (!text)
749 return;
751 /* Show the tip */
752 tip_widget = gtk_window_new(GTK_WINDOW_POPUP);
753 gtk_widget_set_app_paintable(tip_widget, TRUE);
754 gtk_widget_set_name(tip_widget, "gtk-tooltips");
756 g_signal_connect_swapped(tip_widget, "expose_event",
757 G_CALLBACK(tooltip_draw), tip_widget);
759 label = gtk_label_new(text);
760 gtk_misc_set_padding(GTK_MISC(label), 4, 2);
761 gtk_container_add(GTK_CONTAINER(tip_widget), label);
762 gtk_widget_show(label);
763 gtk_widget_realize(tip_widget);
765 w = tip_widget->allocation.width;
766 h = tip_widget->allocation.height;
767 gdk_window_get_pointer(NULL, &x, &py, NULL);
769 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, py);
771 x -= w / 2;
772 y = py + 12; /* I don't know the pointer height so I use a constant */
774 /* Now check for screen boundaries */
775 x = CLAMP(x, monitor_geom[m].x,
776 monitor_geom[m].x + monitor_geom[m].width - w);
777 y = CLAMP(y, monitor_geom[m].y,
778 monitor_geom[m].y + monitor_geom[m].height - h);
780 /* And again test if pointer is over the tooltip window */
781 if (py >= y && py <= y + h)
782 y = py - h - 2;
783 gtk_window_move(GTK_WINDOW(tip_widget), x, y);
784 gtk_widget_show(tip_widget);
786 g_signal_connect_swapped(tip_widget, "destroy",
787 G_CALLBACK(tooltip_destroyed), NULL);
788 time(&tip_time);
791 /* Call callback(user_data) after a while, unless cancelled.
792 * Object is refd now and unref when cancelled / after callback called.
794 void tooltip_prime(GtkFunction callback, GObject *object)
796 time_t now;
797 int delay;
799 g_return_if_fail(tip_timeout == 0);
801 time(&now);
802 delay = now - tip_time > 2 ? 1000 : 200;
804 g_object_ref(object);
805 tip_timeout = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,
806 delay,
807 (GSourceFunc) callback,
808 object,
809 g_object_unref);
812 /* Like gtk_widget_modify_font, but copes with font_desc == NULL */
813 void widget_modify_font(GtkWidget *widget, PangoFontDescription *font_desc)
815 GtkRcStyle *rc_style;
817 g_return_if_fail(GTK_IS_WIDGET(widget));
819 rc_style = gtk_widget_get_modifier_style(widget);
821 if (rc_style->font_desc)
822 pango_font_description_free(rc_style->font_desc);
824 rc_style->font_desc = font_desc
825 ? pango_font_description_copy(font_desc)
826 : NULL;
828 gtk_widget_modify_style(widget, rc_style);
831 /* Confirm the action with the user. If action is NULL, the text from stock
832 * is used.
834 gboolean confirm(const gchar *message, const gchar *stock, const gchar *action)
836 return get_choice(PROJECT, message, 2,
837 GTK_STOCK_CANCEL, NULL,
838 stock, action) == 1;
841 struct _Radios {
842 GList *widgets;
844 void (*changed)(Radios *, gpointer data);
845 gpointer changed_data;
848 /* Create a new set of radio buttons.
849 * Use radios_add to add options, then radios_pack to put them into something.
850 * The radios object will self-destruct with the first widget it contains.
851 * changed(data) is called (if not NULL) when pack is called, and on any
852 * change after that.
854 Radios *radios_new(void (*changed)(Radios *, gpointer data), gpointer data)
856 Radios *radios;
858 radios = g_new(Radios, 1);
860 radios->widgets = NULL;
861 radios->changed = changed;
862 radios->changed_data = data;
864 return radios;
867 static void radios_free(GtkWidget *radio, Radios *radios)
869 g_return_if_fail(radios != NULL);
871 g_list_free(radios->widgets);
872 g_free(radios);
875 void radios_add(Radios *radios, const gchar *tip, gint value,
876 const gchar *label, ...)
878 GtkWidget *radio;
879 GSList *group = NULL;
880 gchar *s;
881 va_list args;
883 g_return_if_fail(radios != NULL);
884 g_return_if_fail(label != NULL);
886 va_start(args, label);
887 s = g_strdup_vprintf(label, args);
888 va_end(args);
890 if (radios->widgets)
892 GtkRadioButton *first = GTK_RADIO_BUTTON(radios->widgets->data);
893 group = gtk_radio_button_get_group(first);
896 radio = gtk_radio_button_new_with_label(group, s);
897 gtk_label_set_line_wrap(GTK_LABEL(GTK_BIN(radio)->child), TRUE);
898 gtk_widget_show(radio);
899 if (tip)
900 gtk_tooltips_set_tip(tooltips, radio, tip, NULL);
901 if (!group)
902 g_signal_connect(G_OBJECT(radio), "destroy",
903 G_CALLBACK(radios_free), radios);
905 radios->widgets = g_list_prepend(radios->widgets, radio);
906 g_object_set_data(G_OBJECT(radio), "rox-radios-value",
907 GINT_TO_POINTER(value));
910 static void radio_toggled(GtkToggleButton *button, Radios *radios)
912 g_return_if_fail(radios != NULL);
914 if (button && !gtk_toggle_button_get_active(button))
915 return; /* Stop double-notifies */
917 if (radios->changed)
918 radios->changed(radios, radios->changed_data);
921 void radios_pack(Radios *radios, GtkBox *box)
923 GList *next;
925 g_return_if_fail(radios != NULL);
927 for (next = g_list_last(radios->widgets); next; next = next->prev)
929 GtkWidget *button = GTK_WIDGET(next->data);
931 gtk_box_pack_start(box, button, FALSE, TRUE, 0);
932 g_signal_connect(button, "toggled",
933 G_CALLBACK(radio_toggled), radios);
935 radio_toggled(NULL, radios);
938 void radios_set_value(Radios *radios, gint value)
940 GList *next;
942 g_return_if_fail(radios != NULL);
944 for (next = radios->widgets; next; next = next->next)
946 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
947 int radio_value;
949 radio_value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(radio),
950 "rox-radios-value"));
952 if (radio_value == value)
954 gtk_toggle_button_set_active(radio, TRUE);
955 return;
959 g_warning("Value %d not in radio group!", value);
962 gint radios_get_value(Radios *radios)
964 GList *next;
966 g_return_val_if_fail(radios != NULL, -1);
968 for (next = radios->widgets; next; next = next->next)
970 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
972 if (gtk_toggle_button_get_active(radio))
973 return GPOINTER_TO_INT(g_object_get_data(
974 G_OBJECT(radio), "rox-radios-value"));
977 g_warning("Nothing in the radio group is selected!");
979 return -1;
982 /* Convert a list of URIs as a string into a GList of EscapedPath URIs.
983 * No unescaping is done.
984 * Lines beginning with # are skipped.
985 * The text block passed in is zero terminated (after the final CRLF)
987 GList *uri_list_to_glist(const char *uri_list)
989 GList *list = NULL;
991 while (*uri_list)
993 char *linebreak;
994 int length;
996 linebreak = strchr(uri_list, 13);
998 if (!linebreak || linebreak[1] != 10)
1000 delayed_error("uri_list_to_glist: %s",
1001 _("Incorrect or missing line "
1002 "break in text/uri-list data"));
1003 return list;
1006 length = linebreak - uri_list;
1008 if (length && uri_list[0] != '#')
1009 list = g_list_append(list, g_strndup(uri_list, length));
1011 uri_list = linebreak + 2;
1014 return list;
1017 typedef struct _SimpleImageClass SimpleImageClass;
1018 typedef struct _SimpleImage SimpleImage;
1020 struct _SimpleImageClass {
1021 GtkWidgetClass parent;
1024 struct _SimpleImage {
1025 GtkWidget widget;
1027 GdkPixbuf *pixbuf;
1028 int width, height;
1031 #define SIMPLE_IMAGE(obj) (GTK_CHECK_CAST((obj), \
1032 simple_image_get_type(), SimpleImage))
1034 static void simple_image_finialize(GObject *object)
1036 SimpleImage *image = SIMPLE_IMAGE(object);
1038 g_object_unref(G_OBJECT(image->pixbuf));
1039 image->pixbuf = NULL;
1042 static void simple_image_size_request(GtkWidget *widget,
1043 GtkRequisition *requisition)
1045 SimpleImage *image = (SimpleImage *) widget;
1047 requisition->width = image->width;
1048 requisition->height = image->height;
1051 /* Render a pixbuf without messing up the clipping */
1052 void render_pixbuf(GdkPixbuf *pixbuf, GdkDrawable *target, GdkGC *gc,
1053 int x, int y, int width, int height)
1055 gdk_draw_pixbuf(target, gc, pixbuf, 0, 0, x, y, width, height,
1056 GDK_RGB_DITHER_NORMAL, 0, 0);
1060 static gint simple_image_expose(GtkWidget *widget, GdkEventExpose *event)
1062 SimpleImage *image = (SimpleImage *) widget;
1063 int x;
1065 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1067 x = widget->allocation.x +
1068 (widget->allocation.width - image->width) / 2;
1070 render_pixbuf(image->pixbuf, widget->window, widget->style->black_gc,
1071 x, widget->allocation.y,
1072 image->width, image->height);
1074 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1075 return FALSE;
1078 static void simple_image_class_init(gpointer gclass, gpointer data)
1080 GObjectClass *object = (GObjectClass *) gclass;
1081 GtkWidgetClass *widget = (GtkWidgetClass *) gclass;
1083 object->finalize = simple_image_finialize;
1084 widget->size_request = simple_image_size_request;
1085 widget->expose_event = simple_image_expose;
1088 static void simple_image_init(GTypeInstance *object, gpointer gclass)
1090 GTK_WIDGET_SET_FLAGS(object, GTK_NO_WINDOW);
1093 static GType simple_image_get_type(void)
1095 static GType type = 0;
1097 if (!type)
1099 static const GTypeInfo info =
1101 sizeof (SimpleImageClass),
1102 NULL, /* base_init */
1103 NULL, /* base_finalise */
1104 simple_image_class_init,
1105 NULL, /* class_finalise */
1106 NULL, /* class_data */
1107 sizeof(SimpleImage),
1108 0, /* n_preallocs */
1109 simple_image_init,
1112 type = g_type_register_static(gtk_widget_get_type(),
1113 "SimpleImage", &info, 0);
1116 return type;
1119 GtkWidget *simple_image_new(GdkPixbuf *pixbuf)
1121 SimpleImage *image;
1123 g_return_val_if_fail(pixbuf != NULL, NULL);
1125 image = g_object_new(simple_image_get_type(), NULL);
1127 image->pixbuf = pixbuf;
1128 g_object_ref(G_OBJECT(pixbuf));
1130 image->width = gdk_pixbuf_get_width(pixbuf);
1131 image->height = gdk_pixbuf_get_height(pixbuf);
1133 return GTK_WIDGET(image);
1136 /* Whether a line l1 long starting from n1 overlaps a line l2 from n2 */
1137 inline static gboolean gui_ranges_overlap(int n1, int l1, int n2, int l2)
1139 return (n1 > n2 && n1 < n2 + l2) ||
1140 (n1 + l1 > n2 && n1 + l1 < n2 + l2) ||
1141 (n1 <= n2 && n1 + l1 >= n2 + l2);
1144 static void gui_get_monitor_adjacent(int monitor, MonitorAdjacent *adj)
1146 int m;
1148 adj->left = adj->right = adj->top = adj->bottom = FALSE;
1150 for (m = 0; m < n_monitors; ++m)
1152 if (m == monitor)
1153 continue;
1154 if (gui_ranges_overlap(monitor_geom[m].y,
1155 monitor_geom[m].height,
1156 monitor_geom[monitor].y,
1157 monitor_geom[monitor].height))
1159 if (monitor_geom[m].x < monitor_geom[monitor].x)
1161 adj->left = TRUE;
1163 else if (monitor_geom[m].x > monitor_geom[monitor].x)
1165 adj->right = TRUE;
1168 if (gui_ranges_overlap(monitor_geom[m].x,
1169 monitor_geom[m].width,
1170 monitor_geom[monitor].x,
1171 monitor_geom[monitor].width))
1173 if (monitor_geom[m].y < monitor_geom[monitor].y)
1175 adj->top = TRUE;
1177 else if (monitor_geom[m].y > monitor_geom[monitor].y)
1179 adj->bottom = TRUE;
1185 static void rox_wmspec_change_state(gboolean add, GdkWindow *window,
1186 GdkAtom state1, GdkAtom state2)
1188 GdkDisplay *display = gdk_drawable_get_display(GDK_DRAWABLE(window));
1189 XEvent xev;
1191 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1192 #define _NET_WM_STATE_ADD 1 /* add/set property */
1193 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1195 xev.xclient.type = ClientMessage;
1196 xev.xclient.serial = 0;
1197 xev.xclient.send_event = True;
1198 xev.xclient.window = GDK_WINDOW_XID(window);
1199 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display(
1200 display, "_NET_WM_STATE");
1201 xev.xclient.format = 32;
1202 xev.xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1203 xev.xclient.data.l[1] = gdk_x11_atom_to_xatom_for_display(display,
1204 state1);
1205 xev.xclient.data.l[2] = gdk_x11_atom_to_xatom_for_display(display,
1206 state2);
1207 xev.xclient.data.l[3] = 0;
1208 xev.xclient.data.l[4] = 0;
1210 XSendEvent(GDK_DISPLAY_XDISPLAY(display),
1211 GDK_WINDOW_XID(
1212 gdk_screen_get_root_window(
1213 gdk_drawable_get_screen(GDK_DRAWABLE(window)))),
1214 False,
1215 SubstructureRedirectMask | SubstructureNotifyMask,
1216 &xev);
1219 void keep_below(GdkWindow *window, gboolean setting)
1221 g_return_if_fail(GDK_IS_WINDOW(window));
1223 if (GDK_WINDOW_DESTROYED(window))
1224 return;
1226 if (gdk_window_is_visible(window))
1228 if (setting)
1230 rox_wmspec_change_state(FALSE, window,
1231 gdk_atom_intern("_NET_WM_STATE_ABOVE", FALSE),
1232 GDK_NONE);
1234 rox_wmspec_change_state(setting, window,
1235 gdk_atom_intern("_NET_WM_STATE_BELOW", FALSE),
1236 GDK_NONE);
1238 #if 0
1239 else
1241 #if GTK_CHECK_VERSION(2,4,0)
1242 gdk_synthesize_window_state(window,
1243 setting ? GDK_WINDOW_STATE_ABOVE :
1244 GDK_WINDOW_STATE_BELOW,
1245 setting ? GDK_WINDOW_STATE_BELOW : 0);
1246 #endif
1248 #endif
1251 static void
1252 size_prepared_cb (GdkPixbufLoader *loader,
1253 int width,
1254 int height,
1255 gpointer data)
1257 struct {
1258 gint width;
1259 gint height;
1260 gboolean preserve_aspect_ratio;
1261 } *info = data;
1263 g_return_if_fail (width > 0 && height > 0);
1265 if(info->preserve_aspect_ratio) {
1266 if ((double)height * (double)info->width >
1267 (double)width * (double)info->height) {
1268 width = 0.5 + (double)width * (double)info->height / (double)height;
1269 height = info->height;
1270 } else {
1271 height = 0.5 + (double)height * (double)info->width / (double)width;
1272 width = info->width;
1274 } else {
1275 width = info->width;
1276 height = info->height;
1279 gdk_pixbuf_loader_set_size (loader, width, height);
1283 * rox_pixbuf_new_from_file_at_scale:
1284 * @filename: Name of file to load.
1285 * @width: The width the image should have
1286 * @height: The height the image should have
1287 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1288 * @error: Return location for an error
1290 * Creates a new pixbuf by loading an image from a file. The file format is
1291 * detected automatically. If %NULL is returned, then @error will be set.
1292 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1293 * The image will be scaled to fit in the requested size, optionally preserving
1294 * the image's aspect ratio.
1296 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1297 * if any of several error conditions occurred: the file could not be opened,
1298 * there was no loader for the file's format, there was not enough memory to
1299 * allocate the image buffer, or the image file contained invalid data.
1301 * Taken from GTK 2.6.
1303 GdkPixbuf *
1304 rox_pixbuf_new_from_file_at_scale (const char *filename,
1305 int width,
1306 int height,
1307 gboolean preserve_aspect_ratio,
1308 GError **error)
1311 GdkPixbufLoader *loader;
1312 GdkPixbuf *pixbuf;
1314 guchar buffer [4096];
1315 int length;
1316 FILE *f;
1317 struct {
1318 gint width;
1319 gint height;
1320 gboolean preserve_aspect_ratio;
1321 } info;
1323 g_return_val_if_fail (filename != NULL, NULL);
1324 g_return_val_if_fail (width > 0 && height > 0, NULL);
1326 f = fopen (filename, "rb");
1327 if (!f) {
1328 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1329 NULL, NULL, NULL);
1330 g_set_error (error,
1331 G_FILE_ERROR,
1332 g_file_error_from_errno (errno),
1333 _("Failed to open file '%s': %s"),
1334 utf8_filename ? utf8_filename : "???",
1335 g_strerror (errno));
1336 g_free (utf8_filename);
1337 return NULL;
1340 loader = gdk_pixbuf_loader_new ();
1342 info.width = width;
1343 info.height = height;
1344 info.preserve_aspect_ratio = preserve_aspect_ratio;
1346 g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), &info);
1348 while (!feof (f) && !ferror (f)) {
1349 length = fread (buffer, 1, sizeof (buffer), f);
1350 if (length > 0)
1351 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1352 gdk_pixbuf_loader_close (loader, NULL);
1353 fclose (f);
1354 g_object_unref (loader);
1355 return NULL;
1359 fclose (f);
1361 if (!gdk_pixbuf_loader_close (loader, error)) {
1362 g_object_unref (loader);
1363 return NULL;
1366 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1368 if (!pixbuf) {
1369 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1370 NULL, NULL, NULL);
1372 g_object_unref (loader);
1374 g_set_error (error,
1375 GDK_PIXBUF_ERROR,
1376 GDK_PIXBUF_ERROR_FAILED,
1377 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1378 utf8_filename ? utf8_filename : "???");
1379 g_free (utf8_filename);
1380 return NULL;
1383 g_object_ref (pixbuf);
1385 g_object_unref (loader);
1387 return pixbuf;
1390 /* Make the name bolder and larger.
1391 * scale_factor can be PANGO_SCALE_X_LARGE, etc.
1393 void make_heading(GtkWidget *label, double scale_factor)
1395 PangoAttribute *attr;
1396 PangoAttrList *list;
1398 list = pango_attr_list_new();
1400 attr = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
1401 attr->start_index = 0;
1402 attr->end_index = -1;
1403 pango_attr_list_insert(list, attr);
1405 attr = pango_attr_scale_new(scale_factor);
1406 attr->start_index = 0;
1407 attr->end_index = -1;
1408 pango_attr_list_insert(list, attr);
1410 gtk_label_set_attributes(GTK_LABEL(label), list);
1413 /* Launch a program using 0launch.
1414 * If button-3 is used, open the GUI with -g.
1416 void launch_uri(const char *uri)
1418 const char *argv[] = {"0launch", NULL, NULL, NULL};
1419 const char *uri_0launch = "/uri/0install/zero-install.sourceforge.net"
1420 "/bin/0launch";
1422 if (!available_in_path(argv[0]))
1424 if (access(uri_0launch, X_OK) == 0)
1425 argv[0] = uri_0launch;
1426 else
1428 delayed_error(_("This program cannot be run, as the "
1429 "0launch command is not available. "
1430 "It can be downloaded from here:\n\n"
1431 "http://0install.net/injector.html"));
1432 return;
1436 if (current_event_button() == 3)
1438 argv[1] = "-g";
1439 argv[2] = uri;
1441 else
1442 argv[1] = uri;
1444 rox_spawn(NULL, argv);
1447 static gint button3_button_pressed(GtkButton *button,
1448 GdkEventButton *event,
1449 gpointer date)
1451 if (event->button == 3)
1453 gtk_grab_add(GTK_WIDGET(button));
1454 gtk_button_pressed(button);
1456 return TRUE;
1459 return FALSE;
1462 static gint button3_button_released(GtkButton *button,
1463 GdkEventButton *event,
1464 FilerWindow *filer_window)
1466 if (event->button == 3)
1468 gtk_grab_remove(GTK_WIDGET(button));
1469 gtk_button_released(button);
1471 return TRUE;
1474 return FALSE;
1477 void allow_right_click(GtkWidget *button)
1479 g_signal_connect(button, "button_press_event",
1480 G_CALLBACK(button3_button_pressed), NULL);
1481 g_signal_connect(button, "button_release_event",
1482 G_CALLBACK(button3_button_released), NULL);
1485 /* Return mouse button used in the current event, or -1 if none (no event,
1486 * or not a click).
1488 gint current_event_button(void)
1490 GdkEventButton *bev;
1491 gint button = -1;
1493 bev = (GdkEventButton *) gtk_get_current_event();
1495 if (bev &&
1496 (bev->type == GDK_BUTTON_PRESS || bev->type == GDK_BUTTON_RELEASE))
1497 button = bev->button;
1499 gdk_event_free((GdkEvent *) bev);
1501 return button;
1504 /* Create a new pixbuf by colourizing 'src' to 'color'. If the function fails,
1505 * 'src' will be returned (with an increased reference count, so it is safe to
1506 * g_object_unref() the return value whether the function fails or not).
1508 GdkPixbuf *create_spotlight_pixbuf(GdkPixbuf *src, GdkColor *color)
1510 guchar opacity = 192;
1511 guchar alpha = 255 - opacity;
1512 GdkPixbuf *dst;
1513 GdkColorspace colorspace;
1514 int width, height, src_rowstride, dst_rowstride, x, y;
1515 int n_channels, bps;
1516 int r, g, b;
1517 guchar *spixels, *dpixels, *src_pixels, *dst_pixels;
1518 gboolean has_alpha;
1520 has_alpha = gdk_pixbuf_get_has_alpha(src);
1521 colorspace = gdk_pixbuf_get_colorspace(src);
1522 n_channels = gdk_pixbuf_get_n_channels(src);
1523 bps = gdk_pixbuf_get_bits_per_sample(src);
1525 if ((colorspace != GDK_COLORSPACE_RGB) ||
1526 (!has_alpha && n_channels != 3) ||
1527 (has_alpha && n_channels != 4) ||
1528 (bps != 8))
1529 goto error;
1531 width = gdk_pixbuf_get_width(src);
1532 height = gdk_pixbuf_get_height(src);
1534 dst = gdk_pixbuf_new(colorspace, has_alpha, bps, width, height);
1535 if (dst == NULL)
1536 goto error;
1538 src_pixels = gdk_pixbuf_get_pixels(src);
1539 dst_pixels = gdk_pixbuf_get_pixels(dst);
1540 src_rowstride = gdk_pixbuf_get_rowstride(src);
1541 dst_rowstride = gdk_pixbuf_get_rowstride(dst);
1543 r = opacity * (color->red >> 8);
1544 g = opacity * (color->green >> 8);
1545 b = opacity * (color->blue >> 8);
1547 for (y = 0; y < height; y++)
1549 spixels = src_pixels + y * src_rowstride;
1550 dpixels = dst_pixels + y * dst_rowstride;
1551 for (x = 0; x < width; x++)
1553 *dpixels++ = (*spixels++ * alpha + r) >> 8;
1554 *dpixels++ = (*spixels++ * alpha + g) >> 8;
1555 *dpixels++ = (*spixels++ * alpha + b) >> 8;
1556 if (has_alpha)
1557 *dpixels++ = *spixels++;
1561 return dst;
1563 error:
1564 g_object_ref(src);
1565 return src;