5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
32 # ifdef HAVE_LINUX_TERMIOS_H
33 # include <linux/termios.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
44 extern int items, curitem;
45 extern char *datafile;
51 int ui_initialized = FALSE;
53 int should_resize = FALSE;
54 int can_resize = FALSE;
56 WINDOW *top = NULL, *bottom = NULL;
63 top = newwin(LIST_TOP - 1, COLS, 0, 0);
65 bottom = newwin(LINES - LIST_BOTTOM, COLS, LIST_BOTTOM, 0);
83 ioctl (0, TIOCGWINSZ, &winsz);
85 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
86 fprintf(stderr, "Warning: COLS=%d, LINES=%d\n", winsz.ws_col, winsz.ws_row);
90 if(winsz.ws_col >= MIN_COLS && winsz.ws_row >= MIN_LINES) {
91 #ifdef HAVE_RESIZETERM
92 resizeterm(winsz.ws_row, winsz.ws_col);
99 should_resize = FALSE;
100 close_list(); /* we need to recreate windows */
106 #endif /* TIOCGWINSZ */
116 should_resize = TRUE;
118 #endif /* SIGWINCH */
124 return ui_initialized;
130 if(!is_ui_initialized())
135 intrflush(stdscr, FALSE);
136 keypad(stdscr, TRUE);
144 fprintf(stderr, "init_abook():\n");
145 fprintf(stderr, " COLS = %d, LINES = %d\n", COLS, LINES);
147 if( LINES < MIN_LINES || COLS < MIN_COLS ) {
148 clear(); refresh(); endwin();
149 fprintf(stderr, "Your terminal size is %dx%d\n", COLS, LINES);
150 fprintf(stderr, "Terminal is too small. Minium terminal size "
152 "%dx%d\n", MIN_COLS, MIN_LINES);
157 signal(SIGWINCH, win_changed);
163 ui_initialized = TRUE;
177 ui_initialized = FALSE;
182 headerline(char *str)
186 mvwhline(top, 1, 0, UI_HLINE_CHAR, COLS);
188 mvwprintw(top, 0, 0, "%s | %s", PACKAGE " " VERSION, str);
199 if( should_resize ) {
206 refresh_statusline();
207 headerline(MAIN_HELPLINE);
215 statusline_msg(char *msg)
218 statusline_addstr(msg);
221 fprintf(stderr, "statusline_msg(\"%s\")\n", msg);
227 statusline_addstr(char *str)
229 mvwaddstr(bottom, 1, 0, str);
235 * function statusline_getnstr
239 * if n >= 0 str is a pointer which points a place where to store
240 * the string, else str is ignored
242 * the maximum length of the string
243 * If n < 0 function will allocate needed space for the string.
244 * Value 0 is not allowed for n.
246 * if this value is nonzero the fileselector is enabled
249 * If n < 0 a pointer to a newly allocated string is returned.
250 * If n > 0 a nonzero value is returned if user has typed a valid
251 * string. If not NULL value is returned. Never really use the
252 * _pointer_ if n > 0.
257 statusline_getnstr(char *str, int n, int use_filesel)
265 buf = wenter_string(bottom, n,
266 (use_filesel ? ESTR_USE_FILESEL:0) | ESTR_DONT_WRAP);
274 strncpy(str, buf, n);
284 statusline_ask_boolean(char *msg, int def)
287 char *msg2 = strconcat(msg, def ? " (Y/n)?" : " (y/N)?", NULL);
289 statusline_addstr(msg2);
293 switch( tolower(getch()) ) {
316 mvwhline(bottom, 0, 0, UI_HLINE_CHAR, COLS);
317 mvwhline(bottom, 2, 0, UI_HLINE_CHAR, COLS);
325 ask_filename(char *prompt, int flags)
331 statusline_addstr(prompt);
332 buf = statusline_getnstr(NULL, -1, flags);
355 display_help(int help)
371 helpw = newwin(LINES - 5, COLS - 6, 2, 3);
375 for( i = 0; tbl[i] != NULL; i++) {
376 waddstr(helpw, tbl[i]);
377 if( ( !( (i+1) % (LINES-8) ) ) ||
378 (tbl[i+1] == NULL) ) {
381 refresh_statusline();
382 statusline_msg("Press any key to continue...");
404 can_resize = TRUE; /* it's safe to resize now */
410 can_resize = FALSE; /* it's not safe to resize anymore */
413 case 'Q': print_stderr(selected_items() ?
414 -1 : list_current_item());
417 display_help(HELP_MAIN);
420 case 'a': add_item(); break;
421 case '\r': edit_item(-1); break;
424 case 'r': ui_remove_items(); break;
425 case 12: refresh_screen(); break;
428 case KEY_UP: scroll_up(); break;
430 case KEY_DOWN: scroll_down(); break;
432 case KEY_PPAGE: page_up(); break;
434 case KEY_NPAGE: page_down(); break;
437 case KEY_HOME: goto_home(); break;
439 case KEY_END: goto_end(); break;
441 case 'w': save_database();
443 case 'l': ui_read_database(); break;
444 case 'i': import_database(); break;
445 case 'e': export_database(); break;
446 case 'C': ui_clear_database(); break;
448 case 'o': ui_open_datafile(); break;
450 case 's': sort_database(); break;
451 case 'S': sort_surname(); break;
453 case '/': ui_find(0); break;
454 case '\\': ui_find(1); break;
456 case ' ': if(curitem >= 0) {
457 selected[curitem] = !selected[curitem];
458 ui_print_number_of_items();
462 case '+': select_all();
465 case '-': select_none();
468 case '*': invert_selection();
471 case 'A': move_curitem(MOVE_ITEM_UP);
473 case 'Z': move_curitem(MOVE_ITEM_DOWN);
476 case 'm': launch_mutt(selected_items() ?
477 -1 : list_current_item());
481 case 'p': ui_print_database(); break;
483 case 'u': launch_wwwbrowser(list_current_item());
497 if(statusline_ask_boolean("Remove selected item(s)", TRUE))
498 remove_selected_items();
507 if(statusline_ask_boolean("Clear WHOLE database", FALSE)) {
517 static char findstr[MAX_FIELD_LEN];
518 int search_fields[] = {NAME, EMAIL, NICK, -1};
525 statusline_addstr("/");
526 statusline_getnstr(findstr, MAX_FIELD_LEN - 1, 0);
530 if( (item = find_item(findstr, curitem + !!next,
531 search_fields )) >= 0 ) {
540 ui_print_number_of_items()
542 char *str = mkstr(" " "|%3d/%3d", selected_items(), items);
544 mvaddstr(0, COLS-strlen(str), str);
553 if(!statusline_ask_boolean("Your current data will be lost - "
554 "Press 'y' to continue", FALSE))
557 load_database(datafile);
566 char *command = options_get_str("print_command");
569 if( list_is_empty() )
572 statusline_addstr("Print All/Selected/Cancel (a/s/C)?");
574 switch( tolower(getch()) ) {
579 if( !selected_items() ) {
580 statusline_msg("No selected items");
583 mode = ENUM_SELECTED;
592 if( ! *command || (handle = popen(command, "w")) == NULL)
595 fexport("text", handle, mode);
606 filename = ask_filename("File to open: ", 1);
613 if( options_get_int("autosave") )
615 else if(statusline_ask_boolean("Save current database", FALSE))
620 load_database(filename);
623 statusline_msg("Sorry, that specified file appears not to be a valid abook addressbook");
624 load_database(datafile);
627 datafile = strdup(filename);