Packard Bell Vibe 500: Start committing plugin keymaps.
[kugel-rb.git] / apps / gui / list.c
blob0c05e017856acd9152a933defe40f3f08cfb6597
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"
43 /* The minimum number of pending button events in queue before starting
44 * to limit list drawing interval.
46 #define FRAMEDROP_TRIGGER 6
48 #ifdef HAVE_LCD_BITMAP
49 static int offset_step = 16; /* pixels per screen scroll step */
50 /* should lines scroll out of the screen */
51 static bool offset_out_of_view = false;
52 #endif
54 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
55 int offset);
56 void list_draw(struct screen *display, struct gui_synclist *list);
58 #ifdef HAVE_LCD_BITMAP
59 static int list_need_reinit = false;
60 static struct viewport parent[NB_SCREENS];
62 static void list_force_reinit(void *param)
64 (void)param;
65 list_need_reinit = true;
68 void list_init(void)
70 add_event(GUI_EVENT_THEME_CHANGED, false, list_force_reinit);
73 static void list_init_viewports(struct gui_synclist *list)
75 int i, parent_used;
77 if (!list)
78 return;
80 parent_used = (*list->parent != &parent[SCREEN_MAIN]);
82 if (!parent_used)
84 FOR_NB_SCREENS(i)
86 list->parent[i] = &parent[i];
87 viewport_set_defaults(&parent[i], i);
88 #ifdef HAVE_BUTTONBAR
89 if (screens[i].has_buttonbar)
90 list->parent[i]->height -= BUTTONBAR_HEIGHT;
91 #endif
94 list_need_reinit = false;
96 #else
97 static struct viewport parent[NB_SCREENS] =
99 [SCREEN_MAIN] =
101 .x = 0,
102 .y = 0,
103 .width = LCD_WIDTH,
104 .height = LCD_HEIGHT
108 #define list_init_viewports(a)
109 #endif
111 #ifdef HAVE_LCD_BITMAP
112 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
114 return list->title != NULL &&
115 viewport_get_nb_lines(list->parent[screen]) > 2;
118 static int list_get_nb_lines(struct gui_synclist *list, enum screen_type screen)
120 struct viewport vp = *list->parent[screen];
121 if (list_display_title(list, screen))
122 vp.height -= font_get(list->parent[screen]->font)->height;
123 return viewport_get_nb_lines(&vp);
125 #else
126 #define list_display_title(l, i) false
127 #define list_get_nb_lines(list, screen) \
128 viewport_get_nb_lines((list)->parent[(screen)]);
129 #endif
132 * Initializes a scrolling list
133 * - gui_list : the list structure to initialize
134 * - callback_get_item_name : pointer to a function that associates a label
135 * to a given item number
136 * - data : extra data passed to the list callback
137 * - scroll_all :
138 * - selected_size :
139 * - parent : the parent viewports to use. NULL means the full screen minus
140 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
142 void gui_synclist_init(struct gui_synclist * gui_list,
143 list_get_name callback_get_item_name,
144 void * data,
145 bool scroll_all,
146 int selected_size, struct viewport list_parent[NB_SCREENS]
149 int i;
150 gui_list->callback_get_item_icon = NULL;
151 gui_list->callback_get_item_name = callback_get_item_name;
152 gui_list->callback_speak_item = NULL;
153 gui_list->nb_items = 0;
154 gui_list->selected_item = 0;
155 FOR_NB_SCREENS(i)
157 gui_list->start_item[i] = 0;
158 gui_list->last_displayed_start_item[i] = -1 ;
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_width = 0;
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;
178 gui_list->last_displayed_selected_item = -1;
180 #ifdef HAVE_LCD_COLOR
181 gui_list->title_color = -1;
182 gui_list->callback_get_item_color = NULL;
183 #endif
186 /* this toggles the selection bar or cursor */
187 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
189 lists->show_selection_marker = !hide;
193 #ifdef HAVE_LCD_BITMAP
194 int gui_list_get_item_offset(struct gui_synclist * gui_list,
195 int item_width,
196 int text_pos,
197 struct screen * display,
198 struct viewport *vp)
200 int item_offset;
202 if (offset_out_of_view)
204 item_offset = gui_list->offset_position[display->screen_type];
206 else
208 /* if text is smaller than view */
209 if (item_width <= vp->width - text_pos)
211 item_offset = 0;
213 /* if text got out of view */
214 else if (gui_list->offset_position[display->screen_type] >
215 item_width - (vp->width - text_pos))
217 item_offset = item_width - (vp->width - text_pos);
219 else
220 item_offset = gui_list->offset_position[display->screen_type];
223 return item_offset;
225 #endif
228 * Force a full screen update.
230 void gui_synclist_draw(struct gui_synclist *gui_list)
232 int i;
233 #ifdef HAVE_LCD_BITMAP
234 if (list_need_reinit)
236 list_init_viewports(gui_list);
237 gui_synclist_select_item(gui_list, gui_list->selected_item);
239 #endif
240 FOR_NB_SCREENS(i)
242 list_draw(&screens[i], gui_list);
246 /* sets up the list so the selection is shown correctly on the screen */
247 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
248 enum screen_type screen)
250 int nb_lines = list_get_nb_lines(gui_list, screen);
251 int bottom = MAX(0, gui_list->nb_items - nb_lines);
252 int new_start_item = gui_list->start_item[screen];
253 int difference = gui_list->selected_item - gui_list->start_item[screen];
254 #ifdef HAVE_LCD_CHARCELLS
255 const int scroll_limit_up = 0;
256 const int scroll_limit_down = 1;
257 #else
258 const int scroll_limit_up = (nb_lines < gui_list->selected_size+2 ? 0:1);
259 const int scroll_limit_down = (scroll_limit_up+gui_list->selected_size);
260 #endif
262 if (gui_list->show_selection_marker == false)
264 new_start_item = gui_list->selected_item;
266 else if (gui_list->selected_size >= nb_lines)
268 new_start_item = gui_list->selected_item;
270 else if (global_settings.scroll_paginated)
272 nb_lines -= nb_lines%gui_list->selected_size;
273 if (difference < 0 || difference >= nb_lines)
275 new_start_item = gui_list->selected_item -
276 (gui_list->selected_item%nb_lines);
279 else if (difference <= scroll_limit_up) /* list moved up */
281 new_start_item = gui_list->selected_item - scroll_limit_up;
283 else if (difference > nb_lines - scroll_limit_down) /* list moved down */
285 new_start_item = gui_list->selected_item + scroll_limit_down - nb_lines;
287 if (new_start_item < 0)
288 gui_list->start_item[screen] = 0;
289 else if (new_start_item > bottom)
290 gui_list->start_item[screen] = bottom;
291 else
292 gui_list->start_item[screen] = new_start_item;
296 * Selects an item in the list
297 * - gui_list : the list structure
298 * - item_number : the number of the item which will be selected
300 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
302 int i;
303 if (item_number >= gui_list->nb_items || item_number < 0)
304 return;
305 gui_list->selected_item = item_number;
306 FOR_NB_SCREENS(i)
307 gui_list_put_selection_on_screen(gui_list, i);
310 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
311 int offset)
313 int new_selection;
314 if (gui_list->selected_size > 1)
316 offset *= gui_list->selected_size;
319 new_selection = gui_list->selected_item + offset;
321 if (new_selection >= gui_list->nb_items)
323 gui_list->selected_item = gui_list->limit_scroll ?
324 gui_list->nb_items - gui_list->selected_size : 0;
326 else if (new_selection < 0)
328 gui_list->selected_item = gui_list->limit_scroll ?
329 0 : gui_list->nb_items - gui_list->selected_size;
331 else if (gui_list->show_selection_marker == false)
333 int i, nb_lines, screen_top;
334 FOR_NB_SCREENS(i)
336 nb_lines = list_get_nb_lines(gui_list, i);
337 if (offset > 0)
339 screen_top = MAX(0, gui_list->nb_items - nb_lines);
340 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
341 gui_list->selected_size);
342 gui_list->selected_item = gui_list->start_item[i];
344 else
346 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
347 gui_list->selected_size);
348 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
351 return;
353 else gui_list->selected_item += offset;
354 gui_synclist_select_item(gui_list, gui_list->selected_item);
358 * Adds an item to the list (the callback will be asked for one more item)
359 * - gui_list : the list structure
361 void gui_synclist_add_item(struct gui_synclist * gui_list)
363 gui_list->nb_items++;
364 /* if only one item in the list, select it */
365 if (gui_list->nb_items == 1)
366 gui_list->selected_item = 0;
370 * Removes an item to the list (the callback will be asked for one less item)
371 * - gui_list : the list structure
373 void gui_synclist_del_item(struct gui_synclist * gui_list)
375 if (gui_list->nb_items > 0)
377 if (gui_list->selected_item == gui_list->nb_items-1)
378 gui_list->selected_item--;
379 gui_list->nb_items--;
380 gui_synclist_select_item(gui_list, gui_list->selected_item);
384 #ifdef HAVE_LCD_BITMAP
385 void gui_list_screen_scroll_step(int ofs)
387 offset_step = ofs;
390 void gui_list_screen_scroll_out_of_view(bool enable)
392 offset_out_of_view = enable;
394 #endif /* HAVE_LCD_BITMAP */
397 * Set the title and title icon of the list. Setting title to NULL disables
398 * both the title and icon. Use NOICON if there is no icon.
400 void gui_synclist_set_title(struct gui_synclist * gui_list,
401 char * title, enum themable_icons icon)
403 gui_list->title = title;
404 gui_list->title_icon = icon;
405 if (title)
407 #ifdef HAVE_LCD_BITMAP
408 int i;
409 FOR_NB_SCREENS(i)
410 screens[i].getstringsize(title, &gui_list->title_width, NULL);
411 #else
412 gui_list->title_width = strlen(title);
413 #endif
415 else
417 gui_list->title_width = 0;
421 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
423 #ifdef HAVE_LCD_BITMAP
424 int i;
425 #endif
426 lists->nb_items = nb_items;
427 #ifdef HAVE_LCD_BITMAP
428 FOR_NB_SCREENS(i)
430 lists->offset_position[i] = 0;
432 #endif
434 int gui_synclist_get_nb_items(struct gui_synclist * lists)
436 return lists->nb_items;
438 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
440 return lists->selected_item;
442 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
443 list_get_icon icon_callback)
445 lists->callback_get_item_icon = icon_callback;
448 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
449 list_speak_item voice_callback)
451 lists->callback_speak_item = voice_callback;
454 #ifdef HAVE_LCD_COLOR
455 void gui_synclist_set_color_callback(struct gui_synclist * lists,
456 list_get_color color_callback)
458 lists->callback_get_item_color = color_callback;
460 #endif
462 static void gui_synclist_select_next_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 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
472 enum screen_type screen)
474 int nb_lines = list_get_nb_lines(lists, screen);
475 if (lists->selected_size > 1)
476 nb_lines = MAX(1, nb_lines/lists->selected_size);
477 gui_list_select_at_offset(lists, -nb_lines);
480 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
482 lists->limit_scroll = scroll;
485 #ifdef HAVE_LCD_BITMAP
487 * Makes all the item in the list scroll by one step to the right.
488 * Should stop increasing the value when reaching the widest item value
489 * in the list.
491 static void gui_synclist_scroll_right(struct gui_synclist * lists)
493 int i;
494 FOR_NB_SCREENS(i)
496 /* FIXME: This is a fake right boundry limiter. there should be some
497 * callback function to find the longest item on the list in pixels,
498 * to stop the list from scrolling past that point */
499 lists->offset_position[i] += offset_step;
500 if (lists->offset_position[i] > 1000)
501 lists->offset_position[i] = 1000;
506 * Makes all the item in the list scroll by one step to the left.
507 * stops at starting position.
509 static void gui_synclist_scroll_left(struct gui_synclist * lists)
511 int i;
512 FOR_NB_SCREENS(i)
514 lists->offset_position[i] -= offset_step;
515 if (lists->offset_position[i] < 0)
516 lists->offset_position[i] = 0;
519 #endif /* HAVE_LCD_BITMAP */
521 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
523 list_speak_item *cb = lists->callback_speak_item;
524 if(cb && gui_synclist_get_nb_items(lists) != 0)
526 int sel = gui_synclist_get_sel_pos(lists);
527 talk_shutup();
528 /* If we got a repeating key action, or we have just very
529 recently started talking, then we want to stay silent for a
530 while until things settle. Likewise if we already had a
531 pending scheduled announcement not yet due: we need to
532 reschedule it. */
533 if(repeating
534 || (lists->scheduled_talk_tick
535 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
536 || (lists->last_talked_tick
537 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
539 lists->scheduled_talk_tick = current_tick +HZ/4;
540 return;
541 } else {
542 lists->scheduled_talk_tick = 0; /* work done */
543 cb(sel, lists->data);
544 lists->last_talked_tick = current_tick;
548 void gui_synclist_speak_item(struct gui_synclist * lists)
549 /* The list user should call this to speak the first item on entering
550 the list, and whenever the list is updated. */
552 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
553 talk_id(VOICE_EMPTY_LIST, true);
554 else _gui_synclist_speak_item(lists, false);
557 bool gui_synclist_do_button(struct gui_synclist * lists,
558 int *actionptr, enum list_wrap wrap)
560 int action = *actionptr;
561 #ifdef HAVE_LCD_BITMAP
562 static bool scrolling_left = false;
563 #endif
565 #ifdef HAVE_WHEEL_ACCELERATION
566 int next_item_modifier = button_apply_acceleration(get_action_data());
567 #else
568 static int next_item_modifier = 1;
569 static int last_accel_tick = 0;
571 if (global_settings.list_accel_start_delay)
573 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
574 int accel_wait = global_settings.list_accel_wait * HZ/2;
576 if (get_action_statuscode(NULL)&ACTION_REPEAT)
578 if (!last_accel_tick)
579 last_accel_tick = current_tick + start_delay;
580 else if (TIME_AFTER(current_tick, last_accel_tick + accel_wait))
582 last_accel_tick = current_tick;
583 next_item_modifier++;
586 else if (last_accel_tick)
588 next_item_modifier = 1;
589 last_accel_tick = 0;
592 #endif
594 #if defined(HAVE_TOUCHSCREEN)
595 if (action == ACTION_TOUCHSCREEN)
596 action = *actionptr = gui_synclist_do_touchscreen(lists);
597 #endif
599 switch (wrap)
601 case LIST_WRAP_ON:
602 gui_synclist_limit_scroll(lists, false);
603 break;
604 case LIST_WRAP_OFF:
605 gui_synclist_limit_scroll(lists, true);
606 break;
607 case LIST_WRAP_UNLESS_HELD:
608 if (action == ACTION_STD_PREVREPEAT ||
609 action == ACTION_STD_NEXTREPEAT ||
610 action == ACTION_LISTTREE_PGUP ||
611 action == ACTION_LISTTREE_PGDOWN)
612 gui_synclist_limit_scroll(lists, true);
613 else gui_synclist_limit_scroll(lists, false);
614 break;
617 switch (action)
619 case ACTION_REDRAW:
620 gui_synclist_draw(lists);
621 return true;
623 #ifdef HAVE_VOLUME_IN_LIST
624 case ACTION_LIST_VOLUP:
625 global_settings.volume += 2;
626 /* up two because the falthrough brings it down one */
627 case ACTION_LIST_VOLDOWN:
628 global_settings.volume--;
629 setvol();
630 return true;
631 #endif
632 case ACTION_STD_PREV:
633 case ACTION_STD_PREVREPEAT:
634 gui_list_select_at_offset(lists, -next_item_modifier);
635 #ifndef HAVE_WHEEL_ACCELERATION
636 if (button_queue_count() < FRAMEDROP_TRIGGER)
637 #endif
638 gui_synclist_draw(lists);
639 _gui_synclist_speak_item(lists,
640 action == ACTION_STD_PREVREPEAT
641 || next_item_modifier > 1);
642 yield();
643 *actionptr = ACTION_STD_PREV;
644 return true;
646 case ACTION_STD_NEXT:
647 case ACTION_STD_NEXTREPEAT:
648 gui_list_select_at_offset(lists, next_item_modifier);
649 #ifndef HAVE_WHEEL_ACCELERATION
650 if (button_queue_count() < FRAMEDROP_TRIGGER)
651 #endif
652 gui_synclist_draw(lists);
653 _gui_synclist_speak_item(lists,
654 action == ACTION_STD_NEXTREPEAT
655 || next_item_modifier >1);
656 yield();
657 *actionptr = ACTION_STD_NEXT;
658 return true;
660 #ifdef HAVE_LCD_BITMAP
661 case ACTION_TREE_PGRIGHT:
662 gui_synclist_scroll_right(lists);
663 gui_synclist_draw(lists);
664 return true;
665 case ACTION_TREE_ROOT_INIT:
666 /* After this button press ACTION_TREE_PGLEFT is allowed
667 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
668 keymaps as a repeated button press (the same as the repeated
669 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
670 button press */
671 if (lists->offset_position[0] == 0)
673 scrolling_left = false;
674 *actionptr = ACTION_STD_CANCEL;
675 return true;
677 *actionptr = ACTION_TREE_PGLEFT;
678 case ACTION_TREE_PGLEFT:
679 if(!scrolling_left && (lists->offset_position[0] == 0))
681 *actionptr = ACTION_STD_CANCEL;
682 return false;
684 gui_synclist_scroll_left(lists);
685 gui_synclist_draw(lists);
686 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
687 skipping to root */
688 return true;
689 #endif
690 /* for pgup / pgdown, we are obliged to have a different behaviour depending
691 * on the screen for which the user pressed the key since for example, remote
692 * and main screen doesn't have the same number of lines */
693 case ACTION_LISTTREE_PGUP:
695 int screen =
696 #ifdef HAVE_REMOTE_LCD
697 get_action_statuscode(NULL)&ACTION_REMOTE ?
698 SCREEN_REMOTE :
699 #endif
700 SCREEN_MAIN;
701 gui_synclist_select_previous_page(lists, screen);
702 gui_synclist_draw(lists);
703 _gui_synclist_speak_item(lists, false);
704 yield();
705 *actionptr = ACTION_STD_NEXT;
707 return true;
709 case ACTION_LISTTREE_PGDOWN:
711 int screen =
712 #ifdef HAVE_REMOTE_LCD
713 get_action_statuscode(NULL)&ACTION_REMOTE ?
714 SCREEN_REMOTE :
715 #endif
716 SCREEN_MAIN;
717 gui_synclist_select_next_page(lists, screen);
718 gui_synclist_draw(lists);
719 _gui_synclist_speak_item(lists, false);
720 yield();
721 *actionptr = ACTION_STD_PREV;
723 return true;
725 if(lists->scheduled_talk_tick
726 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
727 /* scheduled postponed item announcement is due */
728 _gui_synclist_speak_item(lists, false);
729 return false;
732 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
733 /* Returns the lowest of timeout or the delay until a postponed
734 scheduled announcement is due (if any). */
736 if(lists->scheduled_talk_tick)
738 long delay = lists->scheduled_talk_tick -current_tick +1;
739 /* +1 because the trigger condition uses TIME_AFTER(), which
740 is implemented as strictly greater than. */
741 if(delay < 0)
742 delay = 0;
743 if(timeout > delay || timeout == TIMEOUT_BLOCK)
744 timeout = delay;
746 return timeout;
749 bool list_do_action(int context, int timeout,
750 struct gui_synclist *lists, int *action,
751 enum list_wrap wrap)
752 /* Combines the get_action() (with possibly overridden timeout) and
753 gui_synclist_do_button() calls. Returns the list action from
754 do_button, and places the action from get_action in *action. */
756 timeout = list_do_action_timeout(lists, timeout);
757 *action = get_action(context, timeout);
758 return gui_synclist_do_button(lists, action, wrap);
761 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
762 enum screen_type screen, int item)
764 int nb_lines = list_get_nb_lines(lists, screen);
765 return (unsigned)(item - lists->start_item[screen]) < (unsigned) nb_lines;
768 /* Simple use list implementation */
769 static int simplelist_line_count = 0;
770 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
771 /* set the amount of lines shown in the list */
772 void simplelist_set_line_count(int lines)
774 if (lines < 0)
775 simplelist_line_count = 0;
776 else if (lines >= SIMPLELIST_MAX_LINES)
777 simplelist_line_count = SIMPLELIST_MAX_LINES;
778 else
779 simplelist_line_count = lines;
781 /* get the current amount of lines shown */
782 int simplelist_get_line_count(void)
784 return simplelist_line_count;
786 /* add/edit a line in the list.
787 if line_number > number of lines shown it adds the line,
788 else it edits the line */
789 void simplelist_addline(int line_number, const char *fmt, ...)
791 va_list ap;
793 if (line_number > simplelist_line_count)
795 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
796 line_number = simplelist_line_count++;
797 else
798 return;
800 va_start(ap, fmt);
801 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
802 va_end(ap);
805 static const char* simplelist_static_getname(int item,
806 void * data,
807 char *buffer,
808 size_t buffer_len)
810 (void)data; (void)buffer; (void)buffer_len;
811 return simplelist_text[item];
814 bool simplelist_show_list(struct simplelist_info *info)
816 struct gui_synclist lists;
817 int action, old_line_count = simplelist_line_count, i;
818 const char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
819 int wrap = LIST_WRAP_UNLESS_HELD;
820 if (info->get_name)
821 getname = info->get_name;
822 else
823 getname = simplelist_static_getname;
825 FOR_NB_SCREENS(i)
826 viewportmanager_theme_enable(i, true, NULL);
828 gui_synclist_init(&lists, getname, info->callback_data,
829 info->scroll_all, info->selection_size, NULL);
831 if (info->title)
832 gui_synclist_set_title(&lists, info->title, NOICON);
833 if (info->get_icon)
834 gui_synclist_set_icon_callback(&lists, info->get_icon);
835 if (info->get_talk)
836 gui_synclist_set_voice_callback(&lists, info->get_talk);
838 if (info->hide_selection)
840 gui_synclist_hide_selection_marker(&lists, true);
841 wrap = LIST_WRAP_OFF;
844 if (info->action_callback)
845 info->action_callback(ACTION_REDRAW, &lists);
847 if (info->get_name == NULL)
848 gui_synclist_set_nb_items(&lists,
849 simplelist_line_count*info->selection_size);
850 else
851 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
853 gui_synclist_select_item(&lists, info->selection);
855 gui_synclist_draw(&lists);
856 gui_synclist_speak_item(&lists);
858 while(1)
860 list_do_action(CONTEXT_STD, info->timeout,
861 &lists, &action, wrap);
863 /* We must yield in this case or no other thread can run */
864 if (info->timeout == TIMEOUT_NOBLOCK)
865 yield();
867 if (info->action_callback)
869 bool stdok = action==ACTION_STD_OK;
870 action = info->action_callback(action, &lists);
871 if (stdok && action == ACTION_STD_CANCEL)
873 /* callback asked us to exit */
874 info->selection = gui_synclist_get_sel_pos(&lists);
875 break;
878 if (info->get_name == NULL)
879 gui_synclist_set_nb_items(&lists,
880 simplelist_line_count*info->selection_size);
882 if (action == ACTION_STD_CANCEL)
884 info->selection = -1;
885 break;
887 else if ((action == ACTION_REDRAW) ||
888 (old_line_count != simplelist_line_count))
890 if (info->get_name == NULL)
892 gui_synclist_set_nb_items(&lists,
893 simplelist_line_count*info->selection_size);
895 gui_synclist_draw(&lists);
896 old_line_count = simplelist_line_count;
898 else if(default_event_handler(action) == SYS_USB_CONNECTED)
899 return true;
901 talk_shutup();
902 FOR_NB_SCREENS(i)
903 viewportmanager_theme_undo(i, false);
904 return false;
907 void simplelist_info_init(struct simplelist_info *info, char* title,
908 int count, void* data)
910 info->title = title;
911 info->count = count;
912 info->selection_size = 1;
913 info->hide_selection = false;
914 info->scroll_all = false;
915 info->timeout = HZ/10;
916 info->selection = 0;
917 info->action_callback = NULL;
918 info->get_icon = NULL;
919 info->get_name = NULL;
920 info->get_talk = NULL;
921 info->callback_data = data;