Honour escape presses in the dialogue boxes.
[xtoys] / xshutdown.c
CommitLineData
90b2c5d4 1/* -*-c-*-
2 *
87dabe6f 3 * $Id: xshutdown.c,v 1.5 1998/12/03 01:00:19 mdw Exp $
90b2c5d4 4 *
5 * Pretty GTK interface to waking up an xwait
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the Edgeware X tools collection.
13 *
14 * X tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * X tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with X tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: xshutdown.c,v $
87dabe6f 32 * Revision 1.5 1998/12/03 01:00:19 mdw
33 * Honour escape presses in the dialogue boxes.
34 *
7347b0d9 35 * Revision 1.4 1998/12/03 00:39:45 mdw
36 * Force focus when starting up.
37 *
d6130abd 38 * Revision 1.3 1998/11/30 22:36:49 mdw
39 * Tidy up tabbing in help texts very slightly.
40 *
f3b35b6b 41 * Revision 1.2 1998/11/21 22:30:23 mdw
42 * Support GNU-style long options throughout, and introduce proper help
43 * text to all programs. Update manual pages to match.
44 *
90b2c5d4 45 * Revision 1.1 1998/11/16 23:00:49 mdw
46 * Initial versions.
47 *
48 */
49
50/*----- Header files ------------------------------------------------------*/
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55
56#include <X11/Xlib.h>
57#include <X11/Xutil.h>
58
59#include <gtk/gtk.h>
60#include <gdk/gdkprivate.h>
87dabe6f 61#include <gdk/gdkkeysyms.h>
90b2c5d4 62
7347b0d9 63#include "mdwfocus.h"
90b2c5d4 64#include "mdwopt.h"
f3b35b6b 65#include "quis.h"
90b2c5d4 66#include "xwait.h"
67
68/*----- Static variables --------------------------------------------------*/
69
70static char *atom = XWAIT_DIE;
71static char *msg = XWAIT_DIE_MSG;
72
73static Atom xwait_die;
74
75/*----- Main code ---------------------------------------------------------*/
76
87dabe6f 77/* --- @check_escape@ --- *
78 *
79 * Arguments: @GtkWidget *w@ = widget raising the signal
80 * @GdkEventKey *ev@ = pointer to event data
81 * @gpointer *p@ = widget to activate in response
82 *
83 * Returns: ---
84 *
85 * Use: Activates a widget when an escape keypress is detected.
86 */
87
88static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
89{
90 if (ev->keyval == GDK_Escape) {
91 if (p)
92 gtk_widget_activate(GTK_WIDGET(p));
93 else
94 gtk_object_destroy(GTK_OBJECT(w));
95 return (1);
96 }
97 return (0);
98}
99
90b2c5d4 100/* --- @cancel@ --- *
101 *
102 * Just end the main loop.
103 */
104
105static void cancel(GtkWidget *w, gpointer *p)
106{
107 gtk_main_quit();
108}
109
110/* --- @ok@ --- *
111 *
112 * Send the xwait process a message.
113 */
114
115static void ok(GtkWidget *w, gpointer *p)
116{
117 XTextProperty prop;
118 XStringListToTextProperty(&msg, 1, &prop);
119 XSetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
120 &prop, xwait_die);
121 gtk_main_quit();
122}
123
f3b35b6b 124/* --- @version@ --- */
125
126static void version(FILE *fp)
127{
128 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
129}
130
131/* --- @usage@ --- */
132
133static void usage(FILE *fp)
134{
135 fprintf(fp, "Usage: %s [-a ATOM] [-m MSG] [-p PROMPT] [-t TITLE]\n", QUIS);
136}
137
90b2c5d4 138/* --- @main@ --- *
139 *
140 * Main program.
141 */
142
143int main(int argc, char *argv[])
144{
145 char *prompt = "Are you sure you want to shut down this session?";
146 char *title = "xshutdown";
f3b35b6b 147 ego(argv[0]);
90b2c5d4 148 gtk_init(&argc, &argv);
149
150 /* --- Parse options --- */
151
152 for (;;) {
153 static struct option opt[] = {
f3b35b6b 154 { "help", 0, 0, 'h' },
155 { "usage", 0, 0, 'u' },
156 { "version", 0, 0, 'v' },
90b2c5d4 157 { "atom", required_argument, 0, 'a' },
158 { "msg", required_argument, 0, 'm' },
159 { "prompt", required_argument, 0, 'p' },
160 { "title", required_argument, 0, 't' },
161 { 0, 0, 0, 0 }
162 };
163 int i;
164
f3b35b6b 165 i = getopt_long(argc, argv, "huv a:m:p:t:", opt, 0);
90b2c5d4 166
167 if (i < 0)
168 break;
169
170 switch (i) {
f3b35b6b 171 case 'h':
172 version(stdout);
173 fputs("\n", stdout);
174 usage(stdout);
175 fputs(
176"\n"
177"Kills a waiting `xwait' process. Pops up a confirmation window first.\n"
178"\n"
179"Options available are:\n"
180"\n"
181"-h, --help Display this help text\n"
182"-u, --usage Display a short usage summary\n"
183"-v, --version Display the program's version number\n"
184"\n"
d6130abd 185"-a, --atom=ATOM\t Select the atom that `xwait' is waiting for\n"
f3b35b6b 186"-m, --msg=MSG Select the message to send to `xwait'\n"
187"-p, --prompt=PROMPT Select the prompt string in the confirmation box\n"
188"-t, --title=TITLE Select the title string in the confirmation box\n",
189 stdout);
190 exit(0);
191 break;
192 case 'u':
193 usage(stdout);
194 exit(0);
195 break;
196 case 'v':
197 version(stdout);
198 exit(0);
199 break;
200
90b2c5d4 201 case 'a':
202 atom = optarg;
203 break;
204 case 'm':
205 msg = optarg;
206 break;
207 case 'p':
208 prompt = optarg;
209 break;
210 case 't':
211 title = optarg;
212 break;
213 default:
f3b35b6b 214 usage(stderr);
90b2c5d4 215 exit(EXIT_FAILURE);
216 }
217 }
218
219 xwait_die = XInternAtom(gdk_display, atom, False);
220
221 /* --- Decide whether there's an xwait listening --- *
222 *
223 * If not, pop up an error box and quit.
224 */
225
226 {
227 XTextProperty prop;
228
229 if (!XGetTextProperty(gdk_display, DefaultRootWindow(gdk_display),
230 &prop, xwait_die)) {
231 char buf[64];
232 GtkWidget *win = gtk_dialog_new();
233 GtkWidget *w;
234
235 /* --- Make the main window --- */
236
237 gtk_window_set_title(GTK_WINDOW(win), "xshutdown");
238 gtk_signal_connect(GTK_OBJECT(win), "destroy",
239 GTK_SIGNAL_FUNC(cancel), 0);
87dabe6f 240 gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
241 GTK_SIGNAL_FUNC(check_escape), 0);
90b2c5d4 242
243 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
244
245 /* --- Make the label --- */
246
247 sprintf(buf, "no xwait listening for `%s'\n", atom);
248 w = gtk_label_new(buf);
249 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
250 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
251 gtk_widget_show(w);
252
253 /* --- Make the little button --- */
254
255 w = gtk_button_new_with_label("OK");
256 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
257 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
258 gtk_widget_grab_default(w);
259 gtk_signal_connect(GTK_OBJECT(w), "clicked",
260 GTK_SIGNAL_FUNC(cancel), 0);
261 gtk_widget_show(w);
262
263 /* --- Make everything work --- */
264
7347b0d9 265 gtk_widget_realize(win);
266 mdwfocus(win);
90b2c5d4 267 gtk_widget_show(win);
268 gtk_main();
269 exit(EXIT_FAILURE);
270 }
271 }
272
273 /* --- Main code --- */
274
275 {
276 GtkWidget *win;
277 GtkWidget *w;
278
279 /* --- Make the main dialogue box --- */
280
281 win = gtk_dialog_new();
282 gtk_window_set_title(GTK_WINDOW(win), title);
283 gtk_signal_connect(GTK_OBJECT(win), "destroy",
284 GTK_SIGNAL_FUNC(cancel), 0);
285
286 gtk_box_set_homogeneous(GTK_BOX(GTK_DIALOG(win)->action_area), 0);
287
288 /* --- Make the prompt label --- */
289
290 w = gtk_label_new(prompt);
291 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(win)->vbox), w, 1, 1, 0);
292 gtk_misc_set_padding(GTK_MISC(w), 8, 8);
293 gtk_widget_show(w);
294
295 /* --- Make the OK button --- */
296
297 w = gtk_button_new_with_label("OK");
298 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
299 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(ok), 0);
300 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
301 gtk_widget_show(w);
302 gtk_widget_grab_default(w);
303
304 /* --- And the cancel button --- */
305
306 w = gtk_button_new_with_label("Cancel");
307 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(win)->action_area), w, 0, 0, 0);
308 gtk_signal_connect(GTK_OBJECT(w), "clicked", GTK_SIGNAL_FUNC(cancel), 0);
309 GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
310 gtk_widget_show(w);
311
312 /* --- Show the completed window --- */
313
87dabe6f 314 gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
315 GTK_SIGNAL_FUNC(check_escape), 0);
7347b0d9 316 gtk_widget_realize(win);
317 mdwfocus(win);
90b2c5d4 318 gtk_widget_show(win);
319 }
320
321 /* --- Let rip --- */
322
323 gtk_main();
324 return (0);
325}
326
327/*----- That's all, folks -------------------------------------------------*/