5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
31 # ifdef HAVE_LINUX_TERMIOS_H
32 # include <linux/termios.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 # include <sys/ioctl.h>
45 extern int items, curitem;
46 extern char *datafile;
48 extern bool alternative_datafile;
54 static bool ui_initialized = FALSE;
56 static bool should_resize = FALSE;
57 static bool can_resize = FALSE;
59 static WINDOW *top = NULL, *bottom = NULL;
65 top = newwin(LIST_TOP - 1, COLS, 0, 0);
67 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
85 ioctl(0, TIOCGWINSZ, &winsz);
87 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
88 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
92 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
93 #ifdef HAVE_RESIZETERM
94 resizeterm(winsz.ws_row, winsz.ws_col);
101 should_resize = FALSE;
102 close_list(); /* we need to recreate windows */
108 #endif /* TIOCGWINSZ */
118 should_resize = TRUE;
120 #endif /* SIGWINCH */
126 return ui_initialized;
132 if(!is_ui_initialized())
137 intrflush(stdscr, FALSE);
138 keypad(stdscr, TRUE);
146 fprintf(stderr, "init_abook():\n");
147 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
149 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
150 clear(); refresh(); endwin();
151 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
152 fprintf(stderr, "Terminal is too small. Minium terminal size "
154 "%dx%d\n", MIN_COLS, MIN_LINES);
159 signal(SIGWINCH, win_changed);
165 ui_initialized = TRUE;
179 ui_initialized = FALSE;
184 headerline(char *str)
188 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
190 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
201 if( should_resize ) {
208 refresh_statusline();
209 headerline(MAIN_HELPLINE);
217 statusline_msg(char *msg)
222 statusline_addstr(msg);
225 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
233 statusline_addstr(char *str)
235 mvwaddstr(bottom, 1, 0, str);
241 ui_readline(char *prompt, char *s, int limit, bool use_completion)
246 mvwaddstr(bottom, 1, 0, prompt);
250 ret = abook_readline(bottom, y, x, s, limit, use_completion);
259 statusline_ask_boolean(char *msg, int def)
262 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
264 statusline_addstr(msg2);
268 switch(tolower(getch())) {
293 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
301 ask_filename(char *prompt)
307 buf = ui_readline(prompt, NULL, -1, 1);
328 display_help(int help)
344 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
348 for(i = 0; tbl[i] != NULL; i++) {
349 waddstr(helpw, tbl[i]);
350 if( ( !( (i+1) % (LINES-8) ) ) ||
351 (tbl[i+1] == NULL) ) {
354 refresh_statusline();
355 if(statusline_msg("Press any key to continue...")
370 extern char *selected;
379 can_resize = TRUE; /* it's safe to resize now */
380 if(!opt_get_bool(BOOL_SHOW_CURSOR))
385 if(!opt_get_bool(BOOL_SHOW_CURSOR))
387 can_resize = FALSE; /* it's not safe to resize anymore */
390 case 'Q': quit_abook(QUIT_DONTSAVE); break;
391 case 'P': print_stderr(selected_items() ?
392 -1 : list_current_item());
395 display_help(HELP_MAIN);
398 case 'a': add_item(); break;
399 case '\r': edit_item(-1); break;
402 case 'r': ui_remove_items(); break;
403 case 'D': duplicate_item(); break;
404 case 12: refresh_screen(); break;
407 case KEY_UP: scroll_up(); break;
409 case KEY_DOWN: scroll_down(); break;
411 case KEY_PPAGE: page_up(); break;
413 case KEY_NPAGE: page_down(); break;
416 case KEY_HOME: goto_home(); break;
418 case KEY_END: goto_end(); break;
420 case 'w': save_database();
422 case 'l': ui_read_database(); break;
423 case 'i': import_database(); break;
424 case 'e': export_database(); break;
425 case 'C': ui_clear_database(); break;
427 case 'o': ui_open_datafile(); break;
429 case 's': sort_by_field(NAME); break;
430 case 'S': sort_surname(); break;
431 case 'F': sort_by_field(-1); break;
433 case '/': ui_find(0); break;
434 case '\\': ui_find(1); break;
436 case ' ': if(curitem >= 0) {
437 selected[curitem] = !selected[curitem];
438 ui_print_number_of_items();
442 case '+': select_all();
445 case '-': select_none();
448 case '*': invert_selection();
451 case 'A': move_curitem(MOVE_ITEM_UP);
453 case 'Z': move_curitem(MOVE_ITEM_DOWN);
456 case 'm': launch_mutt(selected_items() ?
457 -1 : list_current_item());
461 case 'p': ui_print_database(); break;
463 case 'u': launch_wwwbrowser(list_current_item());
477 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
478 remove_selected_items();
487 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
497 static char findstr[MAX_FIELD_LEN];
498 int search_fields[] = {NAME, EMAIL, NICK, -1};
507 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
508 strncpy(findstr, s, MAX_FIELD_LEN);
512 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
513 (item = find_item(findstr, 0, search_fields)) >= 0)
514 statusline_addstr("Search hit bottom, continuing at top");
524 ui_print_number_of_items()
526 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
528 mvaddstr(0, COLS-strlen(str), str);
537 if(!statusline_ask_boolean("Your current data will be lost - "
538 "Press 'y' to continue", FALSE))
541 load_database(datafile);
550 char *command = opt_get_str(STR_PRINT_COMMAND);
556 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
558 switch(tolower(getch())) {
563 if( !selected_items() ) {
564 statusline_msg("No selected items");
567 mode = ENUM_SELECTED;
576 if( ! *command || (handle = popen(command, "w")) == NULL)
579 fexport("text", handle, mode);
590 filename = ask_filename("File to open: ");
592 if(!filename || ! *filename) {
598 if(opt_get_bool(BOOL_AUTOSAVE))
600 else if(statusline_ask_boolean("Save current database", FALSE))
605 load_database(filename);
608 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
609 load_database(datafile);
612 datafile = strdup(filename);
618 alternative_datafile = TRUE;