5 * by JH <jheinonen@users.sourceforge.net>
7 * Copyright (C) Jaakko Heinonen
25 int first_list_item = -1;
26 char *selected = NULL;
28 int extra_column = -1;
29 int extra_alternative = -1;
31 extern abook_field_list *fields_list;
33 static WINDOW *list = NULL;
37 init_extra_field(enum str_opts option)
42 option_str = opt_get_str(option);
44 if(option_str && *option_str) {
45 find_field_number(option_str, &ret);
47 if(!strcmp(option_str, "name") || !strcmp(option_str, "email"))
57 list = newwin(LIST_LINES, LIST_COLS, LIST_TOP, 0);
61 * init extra_column and extra alternative
64 extra_column = init_extra_field(STR_EXTRA_COLUMN);
65 extra_alternative = init_extra_field(STR_EXTRA_ALTERNATIVE);
82 ui_print_number_of_items();
93 if(first_list_item < 0)
96 if(curitem < first_list_item)
97 first_list_item = curitem;
98 else if(curitem > LAST_LIST_ITEM)
99 first_list_item = max(curitem - LIST_LINES + 1, 0);
101 for(line = 0, i = first_list_item;
102 i <= LAST_LIST_ITEM && i < db_n_items();
105 print_list_line(i, line, i == curitem);
108 if(opt_get_bool(BOOL_SHOW_CURSOR)) {
109 wmove(list, curitem - first_list_item, 0);
110 /* need to call refresh() to update the cursor positions */
117 print_list_line(int i, int line, int highlight)
119 int extra = extra_column;
120 char tmp[MAX_EMAILSTR_LEN], *emails;
121 int real_emaillen = (extra_column > 0 || extra_alternative > 0) ?
122 EMAILLEN : COLS - EMAILPOS;
124 scrollok(list, FALSE);
126 highlight_line(list, line);
129 mvwaddch(list, line, 0, '*' );
131 mvwaddnstr(list, line, NAMEPOS, db_name_get(i),
132 bytes2width(db_name_get(i), NAMELEN));
134 if(opt_get_bool(BOOL_SHOW_ALL_EMAILS)) {
135 emails = db_email_get(i);
136 mvwaddnstr(list, line, EMAILPOS, emails,
137 bytes2width(emails, real_emaillen));
140 get_first_email(tmp, i);
141 mvwaddnstr(list, line, EMAILPOS, tmp,
142 bytes2width(tmp, real_emaillen));
145 if(extra < 0 || !db_fget_byid(i, extra))
146 extra = extra_alternative;
148 mvwaddnstr(list, line, EXTRAPOS,
149 safe_str(db_fget_byid(i, extra)),
150 bytes2width(safe_str(db_fget_byid(i, extra)),
153 scrollok(list, TRUE);
163 #if defined(A_BOLD) && defined(A_NORMAL)
167 mvaddstr(2, NAMEPOS, find_field("name", NULL)->name);
168 mvaddstr(2, EMAILPOS, find_field("email", NULL)->name);
169 if(extra_column > 0) {
170 get_field_keyname(extra_column, NULL, &str);
171 mvaddnstr(2, EXTRAPOS, str, COLS - EXTRAPOS);
174 #if defined(A_BOLD) && defined(A_NORMAL)
193 if(curitem > db_n_items() - 2)
208 curitem = curitem == first_list_item ?
209 ((curitem -= LIST_LINES) < 0 ? 0 : curitem) : first_list_item;
217 if(curitem > db_n_items() - 2)
220 if(curitem == LAST_LIST_ITEM) {
221 if((curitem += LIST_LINES) > last_item())
222 curitem = last_item();
224 curitem = min(LAST_LIST_ITEM, last_item());
233 memset(selected, 0, db_n_items());
239 memset(selected, 1, db_n_items());
243 list_set_selection(int item, int value)
245 assert(is_valid_item(item));
247 selected[item] = !!value;
251 list_invert_curitem_selection()
253 assert(is_valid_item(curitem));
255 selected[curitem] = !selected[curitem];
259 move_curitem(int direction)
263 if(curitem < 0 || curitem > last_item())
267 item_copy(tmp, db_item_get(curitem));
273 item_copy(db_item_get(curitem),
274 db_item_get(curitem - 1));
275 item_copy(db_item_get(curitem-1), tmp);
280 if(curitem >= last_item())
282 item_copy(db_item_get(curitem),
283 db_item_get(curitem + 1));
284 item_copy(db_item_get(curitem + 1), tmp);
306 curitem = last_item();
312 highlight_line(WINDOW *win, int line)
317 * this is a tricky one
321 mvwchgat(win, line, 0, -1, A_STANDOUT, 0, NULL);
324 * buggy function: FIXME
326 scrollok(win, FALSE);
330 for(i = 0; i < COLS; i++)
332 /*wattrset(win, 0);*/
343 for(i = 0; i < db_n_items(); i++)
358 for(i = 0; i < db_n_items(); i++)
359 selected[i] = !selected[i];
365 return db_n_items() < 1;
375 list_set_curitem(int i)
388 item = item_create();
389 item_duplicate(item, db_item_get(curitem));
390 if(add_item2database(item)) {
396 curitem = last_item();