X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=edit.c;h=8d39660156e90ca5ce884e8ba7b1080019e9f6ce;hb=35c85955699b227956e399a76b4a78ab48aa0c18;hp=72c520f6cc4e1dff6fbec9a90244418e72b26236;hpb=12a57d9405d7910566d5ff888d743d29b0716554;p=pkg%2Fabook.git diff --git a/edit.c b/edit.c index 72c520f..8d39660 100644 --- a/edit.c +++ b/edit.c @@ -2,7 +2,7 @@ /* * $Id$ * - * by JH + * by JH * * Copyright (C) Jaakko Heinonen */ @@ -10,6 +10,7 @@ #include #include #include "abook_curses.h" +#include "ui.h" #include "abook.h" #include "database.h" #include "list.h" @@ -32,22 +33,35 @@ extern int items; WINDOW *editw; static void -editor_tab(int tab) +editor_tab(const int tab) { int i; - char *tab_names[] = { - "/ CONTACT \\", - "/ ADDRESS \\", - "/ PHONE \\", - "/ OTHER \\" + const int spacing = 12; + const char *tab_names[] = { + "CONTACT", + "ADDRESS", + " PHONE ", + " OTHER ", + "CUSTOM " }; - mvwhline(editw, TABLINE+1, 0, ACS_HLINE, EDITW_COLS); + mvwhline(editw, TABLINE+1, 0, UI_HLINE_CHAR, EDITW_COLS); + for(i=0; i < TABS; i++) { + mvwaddch(editw, TABLINE+1, spacing * i + 2, UI_TEE_CHAR); + mvwaddch(editw, TABLINE+1, spacing * i + 12, UI_TEE_CHAR); + } - for(i=0; i < TABS; i++) - mvwaddstr(editw, TABLINE, 16 * i + 3, tab_names[i]); + for(i=0; i < TABS; i++) { + mvwaddch(editw, TABLINE, spacing * i + 2, UI_ULCORNER_CHAR); + mvwaddch(editw, TABLINE, spacing * i + 3, UI_LBOXLINE_CHAR); + mvwaddstr(editw, TABLINE, spacing * i + 4, tab_names[i]); + mvwaddch(editw, TABLINE, spacing * i + 11, UI_RBOXLINE_CHAR); + mvwaddch(editw, TABLINE, spacing * i + 12, UI_URCORNER_CHAR); + } - mvwaddstr(editw, TABLINE+1, 16 * tab + 2, "/ \\"); + mvwaddch(editw, TABLINE+1, spacing * tab + 2, UI_LRCORNER_CHAR); + mvwaddstr(editw, TABLINE+1, spacing * tab + 3, " "); + mvwaddch(editw, TABLINE+1, spacing * tab + 12, UI_LLCORNER_CHAR); } void @@ -119,33 +133,30 @@ print_editor_header(int item) { char *header; char email[MAX_EMAIL_LEN]; - int i, x, len; - + if( (header = (char *)malloc(EDITW_COLS)) == NULL ) return; get_first_email(email, item); - - if( snprintf(header, EDITW_COLS, "%s <%s>", database[item][NAME], - email ) == -1 || !*database[item][EMAIL] ) - if( snprintf(header, EDITW_COLS, "%s", database[item][NAME]) == -1) { - free(header); - return; - } - len = strlen(header); - x = (EDITW_COLS - len) / 2; - mvwaddstr(editw, 0, x, header); - for(i = x; i < x + len; i++) - mvwaddch(editw,1, i, '^'); + if( *database[item][EMAIL] ) + snprintf(header, EDITW_COLS, "%s <%s>", + database[item][NAME], + email); + else + snprintf(header, EDITW_COLS, "%s", database[item][NAME]); + + mvwaddstr(editw, 0, (EDITW_COLS - strlen(header)) / 2, + header); + free(header); } static void editor_print_data(int tab, int item) { - const int pos_x = EDITW_COLS > 70 ? 8:4; int i, j; + int y, x; for(i = 0, j = 1; i < ITEM_FIELDS; i++) { if(abook_fields[i].tab != tab) @@ -155,32 +166,45 @@ editor_print_data(int tab, int item) int k; char emails[MAX_EMAILS][MAX_EMAIL_LEN]; split_emailstr(item, emails); - mvwaddstr(editw, (LINES > 21 ? 7:6), pos_x, "E-mail addresses:"); - for(k=0; k < MAX_EMAILS; k++) - mvwprintw(editw, (LINES > 21 ? 9:7)+k*2, pos_x, - "%c -\t\t%s", '2' + k, emails[k] ); + getyx(editw, y, x); + mvwaddstr(editw, y+1, FIELDS_START_X, + "E-mail addresses:"); + for(k = 0; k < MAX_EMAILS; k++) { + getyx(editw, y, x); + mvwprintw(editw, y+1, FIELDS_START_X, + "%c -", '2' + k); + mvwprintw(editw, y +1, TAB_COLON_POS, + ": %s", emails[k]); + } continue; } - - mvwprintw(editw, 3+j*2, pos_x, "%d - %s", + + if(j > 1) { + getyx(editw, y, x); y++; + } else + y = FIELDS_START_Y; + + mvwprintw(editw, y, FIELDS_START_X, "%d - %s", j, abook_fields[i].name); - mvwaddch(editw, 3+j*2, 28, ':'); - mvwaddstr(editw, 3+j*2, 30, safe_str(database[item][i])); + mvwaddch(editw, y, TAB_COLON_POS, ':'); + mvwaddstr(editw, y, TAB_COLON_POS + 2, + safe_str(database[item][i])); + j++; } } /* * function: change_field - * + * * parameters: * (char *msg) * message to display as a prompt * (char **field) - * a pointer to pointer which will point a new string. if the latter + * a pointer to a pointer which will point a new string. if the latter * pointer != NULL it will be freed (if user doesn't cancel) - * + * * returns (int) * a nonzero value if user has cancelled and zero if user has typed a * valid string @@ -189,24 +213,30 @@ editor_print_data(int tab, int item) static int change_field(char *msg, char **field) { - char tmp[MAX_FIELD_LEN]; int max_len = MAX_FIELD_LEN; - int ret; - + char *old; + int ret = 0; + if( !strncmp("E-mail", msg, 6) ) max_len = MAX_EMAIL_LEN; - - statusline_addstr(msg); - if( (ret = statusline_getnstr( tmp, max_len - 1, 0 ) ? 1:0 ) ) { - my_free(*field); - if( *tmp ) - *field = strdup(tmp); + + old = *field; + + *field = ui_readline(msg, old, max_len - 1, 0); + + if(*field) { + free(old); + if(!**field) + my_free(*field); + } else { + *field = old; + ret = 1; } clear_statusline(); refresh_statusline(); - return !ret; + return ret; } static void @@ -235,28 +265,27 @@ fix_email_str(char *str) static void edit_emails(char c, int item) { - char *field = NULL; - char emails[4][MAX_EMAIL_LEN]; + char *field; + char emails[MAX_EMAILS][MAX_EMAIL_LEN]; char tmp[MAX_EMAILSTR_LEN] = ""; int i, len; + int email_num = c - '2'; split_emailstr(item, emails); + field = strdup(emails[email_num]); - if(change_field("E-mail: ", &field)) { -#ifdef DEBUG - fprintf(stderr, "change_field = TRUE\n"); -#endif + if(change_field("E-mail: ", &field)) return; /* user cancelled ( C-g ) */ - } + if(field) { - strncpy(emails[c - '2'], field, MAX_EMAIL_LEN); - fix_email_str(emails[c - '2']); + strncpy(emails[email_num], field, MAX_EMAIL_LEN); + fix_email_str(emails[email_num]); } else - *emails[c - '2'] = 0; - + *emails[email_num] = 0; + my_free(database[item][EMAIL]); - for(i=0; i<4; i++) { + for(i = 0; i < MAX_EMAILS; i++) { if( *emails[i] ) { strcat(tmp, emails[i]); strcat(tmp, ","); @@ -277,7 +306,7 @@ edit_field(int tab, char c, int item) int n = c - '1' + 1; char *str; - if(n < 1 || n > 6) + if(n < 1 || n > MAX_TAB_FIELDS) return 0; edit_undo(item, BACKUP_ITEM); @@ -331,7 +360,7 @@ edit_undo(int item, int mode) free_list_item(backup[0]); my_free(backup); } - backup = abook_malloc(sizeof(list_item)); + backup = (list_item *)abook_malloc(sizeof(list_item)); for(i = 0; i < ITEM_FIELDS; i++) backup[0][i] = safe_strdup(database[item][i]); break; @@ -342,6 +371,8 @@ edit_undo(int item, int mode) my_free(backup); } break; + default: + assert(0); } } @@ -350,9 +381,10 @@ edit_loop(int item) { static int tab = 0; /* first tab */ int c; - + werase(editw); headerline(EDITOR_HELPLINE); + refresh_statusline(); print_editor_header(item); editor_tab(tab); editor_print_data(tab, item); @@ -366,17 +398,28 @@ edit_loop(int item) case 'a': tab = TAB_ADDRESS; break; case 'p': tab = TAB_PHONE; break; case 'o': tab = TAB_OTHER; break; + case 'h': case KEY_LEFT: tab = tab == 0 ? MAX_TAB : tab - 1; break; + case 'l': case KEY_RIGHT: tab = tab == MAX_TAB ? 0 : tab + 1; break; + case KEY_UP: + case '<': + case 'k': if(is_valid_item(item-1)) item--; break; + case KEY_DOWN: + case '>': + case 'j': if(is_valid_item(item+1)) item++; break; case 'r': roll_emails(item); break; - case '?': display_editor_help(editw); break; + case '?': display_help(HELP_EDITOR); break; case 'u': edit_undo(item, RESTORE_ITEM); break; - default: return edit_field(tab, c, item); + case 'm': launch_mutt(item); clearok(stdscr, 1); break; + case 'v': launch_wwwbrowser(item); clearok(stdscr, 1); break; + case 12 : clearok(stdscr, 1); break; /* ^L (refresh screen) */ + default: return edit_field(tab, c, item) ? item : -1; } - return 1; + return item; } void @@ -390,8 +433,9 @@ edit_item(int item) } init_editor(); - while( edit_loop(item) ) - ; + + while( (item = edit_loop(item)) >= 0 ) + curitem = item; /* hmm, this is not very clean way to go */ close_editor(); }