source: trunk/third/gnome-utils/gdialog/inputbox.c @ 18641

Revision 18641, 8.5 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18640, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 *  inputbox.c -- implements the input box
3 *
4 *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 *
6 *  This program is free software; you can redistribute it and/or
7 *  modify it under the terms of the GNU General Public License
8 *  as published by the Free Software Foundation; either version 2
9 *  of the License, or (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software
18 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include "dialog.h"
22
23static GtkWidget *input;
24
25static void cancelled(GtkWidget *w, gpointer *d)
26{
27        exit(-1);
28}
29
30static void okayed(GtkWidget *w, int button, gpointer *d)
31{
32        if(button == GTK_RESPONSE_OK) {
33                gchar *p = (gchar *) gtk_entry_get_text(GTK_ENTRY(input));
34
35                write(2,p, strlen(p));
36                write(2, "\n", 1);
37                exit (0);
38        } else if (button == GTK_RESPONSE_CANCEL)
39                exit(1);
40}
41
42
43unsigned char dialog_input_result[MAX_LEN + 1];
44
45/*
46 * Display a dialog box for inputing a string
47 */
48int dialog_inputbox(const char *title, const char *prompt, int height, int width,
49                    const char *init)
50{
51        int i, x, y, box_y, box_x, box_width;
52        int input_x = 0, scroll = 0, key = 0, button = GTK_RESPONSE_NONE;
53        unsigned char *instr = dialog_input_result;
54        WINDOW *dialog;
55
56        if (gnome_mode) {
57                GtkWidget *w;
58                GList *labellist;
59
60                w = gtk_dialog_new_with_buttons (title, NULL,
61                                                GTK_DIALOG_DESTROY_WITH_PARENT,
62                                                GTK_STOCK_CANCEL,
63                                                GTK_RESPONSE_CANCEL,
64                                                GTK_STOCK_OK,
65                                                GTK_RESPONSE_OK,
66                                                NULL);
67                gtk_dialog_set_default_response (GTK_DIALOG (w),
68                                                 GTK_RESPONSE_OK);
69                gtk_window_set_position (GTK_WINDOW (w), GTK_WIN_POS_CENTER);
70
71                label_autowrap (GTK_DIALOG (w)->vbox, prompt, width);
72
73                input = gtk_entry_new ();
74                gtk_box_pack_start (GTK_BOX (GTK_DIALOG (w)->vbox),
75                                    input, TRUE, TRUE, GNOME_PAD);
76                gtk_signal_connect_object (GTK_OBJECT (input), "activate",
77                        GTK_SIGNAL_FUNC (gtk_window_activate_default),
78                        GTK_OBJECT(w));
79
80                if(init)
81                        gtk_entry_set_text(GTK_ENTRY(input),init);
82
83                if(GTK_IS_ACCESSIBLE(gtk_widget_get_accessible(input))) {
84                        add_atk_namedesc(input, _("GDialog Entry"), _("Enter the text"));
85                        labellist = gtk_container_get_children(GTK_CONTAINER(GTK_DIALOG(w)->vbox));
86                        add_atk_relation(GTK_WIDGET(labellist->data), input, ATK_RELATION_LABEL_FOR);
87                        add_atk_relation(input, GTK_WIDGET(labellist->data), ATK_RELATION_LABELLED_BY);
88                }
89
90                gtk_signal_connect(GTK_OBJECT(w), "destroy",
91                        GTK_SIGNAL_FUNC(cancelled), NULL);
92                gtk_signal_connect(GTK_OBJECT(w), "response",
93                        GTK_SIGNAL_FUNC(okayed), NULL);
94                gtk_widget_show_all(w);
95                gtk_widget_grab_focus (input);
96                gtk_main();
97                return 0;
98        }
99        /* center dialog box on screen */
100        x = (COLS - width) / 2;
101        y = (LINES - height) / 2;
102
103#ifndef NO_COLOR_CURSES
104        if (use_shadow)
105                draw_shadow(stdscr, y, x, height, width);
106#endif
107        dialog = newwin(height, width, y, x);
108#ifdef WITH_GPM
109        mouse_setbase(x, y);
110#endif
111        keypad(dialog, TRUE);
112
113        draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
114        wattrset(dialog, border_attr);
115        wmove(dialog, height - 3, 0);
116        waddch(dialog, ACS_LTEE);
117        for (i = 0; i < width - 2; i++)
118                waddch(dialog, ACS_HLINE);
119        wattrset(dialog, dialog_attr);
120        waddch(dialog, ACS_RTEE);
121        wmove(dialog, height - 2, 1);
122        for (i = 0; i < width - 2; i++)
123                waddch(dialog, ' ');
124
125        if (title != NULL) {
126                wattrset(dialog, title_attr);
127                wmove(dialog, 0, (width - strlen(title)) / 2 - 1);
128                waddch(dialog, ' ');
129                waddstr(dialog, title);
130                waddch(dialog, ' ');
131        }
132        wattrset(dialog, dialog_attr);
133        print_autowrap(dialog, prompt, width - 2, 1, 3);
134
135        /* Draw the input field box */
136        box_width = width - 6;
137        getyx(dialog, y, x);
138        box_y = y + 2;
139        box_x = (width - box_width) / 2;
140#ifdef WITH_GPM
141        mouse_mkregion(y + 1, box_x - 1, 3, box_width + 2, 'i');
142#endif
143        draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
144                 border_attr, dialog_attr);
145
146        x = width / 2 - 11;
147        y = height - 2;
148        print_button(dialog, "Cancel", y, x + 14, FALSE);
149        print_button(dialog, "  OK  ", y, x, TRUE);
150
151        /* Set up the initial value */
152        wmove(dialog, box_y, box_x);
153        wattrset(dialog, inputbox_attr);
154        if (!init)
155                instr[0] = '\0';
156        else
157                strcpy(instr, init);
158        input_x = strlen(instr);
159        if (input_x >= box_width) {
160                scroll = input_x - box_width + 1;
161                input_x = box_width - 1;
162                for (i = 0; i < box_width - 1; i++)
163                        waddch(dialog, instr[scroll + i]);
164        } else
165                waddstr(dialog, instr);
166        wmove(dialog, box_y, box_x + input_x);
167
168        wrefresh(dialog);
169        while (key != ESC) {
170                key = mouse_wgetch(dialog);
171
172                if (button == GTK_RESPONSE_NONE) {      /* Input box selected */
173                        switch (key) {
174                        case TAB:
175                        case KEY_UP:
176                        case KEY_DOWN:
177                        case M_EVENT + 'i':
178                        case M_EVENT + 'o':
179                        case M_EVENT + 'c':
180                                break;
181                        case KEY_LEFT:
182                                continue;
183                        case KEY_RIGHT:
184                                continue;
185                        case KEY_BACKSPACE:
186                        case 127:
187                                if (input_x || scroll) {
188                                        wattrset(dialog, inputbox_attr);
189                                        if (!input_x) {
190                                                scroll = scroll < box_width - 1 ?
191                                                    0 : scroll - (box_width - 1);
192                                                wmove(dialog, box_y, box_x);
193                                                for (i = 0; i < box_width; i++)
194                                                        waddch(dialog, instr[scroll + input_x + i] ?
195                                                               instr[scroll + input_x + i] : ' ');
196                                                input_x = strlen(instr) - scroll;
197                                        } else
198                                                input_x--;
199                                        instr[scroll + input_x] = '\0';
200                                        wmove(dialog, box_y, input_x + box_x);
201                                        waddch(dialog, ' ');
202                                        wmove(dialog, box_y, input_x + box_x);
203                                        wrefresh(dialog);
204                                }
205                                continue;
206                        default:
207                                if (key < 0x100 && isprint(key)) {
208                                        if (scroll + input_x < MAX_LEN) {
209                                                wattrset(dialog, inputbox_attr);
210                                                instr[scroll + input_x] = key;
211                                                instr[scroll + input_x + 1] = '\0';
212                                                if (input_x == box_width - 1) {
213                                                        scroll++;
214                                                        wmove(dialog, box_y, box_x);
215                                                        for (i = 0; i < box_width - 1; i++)
216                                                                waddch(dialog, instr[scroll + i]);
217                                                } else {
218                                                        wmove(dialog, box_y, input_x++ + box_x);
219                                                        waddch(dialog, key);
220                                                }
221                                                wrefresh(dialog);
222                                        } else
223                                                flash();        /* Alarm user about overflow */
224                                        continue;
225                                }
226                        }
227                }
228                switch (key) {
229                case 'O':
230                case 'o':
231                        delwin(dialog);
232                        return 0;
233                case 'C':
234                case 'c':
235                        delwin(dialog);
236                        return 1;
237                case M_EVENT + 'i':     /* mouse enter events */
238                case M_EVENT + 'o':     /* use the code for 'UP' */
239                case M_EVENT + 'c':
240                        if(key == M_EVENT + 'o')
241                        {
242                                if(key == M_EVENT + 'c')
243                                        button = GTK_RESPONSE_OK;
244                                else
245                                        button = GTK_RESPONSE_CANCEL;
246                        }
247                        else
248                        {
249                                if(key == M_EVENT + 'c')
250                                        button = GTK_RESPONSE_NONE;
251                                else
252                                        button = GTK_RESPONSE_OK;
253                        }
254                                                               
255                case KEY_UP:
256                case KEY_LEFT:
257                        switch (button) {
258                        case GTK_RESPONSE_NONE:
259                                button = GTK_RESPONSE_CANCEL;   /* Indicates "Cancel" button is selected */
260                                print_button(dialog, "  OK  ", y, x, FALSE);
261                                print_button(dialog, "Cancel", y, x + 14, TRUE);
262                                wrefresh(dialog);
263                                break;
264                        case GTK_RESPONSE_OK:
265                                button = GTK_RESPONSE_NONE;     /* Indicates input box is selected */
266                                print_button(dialog, "Cancel", y, x + 14, FALSE);
267                                print_button(dialog, "  OK  ", y, x, TRUE);
268                                wmove(dialog, box_y, box_x + input_x);
269                                wrefresh(dialog);
270                                break;
271                        case GTK_RESPONSE_CANCEL:
272                                button = GTK_RESPONSE_OK;/* Indicates "OK" button is selected */
273                                print_button(dialog, "Cancel", y, x + 14, FALSE);
274                                print_button(dialog, "  OK  ", y, x, TRUE);
275                                wrefresh(dialog);
276                                break;
277                        }
278                        break;
279                case TAB:
280                case KEY_DOWN:
281                case KEY_RIGHT:
282                        switch (button) {
283                        case GTK_RESPONSE_NONE:
284                                button = GTK_RESPONSE_OK;/* Indicates "OK" button is selected */
285                                print_button(dialog, "Cancel", y, x + 14, FALSE);
286                                print_button(dialog, "  OK  ", y, x, TRUE);
287                                wrefresh(dialog);
288                                break;
289                        case GTK_RESPONSE_OK:
290                                button = GTK_RESPONSE_CANCEL;   /* Indicates "Cancel" button is selected */
291                                print_button(dialog, "  OK  ", y, x, FALSE);
292                                print_button(dialog, "Cancel", y, x + 14, TRUE);
293                                wrefresh(dialog);
294                                break;
295                        case GTK_RESPONSE_CANCEL:
296                                button = GTK_RESPONSE_NONE;     /* Indicates input box is selected */
297                                print_button(dialog, "Cancel", y, x + 14, FALSE);
298                                print_button(dialog, "  OK  ", y, x, TRUE);
299                                wmove(dialog, box_y, box_x + input_x);
300                                wrefresh(dialog);
301                                break;
302                        }
303                        break;
304                case ' ':
305                case '\n':
306                        delwin(dialog);
307                        if(button == GTK_RESPONSE_NONE || button == GTK_RESPONSE_OK)
308                                return 0;
309                        else if (button == GTK_RESPONSE_CANCEL)
310                                return 1;
311                case ESC:
312                        break;
313                }
314        }
315
316        delwin(dialog);
317        return -1;              /* ESC pressed */
318}
Note: See TracBrowser for help on using the repository browser.