2011-01-13 03:21:23 +01:00
|
|
|
/*
|
2021-02-26 10:05:35 +01:00
|
|
|
* $Id: timebox.c,v 1.69 2020/11/23 09:04:00 tom Exp $
|
2011-01-13 03:21:23 +01:00
|
|
|
*
|
|
|
|
* timebox.c -- implements the timebox dialog
|
|
|
|
*
|
2021-02-26 10:05:35 +01:00
|
|
|
* Copyright 2001-2019,2020 Thomas E. Dickey
|
2011-01-13 03:21:23 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License, version 2.1
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, write to
|
|
|
|
* Free Software Foundation, Inc.
|
|
|
|
* 51 Franklin St., Fifth Floor
|
|
|
|
* Boston, MA 02110, USA.
|
|
|
|
*/
|
|
|
|
|
2021-02-26 10:05:35 +01:00
|
|
|
#include <dlg_internals.h>
|
2011-01-13 03:21:23 +01:00
|
|
|
#include <dlg_keys.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define ONE_HIGH 1
|
|
|
|
#define ONE_WIDE 2
|
|
|
|
#define BTN_HIGH 2
|
|
|
|
|
|
|
|
#define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN))
|
|
|
|
#define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN))
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
sHR = -3
|
|
|
|
,sMN = -2
|
|
|
|
,sSC = -1
|
|
|
|
} STATES;
|
|
|
|
|
|
|
|
struct _box;
|
|
|
|
|
|
|
|
typedef struct _box {
|
|
|
|
WINDOW *parent;
|
|
|
|
WINDOW *window;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int period;
|
|
|
|
int value;
|
|
|
|
} BOX;
|
|
|
|
|
|
|
|
static int
|
|
|
|
next_or_previous(int key)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case DLGK_ITEM_PREV:
|
|
|
|
result = -1;
|
|
|
|
break;
|
|
|
|
case DLGK_ITEM_NEXT:
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
beep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Draw the hour-of-month selection box
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
draw_cell(BOX * data)
|
|
|
|
{
|
|
|
|
werase(data->window);
|
|
|
|
dlg_draw_box(data->parent,
|
|
|
|
data->y - MARGIN, data->x - MARGIN,
|
|
|
|
data->height + (2 * MARGIN), data->width + (2 * MARGIN),
|
2012-10-21 20:18:09 +02:00
|
|
|
menubox_border_attr, menubox_border2_attr);
|
2011-01-13 03:21:23 +01:00
|
|
|
|
2018-10-20 22:32:57 +02:00
|
|
|
dlg_attrset(data->window, item_attr);
|
2011-01-13 03:21:23 +01:00
|
|
|
wprintw(data->window, "%02d", data->value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
init_object(BOX * data,
|
|
|
|
WINDOW *parent,
|
|
|
|
int x, int y,
|
|
|
|
int width, int height,
|
|
|
|
int period, int value,
|
|
|
|
int code)
|
|
|
|
{
|
2011-04-17 19:00:55 +02:00
|
|
|
(void) code;
|
|
|
|
|
2011-01-13 03:21:23 +01:00
|
|
|
data->parent = parent;
|
|
|
|
data->x = x;
|
|
|
|
data->y = y;
|
|
|
|
data->width = width;
|
|
|
|
data->height = height;
|
|
|
|
data->period = period;
|
|
|
|
data->value = value % period;
|
|
|
|
|
2021-02-26 10:05:35 +01:00
|
|
|
data->window = dlg_der_window(data->parent,
|
|
|
|
data->height, data->width,
|
|
|
|
data->y, data->x);
|
2011-01-13 03:21:23 +01:00
|
|
|
if (data->window == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
dlg_mouse_setbase(getbegx(parent), getbegy(parent));
|
|
|
|
dlg_mouse_mkregion(y, x, height, width, code);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars)
|
|
|
|
{
|
|
|
|
dlg_del_window(dialog);
|
|
|
|
dlg_mouse_free_regions();
|
|
|
|
free(prompt);
|
|
|
|
dlg_restore_vars(save_vars);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DrawObject(data) draw_cell(data)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display a dialog box for entering a date
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dialog_timebox(const char *title,
|
|
|
|
const char *subtitle,
|
|
|
|
int height,
|
|
|
|
int width,
|
|
|
|
int hour,
|
|
|
|
int minute,
|
|
|
|
int second)
|
|
|
|
{
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
static DLG_KEYS_BINDING binding[] = {
|
|
|
|
DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ),
|
2011-07-14 15:57:13 +02:00
|
|
|
HELPKEY_BINDINGS,
|
2011-01-13 03:21:23 +01:00
|
|
|
ENTERKEY_BINDINGS,
|
2018-10-20 22:32:57 +02:00
|
|
|
TOGGLEKEY_BINDINGS,
|
2011-01-13 03:21:23 +01:00
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ),
|
|
|
|
DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+'),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NPAGE),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PPAGE ),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ),
|
|
|
|
DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ),
|
|
|
|
END_KEYS_BINDING
|
|
|
|
};
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
#ifdef KEY_RESIZE
|
|
|
|
int old_height = height;
|
|
|
|
int old_width = width;
|
|
|
|
#endif
|
|
|
|
BOX hr_box, mn_box, sc_box;
|
2021-02-26 10:05:35 +01:00
|
|
|
int key, fkey;
|
2011-01-13 03:21:23 +01:00
|
|
|
int button;
|
|
|
|
int result = DLG_EXIT_UNKNOWN;
|
|
|
|
WINDOW *dialog;
|
2021-02-26 10:05:35 +01:00
|
|
|
time_t now_time;
|
2011-01-13 03:21:23 +01:00
|
|
|
struct tm current;
|
2012-10-21 20:18:09 +02:00
|
|
|
int state = dlg_default_button();
|
2011-01-13 03:21:23 +01:00
|
|
|
const char **buttons = dlg_ok_labels();
|
2018-10-20 22:32:57 +02:00
|
|
|
char *prompt;
|
2011-01-13 03:21:23 +01:00
|
|
|
char buffer[MAX_LEN];
|
|
|
|
DIALOG_VARS save_vars;
|
|
|
|
|
2018-10-20 22:32:57 +02:00
|
|
|
DLG_TRACE(("# timebox args:\n"));
|
|
|
|
DLG_TRACE2S("title", title);
|
|
|
|
DLG_TRACE2S("message", subtitle);
|
|
|
|
DLG_TRACE2N("height", height);
|
|
|
|
DLG_TRACE2N("width", width);
|
|
|
|
DLG_TRACE2N("hour", hour);
|
|
|
|
DLG_TRACE2N("minute", minute);
|
|
|
|
DLG_TRACE2N("second", second);
|
|
|
|
|
2011-01-13 03:21:23 +01:00
|
|
|
now_time = time((time_t *) 0);
|
|
|
|
current = *localtime(&now_time);
|
|
|
|
|
|
|
|
dlg_save_vars(&save_vars);
|
|
|
|
dialog_vars.separate_output = TRUE;
|
|
|
|
|
|
|
|
dlg_does_output();
|
|
|
|
|
|
|
|
#ifdef KEY_RESIZE
|
|
|
|
retry:
|
|
|
|
#endif
|
|
|
|
|
2018-10-20 22:32:57 +02:00
|
|
|
prompt = dlg_strclone(subtitle);
|
2021-02-26 10:05:35 +01:00
|
|
|
dlg_auto_size(title, prompt, &height, &width, MIN_HIGH, MIN_WIDE);
|
2018-10-20 22:32:57 +02:00
|
|
|
|
2011-01-13 03:21:23 +01:00
|
|
|
dlg_button_layout(buttons, &width);
|
|
|
|
dlg_print_size(height, width);
|
|
|
|
dlg_ctl_size(height, width);
|
|
|
|
|
|
|
|
dialog = dlg_new_window(height, width,
|
|
|
|
dlg_box_y_ordinate(height),
|
|
|
|
dlg_box_x_ordinate(width));
|
2011-04-17 19:00:55 +02:00
|
|
|
|
|
|
|
if (hour >= 24 || minute >= 60 || second >= 60) {
|
|
|
|
return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
|
|
|
|
}
|
|
|
|
|
2011-01-13 03:21:23 +01:00
|
|
|
dlg_register_window(dialog, "timebox", binding);
|
|
|
|
dlg_register_buttons(dialog, "timebox", buttons);
|
|
|
|
|
2012-10-21 20:18:09 +02:00
|
|
|
dlg_draw_box2(dialog, 0, 0, height, width, dialog_attr, border_attr, border2_attr);
|
|
|
|
dlg_draw_bottom_box2(dialog, border_attr, border2_attr, dialog_attr);
|
2011-01-13 03:21:23 +01:00
|
|
|
dlg_draw_title(dialog, title);
|
2011-07-14 15:57:13 +02:00
|
|
|
dlg_draw_helpline(dialog, FALSE);
|
2011-01-13 03:21:23 +01:00
|
|
|
|
2018-10-20 22:32:57 +02:00
|
|
|
dlg_attrset(dialog, dialog_attr);
|
2011-01-13 03:21:23 +01:00
|
|
|
dlg_print_autowrap(dialog, prompt, height, width);
|
|
|
|
|
|
|
|
/* compute positions of hour, month and year boxes */
|
|
|
|
memset(&hr_box, 0, sizeof(hr_box));
|
|
|
|
memset(&mn_box, 0, sizeof(mn_box));
|
|
|
|
memset(&sc_box, 0, sizeof(sc_box));
|
|
|
|
|
|
|
|
if (init_object(&hr_box,
|
|
|
|
dialog,
|
|
|
|
(width - MIN_WIDE + 1) / 2 + MARGIN,
|
|
|
|
(height - MIN_HIGH + MARGIN),
|
|
|
|
ONE_WIDE,
|
|
|
|
ONE_HIGH,
|
|
|
|
24,
|
|
|
|
hour >= 0 ? hour : current.tm_hour,
|
|
|
|
'H') < 0
|
|
|
|
|| DrawObject(&hr_box) < 0) {
|
|
|
|
return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":");
|
|
|
|
if (init_object(&mn_box,
|
|
|
|
dialog,
|
|
|
|
hr_box.x + (ONE_WIDE + 2 * MARGIN + 1),
|
|
|
|
hr_box.y,
|
|
|
|
hr_box.width,
|
|
|
|
hr_box.height,
|
|
|
|
60,
|
|
|
|
minute >= 0 ? minute : current.tm_min,
|
|
|
|
'M') < 0
|
|
|
|
|| DrawObject(&mn_box) < 0) {
|
|
|
|
return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
|
|
|
|
}
|
|
|
|
|
|
|
|
mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":");
|
|
|
|
if (init_object(&sc_box,
|
|
|
|
dialog,
|
|
|
|
mn_box.x + (ONE_WIDE + 2 * MARGIN + 1),
|
|
|
|
mn_box.y,
|
|
|
|
mn_box.width,
|
|
|
|
mn_box.height,
|
|
|
|
60,
|
|
|
|
second >= 0 ? second : current.tm_sec,
|
|
|
|
'S') < 0
|
|
|
|
|| DrawObject(&sc_box) < 0) {
|
|
|
|
return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars);
|
|
|
|
}
|
|
|
|
|
2012-10-21 20:18:09 +02:00
|
|
|
dlg_trace_win(dialog);
|
2011-01-13 03:21:23 +01:00
|
|
|
while (result == DLG_EXIT_UNKNOWN) {
|
|
|
|
BOX *obj = (state == sHR ? &hr_box
|
|
|
|
: (state == sMN ? &mn_box :
|
|
|
|
(state == sSC ? &sc_box : 0)));
|
2021-02-26 10:05:35 +01:00
|
|
|
int key2;
|
2011-01-13 03:21:23 +01:00
|
|
|
|
|
|
|
button = (state < 0) ? 0 : state;
|
|
|
|
dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width);
|
|
|
|
if (obj != 0)
|
|
|
|
dlg_set_focus(dialog, obj->window);
|
|
|
|
|
|
|
|
key = dlg_mouse_wgetch(dialog, &fkey);
|
2021-02-26 10:05:35 +01:00
|
|
|
if (dlg_result_key(key, fkey, &result)) {
|
|
|
|
if (!dlg_button_key(result, &button, &key, &fkey))
|
|
|
|
break;
|
|
|
|
}
|
2011-01-13 03:21:23 +01:00
|
|
|
|
|
|
|
if ((key2 = dlg_char_to_button(key, buttons)) >= 0) {
|
|
|
|
result = key2;
|
|
|
|
} else {
|
|
|
|
/* handle function-keys */
|
|
|
|
if (fkey) {
|
|
|
|
switch (key) {
|
|
|
|
case DLGK_MOUSE('H'):
|
|
|
|
state = sHR;
|
|
|
|
break;
|
|
|
|
case DLGK_MOUSE('M'):
|
|
|
|
state = sMN;
|
|
|
|
break;
|
|
|
|
case DLGK_MOUSE('S'):
|
|
|
|
state = sSC;
|
|
|
|
break;
|
2018-10-20 22:32:57 +02:00
|
|
|
case DLGK_TOGGLE:
|
2011-01-13 03:21:23 +01:00
|
|
|
case DLGK_ENTER:
|
2021-02-26 10:05:35 +01:00
|
|
|
result = dlg_enter_buttoncode(button);
|
|
|
|
break;
|
|
|
|
case DLGK_LEAVE:
|
2012-10-21 20:18:09 +02:00
|
|
|
result = dlg_ok_buttoncode(button);
|
2011-01-13 03:21:23 +01:00
|
|
|
break;
|
|
|
|
case DLGK_FIELD_PREV:
|
|
|
|
state = dlg_prev_ok_buttonindex(state, sHR);
|
|
|
|
break;
|
|
|
|
case DLGK_FIELD_NEXT:
|
|
|
|
state = dlg_next_ok_buttonindex(state, sHR);
|
|
|
|
break;
|
|
|
|
case DLGK_FIELD_FIRST:
|
|
|
|
if (obj != 0) {
|
|
|
|
obj->value = 0;
|
|
|
|
(void) DrawObject(obj);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DLGK_FIELD_LAST:
|
|
|
|
if (obj != 0) {
|
|
|
|
switch (state) {
|
|
|
|
case sHR:
|
|
|
|
obj->value = 23;
|
|
|
|
break;
|
|
|
|
case sMN:
|
|
|
|
case sSC:
|
|
|
|
obj->value = 59;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(void) DrawObject(obj);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DLGK_DELETE_RIGHT:
|
|
|
|
if (obj != 0) {
|
|
|
|
obj->value /= 10;
|
|
|
|
(void) DrawObject(obj);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef KEY_RESIZE
|
|
|
|
case KEY_RESIZE:
|
2018-10-20 22:32:57 +02:00
|
|
|
dlg_will_resize(dialog);
|
2011-01-13 03:21:23 +01:00
|
|
|
/* reset data */
|
|
|
|
height = old_height;
|
|
|
|
width = old_width;
|
|
|
|
hour = hr_box.value;
|
|
|
|
minute = mn_box.value;
|
|
|
|
second = sc_box.value;
|
|
|
|
/* repaint */
|
2018-10-20 22:32:57 +02:00
|
|
|
free(prompt);
|
2021-02-26 10:05:35 +01:00
|
|
|
_dlg_resize_cleanup(dialog);
|
2011-01-13 03:21:23 +01:00
|
|
|
goto retry;
|
|
|
|
#endif
|
|
|
|
default:
|
2012-10-21 20:18:09 +02:00
|
|
|
if (is_DLGK_MOUSE(key)) {
|
|
|
|
result = dlg_ok_buttoncode(key - M_EVENT);
|
|
|
|
if (result < 0)
|
|
|
|
result = DLG_EXIT_OK;
|
|
|
|
} else if (obj != 0) {
|
2011-01-13 03:21:23 +01:00
|
|
|
int step = next_or_previous(key);
|
|
|
|
if (step != 0) {
|
|
|
|
obj->value += step;
|
|
|
|
while (obj->value < 0)
|
|
|
|
obj->value += obj->period;
|
|
|
|
obj->value %= obj->period;
|
|
|
|
(void) DrawObject(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (isdigit(key)) {
|
|
|
|
if (obj != 0) {
|
|
|
|
int digit = (key - '0');
|
|
|
|
int value = (obj->value * 10) + digit;
|
|
|
|
if (value < obj->period) {
|
|
|
|
obj->value = value;
|
|
|
|
(void) DrawObject(obj);
|
|
|
|
} else {
|
|
|
|
beep();
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 10:05:35 +01:00
|
|
|
} else if (key > 0) {
|
2011-01-13 03:21:23 +01:00
|
|
|
beep();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DefaultFormat(dst, src) \
|
|
|
|
sprintf(dst, "%02d:%02d:%02d", \
|
|
|
|
hr_box.value, mn_box.value, sc_box.value)
|
|
|
|
|
|
|
|
#if defined(HAVE_STRFTIME)
|
|
|
|
if (dialog_vars.time_format != 0) {
|
|
|
|
size_t used;
|
|
|
|
time_t now = time((time_t *) 0);
|
|
|
|
struct tm *parts = localtime(&now);
|
|
|
|
|
|
|
|
parts->tm_sec = sc_box.value;
|
|
|
|
parts->tm_min = mn_box.value;
|
|
|
|
parts->tm_hour = hr_box.value;
|
|
|
|
used = strftime(buffer,
|
|
|
|
sizeof(buffer) - 1,
|
|
|
|
dialog_vars.time_format,
|
|
|
|
parts);
|
|
|
|
if (used == 0 || *buffer == '\0')
|
|
|
|
DefaultFormat(buffer, hr_box);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
DefaultFormat(buffer, hr_box);
|
|
|
|
|
|
|
|
dlg_add_result(buffer);
|
2021-02-26 10:05:35 +01:00
|
|
|
AddLastKey();
|
2011-01-13 03:21:23 +01:00
|
|
|
|
|
|
|
return CleanupResult(result, dialog, prompt, &save_vars);
|
|
|
|
}
|