r210: Added 'Permissions' (chmod) feature.
[rox-filer.git] / ROX-Filer / src / gui_support.c
blob674b7e04f18ac12451c7ec110509fd2c7a6dc3ca
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 <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 *item_font = NULL;
40 GdkFont *fixed_font = NULL;
41 GtkStyle *fixed_style = NULL;
42 gint fixed_width;
44 static GdkAtom xa_cardinal;
46 void gui_support_init()
48 fixed_font = gdk_font_load("fixed");
49 item_font = gtk_widget_get_default_style()->font;
51 fixed_style = gtk_style_copy(gtk_widget_get_default_style());
52 fixed_style->font = fixed_font;
54 fixed_width = gdk_string_width(fixed_font, "m");
56 xa_cardinal = gdk_atom_intern("CARDINAL", FALSE);
59 static void choice_clicked(GtkWidget *widget, gpointer number)
61 int *choice_return;
63 choice_return = gtk_object_get_data(GTK_OBJECT(widget),
64 "choice_return");
66 if (choice_return)
67 *choice_return = (int) number;
70 /* Open a modal dialog box showing a message.
71 * The user can choose from a selection of buttons at the bottom.
72 * Returns -1 if the window is destroyed, or the number of the button
73 * if one is clicked (starting from zero).
75 int get_choice(char *title,
76 char *message,
77 int number_of_buttons, ...)
79 GtkWidget *dialog;
80 GtkWidget *vbox, *action_area, *separator;
81 GtkWidget *text, *text_container;
82 GtkWidget *button = NULL;
83 int i, retval;
84 va_list ap;
85 int choice_return;
87 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
88 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
89 gtk_window_set_title(GTK_WINDOW(dialog), title);
90 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
92 vbox = gtk_vbox_new(FALSE, 0);
93 gtk_container_add(GTK_CONTAINER(dialog), vbox);
95 action_area = gtk_hbox_new(TRUE, 5);
96 gtk_container_set_border_width(GTK_CONTAINER(action_area), 10);
97 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
99 separator = gtk_hseparator_new ();
100 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
102 text = gtk_label_new(message);
103 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
104 text_container = gtk_event_box_new();
105 gtk_container_set_border_width(GTK_CONTAINER(text_container), 32);
106 gtk_container_add(GTK_CONTAINER(text_container), text);
108 gtk_box_pack_start(GTK_BOX(vbox),
109 text_container,
110 TRUE, TRUE, 0);
112 va_start(ap, number_of_buttons);
114 for (i = 0; i < number_of_buttons; i++)
116 button = gtk_button_new_with_label(va_arg(ap, char *));
117 gtk_object_set_data(GTK_OBJECT(button), "choice_return",
118 &choice_return);
119 gtk_box_pack_start(GTK_BOX(action_area),
120 button,
121 TRUE, TRUE, 16);
122 gtk_signal_connect(GTK_OBJECT(button), "clicked",
123 choice_clicked, (gpointer) i);
124 if (i == 0)
125 gtk_window_set_focus(GTK_WINDOW(dialog), button);
127 if (!i)
128 gtk_widget_grab_focus(button);
130 va_end(ap);
132 gtk_object_set_data(GTK_OBJECT(dialog), "choice_return",
133 &choice_return);
134 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", choice_clicked,
135 (gpointer) -1);
137 choice_return = -2;
139 gtk_widget_show_all(dialog);
141 while (choice_return == -2)
142 g_main_iteration(TRUE);
144 retval = choice_return;
146 if (retval != -1)
147 gtk_widget_destroy(dialog);
149 return retval;
152 /* Display a message in a window */
153 void report_error(char *title, char *message)
155 g_return_if_fail(message != NULL);
157 if (!title)
158 title = "Error";
160 get_choice(title, message, 1, "OK");
163 void set_cardinal_property(GdkWindow *window, GdkAtom prop, guint32 value)
165 gdk_property_change(window, prop, xa_cardinal, 32,
166 GDK_PROP_MODE_REPLACE, (gchar *) &value, 1);
169 void make_panel_window(GdkWindow *window)
171 static gboolean need_init = TRUE;
172 static GdkAtom xa_state;
174 if (need_init)
176 xa_state = gdk_atom_intern("_WIN_STATE", FALSE);
177 need_init = FALSE;
180 gdk_window_set_decorations(window, 0);
181 gdk_window_set_functions(window, 0);
183 set_cardinal_property(window, xa_state,
184 WIN_STATE_STICKY | WIN_STATE_HIDDEN |
185 WIN_STATE_FIXED_POSITION | WIN_STATE_ARRANGE_IGNORE);
188 gint hide_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer window)
190 gtk_widget_hide((GtkWidget *) window);
192 return TRUE;
195 static gboolean error_idle_cb(gpointer data)
197 char **error = (char **) data;
199 report_error(error[0], error[1]);
200 g_free(error[0]);
201 g_free(error[1]);
202 error[0] = error[1] = NULL;
204 if (--number_of_windows == 0)
205 gtk_main_quit();
207 return FALSE;
210 /* Display an error next time we are idle */
211 void delayed_error(char *title, char *error)
213 static char *delayed_error_data[2] = {NULL, NULL};
214 gboolean already_open;
216 g_return_if_fail(error != NULL);
218 already_open = delayed_error_data[1] != NULL;
220 g_free(delayed_error_data[0]);
221 g_free(delayed_error_data[1]);
223 delayed_error_data[0] = g_strdup(title);
224 delayed_error_data[1] = g_strdup(error);
226 if (already_open)
227 return;
229 gtk_idle_add(error_idle_cb, delayed_error_data);
231 number_of_windows++;
234 /* Load the file into memory. Return TRUE on success.
235 * Block is zero terminated (but this is not included in the length).
237 gboolean load_file(char *pathname, char **data_out, long *length_out)
239 FILE *file;
240 long length;
241 char *buffer;
242 gboolean retval = FALSE;
244 file = fopen(pathname, "r");
246 if (!file)
248 delayed_error("Opening file for DND", g_strerror(errno));
249 return FALSE;
252 fseek(file, 0, SEEK_END);
253 length = ftell(file);
255 buffer = malloc(length + 1);
256 if (buffer)
258 fseek(file, 0, SEEK_SET);
259 fread(buffer, 1, length, file);
261 if (ferror(file))
263 delayed_error("Loading file for DND",
264 g_strerror(errno));
265 g_free(buffer);
267 else
269 *data_out = buffer;
270 *length_out = length;
271 buffer[length] = '\0';
272 retval = TRUE;
275 else
276 delayed_error("Loading file for DND",
277 "Can't allocate memory for buffer to "
278 "transfer this file");
280 fclose(file);
282 return retval;