Remove some some code in favor of a single parse_list() call.
[kugel-rb.git] / apps / gui / list.c
blob4b5f32794194b029ceef387d2305a9b1c80af2de
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
100 list_need_reinit = false;
102 #else
103 #define list_init_viewports(a)
104 #endif
106 #ifdef HAVE_LCD_BITMAP
107 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
109 return list->title != NULL &&
110 viewport_get_nb_lines(list->parent[screen])>2;
112 #else
113 static struct viewport parent[NB_SCREENS] =
115 [SCREEN_MAIN] =
117 .x = 0,
118 .y = 0,
119 .width = LCD_WIDTH,
120 .height = LCD_HEIGHT
123 #define list_display_title(l, i) false
124 #endif
127 * Initializes a scrolling list
128 * - gui_list : the list structure to initialize
129 * - callback_get_item_name : pointer to a function that associates a label
130 * to a given item number
131 * - data : extra data passed to the list callback
132 * - scroll_all :
133 * - selected_size :
134 * - parent : the parent viewports to use. NULL means the full screen minus
135 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
137 void gui_synclist_init(struct gui_synclist * gui_list,
138 list_get_name callback_get_item_name,
139 void * data,
140 bool scroll_all,
141 int selected_size, struct viewport list_parent[NB_SCREENS]
144 int i;
145 gui_list->callback_get_item_icon = NULL;
146 gui_list->callback_get_item_name = callback_get_item_name;
147 gui_list->callback_speak_item = NULL;
148 gui_list->nb_items = 0;
149 gui_list->selected_item = 0;
150 FOR_NB_SCREENS(i)
152 gui_list->start_item[i] = 0;
153 gui_list->last_displayed_start_item[i] = -1 ;
154 #ifdef HAVE_LCD_BITMAP
155 gui_list->offset_position[i] = 0;
156 #endif
157 if (list_parent)
158 gui_list->parent[i] = &list_parent[i];
159 else
160 gui_list->parent[i] = &parent[i];
162 list_init_viewports(gui_list);
163 gui_list->limit_scroll = false;
164 gui_list->data=data;
165 gui_list->scroll_all=scroll_all;
166 gui_list->selected_size=selected_size;
167 gui_list->title = NULL;
168 gui_list->title_width = 0;
169 gui_list->title_icon = Icon_NOICON;
171 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
172 gui_list->show_selection_marker = true;
173 gui_list->last_displayed_selected_item = -1 ;
175 #ifdef HAVE_LCD_COLOR
176 gui_list->title_color = -1;
177 gui_list->callback_get_item_color = NULL;
178 #endif
181 /* this toggles the selection bar or cursor */
182 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
184 lists->show_selection_marker = !hide;
188 #ifdef HAVE_LCD_BITMAP
189 int gui_list_get_item_offset(struct gui_synclist * gui_list,
190 int item_width,
191 int text_pos,
192 struct screen * display,
193 struct viewport *vp)
195 int item_offset;
197 if (offset_out_of_view)
199 item_offset = gui_list->offset_position[display->screen_type];
201 else
203 /* if text is smaller then view */
204 if (item_width <= vp->width - text_pos)
206 item_offset = 0;
208 else
210 /* if text got out of view */
211 if (gui_list->offset_position[display->screen_type] >
212 item_width - (vp->width - text_pos))
213 item_offset = item_width - (vp->width - text_pos);
214 else
215 item_offset = gui_list->offset_position[display->screen_type];
219 return item_offset;
221 #endif
223 * Force a full screen update.
226 void gui_synclist_draw(struct gui_synclist *gui_list)
228 int i;
229 #ifdef HAVE_LCD_BITMAP
230 if (list_need_reinit)
232 list_init_viewports(gui_list);
233 gui_synclist_select_item(gui_list, gui_list->selected_item);
235 #endif
236 FOR_NB_SCREENS(i)
238 list_draw(&screens[i], gui_list);
242 /* sets up the list so the selection is shown correctly on the screen */
243 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
244 enum screen_type screen)
246 int nb_lines;
247 int difference = gui_list->selected_item - gui_list->start_item[screen];
248 struct viewport vp = *gui_list->parent[screen];
249 #ifdef HAVE_LCD_BITMAP
250 if (list_display_title(gui_list, screen))
251 vp.height -= font_get(gui_list->parent[screen]->font)->height;
252 #endif
253 nb_lines = viewport_get_nb_lines(&vp);
255 /* edge case,, selected last item */
256 if (gui_list->selected_item == gui_list->nb_items -1)
258 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
260 /* selected first item */
261 else if (gui_list->selected_item == 0)
263 gui_list->start_item[screen] = 0;
265 else if (difference < SCROLL_LIMIT) /* list moved up */
267 if (global_settings.scroll_paginated)
269 if (gui_list->start_item[screen] > gui_list->selected_item)
271 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)
272 *nb_lines;
274 if (gui_list->nb_items <= nb_lines)
275 gui_list->start_item[screen] = 0;
277 else
279 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT + 1;
280 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
281 gui_list->start_item[screen] = MAX(0, temp);
284 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
286 int bottom = gui_list->nb_items - nb_lines;
287 /* always move the screen if selection isnt "visible" */
288 if (gui_list->show_selection_marker == false)
290 if (bottom < 0)
291 bottom = 0;
292 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
293 2*gui_list->selected_size);
295 else if (global_settings.scroll_paginated)
297 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
298 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
300 else
302 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
303 int temp = MAX(0, top_of_screen);
304 gui_list->start_item[screen] = MIN(bottom, temp);
309 * Selects an item in the list
310 * - gui_list : the list structure
311 * - item_number : the number of the item which will be selected
313 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
315 int i;
316 if( item_number > gui_list->nb_items-1 || item_number < 0 )
317 return;
318 gui_list->selected_item = item_number;
319 FOR_NB_SCREENS(i)
320 gui_list_put_selection_on_screen(gui_list, i);
323 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
324 int offset)
326 int new_selection;
327 if (gui_list->selected_size > 1)
329 offset *= gui_list->selected_size;
330 /* always select the first item of multi-line lists */
331 offset -= offset%gui_list->selected_size;
333 new_selection = gui_list->selected_item + offset;
334 if (new_selection >= gui_list->nb_items)
336 gui_list->selected_item = gui_list->limit_scroll ?
337 gui_list->nb_items - gui_list->selected_size : 0;
339 else if (new_selection < 0)
341 gui_list->selected_item = gui_list->limit_scroll ?
342 0 : gui_list->nb_items - gui_list->selected_size;
344 else if (gui_list->show_selection_marker == false)
346 int i, nb_lines, screen_top;
347 FOR_NB_SCREENS(i)
349 struct viewport vp = *gui_list->parent[i];
350 #ifdef HAVE_LCD_BITMAP
351 if (list_display_title(gui_list, i))
352 vp.height -= font_get(gui_list->parent[i]->font)->height;
353 #endif
354 nb_lines = viewport_get_nb_lines(&vp);
355 if (offset > 0)
357 screen_top = gui_list->nb_items-nb_lines;
358 if (screen_top < 0)
359 screen_top = 0;
360 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
361 gui_list->selected_size);
362 gui_list->selected_item = gui_list->start_item[i];
364 else
366 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
367 gui_list->selected_size);
368 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
371 return;
373 else gui_list->selected_item += offset;
374 gui_synclist_select_item(gui_list, gui_list->selected_item);
378 * Adds an item to the list (the callback will be asked for one more item)
379 * - gui_list : the list structure
381 void gui_synclist_add_item(struct gui_synclist * gui_list)
383 gui_list->nb_items++;
384 /* if only one item in the list, select it */
385 if(gui_list->nb_items == 1)
386 gui_list->selected_item = 0;
390 * Removes an item to the list (the callback will be asked for one less item)
391 * - gui_list : the list structure
393 void gui_synclist_del_item(struct gui_synclist * gui_list)
395 if(gui_list->nb_items > 0)
397 if (gui_list->selected_item == gui_list->nb_items-1)
398 gui_list->selected_item--;
399 gui_list->nb_items--;
400 gui_synclist_select_item(gui_list, gui_list->selected_item);
404 #ifdef HAVE_LCD_BITMAP
405 void gui_list_screen_scroll_step(int ofs)
407 offset_step = ofs;
410 void gui_list_screen_scroll_out_of_view(bool enable)
412 if (enable)
413 offset_out_of_view = true;
414 else
415 offset_out_of_view = false;
417 #endif /* HAVE_LCD_BITMAP */
420 * Set the title and title icon of the list. Setting title to NULL disables
421 * both the title and icon. Use NOICON if there is no icon.
423 void gui_synclist_set_title(struct gui_synclist * gui_list,
424 char * title, enum themable_icons icon)
426 gui_list->title = title;
427 gui_list->title_icon = icon;
428 if (title) {
429 #ifdef HAVE_LCD_BITMAP
430 int i;
431 FOR_NB_SCREENS(i)
432 screens[i].getstringsize(title, &gui_list->title_width, NULL);
433 #else
434 gui_list->title_width = strlen(title);
435 #endif
436 } else {
437 gui_list->title_width = 0;
442 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
444 #ifdef HAVE_LCD_BITMAP
445 int i;
446 #endif
447 lists->nb_items = nb_items;
448 #ifdef HAVE_LCD_BITMAP
449 FOR_NB_SCREENS(i)
451 lists->offset_position[i] = 0;
453 #endif
455 int gui_synclist_get_nb_items(struct gui_synclist * lists)
457 return lists->nb_items;
459 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
461 return lists->selected_item;
463 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
464 list_get_icon icon_callback)
466 lists->callback_get_item_icon = icon_callback;
469 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
470 list_speak_item voice_callback)
472 lists->callback_speak_item = voice_callback;
475 #ifdef HAVE_LCD_COLOR
476 void gui_synclist_set_color_callback(struct gui_synclist * lists,
477 list_get_color color_callback)
479 lists->callback_get_item_color = color_callback;
481 #endif
483 static void gui_synclist_select_next_page(struct gui_synclist * lists,
484 enum screen_type screen)
486 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
487 #ifdef HAVE_LCD_BITMAP
488 if(list_display_title(lists, screen))
489 nb_lines--;
490 #endif
491 gui_list_select_at_offset(lists, nb_lines);
494 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
495 enum screen_type screen)
497 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
498 #ifdef HAVE_LCD_BITMAP
499 if(list_display_title(lists, screen))
500 nb_lines--;
501 #endif
502 gui_list_select_at_offset(lists, -nb_lines);
505 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
507 lists->limit_scroll = scroll;
510 #ifdef HAVE_LCD_BITMAP
512 * Makes all the item in the list scroll by one step to the right.
513 * Should stop increasing the value when reaching the widest item value
514 * in the list.
516 static void gui_synclist_scroll_right(struct gui_synclist * lists)
518 int i;
519 FOR_NB_SCREENS(i)
521 /* FIXME: This is a fake right boundry limiter. there should be some
522 * callback function to find the longest item on the list in pixels,
523 * to stop the list from scrolling past that point */
524 lists->offset_position[i]+=offset_step;
525 if (lists->offset_position[i] > 1000)
526 lists->offset_position[i] = 1000;
531 * Makes all the item in the list scroll by one step to the left.
532 * stops at starting position.
534 static void gui_synclist_scroll_left(struct gui_synclist * lists)
536 int i;
537 FOR_NB_SCREENS(i)
539 lists->offset_position[i]-=offset_step;
540 if (lists->offset_position[i] < 0)
541 lists->offset_position[i] = 0;
544 #endif /* HAVE_LCD_BITMAP */
546 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
548 list_speak_item *cb = lists->callback_speak_item;
549 if(cb && gui_synclist_get_nb_items(lists) != 0)
551 int sel = gui_synclist_get_sel_pos(lists);
552 talk_shutup();
553 /* If we got a repeating key action, or we have just very
554 recently started talking, then we want to stay silent for a
555 while until things settle. Likewise if we already had a
556 pending scheduled announcement not yet due: we need to
557 reschedule it. */
558 if(repeating
559 || (lists->scheduled_talk_tick
560 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
561 || (lists->last_talked_tick
562 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
564 lists->scheduled_talk_tick = current_tick +HZ/4;
565 return;
566 } else {
567 lists->scheduled_talk_tick = 0; /* work done */
568 cb(sel, lists->data);
569 lists->last_talked_tick = current_tick;
573 void gui_synclist_speak_item(struct gui_synclist * lists)
574 /* The list user should call this to speak the first item on entering
575 the list, and whenever the list is updated. */
577 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
578 talk_id(VOICE_EMPTY_LIST, true);
579 else _gui_synclist_speak_item(lists, false);
582 bool gui_synclist_do_button(struct gui_synclist * lists,
583 int *actionptr, enum list_wrap wrap)
585 int action = *actionptr;
586 #ifdef HAVE_LCD_BITMAP
587 static bool scrolling_left = false;
588 #endif
590 #ifdef HAVE_WHEEL_ACCELERATION
591 int next_item_modifier = button_apply_acceleration(get_action_data());
592 #else
593 static int next_item_modifier = 1;
594 static int last_accel_tick = 0;
596 if (global_settings.list_accel_start_delay)
598 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
599 int accel_wait = global_settings.list_accel_wait * HZ/2;
601 if (get_action_statuscode(NULL)&ACTION_REPEAT)
603 if (!last_accel_tick)
604 last_accel_tick = current_tick + start_delay;
605 else if (current_tick >=
606 last_accel_tick + accel_wait)
608 last_accel_tick = current_tick;
609 next_item_modifier++;
612 else if (last_accel_tick)
614 next_item_modifier = 1;
615 last_accel_tick = 0;
618 #endif
620 #if defined(HAVE_TOUCHSCREEN)
621 if (action == ACTION_TOUCHSCREEN)
622 action = *actionptr = gui_synclist_do_touchscreen(lists);
623 #endif
625 switch (wrap)
627 case LIST_WRAP_ON:
628 gui_synclist_limit_scroll(lists, false);
629 break;
630 case LIST_WRAP_OFF:
631 gui_synclist_limit_scroll(lists, true);
632 break;
633 case LIST_WRAP_UNLESS_HELD:
634 if (action == ACTION_STD_PREVREPEAT ||
635 action == ACTION_STD_NEXTREPEAT ||
636 action == ACTION_LISTTREE_PGUP ||
637 action == ACTION_LISTTREE_PGDOWN)
638 gui_synclist_limit_scroll(lists, true);
639 else gui_synclist_limit_scroll(lists, false);
640 break;
643 switch (action)
645 case ACTION_REDRAW:
646 gui_synclist_draw(lists);
647 return true;
649 #ifdef HAVE_VOLUME_IN_LIST
650 case ACTION_LIST_VOLUP:
651 global_settings.volume += 2;
652 /* up two because the falthrough brings it down one */
653 case ACTION_LIST_VOLDOWN:
654 global_settings.volume--;
655 setvol();
656 return true;
657 #endif
658 case ACTION_STD_PREV:
659 case ACTION_STD_PREVREPEAT:
660 gui_list_select_at_offset(lists, -next_item_modifier);
661 #ifndef HAVE_WHEEL_ACCELERATION
662 if (button_queue_count() < FRAMEDROP_TRIGGER)
663 #endif
664 gui_synclist_draw(lists);
665 _gui_synclist_speak_item(lists,
666 action == ACTION_STD_PREVREPEAT
667 || next_item_modifier >1);
668 yield();
669 *actionptr = ACTION_STD_PREV;
670 return true;
672 case ACTION_STD_NEXT:
673 case ACTION_STD_NEXTREPEAT:
674 gui_list_select_at_offset(lists, next_item_modifier);
675 #ifndef HAVE_WHEEL_ACCELERATION
676 if (button_queue_count() < FRAMEDROP_TRIGGER)
677 #endif
678 gui_synclist_draw(lists);
679 _gui_synclist_speak_item(lists,
680 action == ACTION_STD_NEXTREPEAT
681 || next_item_modifier >1);
682 yield();
683 *actionptr = ACTION_STD_NEXT;
684 return true;
686 #ifdef HAVE_LCD_BITMAP
687 case ACTION_TREE_PGRIGHT:
688 gui_synclist_scroll_right(lists);
689 gui_synclist_draw(lists);
690 return true;
691 case ACTION_TREE_ROOT_INIT:
692 /* After this button press ACTION_TREE_PGLEFT is allowed
693 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
694 keymaps as a repeated button press (the same as the repeated
695 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
696 button press */
697 if (lists->offset_position[0] == 0)
699 scrolling_left = false;
700 *actionptr = ACTION_STD_CANCEL;
701 return true;
703 *actionptr = ACTION_TREE_PGLEFT;
704 case ACTION_TREE_PGLEFT:
705 if(!scrolling_left && (lists->offset_position[0] == 0))
707 *actionptr = ACTION_STD_CANCEL;
708 return false;
710 gui_synclist_scroll_left(lists);
711 gui_synclist_draw(lists);
712 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
713 skipping to root */
714 return true;
715 #endif
716 /* for pgup / pgdown, we are obliged to have a different behaviour depending
717 * on the screen for which the user pressed the key since for example, remote
718 * and main screen doesn't have the same number of lines */
719 case ACTION_LISTTREE_PGUP:
721 int screen =
722 #ifdef HAVE_REMOTE_LCD
723 get_action_statuscode(NULL)&ACTION_REMOTE ?
724 SCREEN_REMOTE :
725 #endif
726 SCREEN_MAIN;
727 gui_synclist_select_previous_page(lists, screen);
728 gui_synclist_draw(lists);
729 _gui_synclist_speak_item(lists, false);
730 yield();
731 *actionptr = ACTION_STD_NEXT;
733 return true;
735 case ACTION_LISTTREE_PGDOWN:
737 int screen =
738 #ifdef HAVE_REMOTE_LCD
739 get_action_statuscode(NULL)&ACTION_REMOTE ?
740 SCREEN_REMOTE :
741 #endif
742 SCREEN_MAIN;
743 gui_synclist_select_next_page(lists, screen);
744 gui_synclist_draw(lists);
745 _gui_synclist_speak_item(lists, false);
746 yield();
747 *actionptr = ACTION_STD_PREV;
749 return true;
751 if(lists->scheduled_talk_tick
752 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
753 /* scheduled postponed item announcement is due */
754 _gui_synclist_speak_item(lists, false);
755 return false;
758 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
759 /* Returns the lowest of timeout or the delay until a postponed
760 scheduled announcement is due (if any). */
762 if(lists->scheduled_talk_tick)
764 long delay = lists->scheduled_talk_tick -current_tick +1;
765 /* +1 because the trigger condition uses TIME_AFTER(), which
766 is implemented as strictly greater than. */
767 if(delay < 0)
768 delay = 0;
769 if(timeout > delay || timeout == TIMEOUT_BLOCK)
770 timeout = delay;
772 return timeout;
775 bool list_do_action(int context, int timeout,
776 struct gui_synclist *lists, int *action,
777 enum list_wrap wrap)
778 /* Combines the get_action() (with possibly overridden timeout) and
779 gui_synclist_do_button() calls. Returns the list action from
780 do_button, and places the action from get_action in *action. */
782 timeout = list_do_action_timeout(lists, timeout);
783 *action = get_action(context, timeout);
784 return gui_synclist_do_button(lists, action, wrap);
787 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
788 enum screen_type screen, int item)
790 struct viewport vp = *lists->parent[screen];
791 #ifdef HAVE_LCD_BITMAP
792 if (list_display_title(lists, screen))
793 vp.height -= font_get(lists->parent[screen]->font)->height;
794 #endif
795 return item <= (lists->start_item[screen] + viewport_get_nb_lines(&vp));
798 /* Simple use list implementation */
799 static int simplelist_line_count = 0;
800 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
801 /* set the amount of lines shown in the list */
802 void simplelist_set_line_count(int lines)
804 if (lines < 0)
805 simplelist_line_count = 0;
806 else if (lines >= SIMPLELIST_MAX_LINES)
807 simplelist_line_count = SIMPLELIST_MAX_LINES;
808 else
809 simplelist_line_count = lines;
811 /* get the current amount of lines shown */
812 int simplelist_get_line_count(void)
814 return simplelist_line_count;
816 /* add/edit a line in the list.
817 if line_number > number of lines shown it adds the line, else it edits the line */
818 void simplelist_addline(int line_number, const char *fmt, ...)
820 va_list ap;
822 if (line_number > simplelist_line_count)
824 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
825 line_number = simplelist_line_count++;
826 else
827 return;
829 va_start(ap, fmt);
830 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
831 va_end(ap);
834 static const char* simplelist_static_getname(int item,
835 void * data,
836 char *buffer,
837 size_t buffer_len)
839 (void)data; (void)buffer; (void)buffer_len;
840 return simplelist_text[item];
843 bool simplelist_show_list(struct simplelist_info *info)
845 struct gui_synclist lists;
846 int action, old_line_count = simplelist_line_count;
847 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
848 const char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
849 int wrap = LIST_WRAP_UNLESS_HELD;
850 if (info->get_name)
851 getname = info->get_name;
852 else
853 getname = simplelist_static_getname;
854 gui_synclist_init(&lists, getname, info->callback_data,
855 info->scroll_all, info->selection_size, NULL);
857 if (info->title)
858 gui_synclist_set_title(&lists, info->title, NOICON);
859 if (info->get_icon)
860 gui_synclist_set_icon_callback(&lists, info->get_icon);
861 if (info->get_talk)
862 gui_synclist_set_voice_callback(&lists, info->get_talk);
864 if (info->hide_selection)
866 gui_synclist_hide_selection_marker(&lists, true);
867 wrap = LIST_WRAP_OFF;
870 if (info->action_callback)
871 info->action_callback(ACTION_REDRAW, &lists);
873 if (info->get_name == NULL)
874 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
875 else
876 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
878 gui_synclist_select_item(&lists, info->selection);
880 gui_synclist_draw(&lists);
881 gui_synclist_speak_item(&lists);
883 while(1)
885 list_do_action(CONTEXT_STD, info->timeout,
886 &lists, &action, wrap);
888 /* We must yield in this case or no other thread can run */
889 if (info->timeout == TIMEOUT_NOBLOCK)
890 yield();
892 if (info->action_callback)
894 bool stdok = action==ACTION_STD_OK;
895 action = info->action_callback(action, &lists);
896 if (stdok && action == ACTION_STD_CANCEL)
898 /* callback asked us to exit */
899 info->selection = gui_synclist_get_sel_pos(&lists);
900 break;
903 if (info->get_name == NULL)
904 gui_synclist_set_nb_items(&lists,
905 simplelist_line_count*info->selection_size);
907 if (action == ACTION_STD_CANCEL)
909 info->selection = -1;
910 break;
912 else if ((action == ACTION_REDRAW) ||
913 (old_line_count != simplelist_line_count))
915 if (info->get_name == NULL)
917 gui_synclist_set_nb_items(&lists,
918 simplelist_line_count*info->selection_size);
920 gui_synclist_draw(&lists);
921 old_line_count = simplelist_line_count;
923 else if(default_event_handler(action) == SYS_USB_CONNECTED)
924 return true;
926 talk_shutup();
927 viewportmanager_set_statusbar(oldbars);
928 return false;
931 void simplelist_info_init(struct simplelist_info *info, char* title,
932 int count, void* data)
934 info->title = title;
935 info->count = count;
936 info->selection_size = 1;
937 info->hide_selection = false;
938 info->scroll_all = false;
939 info->timeout = HZ/10;
940 info->selection = 0;
941 info->action_callback = NULL;
942 info->get_icon = NULL;
943 info->get_name = NULL;
944 info->get_talk = NULL;
945 info->callback_data = data;