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);
66 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
84 ioctl(0, TIOCGWINSZ, &winsz);
86 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
87 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
91 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
92 #ifdef HAVE_RESIZETERM
93 resizeterm(winsz.ws_row, winsz.ws_col);
100 should_resize = FALSE;
101 close_list(); /* we need to recreate windows */
107 #endif /* TIOCGWINSZ */
117 should_resize = TRUE;
119 #endif /* SIGWINCH */
125 return ui_initialized;
131 if(!is_ui_initialized())
136 intrflush(stdscr, FALSE);
137 keypad(stdscr, TRUE);
145 fprintf(stderr, "init_abook():\n");
146 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
148 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
149 clear(); refresh(); endwin();
150 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
151 fprintf(stderr, "Terminal is too small. Minium terminal size "
153 "%dx%d\n", MIN_COLS, MIN_LINES);
160 ui_initialized = TRUE;
163 signal(SIGWINCH, win_changed);
178 ui_initialized = FALSE;
183 headerline(char *str)
187 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
189 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
207 refresh_statusline();
208 headerline(MAIN_HELPLINE);
216 statusline_msg(char *msg)
221 statusline_addstr(msg);
224 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
232 statusline_addstr(char *str)
234 mvwaddstr(bottom, 1, 0, str);
240 ui_readline(char *prompt, char *s, int limit, bool use_completion)
245 mvwaddstr(bottom, 1, 0, prompt);
249 ret = abook_readline(bottom, y, x, s, limit, use_completion);
258 statusline_ask_boolean(char *msg, int def)
261 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
263 statusline_addstr(msg2);
267 switch(tolower(getch())) {
291 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
298 ask_filename(char *prompt)
304 buf = ui_readline(prompt, NULL, -1, 1);
325 display_help(int help)
341 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
345 for(i = 0; tbl[i] != NULL; i++) {
346 waddstr(helpw, tbl[i]);
347 if( (!((i + 1) % (LINES - 8))) ||
348 (tbl[i + 1] == NULL) ) {
351 refresh_statusline();
352 if(statusline_msg("Press any key to continue...")
367 extern char *selected;
376 can_resize = TRUE; /* it's safe to resize now */
377 if(!opt_get_bool(BOOL_SHOW_CURSOR))
382 if(!opt_get_bool(BOOL_SHOW_CURSOR))
384 can_resize = FALSE; /* it's not safe to resize anymore */
387 case 'Q': quit_abook(QUIT_DONTSAVE); break;
388 case 'P': print_stderr(selected_items() ?
389 -1 : list_current_item());
392 display_help(HELP_MAIN);
395 case 'a': add_item(); break;
396 case '\r': edit_item(-1); break;
399 case 'r': ui_remove_items(); break;
400 case 'D': duplicate_item(); break;
401 case 12: refresh_screen(); break;
404 case KEY_UP: scroll_up(); break;
406 case KEY_DOWN: scroll_down(); break;
408 case KEY_PPAGE: page_up(); break;
410 case KEY_NPAGE: page_down(); break;
413 case KEY_HOME: goto_home(); break;
415 case KEY_END: goto_end(); break;
417 case 'w': save_database();
419 case 'l': ui_read_database(); break;
420 case 'i': import_database(); break;
421 case 'e': export_database(); break;
422 case 'C': ui_clear_database(); break;
424 case 'o': ui_open_datafile(); break;
426 case 's': sort_by_field(NAME); break;
427 case 'S': sort_surname(); break;
428 case 'F': sort_by_field(-1); break;
430 case '/': ui_find(0); break;
431 case '\\': ui_find(1); break;
433 case ' ': if(curitem >= 0) {
434 selected[curitem] = !selected[curitem];
435 ui_print_number_of_items();
439 case '+': select_all();
442 case '-': select_none();
445 case '*': invert_selection();
448 case 'A': move_curitem(MOVE_ITEM_UP);
450 case 'Z': move_curitem(MOVE_ITEM_DOWN);
453 case 'm': launch_mutt(selected_items() ?
454 -1 : list_current_item());
458 case 'p': ui_print_database(); break;
460 case 'u': launch_wwwbrowser(list_current_item());
473 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
474 remove_selected_items();
483 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
493 static char findstr[MAX_FIELD_LEN];
494 int search_fields[] = {NAME, EMAIL, NICK, -1};
503 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
504 strncpy(findstr, s, MAX_FIELD_LEN);
508 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
509 (item = find_item(findstr, 0, search_fields)) >= 0)
510 statusline_addstr("Search hit bottom, continuing at top");
520 ui_print_number_of_items()
522 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
524 mvaddstr(0, COLS-strlen(str), str);
533 if(!statusline_ask_boolean("Your current data will be lost - "
534 "Press 'y' to continue", FALSE))
537 load_database(datafile);
546 char *command = opt_get_str(STR_PRINT_COMMAND);
552 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
554 switch(tolower(getch())) {
559 if( !selected_items() ) {
560 statusline_msg("No selected items");
563 mode = ENUM_SELECTED;
572 if( ! *command || (handle = popen(command, "w")) == NULL)
575 fexport("text", handle, mode);
586 filename = ask_filename("File to open: ");
588 if(!filename || ! *filename) {
594 if(opt_get_bool(BOOL_AUTOSAVE))
596 else if(statusline_ask_boolean("Save current database", FALSE))
601 load_database(filename);
604 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
605 load_database(datafile);
608 datafile = strdup(filename);
614 alternative_datafile = TRUE;