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