3 * $Id: xgetline.c,v 1.8 1998/12/16 19:58:53 mdw Exp $
5 * Fetch a line of text from the user
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Edgeware X tools collection.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: xgetline.c,v $
32 * Revision 1.8 1998/12/16 19:58:53 mdw
33 * Stop the dropdown list from dropping down when you press enter.
35 * Revision 1.7 1998/12/11 09:53:02 mdw
36 * Updates for mLib/mgLib. Support history files for recalling past
37 * entries, using a drop-down list.
39 * Revision 1.6 1998/12/03 00:56:29 mdw
40 * Set focus on the entry field, rather than leaving things to luck.
42 * Revision 1.5 1998/12/03 00:39:44 mdw
43 * Force focus when starting up.
45 * Revision 1.4 1998/11/30 22:36:47 mdw
46 * Tidy up tabbing in help texts very slightly.
48 * Revision 1.3 1998/11/21 22:30:20 mdw
49 * Support GNU-style long options throughout, and introduce proper help
50 * text to all programs. Update manual pages to match.
52 * Revision 1.2 1998/11/18 21:25:30 mdw
53 * Remove bogus `-h' option from the options list.
55 * Revision 1.1 1998/11/16 23:00:49 mdw
60 /*----- Header files ------------------------------------------------------*/
70 #include <gdk/gdkkeysyms.h>
72 #include <mLib/alloc.h>
73 #include <mLib/dstr.h>
74 #include <mLib/mdwopt.h>
75 #include <mLib/report.h>
76 #include <mLib/quis.h>
78 #include <mgLib/cancel.h>
79 #include <mgLib/mdwfocus.h>
81 /*----- Main code ---------------------------------------------------------*/
85 * Arguments: @GtkWidget *w@ = widget raising the signal
86 * @gpointer *p@ = pointer to integer result code
90 * Use: Sets the result code to zero (failure) and ends the loop.
93 static void quit(GtkWidget *w, gpointer *p)
102 * Arguments: @GtkWidget *w@ = widget raising the signal
103 * @gpointer *p@ = pointer to integer result code
107 * Use: Sets the result code nonzero (success) and ends the loop.
110 static void done(GtkWidget *w, gpointer *p)
117 /* --- @version@ --- *
119 * Arguments: @FILE *fp@ = output stream to print the message on
123 * Use: Spits out a version message.
126 static void version(FILE *fp)
128 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
133 * Arguments: @FILE *fp@ = output stream to print the message on
137 * Use: Spits out a usage message.
140 static void usage(FILE *fp)
143 "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
144 "\t[-l|-H file] [-m max]\n",
150 * Arguments: @int argc@ = number of command line arguments
151 * @char *argv[]@ = addresses of arguments
153 * Returns: Zero if OK, and we read a string; nonzero if the user
156 * Use: Reads a string from the user, and returns it on standard
160 int main(int argc, char *argv[])
162 /* --- Configuration variables --- */
166 char *title = "Input request";
171 const char *list = 0;
182 /* --- User interface bits --- */
189 /* --- Crank up the toolkit --- *
191 * Have to do this here: GTK snarfs some command line options which my
192 * parser would barf about.
196 gtk_init(&argc, &argv);
198 /* --- Parse options from command line --- */
202 /* --- Long options structure --- */
204 static struct option opt[] = {
205 { "help", 0, 0, 'h' },
206 { "usage", 0, 0, 'u' },
207 { "version", 0, 0, 'v' },
208 { "title", required_argument, 0, 't' },
209 { "prompt", required_argument, 0, 'p' },
210 { "default", required_argument, 0, 'd' },
211 { "password", 0, 0, 'i' },
212 { "invisible", 0, 0, 'i' },
213 { "history", required_argument, 0, 'H' },
214 { "list", required_argument, 0, 'l' },
215 { "histmax", required_argument, 0, 'm' },
216 { "no-choice", 0, 0, 'n' },
221 /* --- Fetch an option --- */
223 i = getopt_long(argc, argv, "huv t:p:d:i H:l:m:n", opt, 0);
227 /* --- Work out what to do with it --- */
236 "Pops up a small window requesting input from a user, and echoes the\n"
237 "response to stdout, where it can be collected by a shell script.\n"
239 "Options available are:\n"
241 "-h, --help Display this help text\n"
242 "-u, --usage Display a short usage summary\n"
243 "-v, --version Display the program's version number\n"
245 "-i, --invisible\t Don't show the user's string as it's typed\n"
246 "-t, --title=TITLE Set the window's title string\n"
247 "-p, --prompt=PROMPT Set the window's prompt string\n"
248 "-d, --default=DEFAULT Set the default string already in the window\n"
250 "-l, --list=FILE Read FILE into a drop-down list\n"
251 "-n, --no-choice No free text input: must choose item from list\n"
252 "-H, --history=FILE As for `--list', but update with new string\n"
253 "-m, --histmax=MAX Maximum number of items written back to file\n",
290 histmax = atoi(optarg);
304 if ((f & f_invis) && list) {
306 "invisible entry is dumb if you provide a list of alternatives!");
309 if ((f & f_nochoice) && !list)
310 die(EXIT_FAILURE, "nothing to restrict choice to!");
312 /* --- Create the main window --- */
314 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
315 gtk_window_set_title(GTK_WINDOW(win), title);
316 gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
317 gtk_signal_connect(GTK_OBJECT(win), "destroy",
318 GTK_SIGNAL_FUNC(quit), &ok);
320 /* --- Create the box for laying out the widgets inside --- */
322 left = (prompt ? 1 : 0);
323 box = gtk_table_new(left + 2, 1, 0);
325 /* --- Maybe create a prompt widget --- */
328 GtkWidget *w = gtk_label_new(prompt);
329 gtk_table_attach(GTK_TABLE(box), w,
330 0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
334 /* --- Create the entry widget --- */
337 FILE *fp = fopen(list, "r");
340 /* --- Read the items in from the file --- *
342 * Inability to open the file is not a disaster.
350 while (dstr_putline(&d, fp) != EOF) {
351 hist = g_list_append(hist, xstrdup(d.buf));
357 /* --- Now create a combo box --- */
359 combo = gtk_combo_new();
360 entry = GTK_COMBO(combo)->entry;
362 gtk_combo_set_popdown_strings(GTK_COMBO(combo), hist);
364 /* --- Do other configuring --- */
366 if (f & f_nochoice) {
367 gtk_combo_set_value_in_list(GTK_COMBO(combo), 1, 0);
368 gtk_entry_set_editable(GTK_ENTRY(entry), 0);
370 gtk_combo_set_case_sensitive(GTK_COMBO(combo), 1);
371 gtk_combo_set_use_arrows_always(GTK_COMBO(combo), 1);
372 gtk_combo_disable_activate(GTK_COMBO(combo));
373 if (strcmp(dfl, "@") == 0)
374 gtk_entry_set_text(GTK_ENTRY(entry), hist ? (char *)hist->data : "");
376 gtk_entry_set_text(GTK_ENTRY(entry), dfl);
378 /* --- Set the widget in the right place and show it --- */
380 gtk_table_attach(GTK_TABLE(box), combo,
381 left, left + 1, 0, 1,
382 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
383 gtk_widget_show(combo);
385 entry = gtk_entry_new();
386 gtk_entry_set_text(GTK_ENTRY(entry), dfl);
388 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
389 gtk_table_attach(GTK_TABLE(box), entry,
390 left, left + 1, 0, 1,
391 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
392 gtk_widget_show(entry);
395 /* --- Create the default action widget --- */
397 btn = gtk_button_new_with_label("OK");
398 gtk_table_attach(GTK_TABLE(box), btn,
399 left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
400 GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
401 gtk_widget_show(btn);
403 /* --- Add the box into the main window --- */
405 gtk_container_add(GTK_CONTAINER(win), box);
406 gtk_widget_show(box);
408 /* --- Last minute configuration things --- */
410 gtk_widget_grab_default(btn);
411 gtk_signal_connect(GTK_OBJECT(btn), "clicked",
412 GTK_SIGNAL_FUNC(done), &ok);
413 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
414 GTK_SIGNAL_FUNC(gtk_widget_activate),
416 cancel(GTK_WINDOW(win), 0);
418 /* --- Go go go --- */
420 gtk_widget_realize(win);
422 gtk_widget_grab_focus(entry);
423 gtk_widget_show(win);
426 /* --- Output the result --- */
429 char *p = gtk_entry_get_text(GTK_ENTRY(entry));
431 /* --- If history is enabled, output a new history file --- *
433 * If the first entry was accepted verbatim, don't bother.
436 if (f & f_history && !(hist && strcmp(p, hist->data) == 0)) {
442 if ((fd = open(list, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
444 if ((fp = fdopen(fd, "w")) == 0) {
452 for (i = 1, g = hist; (histmax < 1 || i < histmax) && g; g = g->next) {
453 if (strcmp(g->data, p) != 0) {
463 /* --- Print the result and go away --- */
468 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
471 /*----- That's all, folks -------------------------------------------------*/