r232: Added 'prune' and 'system' find commands.
[rox-filer.git] / ROX-Filer / src / gui_support.c
blobe6766d27797f3da9db8a8c8ef0f59b0aef82beaf
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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>
33 #include <glib.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <gdk/gdk.h>
38 #include "main.h"
39 #include "gui_support.h"
41 GdkFont *item_font = NULL;
42 GdkFont *fixed_font = NULL;
43 GtkStyle *fixed_style = NULL;
44 gint fixed_width;
46 static GdkAtom xa_cardinal;
48 void gui_support_init()
50 fixed_font = gdk_font_load("fixed");
51 item_font = gtk_widget_get_default_style()->font;
53 fixed_style = gtk_style_copy(gtk_widget_get_default_style());
54 fixed_style->font = fixed_font;
56 fixed_width = gdk_string_width(fixed_font, "m");
58 xa_cardinal = gdk_atom_intern("CARDINAL", FALSE);
61 static void choice_clicked(GtkWidget *widget, gpointer number)
63 int *choice_return;
65 choice_return = gtk_object_get_data(GTK_OBJECT(widget),
66 "choice_return");
68 if (choice_return)
69 *choice_return = (int) number;
72 /* Open a modal dialog box showing a message.
73 * The user can choose from a selection of buttons at the bottom.
74 * Returns -1 if the window is destroyed, or the number of the button
75 * if one is clicked (starting from zero).
77 int get_choice(char *title,
78 char *message,
79 int number_of_buttons, ...)
81 GtkWidget *dialog;
82 GtkWidget *vbox, *action_area, *separator;
83 GtkWidget *text, *text_container;
84 GtkWidget *button = NULL;
85 int i, retval;
86 va_list ap;
87 int choice_return;
89 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
90 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
91 gtk_window_set_title(GTK_WINDOW(dialog), title);
92 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
94 vbox = gtk_vbox_new(FALSE, 0);
95 gtk_container_add(GTK_CONTAINER(dialog), vbox);
97 action_area = gtk_hbox_new(TRUE, 5);
98 gtk_container_set_border_width(GTK_CONTAINER(action_area), 10);
99 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
101 separator = gtk_hseparator_new ();
102 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
104 text = gtk_label_new(message);
105 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
106 text_container = gtk_event_box_new();
107 gtk_container_set_border_width(GTK_CONTAINER(text_container), 32);
108 gtk_container_add(GTK_CONTAINER(text_container), text);
110 gtk_box_pack_start(GTK_BOX(vbox),
111 text_container,
112 TRUE, TRUE, 0);
114 va_start(ap, number_of_buttons);
116 for (i = 0; i < number_of_buttons; i++)
118 button = gtk_button_new_with_label(va_arg(ap, char *));
119 gtk_object_set_data(GTK_OBJECT(button), "choice_return",
120 &choice_return);
121 gtk_box_pack_start(GTK_BOX(action_area),
122 button,
123 TRUE, TRUE, 16);
124 gtk_signal_connect(GTK_OBJECT(button), "clicked",
125 choice_clicked, (gpointer) i);
126 if (i == 0)
127 gtk_window_set_focus(GTK_WINDOW(dialog), button);
129 if (!i)
130 gtk_widget_grab_focus(button);
132 va_end(ap);
134 gtk_object_set_data(GTK_OBJECT(dialog), "choice_return",
135 &choice_return);
136 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", choice_clicked,
137 (gpointer) -1);
139 choice_return = -2;
141 gtk_widget_show_all(dialog);
143 while (choice_return == -2)
144 g_main_iteration(TRUE);
146 retval = choice_return;
148 if (retval != -1)
149 gtk_widget_destroy(dialog);
151 return retval;
154 /* Display a message in a window */
155 void report_error(char *title, char *message)
157 g_return_if_fail(message != NULL);
159 if (!title)
160 title = "Error";
162 get_choice(title, message, 1, "OK");
165 void set_cardinal_property(GdkWindow *window, GdkAtom prop, guint32 value)
167 gdk_property_change(window, prop, xa_cardinal, 32,
168 GDK_PROP_MODE_REPLACE, (gchar *) &value, 1);
171 void make_panel_window(GdkWindow *window)
173 static gboolean need_init = TRUE;
174 static GdkAtom xa_state;
176 if (need_init)
178 xa_state = gdk_atom_intern("_WIN_STATE", FALSE);
179 need_init = FALSE;
182 gdk_window_set_decorations(window, 0);
183 gdk_window_set_functions(window, 0);
185 set_cardinal_property(window, xa_state,
186 WIN_STATE_STICKY | WIN_STATE_HIDDEN |
187 WIN_STATE_FIXED_POSITION | WIN_STATE_ARRANGE_IGNORE);
190 gint hide_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer window)
192 gtk_widget_hide((GtkWidget *) window);
194 return TRUE;
197 static gboolean error_idle_cb(gpointer data)
199 char **error = (char **) data;
201 report_error(error[0], error[1]);
202 g_free(error[0]);
203 g_free(error[1]);
204 error[0] = error[1] = NULL;
206 if (--number_of_windows == 0)
207 gtk_main_quit();
209 return FALSE;
212 /* Display an error next time we are idle */
213 void delayed_error(char *title, char *error)
215 static char *delayed_error_data[2] = {NULL, NULL};
216 gboolean already_open;
218 g_return_if_fail(error != NULL);
220 already_open = delayed_error_data[1] != NULL;
222 g_free(delayed_error_data[0]);
223 g_free(delayed_error_data[1]);
225 delayed_error_data[0] = g_strdup(title);
226 delayed_error_data[1] = g_strdup(error);
228 if (already_open)
229 return;
231 gtk_idle_add(error_idle_cb, delayed_error_data);
233 number_of_windows++;
236 /* Load the file into memory. Return TRUE on success.
237 * Block is zero terminated (but this is not included in the length).
239 gboolean load_file(char *pathname, char **data_out, long *length_out)
241 FILE *file;
242 long length;
243 char *buffer;
244 gboolean retval = FALSE;
246 file = fopen(pathname, "r");
248 if (!file)
250 delayed_error("Opening file for DND", g_strerror(errno));
251 return FALSE;
254 fseek(file, 0, SEEK_END);
255 length = ftell(file);
257 buffer = malloc(length + 1);
258 if (buffer)
260 fseek(file, 0, SEEK_SET);
261 fread(buffer, 1, length, file);
263 if (ferror(file))
265 delayed_error("Loading file for DND",
266 g_strerror(errno));
267 g_free(buffer);
269 else
271 *data_out = buffer;
272 *length_out = length;
273 buffer[length] = '\0';
274 retval = TRUE;
277 else
278 delayed_error("Loading file for DND",
279 "Can't allocate memory for buffer to "
280 "transfer this file");
282 fclose(file);
284 return retval;