r167: Much improved keyboard shortcuts.
[rox-filer.git] / ROX-Filer / src / gui_support.c
blob84558e8a9d894dd2a82f8d991762911ef7417f65
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 static GdkAtom xa_cardinal;
41 void gui_support_init()
43 xa_cardinal = gdk_atom_intern("CARDINAL", FALSE);
46 static void choice_clicked(GtkWidget *widget, gpointer number)
48 int *choice_return;
50 choice_return = gtk_object_get_data(GTK_OBJECT(widget),
51 "choice_return");
53 if (choice_return)
54 *choice_return = (int) number;
57 /* Open a modal dialog box showing a message.
58 * The user can choose from a selection of buttons at the bottom.
59 * Returns -1 if the window is destroyed, or the number of the button
60 * if one is clicked (starting from zero).
62 int get_choice(char *title,
63 char *message,
64 int number_of_buttons, ...)
66 GtkWidget *dialog;
67 GtkWidget *vbox, *action_area, *separator;
68 GtkWidget *text, *text_container;
69 GtkWidget *button = NULL;
70 int i, retval;
71 va_list ap;
72 int choice_return;
74 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
75 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
76 gtk_window_set_title(GTK_WINDOW(dialog), title);
77 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
79 vbox = gtk_vbox_new(FALSE, 0);
80 gtk_container_add(GTK_CONTAINER(dialog), vbox);
82 action_area = gtk_hbox_new(TRUE, 5);
83 gtk_container_set_border_width(GTK_CONTAINER(action_area), 10);
84 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
86 separator = gtk_hseparator_new ();
87 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
89 text = gtk_label_new(message);
90 gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
91 text_container = gtk_event_box_new();
92 gtk_container_set_border_width(GTK_CONTAINER(text_container), 32);
93 gtk_container_add(GTK_CONTAINER(text_container), text);
95 gtk_box_pack_start(GTK_BOX(vbox),
96 text_container,
97 TRUE, TRUE, 0);
99 va_start(ap, number_of_buttons);
101 for (i = 0; i < number_of_buttons; i++)
103 button = gtk_button_new_with_label(va_arg(ap, char *));
104 gtk_object_set_data(GTK_OBJECT(button), "choice_return",
105 &choice_return);
106 gtk_box_pack_start(GTK_BOX(action_area),
107 button,
108 TRUE, TRUE, 16);
109 gtk_signal_connect(GTK_OBJECT(button), "clicked",
110 choice_clicked, (gpointer) i);
111 if (i == 0)
112 gtk_window_set_focus(GTK_WINDOW(dialog), button);
114 if (!i)
115 gtk_widget_grab_focus(button);
117 va_end(ap);
119 gtk_object_set_data(GTK_OBJECT(dialog), "choice_return",
120 &choice_return);
121 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", choice_clicked,
122 (gpointer) -1);
124 choice_return = -2;
126 gtk_widget_show_all(dialog);
128 while (choice_return == -2)
129 g_main_iteration(TRUE);
131 retval = choice_return;
133 if (retval != -1)
134 gtk_widget_destroy(dialog);
136 return retval;
139 /* Display a message in a window */
140 void report_error(char *title, char *message)
142 g_return_if_fail(message != NULL);
144 if (!title)
145 title = "Error";
147 get_choice(title, message, 1, "OK");
150 void set_cardinal_property(GdkWindow *window, GdkAtom prop, guint32 value)
152 gdk_property_change(window, prop, xa_cardinal, 32,
153 GDK_PROP_MODE_REPLACE, (gchar *) &value, 1);
156 void make_panel_window(GdkWindow *window)
158 static gboolean need_init = TRUE;
159 static GdkAtom xa_state;
161 if (need_init)
163 xa_state = gdk_atom_intern("_WIN_STATE", FALSE);
164 need_init = FALSE;
167 gdk_window_set_decorations(window, 0);
168 gdk_window_set_functions(window, 0);
170 set_cardinal_property(window, xa_state,
171 WIN_STATE_STICKY | WIN_STATE_HIDDEN |
172 WIN_STATE_FIXED_POSITION | WIN_STATE_ARRANGE_IGNORE);
175 gint hide_dialog_event(GtkWidget *widget, GdkEvent *event, gpointer window)
177 gtk_widget_hide((GtkWidget *) window);
179 return TRUE;
182 static gboolean error_idle_cb(gpointer data)
184 char **error = (char **) data;
186 report_error(error[0], error[1]);
187 g_free(error[0]);
188 g_free(error[1]);
189 error[0] = error[1] = NULL;
191 if (--number_of_windows == 0)
192 gtk_main_quit();
194 return FALSE;
197 /* Display an error next time we are idle */
198 void delayed_error(char *title, char *error)
200 static char *delayed_error_data[2] = {NULL, NULL};
201 gboolean already_open;
203 g_return_if_fail(error != NULL);
205 already_open = delayed_error_data[1] != NULL;
207 g_free(delayed_error_data[0]);
208 g_free(delayed_error_data[1]);
210 delayed_error_data[0] = g_strdup(title);
211 delayed_error_data[1] = g_strdup(error);
213 if (already_open)
214 return;
216 gtk_idle_add(error_idle_cb, delayed_error_data);
218 number_of_windows++;
221 /* Load the file into memory. Return TRUE on success.
222 * Block is zero terminated (but this is not included in the length).
224 gboolean load_file(char *pathname, char **data_out, long *length_out)
226 FILE *file;
227 long length;
228 char *buffer;
229 gboolean retval = FALSE;
231 file = fopen(pathname, "r");
233 if (!file)
235 delayed_error("Opening file for DND", g_strerror(errno));
236 return FALSE;
239 fseek(file, 0, SEEK_END);
240 length = ftell(file);
242 buffer = malloc(length + 1);
243 if (buffer)
245 fseek(file, 0, SEEK_SET);
246 fread(buffer, 1, length, file);
248 if (ferror(file))
250 delayed_error("Loading file for DND",
251 g_strerror(errno));
252 g_free(buffer);
254 else
256 *data_out = buffer;
257 *length_out = length;
258 buffer[length] = '\0';
259 retval = TRUE;
262 else
263 delayed_error("Loading file for DND",
264 "Can't allocate memory for buffer to "
265 "transfer this file");
267 fclose(file);
269 return retval;