Remove unneeded #include "backdrop.h"
[kugel-rb.git] / apps / gui / list.c
blob393c9bf69efa59be35e3c8fbb3e73032caf48569
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 "list.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
59 static int force_list_reinit = false;
61 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
62 int offset);
63 void list_draw(struct screen *display, struct gui_synclist *list);
65 #ifdef HAVE_LCD_BITMAP
66 static struct viewport parent[NB_SCREENS];
67 void list_init_viewports(struct gui_synclist *list)
69 int i;
70 struct viewport *vp;
71 FOR_NB_SCREENS(i)
73 vp = &parent[i];
74 if (!list || list->parent[i] == vp)
75 viewport_set_defaults(vp, i);
77 #ifdef HAVE_BUTTONBAR
78 if (list && (list->parent[0] == &parent[0]) && global_settings.buttonbar)
79 list->parent[0]->height -= BUTTONBAR_HEIGHT;
80 #endif
81 force_list_reinit = false;
83 #else
84 static struct viewport parent[NB_SCREENS] =
86 [SCREEN_MAIN] =
88 .x = 0,
89 .y = 0,
90 .width = LCD_WIDTH,
91 .height = LCD_HEIGHT
94 void list_init_viewports(struct gui_synclist *list)
96 (void)list;
97 force_list_reinit = false;
99 #endif
101 #ifdef HAVE_LCD_BITMAP
102 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
104 return list->title != NULL &&
105 viewport_get_nb_lines(list->parent[screen])>2;
107 #else
108 #define list_display_title(l, i) false
109 #endif
112 * Initializes a scrolling list
113 * - gui_list : the list structure to initialize
114 * - callback_get_item_name : pointer to a function that associates a label
115 * to a given item number
116 * - data : extra data passed to the list callback
117 * - scroll_all :
118 * - selected_size :
119 * - parent : the parent viewports to use. NULL means the full screen minus
120 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
122 void gui_synclist_init(struct gui_synclist * gui_list,
123 list_get_name callback_get_item_name,
124 void * data,
125 bool scroll_all,
126 int selected_size, struct viewport list_parent[NB_SCREENS]
129 int i;
130 gui_list->callback_get_item_icon = NULL;
131 gui_list->callback_get_item_name = callback_get_item_name;
132 gui_list->callback_speak_item = NULL;
133 gui_list->nb_items = 0;
134 gui_list->selected_item = 0;
135 FOR_NB_SCREENS(i)
137 gui_list->start_item[i] = 0;
138 gui_list->last_displayed_start_item[i] = -1 ;
139 #ifdef HAVE_LCD_BITMAP
140 gui_list->offset_position[i] = 0;
141 #endif
142 if (list_parent)
143 gui_list->parent[i] = &list_parent[i];
144 else
146 gui_list->parent[i] = &parent[i];
149 list_init_viewports(gui_list);
150 gui_list->limit_scroll = false;
151 gui_list->data=data;
152 gui_list->scroll_all=scroll_all;
153 gui_list->selected_size=selected_size;
154 gui_list->title = NULL;
155 gui_list->title_width = 0;
156 gui_list->title_icon = Icon_NOICON;
158 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
159 gui_list->show_selection_marker = true;
160 gui_list->last_displayed_selected_item = -1 ;
162 #ifdef HAVE_LCD_COLOR
163 gui_list->title_color = -1;
164 gui_list->callback_get_item_color = NULL;
165 #endif
166 force_list_reinit = true;
169 /* this toggles the selection bar or cursor */
170 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
172 lists->show_selection_marker = !hide;
176 #ifdef HAVE_LCD_BITMAP
177 int gui_list_get_item_offset(struct gui_synclist * gui_list,
178 int item_width,
179 int text_pos,
180 struct screen * display,
181 struct viewport *vp)
183 int item_offset;
185 if (offset_out_of_view)
187 item_offset = gui_list->offset_position[display->screen_type];
189 else
191 /* if text is smaller then view */
192 if (item_width <= vp->width - text_pos)
194 item_offset = 0;
196 else
198 /* if text got out of view */
199 if (gui_list->offset_position[display->screen_type] >
200 item_width - (vp->width - text_pos))
201 item_offset = item_width - (vp->width - text_pos);
202 else
203 item_offset = gui_list->offset_position[display->screen_type];
207 return item_offset;
209 #endif
211 * Force a full screen update.
214 void gui_synclist_draw(struct gui_synclist *gui_list)
216 int i;
217 static struct gui_synclist *last_list = NULL;
218 static int last_count = -1;
219 #ifdef HAVE_BUTTONBAR
220 static bool last_buttonbar = false;
221 #endif
222 if (force_list_reinit ||
223 #ifdef HAVE_BUTTONBAR
224 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
225 #endif
226 last_list != gui_list ||
227 gui_list->nb_items != last_count)
229 list_init_viewports(gui_list);
230 gui_synclist_select_item(gui_list, gui_list->selected_item);
232 #ifdef HAVE_BUTTONBAR
233 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
234 #endif
235 last_count = gui_list->nb_items;
236 last_list = gui_list;
237 FOR_NB_SCREENS(i)
239 list_draw(&screens[i], gui_list);
243 /* sets up the list so the selection is shown correctly on the screen */
244 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
245 enum screen_type screen)
247 int nb_lines;
248 int difference = gui_list->selected_item - gui_list->start_item[screen];
249 struct viewport vp = *gui_list->parent[screen];
250 #ifdef HAVE_LCD_BITMAP
251 if (list_display_title(gui_list, screen))
252 vp.height -= font_get(gui_list->parent[screen]->font)->height;
253 #endif
254 nb_lines = viewport_get_nb_lines(&vp);
256 /* edge case,, selected last item */
257 if (gui_list->selected_item == gui_list->nb_items -1)
259 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
261 /* selected first item */
262 else if (gui_list->selected_item == 0)
264 gui_list->start_item[screen] = 0;
266 else if (difference < SCROLL_LIMIT) /* list moved up */
268 if (global_settings.scroll_paginated)
270 if (gui_list->start_item[screen] > gui_list->selected_item)
272 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)
273 *nb_lines;
275 if (gui_list->nb_items <= nb_lines)
276 gui_list->start_item[screen] = 0;
278 else
280 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT + 1;
281 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
282 gui_list->start_item[screen] = MAX(0, temp);
285 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
287 int bottom = gui_list->nb_items - nb_lines;
288 /* always move the screen if selection isnt "visible" */
289 if (gui_list->show_selection_marker == false)
291 if (bottom < 0)
292 bottom = 0;
293 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
294 2*gui_list->selected_size);
296 else if (global_settings.scroll_paginated)
298 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
299 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
301 else
303 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
304 int temp = MAX(0, top_of_screen);
305 gui_list->start_item[screen] = MIN(bottom, temp);
310 * Selects an item in the list
311 * - gui_list : the list structure
312 * - item_number : the number of the item which will be selected
314 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
316 int i;
317 if( item_number > gui_list->nb_items-1 || item_number < 0 )
318 return;
319 gui_list->selected_item = item_number;
320 FOR_NB_SCREENS(i)
321 gui_list_put_selection_on_screen(gui_list, i);
324 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
325 int offset)
327 int new_selection;
328 if (gui_list->selected_size > 1)
330 offset *= gui_list->selected_size;
331 /* always select the first item of multi-line lists */
332 offset -= offset%gui_list->selected_size;
334 new_selection = gui_list->selected_item + offset;
335 if (new_selection >= gui_list->nb_items)
337 gui_list->selected_item = gui_list->limit_scroll ?
338 gui_list->nb_items - gui_list->selected_size : 0;
340 else if (new_selection < 0)
342 gui_list->selected_item = gui_list->limit_scroll ?
343 0 : gui_list->nb_items - gui_list->selected_size;
345 else if (gui_list->show_selection_marker == false)
347 int i, nb_lines, screen_top;
348 FOR_NB_SCREENS(i)
350 struct viewport vp = *gui_list->parent[i];
351 #ifdef HAVE_LCD_BITMAP
352 if (list_display_title(gui_list, i))
353 vp.height -= font_get(gui_list->parent[i]->font)->height;
354 #endif
355 nb_lines = viewport_get_nb_lines(&vp);
356 if (offset > 0)
358 screen_top = gui_list->nb_items-nb_lines;
359 if (screen_top < 0)
360 screen_top = 0;
361 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
362 gui_list->selected_size);
363 gui_list->selected_item = gui_list->start_item[i];
365 else
367 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
368 gui_list->selected_size);
369 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
372 return;
374 else gui_list->selected_item += offset;
375 gui_synclist_select_item(gui_list, gui_list->selected_item);
379 * Adds an item to the list (the callback will be asked for one more item)
380 * - gui_list : the list structure
382 void gui_synclist_add_item(struct gui_synclist * gui_list)
384 gui_list->nb_items++;
385 /* if only one item in the list, select it */
386 if(gui_list->nb_items == 1)
387 gui_list->selected_item = 0;
391 * Removes an item to the list (the callback will be asked for one less item)
392 * - gui_list : the list structure
394 void gui_synclist_del_item(struct gui_synclist * gui_list)
396 if(gui_list->nb_items > 0)
398 if (gui_list->selected_item == gui_list->nb_items-1)
399 gui_list->selected_item--;
400 gui_list->nb_items--;
401 gui_synclist_select_item(gui_list, gui_list->selected_item);
405 #ifdef HAVE_LCD_BITMAP
406 void gui_list_screen_scroll_step(int ofs)
408 offset_step = ofs;
411 void gui_list_screen_scroll_out_of_view(bool enable)
413 if (enable)
414 offset_out_of_view = true;
415 else
416 offset_out_of_view = false;
418 #endif /* HAVE_LCD_BITMAP */
421 * Set the title and title icon of the list. Setting title to NULL disables
422 * both the title and icon. Use NOICON if there is no icon.
424 void gui_synclist_set_title(struct gui_synclist * gui_list,
425 char * title, enum themable_icons icon)
427 gui_list->title = title;
428 gui_list->title_icon = icon;
429 if (title) {
430 #ifdef HAVE_LCD_BITMAP
431 int i;
432 FOR_NB_SCREENS(i)
433 screens[i].getstringsize(title, &gui_list->title_width, NULL);
434 #else
435 gui_list->title_width = strlen(title);
436 #endif
437 } else {
438 gui_list->title_width = 0;
440 force_list_reinit = true;
444 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
446 #ifdef HAVE_LCD_BITMAP
447 int i;
448 #endif
449 lists->nb_items = nb_items;
450 #ifdef HAVE_LCD_BITMAP
451 FOR_NB_SCREENS(i)
453 lists->offset_position[i] = 0;
455 #endif
457 int gui_synclist_get_nb_items(struct gui_synclist * lists)
459 return lists->nb_items;
461 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
463 return lists->selected_item;
465 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
466 list_get_icon icon_callback)
468 lists->callback_get_item_icon = icon_callback;
471 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
472 list_speak_item voice_callback)
474 lists->callback_speak_item = voice_callback;
477 #ifdef HAVE_LCD_COLOR
478 void gui_synclist_set_color_callback(struct gui_synclist * lists,
479 list_get_color color_callback)
481 lists->callback_get_item_color = color_callback;
483 #endif
485 static void gui_synclist_select_next_page(struct gui_synclist * lists,
486 enum screen_type screen)
488 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
489 gui_list_select_at_offset(lists, nb_lines);
492 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
493 enum screen_type screen)
495 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
496 gui_list_select_at_offset(lists, -nb_lines);
499 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
501 lists->limit_scroll = scroll;
504 #ifdef HAVE_LCD_BITMAP
506 * Makes all the item in the list scroll by one step to the right.
507 * Should stop increasing the value when reaching the widest item value
508 * in the list.
510 static void gui_synclist_scroll_right(struct gui_synclist * lists)
512 int i;
513 FOR_NB_SCREENS(i)
515 /* FIXME: This is a fake right boundry limiter. there should be some
516 * callback function to find the longest item on the list in pixels,
517 * to stop the list from scrolling past that point */
518 lists->offset_position[i]+=offset_step;
519 if (lists->offset_position[i] > 1000)
520 lists->offset_position[i] = 1000;
525 * Makes all the item in the list scroll by one step to the left.
526 * stops at starting position.
528 static void gui_synclist_scroll_left(struct gui_synclist * lists)
530 int i;
531 FOR_NB_SCREENS(i)
533 lists->offset_position[i]-=offset_step;
534 if (lists->offset_position[i] < 0)
535 lists->offset_position[i] = 0;
538 #endif /* HAVE_LCD_BITMAP */
540 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
542 list_speak_item *cb = lists->callback_speak_item;
543 if(cb && gui_synclist_get_nb_items(lists) != 0)
545 int sel = gui_synclist_get_sel_pos(lists);
546 talk_shutup();
547 /* If we got a repeating key action, or we have just very
548 recently started talking, then we want to stay silent for a
549 while until things settle. Likewise if we already had a
550 pending scheduled announcement not yet due: we need to
551 reschedule it. */
552 if(repeating
553 || (lists->scheduled_talk_tick
554 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
555 || (lists->last_talked_tick
556 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
558 lists->scheduled_talk_tick = current_tick +HZ/4;
559 return;
560 } else {
561 lists->scheduled_talk_tick = 0; /* work done */
562 cb(sel, lists->data);
563 lists->last_talked_tick = current_tick;
567 void gui_synclist_speak_item(struct gui_synclist * lists)
568 /* The list user should call this to speak the first item on entering
569 the list, and whenever the list is updated. */
571 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
572 talk_id(VOICE_EMPTY_LIST, true);
573 else _gui_synclist_speak_item(lists, false);
576 extern intptr_t get_action_data(void);
577 #if defined(HAVE_TOUCHSCREEN)
578 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
579 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list);
580 #endif
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 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 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;