3 * $Id: xgetline.c,v 1.1 1998/11/16 23:00:49 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.1 1998/11/16 23:00:49 mdw
37 /*----- Header files ------------------------------------------------------*/
44 #include <gdk/gdkkeysyms.h>
48 /*----- Main code ---------------------------------------------------------*/
52 * Arguments: @GtkWidget *w@ = widget raising the signal
53 * @gpointer *p@ = pointer to integer result code
57 * Use: Sets the result code to zero (failure) and ends the loop.
60 static void cancel(GtkWidget *w, gpointer *p)
69 * Arguments: @GtkWidget *w@ = widget raising the signal
70 * @gpointer *p@ = pointer to integer result code
74 * Use: Sets the result code nonzero (success) and ends the loop.
77 static void done(GtkWidget *w, gpointer *p)
84 /* --- @check_escape@ --- *
86 * Arguments: @GtkWidget *w@ = widget raising the signal
87 * @GdkEventKey *ev@ = pointer to event data
88 * @gpointer *p@ = widget to activate in response
92 * Use: Activates a widget when an escape keypress is detected.
95 static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
97 if (ev->keyval == GDK_Escape) {
99 gtk_widget_activate(GTK_WIDGET(p));
101 gtk_object_destroy(GTK_OBJECT(w));
109 * Arguments: @int argc@ = number of command line arguments
110 * @char *argv[]@ = addresses of arguments
112 * Returns: Zero if OK, and we read a string; nonzero if the user
115 * Use: Reads a string from the user, and returns it on standard
119 int main(int argc, char *argv[])
121 /* --- Configuration variables --- */
125 char *title = "Input request";
135 /* --- User interface bits --- */
142 /* --- Crank up the toolkit --- *
144 * Have to do this here: GTK snarfs some command line options which my
145 * parser would barf about.
148 gtk_init(&argc, &argv);
150 /* --- Parse options from command line --- */
154 /* --- Long options structure --- */
156 static struct option opt[] = {
157 { "help", 0, 0, 'h' },
158 { "title", required_argument, 0, 't' },
159 { "prompt", required_argument, 0, 'p' },
160 { "default", required_argument, 0, 'd' },
161 { "password", 0, 0, 'i' },
162 { "invisible", 0, 0, 'i' },
167 /* --- Fetch an option --- */
169 i = getopt_long(argc, argv, "ht:p:d:i", opt, 0);
173 /* --- Work out what to do with it --- */
195 fprintf(stderr, "xgetline: bad arguments\n");
199 /* --- Create the main window --- */
201 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
202 gtk_window_set_title(GTK_WINDOW(win), title);
203 gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
204 gtk_signal_connect(GTK_OBJECT(win), "destroy",
205 GTK_SIGNAL_FUNC(cancel), &ok);
207 /* --- Create the box for laying out the widgets inside --- */
209 left = (prompt ? 1 : 0);
210 box = gtk_table_new(left + 2, 1, 0);
212 /* --- Maybe create a prompt widget --- */
215 GtkWidget *w = gtk_label_new(prompt);
216 gtk_table_attach(GTK_TABLE(box), w,
217 0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
221 /* --- Create the entry widget --- */
223 entry = gtk_entry_new();
224 gtk_entry_set_text(GTK_ENTRY(entry), dfl);
225 gtk_table_attach(GTK_TABLE(box), entry,
226 left, left + 1, 0, 1,
227 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
229 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
230 gtk_widget_show(entry);
232 /* --- Create the default action widget --- */
234 btn = gtk_button_new_with_label("OK");
235 gtk_table_attach(GTK_TABLE(box), btn,
236 left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
237 GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
238 gtk_widget_show(btn);
240 /* --- Add the box into the main window --- */
242 gtk_container_add(GTK_CONTAINER(win), box);
243 gtk_widget_show(box);
245 /* --- Last minute configuration things --- */
247 gtk_widget_grab_default(btn);
248 gtk_signal_connect(GTK_OBJECT(btn), "clicked",
249 GTK_SIGNAL_FUNC(done), &ok);
250 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
251 GTK_SIGNAL_FUNC(gtk_widget_activate),
253 gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
254 GTK_SIGNAL_FUNC(check_escape), 0);
256 /* --- Go go go --- */
258 gtk_widget_show(win);
261 /* --- Output the result --- */
264 char *p = gtk_entry_get_text(GTK_ENTRY(entry));
268 return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
271 /*----- That's all, folks -------------------------------------------------*/