2 * $Id: timebox.c,v 1.52 2012/07/02 09:34:04 tom Exp $
4 * timebox.c -- implements the timebox dialog
6 * Copyright 2001-2011,2012 Thomas E. Dickey
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.
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.
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.
33 #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
34 #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
44 typedef int (*BOX_DRAW) (struct _box *, struct tm *);
58 next_or_previous(int key)
76 * Draw the hour-of-month selection box
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);
87 (void) wattrset(data->window, item_attr);
88 wprintw(data->window, "%02d", data->value);
93 init_object(BOX * data,
96 int width, int height,
97 int period, int value,
102 data->parent = parent;
106 data->height = height;
107 data->period = period;
108 data->value = value % period;
110 data->window = derwin(data->parent,
111 data->height, data->width,
113 if (data->window == 0)
115 (void) keypad(data->window, TRUE);
117 dlg_mouse_setbase(getbegx(parent), getbegy(parent));
118 dlg_mouse_mkregion(y, x, height, width, code);
124 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
126 dlg_del_window(dialog);
127 dlg_mouse_free_regions();
129 dlg_restore_vars(save_vars);
134 #define DrawObject(data) draw_cell(data)
137 * Display a dialog box for entering a date
140 dialog_timebox(const char *title,
141 const char *subtitle,
149 static DLG_KEYS_BINDING binding[] = {
150 DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
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 ),
177 int old_height = height;
178 int old_width = width;
180 BOX hr_box, mn_box, sc_box;
181 int key = 0, key2, fkey;
183 int result = DLG_EXIT_UNKNOWN;
185 time_t now_time = time((time_t *) 0);
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;
193 now_time = time((time_t *) 0);
194 current = *localtime(&now_time);
196 dlg_save_vars(&save_vars);
197 dialog_vars.separate_output = TRUE;
205 dlg_auto_size(title, prompt, &height, &width, 0, 0);
207 if (width < MIN_WIDE)
209 dlg_button_layout(buttons, &width);
210 dlg_print_size(height, width);
211 dlg_ctl_size(height, width);
213 dialog = dlg_new_window(height, width,
214 dlg_box_y_ordinate(height),
215 dlg_box_x_ordinate(width));
217 if (hour >= 24 || minute >= 60 || second >= 60) {
218 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
221 dlg_register_window(dialog, "timebox", binding);
222 dlg_register_buttons(dialog, "timebox", buttons);
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);
229 (void) wattrset(dialog, dialog_attr);
230 dlg_print_autowrap(dialog, prompt, height, width);
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));
237 if (init_object(&hr_box,
239 (width - MIN_WIDE + 1) / 2 + MARGIN,
240 (height - MIN_HIGH + MARGIN),
244 hour >= 0 ? hour : current.tm_hour,
246 || DrawObject(&hr_box) < 0) {
247 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
250 mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
251 if (init_object(&mn_box,
253 hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
258 minute >= 0 ? minute : current.tm_min,
260 || DrawObject(&mn_box) < 0) {
261 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
264 mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
265 if (init_object(&sc_box,
267 mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
272 second >= 0 ? second : current.tm_sec,
274 || DrawObject(&sc_box) < 0) {
275 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
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)));
284 button = (state < 0) ? 0 : state;
285 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
287 dlg_set_focus(dialog, obj->window);
289 key = dlg_mouse_wgetch(dialog, &fkey);
290 if (dlg_result_key(key, fkey, &result))
293 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
296 /* handle function-keys */
299 case DLGK_MOUSE('H'):
302 case DLGK_MOUSE('M'):
305 case DLGK_MOUSE('S'):
309 result = dlg_ok_buttoncode(button);
311 case DLGK_FIELD_PREV:
312 state = dlg_prev_ok_buttonindex(state, sHR);
314 case DLGK_FIELD_NEXT:
315 state = dlg_next_ok_buttonindex(state, sHR);
317 case DLGK_FIELD_FIRST:
320 (void) DrawObject(obj);
323 case DLGK_FIELD_LAST:
334 (void) DrawObject(obj);
337 case DLGK_DELETE_RIGHT:
340 (void) DrawObject(obj);
349 minute = mn_box.value;
350 second = sc_box.value;
353 dlg_del_window(dialog);
355 dlg_mouse_free_regions();
359 if (is_DLGK_MOUSE(key)) {
360 result = dlg_ok_buttoncode(key - M_EVENT);
362 result = DLG_EXIT_OK;
363 } else if (obj != 0) {
364 int step = next_or_previous(key);
367 while (obj->value < 0)
368 obj->value += obj->period;
369 obj->value %= obj->period;
370 (void) DrawObject(obj);
375 } else if (isdigit(key)) {
377 int digit = (key - '0');
378 int value = (obj->value * 10) + digit;
379 if (value < obj->period) {
381 (void) DrawObject(obj);
392 #define DefaultFormat(dst, src) \
393 sprintf(dst, "%02d:%02d:%02d", \
394 hr_box.value, mn_box.value, sc_box.value)
396 #if defined(HAVE_STRFTIME)
397 if (dialog_vars.time_format != 0) {
399 time_t now = time((time_t *) 0);
400 struct tm *parts = localtime(&now);
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,
407 dialog_vars.time_format,
409 if (used == 0 || *buffer == '\0')
410 DefaultFormat(buffer, hr_box);
413 DefaultFormat(buffer, hr_box);
415 dlg_add_result(buffer);
418 return CleanupResult(result, dialog, prompt, &save_vars);