X-Git-Url: https://git.deb.at/?a=blobdiff_plain;f=edit.c;h=7a4004a71ffd60342a81008ca7b724b15c95d2da;hb=7a7531e544d236d58ca077ce56b7d6d0c60faa09;hp=9403975263bc1520a48031cc81eb0f1b2e943d9b;hpb=4128f78f5543bebc7cd1868736855207019f1f71;p=pkg%2Fabook.git diff --git a/edit.c b/edit.c index 9403975..7a4004a 100644 --- a/edit.c +++ b/edit.c @@ -9,6 +9,7 @@ #include #include +#include #include "abook_curses.h" #include "ui.h" #include "abook.h" @@ -16,6 +17,7 @@ #include "list.h" #include "edit.h" #include "misc.h" +#include "xmalloc.h" #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -33,22 +35,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; + static char *tab_names[] = { + "CONTACT", + "ADDRESS", + " PHONE ", + " OTHER ", + "CUSTOM " }; 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 @@ -121,7 +136,7 @@ print_editor_header(int item) char *header; char email[MAX_EMAIL_LEN]; - if( (header = (char *)malloc(EDITW_COLS)) == NULL ) + if( (header = xmalloc(EDITW_COLS)) == NULL ) return; get_first_email(email, item); @@ -133,7 +148,7 @@ print_editor_header(int item) else snprintf(header, EDITW_COLS, "%s", database[item][NAME]); - mvwaddstr(editw, 0, (EDITW_COLS - strlen(header)) / 2, + mvwaddstr(editw, 0, (EDITW_COLS - strwidth(header)) / 2, header); free(header); @@ -182,6 +197,21 @@ editor_print_data(int tab, int item) } } +/* + * function: change_field + * + * parameters: + * (char *msg) + * message to display as a prompt + * (char **field) + * 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 + */ + static int change_field(char *msg, char **field) { @@ -199,7 +229,7 @@ change_field(char *msg, char **field) if(*field) { free(old); if(!**field) - my_free(*field); + xfree(*field); } else { *field = old; ret = 1; @@ -220,11 +250,11 @@ change_name_field(char **field) change_field("Name: ", field); if( *field == NULL || ! **field ) { - my_free(*field); + xfree(*field); *field = strdup(tmp); } - my_free(tmp); + xfree(tmp); } static void @@ -246,19 +276,16 @@ edit_emails(char c, int item) split_emailstr(item, emails); field = strdup(emails[email_num]); - if(change_field("E-mail: ", &field)) { -#ifdef DEBUG - fprintf(stderr, "change_field = TRUE\n"); -#endif - return; - } + if(change_field("E-mail: ", &field)) + return; /* user cancelled ( C-g ) */ + if(field) { strncpy(emails[email_num], field, MAX_EMAIL_LEN); fix_email_str(emails[email_num]); } else *emails[email_num] = 0; - my_free(database[item][EMAIL]); + xfree(database[item][EMAIL]); for(i = 0; i < MAX_EMAILS; i++) { if( *emails[i] ) { @@ -327,15 +354,15 @@ edit_undo(int item, int mode) case CLEAR_UNDO: if(backup) { free_list_item(backup[0]); - my_free(backup); + xfree(backup); } break; case BACKUP_ITEM: if(backup) { free_list_item(backup[0]); - my_free(backup); + xfree(backup); } - backup = (list_item *)abook_malloc(sizeof(list_item)); + backup = xmalloc(sizeof(list_item)); for(i = 0; i < ITEM_FIELDS; i++) backup[0][i] = safe_strdup(database[item][i]); break; @@ -343,9 +370,11 @@ edit_undo(int item, int mode) if(backup) { free_list_item(database[item]); itemcpy(database[item], backup[0]); - my_free(backup); + xfree(backup); } break; + default: + assert(0); } } @@ -371,12 +400,17 @@ edit_loop(int item) case 'a': tab = TAB_ADDRESS; break; case 'p': tab = TAB_PHONE; break; case 'o': tab = TAB_OTHER; break; + case 'C': tab = TAB_CUSTOM; 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;