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