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