Accept a quick patch from Alexander Levin to neaten up the #defines and comments...
[kugel-rb.git] / apps / gui / list.c
blob2923ec33c48b4c7dcb7f61d6c9d3f389dc2f5d33
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 "statusbar.h"
37 #include "lang.h"
38 #include "sound.h"
39 #include "misc.h"
40 #include "talk.h"
41 #include "viewport.h"
42 #include "list.h"
44 #ifdef HAVE_LCD_CHARCELLS
45 #define SCROLL_LIMIT 1
46 #else
47 #define SCROLL_LIMIT (nb_lines<3?1:2)
48 #endif
50 /* The minimum number of pending button events in queue before starting
51 * to limit list drawing interval.
53 #define FRAMEDROP_TRIGGER 6
55 #ifdef HAVE_LCD_BITMAP
56 static int offset_step = 16; /* pixels per screen scroll step */
57 /* should lines scroll out of the screen */
58 static bool offset_out_of_view = false;
59 #endif
60 static int force_list_reinit = false;
62 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
63 int offset);
64 void list_draw(struct screen *display, struct gui_synclist *list);
66 #ifdef HAVE_LCD_BITMAP
67 static struct viewport parent[NB_SCREENS];
68 void list_init_viewports(struct gui_synclist *list)
70 int i;
71 struct viewport *vp;
72 FOR_NB_SCREENS(i)
74 vp = &parent[i];
75 if (!list || list->parent[i] == vp)
76 viewport_set_defaults(vp, i);
78 #ifdef HAVE_BUTTONBAR
79 if (list && (list->parent[0] == &parent[0]) && global_settings.buttonbar)
80 list->parent[0]->height -= BUTTONBAR_HEIGHT;
81 #endif
82 force_list_reinit = false;
84 #else
85 static struct viewport parent[NB_SCREENS] =
87 [SCREEN_MAIN] =
89 .x = 0,
90 .y = 0,
91 .width = LCD_WIDTH,
92 .height = LCD_HEIGHT
95 void list_init_viewports(struct gui_synclist *list)
97 (void)list;
98 force_list_reinit = false;
100 #endif
102 #ifdef HAVE_LCD_BITMAP
103 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
105 return list->title != NULL &&
106 viewport_get_nb_lines(list->parent[screen])>2;
108 #else
109 #define list_display_title(l, i) false
110 #endif
113 * Initializes a scrolling list
114 * - gui_list : the list structure to initialize
115 * - callback_get_item_name : pointer to a function that associates a label
116 * to a given item number
117 * - data : extra data passed to the list callback
118 * - scroll_all :
119 * - selected_size :
120 * - parent : the parent viewports to use. NULL means the full screen minus
121 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
123 void gui_synclist_init(struct gui_synclist * gui_list,
124 list_get_name callback_get_item_name,
125 void * data,
126 bool scroll_all,
127 int selected_size, struct viewport list_parent[NB_SCREENS]
130 int i;
131 gui_list->callback_get_item_icon = NULL;
132 gui_list->callback_get_item_name = callback_get_item_name;
133 gui_list->callback_speak_item = NULL;
134 gui_list->nb_items = 0;
135 gui_list->selected_item = 0;
136 FOR_NB_SCREENS(i)
138 gui_list->start_item[i] = 0;
139 gui_list->last_displayed_start_item[i] = -1 ;
140 #ifdef HAVE_LCD_BITMAP
141 gui_list->offset_position[i] = 0;
142 #endif
143 if (list_parent)
144 gui_list->parent[i] = &list_parent[i];
145 else
147 gui_list->parent[i] = &parent[i];
150 list_init_viewports(gui_list);
151 gui_list->limit_scroll = false;
152 gui_list->data=data;
153 gui_list->scroll_all=scroll_all;
154 gui_list->selected_size=selected_size;
155 gui_list->title = NULL;
156 gui_list->title_width = 0;
157 gui_list->title_icon = Icon_NOICON;
159 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
160 gui_list->show_selection_marker = true;
161 gui_list->last_displayed_selected_item = -1 ;
163 #ifdef HAVE_LCD_COLOR
164 gui_list->title_color = -1;
165 gui_list->callback_get_item_color = NULL;
166 #endif
167 force_list_reinit = true;
170 /* this toggles the selection bar or cursor */
171 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
173 lists->show_selection_marker = !hide;
177 #ifdef HAVE_LCD_BITMAP
178 int gui_list_get_item_offset(struct gui_synclist * gui_list,
179 int item_width,
180 int text_pos,
181 struct screen * display,
182 struct viewport *vp)
184 int item_offset;
186 if (offset_out_of_view)
188 item_offset = gui_list->offset_position[display->screen_type];
190 else
192 /* if text is smaller then view */
193 if (item_width <= vp->width - text_pos)
195 item_offset = 0;
197 else
199 /* if text got out of view */
200 if (gui_list->offset_position[display->screen_type] >
201 item_width - (vp->width - text_pos))
202 item_offset = item_width - (vp->width - text_pos);
203 else
204 item_offset = gui_list->offset_position[display->screen_type];
208 return item_offset;
210 #endif
212 * Force a full screen update.
215 void gui_synclist_draw(struct gui_synclist *gui_list)
217 int i;
218 static struct gui_synclist *last_list = NULL;
219 static int last_count = -1;
220 #ifdef HAVE_BUTTONBAR
221 static bool last_buttonbar = false;
222 #endif
223 if (force_list_reinit ||
224 #ifdef HAVE_BUTTONBAR
225 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
226 #endif
227 last_list != gui_list ||
228 gui_list->nb_items != last_count)
230 list_init_viewports(gui_list);
231 gui_synclist_select_item(gui_list, gui_list->selected_item);
233 #ifdef HAVE_BUTTONBAR
234 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
235 #endif
236 last_count = gui_list->nb_items;
237 last_list = gui_list;
238 FOR_NB_SCREENS(i)
240 list_draw(&screens[i], gui_list);
244 /* sets up the list so the selection is shown correctly on the screen */
245 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
246 enum screen_type screen)
248 int nb_lines;
249 int difference = gui_list->selected_item - gui_list->start_item[screen];
250 struct viewport vp = *gui_list->parent[screen];
251 #ifdef HAVE_LCD_BITMAP
252 if (list_display_title(gui_list, screen))
253 vp.height -= font_get(gui_list->parent[screen]->font)->height;
254 #endif
255 nb_lines = viewport_get_nb_lines(&vp);
257 /* edge case,, selected last item */
258 if (gui_list->selected_item == gui_list->nb_items -1)
260 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
262 /* selected first item */
263 else if (gui_list->selected_item == 0)
265 gui_list->start_item[screen] = 0;
267 else if (difference < SCROLL_LIMIT) /* list moved up */
269 if (global_settings.scroll_paginated)
271 if (gui_list->start_item[screen] > gui_list->selected_item)
273 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)
274 *nb_lines;
276 if (gui_list->nb_items <= nb_lines)
277 gui_list->start_item[screen] = 0;
279 else
281 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT + 1;
282 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
283 gui_list->start_item[screen] = MAX(0, temp);
286 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
288 int bottom = gui_list->nb_items - nb_lines;
289 /* always move the screen if selection isnt "visible" */
290 if (gui_list->show_selection_marker == false)
292 if (bottom < 0)
293 bottom = 0;
294 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
295 2*gui_list->selected_size);
297 else if (global_settings.scroll_paginated)
299 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
300 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
302 else
304 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
305 int temp = MAX(0, top_of_screen);
306 gui_list->start_item[screen] = MIN(bottom, temp);
311 * Selects an item in the list
312 * - gui_list : the list structure
313 * - item_number : the number of the item which will be selected
315 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
317 int i;
318 if( item_number > gui_list->nb_items-1 || item_number < 0 )
319 return;
320 gui_list->selected_item = item_number;
321 FOR_NB_SCREENS(i)
322 gui_list_put_selection_on_screen(gui_list, i);
325 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
326 int offset)
328 int new_selection;
329 if (gui_list->selected_size > 1)
331 offset *= gui_list->selected_size;
332 /* always select the first item of multi-line lists */
333 offset -= offset%gui_list->selected_size;
335 new_selection = gui_list->selected_item + offset;
336 if (new_selection >= gui_list->nb_items)
338 gui_list->selected_item = gui_list->limit_scroll ?
339 gui_list->nb_items - gui_list->selected_size : 0;
341 else if (new_selection < 0)
343 gui_list->selected_item = gui_list->limit_scroll ?
344 0 : gui_list->nb_items - gui_list->selected_size;
346 else if (gui_list->show_selection_marker == false)
348 int i, nb_lines, screen_top;
349 FOR_NB_SCREENS(i)
351 struct viewport vp = *gui_list->parent[i];
352 #ifdef HAVE_LCD_BITMAP
353 if (list_display_title(gui_list, i))
354 vp.height -= font_get(gui_list->parent[i]->font)->height;
355 #endif
356 nb_lines = viewport_get_nb_lines(&vp);
357 if (offset > 0)
359 screen_top = gui_list->nb_items-nb_lines;
360 if (screen_top < 0)
361 screen_top = 0;
362 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
363 gui_list->selected_size);
364 gui_list->selected_item = gui_list->start_item[i];
366 else
368 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
369 gui_list->selected_size);
370 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
373 return;
375 else gui_list->selected_item += offset;
376 gui_synclist_select_item(gui_list, gui_list->selected_item);
380 * Adds an item to the list (the callback will be asked for one more item)
381 * - gui_list : the list structure
383 void gui_synclist_add_item(struct gui_synclist * gui_list)
385 gui_list->nb_items++;
386 /* if only one item in the list, select it */
387 if(gui_list->nb_items == 1)
388 gui_list->selected_item = 0;
392 * Removes an item to the list (the callback will be asked for one less item)
393 * - gui_list : the list structure
395 void gui_synclist_del_item(struct gui_synclist * gui_list)
397 if(gui_list->nb_items > 0)
399 if (gui_list->selected_item == gui_list->nb_items-1)
400 gui_list->selected_item--;
401 gui_list->nb_items--;
402 gui_synclist_select_item(gui_list, gui_list->selected_item);
406 #ifdef HAVE_LCD_BITMAP
407 void gui_list_screen_scroll_step(int ofs)
409 offset_step = ofs;
412 void gui_list_screen_scroll_out_of_view(bool enable)
414 if (enable)
415 offset_out_of_view = true;
416 else
417 offset_out_of_view = false;
419 #endif /* HAVE_LCD_BITMAP */
422 * Set the title and title icon of the list. Setting title to NULL disables
423 * both the title and icon. Use NOICON if there is no icon.
425 void gui_synclist_set_title(struct gui_synclist * gui_list,
426 char * title, enum themable_icons icon)
428 gui_list->title = title;
429 gui_list->title_icon = icon;
430 if (title) {
431 #ifdef HAVE_LCD_BITMAP
432 int i;
433 FOR_NB_SCREENS(i)
434 screens[i].getstringsize(title, &gui_list->title_width, NULL);
435 #else
436 gui_list->title_width = strlen(title);
437 #endif
438 } else {
439 gui_list->title_width = 0;
441 force_list_reinit = true;
445 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
447 #ifdef HAVE_LCD_BITMAP
448 int i;
449 #endif
450 lists->nb_items = nb_items;
451 #ifdef HAVE_LCD_BITMAP
452 FOR_NB_SCREENS(i)
454 lists->offset_position[i] = 0;
456 #endif
458 int gui_synclist_get_nb_items(struct gui_synclist * lists)
460 return lists->nb_items;
462 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
464 return lists->selected_item;
466 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
467 list_get_icon icon_callback)
469 lists->callback_get_item_icon = icon_callback;
472 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
473 list_speak_item voice_callback)
475 lists->callback_speak_item = voice_callback;
478 #ifdef HAVE_LCD_COLOR
479 void gui_synclist_set_color_callback(struct gui_synclist * lists,
480 list_get_color color_callback)
482 lists->callback_get_item_color = color_callback;
484 #endif
486 static void gui_synclist_select_next_page(struct gui_synclist * lists,
487 enum screen_type screen)
489 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
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 gui_list_select_at_offset(lists, -nb_lines);
500 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
502 lists->limit_scroll = scroll;
505 #ifdef HAVE_LCD_BITMAP
507 * Makes all the item in the list scroll by one step to the right.
508 * Should stop increasing the value when reaching the widest item value
509 * in the list.
511 static void gui_synclist_scroll_right(struct gui_synclist * lists)
513 int i;
514 FOR_NB_SCREENS(i)
516 /* FIXME: This is a fake right boundry limiter. there should be some
517 * callback function to find the longest item on the list in pixels,
518 * to stop the list from scrolling past that point */
519 lists->offset_position[i]+=offset_step;
520 if (lists->offset_position[i] > 1000)
521 lists->offset_position[i] = 1000;
526 * Makes all the item in the list scroll by one step to the left.
527 * stops at starting position.
529 static void gui_synclist_scroll_left(struct gui_synclist * lists)
531 int i;
532 FOR_NB_SCREENS(i)
534 lists->offset_position[i]-=offset_step;
535 if (lists->offset_position[i] < 0)
536 lists->offset_position[i] = 0;
539 #endif /* HAVE_LCD_BITMAP */
541 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
543 list_speak_item *cb = lists->callback_speak_item;
544 if(cb && gui_synclist_get_nb_items(lists) != 0)
546 int sel = gui_synclist_get_sel_pos(lists);
547 talk_shutup();
548 /* If we got a repeating key action, or we have just very
549 recently started talking, then we want to stay silent for a
550 while until things settle. Likewise if we already had a
551 pending scheduled announcement not yet due: we need to
552 reschedule it. */
553 if(repeating
554 || (lists->scheduled_talk_tick
555 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
556 || (lists->last_talked_tick
557 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
559 lists->scheduled_talk_tick = current_tick +HZ/4;
560 return;
561 } else {
562 lists->scheduled_talk_tick = 0; /* work done */
563 cb(sel, lists->data);
564 lists->last_talked_tick = current_tick;
568 void gui_synclist_speak_item(struct gui_synclist * lists)
569 /* The list user should call this to speak the first item on entering
570 the list, and whenever the list is updated. */
572 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
573 talk_id(VOICE_EMPTY_LIST, true);
574 else _gui_synclist_speak_item(lists, false);
577 extern intptr_t get_action_data(void);
578 #if defined(HAVE_TOUCHSCREEN)
579 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
580 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list);
581 #endif
583 bool gui_synclist_do_button(struct gui_synclist * lists,
584 int *actionptr, enum list_wrap wrap)
586 int action = *actionptr;
587 #ifdef HAVE_LCD_BITMAP
588 static bool scrolling_left = false;
589 #endif
591 #ifdef HAVE_SCROLLWHEEL
592 int next_item_modifier = button_apply_acceleration(get_action_data());
593 #else
594 static int next_item_modifier = 1;
595 static int last_accel_tick = 0;
597 if (global_settings.list_accel_start_delay)
599 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
600 int accel_wait = global_settings.list_accel_wait * HZ/2;
602 if (get_action_statuscode(NULL)&ACTION_REPEAT)
604 if (!last_accel_tick)
605 last_accel_tick = current_tick + start_delay;
606 else if (current_tick >=
607 last_accel_tick + accel_wait)
609 last_accel_tick = current_tick;
610 next_item_modifier++;
613 else if (last_accel_tick)
615 next_item_modifier = 1;
616 last_accel_tick = 0;
619 #endif
621 #if defined(HAVE_TOUCHSCREEN)
622 if (action == ACTION_TOUCHSCREEN)
623 action = *actionptr = gui_synclist_do_touchscreen(lists);
624 #endif
626 switch (wrap)
628 case LIST_WRAP_ON:
629 gui_synclist_limit_scroll(lists, false);
630 break;
631 case LIST_WRAP_OFF:
632 gui_synclist_limit_scroll(lists, true);
633 break;
634 case LIST_WRAP_UNLESS_HELD:
635 if (action == ACTION_STD_PREVREPEAT ||
636 action == ACTION_STD_NEXTREPEAT ||
637 action == ACTION_LISTTREE_PGUP ||
638 action == ACTION_LISTTREE_PGDOWN)
639 gui_synclist_limit_scroll(lists, true);
640 else gui_synclist_limit_scroll(lists, false);
641 break;
644 switch (action)
646 case ACTION_REDRAW:
647 gui_synclist_draw(lists);
648 return true;
650 #ifdef HAVE_VOLUME_IN_LIST
651 case ACTION_LIST_VOLUP:
652 global_settings.volume += 2;
653 /* up two because the falthrough brings it down one */
654 case ACTION_LIST_VOLDOWN:
655 global_settings.volume--;
656 setvol();
657 return true;
658 #endif
659 case ACTION_STD_PREV:
660 case ACTION_STD_PREVREPEAT:
661 gui_list_select_at_offset(lists, -next_item_modifier);
662 #ifndef HAVE_SCROLLWHEEL
663 if (button_queue_count() < FRAMEDROP_TRIGGER)
664 #endif
665 gui_synclist_draw(lists);
666 _gui_synclist_speak_item(lists,
667 action == ACTION_STD_PREVREPEAT
668 || next_item_modifier >1);
669 yield();
670 *actionptr = ACTION_STD_PREV;
671 return true;
673 case ACTION_STD_NEXT:
674 case ACTION_STD_NEXTREPEAT:
675 gui_list_select_at_offset(lists, next_item_modifier);
676 #ifndef HAVE_SCROLLWHEEL
677 if (button_queue_count() < FRAMEDROP_TRIGGER)
678 #endif
679 gui_synclist_draw(lists);
680 _gui_synclist_speak_item(lists,
681 action == ACTION_STD_NEXTREPEAT
682 || next_item_modifier >1);
683 yield();
684 *actionptr = ACTION_STD_NEXT;
685 return true;
687 #ifdef HAVE_LCD_BITMAP
688 case ACTION_TREE_PGRIGHT:
689 gui_synclist_scroll_right(lists);
690 gui_synclist_draw(lists);
691 return true;
692 case ACTION_TREE_ROOT_INIT:
693 /* After this button press ACTION_TREE_PGLEFT is allowed
694 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
695 keymaps as a repeated button press (the same as the repeated
696 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
697 button press */
698 if (lists->offset_position[0] == 0)
700 scrolling_left = false;
701 *actionptr = ACTION_STD_CANCEL;
702 return true;
704 *actionptr = ACTION_TREE_PGLEFT;
705 case ACTION_TREE_PGLEFT:
706 if(!scrolling_left && (lists->offset_position[0] == 0))
708 *actionptr = ACTION_STD_CANCEL;
709 return false;
711 gui_synclist_scroll_left(lists);
712 gui_synclist_draw(lists);
713 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
714 skipping to root */
715 return true;
716 #endif
717 /* for pgup / pgdown, we are obliged to have a different behaviour depending
718 * on the screen for which the user pressed the key since for example, remote
719 * and main screen doesn't have the same number of lines */
720 case ACTION_LISTTREE_PGUP:
722 int screen =
723 #ifdef HAVE_REMOTE_LCD
724 get_action_statuscode(NULL)&ACTION_REMOTE ?
725 SCREEN_REMOTE :
726 #endif
727 SCREEN_MAIN;
728 gui_synclist_select_previous_page(lists, screen);
729 gui_synclist_draw(lists);
730 _gui_synclist_speak_item(lists, false);
731 yield();
732 *actionptr = ACTION_STD_NEXT;
734 return true;
736 case ACTION_LISTTREE_PGDOWN:
738 int screen =
739 #ifdef HAVE_REMOTE_LCD
740 get_action_statuscode(NULL)&ACTION_REMOTE ?
741 SCREEN_REMOTE :
742 #endif
743 SCREEN_MAIN;
744 gui_synclist_select_next_page(lists, screen);
745 gui_synclist_draw(lists);
746 _gui_synclist_speak_item(lists, false);
747 yield();
748 *actionptr = ACTION_STD_PREV;
750 return true;
752 if(lists->scheduled_talk_tick
753 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
754 /* scheduled postponed item announcement is due */
755 _gui_synclist_speak_item(lists, false);
756 return false;
759 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
760 /* Returns the lowest of timeout or the delay until a postponed
761 scheduled announcement is due (if any). */
763 if(lists->scheduled_talk_tick)
765 long delay = lists->scheduled_talk_tick -current_tick +1;
766 /* +1 because the trigger condition uses TIME_AFTER(), which
767 is implemented as strictly greater than. */
768 if(delay < 0)
769 delay = 0;
770 if(timeout > delay || timeout == TIMEOUT_BLOCK)
771 timeout = delay;
773 return timeout;
776 bool list_do_action(int context, int timeout,
777 struct gui_synclist *lists, int *action,
778 enum list_wrap wrap)
779 /* Combines the get_action() (with possibly overridden timeout) and
780 gui_synclist_do_button() calls. Returns the list action from
781 do_button, and places the action from get_action in *action. */
783 timeout = list_do_action_timeout(lists, timeout);
784 *action = get_action(context, timeout);
785 return gui_synclist_do_button(lists, action, wrap);
788 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
789 enum screen_type screen, int item)
791 struct viewport vp = *lists->parent[screen];
792 #ifdef HAVE_LCD_BITMAP
793 if (list_display_title(lists, screen))
794 vp.height -= font_get(lists->parent[screen]->font)->height;
795 #endif
796 return item <= (lists->start_item[screen] + viewport_get_nb_lines(&vp));
799 /* Simple use list implementation */
800 static int simplelist_line_count = 0;
801 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
802 /* set the amount of lines shown in the list */
803 void simplelist_set_line_count(int lines)
805 if (lines < 0)
806 simplelist_line_count = 0;
807 else if (lines >= SIMPLELIST_MAX_LINES)
808 simplelist_line_count = SIMPLELIST_MAX_LINES;
809 else
810 simplelist_line_count = lines;
812 /* get the current amount of lines shown */
813 int simplelist_get_line_count(void)
815 return simplelist_line_count;
817 /* add/edit a line in the list.
818 if line_number > number of lines shown it adds the line, else it edits the line */
819 void simplelist_addline(int line_number, const char *fmt, ...)
821 va_list ap;
823 if (line_number > simplelist_line_count)
825 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
826 line_number = simplelist_line_count++;
827 else
828 return;
830 va_start(ap, fmt);
831 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
832 va_end(ap);
835 static char* simplelist_static_getname(int item,
836 void * data,
837 char *buffer,
838 size_t buffer_len)
840 (void)data; (void)buffer; (void)buffer_len;
841 return simplelist_text[item];
844 bool simplelist_show_list(struct simplelist_info *info)
846 struct gui_synclist lists;
847 int action, old_line_count = simplelist_line_count;
848 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
849 char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
850 int wrap = LIST_WRAP_UNLESS_HELD;
851 if (info->get_name)
852 getname = info->get_name;
853 else
854 getname = simplelist_static_getname;
855 gui_synclist_init(&lists, getname, info->callback_data,
856 info->scroll_all, info->selection_size, NULL);
858 if (info->title)
859 gui_synclist_set_title(&lists, info->title, NOICON);
860 if (info->get_icon)
861 gui_synclist_set_icon_callback(&lists, info->get_icon);
862 if (info->get_talk)
863 gui_synclist_set_voice_callback(&lists, info->get_talk);
865 if (info->hide_selection)
867 gui_synclist_hide_selection_marker(&lists, true);
868 wrap = LIST_WRAP_OFF;
871 if (info->action_callback)
872 info->action_callback(ACTION_REDRAW, &lists);
874 if (info->get_name == NULL)
875 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
876 else
877 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
879 gui_synclist_select_item(&lists, info->selection);
881 gui_synclist_draw(&lists);
882 gui_synclist_speak_item(&lists);
884 while(1)
886 list_do_action(CONTEXT_STD, info->timeout,
887 &lists, &action, wrap);
889 /* We must yield in this case or no other thread can run */
890 if (info->timeout == TIMEOUT_NOBLOCK)
891 yield();
893 if (info->action_callback)
895 bool stdok = action==ACTION_STD_OK;
896 action = info->action_callback(action, &lists);
897 if (stdok && action == ACTION_STD_CANCEL)
899 /* callback asked us to exit */
900 info->selection = gui_synclist_get_sel_pos(&lists);
901 break;
904 if (info->get_name == NULL)
905 gui_synclist_set_nb_items(&lists,
906 simplelist_line_count*info->selection_size);
908 if (action == ACTION_STD_CANCEL)
910 info->selection = -1;
911 break;
913 else if ((action == ACTION_REDRAW) ||
914 (old_line_count != simplelist_line_count))
916 if (info->get_name == NULL)
918 gui_synclist_set_nb_items(&lists,
919 simplelist_line_count*info->selection_size);
921 gui_synclist_draw(&lists);
922 old_line_count = simplelist_line_count;
924 else if(default_event_handler(action) == SYS_USB_CONNECTED)
925 return true;
927 talk_shutup();
928 viewportmanager_set_statusbar(oldbars);
929 return false;
932 void simplelist_info_init(struct simplelist_info *info, char* title,
933 int count, void* data)
935 info->title = title;
936 info->count = count;
937 info->selection_size = 1;
938 info->hide_selection = false;
939 info->scroll_all = false;
940 info->timeout = HZ/10;
941 info->selection = 0;
942 info->action_callback = NULL;
943 info->get_icon = NULL;
944 info->get_name = NULL;
945 info->get_talk = NULL;
946 info->callback_data = data;