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 int alternative_datafile;
54 bool ui_initialized = FALSE;
56 bool should_resize = FALSE;
57 bool can_resize = FALSE;
59 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()) ) {
291 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
299 ask_filename(char *prompt)
305 buf = ui_readline(prompt, NULL, -1, 1);
326 display_help(int help)
342 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
346 for( i = 0; tbl[i] != NULL; i++) {
347 waddstr(helpw, tbl[i]);
348 if( ( !( (i+1) % (LINES-8) ) ) ||
349 (tbl[i+1] == NULL) ) {
352 refresh_statusline();
353 if(statusline_msg("Press any key to continue...")
368 extern char *selected;
377 can_resize = TRUE; /* it's safe to resize now */
383 can_resize = FALSE; /* it's not safe to resize anymore */
386 case 'Q': print_stderr(selected_items() ?
387 -1 : list_current_item());
390 display_help(HELP_MAIN);
393 case 'a': add_item(); break;
394 case '\r': edit_item(-1); break;
397 case 'r': ui_remove_items(); 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_database(); break;
424 case 'S': sort_surname(); break;
426 case '/': ui_find(0); break;
427 case '\\': ui_find(1); break;
429 case ' ': if(curitem >= 0) {
430 selected[curitem] = !selected[curitem];
431 ui_print_number_of_items();
435 case '+': select_all();
438 case '-': select_none();
441 case '*': invert_selection();
444 case 'A': move_curitem(MOVE_ITEM_UP);
446 case 'Z': move_curitem(MOVE_ITEM_DOWN);
449 case 'm': launch_mutt(selected_items() ?
450 -1 : list_current_item());
454 case 'p': ui_print_database(); break;
456 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};
499 s = ui_readline("/", findstr, MAX_FIELD_LEN - 1, 0);
500 strncpy(findstr, s, MAX_FIELD_LEN);
504 if( (item = find_item(findstr, curitem + !!next,
505 search_fields )) >= 0 ) {
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);
543 if( list_is_empty() )
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 = strdup(filename);
608 alternative_datafile = TRUE;