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