chiark / gitweb /
a7c505fb06bc04a61767969a625243eb047b3a3a
[xtoys] / xgetline.c
1 /* -*-c-*-
2  *
3  * $Id: xgetline.c,v 1.5 1998/12/03 00:39:44 mdw Exp $
4  *
5  * Fetch a line of text from the user
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: xgetline.c,v $
32  * Revision 1.5  1998/12/03 00:39:44  mdw
33  * Force focus when starting up.
34  *
35  * Revision 1.4  1998/11/30 22:36:47  mdw
36  * Tidy up tabbing in help texts very slightly.
37  *
38  * Revision 1.3  1998/11/21 22:30:20  mdw
39  * Support GNU-style long options throughout, and introduce proper help
40  * text to all programs.  Update manual pages to match.
41  *
42  * Revision 1.2  1998/11/18 21:25:30  mdw
43  * Remove bogus `-h' option from the options list.
44  *
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 <gtk/gtk.h>
57 #include <gdk/gdkkeysyms.h>
58
59 #include "mdwfocus.h"
60 #include "mdwopt.h"
61 #include "quis.h"
62
63 /*----- Main code ---------------------------------------------------------*/
64
65 /* --- @cancel@ --- *
66  *
67  * Arguments:   @GtkWidget *w@ = widget raising the signal
68  *              @gpointer *p@ = pointer to integer result code
69  *
70  * Returns:     ---
71  *
72  * Use:         Sets the result code to zero (failure) and ends the loop.
73  */
74
75 static void cancel(GtkWidget *w, gpointer *p)
76 {
77   int *ip = (int *)p;
78   *ip = 0;
79   gtk_main_quit();
80 }
81
82 /* --- @done@ --- *
83  *
84  * Arguments:   @GtkWidget *w@ = widget raising the signal
85  *              @gpointer *p@ = pointer to integer result code
86  *
87  * Returns:     ---
88  *
89  * Use:         Sets the result code nonzero (success) and ends the loop.
90  */
91
92 static void done(GtkWidget *w, gpointer *p)
93 {
94   int *ip = (int *)p;
95   *ip = 1;
96   gtk_main_quit();
97 }
98
99 /* --- @check_escape@ --- *
100  *
101  * Arguments:   @GtkWidget *w@ = widget raising the signal
102  *              @GdkEventKey *ev@ = pointer to event data
103  *              @gpointer *p@ = widget to activate in response
104  *
105  * Returns:     ---
106  *
107  * Use:         Activates a widget when an escape keypress is detected.
108  */
109
110 static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
111 {
112   if (ev->keyval == GDK_Escape) {
113     if (p)
114       gtk_widget_activate(GTK_WIDGET(p));
115     else
116       gtk_object_destroy(GTK_OBJECT(w));
117     return (1);
118   }
119   return (0);
120 }
121
122 /* --- @version@ --- *
123  *
124  * Arguments:   @FILE *fp@ = output stream to print the message on
125  *
126  * Returns:     ---
127  *
128  * Use:         Spits out a version message.
129  */
130
131 static void version(FILE *fp)
132 {
133   fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
134 }
135
136 /* --- @usage@ --- *
137  *
138  * Arguments:   @FILE *fp@ = output stream to print the message on
139  *
140  * Returns:     ---
141  *
142  * Use:         Spits out a usage message.
143  */
144
145 static void usage(FILE *fp)
146 {
147   fprintf(fp, "Usage: %s [-i] [-t title] [-p prompt] [-d default]\n", QUIS);
148 }
149
150 /* --- @main@ --- *
151  *
152  * Arguments:   @int argc@ = number of command line arguments
153  *              @char *argv[]@ = addresses of arguments
154  *
155  * Returns:     Zero if OK, and we read a string; nonzero if the user
156  *              cancelled.
157  *
158  * Use:         Reads a string from the user, and returns it on standard
159  *              output.
160  */
161
162 int main(int argc, char *argv[])
163 {
164   /* --- Configuration variables --- */
165
166   char *prompt = 0;
167   char *dfl = "";
168   char *title = "Input request";
169   int left;
170   unsigned f = 0;
171   int ok = 0;
172
173   enum {
174     f_invis = 1,
175     f_duff = 2
176   };
177
178   /* --- User interface bits --- */
179
180   GtkWidget *win;
181   GtkWidget *box;
182   GtkWidget *entry;
183   GtkWidget *btn;
184
185   /* --- Crank up the toolkit --- *
186    *
187    * Have to do this here: GTK snarfs some command line options which my
188    * parser would barf about.
189    */   
190
191   ego(argv[0]);
192   gtk_init(&argc, &argv);
193
194   /* --- Parse options from command line --- */
195
196   for (;;) {
197
198     /* --- Long options structure --- */
199
200     static struct option opt[] = {
201       { "help",         0,                      0,      'h' },
202       { "usage",        0,                      0,      'u' },
203       { "version",      0,                      0,      'v' },
204       { "title",        required_argument,      0,      't' },
205       { "prompt",       required_argument,      0,      'p' },
206       { "default",      required_argument,      0,      'd' },
207       { "password",     0,                      0,      'i' },
208       { "invisible",    0,                      0,      'i' },
209       { 0,              0,                      0,      0 }
210     };
211     int i;
212
213     /* --- Fetch an option --- */
214
215     i = getopt_long(argc, argv, "huv t:p:d:i", opt, 0);
216     if (i < 0)
217       break;
218
219     /* --- Work out what to do with it --- */
220
221     switch (i) {
222       case 'h':
223         version(stdout);
224         fputs("\n", stdout);
225         usage(stdout);
226         fputs(
227 "\n"
228 "Pops up a small window requesting input from a user, and echoes the\n"
229 "response to stdout, where it can be collected by a shell script.\n"
230 "\n"
231 "Options available are:\n"
232 "\n"
233 "-h, --help             Display this help text\n"
234 "-u, --usage            Display a short usage summary\n"
235 "-v, --version          Display the program's version number\n"
236 "\n"
237 "-i, --invisible\t      Don't show the user's string as it's typed\n"
238 "-t, --title=TITLE      Set the window's title string\n"
239 "-p, --prompt=PROMPT    Set the window's prompt string\n"
240 "-d, --default=DEFAULT  Set the default string already in the window\n",
241           stdout);
242         exit(0);
243         break;
244       case 'u':
245         usage(stdout);
246         exit(0);
247         break;
248       case 'v':
249         version(stdout);
250         exit(0);
251         break;
252         
253       case 't':
254         title = optarg;
255         break;
256       case 'p':
257         prompt = optarg;
258         break;
259       case 'd':
260         dfl = optarg;
261         break;
262       case 'i':
263         f |= f_invis;
264         break;
265       default:
266         f |= f_duff;
267         break;
268     }
269   }
270
271   if (f & f_duff) {
272     usage(stderr);
273     exit(EXIT_FAILURE);
274   }
275
276   /* --- Create the main window --- */
277
278   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
279   gtk_window_set_title(GTK_WINDOW(win), title);
280   gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
281   gtk_signal_connect(GTK_OBJECT(win), "destroy",
282                      GTK_SIGNAL_FUNC(cancel), &ok);
283
284   /* --- Create the box for laying out the widgets inside --- */
285
286   left = (prompt ? 1 : 0);
287   box = gtk_table_new(left + 2, 1, 0);
288
289   /* --- Maybe create a prompt widget --- */
290
291   if (prompt) {
292     GtkWidget *w = gtk_label_new(prompt);
293     gtk_table_attach(GTK_TABLE(box), w,
294                      0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
295     gtk_widget_show(w);
296   }
297
298   /* --- Create the entry widget --- */
299
300   entry = gtk_entry_new();
301   gtk_entry_set_text(GTK_ENTRY(entry), dfl);
302   gtk_table_attach(GTK_TABLE(box), entry,
303                    left, left + 1, 0, 1,
304                    GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
305   if (f & f_invis)
306     gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
307   gtk_widget_show(entry);
308
309   /* --- Create the default action widget --- */
310
311   btn = gtk_button_new_with_label("OK");
312   gtk_table_attach(GTK_TABLE(box), btn,
313                    left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
314   GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
315   gtk_widget_show(btn);
316
317   /* --- Add the box into the main window --- */
318
319   gtk_container_add(GTK_CONTAINER(win), box);
320   gtk_widget_show(box);
321
322   /* --- Last minute configuration things --- */
323
324   gtk_widget_grab_default(btn);
325   gtk_signal_connect(GTK_OBJECT(btn), "clicked",
326                      GTK_SIGNAL_FUNC(done), &ok);
327   gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
328                             GTK_SIGNAL_FUNC(gtk_widget_activate),
329                             GTK_OBJECT(btn));
330   gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
331                      GTK_SIGNAL_FUNC(check_escape), 0);
332
333   /* --- Go go go --- */
334
335   gtk_widget_realize(win);
336   mdwfocus(win);
337   gtk_widget_show(win);
338   gtk_main();
339
340   /* --- Output the result --- */
341
342   if (ok) {
343     char *p = gtk_entry_get_text(GTK_ENTRY(entry));
344     puts(p);
345   }
346
347   return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
348 }
349
350 /*----- That's all, folks -------------------------------------------------*/