]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/dialog/timebox.c
Upgrade our copy of llvm/clang to r168974, from upstream's release_32
[FreeBSD/FreeBSD.git] / contrib / dialog / timebox.c
1 /*
2  * $Id: timebox.c,v 1.52 2012/07/02 09:34:04 tom Exp $
3  *
4  *  timebox.c -- implements the timebox dialog
5  *
6  *  Copyright 2001-2011,2012   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_border2_attr);
86
87     (void) 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_default_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_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
225     dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
226     dlg_draw_title(dialog, title);
227     dlg_draw_helpline(dialog, FALSE);
228
229     (void) 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     dlg_trace_win(dialog);
279     while (result == DLG_EXIT_UNKNOWN) {
280         BOX *obj = (state == sHR ? &hr_box
281                     : (state == sMN ? &mn_box :
282                        (state == sSC ? &sc_box : 0)));
283
284         button = (state < 0) ? 0 : state;
285         dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
286         if (obj != 0)
287             dlg_set_focus(dialog, obj->window);
288
289         key = dlg_mouse_wgetch(dialog, &fkey);
290         if (dlg_result_key(key, fkey, &result))
291             break;
292
293         if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
294             result = key2;
295         } else {
296             /* handle function-keys */
297             if (fkey) {
298                 switch (key) {
299                 case DLGK_MOUSE('H'):
300                     state = sHR;
301                     break;
302                 case DLGK_MOUSE('M'):
303                     state = sMN;
304                     break;
305                 case DLGK_MOUSE('S'):
306                     state = sSC;
307                     break;
308                 case DLGK_ENTER:
309                     result = dlg_ok_buttoncode(button);
310                     break;
311                 case DLGK_FIELD_PREV:
312                     state = dlg_prev_ok_buttonindex(state, sHR);
313                     break;
314                 case DLGK_FIELD_NEXT:
315                     state = dlg_next_ok_buttonindex(state, sHR);
316                     break;
317                 case DLGK_FIELD_FIRST:
318                     if (obj != 0) {
319                         obj->value = 0;
320                         (void) DrawObject(obj);
321                     }
322                     break;
323                 case DLGK_FIELD_LAST:
324                     if (obj != 0) {
325                         switch (state) {
326                         case sHR:
327                             obj->value = 23;
328                             break;
329                         case sMN:
330                         case sSC:
331                             obj->value = 59;
332                             break;
333                         }
334                         (void) DrawObject(obj);
335                     }
336                     break;
337                 case DLGK_DELETE_RIGHT:
338                     if (obj != 0) {
339                         obj->value /= 10;
340                         (void) DrawObject(obj);
341                     }
342                     break;
343 #ifdef KEY_RESIZE
344                 case KEY_RESIZE:
345                     /* reset data */
346                     height = old_height;
347                     width = old_width;
348                     hour = hr_box.value;
349                     minute = mn_box.value;
350                     second = sc_box.value;
351                     /* repaint */
352                     dlg_clear();
353                     dlg_del_window(dialog);
354                     refresh();
355                     dlg_mouse_free_regions();
356                     goto retry;
357 #endif
358                 default:
359                     if (is_DLGK_MOUSE(key)) {
360                         result = dlg_ok_buttoncode(key - M_EVENT);
361                         if (result < 0)
362                             result = DLG_EXIT_OK;
363                     } else if (obj != 0) {
364                         int step = next_or_previous(key);
365                         if (step != 0) {
366                             obj->value += step;
367                             while (obj->value < 0)
368                                 obj->value += obj->period;
369                             obj->value %= obj->period;
370                             (void) DrawObject(obj);
371                         }
372                     }
373                     break;
374                 }
375             } else if (isdigit(key)) {
376                 if (obj != 0) {
377                     int digit = (key - '0');
378                     int value = (obj->value * 10) + digit;
379                     if (value < obj->period) {
380                         obj->value = value;
381                         (void) DrawObject(obj);
382                     } else {
383                         beep();
384                     }
385                 }
386             } else {
387                 beep();
388             }
389         }
390     }
391
392 #define DefaultFormat(dst, src) \
393         sprintf(dst, "%02d:%02d:%02d", \
394                 hr_box.value, mn_box.value, sc_box.value)
395
396 #if defined(HAVE_STRFTIME)
397     if (dialog_vars.time_format != 0) {
398         size_t used;
399         time_t now = time((time_t *) 0);
400         struct tm *parts = localtime(&now);
401
402         parts->tm_sec = sc_box.value;
403         parts->tm_min = mn_box.value;
404         parts->tm_hour = hr_box.value;
405         used = strftime(buffer,
406                         sizeof(buffer) - 1,
407                         dialog_vars.time_format,
408                         parts);
409         if (used == 0 || *buffer == '\0')
410             DefaultFormat(buffer, hr_box);
411     } else
412 #endif
413         DefaultFormat(buffer, hr_box);
414
415     dlg_add_result(buffer);
416     dlg_add_separator();
417
418     return CleanupResult(result, dialog, prompt, &save_vars);
419 }