2 * $Id: timebox.c,v 1.45 2011/06/27 08:20:22 tom Exp $
4 * timebox.c -- implements the timebox dialog
6 * Copyright 2001-2010,2011 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_attr);
87 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_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;
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_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);
229 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 while (result == DLG_EXIT_UNKNOWN) {
279 BOX *obj = (state == sHR ? &hr_box
280 : (state == sMN ? &mn_box :
281 (state == sSC ? &sc_box : 0)));
283 button = (state < 0) ? 0 : state;
284 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
286 dlg_set_focus(dialog, obj->window);
288 key = dlg_mouse_wgetch(dialog, &fkey);
289 if (dlg_result_key(key, fkey, &result))
292 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
295 /* handle function-keys */
299 result = DLG_EXIT_OK;
302 result = DLG_EXIT_CANCEL;
304 case DLGK_MOUSE('H'):
307 case DLGK_MOUSE('M'):
310 case DLGK_MOUSE('S'):
316 case DLGK_FIELD_PREV:
317 state = dlg_prev_ok_buttonindex(state, sHR);
319 case DLGK_FIELD_NEXT:
320 state = dlg_next_ok_buttonindex(state, sHR);
322 case DLGK_FIELD_FIRST:
325 (void) DrawObject(obj);
328 case DLGK_FIELD_LAST:
339 (void) DrawObject(obj);
342 case DLGK_DELETE_RIGHT:
345 (void) DrawObject(obj);
354 minute = mn_box.value;
355 second = sc_box.value;
358 dlg_del_window(dialog);
360 dlg_mouse_free_regions();
365 int step = next_or_previous(key);
368 while (obj->value < 0)
369 obj->value += obj->period;
370 obj->value %= obj->period;
371 (void) DrawObject(obj);
376 } else if (isdigit(key)) {
378 int digit = (key - '0');
379 int value = (obj->value * 10) + digit;
380 if (value < obj->period) {
382 (void) DrawObject(obj);
393 #define DefaultFormat(dst, src) \
394 sprintf(dst, "%02d:%02d:%02d", \
395 hr_box.value, mn_box.value, sc_box.value)
397 #if defined(HAVE_STRFTIME)
398 if (dialog_vars.time_format != 0) {
400 time_t now = time((time_t *) 0);
401 struct tm *parts = localtime(&now);
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,
408 dialog_vars.time_format,
410 if (used == 0 || *buffer == '\0')
411 DefaultFormat(buffer, hr_box);
414 DefaultFormat(buffer, hr_box);
416 dlg_add_result(buffer);
419 return CleanupResult(result, dialog, prompt, &save_vars);