Reversi: Make Reversi respect the global touchscreen setting when in a menu.
[kugel-rb.git] / apps / gui / list.c
blobeb9c025a95f08c79ec767eb59d09802b7ec43021
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
58 static int force_list_reinit = false;
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 struct viewport parent[NB_SCREENS];
66 void list_init_viewports(struct gui_synclist *list)
68 int i;
69 struct viewport *vp;
70 FOR_NB_SCREENS(i)
72 vp = &parent[i];
73 if (!list || list->parent[i] == vp)
74 viewport_set_defaults(vp, i);
76 #ifdef HAVE_BUTTONBAR
77 if (list && (list->parent[0] == &parent[0]) && global_settings.buttonbar)
78 list->parent[0]->height -= BUTTONBAR_HEIGHT;
79 #endif
80 force_list_reinit = false;
82 #else
83 static struct viewport parent[NB_SCREENS] =
85 [SCREEN_MAIN] =
87 .x = 0,
88 .y = 0,
89 .width = LCD_WIDTH,
90 .height = LCD_HEIGHT
93 void list_init_viewports(struct gui_synclist *list)
95 (void)list;
96 force_list_reinit = false;
98 #endif
100 #ifdef HAVE_LCD_BITMAP
101 bool list_display_title(struct gui_synclist *list, enum screen_type screen)
103 return list->title != NULL &&
104 viewport_get_nb_lines(list->parent[screen])>2;
106 #else
107 #define list_display_title(l, i) false
108 #endif
111 * Initializes a scrolling list
112 * - gui_list : the list structure to initialize
113 * - callback_get_item_name : pointer to a function that associates a label
114 * to a given item number
115 * - data : extra data passed to the list callback
116 * - scroll_all :
117 * - selected_size :
118 * - parent : the parent viewports to use. NULL means the full screen minus
119 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
121 void gui_synclist_init(struct gui_synclist * gui_list,
122 list_get_name callback_get_item_name,
123 void * data,
124 bool scroll_all,
125 int selected_size, struct viewport list_parent[NB_SCREENS]
128 int i;
129 gui_list->callback_get_item_icon = NULL;
130 gui_list->callback_get_item_name = callback_get_item_name;
131 gui_list->callback_speak_item = NULL;
132 gui_list->nb_items = 0;
133 gui_list->selected_item = 0;
134 FOR_NB_SCREENS(i)
136 gui_list->start_item[i] = 0;
137 gui_list->last_displayed_start_item[i] = -1 ;
138 #ifdef HAVE_LCD_BITMAP
139 gui_list->offset_position[i] = 0;
140 #endif
141 if (list_parent)
142 gui_list->parent[i] = &list_parent[i];
143 else
145 gui_list->parent[i] = &parent[i];
148 list_init_viewports(gui_list);
149 gui_list->limit_scroll = false;
150 gui_list->data=data;
151 gui_list->scroll_all=scroll_all;
152 gui_list->selected_size=selected_size;
153 gui_list->title = NULL;
154 gui_list->title_width = 0;
155 gui_list->title_icon = Icon_NOICON;
157 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
158 gui_list->show_selection_marker = true;
159 gui_list->last_displayed_selected_item = -1 ;
161 #ifdef HAVE_LCD_COLOR
162 gui_list->title_color = -1;
163 gui_list->callback_get_item_color = NULL;
164 #endif
165 force_list_reinit = true;
168 /* this toggles the selection bar or cursor */
169 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
171 lists->show_selection_marker = !hide;
175 #ifdef HAVE_LCD_BITMAP
176 int gui_list_get_item_offset(struct gui_synclist * gui_list,
177 int item_width,
178 int text_pos,
179 struct screen * display,
180 struct viewport *vp)
182 int item_offset;
184 if (offset_out_of_view)
186 item_offset = gui_list->offset_position[display->screen_type];
188 else
190 /* if text is smaller then view */
191 if (item_width <= vp->width - text_pos)
193 item_offset = 0;
195 else
197 /* if text got out of view */
198 if (gui_list->offset_position[display->screen_type] >
199 item_width - (vp->width - text_pos))
200 item_offset = item_width - (vp->width - text_pos);
201 else
202 item_offset = gui_list->offset_position[display->screen_type];
206 return item_offset;
208 #endif
210 * Force a full screen update.
213 void gui_synclist_draw(struct gui_synclist *gui_list)
215 int i;
216 static struct gui_synclist *last_list = NULL;
217 static int last_count = -1;
218 #ifdef HAVE_BUTTONBAR
219 static bool last_buttonbar = false;
220 #endif
221 if (force_list_reinit ||
222 #ifdef HAVE_BUTTONBAR
223 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
224 #endif
225 last_list != gui_list ||
226 gui_list->nb_items != last_count)
228 list_init_viewports(gui_list);
229 gui_synclist_select_item(gui_list, gui_list->selected_item);
231 #ifdef HAVE_BUTTONBAR
232 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
233 #endif
234 last_count = gui_list->nb_items;
235 last_list = gui_list;
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;
439 force_list_reinit = true;
443 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
445 #ifdef HAVE_LCD_BITMAP
446 int i;
447 #endif
448 lists->nb_items = nb_items;
449 #ifdef HAVE_LCD_BITMAP
450 FOR_NB_SCREENS(i)
452 lists->offset_position[i] = 0;
454 #endif
456 int gui_synclist_get_nb_items(struct gui_synclist * lists)
458 return lists->nb_items;
460 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
462 return lists->selected_item;
464 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
465 list_get_icon icon_callback)
467 lists->callback_get_item_icon = icon_callback;
470 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
471 list_speak_item voice_callback)
473 lists->callback_speak_item = voice_callback;
476 #ifdef HAVE_LCD_COLOR
477 void gui_synclist_set_color_callback(struct gui_synclist * lists,
478 list_get_color color_callback)
480 lists->callback_get_item_color = color_callback;
482 #endif
484 static void gui_synclist_select_next_page(struct gui_synclist * lists,
485 enum screen_type screen)
487 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
488 #ifdef HAVE_LCD_BITMAP
489 if(list_display_title(lists, screen))
490 nb_lines--;
491 #endif
492 gui_list_select_at_offset(lists, nb_lines);
495 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
496 enum screen_type screen)
498 int nb_lines = viewport_get_nb_lines(lists->parent[screen]);
499 #ifdef HAVE_LCD_BITMAP
500 if(list_display_title(lists, screen))
501 nb_lines--;
502 #endif
503 gui_list_select_at_offset(lists, -nb_lines);
506 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
508 lists->limit_scroll = scroll;
511 #ifdef HAVE_LCD_BITMAP
513 * Makes all the item in the list scroll by one step to the right.
514 * Should stop increasing the value when reaching the widest item value
515 * in the list.
517 static void gui_synclist_scroll_right(struct gui_synclist * lists)
519 int i;
520 FOR_NB_SCREENS(i)
522 /* FIXME: This is a fake right boundry limiter. there should be some
523 * callback function to find the longest item on the list in pixels,
524 * to stop the list from scrolling past that point */
525 lists->offset_position[i]+=offset_step;
526 if (lists->offset_position[i] > 1000)
527 lists->offset_position[i] = 1000;
532 * Makes all the item in the list scroll by one step to the left.
533 * stops at starting position.
535 static void gui_synclist_scroll_left(struct gui_synclist * lists)
537 int i;
538 FOR_NB_SCREENS(i)
540 lists->offset_position[i]-=offset_step;
541 if (lists->offset_position[i] < 0)
542 lists->offset_position[i] = 0;
545 #endif /* HAVE_LCD_BITMAP */
547 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
549 list_speak_item *cb = lists->callback_speak_item;
550 if(cb && gui_synclist_get_nb_items(lists) != 0)
552 int sel = gui_synclist_get_sel_pos(lists);
553 talk_shutup();
554 /* If we got a repeating key action, or we have just very
555 recently started talking, then we want to stay silent for a
556 while until things settle. Likewise if we already had a
557 pending scheduled announcement not yet due: we need to
558 reschedule it. */
559 if(repeating
560 || (lists->scheduled_talk_tick
561 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
562 || (lists->last_talked_tick
563 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
565 lists->scheduled_talk_tick = current_tick +HZ/4;
566 return;
567 } else {
568 lists->scheduled_talk_tick = 0; /* work done */
569 cb(sel, lists->data);
570 lists->last_talked_tick = current_tick;
574 void gui_synclist_speak_item(struct gui_synclist * lists)
575 /* The list user should call this to speak the first item on entering
576 the list, and whenever the list is updated. */
578 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
579 talk_id(VOICE_EMPTY_LIST, true);
580 else _gui_synclist_speak_item(lists, false);
583 #if defined(HAVE_TOUCHSCREEN)
584 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
585 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list);
586 #endif
588 bool gui_synclist_do_button(struct gui_synclist * lists,
589 int *actionptr, enum list_wrap wrap)
591 int action = *actionptr;
592 #ifdef HAVE_LCD_BITMAP
593 static bool scrolling_left = false;
594 #endif
596 #ifdef HAVE_WHEEL_ACCELERATION
597 int next_item_modifier = button_apply_acceleration(get_action_data());
598 #else
599 static int next_item_modifier = 1;
600 static int last_accel_tick = 0;
602 if (global_settings.list_accel_start_delay)
604 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
605 int accel_wait = global_settings.list_accel_wait * HZ/2;
607 if (get_action_statuscode(NULL)&ACTION_REPEAT)
609 if (!last_accel_tick)
610 last_accel_tick = current_tick + start_delay;
611 else if (current_tick >=
612 last_accel_tick + accel_wait)
614 last_accel_tick = current_tick;
615 next_item_modifier++;
618 else if (last_accel_tick)
620 next_item_modifier = 1;
621 last_accel_tick = 0;
624 #endif
626 #if defined(HAVE_TOUCHSCREEN)
627 if (action == ACTION_TOUCHSCREEN)
628 action = *actionptr = gui_synclist_do_touchscreen(lists);
629 #endif
631 switch (wrap)
633 case LIST_WRAP_ON:
634 gui_synclist_limit_scroll(lists, false);
635 break;
636 case LIST_WRAP_OFF:
637 gui_synclist_limit_scroll(lists, true);
638 break;
639 case LIST_WRAP_UNLESS_HELD:
640 if (action == ACTION_STD_PREVREPEAT ||
641 action == ACTION_STD_NEXTREPEAT ||
642 action == ACTION_LISTTREE_PGUP ||
643 action == ACTION_LISTTREE_PGDOWN)
644 gui_synclist_limit_scroll(lists, true);
645 else gui_synclist_limit_scroll(lists, false);
646 break;
649 switch (action)
651 case ACTION_REDRAW:
652 gui_synclist_draw(lists);
653 return true;
655 #ifdef HAVE_VOLUME_IN_LIST
656 case ACTION_LIST_VOLUP:
657 global_settings.volume += 2;
658 /* up two because the falthrough brings it down one */
659 case ACTION_LIST_VOLDOWN:
660 global_settings.volume--;
661 setvol();
662 return true;
663 #endif
664 case ACTION_STD_PREV:
665 case ACTION_STD_PREVREPEAT:
666 gui_list_select_at_offset(lists, -next_item_modifier);
667 #ifndef HAVE_WHEEL_ACCELERATION
668 if (button_queue_count() < FRAMEDROP_TRIGGER)
669 #endif
670 gui_synclist_draw(lists);
671 _gui_synclist_speak_item(lists,
672 action == ACTION_STD_PREVREPEAT
673 || next_item_modifier >1);
674 yield();
675 *actionptr = ACTION_STD_PREV;
676 return true;
678 case ACTION_STD_NEXT:
679 case ACTION_STD_NEXTREPEAT:
680 gui_list_select_at_offset(lists, next_item_modifier);
681 #ifndef HAVE_WHEEL_ACCELERATION
682 if (button_queue_count() < FRAMEDROP_TRIGGER)
683 #endif
684 gui_synclist_draw(lists);
685 _gui_synclist_speak_item(lists,
686 action == ACTION_STD_NEXTREPEAT
687 || next_item_modifier >1);
688 yield();
689 *actionptr = ACTION_STD_NEXT;
690 return true;
692 #ifdef HAVE_LCD_BITMAP
693 case ACTION_TREE_PGRIGHT:
694 gui_synclist_scroll_right(lists);
695 gui_synclist_draw(lists);
696 return true;
697 case ACTION_TREE_ROOT_INIT:
698 /* After this button press ACTION_TREE_PGLEFT is allowed
699 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
700 keymaps as a repeated button press (the same as the repeated
701 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
702 button press */
703 if (lists->offset_position[0] == 0)
705 scrolling_left = false;
706 *actionptr = ACTION_STD_CANCEL;
707 return true;
709 *actionptr = ACTION_TREE_PGLEFT;
710 case ACTION_TREE_PGLEFT:
711 if(!scrolling_left && (lists->offset_position[0] == 0))
713 *actionptr = ACTION_STD_CANCEL;
714 return false;
716 gui_synclist_scroll_left(lists);
717 gui_synclist_draw(lists);
718 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
719 skipping to root */
720 return true;
721 #endif
722 /* for pgup / pgdown, we are obliged to have a different behaviour depending
723 * on the screen for which the user pressed the key since for example, remote
724 * and main screen doesn't have the same number of lines */
725 case ACTION_LISTTREE_PGUP:
727 int screen =
728 #ifdef HAVE_REMOTE_LCD
729 get_action_statuscode(NULL)&ACTION_REMOTE ?
730 SCREEN_REMOTE :
731 #endif
732 SCREEN_MAIN;
733 gui_synclist_select_previous_page(lists, screen);
734 gui_synclist_draw(lists);
735 _gui_synclist_speak_item(lists, false);
736 yield();
737 *actionptr = ACTION_STD_NEXT;
739 return true;
741 case ACTION_LISTTREE_PGDOWN:
743 int screen =
744 #ifdef HAVE_REMOTE_LCD
745 get_action_statuscode(NULL)&ACTION_REMOTE ?
746 SCREEN_REMOTE :
747 #endif
748 SCREEN_MAIN;
749 gui_synclist_select_next_page(lists, screen);
750 gui_synclist_draw(lists);
751 _gui_synclist_speak_item(lists, false);
752 yield();
753 *actionptr = ACTION_STD_PREV;
755 return true;
757 if(lists->scheduled_talk_tick
758 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
759 /* scheduled postponed item announcement is due */
760 _gui_synclist_speak_item(lists, false);
761 return false;
764 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
765 /* Returns the lowest of timeout or the delay until a postponed
766 scheduled announcement is due (if any). */
768 if(lists->scheduled_talk_tick)
770 long delay = lists->scheduled_talk_tick -current_tick +1;
771 /* +1 because the trigger condition uses TIME_AFTER(), which
772 is implemented as strictly greater than. */
773 if(delay < 0)
774 delay = 0;
775 if(timeout > delay || timeout == TIMEOUT_BLOCK)
776 timeout = delay;
778 return timeout;
781 bool list_do_action(int context, int timeout,
782 struct gui_synclist *lists, int *action,
783 enum list_wrap wrap)
784 /* Combines the get_action() (with possibly overridden timeout) and
785 gui_synclist_do_button() calls. Returns the list action from
786 do_button, and places the action from get_action in *action. */
788 timeout = list_do_action_timeout(lists, timeout);
789 *action = get_action(context, timeout);
790 return gui_synclist_do_button(lists, action, wrap);
793 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
794 enum screen_type screen, int item)
796 struct viewport vp = *lists->parent[screen];
797 #ifdef HAVE_LCD_BITMAP
798 if (list_display_title(lists, screen))
799 vp.height -= font_get(lists->parent[screen]->font)->height;
800 #endif
801 return item <= (lists->start_item[screen] + viewport_get_nb_lines(&vp));
804 /* Simple use list implementation */
805 static int simplelist_line_count = 0;
806 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
807 /* set the amount of lines shown in the list */
808 void simplelist_set_line_count(int lines)
810 if (lines < 0)
811 simplelist_line_count = 0;
812 else if (lines >= SIMPLELIST_MAX_LINES)
813 simplelist_line_count = SIMPLELIST_MAX_LINES;
814 else
815 simplelist_line_count = lines;
817 /* get the current amount of lines shown */
818 int simplelist_get_line_count(void)
820 return simplelist_line_count;
822 /* add/edit a line in the list.
823 if line_number > number of lines shown it adds the line, else it edits the line */
824 void simplelist_addline(int line_number, const char *fmt, ...)
826 va_list ap;
828 if (line_number > simplelist_line_count)
830 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
831 line_number = simplelist_line_count++;
832 else
833 return;
835 va_start(ap, fmt);
836 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
837 va_end(ap);
840 static char* simplelist_static_getname(int item,
841 void * data,
842 char *buffer,
843 size_t buffer_len)
845 (void)data; (void)buffer; (void)buffer_len;
846 return simplelist_text[item];
849 bool simplelist_show_list(struct simplelist_info *info)
851 struct gui_synclist lists;
852 int action, old_line_count = simplelist_line_count;
853 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
854 char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
855 int wrap = LIST_WRAP_UNLESS_HELD;
856 if (info->get_name)
857 getname = info->get_name;
858 else
859 getname = simplelist_static_getname;
860 gui_synclist_init(&lists, getname, info->callback_data,
861 info->scroll_all, info->selection_size, NULL);
863 if (info->title)
864 gui_synclist_set_title(&lists, info->title, NOICON);
865 if (info->get_icon)
866 gui_synclist_set_icon_callback(&lists, info->get_icon);
867 if (info->get_talk)
868 gui_synclist_set_voice_callback(&lists, info->get_talk);
870 if (info->hide_selection)
872 gui_synclist_hide_selection_marker(&lists, true);
873 wrap = LIST_WRAP_OFF;
876 if (info->action_callback)
877 info->action_callback(ACTION_REDRAW, &lists);
879 if (info->get_name == NULL)
880 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
881 else
882 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
884 gui_synclist_select_item(&lists, info->selection);
886 gui_synclist_draw(&lists);
887 gui_synclist_speak_item(&lists);
889 while(1)
891 list_do_action(CONTEXT_STD, info->timeout,
892 &lists, &action, wrap);
894 /* We must yield in this case or no other thread can run */
895 if (info->timeout == TIMEOUT_NOBLOCK)
896 yield();
898 if (info->action_callback)
900 bool stdok = action==ACTION_STD_OK;
901 action = info->action_callback(action, &lists);
902 if (stdok && action == ACTION_STD_CANCEL)
904 /* callback asked us to exit */
905 info->selection = gui_synclist_get_sel_pos(&lists);
906 break;
909 if (info->get_name == NULL)
910 gui_synclist_set_nb_items(&lists,
911 simplelist_line_count*info->selection_size);
913 if (action == ACTION_STD_CANCEL)
915 info->selection = -1;
916 break;
918 else if ((action == ACTION_REDRAW) ||
919 (old_line_count != simplelist_line_count))
921 if (info->get_name == NULL)
923 gui_synclist_set_nb_items(&lists,
924 simplelist_line_count*info->selection_size);
926 gui_synclist_draw(&lists);
927 old_line_count = simplelist_line_count;
929 else if(default_event_handler(action) == SYS_USB_CONNECTED)
930 return true;
932 talk_shutup();
933 viewportmanager_set_statusbar(oldbars);
934 return false;
937 void simplelist_info_init(struct simplelist_info *info, char* title,
938 int count, void* data)
940 info->title = title;
941 info->count = count;
942 info->selection_size = 1;
943 info->hide_selection = false;
944 info->scroll_all = false;
945 info->timeout = HZ/10;
946 info->selection = 0;
947 info->action_callback = NULL;
948 info->get_icon = NULL;
949 info->get_name = NULL;
950 info->get_talk = NULL;
951 info->callback_data = data;