3 * $Id: xgetline.c,v 1.3 1998/11/21 22:30:20 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.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.
36 * Revision 1.2 1998/11/18 21:25:30 mdw
37 * Remove bogus `-h' option from the options list.
39 * Revision 1.1 1998/11/16 23:00:49 mdw
44 /*----- Header files ------------------------------------------------------*/
51 #include <gdk/gdkkeysyms.h>
56 /*----- Main code ---------------------------------------------------------*/
60 * Arguments: @GtkWidget *w@ = widget raising the signal
61 * @gpointer *p@ = pointer to integer result code
65 * Use: Sets the result code to zero (failure) and ends the loop.
68 static void cancel(GtkWidget *w, gpointer *p)
77 * Arguments: @GtkWidget *w@ = widget raising the signal
78 * @gpointer *p@ = pointer to integer result code
82 * Use: Sets the result code nonzero (success) and ends the loop.
85 static void done(GtkWidget *w, gpointer *p)
92 /* --- @check_escape@ --- *
94 * Arguments: @GtkWidget *w@ = widget raising the signal
95 * @GdkEventKey *ev@ = pointer to event data
96 * @gpointer *p@ = widget to activate in response
100 * Use: Activates a widget when an escape keypress is detected.
103 static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
105 if (ev->keyval == GDK_Escape) {
107 gtk_widget_activate(GTK_WIDGET(p));
109 gtk_object_destroy(GTK_OBJECT(w));
115 /* --- @version@ --- *
117 * Arguments: @FILE *fp@ = output stream to print the message on
121 * Use: Spits out a version message.
124 static void version(FILE *fp)
126 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
131 * Arguments: @FILE *fp@ = output stream to print the message on
135 * Use: Spits out a usage message.
138 static void usage(FILE *fp)
140 fprintf(fp, "Usage: %s [-i] [-t title] [-p prompt] [-d default]\n", QUIS);
145 * Arguments: @int argc@ = number of command line arguments
146 * @char *argv[]@ = addresses of arguments
148 * Returns: Zero if OK, and we read a string; nonzero if the user
151 * Use: Reads a string from the user, and returns it on standard
155 int main(int argc, char *argv[])
157 /* --- Configuration variables --- */
161 char *title = "Input request";
171 /* --- User interface bits --- */
178 /* --- Crank up the toolkit --- *
180 * Have to do this here: GTK snarfs some command line options which my
181 * parser would barf about.
185 gtk_init(&argc, &argv);
187 /* --- Parse options from command line --- */
191 /* --- Long options structure --- */
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' },
206 /* --- Fetch an option --- */
208 i = getopt_long(argc, argv, "huv t:p:d:i", opt, 0);
212 /* --- Work out what to do with it --- */
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"
224 "Options available are:\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"
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",
269 /* --- Create the main window --- */
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);
277 /* --- Create the box for laying out the widgets inside --- */
279 left = (prompt ? 1 : 0);
280 box = gtk_table_new(left + 2, 1, 0);
282 /* --- Maybe create a prompt widget --- */
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);
291 /* --- Create the entry widget --- */
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);
299 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
300 gtk_widget_show(entry);
302 /* --- Create the default action widget --- */
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);
310 /* --- Add the box into the main window --- */
312 gtk_container_add(GTK_CONTAINER(win), box);
313 gtk_widget_show(box);
315 /* --- Last minute configuration things --- */
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),
323 gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
324 GTK_SIGNAL_FUNC(check_escape), 0);
326 /* --- Go go go --- */
328 gtk_widget_show(win);
331 /* --- Output the result --- */
334 char *p = gtk_entry_get_text(GTK_ENTRY(entry));
338 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
341 /*----- That's all, folks -------------------------------------------------*/