5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
32 #ifdef HAVE_SYS_IOCTL_H
33 # include <sys/ioctl.h>
42 extern int items, curitem;
43 extern char *datafile;
45 extern bool alternative_datafile;
51 static bool ui_initialized = FALSE;
53 static bool should_resize = FALSE;
54 static bool can_resize = FALSE;
56 static WINDOW *top = NULL, *bottom = NULL;
62 top = newwin(LIST_TOP - 1, COLS, 0, 0);
63 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
81 ioctl(0, TIOCGWINSZ, &winsz);
83 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
84 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
88 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
89 #ifdef HAVE_RESIZETERM
90 resizeterm(winsz.ws_row, winsz.ws_col);
97 should_resize = FALSE;
98 close_list(); /* we need to recreate windows */
104 #endif /* TIOCGWINSZ */
114 should_resize = TRUE;
116 #endif /* SIGWINCH */
122 return ui_initialized;
128 if(!is_ui_initialized())
133 intrflush(stdscr, FALSE);
134 keypad(stdscr, TRUE);
142 fprintf(stderr, "init_abook():\n");
143 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
145 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
146 clear(); refresh(); endwin();
147 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
148 fprintf(stderr, "Terminal is too small. Minium terminal size "
150 "%dx%d\n", MIN_COLS, MIN_LINES);
157 ui_initialized = TRUE;
160 signal(SIGWINCH, win_changed);
175 ui_initialized = FALSE;
180 headerline(char *str)
184 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
186 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
204 refresh_statusline();
205 headerline(MAIN_HELPLINE);
213 statusline_msg(char *msg)
218 statusline_addstr(msg);
221 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
229 statusline_addstr(char *str)
231 mvwaddstr(bottom, 1, 0, str);
237 ui_readline(char *prompt, char *s, int limit, bool use_completion)
242 mvwaddstr(bottom, 1, 0, prompt);
246 ret = abook_readline(bottom, y, x, s, limit, use_completion);
255 statusline_ask_boolean(char *msg, int def)
258 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
260 statusline_addstr(msg2);
264 switch(tolower(getch())) {
288 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
295 ask_filename(char *prompt)
301 buf = ui_readline(prompt, NULL, -1, 1);
322 display_help(int help)
338 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
342 for(i = 0; tbl[i] != NULL; i++) {
343 waddstr(helpw, tbl[i]);
344 if( (!((i + 1) % (LINES - 8))) ||
345 (tbl[i + 1] == NULL) ) {
348 refresh_statusline();
349 if(statusline_msg("Press any key to continue...")
364 extern char *selected;
373 can_resize = TRUE; /* it's safe to resize now */
374 if(!opt_get_bool(BOOL_SHOW_CURSOR))
379 if(!opt_get_bool(BOOL_SHOW_CURSOR))
381 can_resize = FALSE; /* it's not safe to resize anymore */
384 case 'Q': quit_abook(QUIT_DONTSAVE); break;
385 case 'P': print_stderr(selected_items() ?
386 -1 : list_current_item());
389 display_help(HELP_MAIN);
392 case 'a': add_item(); break;
393 case '\r': edit_item(-1); break;
396 case 'r': ui_remove_items(); break;
397 case 'D': duplicate_item(); break;
398 case 12: refresh_screen(); break;
401 case KEY_UP: scroll_up(); break;
403 case KEY_DOWN: scroll_down(); break;
405 case KEY_PPAGE: page_up(); break;
407 case KEY_NPAGE: page_down(); break;
410 case KEY_HOME: goto_home(); break;
412 case KEY_END: goto_end(); break;
414 case 'w': save_database();
416 case 'l': ui_read_database(); break;
417 case 'i': import_database(); break;
418 case 'e': export_database(); break;
419 case 'C': ui_clear_database(); break;
421 case 'o': ui_open_datafile(); break;
423 case 's': sort_by_field(NAME); break;
424 case 'S': sort_surname(); break;
425 case 'F': sort_by_field(-1); break;
427 case '/': ui_find(0); break;
428 case '\\': ui_find(1); break;
430 case ' ': if(curitem >= 0) {
431 selected[curitem] = !selected[curitem];
432 ui_print_number_of_items();
436 case '+': select_all();
439 case '-': select_none();
442 case '*': invert_selection();
445 case 'A': move_curitem(MOVE_ITEM_UP);
447 case 'Z': move_curitem(MOVE_ITEM_DOWN);
450 case 'm': launch_mutt(selected_items() ?
451 -1 : list_current_item());
455 case 'p': ui_print_database(); break;
457 case 'u': launch_wwwbrowser(list_current_item());
470 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
471 remove_selected_items();
480 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
490 static char findstr[MAX_FIELD_LEN];
491 int search_fields[] = {NAME, EMAIL, NICK, -1};
500 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
501 strncpy(findstr, s, MAX_FIELD_LEN);
505 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
506 (item = find_item(findstr, 0, search_fields)) >= 0)
507 statusline_addstr("Search hit bottom, continuing at top");
517 ui_print_number_of_items()
519 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
521 mvaddstr(0, COLS-strlen(str), str);
530 if(!statusline_ask_boolean("Your current data will be lost - "
531 "Press 'y' to continue", FALSE))
534 load_database(datafile);
543 char *command = opt_get_str(STR_PRINT_COMMAND);
549 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
551 switch(tolower(getch())) {
556 if( !selected_items() ) {
557 statusline_msg("No selected items");
560 mode = ENUM_SELECTED;
569 if( ! *command || (handle = popen(command, "w")) == NULL)
572 fexport("text", handle, mode);
583 filename = ask_filename("File to open: ");
585 if(!filename || ! *filename) {
591 if(opt_get_bool(BOOL_AUTOSAVE))
593 else if(statusline_ask_boolean("Save current database", FALSE))
598 load_database(filename);
601 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
602 load_database(datafile);
605 datafile = xstrdup(filename);
611 alternative_datafile = TRUE;