5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
30 #ifdef HAVE_SYS_IOCTL_H
31 # include <sys/ioctl.h>
40 extern int items, curitem;
41 extern char *datafile;
43 extern bool alternative_datafile;
49 static bool ui_initialized = FALSE;
51 static bool should_resize = FALSE;
52 static bool can_resize = FALSE;
54 static WINDOW *top = NULL, *bottom = NULL;
60 top = newwin(LIST_TOP - 1, COLS, 0, 0);
61 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
79 ioctl(0, TIOCGWINSZ, &winsz);
81 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
82 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
86 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
87 #ifdef HAVE_RESIZETERM
88 resizeterm(winsz.ws_row, winsz.ws_col);
95 should_resize = FALSE;
96 close_list(); /* we need to recreate windows */
102 #endif /* TIOCGWINSZ */
112 should_resize = TRUE;
114 #endif /* SIGWINCH */
120 return ui_initialized;
126 if(!is_ui_initialized())
131 intrflush(stdscr, FALSE);
132 keypad(stdscr, TRUE);
140 fprintf(stderr, "init_abook():\n");
141 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
143 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
144 clear(); refresh(); endwin();
145 fprintf(stderr, _("Your terminal size is %dx%d\n"), COLS, LINES);
146 fprintf(stderr, _("Terminal is too small. Minium terminal size "
148 "%dx%d\n"), MIN_COLS, MIN_LINES);
155 ui_initialized = TRUE;
158 signal(SIGWINCH, win_changed);
173 ui_initialized = FALSE;
178 headerline(const char *str)
182 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
184 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
202 refresh_statusline();
203 headerline(gettext(MAIN_HELPLINE));
211 statusline_msg(const char *msg)
216 statusline_addstr(msg);
219 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
227 statusline_addstr(const char *str)
229 mvwaddstr(bottom, 1, 0, str);
235 ui_readline(char *prompt, char *s, size_t limit, bool use_completion)
240 mvwaddstr(bottom, 1, 0, prompt);
244 ret = abook_readline(bottom, y, x, s, use_completion);
248 if(strlen(ret) > limit && limit > 0)
256 statusline_ask_boolean(char *msg, int def)
259 char *msg2 = strconcat(msg, def ? _(" (Y/n)?") : _(" (y/N)?"), NULL);
262 statusline_addstr(msg2);
266 ch = tolower(getch());
268 if(ch == *(S_("keybinding for no|n")))
270 else if(ch == *(S_("keybinding for yes|y")))
285 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
292 ask_filename(char *prompt)
298 buf = ui_readline(prompt, NULL, -1, 1);
319 display_help(int help)
335 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
337 headerline(_("help"));
339 for(i = 0; tbl[i] != NULL; i++) {
340 waddstr(helpw, gettext(tbl[i]));
341 if( (!((i + 1) % (LINES - 8))) ||
342 (tbl[i + 1] == NULL) ) {
345 refresh_statusline();
346 if(statusline_msg(_("Press any key to continue..."))
361 extern char *selected;
370 can_resize = TRUE; /* it's safe to resize now */
371 if(!opt_get_bool(BOOL_SHOW_CURSOR))
376 if(!opt_get_bool(BOOL_SHOW_CURSOR))
378 can_resize = FALSE; /* it's not safe to resize anymore */
381 case 'Q': quit_abook(QUIT_DONTSAVE); break;
382 case 'P': print_stderr(selected_items() ?
383 -1 : list_current_item());
386 display_help(HELP_MAIN);
389 case 'a': add_item(); break;
390 case '\r': edit_item(-1); break;
393 case 'r': ui_remove_items(); break;
394 case 'D': duplicate_item(); break;
395 case 12: refresh_screen(); break;
398 case KEY_UP: scroll_up(); break;
400 case KEY_DOWN: scroll_down(); break;
402 case KEY_PPAGE: page_up(); break;
404 case KEY_NPAGE: page_down(); break;
407 case KEY_HOME: goto_home(); break;
409 case KEY_END: goto_end(); break;
411 case 'w': save_database();
413 case 'l': ui_read_database(); break;
414 case 'i': import_database(); break;
415 case 'e': export_database(); break;
416 case 'C': ui_clear_database(); break;
418 case 'o': ui_open_datafile(); break;
420 case 's': sort_by_field(NAME); break;
421 case 'S': sort_surname(); break;
422 case 'F': sort_by_field(-1); break;
424 case '/': ui_find(0); break;
425 case '\\': ui_find(1); break;
427 case ' ': if(curitem >= 0) {
428 selected[curitem] = !selected[curitem];
429 ui_print_number_of_items();
433 case '+': select_all();
436 case '-': select_none();
439 case '*': invert_selection();
442 case 'A': move_curitem(MOVE_ITEM_UP);
444 case 'Z': move_curitem(MOVE_ITEM_DOWN);
447 case 'm': launch_mutt(selected_items() ?
448 -1 : list_current_item());
452 case 'p': ui_print_database(); break;
454 case 'v': launch_wwwbrowser(list_current_item());
467 if(statusline_ask_boolean(_("Remove selected item(s)"), TRUE))
468 remove_selected_items();
477 if(statusline_ask_boolean(_("Clear WHOLE database"), FALSE)) {
487 static char findstr[MAX_FIELD_LEN];
488 int search_fields[] = {NAME, EMAIL, NICK, -1};
497 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
498 strncpy(findstr, s, MAX_FIELD_LEN);
503 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
504 (item = find_item(findstr, 0, search_fields)) >= 0)
505 statusline_addstr(_("Search hit bottom, continuing at top"));
515 ui_print_number_of_items()
517 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
519 mvaddstr(0, COLS-strlen(str), str);
528 if(!statusline_ask_boolean(_("Your current data will be lost - "
529 "Press 'y' to continue"), FALSE))
532 load_database(datafile);
541 char *command = opt_get_str(STR_PRINT_COMMAND);
547 statusline_addstr(_("Print All/Selected/Cancel (a/s/C)?"));
549 switch(tolower(getch())) {
554 if( !selected_items() ) {
555 statusline_msg(_("No selected items"));
558 mode = ENUM_SELECTED;
567 if( ! *command || (handle = popen(command, "w")) == NULL)
570 fexport("text", handle, mode);
581 filename = ask_filename(_("File to open: "));
583 if(!filename || ! *filename) {
589 if(opt_get_bool(BOOL_AUTOSAVE))
591 else if(statusline_ask_boolean(_("Save current database"), FALSE))
596 load_database(filename);
599 statusline_msg(_("Sorry, the specified file appears not to be a valid abook addressbook"));
600 load_database(datafile);
603 datafile = xstrdup(filename);
609 alternative_datafile = TRUE;