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);
246 /* XXX: check that string doesn't exceed limit */
255 statusline_ask_boolean(char *msg, int def)
258 char *msg2 = strconcat(msg, def ? _(" (Y/n)?") : _(" (y/N)?"), NULL);
261 statusline_addstr(msg2);
265 ch = tolower(getch());
267 if(ch == *(S_("keybinding for no|n")))
269 else if(ch == *(S_("keybinding for yes|y")))
284 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
291 ask_filename(char *prompt)
297 buf = ui_readline(prompt, NULL, -1, 1);
318 display_help(int help)
334 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
336 headerline(_("help"));
338 for(i = 0; tbl[i] != NULL; i++) {
339 waddstr(helpw, gettext(tbl[i]));
340 if( (!((i + 1) % (LINES - 8))) ||
341 (tbl[i + 1] == NULL) ) {
344 refresh_statusline();
345 if(statusline_msg(_("Press any key to continue..."))
360 extern char *selected;
369 can_resize = TRUE; /* it's safe to resize now */
370 if(!opt_get_bool(BOOL_SHOW_CURSOR))
375 if(!opt_get_bool(BOOL_SHOW_CURSOR))
377 can_resize = FALSE; /* it's not safe to resize anymore */
380 case 'Q': quit_abook(QUIT_DONTSAVE); break;
381 case 'P': print_stderr(selected_items() ?
382 -1 : list_current_item());
385 display_help(HELP_MAIN);
388 case 'a': add_item(); break;
389 case '\r': edit_item(-1); break;
392 case 'r': ui_remove_items(); break;
393 case 'D': duplicate_item(); break;
394 case 12: refresh_screen(); break;
397 case KEY_UP: scroll_up(); break;
399 case KEY_DOWN: scroll_down(); break;
401 case KEY_PPAGE: page_up(); break;
403 case KEY_NPAGE: page_down(); break;
406 case KEY_HOME: goto_home(); break;
408 case KEY_END: goto_end(); break;
410 case 'w': save_database();
412 case 'l': ui_read_database(); break;
413 case 'i': import_database(); break;
414 case 'e': export_database(); break;
415 case 'C': ui_clear_database(); break;
417 case 'o': ui_open_datafile(); break;
419 case 's': sort_by_field(NAME); break;
420 case 'S': sort_surname(); break;
421 case 'F': sort_by_field(-1); break;
423 case '/': ui_find(0); break;
424 case '\\': ui_find(1); break;
426 case ' ': if(curitem >= 0) {
427 selected[curitem] = !selected[curitem];
428 ui_print_number_of_items();
432 case '+': select_all();
435 case '-': select_none();
438 case '*': invert_selection();
441 case 'A': move_curitem(MOVE_ITEM_UP);
443 case 'Z': move_curitem(MOVE_ITEM_DOWN);
446 case 'm': launch_mutt(selected_items() ?
447 -1 : list_current_item());
451 case 'p': ui_print_database(); break;
453 case 'v': launch_wwwbrowser(list_current_item());
466 if(statusline_ask_boolean(_("Remove selected item(s)"), TRUE))
467 remove_selected_items();
476 if(statusline_ask_boolean(_("Clear WHOLE database"), FALSE)) {
486 static char findstr[MAX_FIELD_LEN];
487 int search_fields[] = {NAME, EMAIL, NICK, -1};
496 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
497 strncpy(findstr, s, MAX_FIELD_LEN);
502 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
503 (item = find_item(findstr, 0, search_fields)) >= 0)
504 statusline_addstr(_("Search hit bottom, continuing at top"));
514 ui_print_number_of_items()
516 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
518 mvaddstr(0, COLS-strlen(str), str);
527 if(!statusline_ask_boolean(_("Your current data will be lost - "
528 "Press 'y' to continue"), FALSE))
531 load_database(datafile);
540 char *command = opt_get_str(STR_PRINT_COMMAND);
546 statusline_addstr(_("Print All/Selected/Cancel (a/s/C)?"));
548 switch(tolower(getch())) {
553 if( !selected_items() ) {
554 statusline_msg(_("No selected items"));
557 mode = ENUM_SELECTED;
566 if( ! *command || (handle = popen(command, "w")) == NULL)
569 fexport("text", handle, mode);
580 filename = ask_filename(_("File to open: "));
582 if(!filename || ! *filename) {
588 if(opt_get_bool(BOOL_AUTOSAVE))
590 else if(statusline_ask_boolean(_("Save current database"), FALSE))
595 load_database(filename);
598 statusline_msg(_("Sorry, the specified file appears not to be a valid abook addressbook"));
599 load_database(datafile);
602 datafile = xstrdup(filename);
608 alternative_datafile = TRUE;