]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/dialog/timebox.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / dialog / timebox.c
1 /*
2  * $Id: timebox.c,v 1.45 2011/06/27 08:20:22 tom Exp $
3  *
4  *  timebox.c -- implements the timebox dialog
5  *
6  *  Copyright 2001-2010,2011   Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *      Free Software Foundation, Inc.
20  *      51 Franklin St., Fifth Floor
21  *      Boston, MA 02110, USA.
22  */
23
24 #include <dialog.h>
25 #include <dlg_keys.h>
26
27 #include <time.h>
28
29 #define ONE_HIGH 1
30 #define ONE_WIDE 2
31 #define BTN_HIGH 2
32
33 #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
34 #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
35
36 typedef enum {
37     sHR = -3
38     ,sMN = -2
39     ,sSC = -1
40 } STATES;
41
42 struct _box;
43
44 typedef int (*BOX_DRAW) (struct _box *, struct tm *);
45
46 typedef struct _box {
47     WINDOW *parent;
48     WINDOW *window;
49     int x;
50     int y;
51     int width;
52     int height;
53     int period;
54     int value;
55 } BOX;
56
57 static int
58 next_or_previous(int key)
59 {
60     int result = 0;
61
62     switch (key) {
63     case DLGK_ITEM_PREV:
64         result = -1;
65         break;
66     case DLGK_ITEM_NEXT:
67         result = 1;
68         break;
69     default:
70         beep();
71         break;
72     }
73     return result;
74 }
75 /*
76  * Draw the hour-of-month selection box
77  */
78 static int
79 draw_cell(BOX * data)
80 {
81     werase(data->window);
82     dlg_draw_box(data->parent,
83                  data->y - MARGIN, data->x - MARGIN,
84                  data->height + (2 * MARGIN), data->width + (2 * MARGIN),
85                  menubox_border_attr, menubox_attr);
86
87     wattrset(data->window, item_attr);
88     wprintw(data->window, "%02d", data->value);
89     return 0;
90 }
91
92 static int
93 init_object(BOX * data,
94             WINDOW *parent,
95             int x, int y,
96             int width, int height,
97             int period, int value,
98             int code)
99 {
100     (void) code;
101
102     data->parent = parent;
103     data->x = x;
104     data->y = y;
105     data->width = width;
106     data->height = height;
107     data->period = period;
108     data->value = value % period;
109
110     data->window = derwin(data->parent,
111                           data->height, data->width,
112                           data->y, data->x);
113     if (data->window == 0)
114         return -1;
115     (void) keypad(data->window, TRUE);
116
117     dlg_mouse_setbase(getbegx(parent), getbegy(parent));
118     dlg_mouse_mkregion(y, x, height, width, code);
119
120     return 0;
121 }
122
123 static int
124 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
125 {
126     dlg_del_window(dialog);
127     dlg_mouse_free_regions();
128     free(prompt);
129     dlg_restore_vars(save_vars);
130
131     return code;
132 }
133
134 #define DrawObject(data) draw_cell(data)
135
136 /*
137  * Display a dialog box for entering a date
138  */
139 int
140 dialog_timebox(const char *title,
141                const char *subtitle,
142                int height,
143                int width,
144                int hour,
145                int minute,
146                int second)
147 {
148     /* *INDENT-OFF* */
149     static DLG_KEYS_BINDING binding[] = {
150         DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
151         HELPKEY_BINDINGS,
152         ENTERKEY_BINDINGS,
153         DLG_KEYS_DATA( DLGK_ENTER,      ' ' ),
154         DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
155         DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
156         DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
157         DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
158         DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
159         DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
160         DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
161         DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
162         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
163         DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
164         DLG_KEYS_DATA( DLGK_ITEM_NEXT,  '+'),
165         DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_DOWN),
166         DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NEXT),
167         DLG_KEYS_DATA( DLGK_ITEM_NEXT,  KEY_NPAGE),
168         DLG_KEYS_DATA( DLGK_ITEM_PREV,  '-' ),
169         DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PPAGE ),
170         DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_PREVIOUS ),
171         DLG_KEYS_DATA( DLGK_ITEM_PREV,  KEY_UP ),
172         END_KEYS_BINDING
173     };
174     /* *INDENT-ON* */
175
176 #ifdef KEY_RESIZE
177     int old_height = height;
178     int old_width = width;
179 #endif
180     BOX hr_box, mn_box, sc_box;
181     int key = 0, key2, fkey;
182     int button;
183     int result = DLG_EXIT_UNKNOWN;
184     WINDOW *dialog;
185     time_t now_time = time((time_t *) 0);
186     struct tm current;
187     int state = dlg_defaultno_button();
188     const char **buttons = dlg_ok_labels();
189     char *prompt = dlg_strclone(subtitle);
190     char buffer[MAX_LEN];
191     DIALOG_VARS save_vars;
192
193     now_time = time((time_t *) 0);
194     current = *localtime(&now_time);
195
196     dlg_save_vars(&save_vars);
197     dialog_vars.separate_output = TRUE;
198
199     dlg_does_output();
200
201 #ifdef KEY_RESIZE
202   retry:
203 #endif
204
205     dlg_auto_size(title, prompt, &height, &width, 0, 0);
206     height += MIN_HIGH;
207     if (width < MIN_WIDE)
208         width = MIN_WIDE;
209     dlg_button_layout(buttons, &width);
210     dlg_print_size(height, width);
211     dlg_ctl_size(height, width);
212
213     dialog = dlg_new_window(height, width,
214                             dlg_box_y_ordinate(height),
215                             dlg_box_x_ordinate(width));
216
217     if (hour >= 24 || minute >= 60 || second >= 60) {
218         return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
219     }
220
221     dlg_register_window(dialog, "timebox", binding);
222     dlg_register_buttons(dialog, "timebox", buttons);
223
224     dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
225     dlg_draw_bottom_box(dialog);
226     dlg_draw_title(dialog, title);
227     dlg_draw_helpline(dialog, FALSE);
228
229     wattrset(dialog, dialog_attr);
230     dlg_print_autowrap(dialog, prompt, height, width);
231
232     /* compute positions of hour, month and year boxes */
233     memset(&hr_box, 0, sizeof(hr_box));
234     memset(&mn_box, 0, sizeof(mn_box));
235     memset(&sc_box, 0, sizeof(sc_box));
236
237     if (init_object(&hr_box,
238                     dialog,
239                     (width - MIN_WIDE + 1) / 2 + MARGIN,
240                     (height - MIN_HIGH + MARGIN),
241                     ONE_WIDE,
242                     ONE_HIGH,
243                     24,
244                     hour >= 0 ? hour : current.tm_hour,
245                     'H') < 0
246         || DrawObject(&hr_box) < 0) {
247         return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
248     }
249
250     mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
251     if (init_object(&mn_box,
252                     dialog,
253                     hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
254                     hr_box.y,
255                     hr_box.width,
256                     hr_box.height,
257                     60,
258                     minute >= 0 ? minute : current.tm_min,
259                     'M') < 0
260         || DrawObject(&mn_box) < 0) {
261         return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
262     }
263
264     mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
265     if (init_object(&sc_box,
266                     dialog,
267                     mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
268                     mn_box.y,
269                     mn_box.width,
270                     mn_box.height,
271                     60,
272                     second >= 0 ? second : current.tm_sec,
273                     'S') < 0
274         || DrawObject(&sc_box) < 0) {
275         return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
276     }
277
278     while (result == DLG_EXIT_UNKNOWN) {
279         BOX *obj = (state == sHR ? &hr_box
280                     : (state == sMN ? &mn_box :
281                        (state == sSC ? &sc_box : 0)));
282
283         button = (state < 0) ? 0 : state;
284         dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
285         if (obj != 0)
286             dlg_set_focus(dialog, obj->window);
287
288         key = dlg_mouse_wgetch(dialog, &fkey);
289         if (dlg_result_key(key, fkey, &result))
290             break;
291
292         if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
293             result = key2;
294         } else {
295             /* handle function-keys */
296             if (fkey) {
297                 switch (key) {
298                 case DLGK_MOUSE(0):
299                     result = DLG_EXIT_OK;
300                     break;
301                 case DLGK_MOUSE(1):
302                     result = DLG_EXIT_CANCEL;
303                     break;
304                 case DLGK_MOUSE('H'):
305                     state = sHR;
306                     break;
307                 case DLGK_MOUSE('M'):
308                     state = sMN;
309                     break;
310                 case DLGK_MOUSE('S'):
311                     state = sSC;
312                     break;
313                 case DLGK_ENTER:
314                     result = button;
315                     break;
316                 case DLGK_FIELD_PREV:
317                     state = dlg_prev_ok_buttonindex(state, sHR);
318                     break;
319                 case DLGK_FIELD_NEXT:
320                     state = dlg_next_ok_buttonindex(state, sHR);
321                     break;
322                 case DLGK_FIELD_FIRST:
323                     if (obj != 0) {
324                         obj->value = 0;
325                         (void) DrawObject(obj);
326                     }
327                     break;
328                 case DLGK_FIELD_LAST:
329                     if (obj != 0) {
330                         switch (state) {
331                         case sHR:
332                             obj->value = 23;
333                             break;
334                         case sMN:
335                         case sSC:
336                             obj->value = 59;
337                             break;
338                         }
339                         (void) DrawObject(obj);
340                     }
341                     break;
342                 case DLGK_DELETE_RIGHT:
343                     if (obj != 0) {
344                         obj->value /= 10;
345                         (void) DrawObject(obj);
346                     }
347                     break;
348 #ifdef KEY_RESIZE
349                 case KEY_RESIZE:
350                     /* reset data */
351                     height = old_height;
352                     width = old_width;
353                     hour = hr_box.value;
354                     minute = mn_box.value;
355                     second = sc_box.value;
356                     /* repaint */
357                     dlg_clear();
358                     dlg_del_window(dialog);
359                     refresh();
360                     dlg_mouse_free_regions();
361                     goto retry;
362 #endif
363                 default:
364                     if (obj != 0) {
365                         int step = next_or_previous(key);
366                         if (step != 0) {
367                             obj->value += step;
368                             while (obj->value < 0)
369                                 obj->value += obj->period;
370                             obj->value %= obj->period;
371                             (void) DrawObject(obj);
372                         }
373                     }
374                     break;
375                 }
376             } else if (isdigit(key)) {
377                 if (obj != 0) {
378                     int digit = (key - '0');
379                     int value = (obj->value * 10) + digit;
380                     if (value < obj->period) {
381                         obj->value = value;
382                         (void) DrawObject(obj);
383                     } else {
384                         beep();
385                     }
386                 }
387             } else {
388                 beep();
389             }
390         }
391     }
392
393 #define DefaultFormat(dst, src) \
394         sprintf(dst, "%02d:%02d:%02d", \
395                 hr_box.value, mn_box.value, sc_box.value)
396
397 #if defined(HAVE_STRFTIME)
398     if (dialog_vars.time_format != 0) {
399         size_t used;
400         time_t now = time((time_t *) 0);
401         struct tm *parts = localtime(&now);
402
403         parts->tm_sec = sc_box.value;
404         parts->tm_min = mn_box.value;
405         parts->tm_hour = hr_box.value;
406         used = strftime(buffer,
407                         sizeof(buffer) - 1,
408                         dialog_vars.time_format,
409                         parts);
410         if (used == 0 || *buffer == '\0')
411             DefaultFormat(buffer, hr_box);
412     } else
413 #endif
414         DefaultFormat(buffer, hr_box);
415
416     dlg_add_result(buffer);
417     dlg_add_separator();
418
419     return CleanupResult(result, dialog, prompt, &save_vars);
420 }