3 * $Id: xgetline.c,v 1.4 1998/11/30 22:36:47 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.4 1998/11/30 22:36:47 mdw
33 * Tidy up tabbing in help texts very slightly.
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.
39 * Revision 1.2 1998/11/18 21:25:30 mdw
40 * Remove bogus `-h' option from the options list.
42 * Revision 1.1 1998/11/16 23:00:49 mdw
47 /*----- Header files ------------------------------------------------------*/
54 #include <gdk/gdkkeysyms.h>
59 /*----- Main code ---------------------------------------------------------*/
63 * Arguments: @GtkWidget *w@ = widget raising the signal
64 * @gpointer *p@ = pointer to integer result code
68 * Use: Sets the result code to zero (failure) and ends the loop.
71 static void cancel(GtkWidget
*w
, gpointer
*p
)
80 * Arguments: @GtkWidget *w@ = widget raising the signal
81 * @gpointer *p@ = pointer to integer result code
85 * Use: Sets the result code nonzero (success) and ends the loop.
88 static void done(GtkWidget
*w
, gpointer
*p
)
95 /* --- @check_escape@ --- *
97 * Arguments: @GtkWidget *w@ = widget raising the signal
98 * @GdkEventKey *ev@ = pointer to event data
99 * @gpointer *p@ = widget to activate in response
103 * Use: Activates a widget when an escape keypress is detected.
106 static gboolean
check_escape(GtkWidget
*w
, GdkEventKey
*ev
, gpointer
*p
)
108 if (ev
->keyval
== GDK_Escape
) {
110 gtk_widget_activate(GTK_WIDGET(p
));
112 gtk_object_destroy(GTK_OBJECT(w
));
118 /* --- @version@ --- *
120 * Arguments: @FILE *fp@ = output stream to print the message on
124 * Use: Spits out a version message.
127 static void version(FILE *fp
)
129 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
134 * Arguments: @FILE *fp@ = output stream to print the message on
138 * Use: Spits out a usage message.
141 static void usage(FILE *fp
)
143 fprintf(fp
, "Usage: %s [-i] [-t title] [-p prompt] [-d default]\n", QUIS
);
148 * Arguments: @int argc@ = number of command line arguments
149 * @char *argv[]@ = addresses of arguments
151 * Returns: Zero if OK, and we read a string; nonzero if the user
154 * Use: Reads a string from the user, and returns it on standard
158 int main(int argc
, char *argv
[])
160 /* --- Configuration variables --- */
164 char *title
= "Input request";
174 /* --- User interface bits --- */
181 /* --- Crank up the toolkit --- *
183 * Have to do this here: GTK snarfs some command line options which my
184 * parser would barf about.
188 gtk_init(&argc
, &argv
);
190 /* --- Parse options from command line --- */
194 /* --- Long options structure --- */
196 static struct option opt
[] = {
197 { "help", 0, 0, 'h' },
198 { "usage", 0, 0, 'u' },
199 { "version", 0, 0, 'v' },
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' },
209 /* --- Fetch an option --- */
211 i
= getopt_long(argc
, argv
, "huv t:p:d:i", opt
, 0);
215 /* --- Work out what to do with it --- */
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"
227 "Options available are:\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"
233 "-i, --invisible\t Don't show the user's string as it's typed\n"
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",
272 /* --- Create the main window --- */
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
);
280 /* --- Create the box for laying out the widgets inside --- */
282 left
= (prompt ?
1 : 0);
283 box
= gtk_table_new(left
+ 2, 1, 0);
285 /* --- Maybe create a prompt widget --- */
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);
294 /* --- Create the entry widget --- */
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);
302 gtk_entry_set_visibility(GTK_ENTRY(entry
), FALSE
);
303 gtk_widget_show(entry
);
305 /* --- Create the default action widget --- */
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
);
313 /* --- Add the box into the main window --- */
315 gtk_container_add(GTK_CONTAINER(win
), box
);
316 gtk_widget_show(box
);
318 /* --- Last minute configuration things --- */
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
),
326 gtk_signal_connect(GTK_OBJECT(win
), "key_press_event",
327 GTK_SIGNAL_FUNC(check_escape
), 0);
329 /* --- Go go go --- */
331 gtk_widget_show(win
);
334 /* --- Output the result --- */
337 char *p
= gtk_entry_get_text(GTK_ENTRY(entry
));
341 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
344 /*----- That's all, folks -------------------------------------------------*/