chiark / gitweb /
264777279947fc1a9fafcb28cf43b1be55778d72
[xtoys] / xgetline.c
1 /* -*-c-*-
2  *
3  * $Id: xgetline.c,v 1.2 1998/11/18 21:25:30 mdw Exp $
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 $
32  * Revision 1.2  1998/11/18 21:25:30  mdw
33  * Remove bogus `-h' option from the options list.
34  *
35  * Revision 1.1  1998/11/16 23:00:49  mdw
36  * Initial versions.
37  *
38  */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include <gtk/gtk.h>
47 #include <gdk/gdkkeysyms.h>
48
49 #include "mdwopt.h"
50
51 /*----- Main code ---------------------------------------------------------*/
52
53 /* --- @cancel@ --- *
54  *
55  * Arguments:   @GtkWidget *w@ = widget raising the signal
56  *              @gpointer *p@ = pointer to integer result code
57  *
58  * Returns:     ---
59  *
60  * Use:         Sets the result code to zero (failure) and ends the loop.
61  */
62
63 static void cancel(GtkWidget *w, gpointer *p)
64 {
65   int *ip = (int *)p;
66   *ip = 0;
67   gtk_main_quit();
68 }
69
70 /* --- @done@ --- *
71  *
72  * Arguments:   @GtkWidget *w@ = widget raising the signal
73  *              @gpointer *p@ = pointer to integer result code
74  *
75  * Returns:     ---
76  *
77  * Use:         Sets the result code nonzero (success) and ends the loop.
78  */
79
80 static void done(GtkWidget *w, gpointer *p)
81 {
82   int *ip = (int *)p;
83   *ip = 1;
84   gtk_main_quit();
85 }
86
87 /* --- @check_escape@ --- *
88  *
89  * Arguments:   @GtkWidget *w@ = widget raising the signal
90  *              @GdkEventKey *ev@ = pointer to event data
91  *              @gpointer *p@ = widget to activate in response
92  *
93  * Returns:     ---
94  *
95  * Use:         Activates a widget when an escape keypress is detected.
96  */
97
98 static gboolean check_escape(GtkWidget *w, GdkEventKey *ev, gpointer *p)
99 {
100   if (ev->keyval == GDK_Escape) {
101     if (p)
102       gtk_widget_activate(GTK_WIDGET(p));
103     else
104       gtk_object_destroy(GTK_OBJECT(w));
105     return (1);
106   }
107   return (0);
108 }
109
110 /* --- @main@ --- *
111  *
112  * Arguments:   @int argc@ = number of command line arguments
113  *              @char *argv[]@ = addresses of arguments
114  *
115  * Returns:     Zero if OK, and we read a string; nonzero if the user
116  *              cancelled.
117  *
118  * Use:         Reads a string from the user, and returns it on standard
119  *              output.
120  */
121
122 int main(int argc, char *argv[])
123 {
124   /* --- Configuration variables --- */
125
126   char *prompt = 0;
127   char *dfl = "";
128   char *title = "Input request";
129   int left;
130   unsigned f = 0;
131   int ok = 0;
132
133   enum {
134     f_invis = 1,
135     f_duff = 2
136   };
137
138   /* --- User interface bits --- */
139
140   GtkWidget *win;
141   GtkWidget *box;
142   GtkWidget *entry;
143   GtkWidget *btn;
144
145   /* --- Crank up the toolkit --- *
146    *
147    * Have to do this here: GTK snarfs some command line options which my
148    * parser would barf about.
149    */   
150
151   gtk_init(&argc, &argv);
152
153   /* --- Parse options from command line --- */
154
155   for (;;) {
156
157     /* --- Long options structure --- */
158
159     static struct option opt[] = {
160       { "help",         0,                      0,      'h' },
161       { "title",        required_argument,      0,      't' },
162       { "prompt",       required_argument,      0,      'p' },
163       { "default",      required_argument,      0,      'd' },
164       { "password",     0,                      0,      'i' },
165       { "invisible",    0,                      0,      'i' },
166       { 0,              0,                      0,      0 }
167     };
168     int i;
169
170     /* --- Fetch an option --- */
171
172     i = getopt_long(argc, argv, "t:p:d:i", opt, 0);
173     if (i < 0)
174       break;
175
176     /* --- Work out what to do with it --- */
177
178     switch (i) {
179       case 't':
180         title = optarg;
181         break;
182       case 'p':
183         prompt = optarg;
184         break;
185       case 'd':
186         dfl = optarg;
187         break;
188       case 'i':
189         f |= f_invis;
190         break;
191       default:
192         f |= f_duff;
193         break;
194     }
195   }
196
197   if (f & f_duff) {
198     fprintf(stderr, "xgetline: bad arguments\n");
199     exit(EXIT_FAILURE);
200   }
201
202   /* --- Create the main window --- */
203
204   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
205   gtk_window_set_title(GTK_WINDOW(win), title);
206   gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
207   gtk_signal_connect(GTK_OBJECT(win), "destroy",
208                      GTK_SIGNAL_FUNC(cancel), &ok);
209
210   /* --- Create the box for laying out the widgets inside --- */
211
212   left = (prompt ? 1 : 0);
213   box = gtk_table_new(left + 2, 1, 0);
214
215   /* --- Maybe create a prompt widget --- */
216
217   if (prompt) {
218     GtkWidget *w = gtk_label_new(prompt);
219     gtk_table_attach(GTK_TABLE(box), w,
220                      0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
221     gtk_widget_show(w);
222   }
223
224   /* --- Create the entry widget --- */
225
226   entry = gtk_entry_new();
227   gtk_entry_set_text(GTK_ENTRY(entry), dfl);
228   gtk_table_attach(GTK_TABLE(box), entry,
229                    left, left + 1, 0, 1,
230                    GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
231   if (f & f_invis)
232     gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
233   gtk_widget_show(entry);
234
235   /* --- Create the default action widget --- */
236
237   btn = gtk_button_new_with_label("OK");
238   gtk_table_attach(GTK_TABLE(box), btn,
239                    left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
240   GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
241   gtk_widget_show(btn);
242
243   /* --- Add the box into the main window --- */
244
245   gtk_container_add(GTK_CONTAINER(win), box);
246   gtk_widget_show(box);
247
248   /* --- Last minute configuration things --- */
249
250   gtk_widget_grab_default(btn);
251   gtk_signal_connect(GTK_OBJECT(btn), "clicked",
252                      GTK_SIGNAL_FUNC(done), &ok);
253   gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
254                             GTK_SIGNAL_FUNC(gtk_widget_activate),
255                             GTK_OBJECT(btn));
256   gtk_signal_connect(GTK_OBJECT(win), "key_press_event",
257                      GTK_SIGNAL_FUNC(check_escape), 0);
258
259   /* --- Go go go --- */
260
261   gtk_widget_show(win);
262   gtk_main();
263
264   /* --- Output the result --- */
265
266   if (ok) {
267     char *p = gtk_entry_get_text(GTK_ENTRY(entry));
268     puts(p);
269   }
270
271   return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
272 }
273
274 /*----- That's all, folks -------------------------------------------------*/