Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / apps / gui / list.c
blob24ddcd630273b02b520ba5c8c3a6d1646e6d27fa
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;
113 static int list_get_nb_lines(struct gui_synclist *list, enum screen_type screen)
115 struct viewport vp = *list->parent[screen];
116 if (list_display_title(list, screen))
117 vp.height -= font_get(list->parent[screen]->font)->height;
118 return viewport_get_nb_lines(&vp);
120 #else
121 static struct viewport parent[NB_SCREENS] =
123 [SCREEN_MAIN] =
125 .x = 0,
126 .y = 0,
127 .width = LCD_WIDTH,
128 .height = LCD_HEIGHT
131 #define list_display_title(l, i) false
132 #define list_get_nb_lines(list, screen) \
133 viewport_get_nb_lines((list)->parent[(screen)]);
134 #endif
137 * Initializes a scrolling list
138 * - gui_list : the list structure to initialize
139 * - callback_get_item_name : pointer to a function that associates a label
140 * to a given item number
141 * - data : extra data passed to the list callback
142 * - scroll_all :
143 * - selected_size :
144 * - parent : the parent viewports to use. NULL means the full screen minus
145 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
147 void gui_synclist_init(struct gui_synclist * gui_list,
148 list_get_name callback_get_item_name,
149 void * data,
150 bool scroll_all,
151 int selected_size, struct viewport list_parent[NB_SCREENS]
154 int i;
155 gui_list->callback_get_item_icon = NULL;
156 gui_list->callback_get_item_name = callback_get_item_name;
157 gui_list->callback_speak_item = NULL;
158 gui_list->nb_items = 0;
159 gui_list->selected_item = 0;
160 FOR_NB_SCREENS(i)
162 gui_list->start_item[i] = 0;
163 gui_list->last_displayed_start_item[i] = -1 ;
164 #ifdef HAVE_LCD_BITMAP
165 gui_list->offset_position[i] = 0;
166 #endif
167 if (list_parent)
168 gui_list->parent[i] = &list_parent[i];
169 else
170 gui_list->parent[i] = &parent[i];
172 list_init_viewports(gui_list);
173 gui_list->limit_scroll = false;
174 gui_list->data = data;
175 gui_list->scroll_all = scroll_all;
176 gui_list->selected_size = selected_size;
177 gui_list->title = NULL;
178 gui_list->title_width = 0;
179 gui_list->title_icon = Icon_NOICON;
181 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
182 gui_list->show_selection_marker = true;
183 gui_list->last_displayed_selected_item = -1;
185 #ifdef HAVE_LCD_COLOR
186 gui_list->title_color = -1;
187 gui_list->callback_get_item_color = NULL;
188 #endif
191 /* this toggles the selection bar or cursor */
192 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
194 lists->show_selection_marker = !hide;
198 #ifdef HAVE_LCD_BITMAP
199 int gui_list_get_item_offset(struct gui_synclist * gui_list,
200 int item_width,
201 int text_pos,
202 struct screen * display,
203 struct viewport *vp)
205 int item_offset;
207 if (offset_out_of_view)
209 item_offset = gui_list->offset_position[display->screen_type];
211 else
213 /* if text is smaller than view */
214 if (item_width <= vp->width - text_pos)
216 item_offset = 0;
218 /* if text got out of view */
219 else if (gui_list->offset_position[display->screen_type] >
220 item_width - (vp->width - text_pos))
222 item_offset = item_width - (vp->width - text_pos);
224 else
225 item_offset = gui_list->offset_position[display->screen_type];
228 return item_offset;
230 #endif
233 * Force a full screen update.
235 void gui_synclist_draw(struct gui_synclist *gui_list)
237 int i;
238 #ifdef HAVE_LCD_BITMAP
239 if (list_need_reinit)
241 list_init_viewports(gui_list);
242 gui_synclist_select_item(gui_list, gui_list->selected_item);
244 #endif
245 FOR_NB_SCREENS(i)
247 list_draw(&screens[i], gui_list);
251 /* sets up the list so the selection is shown correctly on the screen */
252 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
253 enum screen_type screen)
255 int nb_lines = list_get_nb_lines(gui_list, screen);
256 int bottom = MAX(0, gui_list->nb_items - nb_lines);
257 int new_start_item = gui_list->start_item[screen];
258 int difference = gui_list->selected_item - gui_list->start_item[screen];
260 /* edge case, selected last item */
261 if (gui_list->selected_item == gui_list->nb_items -1)
263 new_start_item = bottom;
265 /* selected first item */
266 else if (gui_list->selected_item == 0)
268 new_start_item = 0;
270 else if (difference < SCROLL_LIMIT) /* list moved up */
272 if (global_settings.scroll_paginated)
274 if (new_start_item > gui_list->selected_item)
275 new_start_item = (gui_list->selected_item/nb_lines)*nb_lines;
277 else
279 new_start_item = gui_list->selected_item - SCROLL_LIMIT + 1;
282 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
284 /* always move the screen if selection isnt "visible" */
285 if (gui_list->show_selection_marker == false)
287 new_start_item += 2*gui_list->selected_size;
289 else if (global_settings.scroll_paginated)
291 if (new_start_item + nb_lines <= gui_list->selected_item)
292 new_start_item = (gui_list->selected_item/nb_lines)*nb_lines;
294 else
296 new_start_item = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
299 if (new_start_item < 0)
300 gui_list->start_item[screen] = 0;
301 else if (new_start_item > bottom)
302 gui_list->start_item[screen] = bottom;
303 else
304 gui_list->start_item[screen] = new_start_item;
308 * Selects an item in the list
309 * - gui_list : the list structure
310 * - item_number : the number of the item which will be selected
312 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
314 int i;
315 if (item_number >= gui_list->nb_items || item_number < 0)
316 return;
317 gui_list->selected_item = item_number;
318 FOR_NB_SCREENS(i)
319 gui_list_put_selection_on_screen(gui_list, i);
322 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
323 int offset)
325 int new_selection;
326 if (gui_list->selected_size > 1)
328 offset *= gui_list->selected_size;
329 /* always select the first item of multi-line lists */
330 offset -= offset%gui_list->selected_size;
332 new_selection = gui_list->selected_item + offset;
333 if (new_selection >= gui_list->nb_items)
335 gui_list->selected_item = gui_list->limit_scroll ?
336 gui_list->nb_items - gui_list->selected_size : 0;
338 else if (new_selection < 0)
340 gui_list->selected_item = gui_list->limit_scroll ?
341 0 : gui_list->nb_items - gui_list->selected_size;
343 else if (gui_list->show_selection_marker == false)
345 int i, nb_lines, screen_top;
346 FOR_NB_SCREENS(i)
348 nb_lines = list_get_nb_lines(gui_list, i);
349 if (offset > 0)
351 screen_top = MAX(0, gui_list->nb_items - nb_lines);
352 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
353 gui_list->selected_size);
354 gui_list->selected_item = gui_list->start_item[i];
356 else
358 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
359 gui_list->selected_size);
360 gui_list->selected_item = gui_list->start_item[i] + nb_lines;
363 return;
365 else gui_list->selected_item += offset;
366 gui_synclist_select_item(gui_list, gui_list->selected_item);
370 * Adds an item to the list (the callback will be asked for one more item)
371 * - gui_list : the list structure
373 void gui_synclist_add_item(struct gui_synclist * gui_list)
375 gui_list->nb_items++;
376 /* if only one item in the list, select it */
377 if (gui_list->nb_items == 1)
378 gui_list->selected_item = 0;
382 * Removes an item to the list (the callback will be asked for one less item)
383 * - gui_list : the list structure
385 void gui_synclist_del_item(struct gui_synclist * gui_list)
387 if (gui_list->nb_items > 0)
389 if (gui_list->selected_item == gui_list->nb_items-1)
390 gui_list->selected_item--;
391 gui_list->nb_items--;
392 gui_synclist_select_item(gui_list, gui_list->selected_item);
396 #ifdef HAVE_LCD_BITMAP
397 void gui_list_screen_scroll_step(int ofs)
399 offset_step = ofs;
402 void gui_list_screen_scroll_out_of_view(bool enable)
404 offset_out_of_view = enable;
406 #endif /* HAVE_LCD_BITMAP */
409 * Set the title and title icon of the list. Setting title to NULL disables
410 * both the title and icon. Use NOICON if there is no icon.
412 void gui_synclist_set_title(struct gui_synclist * gui_list,
413 char * title, enum themable_icons icon)
415 gui_list->title = title;
416 gui_list->title_icon = icon;
417 if (title)
419 #ifdef HAVE_LCD_BITMAP
420 int i;
421 FOR_NB_SCREENS(i)
422 screens[i].getstringsize(title, &gui_list->title_width, NULL);
423 #else
424 gui_list->title_width = strlen(title);
425 #endif
427 else
429 gui_list->title_width = 0;
433 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
435 #ifdef HAVE_LCD_BITMAP
436 int i;
437 #endif
438 lists->nb_items = nb_items;
439 #ifdef HAVE_LCD_BITMAP
440 FOR_NB_SCREENS(i)
442 lists->offset_position[i] = 0;
444 #endif
446 int gui_synclist_get_nb_items(struct gui_synclist * lists)
448 return lists->nb_items;
450 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
452 return lists->selected_item;
454 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
455 list_get_icon icon_callback)
457 lists->callback_get_item_icon = icon_callback;
460 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
461 list_speak_item voice_callback)
463 lists->callback_speak_item = voice_callback;
466 #ifdef HAVE_LCD_COLOR
467 void gui_synclist_set_color_callback(struct gui_synclist * lists,
468 list_get_color color_callback)
470 lists->callback_get_item_color = color_callback;
472 #endif
474 static void gui_synclist_select_next_page(struct gui_synclist * lists,
475 enum screen_type screen)
477 int nb_lines = list_get_nb_lines(lists, screen);
478 gui_list_select_at_offset(lists, nb_lines);
481 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
482 enum screen_type screen)
484 int nb_lines = list_get_nb_lines(lists, screen);
485 gui_list_select_at_offset(lists, -nb_lines);
488 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
490 lists->limit_scroll = scroll;
493 #ifdef HAVE_LCD_BITMAP
495 * Makes all the item in the list scroll by one step to the right.
496 * Should stop increasing the value when reaching the widest item value
497 * in the list.
499 static void gui_synclist_scroll_right(struct gui_synclist * lists)
501 int i;
502 FOR_NB_SCREENS(i)
504 /* FIXME: This is a fake right boundry limiter. there should be some
505 * callback function to find the longest item on the list in pixels,
506 * to stop the list from scrolling past that point */
507 lists->offset_position[i] += offset_step;
508 if (lists->offset_position[i] > 1000)
509 lists->offset_position[i] = 1000;
514 * Makes all the item in the list scroll by one step to the left.
515 * stops at starting position.
517 static void gui_synclist_scroll_left(struct gui_synclist * lists)
519 int i;
520 FOR_NB_SCREENS(i)
522 lists->offset_position[i] -= offset_step;
523 if (lists->offset_position[i] < 0)
524 lists->offset_position[i] = 0;
527 #endif /* HAVE_LCD_BITMAP */
529 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
531 list_speak_item *cb = lists->callback_speak_item;
532 if(cb && gui_synclist_get_nb_items(lists) != 0)
534 int sel = gui_synclist_get_sel_pos(lists);
535 talk_shutup();
536 /* If we got a repeating key action, or we have just very
537 recently started talking, then we want to stay silent for a
538 while until things settle. Likewise if we already had a
539 pending scheduled announcement not yet due: we need to
540 reschedule it. */
541 if(repeating
542 || (lists->scheduled_talk_tick
543 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
544 || (lists->last_talked_tick
545 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
547 lists->scheduled_talk_tick = current_tick +HZ/4;
548 return;
549 } else {
550 lists->scheduled_talk_tick = 0; /* work done */
551 cb(sel, lists->data);
552 lists->last_talked_tick = current_tick;
556 void gui_synclist_speak_item(struct gui_synclist * lists)
557 /* The list user should call this to speak the first item on entering
558 the list, and whenever the list is updated. */
560 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
561 talk_id(VOICE_EMPTY_LIST, true);
562 else _gui_synclist_speak_item(lists, false);
565 bool gui_synclist_do_button(struct gui_synclist * lists,
566 int *actionptr, enum list_wrap wrap)
568 int action = *actionptr;
569 #ifdef HAVE_LCD_BITMAP
570 static bool scrolling_left = false;
571 #endif
573 #ifdef HAVE_WHEEL_ACCELERATION
574 int next_item_modifier = button_apply_acceleration(get_action_data());
575 #else
576 static int next_item_modifier = 1;
577 static int last_accel_tick = 0;
579 if (global_settings.list_accel_start_delay)
581 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
582 int accel_wait = global_settings.list_accel_wait * HZ/2;
584 if (get_action_statuscode(NULL)&ACTION_REPEAT)
586 if (!last_accel_tick)
587 last_accel_tick = current_tick + start_delay;
588 else if (TIME_AFTER(current_tick, last_accel_tick + accel_wait))
590 last_accel_tick = current_tick;
591 next_item_modifier++;
594 else if (last_accel_tick)
596 next_item_modifier = 1;
597 last_accel_tick = 0;
600 #endif
602 #if defined(HAVE_TOUCHSCREEN)
603 if (action == ACTION_TOUCHSCREEN)
604 action = *actionptr = gui_synclist_do_touchscreen(lists);
605 #endif
607 switch (wrap)
609 case LIST_WRAP_ON:
610 gui_synclist_limit_scroll(lists, false);
611 break;
612 case LIST_WRAP_OFF:
613 gui_synclist_limit_scroll(lists, true);
614 break;
615 case LIST_WRAP_UNLESS_HELD:
616 if (action == ACTION_STD_PREVREPEAT ||
617 action == ACTION_STD_NEXTREPEAT ||
618 action == ACTION_LISTTREE_PGUP ||
619 action == ACTION_LISTTREE_PGDOWN)
620 gui_synclist_limit_scroll(lists, true);
621 else gui_synclist_limit_scroll(lists, false);
622 break;
625 switch (action)
627 case ACTION_REDRAW:
628 gui_synclist_draw(lists);
629 return true;
631 #ifdef HAVE_VOLUME_IN_LIST
632 case ACTION_LIST_VOLUP:
633 global_settings.volume += 2;
634 /* up two because the falthrough brings it down one */
635 case ACTION_LIST_VOLDOWN:
636 global_settings.volume--;
637 setvol();
638 return true;
639 #endif
640 case ACTION_STD_PREV:
641 case ACTION_STD_PREVREPEAT:
642 gui_list_select_at_offset(lists, -next_item_modifier);
643 #ifndef HAVE_WHEEL_ACCELERATION
644 if (button_queue_count() < FRAMEDROP_TRIGGER)
645 #endif
646 gui_synclist_draw(lists);
647 _gui_synclist_speak_item(lists,
648 action == ACTION_STD_PREVREPEAT
649 || next_item_modifier > 1);
650 yield();
651 *actionptr = ACTION_STD_PREV;
652 return true;
654 case ACTION_STD_NEXT:
655 case ACTION_STD_NEXTREPEAT:
656 gui_list_select_at_offset(lists, next_item_modifier);
657 #ifndef HAVE_WHEEL_ACCELERATION
658 if (button_queue_count() < FRAMEDROP_TRIGGER)
659 #endif
660 gui_synclist_draw(lists);
661 _gui_synclist_speak_item(lists,
662 action == ACTION_STD_NEXTREPEAT
663 || next_item_modifier >1);
664 yield();
665 *actionptr = ACTION_STD_NEXT;
666 return true;
668 #ifdef HAVE_LCD_BITMAP
669 case ACTION_TREE_PGRIGHT:
670 gui_synclist_scroll_right(lists);
671 gui_synclist_draw(lists);
672 return true;
673 case ACTION_TREE_ROOT_INIT:
674 /* After this button press ACTION_TREE_PGLEFT is allowed
675 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
676 keymaps as a repeated button press (the same as the repeated
677 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
678 button press */
679 if (lists->offset_position[0] == 0)
681 scrolling_left = false;
682 *actionptr = ACTION_STD_CANCEL;
683 return true;
685 *actionptr = ACTION_TREE_PGLEFT;
686 case ACTION_TREE_PGLEFT:
687 if(!scrolling_left && (lists->offset_position[0] == 0))
689 *actionptr = ACTION_STD_CANCEL;
690 return false;
692 gui_synclist_scroll_left(lists);
693 gui_synclist_draw(lists);
694 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
695 skipping to root */
696 return true;
697 #endif
698 /* for pgup / pgdown, we are obliged to have a different behaviour depending
699 * on the screen for which the user pressed the key since for example, remote
700 * and main screen doesn't have the same number of lines */
701 case ACTION_LISTTREE_PGUP:
703 int screen =
704 #ifdef HAVE_REMOTE_LCD
705 get_action_statuscode(NULL)&ACTION_REMOTE ?
706 SCREEN_REMOTE :
707 #endif
708 SCREEN_MAIN;
709 gui_synclist_select_previous_page(lists, screen);
710 gui_synclist_draw(lists);
711 _gui_synclist_speak_item(lists, false);
712 yield();
713 *actionptr = ACTION_STD_NEXT;
715 return true;
717 case ACTION_LISTTREE_PGDOWN:
719 int screen =
720 #ifdef HAVE_REMOTE_LCD
721 get_action_statuscode(NULL)&ACTION_REMOTE ?
722 SCREEN_REMOTE :
723 #endif
724 SCREEN_MAIN;
725 gui_synclist_select_next_page(lists, screen);
726 gui_synclist_draw(lists);
727 _gui_synclist_speak_item(lists, false);
728 yield();
729 *actionptr = ACTION_STD_PREV;
731 return true;
733 if(lists->scheduled_talk_tick
734 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
735 /* scheduled postponed item announcement is due */
736 _gui_synclist_speak_item(lists, false);
737 return false;
740 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
741 /* Returns the lowest of timeout or the delay until a postponed
742 scheduled announcement is due (if any). */
744 if(lists->scheduled_talk_tick)
746 long delay = lists->scheduled_talk_tick -current_tick +1;
747 /* +1 because the trigger condition uses TIME_AFTER(), which
748 is implemented as strictly greater than. */
749 if(delay < 0)
750 delay = 0;
751 if(timeout > delay || timeout == TIMEOUT_BLOCK)
752 timeout = delay;
754 return timeout;
757 bool list_do_action(int context, int timeout,
758 struct gui_synclist *lists, int *action,
759 enum list_wrap wrap)
760 /* Combines the get_action() (with possibly overridden timeout) and
761 gui_synclist_do_button() calls. Returns the list action from
762 do_button, and places the action from get_action in *action. */
764 timeout = list_do_action_timeout(lists, timeout);
765 *action = get_action(context, timeout);
766 return gui_synclist_do_button(lists, action, wrap);
769 bool gui_synclist_item_is_onscreen(struct gui_synclist *lists,
770 enum screen_type screen, int item)
772 int nb_lines = list_get_nb_lines(lists, screen);
773 return (unsigned)(item - lists->start_item[screen]) < (unsigned) nb_lines;
776 /* Simple use list implementation */
777 static int simplelist_line_count = 0;
778 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
779 /* set the amount of lines shown in the list */
780 void simplelist_set_line_count(int lines)
782 if (lines < 0)
783 simplelist_line_count = 0;
784 else if (lines >= SIMPLELIST_MAX_LINES)
785 simplelist_line_count = SIMPLELIST_MAX_LINES;
786 else
787 simplelist_line_count = lines;
789 /* get the current amount of lines shown */
790 int simplelist_get_line_count(void)
792 return simplelist_line_count;
794 /* add/edit a line in the list.
795 if line_number > number of lines shown it adds the line,
796 else it edits the line */
797 void simplelist_addline(int line_number, const char *fmt, ...)
799 va_list ap;
801 if (line_number > simplelist_line_count)
803 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
804 line_number = simplelist_line_count++;
805 else
806 return;
808 va_start(ap, fmt);
809 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
810 va_end(ap);
813 static const char* simplelist_static_getname(int item,
814 void * data,
815 char *buffer,
816 size_t buffer_len)
818 (void)data; (void)buffer; (void)buffer_len;
819 return simplelist_text[item];
822 bool simplelist_show_list(struct simplelist_info *info)
824 struct gui_synclist lists;
825 int action, old_line_count = simplelist_line_count;
826 int oldbars = viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
827 const char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
828 int wrap = LIST_WRAP_UNLESS_HELD;
829 if (info->get_name)
830 getname = info->get_name;
831 else
832 getname = simplelist_static_getname;
833 gui_synclist_init(&lists, getname, info->callback_data,
834 info->scroll_all, info->selection_size, NULL);
836 if (info->title)
837 gui_synclist_set_title(&lists, info->title, NOICON);
838 if (info->get_icon)
839 gui_synclist_set_icon_callback(&lists, info->get_icon);
840 if (info->get_talk)
841 gui_synclist_set_voice_callback(&lists, info->get_talk);
843 if (info->hide_selection)
845 gui_synclist_hide_selection_marker(&lists, true);
846 wrap = LIST_WRAP_OFF;
849 if (info->action_callback)
850 info->action_callback(ACTION_REDRAW, &lists);
852 if (info->get_name == NULL)
853 gui_synclist_set_nb_items(&lists,
854 simplelist_line_count*info->selection_size);
855 else
856 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
858 gui_synclist_select_item(&lists, info->selection);
860 gui_synclist_draw(&lists);
861 gui_synclist_speak_item(&lists);
863 while(1)
865 list_do_action(CONTEXT_STD, info->timeout,
866 &lists, &action, wrap);
868 /* We must yield in this case or no other thread can run */
869 if (info->timeout == TIMEOUT_NOBLOCK)
870 yield();
872 if (info->action_callback)
874 bool stdok = action==ACTION_STD_OK;
875 action = info->action_callback(action, &lists);
876 if (stdok && action == ACTION_STD_CANCEL)
878 /* callback asked us to exit */
879 info->selection = gui_synclist_get_sel_pos(&lists);
880 break;
883 if (info->get_name == NULL)
884 gui_synclist_set_nb_items(&lists,
885 simplelist_line_count*info->selection_size);
887 if (action == ACTION_STD_CANCEL)
889 info->selection = -1;
890 break;
892 else if ((action == ACTION_REDRAW) ||
893 (old_line_count != simplelist_line_count))
895 if (info->get_name == NULL)
897 gui_synclist_set_nb_items(&lists,
898 simplelist_line_count*info->selection_size);
900 gui_synclist_draw(&lists);
901 old_line_count = simplelist_line_count;
903 else if(default_event_handler(action) == SYS_USB_CONNECTED)
904 return true;
906 talk_shutup();
907 viewportmanager_set_statusbar(oldbars);
908 return false;
911 void simplelist_info_init(struct simplelist_info *info, char* title,
912 int count, void* data)
914 info->title = title;
915 info->count = count;
916 info->selection_size = 1;
917 info->hide_selection = false;
918 info->scroll_all = false;
919 info->timeout = HZ/10;
920 info->selection = 0;
921 info->action_callback = NULL;
922 info->get_icon = NULL;
923 info->get_name = NULL;
924 info->get_talk = NULL;
925 info->callback_data = data;