Fix ret for buttonbar targets.
[kugel-rb/myfork.git] / apps / gui / list.c
blob332459c57f98f11edf80420e49107c6ed9aa8386
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"
42 #ifdef HAVE_LCD_CHARCELLS
43 #define SCROLL_LIMIT 1
44 #else
45 #define SCROLL_LIMIT (nb_lines<3?1:2)
46 #endif
48 /* The minimum number of pending button events in queue before starting
49 * to limit list drawing interval.
51 #define FRAMEDROP_TRIGGER 6
53 #ifdef HAVE_LCD_BITMAP
54 static int offset_step = 16; /* pixels per screen scroll step */
55 /* should lines scroll out of the screen */
56 static bool offset_out_of_view = false;
57 #endif
59 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
60 int offset);
61 void list_draw(struct screen *display, struct gui_synclist *list);
63 #ifdef HAVE_LCD_BITMAP
64 static void list_init_viewports(struct gui_synclist *list)
66 struct viewport* vp;
67 int i;
68 bool parent_used = (*list->parent != NULL);
69 if (!parent_used)
71 vp = viewport_get_current_vp();
72 FOR_NB_SCREENS(i)
73 list->parent[i] = &vp[i];
75 #ifdef HAVE_BUTTONBAR
76 if (list && !parent_used && global_settings.buttonbar)
77 list->parent[0]->height -= BUTTONBAR_HEIGHT;
78 #endif
80 #else
81 #define list_init_viewports(a)
82 #endif
84 #ifdef HAVE_LCD_BITMAP
85 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
87 return list->title != NULL &&
88 viewport_get_nb_lines(list->parent[screen])>2;
90 #else
91 static struct viewport parent[NB_SCREENS] =
93 [SCREEN_MAIN] =
95 .x = 0,
96 .y = 0,
97 .width = LCD_WIDTH,
98 .height = LCD_HEIGHT
101 #define list_display_title(l, i) false
102 #endif
105 * Initializes a scrolling list
106 * - gui_list : the list structure to initialize
107 * - callback_get_item_name : pointer to a function that associates a label
108 * to a given item number
109 * - data : extra data passed to the list callback
110 * - scroll_all :
111 * - selected_size :
112 * - parent : the parent viewports to use. NULL means the full screen minus
113 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
115 void gui_synclist_init(struct gui_synclist * gui_list,
116 list_get_name callback_get_item_name,
117 void * data,
118 bool scroll_all,
119 int selected_size, struct viewport list_parent[NB_SCREENS]
122 int i;
123 gui_list->callback_get_item_icon = NULL;
124 gui_list->callback_get_item_name = callback_get_item_name;
125 gui_list->callback_speak_item = NULL;
126 gui_list->nb_items = 0;
127 gui_list->selected_item = 0;
128 FOR_NB_SCREENS(i)
130 gui_list->start_item[i] = 0;
131 gui_list->last_displayed_start_item[i] = -1 ;
132 #ifdef HAVE_LCD_BITMAP
133 gui_list->offset_position[i] = 0;
134 #endif
135 if (list_parent)
136 gui_list->parent[i] = &list_parent[i];
137 else
138 gui_list->parent[i] =
139 #ifdef HAVE_LCD_BITMAP
140 NULL;
141 #else
142 &parent[i];
143 #endif
145 list_init_viewports(gui_list);
146 gui_list->limit_scroll = false;
147 gui_list->data=data;
148 gui_list->scroll_all=scroll_all;
149 gui_list->selected_size=selected_size;
150 gui_list->title = NULL;
151 gui_list->title_width = 0;
152 gui_list->title_icon = Icon_NOICON;
154 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
155 gui_list->show_selection_marker = true;
156 gui_list->last_displayed_selected_item = -1 ;
158 #ifdef HAVE_LCD_COLOR
159 gui_list->title_color = -1;
160 gui_list->callback_get_item_color = NULL;
161 #endif
164 /* this toggles the selection bar or cursor */
165 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
167 lists->show_selection_marker = !hide;
171 #ifdef HAVE_LCD_BITMAP
172 int gui_list_get_item_offset(struct gui_synclist * gui_list,
173 int item_width,
174 int text_pos,
175 struct screen * display,
176 struct viewport *vp)
178 int item_offset;
180 if (offset_out_of_view)
182 item_offset = gui_list->offset_position[display->screen_type];
184 else
186 /* if text is smaller then view */
187 if (item_width <= vp->width - text_pos)
189 item_offset = 0;
191 else
193 /* if text got out of view */
194 if (gui_list->offset_position[display->screen_type] >
195 item_width - (vp->width - text_pos))
196 item_offset = item_width - (vp->width - text_pos);
197 else
198 item_offset = gui_list->offset_position[display->screen_type];
202 return item_offset;
204 #endif
206 * Force a full screen update.
209 void gui_synclist_draw(struct gui_synclist *gui_list)
211 int i;
212 static struct gui_synclist *last_list = NULL;
213 static int last_count = -1;
214 #ifdef HAVE_BUTTONBAR
215 static bool last_buttonbar = false;
216 #endif
217 if (
218 #ifdef HAVE_BUTTONBAR
219 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
220 #endif
221 last_list != gui_list ||
222 gui_list->nb_items != last_count)
224 list_init_viewports(gui_list);
225 gui_synclist_select_item(gui_list, gui_list->selected_item);
227 #ifdef HAVE_BUTTONBAR
228 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
229 #endif
230 last_count = gui_list->nb_items;
231 last_list = gui_list;
232 FOR_NB_SCREENS(i)
234 list_draw(&screens[i], gui_list);
238 /* sets up the list so the selection is shown correctly on the screen */
239 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
240 enum screen_type screen)
242 int nb_lines;
243 int difference = gui_list->selected_item - gui_list->start_item[screen];
244 struct viewport vp = *gui_list->parent[screen];
245 #ifdef HAVE_LCD_BITMAP
246 if (list_display_title(gui_list, screen))
247 vp.height -= font_get(gui_list->parent[screen]->font)->height;
248 #endif
249 nb_lines = viewport_get_nb_lines(&vp);
251 /* edge case,, selected last item */
252 if (gui_list->selected_item == gui_list->nb_items -1)
254 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
256 /* selected first item */
257 else if (gui_list->selected_item == 0)
259 gui_list->start_item[screen] = 0;
261 else if (difference < SCROLL_LIMIT) /* list moved up */
263 if (global_settings.scroll_paginated)
265 if (gui_list->start_item[screen] > gui_list->selected_item)
267 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)
268 *nb_lines;
270 if (gui_list->nb_items <= nb_lines)
271 gui_list->start_item[screen] = 0;
273 else
275 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT + 1;
276 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
277 gui_list->start_item[screen] = MAX(0, temp);
280 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
282 int bottom = gui_list->nb_items - nb_lines;
283 /* always move the screen if selection isnt "visible" */
284 if (gui_list->show_selection_marker == false)
286 if (bottom < 0)
287 bottom = 0;
288 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
289 2*gui_list->selected_size);
291 else if (global_settings.scroll_paginated)
293 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
294 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
296 else
298 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
299 int temp = MAX(0, top_of_screen);
300 gui_list->start_item[screen] = MIN(bottom, temp);
305 * Selects an item in the list
306 * - gui_list : the list structure
307 * - item_number : the number of the item which will be selected
309 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
311 int i;
312 if( item_number > gui_list->nb_items-1 || item_number < 0 )
313 return;
314 gui_list->selected_item = item_number;
315 FOR_NB_SCREENS(i)
316 gui_list_put_selection_on_screen(gui_list, i);
319 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
320 int offset)
322 int new_selection;
323 if (gui_list->selected_size > 1)
325 offset *= gui_list->selected_size;
326 /* always select the first item of multi-line lists */
327 offset -= offset%gui_list->selected_size;
329 new_selection = gui_list->selected_item + offset;
330 if (new_selection >= gui_list->nb_items)
332 gui_list->selected_item = gui_list->limit_scroll ?
333 gui_list->nb_items - gui_list->selected_size : 0;
335 else if (new_selection < 0)
337 gui_list->selected_item = gui_list->limit_scroll ?
338 0 : gui_list->nb_items - gui_list->selected_size;
340 else if (gui_list->show_selection_marker == false)
342 int i, nb_lines, screen_top;
343 FOR_NB_SCREENS(i)
345 struct viewport vp = *gui_list->parent[i];
346 #ifdef HAVE_LCD_BITMAP
347 if (list_display_title(gui_list, i))
348 vp.height -= font_get(gui_list->parent[i]->font)->height;
349 #endif
350 nb_lines = viewport_get_nb_lines(&vp);
351 if (offset > 0)
353 screen_top = gui_list->nb_items-nb_lines;
354 if (screen_top < 0)
355 screen_top = 0;
356 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
357 gui_list->selected_size);
358 gui_list->selected_item = gui_list->start_item[i];
360 else
362 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
363 gui_list->selected_size);
364 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
367 return;
369 else gui_list->selected_item += offset;
370 gui_synclist_select_item(gui_list, gui_list->selected_item);
374 * Adds an item to the list (the callback will be asked for one more item)
375 * - gui_list : the list structure
377 void gui_synclist_add_item(struct gui_synclist * gui_list)
379 gui_list->nb_items++;
380 /* if only one item in the list, select it */
381 if(gui_list->nb_items == 1)
382 gui_list->selected_item = 0;
386 * Removes an item to the list (the callback will be asked for one less item)
387 * - gui_list : the list structure
389 void gui_synclist_del_item(struct gui_synclist * gui_list)
391 if(gui_list->nb_items > 0)
393 if (gui_list->selected_item == gui_list->nb_items-1)
394 gui_list->selected_item--;
395 gui_list->nb_items--;
396 gui_synclist_select_item(gui_list, gui_list->selected_item);
400 #ifdef HAVE_LCD_BITMAP
401 void gui_list_screen_scroll_step(int ofs)
403 offset_step = ofs;
406 void gui_list_screen_scroll_out_of_view(bool enable)
408 if (enable)
409 offset_out_of_view = true;
410 else
411 offset_out_of_view = false;
413 #endif /* HAVE_LCD_BITMAP */
416 * Set the title and title icon of the list. Setting title to NULL disables
417 * both the title and icon. Use NOICON if there is no icon.
419 void gui_synclist_set_title(struct gui_synclist * gui_list,
420 char * title, enum themable_icons icon)
422 gui_list->title = title;
423 gui_list->title_icon = icon;
424 if (title) {
425 #ifdef HAVE_LCD_BITMAP
426 int i;
427 FOR_NB_SCREENS(i)
428 screens[i].getstringsize(title, &gui_list->title_width, NULL);
429 #else
430 gui_list->title_width = strlen(title);
431 #endif
432 } else {
433 gui_list->title_width = 0;
438 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
440 #ifdef HAVE_LCD_BITMAP
441 int i;
442 #endif
443 lists->nb_items = nb_items;
444 #ifdef HAVE_LCD_BITMAP
445 FOR_NB_SCREENS(i)
447 lists->offset_position[i] = 0;
449 #endif
451 int gui_synclist_get_nb_items(struct gui_synclist * lists)
453 return lists->nb_items;
455 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
457 return lists->selected_item;
459 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
460 list_get_icon icon_callback)
462 lists->callback_get_item_icon = icon_callback;
465 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
466 list_speak_item voice_callback)
468 lists->callback_speak_item = voice_callback;
471 #ifdef HAVE_LCD_COLOR
472 void gui_synclist_set_color_callback(struct gui_synclist * lists,
473 list_get_color color_callback)
475 lists->callback_get_item_color = color_callback;
477 #endif
479 static void gui_synclist_select_next_page(struct gui_synclist * lists,
480 enum screen_type screen)
482 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
483 #ifdef HAVE_LCD_BITMAP
484 if(list_display_title(lists, screen))
485 nb_lines--;
486 #endif
487 gui_list_select_at_offset(lists, nb_lines);
490 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
491 enum screen_type screen)
493 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
494 #ifdef HAVE_LCD_BITMAP
495 if(list_display_title(lists, screen))
496 nb_lines--;
497 #endif
498 gui_list_select_at_offset(lists, -nb_lines);
501 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
503 lists->limit_scroll = scroll;
506 #ifdef HAVE_LCD_BITMAP
508 * Makes all the item in the list scroll by one step to the right.
509 * Should stop increasing the value when reaching the widest item value
510 * in the list.
512 static void gui_synclist_scroll_right(struct gui_synclist * lists)
514 int i;
515 FOR_NB_SCREENS(i)
517 /* FIXME: This is a fake right boundry limiter. there should be some
518 * callback function to find the longest item on the list in pixels,
519 * to stop the list from scrolling past that point */
520 lists->offset_position[i]+=offset_step;
521 if (lists->offset_position[i] > 1000)
522 lists->offset_position[i] = 1000;
527 * Makes all the item in the list scroll by one step to the left.
528 * stops at starting position.
530 static void gui_synclist_scroll_left(struct gui_synclist * lists)
532 int i;
533 FOR_NB_SCREENS(i)
535 lists->offset_position[i]-=offset_step;
536 if (lists->offset_position[i] < 0)
537 lists->offset_position[i] = 0;
540 #endif /* HAVE_LCD_BITMAP */
542 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
544 list_speak_item *cb = lists->callback_speak_item;
545 if(cb && gui_synclist_get_nb_items(lists) != 0)
547 int sel = gui_synclist_get_sel_pos(lists);
548 talk_shutup();
549 /* If we got a repeating key action, or we have just very
550 recently started talking, then we want to stay silent for a
551 while until things settle. Likewise if we already had a
552 pending scheduled announcement not yet due: we need to
553 reschedule it. */
554 if(repeating
555 || (lists->scheduled_talk_tick
556 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
557 || (lists->last_talked_tick
558 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
560 lists->scheduled_talk_tick = current_tick +HZ/4;
561 return;
562 } else {
563 lists->scheduled_talk_tick = 0; /* work done */
564 cb(sel, lists->data);
565 lists->last_talked_tick = current_tick;
569 void gui_synclist_speak_item(struct gui_synclist * lists)
570 /* The list user should call this to speak the first item on entering
571 the list, and whenever the list is updated. */
573 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
574 talk_id(VOICE_EMPTY_LIST, true);
575 else _gui_synclist_speak_item(lists, false);
578 bool gui_synclist_do_button(struct gui_synclist * lists,
579 int *actionptr, enum list_wrap wrap)
581 int action = *actionptr;
582 #ifdef HAVE_LCD_BITMAP
583 static bool scrolling_left = false;
584 #endif
586 #ifdef HAVE_WHEEL_ACCELERATION
587 int next_item_modifier = button_apply_acceleration(get_action_data());
588 #else
589 static int next_item_modifier = 1;
590 static int last_accel_tick = 0;
592 if (global_settings.list_accel_start_delay)
594 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
595 int accel_wait = global_settings.list_accel_wait * HZ/2;
597 if (get_action_statuscode(NULL)&ACTION_REPEAT)
599 if (!last_accel_tick)
600 last_accel_tick = current_tick + start_delay;
601 else if (current_tick >=
602 last_accel_tick + accel_wait)
604 last_accel_tick = current_tick;
605 next_item_modifier++;
608 else if (last_accel_tick)
610 next_item_modifier = 1;
611 last_accel_tick = 0;
614 #endif
616 #if defined(HAVE_TOUCHSCREEN)
617 if (action == ACTION_TOUCHSCREEN)
618 action = *actionptr = gui_synclist_do_touchscreen(lists);
619 #endif
621 switch (wrap)
623 case LIST_WRAP_ON:
624 gui_synclist_limit_scroll(lists, false);
625 break;
626 case LIST_WRAP_OFF:
627 gui_synclist_limit_scroll(lists, true);
628 break;
629 case LIST_WRAP_UNLESS_HELD:
630 if (action == ACTION_STD_PREVREPEAT ||
631 action == ACTION_STD_NEXTREPEAT ||
632 action == ACTION_LISTTREE_PGUP ||
633 action == ACTION_LISTTREE_PGDOWN)
634 gui_synclist_limit_scroll(lists, true);
635 else gui_synclist_limit_scroll(lists, false);
636 break;
639 switch (action)
641 case ACTION_REDRAW:
642 gui_synclist_draw(lists);
643 return true;
645 #ifdef HAVE_VOLUME_IN_LIST
646 case ACTION_LIST_VOLUP:
647 global_settings.volume += 2;
648 /* up two because the falthrough brings it down one */
649 case ACTION_LIST_VOLDOWN:
650 global_settings.volume--;
651 setvol();
652 return true;
653 #endif
654 case ACTION_STD_PREV:
655 case ACTION_STD_PREVREPEAT:
656 gui_list_select_at_offset(lists, -next_item_modifier);
657 #ifndef HAVE_WHEEL_ACCELERATION
658 if (button_queue_count() < FRAMEDROP_TRIGGER)
659 #endif
660 gui_synclist_draw(lists);
661 _gui_synclist_speak_item(lists,
662 action == ACTION_STD_PREVREPEAT
663 || next_item_modifier >1);
664 yield();
665 *actionptr = ACTION_STD_PREV;
666 return true;
668 case ACTION_STD_NEXT:
669 case ACTION_STD_NEXTREPEAT:
670 gui_list_select_at_offset(lists, next_item_modifier);
671 #ifndef HAVE_WHEEL_ACCELERATION
672 if (button_queue_count() < FRAMEDROP_TRIGGER)
673 #endif
674 gui_synclist_draw(lists);
675 _gui_synclist_speak_item(lists,
676 action == ACTION_STD_NEXTREPEAT
677 || next_item_modifier >1);
678 yield();
679 *actionptr = ACTION_STD_NEXT;
680 return true;
682 #ifdef HAVE_LCD_BITMAP
683 case ACTION_TREE_PGRIGHT:
684 gui_synclist_scroll_right(lists);
685 gui_synclist_draw(lists);
686 return true;
687 case ACTION_TREE_ROOT_INIT:
688 /* After this button press ACTION_TREE_PGLEFT is allowed
689 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
690 keymaps as a repeated button press (the same as the repeated
691 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
692 button press */
693 if (lists->offset_position[0] == 0)
695 scrolling_left = false;
696 *actionptr = ACTION_STD_CANCEL;
697 return true;
699 *actionptr = ACTION_TREE_PGLEFT;
700 case ACTION_TREE_PGLEFT:
701 if(!scrolling_left && (lists->offset_position[0] == 0))
703 *actionptr = ACTION_STD_CANCEL;
704 return false;
706 gui_synclist_scroll_left(lists);
707 gui_synclist_draw(lists);
708 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
709 skipping to root */
710 return true;
711 #endif
712 /* for pgup / pgdown, we are obliged to have a different behaviour depending
713 * on the screen for which the user pressed the key since for example, remote
714 * and main screen doesn't have the same number of lines */
715 case ACTION_LISTTREE_PGUP:
717 int screen =
718 #ifdef HAVE_REMOTE_LCD
719 get_action_statuscode(NULL)&ACTION_REMOTE ?
720 SCREEN_REMOTE :
721 #endif
722 SCREEN_MAIN;
723 gui_synclist_select_previous_page(lists, screen);
724 gui_synclist_draw(lists);
725 _gui_synclist_speak_item(lists, false);
726 yield();
727 *actionptr = ACTION_STD_NEXT;
729 return true;
731 case ACTION_LISTTREE_PGDOWN:
733 int screen =
734 #ifdef HAVE_REMOTE_LCD
735 get_action_statuscode(NULL)&ACTION_REMOTE ?
736 SCREEN_REMOTE :
737 #endif
738 SCREEN_MAIN;
739 gui_synclist_select_next_page(lists, screen);
740 gui_synclist_draw(lists);
741 _gui_synclist_speak_item(lists, false);
742 yield();
743 *actionptr = ACTION_STD_PREV;
745 return true;
747 if(lists->scheduled_talk_tick
748 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
749 /* scheduled postponed item announcement is due */
750 _gui_synclist_speak_item(lists, false);
751 return false;
754 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
755 /* Returns the lowest of timeout or the delay until a postponed
756 scheduled announcement is due (if any). */
758 if(lists->scheduled_talk_tick)
760 long delay = lists->scheduled_talk_tick -current_tick +1;
761 /* +1 because the trigger condition uses TIME_AFTER(), which
762 is implemented as strictly greater than. */
763 if(delay < 0)
764 delay = 0;
765 if(timeout > delay || timeout == TIMEOUT_BLOCK)
766 timeout = delay;
768 return timeout;
771 bool list_do_action(int context, int timeout,
772 struct gui_synclist *lists, int *action,
773 enum list_wrap wrap)
774 /* Combines the get_action() (with possibly overridden timeout) and
775 gui_synclist_do_button() calls. Returns the list action from
776 do_button, and places the action from get_action in *action. */
778 timeout = list_do_action_timeout(lists, timeout);
779 *action = get_action(context, timeout);
780 return gui_synclist_do_button(lists, action, wrap);
783 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
784 enum screen_type screen, int item)
786 struct viewport vp = *lists->parent[screen];
787 #ifdef HAVE_LCD_BITMAP
788 if (list_display_title(lists, screen))
789 vp.height -= font_get(lists->parent[screen]->font)->height;
790 #endif
791 return item <= (lists->start_item[screen] + viewport_get_nb_lines(&vp));
794 /* Simple use list implementation */
795 static int simplelist_line_count = 0;
796 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
797 /* set the amount of lines shown in the list */
798 void simplelist_set_line_count(int lines)
800 if (lines < 0)
801 simplelist_line_count = 0;
802 else if (lines >= SIMPLELIST_MAX_LINES)
803 simplelist_line_count = SIMPLELIST_MAX_LINES;
804 else
805 simplelist_line_count = lines;
807 /* get the current amount of lines shown */
808 int simplelist_get_line_count(void)
810 return simplelist_line_count;
812 /* add/edit a line in the list.
813 if line_number > number of lines shown it adds the line, else it edits the line */
814 void simplelist_addline(int line_number, const char *fmt, ...)
816 va_list ap;
818 if (line_number > simplelist_line_count)
820 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
821 line_number = simplelist_line_count++;
822 else
823 return;
825 va_start(ap, fmt);
826 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
827 va_end(ap);
830 static char* simplelist_static_getname(int item,
831 void * data,
832 char *buffer,
833 size_t buffer_len)
835 (void)data; (void)buffer; (void)buffer_len;
836 return simplelist_text[item];
839 bool simplelist_show_list(struct simplelist_info *info)
841 struct gui_synclist lists;
842 int action, old_line_count = simplelist_line_count;
843 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
844 char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
845 int wrap = LIST_WRAP_UNLESS_HELD;
846 if (info->get_name)
847 getname = info->get_name;
848 else
849 getname = simplelist_static_getname;
850 gui_synclist_init(&lists, getname, info->callback_data,
851 info->scroll_all, info->selection_size, NULL);
853 if (info->title)
854 gui_synclist_set_title(&lists, info->title, NOICON);
855 if (info->get_icon)
856 gui_synclist_set_icon_callback(&lists, info->get_icon);
857 if (info->get_talk)
858 gui_synclist_set_voice_callback(&lists, info->get_talk);
860 if (info->hide_selection)
862 gui_synclist_hide_selection_marker(&lists, true);
863 wrap = LIST_WRAP_OFF;
866 if (info->action_callback)
867 info->action_callback(ACTION_REDRAW, &lists);
869 if (info->get_name == NULL)
870 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
871 else
872 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
874 gui_synclist_select_item(&lists, info->selection);
876 gui_synclist_draw(&lists);
877 gui_synclist_speak_item(&lists);
879 while(1)
881 list_do_action(CONTEXT_STD, info->timeout,
882 &lists, &action, wrap);
884 /* We must yield in this case or no other thread can run */
885 if (info->timeout == TIMEOUT_NOBLOCK)
886 yield();
888 if (info->action_callback)
890 bool stdok = action==ACTION_STD_OK;
891 action = info->action_callback(action, &lists);
892 if (stdok && action == ACTION_STD_CANCEL)
894 /* callback asked us to exit */
895 info->selection = gui_synclist_get_sel_pos(&lists);
896 break;
899 if (info->get_name == NULL)
900 gui_synclist_set_nb_items(&lists,
901 simplelist_line_count*info->selection_size);
903 if (action == ACTION_STD_CANCEL)
905 info->selection = -1;
906 break;
908 else if ((action == ACTION_REDRAW) ||
909 (old_line_count != simplelist_line_count))
911 if (info->get_name == NULL)
913 gui_synclist_set_nb_items(&lists,
914 simplelist_line_count*info->selection_size);
916 gui_synclist_draw(&lists);
917 old_line_count = simplelist_line_count;
919 else if(default_event_handler(action) == SYS_USB_CONNECTED)
920 return true;
922 talk_shutup();
923 viewportmanager_set_statusbar(oldbars);
924 return false;
927 void simplelist_info_init(struct simplelist_info *info, char* title,
928 int count, void* data)
930 info->title = title;
931 info->count = count;
932 info->selection_size = 1;
933 info->hide_selection = false;
934 info->scroll_all = false;
935 info->timeout = HZ/10;
936 info->selection = 0;
937 info->action_callback = NULL;
938 info->get_icon = NULL;
939 info->get_name = NULL;
940 info->get_talk = NULL;
941 info->callback_data = data;