1 /* 2 * $Id: timebox.c,v 1.43 2011/01/18 01:07:45 Garrett.Cooper Exp $ 3 * 4 * timebox.c -- implements the timebox dialog 5 * 6 * Copyright 2001-2010,2011 Thomas E. Dickey 7 * 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. 11 * 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. 16 * 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. 22 */ 23 24 #include <dialog.h> 25 #include <dlg_keys.h> 26 27 #include <time.h> 28 29 #define ONE_HIGH 1 30 #define ONE_WIDE 2 31 #define BTN_HIGH 2 32 33 #define MIN_HIGH (ONE_HIGH + BTN_HIGH + (4 * MARGIN)) 34 #define MIN_WIDE ((3 * (ONE_WIDE + 2 * MARGIN)) + 2 + (2 * MARGIN)) 35 36 typedef enum { 37 sHR = -3 38 ,sMN = -2 39 ,sSC = -1 40 } STATES; 41 42 struct _box; 43 44 typedef int (*BOX_DRAW) (struct _box *, struct tm *); 45 46 typedef struct _box { 47 WINDOW *parent; 48 WINDOW *window; 49 int x; 50 int y; 51 int width; 52 int height; 53 int period; 54 int value; 55 } BOX; 56 57 static int 58 next_or_previous(int key) 59 { 60 int result = 0; 61 62 switch (key) { 63 case DLGK_ITEM_PREV: 64 result = -1; 65 break; 66 case DLGK_ITEM_NEXT: 67 result = 1; 68 break; 69 default: 70 beep(); 71 break; 72 } 73 return result; 74 } 75 /* 76 * Draw the hour-of-month selection box 77 */ 78 static int 79 draw_cell(BOX * data) 80 { 81 werase(data->window); 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); 86 87 wattrset(data->window, item_attr); 88 wprintw(data->window, "%02d", data->value); 89 return 0; 90 } 91 92 static int 93 init_object(BOX * data, 94 WINDOW *parent, 95 int x, int y, 96 int width, int height, 97 int period, int value, 98 int code) 99 { 100 (void) code; 101 102 data->parent = parent; 103 data->x = x; 104 data->y = y; 105 data->width = width; 106 data->height = height; 107 data->period = period; 108 data->value = value % period; 109 110 data->window = derwin(data->parent, 111 data->height, data->width, 112 data->y, data->x); 113 if (data->window == 0) 114 return -1; 115 (void) keypad(data->window, TRUE); 116 117 dlg_mouse_setbase(getbegx(parent), getbegy(parent)); 118 dlg_mouse_mkregion(y, x, height, width, code); 119 120 return 0; 121 } 122 123 static int 124 CleanupResult(int code, WINDOW *dialog, char *prompt, DIALOG_VARS * save_vars) 125 { 126 dlg_del_window(dialog); 127 dlg_mouse_free_regions(); 128 free(prompt); 129 dlg_restore_vars(save_vars); 130 131 return code; 132 } 133 134 #define DrawObject(data) draw_cell(data) 135 136 /* 137 * Display a dialog box for entering a date 138 */ 139 int 140 dialog_timebox(const char *title, 141 const char *subtitle, 142 int height, 143 int width, 144 int hour, 145 int minute, 146 int second) 147 { 148 /* *INDENT-OFF* */ 149 static DLG_KEYS_BINDING binding[] = { 150 DLG_KEYS_DATA( DLGK_DELETE_RIGHT,KEY_DC ), 151 ENTERKEY_BINDINGS, 152 DLG_KEYS_DATA( DLGK_ENTER, ' ' ), 153 DLG_KEYS_DATA( DLGK_FIELD_FIRST,KEY_HOME ), 154 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_END ), 155 DLG_KEYS_DATA( DLGK_FIELD_LAST, KEY_LL ), 156 DLG_KEYS_DATA( DLGK_FIELD_NEXT, CHR_NEXT ), 157 DLG_KEYS_DATA( DLGK_FIELD_NEXT, KEY_RIGHT ), 158 DLG_KEYS_DATA( DLGK_FIELD_NEXT, TAB ), 159 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_BACKSPACE ), 160 DLG_KEYS_DATA( DLGK_FIELD_PREV, CHR_PREVIOUS ), 161 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_BTAB ), 162 DLG_KEYS_DATA( DLGK_FIELD_PREV, KEY_LEFT ), 163 DLG_KEYS_DATA( DLGK_ITEM_NEXT, '+'), 164 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_DOWN), 165 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NEXT), 166 DLG_KEYS_DATA( DLGK_ITEM_NEXT, KEY_NPAGE), 167 DLG_KEYS_DATA( DLGK_ITEM_PREV, '-' ), 168 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PPAGE ), 169 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_PREVIOUS ), 170 DLG_KEYS_DATA( DLGK_ITEM_PREV, KEY_UP ), 171 END_KEYS_BINDING 172 }; 173 /* *INDENT-ON* */ 174 175 #ifdef KEY_RESIZE 176 int old_height = height; 177 int old_width = width; 178 #endif 179 BOX hr_box, mn_box, sc_box; 180 int key = 0, key2, fkey; 181 int button; 182 int result = DLG_EXIT_UNKNOWN; 183 WINDOW *dialog; 184 time_t now_time = time((time_t *) 0); 185 struct tm current; 186 int state = dlg_defaultno_button(); 187 const char **buttons = dlg_ok_labels(); 188 char *prompt = dlg_strclone(subtitle); 189 char buffer[MAX_LEN]; 190 DIALOG_VARS save_vars; 191 192 now_time = time((time_t *) 0); 193 current = *localtime(&now_time); 194 195 dlg_save_vars(&save_vars); 196 dialog_vars.separate_output = TRUE; 197 198 dlg_does_output(); 199 200 #ifdef KEY_RESIZE 201 retry: 202 #endif 203 204 dlg_auto_size(title, prompt, &height, &width, 0, 0); 205 height += MIN_HIGH; 206 if (width < MIN_WIDE) 207 width = MIN_WIDE; 208 dlg_button_layout(buttons, &width); 209 dlg_print_size(height, width); 210 dlg_ctl_size(height, width); 211 212 dialog = dlg_new_window(height, width, 213 dlg_box_y_ordinate(height), 214 dlg_box_x_ordinate(width)); 215 216 if (hour >= 24 || minute >= 60 || second >= 60) { 217 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars); 218 } 219 220 dlg_register_window(dialog, "timebox", binding); 221 dlg_register_buttons(dialog, "timebox", buttons); 222 223 dlg_draw_box(dialog, 0, 0, height, width, dialog_attr, border_attr); 224 dlg_draw_bottom_box(dialog); 225 dlg_draw_title(dialog, title); 226 227 wattrset(dialog, dialog_attr); 228 dlg_print_autowrap(dialog, prompt, height, width); 229 230 /* compute positions of hour, month and year boxes */ 231 memset(&hr_box, 0, sizeof(hr_box)); 232 memset(&mn_box, 0, sizeof(mn_box)); 233 memset(&sc_box, 0, sizeof(sc_box)); 234 235 if (init_object(&hr_box, 236 dialog, 237 (width - MIN_WIDE + 1) / 2 + MARGIN, 238 (height - MIN_HIGH + MARGIN), 239 ONE_WIDE, 240 ONE_HIGH, 241 24, 242 hour >= 0 ? hour : current.tm_hour, 243 'H') < 0 244 || DrawObject(&hr_box) < 0) { 245 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars); 246 } 247 248 mvwprintw(dialog, hr_box.y, hr_box.x + ONE_WIDE + MARGIN, ":"); 249 if (init_object(&mn_box, 250 dialog, 251 hr_box.x + (ONE_WIDE + 2 * MARGIN + 1), 252 hr_box.y, 253 hr_box.width, 254 hr_box.height, 255 60, 256 minute >= 0 ? minute : current.tm_min, 257 'M') < 0 258 || DrawObject(&mn_box) < 0) { 259 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars); 260 } 261 262 mvwprintw(dialog, mn_box.y, mn_box.x + ONE_WIDE + MARGIN, ":"); 263 if (init_object(&sc_box, 264 dialog, 265 mn_box.x + (ONE_WIDE + 2 * MARGIN + 1), 266 mn_box.y, 267 mn_box.width, 268 mn_box.height, 269 60, 270 second >= 0 ? second : current.tm_sec, 271 'S') < 0 272 || DrawObject(&sc_box) < 0) { 273 return CleanupResult(DLG_EXIT_ERROR, dialog, prompt, &save_vars); 274 } 275 276 while (result == DLG_EXIT_UNKNOWN) { 277 BOX *obj = (state == sHR ? &hr_box 278 : (state == sMN ? &mn_box : 279 (state == sSC ? &sc_box : 0))); 280 281 button = (state < 0) ? 0 : state; 282 dlg_draw_buttons(dialog, height - 2, 0, buttons, button, FALSE, width); 283 if (obj != 0) 284 dlg_set_focus(dialog, obj->window); 285 286 key = dlg_mouse_wgetch(dialog, &fkey); 287 if (dlg_result_key(key, fkey, &result)) 288 break; 289 290 if ((key2 = dlg_char_to_button(key, buttons)) >= 0) { 291 result = key2; 292 } else { 293 /* handle function-keys */ 294 if (fkey) { 295 switch (key) { 296 case DLGK_MOUSE(0): 297 result = DLG_EXIT_OK; 298 break; 299 case DLGK_MOUSE(1): 300 result = DLG_EXIT_CANCEL; 301 break; 302 case DLGK_MOUSE('H'): 303 state = sHR; 304 break; 305 case DLGK_MOUSE('M'): 306 state = sMN; 307 break; 308 case DLGK_MOUSE('S'): 309 state = sSC; 310 break; 311 case DLGK_ENTER: 312 result = button; 313 break; 314 case DLGK_FIELD_PREV: 315 state = dlg_prev_ok_buttonindex(state, sHR); 316 break; 317 case DLGK_FIELD_NEXT: 318 state = dlg_next_ok_buttonindex(state, sHR); 319 break; 320 case DLGK_FIELD_FIRST: 321 if (obj != 0) { 322 obj->value = 0; 323 (void) DrawObject(obj); 324 } 325 break; 326 case DLGK_FIELD_LAST: 327 if (obj != 0) { 328 switch (state) { 329 case sHR: 330 obj->value = 23; 331 break; 332 case sMN: 333 case sSC: 334 obj->value = 59; 335 break; 336 } 337 (void) DrawObject(obj); 338 } 339 break; 340 case DLGK_DELETE_RIGHT: 341 if (obj != 0) { 342 obj->value /= 10; 343 (void) DrawObject(obj); 344 } 345 break; 346 #ifdef KEY_RESIZE 347 case KEY_RESIZE: 348 /* reset data */ 349 height = old_height; 350 width = old_width; 351 hour = hr_box.value; 352 minute = mn_box.value; 353 second = sc_box.value; 354 /* repaint */ 355 dlg_clear(); 356 dlg_del_window(dialog); 357 refresh(); 358 dlg_mouse_free_regions(); 359 goto retry; 360 #endif 361 default: 362 if (obj != 0) { 363 int step = next_or_previous(key); 364 if (step != 0) { 365 obj->value += step; 366 while (obj->value < 0) 367 obj->value += obj->period; 368 obj->value %= obj->period; 369 (void) DrawObject(obj); 370 } 371 } 372 break; 373 } 374 } else if (isdigit(key)) { 375 if (obj != 0) { 376 int digit = (key - '0'); 377 int value = (obj->value * 10) + digit; 378 if (value < obj->period) { 379 obj->value = value; 380 (void) DrawObject(obj); 381 } else { 382 beep(); 383 } 384 } 385 } else { 386 beep(); 387 } 388 } 389 } 390 391 #define DefaultFormat(dst, src) \ 392 sprintf(dst, "%02d:%02d:%02d", \ 393 hr_box.value, mn_box.value, sc_box.value) 394 395 #if defined(HAVE_STRFTIME) 396 if (dialog_vars.time_format != 0) { 397 size_t used; 398 time_t now = time((time_t *) 0); 399 struct tm *parts = localtime(&now); 400 401 parts->tm_sec = sc_box.value; 402 parts->tm_min = mn_box.value; 403 parts->tm_hour = hr_box.value; 404 used = strftime(buffer, 405 sizeof(buffer) - 1, 406 dialog_vars.time_format, 407 parts); 408 if (used == 0 || *buffer == '\0') 409 DefaultFormat(buffer, hr_box); 410 } else 411 #endif 412 DefaultFormat(buffer, hr_box); 413 414 dlg_add_result(buffer); 415 dlg_add_separator(); 416 417 return CleanupResult(result, dialog, prompt, &save_vars); 418 } 419