2 * $Id: timebox.c,v 1.67 2020/03/27 20:26:21 tom Exp $
4 * timebox.c -- implements the timebox dialog
6 * Copyright 2001-2019,2020 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.
24 #include <dlg_internals.h>
33 #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
34 #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
56 next_or_previous(int key)
74 * Draw the hour-of-month selection box
80 dlg_draw_box(data->parent,
81 data->y - MARGIN, data->x - MARGIN,
82 data->height + (2 * MARGIN), data->width + (2 * MARGIN),
83 menubox_border_attr, menubox_border2_attr);
85 dlg_attrset(data->window, item_attr);
86 wprintw(data->window, "%02d", data->value);
91 init_object(BOX * data,
94 int width, int height,
95 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 ),
152 DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
153 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
154 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
155 DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
156 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
157 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
158 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
159 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
160 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
161 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
162 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+'),
163 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN),
164 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT),
165 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NPAGE),
166 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
167 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PPAGE ),
168 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ),
169 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
175 int old_height = height;
176 int old_width = width;
178 BOX hr_box, mn_box, sc_box;
181 int result = DLG_EXIT_UNKNOWN;
185 int state = dlg_default_button();
186 const char **buttons = dlg_ok_labels();
188 char buffer[MAX_LEN];
189 DIALOG_VARS save_vars;
191 DLG_TRACE(("# timebox args:\n"));
192 DLG_TRACE2S("title", title);
193 DLG_TRACE2S("message", subtitle);
194 DLG_TRACE2N("height", height);
195 DLG_TRACE2N("width", width);
196 DLG_TRACE2N("hour", hour);
197 DLG_TRACE2N("minute", minute);
198 DLG_TRACE2N("second", second);
200 now_time = time((time_t *) 0);
201 current = *localtime(&now_time);
203 dlg_save_vars(&save_vars);
204 dialog_vars.separate_output = TRUE;
212 prompt = dlg_strclone(subtitle);
213 dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
215 dlg_button_layout(buttons, &width);
216 dlg_print_size(height, width);
217 dlg_ctl_size(height, width);
219 dialog = dlg_new_window(height, width,
220 dlg_box_y_ordinate(height),
221 dlg_box_x_ordinate(width));
223 if (hour >= 24 || minute >= 60 || second >= 60) {
224 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
227 dlg_register_window(dialog, "timebox", binding);
228 dlg_register_buttons(dialog, "timebox", buttons);
230 dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
231 dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
232 dlg_draw_title(dialog, title);
233 dlg_draw_helpline(dialog, FALSE);
235 dlg_attrset(dialog, dialog_attr);
236 dlg_print_autowrap(dialog, prompt, height, width);
238 /* compute positions of hour, month and year boxes */
239 memset(&hr_box, 0, sizeof(hr_box));
240 memset(&mn_box, 0, sizeof(mn_box));
241 memset(&sc_box, 0, sizeof(sc_box));
243 if (init_object(&hr_box,
245 (width - MIN_WIDE + 1) / 2 + MARGIN,
246 (height - MIN_HIGH + MARGIN),
250 hour >= 0 ? hour : current.tm_hour,
252 || DrawObject(&hr_box) < 0) {
253 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
256 mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
257 if (init_object(&mn_box,
259 hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
264 minute >= 0 ? minute : current.tm_min,
266 || DrawObject(&mn_box) < 0) {
267 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
270 mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
271 if (init_object(&sc_box,
273 mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
278 second >= 0 ? second : current.tm_sec,
280 || DrawObject(&sc_box) < 0) {
281 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
284 dlg_trace_win(dialog);
285 while (result == DLG_EXIT_UNKNOWN) {
286 BOX *obj = (state == sHR ? &hr_box
287 : (state == sMN ? &mn_box :
288 (state == sSC ? &sc_box : 0)));
291 button = (state < 0) ? 0 : state;
292 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
294 dlg_set_focus(dialog, obj->window);
296 key = dlg_mouse_wgetch(dialog, &fkey);
297 if (dlg_result_key(key, fkey, &result)) {
298 if (!dlg_button_key(result, &button, &key, &fkey))
302 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
305 /* handle function-keys */
308 case DLGK_MOUSE('H'):
311 case DLGK_MOUSE('M'):
314 case DLGK_MOUSE('S'):
319 result = dlg_ok_buttoncode(button);
321 case DLGK_FIELD_PREV:
322 state = dlg_prev_ok_buttonindex(state, sHR);
324 case DLGK_FIELD_NEXT:
325 state = dlg_next_ok_buttonindex(state, sHR);
327 case DLGK_FIELD_FIRST:
330 (void) DrawObject(obj);
333 case DLGK_FIELD_LAST:
344 (void) DrawObject(obj);
347 case DLGK_DELETE_RIGHT:
350 (void) DrawObject(obj);
355 dlg_will_resize(dialog);
360 minute = mn_box.value;
361 second = sc_box.value;
364 _dlg_resize_cleanup(dialog);
368 if (is_DLGK_MOUSE(key)) {
369 result = dlg_ok_buttoncode(key - M_EVENT);
371 result = DLG_EXIT_OK;
372 } else if (obj != 0) {
373 int step = next_or_previous(key);
376 while (obj->value < 0)
377 obj->value += obj->period;
378 obj->value %= obj->period;
379 (void) DrawObject(obj);
384 } else if (isdigit(key)) {
386 int digit = (key - '0');
387 int value = (obj->value * 10) + digit;
388 if (value < obj->period) {
390 (void) DrawObject(obj);
395 } else if (key > 0) {
401 #define DefaultFormat(dst, src) \
402 sprintf(dst, "%02d:%02d:%02d", \
403 hr_box.value, mn_box.value, sc_box.value)
405 #if defined(HAVE_STRFTIME)
406 if (dialog_vars.time_format != 0) {
408 time_t now = time((time_t *) 0);
409 struct tm *parts = localtime(&now);
411 parts->tm_sec = sc_box.value;
412 parts->tm_min = mn_box.value;
413 parts->tm_hour = hr_box.value;
414 used = strftime(buffer,
416 dialog_vars.time_format,
418 if (used == 0 || *buffer == '\0')
419 DefaultFormat(buffer, hr_box);
422 DefaultFormat(buffer, hr_box);
424 dlg_add_result(buffer);
427 return CleanupResult(result, dialog, prompt, &save_vars);