2 * $Id: timebox.c,v 1.41 2010/01/18 10:33:42 tom Exp $
4 * timebox.c -- implements the timebox dialog
6 * Copyright 2001-2009,2010 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,
100 data->parent = parent;
104 data->height = height;
105 data->period = period;
106 data->value = value % period;
108 data->window = derwin(data->parent,
109 data->height, data->width,
111 if (data->window == 0)
113 (void) keypad(data->window, TRUE);
115 dlg_mouse_setbase(getbegx(parent), getbegy(parent));
116 dlg_mouse_mkregion(y, x, height, width, code);
122 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
124 dlg_del_window(dialog);
125 dlg_mouse_free_regions();
127 dlg_restore_vars(save_vars);
132 #define DrawObject(data) draw_cell(data)
135 * Display a dialog box for entering a date
138 dialog_timebox(const char *title,
139 const char *subtitle,
147 static DLG_KEYS_BINDING binding[] = {
148 DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
150 DLG_KEYS_DATA( DLGK_ENTER, ' ' ),
151 DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
152 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
153 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
154 DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
155 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
156 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
157 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
158 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
159 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
160 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
161 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+'),
162 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN),
163 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT),
164 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NPAGE),
165 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
166 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PPAGE ),
167 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ),
168 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
174 int old_height = height;
175 int old_width = width;
177 BOX hr_box, mn_box, sc_box;
178 int key = 0, key2, fkey;
180 int result = DLG_EXIT_UNKNOWN;
182 time_t now_time = time((time_t *) 0);
184 int state = dlg_defaultno_button();
185 const char **buttons = dlg_ok_labels();
186 char *prompt = dlg_strclone(subtitle);
187 char buffer[MAX_LEN];
188 DIALOG_VARS save_vars;
190 now_time = time((time_t *) 0);
191 current = *localtime(&now_time);
193 dlg_save_vars(&save_vars);
194 dialog_vars.separate_output = TRUE;
202 dlg_auto_size(title, prompt, &height, &width, 0, 0);
204 if (width < MIN_WIDE)
206 dlg_button_layout(buttons, &width);
207 dlg_print_size(height, width);
208 dlg_ctl_size(height, width);
210 dialog = dlg_new_window(height, width,
211 dlg_box_y_ordinate(height),
212 dlg_box_x_ordinate(width));
213 dlg_register_window(dialog, "timebox", binding);
214 dlg_register_buttons(dialog, "timebox", buttons);
216 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr);
217 dlg_draw_bottom_box(dialog);
218 dlg_draw_title(dialog, title);
220 wattrset(dialog, dialog_attr);
221 dlg_print_autowrap(dialog, prompt, height, width);
223 /* compute positions of hour, month and year boxes */
224 memset(&hr_box, 0, sizeof(hr_box));
225 memset(&mn_box, 0, sizeof(mn_box));
226 memset(&sc_box, 0, sizeof(sc_box));
228 if (init_object(&hr_box,
230 (width - MIN_WIDE + 1) / 2 + MARGIN,
231 (height - MIN_HIGH + MARGIN),
235 hour >= 0 ? hour : current.tm_hour,
237 || DrawObject(&hr_box) < 0) {
238 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
241 mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
242 if (init_object(&mn_box,
244 hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
249 minute >= 0 ? minute : current.tm_min,
251 || DrawObject(&mn_box) < 0) {
252 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
255 mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
256 if (init_object(&sc_box,
258 mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
263 second >= 0 ? second : current.tm_sec,
265 || DrawObject(&sc_box) < 0) {
266 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
269 while (result == DLG_EXIT_UNKNOWN) {
270 BOX *obj = (state == sHR ? &hr_box
271 : (state == sMN ? &mn_box :
272 (state == sSC ? &sc_box : 0)));
274 button = (state < 0) ? 0 : state;
275 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
277 dlg_set_focus(dialog, obj->window);
279 key = dlg_mouse_wgetch(dialog, &fkey);
280 if (dlg_result_key(key, fkey, &result))
283 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
286 /* handle function-keys */
290 result = DLG_EXIT_OK;
293 result = DLG_EXIT_CANCEL;
295 case DLGK_MOUSE('H'):
298 case DLGK_MOUSE('M'):
301 case DLGK_MOUSE('S'):
307 case DLGK_FIELD_PREV:
308 state = dlg_prev_ok_buttonindex(state, sHR);
310 case DLGK_FIELD_NEXT:
311 state = dlg_next_ok_buttonindex(state, sHR);
313 case DLGK_FIELD_FIRST:
316 (void) DrawObject(obj);
319 case DLGK_FIELD_LAST:
330 (void) DrawObject(obj);
333 case DLGK_DELETE_RIGHT:
336 (void) DrawObject(obj);
345 minute = mn_box.value;
346 second = sc_box.value;
349 dlg_del_window(dialog);
351 dlg_mouse_free_regions();
356 int step = next_or_previous(key);
359 while (obj->value < 0)
360 obj->value += obj->period;
361 obj->value %= obj->period;
362 (void) DrawObject(obj);
367 } else if (isdigit(key)) {
369 int digit = (key - '0');
370 int value = (obj->value * 10) + digit;
371 if (value < obj->period) {
373 (void) DrawObject(obj);
384 #define DefaultFormat(dst, src) \
385 sprintf(dst, "%02d:%02d:%02d", \
386 hr_box.value, mn_box.value, sc_box.value)
388 #if defined(HAVE_STRFTIME)
389 if (dialog_vars.time_format != 0) {
391 time_t now = time((time_t *) 0);
392 struct tm *parts = localtime(&now);
394 parts->tm_sec = sc_box.value;
395 parts->tm_min = mn_box.value;
396 parts->tm_hour = hr_box.value;
397 used = strftime(buffer,
399 dialog_vars.time_format,
401 if (used == 0 || *buffer == '\0')
402 DefaultFormat(buffer, hr_box);
405 DefaultFormat(buffer, hr_box);
407 dlg_add_result(buffer);
410 return CleanupResult(result, dialog, prompt, &save_vars);