2 new tags for the base skin.
[kugel-rb.git] / apps / gui / list.c
blobccb51959ff3103d8e5da34e6c7087381d4ccdc6b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Kevin Ferrare
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "lcd.h"
24 #include "font.h"
25 #include "button.h"
26 #include "sprintf.h"
27 #include "string.h"
28 #include "settings.h"
29 #include "kernel.h"
30 #include "system.h"
32 #include "action.h"
33 #include "screen_access.h"
34 #include "list.h"
35 #include "scrollbar.h"
36 #include "lang.h"
37 #include "sound.h"
38 #include "misc.h"
39 #include "talk.h"
40 #include "viewport.h"
41 #include "appevents.h"
42 #include "statusbar-skinned.h"
44 /* The minimum number of pending button events in queue before starting
45 * to limit list drawing interval.
47 #define FRAMEDROP_TRIGGER 6
49 #ifdef HAVE_LCD_BITMAP
50 static int offset_step = 16; /* pixels per screen scroll step */
51 /* should lines scroll out of the screen */
52 static bool offset_out_of_view = false;
53 #endif
55 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
56 int offset);
57 void list_draw(struct screen *display, struct gui_synclist *list);
59 #ifdef HAVE_LCD_BITMAP
60 static int list_need_reinit = false;
61 static struct viewport parent[NB_SCREENS];
63 static void list_force_reinit(void *param)
65 (void)param;
66 list_need_reinit = true;
69 void list_init(void)
71 add_event(GUI_EVENT_THEME_CHANGED, false, list_force_reinit);
74 static void list_init_viewports(struct gui_synclist *list)
76 int i, parent_used;
78 if (!list)
79 return;
81 parent_used = (*list->parent != &parent[SCREEN_MAIN]);
83 if (!parent_used)
85 FOR_NB_SCREENS(i)
87 list->parent[i] = &parent[i];
88 viewport_set_defaults(&parent[i], i);
89 #ifdef HAVE_BUTTONBAR
90 if (screens[i].has_buttonbar)
91 list->parent[i]->height -= BUTTONBAR_HEIGHT;
92 #endif
95 list_need_reinit = false;
97 #else
98 static struct viewport parent[NB_SCREENS] =
100 [SCREEN_MAIN] =
102 .x = 0,
103 .y = 0,
104 .width = LCD_WIDTH,
105 .height = LCD_HEIGHT
109 #define list_init_viewports(a)
110 #endif
112 #ifdef HAVE_LCD_BITMAP
113 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
115 return list->title != NULL &&
116 viewport_get_nb_lines(list->parent[screen]) > 2;
119 static int list_get_nb_lines(struct gui_synclist *list, enum screen_type screen)
121 struct viewport vp = *list->parent[screen];
122 if (list_display_title(list, screen))
123 vp.height -= font_get(list->parent[screen]->font)->height;
124 return viewport_get_nb_lines(&vp);
126 #else
127 #define list_display_title(l, i) false
128 #define list_get_nb_lines(list, screen) \
129 viewport_get_nb_lines((list)->parent[(screen)]);
130 #endif
133 * Initializes a scrolling list
134 * - gui_list : the list structure to initialize
135 * - callback_get_item_name : pointer to a function that associates a label
136 * to a given item number
137 * - data : extra data passed to the list callback
138 * - scroll_all :
139 * - selected_size :
140 * - parent : the parent viewports to use. NULL means the full screen minus
141 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
143 void gui_synclist_init(struct gui_synclist * gui_list,
144 list_get_name callback_get_item_name,
145 void * data,
146 bool scroll_all,
147 int selected_size, struct viewport list_parent[NB_SCREENS]
150 int i;
151 gui_list->callback_get_item_icon = NULL;
152 gui_list->callback_get_item_name = callback_get_item_name;
153 gui_list->callback_speak_item = NULL;
154 gui_list->nb_items = 0;
155 gui_list->selected_item = 0;
156 FOR_NB_SCREENS(i)
158 gui_list->start_item[i] = 0;
159 #ifdef HAVE_LCD_BITMAP
160 gui_list->offset_position[i] = 0;
161 #endif
162 if (list_parent)
163 gui_list->parent[i] = &list_parent[i];
164 else
165 gui_list->parent[i] = &parent[i];
167 list_init_viewports(gui_list);
168 gui_list->limit_scroll = false;
169 gui_list->data = data;
170 gui_list->scroll_all = scroll_all;
171 gui_list->selected_size = selected_size;
172 gui_list->title = NULL;
173 gui_list->title_icon = Icon_NOICON;
175 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
176 gui_list->show_selection_marker = true;
178 #ifdef HAVE_LCD_COLOR
179 gui_list->title_color = -1;
180 gui_list->callback_get_item_color = NULL;
181 #endif
184 /* this toggles the selection bar or cursor */
185 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
187 lists->show_selection_marker = !hide;
191 #ifdef HAVE_LCD_BITMAP
192 int gui_list_get_item_offset(struct gui_synclist * gui_list,
193 int item_width,
194 int text_pos,
195 struct screen * display,
196 struct viewport *vp)
198 int item_offset;
200 if (offset_out_of_view)
202 item_offset = gui_list->offset_position[display->screen_type];
204 else
206 /* if text is smaller than view */
207 if (item_width <= vp->width - text_pos)
209 item_offset = 0;
211 /* if text got out of view */
212 else if (gui_list->offset_position[display->screen_type] >
213 item_width - (vp->width - text_pos))
215 item_offset = item_width - (vp->width - text_pos);
217 else
218 item_offset = gui_list->offset_position[display->screen_type];
221 return item_offset;
223 #endif
226 * Force a full screen update.
228 void gui_synclist_draw(struct gui_synclist *gui_list)
230 int i;
231 #ifdef HAVE_LCD_BITMAP
232 if (list_need_reinit)
234 list_init_viewports(gui_list);
235 gui_synclist_select_item(gui_list, gui_list->selected_item);
237 #endif
238 FOR_NB_SCREENS(i)
240 list_draw(&screens[i], gui_list);
244 /* sets up the list so the selection is shown correctly on the screen */
245 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
246 enum screen_type screen)
248 int nb_lines = list_get_nb_lines(gui_list, screen);
249 int bottom = MAX(0, gui_list->nb_items - nb_lines);
250 int new_start_item = gui_list->start_item[screen];
251 int difference = gui_list->selected_item - gui_list->start_item[screen];
252 #ifdef HAVE_LCD_CHARCELLS
253 const int scroll_limit_up = 0;
254 const int scroll_limit_down = 1;
255 #else
256 const int scroll_limit_up = (nb_lines < gui_list->selected_size+2 ? 0:1);
257 const int scroll_limit_down = (scroll_limit_up+gui_list->selected_size);
258 #endif
260 if (gui_list->show_selection_marker == false)
262 new_start_item = gui_list->selected_item;
264 else if (gui_list->selected_size >= nb_lines)
266 new_start_item = gui_list->selected_item;
268 else if (global_settings.scroll_paginated)
270 nb_lines -= nb_lines%gui_list->selected_size;
271 if (difference < 0 || difference >= nb_lines)
273 new_start_item = gui_list->selected_item -
274 (gui_list->selected_item%nb_lines);
277 else if (difference <= scroll_limit_up) /* list moved up */
279 new_start_item = gui_list->selected_item - scroll_limit_up;
281 else if (difference > nb_lines - scroll_limit_down) /* list moved down */
283 new_start_item = gui_list->selected_item + scroll_limit_down - nb_lines;
285 if (new_start_item < 0)
286 gui_list->start_item[screen] = 0;
287 else if (new_start_item > bottom)
288 gui_list->start_item[screen] = bottom;
289 else
290 gui_list->start_item[screen] = new_start_item;
294 * Selects an item in the list
295 * - gui_list : the list structure
296 * - item_number : the number of the item which will be selected
298 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
300 int i;
301 if (item_number >= gui_list->nb_items || item_number < 0)
302 return;
303 gui_list->selected_item = item_number;
304 FOR_NB_SCREENS(i)
305 gui_list_put_selection_on_screen(gui_list, i);
308 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
309 int offset)
311 int new_selection;
312 if (gui_list->selected_size > 1)
314 offset *= gui_list->selected_size;
317 new_selection = gui_list->selected_item + offset;
319 if (new_selection >= gui_list->nb_items)
321 gui_list->selected_item = gui_list->limit_scroll ?
322 gui_list->nb_items - gui_list->selected_size : 0;
324 else if (new_selection < 0)
326 gui_list->selected_item = gui_list->limit_scroll ?
327 0 : gui_list->nb_items - gui_list->selected_size;
329 else if (gui_list->show_selection_marker == false)
331 int i, nb_lines, screen_top;
332 FOR_NB_SCREENS(i)
334 nb_lines = list_get_nb_lines(gui_list, i);
335 if (offset > 0)
337 screen_top = MAX(0, gui_list->nb_items - nb_lines);
338 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
339 gui_list->selected_size);
340 gui_list->selected_item = gui_list->start_item[i];
342 else
344 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
345 gui_list->selected_size);
346 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
349 return;
351 else gui_list->selected_item += offset;
352 gui_synclist_select_item(gui_list, gui_list->selected_item);
356 * Adds an item to the list (the callback will be asked for one more item)
357 * - gui_list : the list structure
359 void gui_synclist_add_item(struct gui_synclist * gui_list)
361 gui_list->nb_items++;
362 /* if only one item in the list, select it */
363 if (gui_list->nb_items == 1)
364 gui_list->selected_item = 0;
368 * Removes an item to the list (the callback will be asked for one less item)
369 * - gui_list : the list structure
371 void gui_synclist_del_item(struct gui_synclist * gui_list)
373 if (gui_list->nb_items > 0)
375 if (gui_list->selected_item == gui_list->nb_items-1)
376 gui_list->selected_item--;
377 gui_list->nb_items--;
378 gui_synclist_select_item(gui_list, gui_list->selected_item);
382 #ifdef HAVE_LCD_BITMAP
383 void gui_list_screen_scroll_step(int ofs)
385 offset_step = ofs;
388 void gui_list_screen_scroll_out_of_view(bool enable)
390 offset_out_of_view = enable;
392 #endif /* HAVE_LCD_BITMAP */
395 * Set the title and title icon of the list. Setting title to NULL disables
396 * both the title and icon. Use NOICON if there is no icon.
398 void gui_synclist_set_title(struct gui_synclist * gui_list,
399 char * title, enum themable_icons icon)
401 gui_list->title = title;
402 gui_list->title_icon = icon;
403 #ifdef HAVE_LCD_BITMAP
404 int i;
405 FOR_NB_SCREENS(i)
406 sb_set_title_text(title, icon, i);
407 #endif
408 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
411 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
413 #ifdef HAVE_LCD_BITMAP
414 int i;
415 #endif
416 lists->nb_items = nb_items;
417 #ifdef HAVE_LCD_BITMAP
418 FOR_NB_SCREENS(i)
420 lists->offset_position[i] = 0;
422 #endif
424 int gui_synclist_get_nb_items(struct gui_synclist * lists)
426 return lists->nb_items;
428 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
430 return lists->selected_item;
432 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
433 list_get_icon icon_callback)
435 lists->callback_get_item_icon = icon_callback;
438 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
439 list_speak_item voice_callback)
441 lists->callback_speak_item = voice_callback;
444 #ifdef HAVE_LCD_COLOR
445 void gui_synclist_set_color_callback(struct gui_synclist * lists,
446 list_get_color color_callback)
448 lists->callback_get_item_color = color_callback;
450 #endif
452 static void gui_synclist_select_next_page(struct gui_synclist * lists,
453 enum screen_type screen)
455 int nb_lines = list_get_nb_lines(lists, screen);
456 if (lists->selected_size > 1)
457 nb_lines = MAX(1, nb_lines/lists->selected_size);
458 gui_list_select_at_offset(lists, nb_lines);
461 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
462 enum screen_type screen)
464 int nb_lines = list_get_nb_lines(lists, screen);
465 if (lists->selected_size > 1)
466 nb_lines = MAX(1, nb_lines/lists->selected_size);
467 gui_list_select_at_offset(lists, -nb_lines);
470 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
472 lists->limit_scroll = scroll;
475 #ifdef HAVE_LCD_BITMAP
477 * Makes all the item in the list scroll by one step to the right.
478 * Should stop increasing the value when reaching the widest item value
479 * in the list.
481 static void gui_synclist_scroll_right(struct gui_synclist * lists)
483 int i;
484 FOR_NB_SCREENS(i)
486 /* FIXME: This is a fake right boundry limiter. there should be some
487 * callback function to find the longest item on the list in pixels,
488 * to stop the list from scrolling past that point */
489 lists->offset_position[i] += offset_step;
490 if (lists->offset_position[i] > 1000)
491 lists->offset_position[i] = 1000;
496 * Makes all the item in the list scroll by one step to the left.
497 * stops at starting position.
499 static void gui_synclist_scroll_left(struct gui_synclist * lists)
501 int i;
502 FOR_NB_SCREENS(i)
504 lists->offset_position[i] -= offset_step;
505 if (lists->offset_position[i] < 0)
506 lists->offset_position[i] = 0;
509 #endif /* HAVE_LCD_BITMAP */
511 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
513 list_speak_item *cb = lists->callback_speak_item;
514 if(cb && gui_synclist_get_nb_items(lists) != 0)
516 int sel = gui_synclist_get_sel_pos(lists);
517 talk_shutup();
518 /* If we got a repeating key action, or we have just very
519 recently started talking, then we want to stay silent for a
520 while until things settle. Likewise if we already had a
521 pending scheduled announcement not yet due: we need to
522 reschedule it. */
523 if(repeating
524 || (lists->scheduled_talk_tick
525 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
526 || (lists->last_talked_tick
527 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
529 lists->scheduled_talk_tick = current_tick +HZ/4;
530 return;
531 } else {
532 lists->scheduled_talk_tick = 0; /* work done */
533 cb(sel, lists->data);
534 lists->last_talked_tick = current_tick;
538 void gui_synclist_speak_item(struct gui_synclist * lists)
539 /* The list user should call this to speak the first item on entering
540 the list, and whenever the list is updated. */
542 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
543 talk_id(VOICE_EMPTY_LIST, true);
544 else _gui_synclist_speak_item(lists, false);
547 bool gui_synclist_do_button(struct gui_synclist * lists,
548 int *actionptr, enum list_wrap wrap)
550 int action = *actionptr;
551 #ifdef HAVE_LCD_BITMAP
552 static bool scrolling_left = false;
553 #endif
555 #ifdef HAVE_WHEEL_ACCELERATION
556 int next_item_modifier = button_apply_acceleration(get_action_data());
557 #else
558 static int next_item_modifier = 1;
559 static int last_accel_tick = 0;
560 if (global_settings.list_accel_start_delay)
562 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
563 int accel_wait = global_settings.list_accel_wait * HZ/2;
565 if (get_action_statuscode(NULL)&ACTION_REPEAT)
567 if (!last_accel_tick)
568 last_accel_tick = current_tick + start_delay;
569 else if (TIME_AFTER(current_tick, last_accel_tick + accel_wait))
571 last_accel_tick = current_tick;
572 next_item_modifier++;
575 else if (last_accel_tick)
577 next_item_modifier = 1;
578 last_accel_tick = 0;
581 #endif
583 #if defined(HAVE_TOUCHSCREEN)
584 if (action == ACTION_TOUCHSCREEN)
585 action = *actionptr = gui_synclist_do_touchscreen(lists);
586 #endif
588 switch (wrap)
590 case LIST_WRAP_ON:
591 gui_synclist_limit_scroll(lists, false);
592 break;
593 case LIST_WRAP_OFF:
594 gui_synclist_limit_scroll(lists, true);
595 break;
596 case LIST_WRAP_UNLESS_HELD:
597 if (action == ACTION_STD_PREVREPEAT ||
598 action == ACTION_STD_NEXTREPEAT ||
599 action == ACTION_LISTTREE_PGUP ||
600 action == ACTION_LISTTREE_PGDOWN)
601 gui_synclist_limit_scroll(lists, true);
602 else gui_synclist_limit_scroll(lists, false);
603 break;
606 switch (action)
608 case ACTION_REDRAW:
609 gui_synclist_draw(lists);
610 return true;
612 #ifdef HAVE_VOLUME_IN_LIST
613 case ACTION_LIST_VOLUP:
614 global_settings.volume += 2;
615 /* up two because the falthrough brings it down one */
616 case ACTION_LIST_VOLDOWN:
617 global_settings.volume--;
618 setvol();
619 return true;
620 #endif
621 case ACTION_STD_PREV:
622 case ACTION_STD_PREVREPEAT:
623 gui_list_select_at_offset(lists, -next_item_modifier);
624 #ifndef HAVE_WHEEL_ACCELERATION
625 if (button_queue_count() < FRAMEDROP_TRIGGER)
626 #endif
627 gui_synclist_draw(lists);
628 _gui_synclist_speak_item(lists,
629 action == ACTION_STD_PREVREPEAT
630 || next_item_modifier > 1);
631 yield();
632 *actionptr = ACTION_STD_PREV;
633 return true;
635 case ACTION_STD_NEXT:
636 case ACTION_STD_NEXTREPEAT:
637 gui_list_select_at_offset(lists, next_item_modifier);
638 #ifndef HAVE_WHEEL_ACCELERATION
639 if (button_queue_count() < FRAMEDROP_TRIGGER)
640 #endif
641 gui_synclist_draw(lists);
642 _gui_synclist_speak_item(lists,
643 action == ACTION_STD_NEXTREPEAT
644 || next_item_modifier >1);
645 yield();
646 *actionptr = ACTION_STD_NEXT;
647 return true;
649 #ifdef HAVE_LCD_BITMAP
650 case ACTION_TREE_PGRIGHT:
651 gui_synclist_scroll_right(lists);
652 gui_synclist_draw(lists);
653 return true;
654 case ACTION_TREE_ROOT_INIT:
655 /* After this button press ACTION_TREE_PGLEFT is allowed
656 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
657 keymaps as a repeated button press (the same as the repeated
658 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
659 button press */
660 if (lists->offset_position[0] == 0)
662 scrolling_left = false;
663 *actionptr = ACTION_STD_CANCEL;
664 return true;
666 *actionptr = ACTION_TREE_PGLEFT;
667 case ACTION_TREE_PGLEFT:
668 if(!scrolling_left && (lists->offset_position[0] == 0))
670 *actionptr = ACTION_STD_CANCEL;
671 return false;
673 gui_synclist_scroll_left(lists);
674 gui_synclist_draw(lists);
675 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
676 skipping to root */
677 return true;
678 #endif
679 /* for pgup / pgdown, we are obliged to have a different behaviour depending
680 * on the screen for which the user pressed the key since for example, remote
681 * and main screen doesn't have the same number of lines */
682 case ACTION_LISTTREE_PGUP:
684 int screen =
685 #ifdef HAVE_REMOTE_LCD
686 get_action_statuscode(NULL)&ACTION_REMOTE ?
687 SCREEN_REMOTE :
688 #endif
689 SCREEN_MAIN;
690 gui_synclist_select_previous_page(lists, screen);
691 gui_synclist_draw(lists);
692 _gui_synclist_speak_item(lists, false);
693 yield();
694 *actionptr = ACTION_STD_NEXT;
696 return true;
698 case ACTION_LISTTREE_PGDOWN:
700 int screen =
701 #ifdef HAVE_REMOTE_LCD
702 get_action_statuscode(NULL)&ACTION_REMOTE ?
703 SCREEN_REMOTE :
704 #endif
705 SCREEN_MAIN;
706 gui_synclist_select_next_page(lists, screen);
707 gui_synclist_draw(lists);
708 _gui_synclist_speak_item(lists, false);
709 yield();
710 *actionptr = ACTION_STD_PREV;
712 return true;
714 if(lists->scheduled_talk_tick
715 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
716 /* scheduled postponed item announcement is due */
717 _gui_synclist_speak_item(lists, false);
718 return false;
721 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
722 /* Returns the lowest of timeout or the delay until a postponed
723 scheduled announcement is due (if any). */
725 if(lists->scheduled_talk_tick)
727 long delay = lists->scheduled_talk_tick -current_tick +1;
728 /* +1 because the trigger condition uses TIME_AFTER(), which
729 is implemented as strictly greater than. */
730 if(delay < 0)
731 delay = 0;
732 if(timeout > delay || timeout == TIMEOUT_BLOCK)
733 timeout = delay;
735 return timeout;
738 bool list_do_action(int context, int timeout,
739 struct gui_synclist *lists, int *action,
740 enum list_wrap wrap)
741 /* Combines the get_action() (with possibly overridden timeout) and
742 gui_synclist_do_button() calls. Returns the list action from
743 do_button, and places the action from get_action in *action. */
745 timeout = list_do_action_timeout(lists, timeout);
746 *action = get_action(context, timeout);
747 return gui_synclist_do_button(lists, action, wrap);
750 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
751 enum screen_type screen, int item)
753 int nb_lines = list_get_nb_lines(lists, screen);
754 return (unsigned)(item - lists->start_item[screen]) < (unsigned) nb_lines;
757 /* Simple use list implementation */
758 static int simplelist_line_count = 0;
759 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
760 /* set the amount of lines shown in the list */
761 void simplelist_set_line_count(int lines)
763 if (lines < 0)
764 simplelist_line_count = 0;
765 else if (lines >= SIMPLELIST_MAX_LINES)
766 simplelist_line_count = SIMPLELIST_MAX_LINES;
767 else
768 simplelist_line_count = lines;
770 /* get the current amount of lines shown */
771 int simplelist_get_line_count(void)
773 return simplelist_line_count;
775 /* add/edit a line in the list.
776 if line_number > number of lines shown it adds the line,
777 else it edits the line */
778 void simplelist_addline(int line_number, const char *fmt, ...)
780 va_list ap;
782 if (line_number > simplelist_line_count)
784 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
785 line_number = simplelist_line_count++;
786 else
787 return;
789 va_start(ap, fmt);
790 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
791 va_end(ap);
794 static const char* simplelist_static_getname(int item,
795 void * data,
796 char *buffer,
797 size_t buffer_len)
799 (void)data; (void)buffer; (void)buffer_len;
800 return simplelist_text[item];
803 bool simplelist_show_list(struct simplelist_info *info)
805 struct gui_synclist lists;
806 int action, old_line_count = simplelist_line_count, i;
807 const char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
808 int wrap = LIST_WRAP_UNLESS_HELD;
809 if (info->get_name)
810 getname = info->get_name;
811 else
812 getname = simplelist_static_getname;
814 FOR_NB_SCREENS(i)
815 viewportmanager_theme_enable(i, true, NULL);
817 gui_synclist_init(&lists, getname, info->callback_data,
818 info->scroll_all, info->selection_size, NULL);
820 if (info->title)
821 gui_synclist_set_title(&lists, info->title, NOICON);
822 if (info->get_icon)
823 gui_synclist_set_icon_callback(&lists, info->get_icon);
824 if (info->get_talk)
825 gui_synclist_set_voice_callback(&lists, info->get_talk);
827 if (info->hide_selection)
829 gui_synclist_hide_selection_marker(&lists, true);
830 wrap = LIST_WRAP_OFF;
833 if (info->action_callback)
834 info->action_callback(ACTION_REDRAW, &lists);
836 if (info->get_name == NULL)
837 gui_synclist_set_nb_items(&lists,
838 simplelist_line_count*info->selection_size);
839 else
840 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
842 gui_synclist_select_item(&lists, info->selection);
844 gui_synclist_draw(&lists);
845 gui_synclist_speak_item(&lists);
847 while(1)
849 list_do_action(CONTEXT_STD, info->timeout,
850 &lists, &action, wrap);
852 /* We must yield in this case or no other thread can run */
853 if (info->timeout == TIMEOUT_NOBLOCK)
854 yield();
856 if (info->action_callback)
858 bool stdok = action==ACTION_STD_OK;
859 action = info->action_callback(action, &lists);
860 if (stdok && action == ACTION_STD_CANCEL)
862 /* callback asked us to exit */
863 info->selection = gui_synclist_get_sel_pos(&lists);
864 break;
867 if (info->get_name == NULL)
868 gui_synclist_set_nb_items(&lists,
869 simplelist_line_count*info->selection_size);
871 if (action == ACTION_STD_CANCEL)
873 info->selection = -1;
874 break;
876 else if ((action == ACTION_REDRAW) ||
877 (old_line_count != simplelist_line_count))
879 if (info->get_name == NULL)
881 gui_synclist_set_nb_items(&lists,
882 simplelist_line_count*info->selection_size);
884 gui_synclist_draw(&lists);
885 old_line_count = simplelist_line_count;
887 else if(default_event_handler(action) == SYS_USB_CONNECTED)
888 return true;
890 talk_shutup();
891 FOR_NB_SCREENS(i)
892 viewportmanager_theme_undo(i, false);
893 return false;
896 void simplelist_info_init(struct simplelist_info *info, char* title,
897 int count, void* data)
899 info->title = title;
900 info->count = count;
901 info->selection_size = 1;
902 info->hide_selection = false;
903 info->scroll_all = false;
904 info->timeout = HZ/10;
905 info->selection = 0;
906 info->action_callback = NULL;
907 info->get_icon = NULL;
908 info->get_name = NULL;
909 info->get_talk = NULL;
910 info->callback_data = data;