3 * $Id: ui.c,v 1.39 2005/08/23 10:03:34 jheinonen Exp $
5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
29 #ifdef HAVE_SYS_IOCTL_H
30 # include <sys/ioctl.h>
39 extern int items, curitem;
40 extern char *datafile;
42 extern bool alternative_datafile;
48 static bool ui_initialized = FALSE;
50 static bool should_resize = FALSE;
51 static bool can_resize = FALSE;
53 static WINDOW *top = NULL, *bottom = NULL;
59 top = newwin(LIST_TOP - 1, COLS, 0, 0);
60 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
78 ioctl(0, TIOCGWINSZ, &winsz);
80 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
81 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
85 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
86 #ifdef HAVE_RESIZETERM
87 resizeterm(winsz.ws_row, winsz.ws_col);
94 should_resize = FALSE;
95 close_list(); /* we need to recreate windows */
101 #endif /* TIOCGWINSZ */
111 should_resize = TRUE;
113 #endif /* SIGWINCH */
119 return ui_initialized;
125 if(!is_ui_initialized())
130 intrflush(stdscr, FALSE);
131 keypad(stdscr, TRUE);
139 fprintf(stderr, "init_abook():\n");
140 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
142 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
143 clear(); refresh(); endwin();
144 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
145 fprintf(stderr, "Terminal is too small. Minium terminal size "
147 "%dx%d\n", MIN_COLS, MIN_LINES);
154 ui_initialized = TRUE;
157 signal(SIGWINCH, win_changed);
172 ui_initialized = FALSE;
177 headerline(char *str)
181 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
183 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
201 refresh_statusline();
202 headerline(MAIN_HELPLINE);
210 statusline_msg(char *msg)
215 statusline_addstr(msg);
218 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
226 statusline_addstr(char *str)
228 mvwaddstr(bottom, 1, 0, str);
234 ui_readline(char *prompt, char *s, int limit, bool use_completion)
239 mvwaddstr(bottom, 1, 0, prompt);
243 ret = abook_readline(bottom, y, x, s, limit, use_completion);
252 statusline_ask_boolean(char *msg, int def)
255 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
257 statusline_addstr(msg2);
261 switch(tolower(getch())) {
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);
339 for(i = 0; tbl[i] != NULL; i++) {
340 waddstr(helpw, 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 'u': 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);
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, that specified file appears not to be a valid abook addressbook");
599 load_database(datafile);
602 datafile = xstrdup(filename);
608 alternative_datafile = TRUE;