Fix buttonbar handling/theme changed handling in lists.
[kugel-rb.git] / apps / gui / list.c
blob284b83012af3906812caafe757a69bf0dbbfe9f0
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 #ifdef HAVE_LCD_CHARCELLS
44 #define SCROLL_LIMIT 1
45 #else
46 #define SCROLL_LIMIT (nb_lines<3?1:2)
47 #endif
49 /* The minimum number of pending button events in queue before starting
50 * to limit list drawing interval.
52 #define FRAMEDROP_TRIGGER 6
54 #ifdef HAVE_LCD_BITMAP
55 static int offset_step = 16; /* pixels per screen scroll step */
56 /* should lines scroll out of the screen */
57 static bool offset_out_of_view = false;
58 #endif
60 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
61 int offset);
62 void list_draw(struct screen *display, struct gui_synclist *list);
64 #ifdef HAVE_LCD_BITMAP
65 static int list_need_reinit = false;
66 static struct viewport parent[NB_SCREENS];
68 static void list_force_reinit(void *param)
70 (void)param;
71 list_need_reinit = true;
74 void list_init(void)
76 add_event(GUI_EVENT_THEME_CHANGED, false, list_force_reinit);
79 static void list_init_viewports(struct gui_synclist *list)
81 int i, parent_used;
83 if (!list)
84 return;
86 parent_used = (*list->parent != &parent[SCREEN_MAIN]);
88 if (!parent_used)
90 FOR_NB_SCREENS(i)
92 list->parent[i] = &parent[i];
93 viewport_set_defaults(&parent[i], i);
94 #ifdef HAVE_BUTTONBAR
95 if (screens[i].has_buttonbar && !viewport_ui_vp_get_state(i))
96 list->parent[i]->height -= BUTTONBAR_HEIGHT;
97 #endif
101 #else
102 #define list_init_viewports(a)
103 #endif
105 #ifdef HAVE_LCD_BITMAP
106 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
108 return list->title != NULL &&
109 viewport_get_nb_lines(list->parent[screen])>2;
111 #else
112 static struct viewport parent[NB_SCREENS] =
114 [SCREEN_MAIN] =
116 .x = 0,
117 .y = 0,
118 .width = LCD_WIDTH,
119 .height = LCD_HEIGHT
122 #define list_display_title(l, i) false
123 #endif
126 * Initializes a scrolling list
127 * - gui_list : the list structure to initialize
128 * - callback_get_item_name : pointer to a function that associates a label
129 * to a given item number
130 * - data : extra data passed to the list callback
131 * - scroll_all :
132 * - selected_size :
133 * - parent : the parent viewports to use. NULL means the full screen minus
134 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
136 void gui_synclist_init(struct gui_synclist * gui_list,
137 list_get_name callback_get_item_name,
138 void * data,
139 bool scroll_all,
140 int selected_size, struct viewport list_parent[NB_SCREENS]
143 int i;
144 gui_list->callback_get_item_icon = NULL;
145 gui_list->callback_get_item_name = callback_get_item_name;
146 gui_list->callback_speak_item = NULL;
147 gui_list->nb_items = 0;
148 gui_list->selected_item = 0;
149 FOR_NB_SCREENS(i)
151 gui_list->start_item[i] = 0;
152 gui_list->last_displayed_start_item[i] = -1 ;
153 #ifdef HAVE_LCD_BITMAP
154 gui_list->offset_position[i] = 0;
155 #endif
156 if (list_parent)
157 gui_list->parent[i] = &list_parent[i];
158 else
159 gui_list->parent[i] = &parent[i];
161 list_init_viewports(gui_list);
162 gui_list->limit_scroll = false;
163 gui_list->data=data;
164 gui_list->scroll_all=scroll_all;
165 gui_list->selected_size=selected_size;
166 gui_list->title = NULL;
167 gui_list->title_width = 0;
168 gui_list->title_icon = Icon_NOICON;
170 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
171 gui_list->show_selection_marker = true;
172 gui_list->last_displayed_selected_item = -1 ;
174 #ifdef HAVE_LCD_COLOR
175 gui_list->title_color = -1;
176 gui_list->callback_get_item_color = NULL;
177 #endif
180 /* this toggles the selection bar or cursor */
181 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
183 lists->show_selection_marker = !hide;
187 #ifdef HAVE_LCD_BITMAP
188 int gui_list_get_item_offset(struct gui_synclist * gui_list,
189 int item_width,
190 int text_pos,
191 struct screen * display,
192 struct viewport *vp)
194 int item_offset;
196 if (offset_out_of_view)
198 item_offset = gui_list->offset_position[display->screen_type];
200 else
202 /* if text is smaller then view */
203 if (item_width <= vp->width - text_pos)
205 item_offset = 0;
207 else
209 /* if text got out of view */
210 if (gui_list->offset_position[display->screen_type] >
211 item_width - (vp->width - text_pos))
212 item_offset = item_width - (vp->width - text_pos);
213 else
214 item_offset = gui_list->offset_position[display->screen_type];
218 return item_offset;
220 #endif
222 * Force a full screen update.
225 void gui_synclist_draw(struct gui_synclist *gui_list)
227 int i;
228 #ifdef HAVE_LCD_BITMAP
229 if (list_need_reinit)
231 list_init_viewports(gui_list);
232 gui_synclist_select_item(gui_list, gui_list->selected_item);
234 #endif
235 FOR_NB_SCREENS(i)
237 list_draw(&screens[i], gui_list);
241 /* sets up the list so the selection is shown correctly on the screen */
242 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
243 enum screen_type screen)
245 int nb_lines;
246 int difference = gui_list->selected_item - gui_list->start_item[screen];
247 struct viewport vp = *gui_list->parent[screen];
248 #ifdef HAVE_LCD_BITMAP
249 if (list_display_title(gui_list, screen))
250 vp.height -= font_get(gui_list->parent[screen]->font)->height;
251 #endif
252 nb_lines = viewport_get_nb_lines(&vp);
254 /* edge case,, selected last item */
255 if (gui_list->selected_item == gui_list->nb_items -1)
257 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
259 /* selected first item */
260 else if (gui_list->selected_item == 0)
262 gui_list->start_item[screen] = 0;
264 else if (difference < SCROLL_LIMIT) /* list moved up */
266 if (global_settings.scroll_paginated)
268 if (gui_list->start_item[screen] > gui_list->selected_item)
270 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)
271 *nb_lines;
273 if (gui_list->nb_items <= nb_lines)
274 gui_list->start_item[screen] = 0;
276 else
278 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT + 1;
279 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
280 gui_list->start_item[screen] = MAX(0, temp);
283 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
285 int bottom = gui_list->nb_items - nb_lines;
286 /* always move the screen if selection isnt "visible" */
287 if (gui_list->show_selection_marker == false)
289 if (bottom < 0)
290 bottom = 0;
291 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
292 2*gui_list->selected_size);
294 else if (global_settings.scroll_paginated)
296 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
297 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
299 else
301 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
302 int temp = MAX(0, top_of_screen);
303 gui_list->start_item[screen] = MIN(bottom, temp);
308 * Selects an item in the list
309 * - gui_list : the list structure
310 * - item_number : the number of the item which will be selected
312 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
314 int i;
315 if( item_number > gui_list->nb_items-1 || item_number < 0 )
316 return;
317 gui_list->selected_item = item_number;
318 FOR_NB_SCREENS(i)
319 gui_list_put_selection_on_screen(gui_list, i);
322 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
323 int offset)
325 int new_selection;
326 if (gui_list->selected_size > 1)
328 offset *= gui_list->selected_size;
329 /* always select the first item of multi-line lists */
330 offset -= offset%gui_list->selected_size;
332 new_selection = gui_list->selected_item + offset;
333 if (new_selection >= gui_list->nb_items)
335 gui_list->selected_item = gui_list->limit_scroll ?
336 gui_list->nb_items - gui_list->selected_size : 0;
338 else if (new_selection < 0)
340 gui_list->selected_item = gui_list->limit_scroll ?
341 0 : gui_list->nb_items - gui_list->selected_size;
343 else if (gui_list->show_selection_marker == false)
345 int i, nb_lines, screen_top;
346 FOR_NB_SCREENS(i)
348 struct viewport vp = *gui_list->parent[i];
349 #ifdef HAVE_LCD_BITMAP
350 if (list_display_title(gui_list, i))
351 vp.height -= font_get(gui_list->parent[i]->font)->height;
352 #endif
353 nb_lines = viewport_get_nb_lines(&vp);
354 if (offset > 0)
356 screen_top = gui_list->nb_items-nb_lines;
357 if (screen_top < 0)
358 screen_top = 0;
359 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
360 gui_list->selected_size);
361 gui_list->selected_item = gui_list->start_item[i];
363 else
365 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
366 gui_list->selected_size);
367 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
370 return;
372 else gui_list->selected_item += offset;
373 gui_synclist_select_item(gui_list, gui_list->selected_item);
377 * Adds an item to the list (the callback will be asked for one more item)
378 * - gui_list : the list structure
380 void gui_synclist_add_item(struct gui_synclist * gui_list)
382 gui_list->nb_items++;
383 /* if only one item in the list, select it */
384 if(gui_list->nb_items == 1)
385 gui_list->selected_item = 0;
389 * Removes an item to the list (the callback will be asked for one less item)
390 * - gui_list : the list structure
392 void gui_synclist_del_item(struct gui_synclist * gui_list)
394 if(gui_list->nb_items > 0)
396 if (gui_list->selected_item == gui_list->nb_items-1)
397 gui_list->selected_item--;
398 gui_list->nb_items--;
399 gui_synclist_select_item(gui_list, gui_list->selected_item);
403 #ifdef HAVE_LCD_BITMAP
404 void gui_list_screen_scroll_step(int ofs)
406 offset_step = ofs;
409 void gui_list_screen_scroll_out_of_view(bool enable)
411 if (enable)
412 offset_out_of_view = true;
413 else
414 offset_out_of_view = false;
416 #endif /* HAVE_LCD_BITMAP */
419 * Set the title and title icon of the list. Setting title to NULL disables
420 * both the title and icon. Use NOICON if there is no icon.
422 void gui_synclist_set_title(struct gui_synclist * gui_list,
423 char * title, enum themable_icons icon)
425 gui_list->title = title;
426 gui_list->title_icon = icon;
427 if (title) {
428 #ifdef HAVE_LCD_BITMAP
429 int i;
430 FOR_NB_SCREENS(i)
431 screens[i].getstringsize(title, &gui_list->title_width, NULL);
432 #else
433 gui_list->title_width = strlen(title);
434 #endif
435 } else {
436 gui_list->title_width = 0;
441 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
443 #ifdef HAVE_LCD_BITMAP
444 int i;
445 #endif
446 lists->nb_items = nb_items;
447 #ifdef HAVE_LCD_BITMAP
448 FOR_NB_SCREENS(i)
450 lists->offset_position[i] = 0;
452 #endif
454 int gui_synclist_get_nb_items(struct gui_synclist * lists)
456 return lists->nb_items;
458 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
460 return lists->selected_item;
462 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
463 list_get_icon icon_callback)
465 lists->callback_get_item_icon = icon_callback;
468 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
469 list_speak_item voice_callback)
471 lists->callback_speak_item = voice_callback;
474 #ifdef HAVE_LCD_COLOR
475 void gui_synclist_set_color_callback(struct gui_synclist * lists,
476 list_get_color color_callback)
478 lists->callback_get_item_color = color_callback;
480 #endif
482 static void gui_synclist_select_next_page(struct gui_synclist * lists,
483 enum screen_type screen)
485 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
486 #ifdef HAVE_LCD_BITMAP
487 if(list_display_title(lists, screen))
488 nb_lines--;
489 #endif
490 gui_list_select_at_offset(lists, nb_lines);
493 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
494 enum screen_type screen)
496 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
497 #ifdef HAVE_LCD_BITMAP
498 if(list_display_title(lists, screen))
499 nb_lines--;
500 #endif
501 gui_list_select_at_offset(lists, -nb_lines);
504 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
506 lists->limit_scroll = scroll;
509 #ifdef HAVE_LCD_BITMAP
511 * Makes all the item in the list scroll by one step to the right.
512 * Should stop increasing the value when reaching the widest item value
513 * in the list.
515 static void gui_synclist_scroll_right(struct gui_synclist * lists)
517 int i;
518 FOR_NB_SCREENS(i)
520 /* FIXME: This is a fake right boundry limiter. there should be some
521 * callback function to find the longest item on the list in pixels,
522 * to stop the list from scrolling past that point */
523 lists->offset_position[i]+=offset_step;
524 if (lists->offset_position[i] > 1000)
525 lists->offset_position[i] = 1000;
530 * Makes all the item in the list scroll by one step to the left.
531 * stops at starting position.
533 static void gui_synclist_scroll_left(struct gui_synclist * lists)
535 int i;
536 FOR_NB_SCREENS(i)
538 lists->offset_position[i]-=offset_step;
539 if (lists->offset_position[i] < 0)
540 lists->offset_position[i] = 0;
543 #endif /* HAVE_LCD_BITMAP */
545 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
547 list_speak_item *cb = lists->callback_speak_item;
548 if(cb && gui_synclist_get_nb_items(lists) != 0)
550 int sel = gui_synclist_get_sel_pos(lists);
551 talk_shutup();
552 /* If we got a repeating key action, or we have just very
553 recently started talking, then we want to stay silent for a
554 while until things settle. Likewise if we already had a
555 pending scheduled announcement not yet due: we need to
556 reschedule it. */
557 if(repeating
558 || (lists->scheduled_talk_tick
559 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
560 || (lists->last_talked_tick
561 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
563 lists->scheduled_talk_tick = current_tick +HZ/4;
564 return;
565 } else {
566 lists->scheduled_talk_tick = 0; /* work done */
567 cb(sel, lists->data);
568 lists->last_talked_tick = current_tick;
572 void gui_synclist_speak_item(struct gui_synclist * lists)
573 /* The list user should call this to speak the first item on entering
574 the list, and whenever the list is updated. */
576 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
577 talk_id(VOICE_EMPTY_LIST, true);
578 else _gui_synclist_speak_item(lists, false);
581 bool gui_synclist_do_button(struct gui_synclist * lists,
582 int *actionptr, enum list_wrap wrap)
584 int action = *actionptr;
585 #ifdef HAVE_LCD_BITMAP
586 static bool scrolling_left = false;
587 #endif
589 #ifdef HAVE_WHEEL_ACCELERATION
590 int next_item_modifier = button_apply_acceleration(get_action_data());
591 #else
592 static int next_item_modifier = 1;
593 static int last_accel_tick = 0;
595 if (global_settings.list_accel_start_delay)
597 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
598 int accel_wait = global_settings.list_accel_wait * HZ/2;
600 if (get_action_statuscode(NULL)&ACTION_REPEAT)
602 if (!last_accel_tick)
603 last_accel_tick = current_tick + start_delay;
604 else if (current_tick >=
605 last_accel_tick + accel_wait)
607 last_accel_tick = current_tick;
608 next_item_modifier++;
611 else if (last_accel_tick)
613 next_item_modifier = 1;
614 last_accel_tick = 0;
617 #endif
619 #if defined(HAVE_TOUCHSCREEN)
620 if (action == ACTION_TOUCHSCREEN)
621 action = *actionptr = gui_synclist_do_touchscreen(lists);
622 #endif
624 switch (wrap)
626 case LIST_WRAP_ON:
627 gui_synclist_limit_scroll(lists, false);
628 break;
629 case LIST_WRAP_OFF:
630 gui_synclist_limit_scroll(lists, true);
631 break;
632 case LIST_WRAP_UNLESS_HELD:
633 if (action == ACTION_STD_PREVREPEAT ||
634 action == ACTION_STD_NEXTREPEAT ||
635 action == ACTION_LISTTREE_PGUP ||
636 action == ACTION_LISTTREE_PGDOWN)
637 gui_synclist_limit_scroll(lists, true);
638 else gui_synclist_limit_scroll(lists, false);
639 break;
642 switch (action)
644 case ACTION_REDRAW:
645 gui_synclist_draw(lists);
646 return true;
648 #ifdef HAVE_VOLUME_IN_LIST
649 case ACTION_LIST_VOLUP:
650 global_settings.volume += 2;
651 /* up two because the falthrough brings it down one */
652 case ACTION_LIST_VOLDOWN:
653 global_settings.volume--;
654 setvol();
655 return true;
656 #endif
657 case ACTION_STD_PREV:
658 case ACTION_STD_PREVREPEAT:
659 gui_list_select_at_offset(lists, -next_item_modifier);
660 #ifndef HAVE_WHEEL_ACCELERATION
661 if (button_queue_count() < FRAMEDROP_TRIGGER)
662 #endif
663 gui_synclist_draw(lists);
664 _gui_synclist_speak_item(lists,
665 action == ACTION_STD_PREVREPEAT
666 || next_item_modifier >1);
667 yield();
668 *actionptr = ACTION_STD_PREV;
669 return true;
671 case ACTION_STD_NEXT:
672 case ACTION_STD_NEXTREPEAT:
673 gui_list_select_at_offset(lists, next_item_modifier);
674 #ifndef HAVE_WHEEL_ACCELERATION
675 if (button_queue_count() < FRAMEDROP_TRIGGER)
676 #endif
677 gui_synclist_draw(lists);
678 _gui_synclist_speak_item(lists,
679 action == ACTION_STD_NEXTREPEAT
680 || next_item_modifier >1);
681 yield();
682 *actionptr = ACTION_STD_NEXT;
683 return true;
685 #ifdef HAVE_LCD_BITMAP
686 case ACTION_TREE_PGRIGHT:
687 gui_synclist_scroll_right(lists);
688 gui_synclist_draw(lists);
689 return true;
690 case ACTION_TREE_ROOT_INIT:
691 /* After this button press ACTION_TREE_PGLEFT is allowed
692 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
693 keymaps as a repeated button press (the same as the repeated
694 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
695 button press */
696 if (lists->offset_position[0] == 0)
698 scrolling_left = false;
699 *actionptr = ACTION_STD_CANCEL;
700 return true;
702 *actionptr = ACTION_TREE_PGLEFT;
703 case ACTION_TREE_PGLEFT:
704 if(!scrolling_left && (lists->offset_position[0] == 0))
706 *actionptr = ACTION_STD_CANCEL;
707 return false;
709 gui_synclist_scroll_left(lists);
710 gui_synclist_draw(lists);
711 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
712 skipping to root */
713 return true;
714 #endif
715 /* for pgup / pgdown, we are obliged to have a different behaviour depending
716 * on the screen for which the user pressed the key since for example, remote
717 * and main screen doesn't have the same number of lines */
718 case ACTION_LISTTREE_PGUP:
720 int screen =
721 #ifdef HAVE_REMOTE_LCD
722 get_action_statuscode(NULL)&ACTION_REMOTE ?
723 SCREEN_REMOTE :
724 #endif
725 SCREEN_MAIN;
726 gui_synclist_select_previous_page(lists, screen);
727 gui_synclist_draw(lists);
728 _gui_synclist_speak_item(lists, false);
729 yield();
730 *actionptr = ACTION_STD_NEXT;
732 return true;
734 case ACTION_LISTTREE_PGDOWN:
736 int screen =
737 #ifdef HAVE_REMOTE_LCD
738 get_action_statuscode(NULL)&ACTION_REMOTE ?
739 SCREEN_REMOTE :
740 #endif
741 SCREEN_MAIN;
742 gui_synclist_select_next_page(lists, screen);
743 gui_synclist_draw(lists);
744 _gui_synclist_speak_item(lists, false);
745 yield();
746 *actionptr = ACTION_STD_PREV;
748 return true;
750 if(lists->scheduled_talk_tick
751 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
752 /* scheduled postponed item announcement is due */
753 _gui_synclist_speak_item(lists, false);
754 return false;
757 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
758 /* Returns the lowest of timeout or the delay until a postponed
759 scheduled announcement is due (if any). */
761 if(lists->scheduled_talk_tick)
763 long delay = lists->scheduled_talk_tick -current_tick +1;
764 /* +1 because the trigger condition uses TIME_AFTER(), which
765 is implemented as strictly greater than. */
766 if(delay < 0)
767 delay = 0;
768 if(timeout > delay || timeout == TIMEOUT_BLOCK)
769 timeout = delay;
771 return timeout;
774 bool list_do_action(int context, int timeout,
775 struct gui_synclist *lists, int *action,
776 enum list_wrap wrap)
777 /* Combines the get_action() (with possibly overridden timeout) and
778 gui_synclist_do_button() calls. Returns the list action from
779 do_button, and places the action from get_action in *action. */
781 timeout = list_do_action_timeout(lists, timeout);
782 *action = get_action(context, timeout);
783 return gui_synclist_do_button(lists, action, wrap);
786 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
787 enum screen_type screen, int item)
789 struct viewport vp = *lists->parent[screen];
790 #ifdef HAVE_LCD_BITMAP
791 if (list_display_title(lists, screen))
792 vp.height -= font_get(lists->parent[screen]->font)->height;
793 #endif
794 return item <= (lists->start_item[screen] + viewport_get_nb_lines(&vp));
797 /* Simple use list implementation */
798 static int simplelist_line_count = 0;
799 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
800 /* set the amount of lines shown in the list */
801 void simplelist_set_line_count(int lines)
803 if (lines < 0)
804 simplelist_line_count = 0;
805 else if (lines >= SIMPLELIST_MAX_LINES)
806 simplelist_line_count = SIMPLELIST_MAX_LINES;
807 else
808 simplelist_line_count = lines;
810 /* get the current amount of lines shown */
811 int simplelist_get_line_count(void)
813 return simplelist_line_count;
815 /* add/edit a line in the list.
816 if line_number > number of lines shown it adds the line, else it edits the line */
817 void simplelist_addline(int line_number, const char *fmt, ...)
819 va_list ap;
821 if (line_number > simplelist_line_count)
823 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
824 line_number = simplelist_line_count++;
825 else
826 return;
828 va_start(ap, fmt);
829 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
830 va_end(ap);
833 static const char* simplelist_static_getname(int item,
834 void * data,
835 char *buffer,
836 size_t buffer_len)
838 (void)data; (void)buffer; (void)buffer_len;
839 return simplelist_text[item];
842 bool simplelist_show_list(struct simplelist_info *info)
844 struct gui_synclist lists;
845 int action, old_line_count = simplelist_line_count;
846 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
847 const char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
848 int wrap = LIST_WRAP_UNLESS_HELD;
849 if (info->get_name)
850 getname = info->get_name;
851 else
852 getname = simplelist_static_getname;
853 gui_synclist_init(&lists, getname, info->callback_data,
854 info->scroll_all, info->selection_size, NULL);
856 if (info->title)
857 gui_synclist_set_title(&lists, info->title, NOICON);
858 if (info->get_icon)
859 gui_synclist_set_icon_callback(&lists, info->get_icon);
860 if (info->get_talk)
861 gui_synclist_set_voice_callback(&lists, info->get_talk);
863 if (info->hide_selection)
865 gui_synclist_hide_selection_marker(&lists, true);
866 wrap = LIST_WRAP_OFF;
869 if (info->action_callback)
870 info->action_callback(ACTION_REDRAW, &lists);
872 if (info->get_name == NULL)
873 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
874 else
875 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
877 gui_synclist_select_item(&lists, info->selection);
879 gui_synclist_draw(&lists);
880 gui_synclist_speak_item(&lists);
882 while(1)
884 list_do_action(CONTEXT_STD, info->timeout,
885 &lists, &action, wrap);
887 /* We must yield in this case or no other thread can run */
888 if (info->timeout == TIMEOUT_NOBLOCK)
889 yield();
891 if (info->action_callback)
893 bool stdok = action==ACTION_STD_OK;
894 action = info->action_callback(action, &lists);
895 if (stdok && action == ACTION_STD_CANCEL)
897 /* callback asked us to exit */
898 info->selection = gui_synclist_get_sel_pos(&lists);
899 break;
902 if (info->get_name == NULL)
903 gui_synclist_set_nb_items(&lists,
904 simplelist_line_count*info->selection_size);
906 if (action == ACTION_STD_CANCEL)
908 info->selection = -1;
909 break;
911 else if ((action == ACTION_REDRAW) ||
912 (old_line_count != simplelist_line_count))
914 if (info->get_name == NULL)
916 gui_synclist_set_nb_items(&lists,
917 simplelist_line_count*info->selection_size);
919 gui_synclist_draw(&lists);
920 old_line_count = simplelist_line_count;
922 else if(default_event_handler(action) == SYS_USB_CONNECTED)
923 return true;
925 talk_shutup();
926 viewportmanager_set_statusbar(oldbars);
927 return false;
930 void simplelist_info_init(struct simplelist_info *info, char* title,
931 int count, void* data)
933 info->title = title;
934 info->count = count;
935 info->selection_size = 1;
936 info->hide_selection = false;
937 info->scroll_all = false;
938 info->timeout = HZ/10;
939 info->selection = 0;
940 info->action_callback = NULL;
941 info->get_icon = NULL;
942 info->get_name = NULL;
943 info->get_talk = NULL;
944 info->callback_data = data;