Use API call instead of accessing a global variable for receiving the current thread.
[kugel-rb.git] / apps / gui / list.c
blob11a4c360158fcddea5f7d272c65bc3efe1ff0060
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 !sb_set_title_text(list->title, list->title_icon, screen) &&
117 viewport_get_nb_lines(list->parent[screen]) > 2;
120 static int list_get_nb_lines(struct gui_synclist *list, enum screen_type screen)
122 struct viewport vp = *list->parent[screen];
123 if (list_display_title(list, screen))
124 vp.height -= font_get(list->parent[screen]->font)->height;
125 return viewport_get_nb_lines(&vp);
127 #else
128 #define list_display_title(l, i) false
129 #define list_get_nb_lines(list, screen) \
130 viewport_get_nb_lines((list)->parent[(screen)]);
131 #endif
134 * Initializes a scrolling list
135 * - gui_list : the list structure to initialize
136 * - callback_get_item_name : pointer to a function that associates a label
137 * to a given item number
138 * - data : extra data passed to the list callback
139 * - scroll_all :
140 * - selected_size :
141 * - parent : the parent viewports to use. NULL means the full screen minus
142 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
144 void gui_synclist_init(struct gui_synclist * gui_list,
145 list_get_name callback_get_item_name,
146 void * data,
147 bool scroll_all,
148 int selected_size, struct viewport list_parent[NB_SCREENS]
151 int i;
152 gui_list->callback_get_item_icon = NULL;
153 gui_list->callback_get_item_name = callback_get_item_name;
154 gui_list->callback_speak_item = NULL;
155 gui_list->nb_items = 0;
156 gui_list->selected_item = 0;
157 FOR_NB_SCREENS(i)
159 gui_list->start_item[i] = 0;
160 #ifdef HAVE_LCD_BITMAP
161 gui_list->offset_position[i] = 0;
162 #endif
163 if (list_parent)
164 gui_list->parent[i] = &list_parent[i];
165 else
166 gui_list->parent[i] = &parent[i];
168 list_init_viewports(gui_list);
169 gui_list->limit_scroll = false;
170 gui_list->data = data;
171 gui_list->scroll_all = scroll_all;
172 gui_list->selected_size = selected_size;
173 gui_list->title = NULL;
174 gui_list->title_icon = Icon_NOICON;
176 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
177 gui_list->show_selection_marker = true;
179 #ifdef HAVE_LCD_COLOR
180 gui_list->title_color = -1;
181 gui_list->callback_get_item_color = NULL;
182 #endif
185 /* this toggles the selection bar or cursor */
186 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
188 lists->show_selection_marker = !hide;
192 #ifdef HAVE_LCD_BITMAP
193 int gui_list_get_item_offset(struct gui_synclist * gui_list,
194 int item_width,
195 int text_pos,
196 struct screen * display,
197 struct viewport *vp)
199 int item_offset;
201 if (offset_out_of_view)
203 item_offset = gui_list->offset_position[display->screen_type];
205 else
207 /* if text is smaller than view */
208 if (item_width <= vp->width - text_pos)
210 item_offset = 0;
212 /* if text got out of view */
213 else if (gui_list->offset_position[display->screen_type] >
214 item_width - (vp->width - text_pos))
216 item_offset = item_width - (vp->width - text_pos);
218 else
219 item_offset = gui_list->offset_position[display->screen_type];
222 return item_offset;
224 #endif
227 * Force a full screen update.
229 void gui_synclist_draw(struct gui_synclist *gui_list)
231 int i;
232 #ifdef HAVE_LCD_BITMAP
233 if (list_need_reinit)
235 list_init_viewports(gui_list);
236 gui_synclist_select_item(gui_list, gui_list->selected_item);
238 #endif
239 FOR_NB_SCREENS(i)
241 list_draw(&screens[i], gui_list);
245 /* sets up the list so the selection is shown correctly on the screen */
246 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
247 enum screen_type screen)
249 int nb_lines = list_get_nb_lines(gui_list, screen);
250 int bottom = MAX(0, gui_list->nb_items - nb_lines);
251 int new_start_item = gui_list->start_item[screen];
252 int difference = gui_list->selected_item - gui_list->start_item[screen];
253 #ifdef HAVE_LCD_CHARCELLS
254 const int scroll_limit_up = 0;
255 const int scroll_limit_down = 1;
256 #else
257 const int scroll_limit_up = (nb_lines < gui_list->selected_size+2 ? 0:1);
258 const int scroll_limit_down = (scroll_limit_up+gui_list->selected_size);
259 #endif
261 if (gui_list->show_selection_marker == false)
263 new_start_item = gui_list->selected_item;
265 else if (gui_list->selected_size >= nb_lines)
267 new_start_item = gui_list->selected_item;
269 else if (global_settings.scroll_paginated)
271 nb_lines -= nb_lines%gui_list->selected_size;
272 if (difference < 0 || difference >= nb_lines)
274 new_start_item = gui_list->selected_item -
275 (gui_list->selected_item%nb_lines);
278 else if (difference <= scroll_limit_up) /* list moved up */
280 new_start_item = gui_list->selected_item - scroll_limit_up;
282 else if (difference > nb_lines - scroll_limit_down) /* list moved down */
284 new_start_item = gui_list->selected_item + scroll_limit_down - nb_lines;
286 if (new_start_item < 0)
287 gui_list->start_item[screen] = 0;
288 else if (new_start_item > bottom)
289 gui_list->start_item[screen] = bottom;
290 else
291 gui_list->start_item[screen] = new_start_item;
295 * Selects an item in the list
296 * - gui_list : the list structure
297 * - item_number : the number of the item which will be selected
299 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
301 int i;
302 if (item_number >= gui_list->nb_items || item_number < 0)
303 return;
304 gui_list->selected_item = item_number;
305 FOR_NB_SCREENS(i)
306 gui_list_put_selection_on_screen(gui_list, i);
309 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
310 int offset)
312 int new_selection;
313 if (gui_list->selected_size > 1)
315 offset *= gui_list->selected_size;
318 new_selection = gui_list->selected_item + offset;
320 if (new_selection >= gui_list->nb_items)
322 gui_list->selected_item = gui_list->limit_scroll ?
323 gui_list->nb_items - gui_list->selected_size : 0;
325 else if (new_selection < 0)
327 gui_list->selected_item = gui_list->limit_scroll ?
328 0 : gui_list->nb_items - gui_list->selected_size;
330 else if (gui_list->show_selection_marker == false)
332 int i, nb_lines, screen_top;
333 FOR_NB_SCREENS(i)
335 nb_lines = list_get_nb_lines(gui_list, i);
336 if (offset > 0)
338 screen_top = MAX(0, gui_list->nb_items - nb_lines);
339 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
340 gui_list->selected_size);
341 gui_list->selected_item = gui_list->start_item[i];
343 else
345 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
346 gui_list->selected_size);
347 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
350 return;
352 else gui_list->selected_item += offset;
353 gui_synclist_select_item(gui_list, gui_list->selected_item);
357 * Adds an item to the list (the callback will be asked for one more item)
358 * - gui_list : the list structure
360 void gui_synclist_add_item(struct gui_synclist * gui_list)
362 gui_list->nb_items++;
363 /* if only one item in the list, select it */
364 if (gui_list->nb_items == 1)
365 gui_list->selected_item = 0;
369 * Removes an item to the list (the callback will be asked for one less item)
370 * - gui_list : the list structure
372 void gui_synclist_del_item(struct gui_synclist * gui_list)
374 if (gui_list->nb_items > 0)
376 if (gui_list->selected_item == gui_list->nb_items-1)
377 gui_list->selected_item--;
378 gui_list->nb_items--;
379 gui_synclist_select_item(gui_list, gui_list->selected_item);
383 #ifdef HAVE_LCD_BITMAP
384 void gui_list_screen_scroll_step(int ofs)
386 offset_step = ofs;
389 void gui_list_screen_scroll_out_of_view(bool enable)
391 offset_out_of_view = enable;
393 #endif /* HAVE_LCD_BITMAP */
396 * Set the title and title icon of the list. Setting title to NULL disables
397 * both the title and icon. Use NOICON if there is no icon.
399 void gui_synclist_set_title(struct gui_synclist * gui_list,
400 char * title, enum themable_icons icon)
402 gui_list->title = title;
403 gui_list->title_icon = icon;
404 #ifdef HAVE_LCD_BITMAP
405 int i;
406 FOR_NB_SCREENS(i)
407 sb_set_title_text(title, icon, i);
408 #endif
409 send_event(GUI_EVENT_ACTIONUPDATE, (void*)1);
412 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
414 #ifdef HAVE_LCD_BITMAP
415 int i;
416 #endif
417 lists->nb_items = nb_items;
418 #ifdef HAVE_LCD_BITMAP
419 FOR_NB_SCREENS(i)
421 lists->offset_position[i] = 0;
423 #endif
425 int gui_synclist_get_nb_items(struct gui_synclist * lists)
427 return lists->nb_items;
429 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
431 return lists->selected_item;
433 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
434 list_get_icon icon_callback)
436 lists->callback_get_item_icon = icon_callback;
439 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
440 list_speak_item voice_callback)
442 lists->callback_speak_item = voice_callback;
445 #ifdef HAVE_LCD_COLOR
446 void gui_synclist_set_color_callback(struct gui_synclist * lists,
447 list_get_color color_callback)
449 lists->callback_get_item_color = color_callback;
451 #endif
453 static void gui_synclist_select_next_page(struct gui_synclist * lists,
454 enum screen_type screen)
456 int nb_lines = list_get_nb_lines(lists, screen);
457 if (lists->selected_size > 1)
458 nb_lines = MAX(1, nb_lines/lists->selected_size);
459 gui_list_select_at_offset(lists, nb_lines);
462 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
463 enum screen_type screen)
465 int nb_lines = list_get_nb_lines(lists, screen);
466 if (lists->selected_size > 1)
467 nb_lines = MAX(1, nb_lines/lists->selected_size);
468 gui_list_select_at_offset(lists, -nb_lines);
471 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
473 lists->limit_scroll = scroll;
476 #ifdef HAVE_LCD_BITMAP
478 * Makes all the item in the list scroll by one step to the right.
479 * Should stop increasing the value when reaching the widest item value
480 * in the list.
482 static void gui_synclist_scroll_right(struct gui_synclist * lists)
484 int i;
485 FOR_NB_SCREENS(i)
487 /* FIXME: This is a fake right boundry limiter. there should be some
488 * callback function to find the longest item on the list in pixels,
489 * to stop the list from scrolling past that point */
490 lists->offset_position[i] += offset_step;
491 if (lists->offset_position[i] > 1000)
492 lists->offset_position[i] = 1000;
497 * Makes all the item in the list scroll by one step to the left.
498 * stops at starting position.
500 static void gui_synclist_scroll_left(struct gui_synclist * lists)
502 int i;
503 FOR_NB_SCREENS(i)
505 lists->offset_position[i] -= offset_step;
506 if (lists->offset_position[i] < 0)
507 lists->offset_position[i] = 0;
510 #endif /* HAVE_LCD_BITMAP */
512 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
514 list_speak_item *cb = lists->callback_speak_item;
515 if(cb && gui_synclist_get_nb_items(lists) != 0)
517 int sel = gui_synclist_get_sel_pos(lists);
518 talk_shutup();
519 /* If we got a repeating key action, or we have just very
520 recently started talking, then we want to stay silent for a
521 while until things settle. Likewise if we already had a
522 pending scheduled announcement not yet due: we need to
523 reschedule it. */
524 if(repeating
525 || (lists->scheduled_talk_tick
526 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
527 || (lists->last_talked_tick
528 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
530 lists->scheduled_talk_tick = current_tick +HZ/4;
531 return;
532 } else {
533 lists->scheduled_talk_tick = 0; /* work done */
534 cb(sel, lists->data);
535 lists->last_talked_tick = current_tick;
539 void gui_synclist_speak_item(struct gui_synclist * lists)
540 /* The list user should call this to speak the first item on entering
541 the list, and whenever the list is updated. */
543 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
544 talk_id(VOICE_EMPTY_LIST, true);
545 else _gui_synclist_speak_item(lists, false);
548 bool gui_synclist_do_button(struct gui_synclist * lists,
549 int *actionptr, enum list_wrap wrap)
551 int action = *actionptr;
552 #ifdef HAVE_LCD_BITMAP
553 static bool scrolling_left = false;
554 #endif
556 #ifdef HAVE_WHEEL_ACCELERATION
557 int next_item_modifier = button_apply_acceleration(get_action_data());
558 #else
559 static int next_item_modifier = 1;
560 static int last_accel_tick = 0;
561 if (global_settings.list_accel_start_delay)
563 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
564 int accel_wait = global_settings.list_accel_wait * HZ/2;
566 if (get_action_statuscode(NULL)&ACTION_REPEAT)
568 if (!last_accel_tick)
569 last_accel_tick = current_tick + start_delay;
570 else if (TIME_AFTER(current_tick, last_accel_tick + accel_wait))
572 last_accel_tick = current_tick;
573 next_item_modifier++;
576 else if (last_accel_tick)
578 next_item_modifier = 1;
579 last_accel_tick = 0;
582 #endif
584 #if defined(HAVE_TOUCHSCREEN)
585 if (action == ACTION_TOUCHSCREEN)
586 action = *actionptr = gui_synclist_do_touchscreen(lists);
587 #endif
589 switch (wrap)
591 case LIST_WRAP_ON:
592 gui_synclist_limit_scroll(lists, false);
593 break;
594 case LIST_WRAP_OFF:
595 gui_synclist_limit_scroll(lists, true);
596 break;
597 case LIST_WRAP_UNLESS_HELD:
598 if (action == ACTION_STD_PREVREPEAT ||
599 action == ACTION_STD_NEXTREPEAT ||
600 action == ACTION_LISTTREE_PGUP ||
601 action == ACTION_LISTTREE_PGDOWN)
602 gui_synclist_limit_scroll(lists, true);
603 else gui_synclist_limit_scroll(lists, false);
604 break;
607 switch (action)
609 case ACTION_REDRAW:
610 gui_synclist_draw(lists);
611 return true;
613 #ifdef HAVE_VOLUME_IN_LIST
614 case ACTION_LIST_VOLUP:
615 global_settings.volume += 2;
616 /* up two because the falthrough brings it down one */
617 case ACTION_LIST_VOLDOWN:
618 global_settings.volume--;
619 setvol();
620 return true;
621 #endif
622 case ACTION_STD_PREV:
623 case ACTION_STD_PREVREPEAT:
624 gui_list_select_at_offset(lists, -next_item_modifier);
625 #ifndef HAVE_WHEEL_ACCELERATION
626 if (button_queue_count() < FRAMEDROP_TRIGGER)
627 #endif
628 gui_synclist_draw(lists);
629 _gui_synclist_speak_item(lists,
630 action == ACTION_STD_PREVREPEAT
631 || next_item_modifier > 1);
632 yield();
633 *actionptr = ACTION_STD_PREV;
634 return true;
636 case ACTION_STD_NEXT:
637 case ACTION_STD_NEXTREPEAT:
638 gui_list_select_at_offset(lists, next_item_modifier);
639 #ifndef HAVE_WHEEL_ACCELERATION
640 if (button_queue_count() < FRAMEDROP_TRIGGER)
641 #endif
642 gui_synclist_draw(lists);
643 _gui_synclist_speak_item(lists,
644 action == ACTION_STD_NEXTREPEAT
645 || next_item_modifier >1);
646 yield();
647 *actionptr = ACTION_STD_NEXT;
648 return true;
650 #ifdef HAVE_LCD_BITMAP
651 case ACTION_TREE_PGRIGHT:
652 gui_synclist_scroll_right(lists);
653 gui_synclist_draw(lists);
654 return true;
655 case ACTION_TREE_ROOT_INIT:
656 /* After this button press ACTION_TREE_PGLEFT is allowed
657 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
658 keymaps as a repeated button press (the same as the repeated
659 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
660 button press */
661 if (lists->offset_position[0] == 0)
663 scrolling_left = false;
664 *actionptr = ACTION_STD_CANCEL;
665 return true;
667 *actionptr = ACTION_TREE_PGLEFT;
668 case ACTION_TREE_PGLEFT:
669 if(!scrolling_left && (lists->offset_position[0] == 0))
671 *actionptr = ACTION_STD_CANCEL;
672 return false;
674 gui_synclist_scroll_left(lists);
675 gui_synclist_draw(lists);
676 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
677 skipping to root */
678 return true;
679 #endif
680 /* for pgup / pgdown, we are obliged to have a different behaviour depending
681 * on the screen for which the user pressed the key since for example, remote
682 * and main screen doesn't have the same number of lines */
683 case ACTION_LISTTREE_PGUP:
685 int screen =
686 #ifdef HAVE_REMOTE_LCD
687 get_action_statuscode(NULL)&ACTION_REMOTE ?
688 SCREEN_REMOTE :
689 #endif
690 SCREEN_MAIN;
691 gui_synclist_select_previous_page(lists, screen);
692 gui_synclist_draw(lists);
693 _gui_synclist_speak_item(lists, false);
694 yield();
695 *actionptr = ACTION_STD_NEXT;
697 return true;
699 case ACTION_LISTTREE_PGDOWN:
701 int screen =
702 #ifdef HAVE_REMOTE_LCD
703 get_action_statuscode(NULL)&ACTION_REMOTE ?
704 SCREEN_REMOTE :
705 #endif
706 SCREEN_MAIN;
707 gui_synclist_select_next_page(lists, screen);
708 gui_synclist_draw(lists);
709 _gui_synclist_speak_item(lists, false);
710 yield();
711 *actionptr = ACTION_STD_PREV;
713 return true;
715 if(lists->scheduled_talk_tick
716 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
717 /* scheduled postponed item announcement is due */
718 _gui_synclist_speak_item(lists, false);
719 return false;
722 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
723 /* Returns the lowest of timeout or the delay until a postponed
724 scheduled announcement is due (if any). */
726 if(lists->scheduled_talk_tick)
728 long delay = lists->scheduled_talk_tick -current_tick +1;
729 /* +1 because the trigger condition uses TIME_AFTER(), which
730 is implemented as strictly greater than. */
731 if(delay < 0)
732 delay = 0;
733 if(timeout > delay || timeout == TIMEOUT_BLOCK)
734 timeout = delay;
736 return timeout;
739 bool list_do_action(int context, int timeout,
740 struct gui_synclist *lists, int *action,
741 enum list_wrap wrap)
742 /* Combines the get_action() (with possibly overridden timeout) and
743 gui_synclist_do_button() calls. Returns the list action from
744 do_button, and places the action from get_action in *action. */
746 timeout = list_do_action_timeout(lists, timeout);
747 *action = get_action(context, timeout);
748 return gui_synclist_do_button(lists, action, wrap);
751 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
752 enum screen_type screen, int item)
754 int nb_lines = list_get_nb_lines(lists, screen);
755 return (unsigned)(item - lists->start_item[screen]) < (unsigned) nb_lines;
758 /* Simple use list implementation */
759 static int simplelist_line_count = 0;
760 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
761 /* set the amount of lines shown in the list */
762 void simplelist_set_line_count(int lines)
764 if (lines < 0)
765 simplelist_line_count = 0;
766 else if (lines >= SIMPLELIST_MAX_LINES)
767 simplelist_line_count = SIMPLELIST_MAX_LINES;
768 else
769 simplelist_line_count = lines;
771 /* get the current amount of lines shown */
772 int simplelist_get_line_count(void)
774 return simplelist_line_count;
776 /* add/edit a line in the list.
777 if line_number > number of lines shown it adds the line,
778 else it edits the line */
779 void simplelist_addline(int line_number, const char *fmt, ...)
781 va_list ap;
783 if (line_number > simplelist_line_count)
785 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
786 line_number = simplelist_line_count++;
787 else
788 return;
790 va_start(ap, fmt);
791 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
792 va_end(ap);
795 static const char* simplelist_static_getname(int item,
796 void * data,
797 char *buffer,
798 size_t buffer_len)
800 (void)data; (void)buffer; (void)buffer_len;
801 return simplelist_text[item];
804 bool simplelist_show_list(struct simplelist_info *info)
806 struct gui_synclist lists;
807 int action, old_line_count = simplelist_line_count, i;
808 list_get_name *getname;
809 int wrap = LIST_WRAP_UNLESS_HELD;
810 if (info->get_name)
811 getname = info->get_name;
812 else
813 getname = simplelist_static_getname;
815 FOR_NB_SCREENS(i)
816 viewportmanager_theme_enable(i, true, NULL);
818 gui_synclist_init(&lists, getname, info->callback_data,
819 info->scroll_all, info->selection_size, NULL);
821 if (info->title)
822 gui_synclist_set_title(&lists, info->title, NOICON);
823 if (info->get_icon)
824 gui_synclist_set_icon_callback(&lists, info->get_icon);
825 if (info->get_talk)
826 gui_synclist_set_voice_callback(&lists, info->get_talk);
828 if (info->hide_selection)
830 gui_synclist_hide_selection_marker(&lists, true);
831 wrap = LIST_WRAP_OFF;
834 if (info->action_callback)
835 info->action_callback(ACTION_REDRAW, &lists);
837 if (info->get_name == NULL)
838 gui_synclist_set_nb_items(&lists,
839 simplelist_line_count*info->selection_size);
840 else
841 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
843 gui_synclist_select_item(&lists, info->selection);
845 gui_synclist_draw(&lists);
846 gui_synclist_speak_item(&lists);
848 while(1)
850 list_do_action(CONTEXT_STD, info->timeout,
851 &lists, &action, wrap);
853 /* We must yield in this case or no other thread can run */
854 if (info->timeout == TIMEOUT_NOBLOCK)
855 yield();
857 if (info->action_callback)
859 bool stdok = action==ACTION_STD_OK;
860 action = info->action_callback(action, &lists);
861 if (stdok && action == ACTION_STD_CANCEL)
863 /* callback asked us to exit */
864 info->selection = gui_synclist_get_sel_pos(&lists);
865 break;
868 if (info->get_name == NULL)
869 gui_synclist_set_nb_items(&lists,
870 simplelist_line_count*info->selection_size);
872 if (action == ACTION_STD_CANCEL)
874 info->selection = -1;
875 break;
877 else if ((action == ACTION_REDRAW) ||
878 (old_line_count != simplelist_line_count))
880 if (info->get_name == NULL)
882 gui_synclist_set_nb_items(&lists,
883 simplelist_line_count*info->selection_size);
885 gui_synclist_draw(&lists);
886 old_line_count = simplelist_line_count;
888 else if(default_event_handler(action) == SYS_USB_CONNECTED)
889 return true;
891 talk_shutup();
892 FOR_NB_SCREENS(i)
893 viewportmanager_theme_undo(i, false);
894 return false;
897 void simplelist_info_init(struct simplelist_info *info, char* title,
898 int count, void* data)
900 info->title = title;
901 info->count = count;
902 info->selection_size = 1;
903 info->hide_selection = false;
904 info->scroll_all = false;
905 info->timeout = HZ/10;
906 info->selection = 0;
907 info->action_callback = NULL;
908 info->get_icon = NULL;
909 info->get_name = NULL;
910 info->get_talk = NULL;
911 info->callback_data = data;
912 simplelist_line_count = 0;