3 * $Id: ui.c,v 1.54.4.1 2006/04/09 18:57:34 jheinonen Exp $
5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
31 #ifdef HAVE_SYS_IOCTL_H
32 # include <sys/ioctl.h>
41 extern int items, curitem;
42 extern char *datafile;
44 extern bool alternative_datafile;
50 static bool ui_initialized = FALSE;
52 static bool should_resize = FALSE;
53 static bool can_resize = FALSE;
55 static WINDOW *top = NULL, *bottom = NULL;
61 top = newwin(LIST_TOP - 1, COLS, 0, 0);
62 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
80 ioctl(0, TIOCGWINSZ, &winsz);
82 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
83 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
87 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
88 #ifdef HAVE_RESIZETERM
89 resizeterm(winsz.ws_row, winsz.ws_col);
96 should_resize = FALSE;
97 close_list(); /* we need to recreate windows */
103 #endif /* TIOCGWINSZ */
113 should_resize = TRUE;
115 #endif /* SIGWINCH */
121 return ui_initialized;
127 if(!is_ui_initialized())
132 intrflush(stdscr, FALSE);
133 keypad(stdscr, TRUE);
141 fprintf(stderr, "init_abook():\n");
142 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
144 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
145 clear(); refresh(); endwin();
146 fprintf(stderr, _("Your terminal size is %dx%d\n"), COLS, LINES);
147 fprintf(stderr, _("Terminal is too small. Minimum terminal "
149 "%dx%d\n"), MIN_COLS, MIN_LINES);
156 ui_initialized = TRUE;
159 signal(SIGWINCH, win_changed);
174 ui_initialized = FALSE;
179 headerline(const char *str)
183 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
185 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
203 refresh_statusline();
204 headerline(gettext(MAIN_HELPLINE));
212 statusline_msg(const char *msg)
217 statusline_addstr(msg);
220 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
228 statusline_addstr(const char *str)
230 mvwaddstr(bottom, 1, 0, str);
235 /* Same as statusline_addstr(), but hilight "<str>" sequences if the terminal
238 statusline_addhlstr(const char *str)
240 #if defined(A_BOLD) && defined(A_NORMAL) && defined(A_DIM)
241 const char *p = str, *start = str;
246 if(!*p || strchr("<>", *p)) {
248 wattrset(bottom, (*p == '>') ? A_BOLD : A_NORMAL);
249 tmp = xstrndup(start, p - start);
250 mvwaddstr(bottom, 1, pos, tmp);
251 pos += strwidth(tmp);
257 /* show tag markers */
258 wattrset(bottom, A_DIM);
259 mvwaddch(bottom, 1, pos++, *p);
264 wattrset(bottom, A_NORMAL);
271 mvwaddstr(bottom, 1, 0, str);
279 statusline_askchoice(const char *msg, const char *choices, short dflt)
284 assert((dflt < 0) || (dflt > strlen(choices)));
287 s = strdup_printf("%s [%c]", msg, choices[dflt - 1]);
288 statusline_addhlstr(s);
291 statusline_addhlstr(msg);
295 ch = tolower(getch());
297 if(ch == 7) /* ctrl+G */
300 if(dflt && (ch == '\r')) /* default choice */
303 if((s = strchr(choices, ch)))
304 return (s - choices + 1);
309 ui_readline(const char *prompt, char *s, size_t limit, bool use_completion)
314 mvwaddstr(bottom, 1, 0, prompt);
318 ret = abook_readline(bottom, y, x, s, use_completion);
322 if(strlen(ret) > limit && limit > 0)
330 statusline_ask_boolean(const char *msg, int def)
333 char *msg2 = strconcat(msg, def ? _(" (Y/n)?") : _(" (y/N)?"), NULL);
336 statusline_addstr(msg2);
340 ch = tolower(getch());
342 if(ch == *(S_("keybinding for no|n")))
344 else if(ch == *(S_("keybinding for yes|y")))
359 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
366 ask_filename(const char *prompt)
372 buf = ui_readline(prompt, NULL, -1, 1);
393 display_help(int help)
409 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
411 headerline(_("help"));
413 for(i = 0; tbl[i] != NULL; i++) {
414 waddstr(helpw, gettext(tbl[i]));
415 if( (!((i + 1) % (LINES - 8))) ||
416 (tbl[i + 1] == NULL) ) {
419 refresh_statusline();
420 if(statusline_msg(_("Press any key to continue..."))
435 extern char *selected;
444 can_resize = TRUE; /* it's safe to resize now */
445 if(!opt_get_bool(BOOL_SHOW_CURSOR))
450 if(!opt_get_bool(BOOL_SHOW_CURSOR))
452 can_resize = FALSE; /* it's not safe to resize anymore */
455 case 'Q': quit_abook(QUIT_DONTSAVE); break;
456 case 'P': print_stderr(selected_items() ?
457 -1 : list_current_item());
460 display_help(HELP_MAIN);
463 case 'a': add_item(); break;
464 case '\r': edit_item(-1); break;
467 case 'r': ui_remove_items(); break;
468 case 'D': duplicate_item(); break;
469 case 12: refresh_screen(); break;
472 case KEY_UP: scroll_up(); break;
474 case KEY_DOWN: scroll_down(); break;
476 case KEY_PPAGE: page_up(); break;
478 case KEY_NPAGE: page_down(); break;
481 case KEY_HOME: goto_home(); break;
483 case KEY_END: goto_end(); break;
485 case 'w': save_database();
487 case 'l': ui_read_database(); break;
488 case 'i': import_database(); break;
489 case 'e': export_database(); break;
490 case 'C': ui_clear_database(); break;
492 case 'o': ui_open_datafile(); break;
494 case 's': sort_by_field(NAME); break;
495 case 'S': sort_surname(); break;
496 case 'F': sort_by_field(-1); break;
498 case '/': ui_find(0); break;
499 case '\\': ui_find(1); break;
501 case ' ': if(curitem >= 0) {
502 selected[curitem] = !selected[curitem];
503 ui_print_number_of_items();
507 case '+': select_all();
510 case '-': select_none();
513 case '*': invert_selection();
516 case 'A': move_curitem(MOVE_ITEM_UP);
518 case 'Z': move_curitem(MOVE_ITEM_DOWN);
521 case 'm': launch_mutt(selected_items() ?
522 -1 : list_current_item());
526 case 'p': ui_print_database(); break;
528 case 'v': launch_wwwbrowser(list_current_item());
541 if(statusline_ask_boolean(_("Remove selected item(s)"), TRUE))
542 remove_selected_items();
551 if(statusline_ask_boolean(_("Clear WHOLE database"), FALSE)) {
561 static char findstr[MAX_FIELD_LEN];
562 int search_fields[] = {NAME, EMAIL, NICK, -1};
571 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
572 strncpy(findstr, s, MAX_FIELD_LEN);
577 if( (item = find_item(findstr, curitem + !!next, search_fields)) < 0 &&
578 (item = find_item(findstr, 0, search_fields)) >= 0)
579 statusline_addstr(_("Search hit bottom, continuing at top"));
589 ui_print_number_of_items()
591 char *str = strdup_printf(" " "|%3d/%3d", selected_items(), items);
593 mvaddstr(0, COLS-strlen(str), str);
604 msg = strdup_printf(_("Your current data will be lost - "
605 "Press '%c' to continue"),
606 *(S_("keybinding for yes|y")));
607 if(!statusline_ask_boolean(msg, FALSE)) {
614 load_database(datafile);
623 char *command = opt_get_str(STR_PRINT_COMMAND);
629 switch(statusline_askchoice(_("Print <a>ll, print <s>elected, or <c>ancel?"), S_("keybindings:all/selected/cancel|asc"), 3)) {
634 if( !selected_items() ) {
635 statusline_msg(_("No selected items"));
638 mode = ENUM_SELECTED;
647 if( ! *command || (handle = popen(command, "w")) == NULL)
650 fexport("text", handle, mode);
661 filename = ask_filename(_("File to open: "));
663 if(!filename || ! *filename) {
669 if(opt_get_bool(BOOL_AUTOSAVE))
671 else if(statusline_ask_boolean(_("Save current database"), FALSE))
676 load_database(filename);
679 statusline_msg(_("Sorry, the specified file appears not to be a valid abook addressbook"));
680 load_database(datafile);
683 datafile = xstrdup(filename);
689 alternative_datafile = TRUE;