r195: The effective permissions are now underlined rather than uppercased.
[rox-filer.git] / ROX-Filer / src / gui_support.c
blob9d5e6a6d9df2fbdd3cd0a35fecccf46f67924307
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, 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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/param.h>
28 #include <stdarg.h>
29 #include <errno.h>
31 #include <glib.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xatom.h>
34 #include <gdk/gdk.h>
36 #include "main.h"
37 #include "gui_support.h"
39 GdkFont *fixed_font = NULL;
40 GtkStyle *fixed_style = NULL;
41 gint fixed_width;
43 static GdkAtom xa_cardinal;
45 void gui_support_init()
47 fixed_font = gdk_font_load("fixed");
49 fixed_style = gtk_style_copy(gtk_widget_get_default_style());
50 fixed_style->font = fixed_font;
52 fixed_width = gdk_string_width(fixed_font, "m");
54 xa_cardinal = gdk_atom_intern("CARDINAL", FALSE);
57 static void choice_clicked(GtkWidget *widget, gpointer number)
59 int *choice_return;
61 choice_return = gtk_object_get_data(GTK_OBJECT(widget),
62 "choice_return");
64 if (choice_return)
65 *choice_return = (int) number;
68 /* Open a modal dialog box showing a message.
69 * The user can choose from a selection of buttons at the bottom.
70 * Returns -1 if the window is destroyed, or the number of the button
71 * if one is clicked (starting from zero).
73 int get_choice(char *title,
74 char *message,
75 int number_of_buttons, ...)
77 GtkWidget *dialog;
78 GtkWidget *vbox, *action_area, *separator;
79 GtkWidget *text, *text_container;
80 GtkWidget *button = NULL;
81 int i, retval;
82 va_list ap;
83 int choice_return;
85 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
86 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
87 gtk_window_set_title(GTK_WINDOW(dialog), title);
88 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
90 vbox = gtk_vbox_new(FALSE, 0);
91 gtk_container_add(GTK_CONTAINER(dialog), vbox);
93 action_area = gtk_hbox_new(TRUE, 5);
94 gtk_container_set_border_width(GTK_CONTAINER(action_area), 10);
95 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
97 separator = gtk_hseparator_new ();
98 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
100 text = gtk_label_new(message);
101 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
102 text_container = gtk_event_box_new();
103 gtk_container_set_border_width(GTK_CONTAINER(text_container), 32);
104 gtk_container_add(GTK_CONTAINER(text_container), text);
106 gtk_box_pack_start(GTK_BOX(vbox),
107 text_container,
108 TRUE, TRUE, 0);
110 va_start(ap, number_of_buttons);
112 for (i = 0; i < number_of_buttons; i++)
114 button = gtk_button_new_with_label(va_arg(ap, char *));
115 gtk_object_set_data(GTK_OBJECT(button), "choice_return",
116 &choice_return);
117 gtk_box_pack_start(GTK_BOX(action_area),
118 button,
119 TRUE, TRUE, 16);
120 gtk_signal_connect(GTK_OBJECT(button), "clicked",
121 choice_clicked, (gpointer) i);
122 if (i == 0)
123 gtk_window_set_focus(GTK_WINDOW(dialog), button);
125 if (!i)
126 gtk_widget_grab_focus(button);
128 va_end(ap);
130 gtk_object_set_data(GTK_OBJECT(dialog), "choice_return",
131 &choice_return);
132 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", choice_clicked,
133 (gpointer) -1);
135 choice_return = -2;
137 gtk_widget_show_all(dialog);
139 while (choice_return == -2)
140 g_main_iteration(TRUE);
142 retval = choice_return;
144 if (retval != -1)
145 gtk_widget_destroy(dialog);
147 return retval;
150 /* Display a message in a window */
151 void report_error(char *title, char *message)
153 g_return_if_fail(message != NULL);
155 if (!title)
156 title = "Error";
158 get_choice(title, message, 1, "OK");
161 void set_cardinal_property(GdkWindow *window, GdkAtom prop, guint32 value)
163 gdk_property_change(window, prop, xa_cardinal, 32,
164 GDK_PROP_MODE_REPLACE, (gchar *) &value, 1);
167 void make_panel_window(GdkWindow *window)
169 static gboolean need_init = TRUE;
170 static GdkAtom xa_state;
172 if (need_init)
174 xa_state = gdk_atom_intern("_WIN_STATE", FALSE);
175 need_init = FALSE;
178 gdk_window_set_decorations(window, 0);
179 gdk_window_set_functions(window, 0);
181 set_cardinal_property(window, xa_state,
182 WIN_STATE_STICKY | WIN_STATE_HIDDEN |
183 WIN_STATE_FIXED_POSITION | WIN_STATE_ARRANGE_IGNORE);
186 gint hide_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer window)
188 gtk_widget_hide((GtkWidget *) window);
190 return TRUE;
193 static gboolean error_idle_cb(gpointer data)
195 char **error = (char **) data;
197 report_error(error[0], error[1]);
198 g_free(error[0]);
199 g_free(error[1]);
200 error[0] = error[1] = NULL;
202 if (--number_of_windows == 0)
203 gtk_main_quit();
205 return FALSE;
208 /* Display an error next time we are idle */
209 void delayed_error(char *title, char *error)
211 static char *delayed_error_data[2] = {NULL, NULL};
212 gboolean already_open;
214 g_return_if_fail(error != NULL);
216 already_open = delayed_error_data[1] != NULL;
218 g_free(delayed_error_data[0]);
219 g_free(delayed_error_data[1]);
221 delayed_error_data[0] = g_strdup(title);
222 delayed_error_data[1] = g_strdup(error);
224 if (already_open)
225 return;
227 gtk_idle_add(error_idle_cb, delayed_error_data);
229 number_of_windows++;
232 /* Load the file into memory. Return TRUE on success.
233 * Block is zero terminated (but this is not included in the length).
235 gboolean load_file(char *pathname, char **data_out, long *length_out)
237 FILE *file;
238 long length;
239 char *buffer;
240 gboolean retval = FALSE;
242 file = fopen(pathname, "r");
244 if (!file)
246 delayed_error("Opening file for DND", g_strerror(errno));
247 return FALSE;
250 fseek(file, 0, SEEK_END);
251 length = ftell(file);
253 buffer = malloc(length + 1);
254 if (buffer)
256 fseek(file, 0, SEEK_SET);
257 fread(buffer, 1, length, file);
259 if (ferror(file))
261 delayed_error("Loading file for DND",
262 g_strerror(errno));
263 g_free(buffer);
265 else
267 *data_out = buffer;
268 *length_out = length;
269 buffer[length] = '\0';
270 retval = TRUE;
273 else
274 delayed_error("Loading file for DND",
275 "Can't allocate memory for buffer to "
276 "transfer this file");
278 fclose(file);
280 return retval;