r3768: Updated years.
[rox-filer.git] / ROX-Filer / src / gui_support.c
blob509d09f495b5ee61b738d07abaf1517107ffe8ed
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, guint32 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 gint32 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(gint32));
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 click on Save.\n"
421 "Further errors will be ignored."),
422 path,
423 line_number,
424 error);
425 seen_error = TRUE;
428 if (!eol)
429 break;
430 line = eol + 1;
431 line_number++;
433 g_free(data);
437 /* Returns the position of the pointer.
438 * TRUE if any modifier keys or mouse buttons are pressed.
440 gboolean get_pointer_xy(int *x, int *y)
442 unsigned int mask;
444 gdk_window_get_pointer(NULL, x, y, &mask);
446 return mask != 0;
449 #define DECOR_BORDER 32
451 /* Centre the window at these coords */
452 void centre_window(GdkWindow *window, int x, int y)
454 int w, h;
455 int m;
457 g_return_if_fail(window != NULL);
459 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, y);
461 gdk_drawable_get_size(window, &w, &h);
463 x -= w / 2;
464 y -= h / 2;
466 gdk_window_move(window,
467 CLAMP(x, DECOR_BORDER + monitor_geom[m].x,
468 monitor_geom[m].x + monitor_geom[m].width
469 - w - DECOR_BORDER),
470 CLAMP(y, DECOR_BORDER + monitor_geom[m].y,
471 monitor_geom[m].y + monitor_geom[m].height
472 - h - DECOR_BORDER));
475 static void run_error_info_dialog(GtkMessageType type, const char *message,
476 va_list args)
478 GtkWidget *dialog;
479 gchar *s;
481 g_return_if_fail(message != NULL);
483 s = g_strdup_vprintf(message, args);
484 va_end(args);
486 dialog = gtk_message_dialog_new(NULL,
487 GTK_DIALOG_MODAL,
488 type,
489 GTK_BUTTONS_OK,
490 "%s", s);
491 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
492 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
493 gtk_dialog_run(GTK_DIALOG(dialog));
494 gtk_widget_destroy(dialog);
496 g_free(s);
499 static GtkWidget *current_wink_widget = NULL;
500 static gint wink_timeout = -1; /* Called when it's time to stop */
501 static gulong wink_destroy; /* Called if the widget dies first */
503 static gboolean end_wink(gpointer data)
505 gtk_drag_unhighlight(current_wink_widget);
507 g_signal_handler_disconnect(current_wink_widget, wink_destroy);
509 current_wink_widget = NULL;
511 return FALSE;
514 static void cancel_wink(void)
516 g_source_remove(wink_timeout);
517 end_wink(NULL);
520 static void wink_widget_died(gpointer data)
522 current_wink_widget = NULL;
523 g_source_remove(wink_timeout);
526 /* Draw a black box around this widget, briefly.
527 * Note: uses the drag highlighting code for now.
529 void wink_widget(GtkWidget *widget)
531 g_return_if_fail(widget != NULL);
533 if (current_wink_widget)
534 cancel_wink();
536 current_wink_widget = widget;
537 gtk_drag_highlight(current_wink_widget);
539 wink_timeout = g_timeout_add(300, (GSourceFunc) end_wink, NULL);
541 wink_destroy = g_signal_connect_swapped(widget, "destroy",
542 G_CALLBACK(wink_widget_died), NULL);
545 static gboolean idle_destroy_cb(GtkWidget *widget)
547 gtk_widget_unref(widget);
548 gtk_widget_destroy(widget);
549 return FALSE;
552 /* Destroy the widget in an idle callback */
553 void destroy_on_idle(GtkWidget *widget)
555 gtk_widget_ref(widget);
556 g_idle_add((GSourceFunc) idle_destroy_cb, widget);
559 /* Spawn a child process (as spawn_full), and report errors.
560 * Returns the child's PID on succes, or 0 on failure.
562 gint rox_spawn(const gchar *dir, const gchar **argv)
564 GError *error = NULL;
565 gint pid = 0;
567 if (!g_spawn_async_with_pipes(dir, (gchar **) argv, NULL,
568 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_STDOUT_TO_DEV_NULL |
569 G_SPAWN_SEARCH_PATH,
570 NULL, NULL, /* Child setup fn */
571 &pid, /* Child PID */
572 NULL, NULL, NULL, /* Standard pipes */
573 &error))
575 delayed_error("%s", error ? error->message : "(null)");
576 g_error_free(error);
578 return 0;
581 return pid;
584 GtkWidget *button_new_image_text(GtkWidget *image, const char *message)
586 GtkWidget *button, *align, *hbox, *label;
588 button = gtk_button_new();
589 label = gtk_label_new_with_mnemonic(message);
590 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
592 hbox = gtk_hbox_new(FALSE, 2);
594 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
596 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
597 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
599 gtk_container_add(GTK_CONTAINER(button), align);
600 gtk_container_add(GTK_CONTAINER(align), hbox);
601 gtk_widget_show_all(align);
603 return button;
606 GtkWidget *button_new_mixed(const char *stock, const char *message)
608 return button_new_image_text(gtk_image_new_from_stock(stock,
609 GTK_ICON_SIZE_BUTTON),
610 message);
613 /* Highlight entry in red if 'error' is TRUE */
614 void entry_set_error(GtkWidget *entry, gboolean error)
616 GdkColor red = {0, 0xffff, 0, 0};
617 static gboolean need_init = TRUE;
618 static GdkColor normal;
620 if (need_init)
622 normal = entry->style->text[GTK_STATE_NORMAL];
623 need_init = FALSE;
626 gtk_widget_modify_text(entry, GTK_STATE_NORMAL, error ? &red : &normal);
629 /* Change stacking position of higher to be just above lower.
630 * If lower is NULL, put higher at the bottom of the stack.
632 void window_put_just_above(GdkWindow *higher, GdkWindow *lower)
634 if (o_override_redirect.int_value && lower)
636 XWindowChanges restack;
638 gdk_error_trap_push();
640 restack.stack_mode = Above;
642 restack.sibling = GDK_WINDOW_XWINDOW(lower);
644 XConfigureWindow(gdk_display, GDK_WINDOW_XWINDOW(higher),
645 CWSibling | CWStackMode, &restack);
647 gdk_flush();
648 if (gdk_error_trap_pop())
649 g_warning("window_put_just_above()\n");
651 else
652 gdk_window_lower(higher); /* To bottom of stack */
655 /* Copied from Gtk */
656 static GtkFixedChild* fixed_get_child(GtkFixed *fixed, GtkWidget *widget)
658 GList *children;
660 children = fixed->children;
661 while (children)
663 GtkFixedChild *child;
665 child = children->data;
666 children = children->next;
668 if (child->widget == widget)
669 return child;
672 return NULL;
675 /* Like gtk_fixed_move(), except not insanely slow */
676 void fixed_move_fast(GtkFixed *fixed, GtkWidget *widget, int x, int y)
678 GtkFixedChild *child;
680 child = fixed_get_child(fixed, widget);
682 g_assert(child);
684 gtk_widget_freeze_child_notify(widget);
686 child->x = x;
687 gtk_widget_child_notify(widget, "x");
689 child->y = y;
690 gtk_widget_child_notify(widget, "y");
692 gtk_widget_thaw_child_notify(widget);
694 if (GTK_WIDGET_VISIBLE(widget) && GTK_WIDGET_VISIBLE(fixed))
696 int border_width = GTK_CONTAINER(fixed)->border_width;
697 GtkAllocation child_allocation;
698 GtkRequisition child_requisition;
700 gtk_widget_get_child_requisition(child->widget,
701 &child_requisition);
702 child_allocation.x = child->x + border_width;
703 child_allocation.y = child->y + border_width;
705 child_allocation.x += GTK_WIDGET(fixed)->allocation.x;
706 child_allocation.y += GTK_WIDGET(fixed)->allocation.y;
708 child_allocation.width = child_requisition.width;
709 child_allocation.height = child_requisition.height;
710 gtk_widget_size_allocate(child->widget, &child_allocation);
714 /* Draw the black border */
715 static gint tooltip_draw(GtkWidget *w)
717 gdk_draw_rectangle(w->window, w->style->fg_gc[w->state], FALSE, 0, 0,
718 w->allocation.width - 1, w->allocation.height - 1);
720 return FALSE;
723 /* When the tips window closed, record the time. If we try to open another
724 * tip soon, it will appear more quickly.
726 static void tooltip_destroyed(gpointer data)
728 time(&tip_time);
731 /* Display a tooltip-like widget near the pointer with 'text'. If 'text' is
732 * NULL, close any current tooltip.
734 void tooltip_show(guchar *text)
736 GtkWidget *label;
737 int x, y, py;
738 int w, h;
739 int m;
741 if (tip_timeout)
743 g_source_remove(tip_timeout);
744 tip_timeout = 0;
747 if (tip_widget)
749 gtk_widget_destroy(tip_widget);
750 tip_widget = NULL;
753 if (!text)
754 return;
756 /* Show the tip */
757 tip_widget = gtk_window_new(GTK_WINDOW_POPUP);
758 gtk_widget_set_app_paintable(tip_widget, TRUE);
759 gtk_widget_set_name(tip_widget, "gtk-tooltips");
761 g_signal_connect_swapped(tip_widget, "expose_event",
762 G_CALLBACK(tooltip_draw), tip_widget);
764 label = gtk_label_new(text);
765 gtk_misc_set_padding(GTK_MISC(label), 4, 2);
766 gtk_container_add(GTK_CONTAINER(tip_widget), label);
767 gtk_widget_show(label);
768 gtk_widget_realize(tip_widget);
770 w = tip_widget->allocation.width;
771 h = tip_widget->allocation.height;
772 gdk_window_get_pointer(NULL, &x, &py, NULL);
774 m = gdk_screen_get_monitor_at_point(gdk_screen_get_default(), x, py);
776 x -= w / 2;
777 y = py + 12; /* I don't know the pointer height so I use a constant */
779 /* Now check for screen boundaries */
780 x = CLAMP(x, monitor_geom[m].x,
781 monitor_geom[m].x + monitor_geom[m].width - w);
782 y = CLAMP(y, monitor_geom[m].y,
783 monitor_geom[m].y + monitor_geom[m].height - h);
785 /* And again test if pointer is over the tooltip window */
786 if (py >= y && py <= y + h)
787 y = py - h - 2;
788 gtk_window_move(GTK_WINDOW(tip_widget), x, y);
789 gtk_widget_show(tip_widget);
791 g_signal_connect_swapped(tip_widget, "destroy",
792 G_CALLBACK(tooltip_destroyed), NULL);
793 time(&tip_time);
796 /* Call callback(user_data) after a while, unless cancelled.
797 * Object is refd now and unref when cancelled / after callback called.
799 void tooltip_prime(GtkFunction callback, GObject *object)
801 time_t now;
802 int delay;
804 g_return_if_fail(tip_timeout == 0);
806 time(&now);
807 delay = now - tip_time > 2 ? 1000 : 200;
809 g_object_ref(object);
810 tip_timeout = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE,
811 delay,
812 (GSourceFunc) callback,
813 object,
814 g_object_unref);
817 /* Like gtk_widget_modify_font, but copes with font_desc == NULL */
818 void widget_modify_font(GtkWidget *widget, PangoFontDescription *font_desc)
820 GtkRcStyle *rc_style;
822 g_return_if_fail(GTK_IS_WIDGET(widget));
824 rc_style = gtk_widget_get_modifier_style(widget);
826 if (rc_style->font_desc)
827 pango_font_description_free(rc_style->font_desc);
829 rc_style->font_desc = font_desc
830 ? pango_font_description_copy(font_desc)
831 : NULL;
833 gtk_widget_modify_style(widget, rc_style);
836 /* Confirm the action with the user. If action is NULL, the text from stock
837 * is used.
839 gboolean confirm(const gchar *message, const gchar *stock, const gchar *action)
841 return get_choice(PROJECT, message, 2,
842 GTK_STOCK_CANCEL, NULL,
843 stock, action) == 1;
846 struct _Radios {
847 GList *widgets;
849 void (*changed)(gpointer data);
850 gpointer changed_data;
853 /* Create a new set of radio buttons.
854 * Use radios_add to add options, then radios_pack to put them into something.
855 * The radios object will self-destruct with the first widget it contains.
856 * changed(data) is called (if not NULL) when pack is called, and on any
857 * change after that.
859 Radios *radios_new(void (*changed)(gpointer data), gpointer data)
861 Radios *radios;
863 radios = g_new(Radios, 1);
865 radios->widgets = NULL;
866 radios->changed = changed;
867 radios->changed_data = data;
869 return radios;
872 static void radios_free(GtkWidget *radio, Radios *radios)
874 g_return_if_fail(radios != NULL);
876 g_list_free(radios->widgets);
877 g_free(radios);
880 void radios_add(Radios *radios, const gchar *tip, gint value,
881 const gchar *label, ...)
883 GtkWidget *radio;
884 GSList *group = NULL;
885 gchar *s;
886 va_list args;
888 g_return_if_fail(radios != NULL);
889 g_return_if_fail(label != NULL);
891 va_start(args, label);
892 s = g_strdup_vprintf(label, args);
893 va_end(args);
895 if (radios->widgets)
897 GtkRadioButton *first = GTK_RADIO_BUTTON(radios->widgets->data);
898 group = gtk_radio_button_get_group(first);
901 radio = gtk_radio_button_new_with_label(group, s);
902 gtk_label_set_line_wrap(GTK_LABEL(GTK_BIN(radio)->child), TRUE);
903 gtk_widget_show(radio);
904 if (tip)
905 gtk_tooltips_set_tip(tooltips, radio, tip, NULL);
906 if (!group)
907 g_signal_connect(G_OBJECT(radio), "destroy",
908 G_CALLBACK(radios_free), radios);
910 radios->widgets = g_list_prepend(radios->widgets, radio);
911 g_object_set_data(G_OBJECT(radio), "rox-radios-value",
912 GINT_TO_POINTER(value));
915 static void radio_toggled(GtkToggleButton *button, Radios *radios)
917 g_return_if_fail(radios != NULL);
919 if (radios->changed)
920 radios->changed(radios->changed_data);
923 void radios_pack(Radios *radios, GtkBox *box)
925 GList *next;
927 g_return_if_fail(radios != NULL);
929 for (next = g_list_last(radios->widgets); next; next = next->prev)
931 GtkWidget *button = GTK_WIDGET(next->data);
933 gtk_box_pack_start(box, button, FALSE, TRUE, 0);
934 g_signal_connect(button, "toggled",
935 G_CALLBACK(radio_toggled), radios);
937 radio_toggled(NULL, radios);
940 void radios_set_value(Radios *radios, gint value)
942 GList *next;
944 g_return_if_fail(radios != NULL);
946 for (next = radios->widgets; next; next = next->next)
948 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
949 int radio_value;
951 radio_value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(radio),
952 "rox-radios-value"));
954 if (radio_value == value)
956 gtk_toggle_button_set_active(radio, TRUE);
957 return;
961 g_warning("Value %d not in radio group!", value);
964 gint radios_get_value(Radios *radios)
966 GList *next;
968 g_return_val_if_fail(radios != NULL, -1);
970 for (next = radios->widgets; next; next = next->next)
972 GtkToggleButton *radio = GTK_TOGGLE_BUTTON(next->data);
974 if (gtk_toggle_button_get_active(radio))
975 return GPOINTER_TO_INT(g_object_get_data(
976 G_OBJECT(radio), "rox-radios-value"));
979 g_warning("Nothing in the radio group is selected!");
981 return -1;
984 /* Convert a list of URIs as a string into a GList of EscapedPath URIs.
985 * No unescaping is done.
986 * Lines beginning with # are skipped.
987 * The text block passed in is zero terminated (after the final CRLF)
989 GList *uri_list_to_glist(const char *uri_list)
991 GList *list = NULL;
993 while (*uri_list)
995 char *linebreak;
996 int length;
998 linebreak = strchr(uri_list, 13);
1000 if (!linebreak || linebreak[1] != 10)
1002 delayed_error("uri_list_to_glist: %s",
1003 _("Incorrect or missing line "
1004 "break in text/uri-list data"));
1005 return list;
1008 length = linebreak - uri_list;
1010 if (length && uri_list[0] != '#')
1011 list = g_list_append(list, g_strndup(uri_list, length));
1013 uri_list = linebreak + 2;
1016 return list;
1019 typedef struct _SimpleImageClass SimpleImageClass;
1020 typedef struct _SimpleImage SimpleImage;
1022 struct _SimpleImageClass {
1023 GtkWidgetClass parent;
1026 struct _SimpleImage {
1027 GtkWidget widget;
1029 GdkPixbuf *pixbuf;
1030 int width, height;
1033 #define SIMPLE_IMAGE(obj) (GTK_CHECK_CAST((obj), \
1034 simple_image_get_type(), SimpleImage))
1036 static void simple_image_finialize(GObject *object)
1038 SimpleImage *image = SIMPLE_IMAGE(object);
1040 g_object_unref(G_OBJECT(image->pixbuf));
1041 image->pixbuf = NULL;
1044 static void simple_image_size_request(GtkWidget *widget,
1045 GtkRequisition *requisition)
1047 SimpleImage *image = (SimpleImage *) widget;
1049 requisition->width = image->width;
1050 requisition->height = image->height;
1053 /* Render a pixbuf without messing up the clipping */
1054 void render_pixbuf(GdkPixbuf *pixbuf, GdkDrawable *target, GdkGC *gc,
1055 int x, int y, int width, int height)
1057 gdk_draw_pixbuf(target, gc, pixbuf, 0, 0, x, y, width, height,
1058 GDK_RGB_DITHER_NORMAL, 0, 0);
1062 static gint simple_image_expose(GtkWidget *widget, GdkEventExpose *event)
1064 SimpleImage *image = (SimpleImage *) widget;
1065 int x;
1067 gdk_gc_set_clip_region(widget->style->black_gc, event->region);
1069 x = widget->allocation.x +
1070 (widget->allocation.width - image->width) / 2;
1072 render_pixbuf(image->pixbuf, widget->window, widget->style->black_gc,
1073 x, widget->allocation.y,
1074 image->width, image->height);
1076 gdk_gc_set_clip_region(widget->style->black_gc, NULL);
1077 return FALSE;
1080 static void simple_image_class_init(gpointer gclass, gpointer data)
1082 GObjectClass *object = (GObjectClass *) gclass;
1083 GtkWidgetClass *widget = (GtkWidgetClass *) gclass;
1085 object->finalize = simple_image_finialize;
1086 widget->size_request = simple_image_size_request;
1087 widget->expose_event = simple_image_expose;
1090 static void simple_image_init(GTypeInstance *object, gpointer gclass)
1092 GTK_WIDGET_SET_FLAGS(object, GTK_NO_WINDOW);
1095 static GType simple_image_get_type(void)
1097 static GType type = 0;
1099 if (!type)
1101 static const GTypeInfo info =
1103 sizeof (SimpleImageClass),
1104 NULL, /* base_init */
1105 NULL, /* base_finalise */
1106 simple_image_class_init,
1107 NULL, /* class_finalise */
1108 NULL, /* class_data */
1109 sizeof(SimpleImage),
1110 0, /* n_preallocs */
1111 simple_image_init,
1114 type = g_type_register_static(gtk_widget_get_type(),
1115 "SimpleImage", &info, 0);
1118 return type;
1121 GtkWidget *simple_image_new(GdkPixbuf *pixbuf)
1123 SimpleImage *image;
1125 g_return_val_if_fail(pixbuf != NULL, NULL);
1127 image = g_object_new(simple_image_get_type(), NULL);
1129 image->pixbuf = pixbuf;
1130 g_object_ref(G_OBJECT(pixbuf));
1132 image->width = gdk_pixbuf_get_width(pixbuf);
1133 image->height = gdk_pixbuf_get_height(pixbuf);
1135 return GTK_WIDGET(image);
1138 /* Whether a line l1 long starting from n1 overlaps a line l2 from n2 */
1139 inline static gboolean gui_ranges_overlap(int n1, int l1, int n2, int l2)
1141 return (n1 > n2 && n1 < n2 + l2) ||
1142 (n1 + l1 > n2 && n1 + l1 < n2 + l2) ||
1143 (n1 <= n2 && n1 + l1 >= n2 + l2);
1146 static void gui_get_monitor_adjacent(int monitor, MonitorAdjacent *adj)
1148 int m;
1150 adj->left = adj->right = adj->top = adj->bottom = FALSE;
1152 for (m = 0; m < n_monitors; ++m)
1154 if (m == monitor)
1155 continue;
1156 if (gui_ranges_overlap(monitor_geom[m].y,
1157 monitor_geom[m].height,
1158 monitor_geom[monitor].y,
1159 monitor_geom[monitor].height))
1161 if (monitor_geom[m].x < monitor_geom[monitor].x)
1163 adj->left = TRUE;
1165 else if (monitor_geom[m].x > monitor_geom[monitor].x)
1167 adj->right = TRUE;
1170 if (gui_ranges_overlap(monitor_geom[m].x,
1171 monitor_geom[m].width,
1172 monitor_geom[monitor].x,
1173 monitor_geom[monitor].width))
1175 if (monitor_geom[m].y < monitor_geom[monitor].y)
1177 adj->top = TRUE;
1179 else if (monitor_geom[m].y > monitor_geom[monitor].y)
1181 adj->bottom = TRUE;
1187 static void rox_wmspec_change_state(gboolean add, GdkWindow *window,
1188 GdkAtom state1, GdkAtom state2)
1190 GdkDisplay *display = gdk_drawable_get_display(GDK_DRAWABLE(window));
1191 XEvent xev;
1193 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
1194 #define _NET_WM_STATE_ADD 1 /* add/set property */
1195 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
1197 xev.xclient.type = ClientMessage;
1198 xev.xclient.serial = 0;
1199 xev.xclient.send_event = True;
1200 xev.xclient.window = GDK_WINDOW_XID(window);
1201 xev.xclient.message_type = gdk_x11_get_xatom_by_name_for_display(
1202 display, "_NET_WM_STATE");
1203 xev.xclient.format = 32;
1204 xev.xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1205 xev.xclient.data.l[1] = gdk_x11_atom_to_xatom_for_display(display,
1206 state1);
1207 xev.xclient.data.l[2] = gdk_x11_atom_to_xatom_for_display(display,
1208 state2);
1209 xev.xclient.data.l[3] = 0;
1210 xev.xclient.data.l[4] = 0;
1212 XSendEvent(GDK_DISPLAY_XDISPLAY(display),
1213 GDK_WINDOW_XID(
1214 gdk_screen_get_root_window(
1215 gdk_drawable_get_screen(GDK_DRAWABLE(window)))),
1216 False,
1217 SubstructureRedirectMask | SubstructureNotifyMask,
1218 &xev);
1221 void keep_below(GdkWindow *window, gboolean setting)
1223 g_return_if_fail(GDK_IS_WINDOW(window));
1225 if (GDK_WINDOW_DESTROYED(window))
1226 return;
1228 if (gdk_window_is_visible(window))
1230 if (setting)
1232 rox_wmspec_change_state(FALSE, window,
1233 gdk_atom_intern("_NET_WM_STATE_ABOVE", FALSE),
1234 GDK_NONE);
1236 rox_wmspec_change_state(setting, window,
1237 gdk_atom_intern("_NET_WM_STATE_BELOW", FALSE),
1238 GDK_NONE);
1240 #if 0
1241 else
1243 #if GTK_CHECK_VERSION(2,4,0)
1244 gdk_synthesize_window_state(window,
1245 setting ? GDK_WINDOW_STATE_ABOVE :
1246 GDK_WINDOW_STATE_BELOW,
1247 setting ? GDK_WINDOW_STATE_BELOW : 0);
1248 #endif
1250 #endif
1253 static void
1254 size_prepared_cb (GdkPixbufLoader *loader,
1255 int width,
1256 int height,
1257 gpointer data)
1259 struct {
1260 gint width;
1261 gint height;
1262 gboolean preserve_aspect_ratio;
1263 } *info = data;
1265 g_return_if_fail (width > 0 && height > 0);
1267 if(info->preserve_aspect_ratio) {
1268 if ((double)height * (double)info->width >
1269 (double)width * (double)info->height) {
1270 width = 0.5 + (double)width * (double)info->height / (double)height;
1271 height = info->height;
1272 } else {
1273 height = 0.5 + (double)height * (double)info->width / (double)width;
1274 width = info->width;
1276 } else {
1277 width = info->width;
1278 height = info->height;
1281 gdk_pixbuf_loader_set_size (loader, width, height);
1285 * rox_pixbuf_new_from_file_at_scale:
1286 * @filename: Name of file to load.
1287 * @width: The width the image should have
1288 * @height: The height the image should have
1289 * @preserve_aspect_ratio: %TRUE to preserve the image's aspect ratio
1290 * @error: Return location for an error
1292 * Creates a new pixbuf by loading an image from a file. The file format is
1293 * detected automatically. If %NULL is returned, then @error will be set.
1294 * Possible errors are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
1295 * The image will be scaled to fit in the requested size, optionally preserving
1296 * the image's aspect ratio.
1298 * Return value: A newly-created pixbuf with a reference count of 1, or %NULL
1299 * if any of several error conditions occurred: the file could not be opened,
1300 * there was no loader for the file's format, there was not enough memory to
1301 * allocate the image buffer, or the image file contained invalid data.
1303 * Taken from GTK 2.6.
1305 GdkPixbuf *
1306 rox_pixbuf_new_from_file_at_scale (const char *filename,
1307 int width,
1308 int height,
1309 gboolean preserve_aspect_ratio,
1310 GError **error)
1313 GdkPixbufLoader *loader;
1314 GdkPixbuf *pixbuf;
1316 guchar buffer [4096];
1317 int length;
1318 FILE *f;
1319 struct {
1320 gint width;
1321 gint height;
1322 gboolean preserve_aspect_ratio;
1323 } info;
1325 g_return_val_if_fail (filename != NULL, NULL);
1326 g_return_val_if_fail (width > 0 && height > 0, NULL);
1328 f = fopen (filename, "rb");
1329 if (!f) {
1330 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1331 NULL, NULL, NULL);
1332 g_set_error (error,
1333 G_FILE_ERROR,
1334 g_file_error_from_errno (errno),
1335 _("Failed to open file '%s': %s"),
1336 utf8_filename ? utf8_filename : "???",
1337 g_strerror (errno));
1338 g_free (utf8_filename);
1339 return NULL;
1342 loader = gdk_pixbuf_loader_new ();
1344 info.width = width;
1345 info.height = height;
1346 info.preserve_aspect_ratio = preserve_aspect_ratio;
1348 g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), &info);
1350 while (!feof (f) && !ferror (f)) {
1351 length = fread (buffer, 1, sizeof (buffer), f);
1352 if (length > 0)
1353 if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
1354 gdk_pixbuf_loader_close (loader, NULL);
1355 fclose (f);
1356 g_object_unref (loader);
1357 return NULL;
1361 fclose (f);
1363 if (!gdk_pixbuf_loader_close (loader, error)) {
1364 g_object_unref (loader);
1365 return NULL;
1368 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
1370 if (!pixbuf) {
1371 gchar *utf8_filename = g_filename_to_utf8 (filename, -1,
1372 NULL, NULL, NULL);
1374 g_object_unref (loader);
1376 g_set_error (error,
1377 GDK_PIXBUF_ERROR,
1378 GDK_PIXBUF_ERROR_FAILED,
1379 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1380 utf8_filename ? utf8_filename : "???");
1381 g_free (utf8_filename);
1382 return NULL;
1385 g_object_ref (pixbuf);
1387 g_object_unref (loader);
1389 return pixbuf;