So, I was skimming the irc logs from my 3 day absense and I was pinged about a bug...
[Rockbox.git] / apps / gui / list.c
blob2c849660d660cb3065bb22da437bb68cb0421167
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Kevin Ferrare
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #include "lcd.h"
22 #include "font.h"
23 #include "button.h"
24 #include "sprintf.h"
25 #include "string.h"
26 #include "settings.h"
27 #include "kernel.h"
28 #include "system.h"
30 #include "action.h"
31 #include "screen_access.h"
32 #include "list.h"
33 #include "scrollbar.h"
34 #include "statusbar.h"
35 #include "textarea.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 viewport *parent, 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)
74 viewport_set_defaults(vp, i);
75 else if (list->parent[i] == vp)
77 viewport_set_defaults(vp, i);
78 list->parent[i]->y = global_settings.statusbar?STATUSBAR_HEIGHT:0;
79 list->parent[i]->height = screens[i].height - list->parent[i]->y;
82 #ifdef HAS_BUTTONBAR
83 if (list && (list->parent[0] == &parent[0]) && global_settings.buttonbar)
84 list->parent[0]->height -= BUTTONBAR_HEIGHT;
85 #endif
86 force_list_reinit = false;
88 #else
89 static struct viewport parent[NB_SCREENS] =
91 [SCREEN_MAIN] =
93 .x = 0,
94 .y = 0,
95 .width = LCD_WIDTH,
96 .height = LCD_HEIGHT
99 void list_init_viewports(struct gui_synclist *list)
101 (void)list;
103 #endif
105 #ifdef HAVE_LCD_BITMAP
106 bool list_display_title(struct gui_synclist *list, struct viewport *vp)
108 return list->title != NULL && viewport_get_nb_lines(vp)>2;
110 #else
111 #define list_display_title(l,v) false
112 #endif
115 * Initializes a scrolling list
116 * - gui_list : the list structure to initialize
117 * - callback_get_item_name : pointer to a function that associates a label
118 * to a given item number
119 * - data : extra data passed to the list callback
120 * - scroll_all :
121 * - selected_size :
122 * - parent : the parent viewports to use. NULL means the full screen minus
123 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
125 void gui_synclist_init(struct gui_synclist * gui_list,
126 list_get_name callback_get_item_name,
127 void * data,
128 bool scroll_all,
129 int selected_size, struct viewport list_parent[NB_SCREENS]
132 int i;
133 gui_list->callback_get_item_icon = NULL;
134 gui_list->callback_get_item_name = callback_get_item_name;
135 gui_list->callback_speak_item = NULL;
136 gui_list->nb_items = 0;
137 gui_list->selected_item = 0;
138 FOR_NB_SCREENS(i)
140 gui_list->start_item[i] = 0;
141 gui_list->last_displayed_start_item[i] = -1 ;
142 #ifdef HAVE_LCD_BITMAP
143 gui_list->offset_position[i] = 0;
144 #endif
145 if (list_parent)
146 gui_list->parent[i] = &list_parent[i];
147 else
149 gui_list->parent[i] = &parent[i];
152 list_init_viewports(gui_list);
153 gui_list->limit_scroll = false;
154 gui_list->data=data;
155 gui_list->scroll_all=scroll_all;
156 gui_list->selected_size=selected_size;
157 gui_list->title = NULL;
158 gui_list->title_width = 0;
159 gui_list->title_icon = Icon_NOICON;
161 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
162 gui_list->show_selection_marker = true;
163 gui_list->last_displayed_selected_item = -1 ;
165 #ifdef HAVE_LCD_COLOR
166 gui_list->title_color = -1;
167 gui_list->callback_get_item_color = NULL;
168 #endif
169 force_list_reinit = true;
172 /* this toggles the selection bar or cursor */
173 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
175 lists->show_selection_marker = !hide;
179 #ifdef HAVE_LCD_BITMAP
180 int list_title_height(struct gui_synclist *list, struct viewport *vp);
182 int gui_list_get_item_offset(struct gui_synclist * gui_list, int item_width,
183 int text_pos, struct screen * display, struct viewport *vp)
185 int item_offset;
187 if (offset_out_of_view)
189 item_offset = gui_list->offset_position[display->screen_type];
191 else
193 /* if text is smaller then view */
194 if (item_width <= vp->width - text_pos)
196 item_offset = 0;
198 else
200 /* if text got out of view */
201 if (gui_list->offset_position[display->screen_type] >
202 item_width - (vp->width - text_pos))
203 item_offset = item_width - (vp->width - text_pos);
204 else
205 item_offset = gui_list->offset_position[display->screen_type];
209 return item_offset;
211 #endif
213 * Force a full screen update.
216 void gui_synclist_draw(struct gui_synclist *gui_list)
218 int i;
219 static struct gui_synclist *last_list = NULL;
220 static int last_count = -1;
221 #ifdef HAS_BUTTONBAR
222 static bool last_buttonbar = false;
223 #endif
224 if (force_list_reinit ||
225 #ifdef HAS_BUTTONBAR
226 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
227 #endif
228 last_list != gui_list ||
229 gui_list->nb_items != last_count)
231 list_init_viewports(gui_list);
232 force_list_reinit = false;
234 #ifdef HAS_BUTTONBAR
235 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
236 #endif
237 last_count = gui_list->nb_items;
238 last_list = gui_list;
239 FOR_NB_SCREENS(i)
241 list_draw(&screens[i], gui_list->parent[i], gui_list);
245 /* sets up the list so the selection is shown correctly on the screen */
246 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
247 enum screen_type screen)
249 int nb_lines;
250 int difference = gui_list->selected_item - gui_list->start_item[screen];
251 struct viewport vp = *gui_list->parent[screen];
252 #ifdef HAVE_LCD_BITMAP
253 if (list_display_title(gui_list, gui_list->parent[screen]))
254 vp.height -= list_title_height(gui_list,gui_list->parent[screen]);
255 #endif
256 nb_lines = viewport_get_nb_lines(&vp);
258 /* edge case,, selected last item */
259 if (gui_list->selected_item == gui_list->nb_items -1)
261 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
263 /* selected first item */
264 else if (gui_list->selected_item == 0)
266 gui_list->start_item[screen] = 0;
268 else if (difference < SCROLL_LIMIT) /* list moved up */
270 if (global_settings.scroll_paginated)
272 if (gui_list->start_item[screen] > gui_list->selected_item)
273 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)*nb_lines;
275 else
277 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT;
278 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
279 gui_list->start_item[screen] = MAX(0, temp);
282 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
284 int bottom = gui_list->nb_items - nb_lines;
285 /* always move the screen if selection isnt "visible" */
286 if (gui_list->show_selection_marker == false)
288 if (bottom < 0)
289 bottom = 0;
290 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
291 2*gui_list->selected_size);
293 else if (global_settings.scroll_paginated)
295 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
296 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
298 else
300 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
301 int temp = MAX(0, top_of_screen);
302 gui_list->start_item[screen] = MIN(bottom, temp);
307 * Selects an item in the list
308 * - gui_list : the list structure
309 * - item_number : the number of the item which will be selected
311 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
313 int i;
314 if( item_number > gui_list->nb_items-1 || item_number < 0 )
315 return;
316 gui_list->selected_item = item_number;
317 FOR_NB_SCREENS(i)
318 gui_list_put_selection_on_screen(gui_list, i);
321 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
322 int offset)
324 int new_selection;
325 if (gui_list->selected_size > 1)
327 offset *= gui_list->selected_size;
328 /* always select the first item of multi-line lists */
329 offset -= offset%gui_list->selected_size;
331 new_selection = gui_list->selected_item + offset;
332 if (new_selection >= gui_list->nb_items)
334 gui_list->selected_item = gui_list->limit_scroll ?
335 gui_list->nb_items - gui_list->selected_size : 0;
337 else if (new_selection < 0)
339 gui_list->selected_item = gui_list->limit_scroll ?
340 0 : gui_list->nb_items - gui_list->selected_size;
342 else if (gui_list->show_selection_marker == false)
344 /* NOTE: this part doesnt work as well as it used to, the problem is
345 we want to scroll the lists seperatly but we only have one
346 selected item now, I dont think this is such a big deal though */
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, gui_list->parent[i]))
353 vp.height -= list_title_height(gui_list,gui_list->parent[i]);
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);
364 else
366 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
367 gui_list->selected_size);
370 return;
372 else gui_list->selected_item += offset;
373 gui_synclist_select_item(gui_list, gui_list->selected_item);
377 * Adds an item to the list (the callback will be asked for one more item)
378 * - gui_list : the list structure
380 void gui_synclist_add_item(struct gui_synclist * gui_list)
382 gui_list->nb_items++;
383 /* if only one item in the list, select it */
384 if(gui_list->nb_items == 1)
385 gui_list->selected_item = 0;
389 * Removes an item to the list (the callback will be asked for one less item)
390 * - gui_list : the list structure
392 void gui_synclist_del_item(struct gui_synclist * gui_list)
394 if(gui_list->nb_items > 0)
396 if (gui_list->selected_item == gui_list->nb_items-1)
397 gui_list->selected_item--;
398 gui_list->nb_items--;
399 gui_synclist_select_item(gui_list, gui_list->selected_item);
403 #ifdef HAVE_LCD_BITMAP
404 void gui_list_screen_scroll_step(int ofs)
406 offset_step = ofs;
409 void gui_list_screen_scroll_out_of_view(bool enable)
411 if (enable)
412 offset_out_of_view = true;
413 else
414 offset_out_of_view = false;
416 #endif /* HAVE_LCD_BITMAP */
419 * Set the title and title icon of the list. Setting title to NULL disables
420 * both the title and icon. Use NOICON if there is no icon.
422 void gui_synclist_set_title(struct gui_synclist * gui_list,
423 char * title, enum themable_icons icon)
425 gui_list->title = title;
426 gui_list->title_icon = icon;
427 if (title) {
428 #ifdef HAVE_LCD_BITMAP
429 int i;
430 FOR_NB_SCREENS(i)
431 screens[i].getstringsize(title, &gui_list->title_width, NULL);
432 #else
433 gui_list->title_width = strlen(title);
434 #endif
435 } else {
436 gui_list->title_width = 0;
438 force_list_reinit = true;
442 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
444 #ifdef HAVE_LCD_BITMAP
445 int i;
446 #endif
447 lists->nb_items = nb_items;
448 #ifdef HAVE_LCD_BITMAP
449 FOR_NB_SCREENS(i)
451 lists->offset_position[i] = 0;
453 #endif
455 int gui_synclist_get_nb_items(struct gui_synclist * lists)
457 return lists->nb_items;
459 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
461 return lists->selected_item;
463 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
464 list_get_icon icon_callback)
466 lists->callback_get_item_icon = icon_callback;
469 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
470 list_speak_item voice_callback)
472 lists->callback_speak_item = voice_callback;
475 #ifdef HAVE_LCD_COLOR
476 void gui_synclist_set_color_callback(struct gui_synclist * lists,
477 list_get_color color_callback)
479 lists->callback_get_item_color = color_callback;
481 #endif
483 static void gui_synclist_select_next_page(struct gui_synclist * lists,
484 enum screen_type screen)
486 gui_list_select_at_offset(lists, screens[screen].nb_lines);
489 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
490 enum screen_type screen)
492 gui_list_select_at_offset(lists, -screens[screen].nb_lines);
495 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
497 lists->limit_scroll = scroll;
500 #ifdef HAVE_LCD_BITMAP
502 * Makes all the item in the list scroll by one step to the right.
503 * Should stop increasing the value when reaching the widest item value
504 * in the list.
506 static void gui_synclist_scroll_right(struct gui_synclist * lists)
508 int i;
509 FOR_NB_SCREENS(i)
511 /* FIXME: This is a fake right boundry limiter. there should be some
512 * callback function to find the longest item on the list in pixels,
513 * to stop the list from scrolling past that point */
514 lists->offset_position[i]+=offset_step;
515 if (lists->offset_position[i] > 1000)
516 lists->offset_position[i] = 1000;
521 * Makes all the item in the list scroll by one step to the left.
522 * stops at starting position.
524 static void gui_synclist_scroll_left(struct gui_synclist * lists)
526 int i;
527 FOR_NB_SCREENS(i)
529 lists->offset_position[i]-=offset_step;
530 if (lists->offset_position[i] < 0)
531 lists->offset_position[i] = 0;
534 #endif /* HAVE_LCD_BITMAP */
536 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
538 list_speak_item *cb = lists->callback_speak_item;
539 if(cb && gui_synclist_get_nb_items(lists) != 0)
541 int sel = gui_synclist_get_sel_pos(lists);
542 talk_shutup();
543 /* If we got a repeating key action, or we have just very
544 recently started talking, then we want to stay silent for a
545 while until things settle. Likewise if we already had a
546 pending scheduled announcement not yet due: we need to
547 reschedule it. */
548 if(repeating
549 || (lists->scheduled_talk_tick
550 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
551 || (lists->last_talked_tick
552 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
554 lists->scheduled_talk_tick = current_tick +HZ/4;
555 return;
556 } else {
557 lists->scheduled_talk_tick = 0; /* work done */
558 cb(sel, lists->data);
559 lists->last_talked_tick = current_tick;
563 void gui_synclist_speak_item(struct gui_synclist * lists)
564 /* The list user should call this to speak the first item on entering
565 the list, and whenever the list is updated. */
567 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
568 talk_id(VOICE_EMPTY_LIST, true);
569 else _gui_synclist_speak_item(lists, false);
572 extern intptr_t get_action_data(void);
573 #if defined(HAVE_TOUCHPAD)
574 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
575 unsigned gui_synclist_do_touchpad(struct gui_synclist * gui_list, struct viewport *parent);
576 #endif
578 bool gui_synclist_do_button(struct gui_synclist * lists,
579 unsigned *actionptr, enum list_wrap wrap)
581 int action = *actionptr;
582 #ifdef HAVE_LCD_BITMAP
583 static bool scrolling_left = false;
584 #endif
586 #ifdef HAVE_SCROLLWHEEL
587 int next_item_modifier = button_apply_acceleration(get_action_data());
588 #else
589 static int next_item_modifier = 1;
590 static int last_accel_tick = 0;
592 if (global_settings.list_accel_start_delay)
594 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
595 int accel_wait = global_settings.list_accel_wait * HZ/2;
597 if (get_action_statuscode(NULL)&ACTION_REPEAT)
599 if (!last_accel_tick)
600 last_accel_tick = current_tick + start_delay;
601 else if (current_tick >=
602 last_accel_tick + accel_wait)
604 last_accel_tick = current_tick;
605 next_item_modifier++;
608 else if (last_accel_tick)
610 next_item_modifier = 1;
611 last_accel_tick = 0;
614 #endif
616 #if defined(HAVE_TOUCHPAD)
617 if (action == ACTION_TOUCHPAD)
618 action = *actionptr = gui_synclist_do_touchpad(lists, &parent[SCREEN_MAIN]);
619 #endif
621 switch (wrap)
623 case LIST_WRAP_ON:
624 gui_synclist_limit_scroll(lists, false);
625 break;
626 case LIST_WRAP_OFF:
627 gui_synclist_limit_scroll(lists, true);
628 break;
629 case LIST_WRAP_UNLESS_HELD:
630 if (action == ACTION_STD_PREVREPEAT ||
631 action == ACTION_STD_NEXTREPEAT ||
632 action == ACTION_LISTTREE_PGUP ||
633 action == ACTION_LISTTREE_PGDOWN)
634 gui_synclist_limit_scroll(lists, true);
635 else gui_synclist_limit_scroll(lists, false);
636 break;
639 switch (action)
641 case ACTION_REDRAW:
642 gui_synclist_draw(lists);
643 return true;
645 #ifdef HAVE_VOLUME_IN_LIST
646 case ACTION_LIST_VOLUP:
647 global_settings.volume += 2;
648 /* up two because the falthrough brings it down one */
649 case ACTION_LIST_VOLDOWN:
650 global_settings.volume--;
651 setvol();
652 return true;
653 #endif
654 case ACTION_STD_PREV:
655 case ACTION_STD_PREVREPEAT:
656 gui_list_select_at_offset(lists, -next_item_modifier);
657 #ifndef HAVE_SCROLLWHEEL
658 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
659 #endif
660 gui_synclist_draw(lists);
661 _gui_synclist_speak_item(lists,
662 action == ACTION_STD_PREVREPEAT
663 || next_item_modifier >1);
664 yield();
665 *actionptr = ACTION_STD_PREV;
666 return true;
668 case ACTION_STD_NEXT:
669 case ACTION_STD_NEXTREPEAT:
670 gui_list_select_at_offset(lists, next_item_modifier);
671 #ifndef HAVE_SCROLLWHEEL
672 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
673 #endif
674 gui_synclist_draw(lists);
675 _gui_synclist_speak_item(lists,
676 action == ACTION_STD_NEXTREPEAT
677 || next_item_modifier >1);
678 yield();
679 *actionptr = ACTION_STD_NEXT;
680 return true;
682 #ifdef HAVE_LCD_BITMAP
683 case ACTION_TREE_PGRIGHT:
684 gui_synclist_scroll_right(lists);
685 gui_synclist_draw(lists);
686 return true;
687 case ACTION_TREE_ROOT_INIT:
688 /* After this button press ACTION_TREE_PGLEFT is allowed
689 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
690 keymaps as a repeated button press (the same as the repeated
691 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
692 button press */
693 if (lists->offset_position[0] == 0)
695 scrolling_left = false;
696 *actionptr = ACTION_STD_CANCEL;
697 return true;
699 *actionptr = ACTION_TREE_PGLEFT;
700 case ACTION_TREE_PGLEFT:
701 if(!scrolling_left && (lists->offset_position[0] == 0))
703 *actionptr = ACTION_STD_CANCEL;
704 return false;
706 gui_synclist_scroll_left(lists);
707 gui_synclist_draw(lists);
708 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
709 skipping to root */
710 return true;
711 #endif
712 /* for pgup / pgdown, we are obliged to have a different behaviour depending
713 * on the screen for which the user pressed the key since for example, remote
714 * and main screen doesn't have the same number of lines */
715 case ACTION_LISTTREE_PGUP:
717 int screen =
718 #ifdef HAVE_REMOTE_LCD
719 get_action_statuscode(NULL)&ACTION_REMOTE ?
720 SCREEN_REMOTE :
721 #endif
722 SCREEN_MAIN;
723 gui_synclist_select_previous_page(lists, screen);
724 gui_synclist_draw(lists);
725 _gui_synclist_speak_item(lists, false);
726 yield();
727 *actionptr = ACTION_STD_NEXT;
729 return true;
731 case ACTION_LISTTREE_PGDOWN:
733 int screen =
734 #ifdef HAVE_REMOTE_LCD
735 get_action_statuscode(NULL)&ACTION_REMOTE ?
736 SCREEN_REMOTE :
737 #endif
738 SCREEN_MAIN;
739 gui_synclist_select_next_page(lists, screen);
740 gui_synclist_draw(lists);
741 _gui_synclist_speak_item(lists, false);
742 yield();
743 *actionptr = ACTION_STD_PREV;
745 return true;
747 if(lists->scheduled_talk_tick
748 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
749 /* scheduled postponed item announcement is due */
750 _gui_synclist_speak_item(lists, false);
751 return false;
754 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
755 /* Returns the lowest of timeout or the delay until a postponed
756 scheduled announcement is due (if any). */
758 if(lists->scheduled_talk_tick)
760 long delay = lists->scheduled_talk_tick -current_tick +1;
761 /* +1 because the trigger condition uses TIME_AFTER(), which
762 is implemented as strictly greater than. */
763 if(delay < 0)
764 delay = 0;
765 if(timeout > delay || timeout == TIMEOUT_BLOCK)
766 timeout = delay;
768 return timeout;
771 bool list_do_action(int context, int timeout,
772 struct gui_synclist *lists, int *action,
773 enum list_wrap wrap)
774 /* Combines the get_action() (with possibly overridden timeout) and
775 gui_synclist_do_button() calls. Returns the list action from
776 do_button, and places the action from get_action in *action. */
778 timeout = list_do_action_timeout(lists, timeout);
779 *action = get_action(context, timeout);
780 return gui_synclist_do_button(lists, action, wrap);
783 /* Simple use list implementation */
784 static int simplelist_line_count = 0;
785 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
786 /* set the amount of lines shown in the list */
787 void simplelist_set_line_count(int lines)
789 if (lines < 0)
790 simplelist_line_count = 0;
791 else if (lines >= SIMPLELIST_MAX_LINES)
792 simplelist_line_count = SIMPLELIST_MAX_LINES;
793 else
794 simplelist_line_count = lines;
796 /* get the current amount of lines shown */
797 int simplelist_get_line_count(void)
799 return simplelist_line_count;
801 /* add/edit a line in the list.
802 if line_number > number of lines shown it adds the line, else it edits the line */
803 void simplelist_addline(int line_number, const char *fmt, ...)
805 va_list ap;
807 if (line_number > simplelist_line_count)
809 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
810 line_number = simplelist_line_count++;
811 else
812 return;
814 va_start(ap, fmt);
815 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
816 va_end(ap);
819 static char* simplelist_static_getname(int item,
820 void * data,
821 char *buffer,
822 size_t buffer_len)
824 (void)data; (void)buffer; (void)buffer_len;
825 return simplelist_text[item];
828 bool simplelist_show_list(struct simplelist_info *info)
830 struct gui_synclist lists;
831 struct viewport vp[NB_SCREENS];
832 int action, old_line_count = simplelist_line_count,i;
833 char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
834 if (info->get_name)
835 getname = info->get_name;
836 else
837 getname = simplelist_static_getname;
838 FOR_NB_SCREENS(i)
840 viewport_set_defaults(&vp[i], i);
842 gui_synclist_init(&lists, getname, info->callback_data,
843 info->scroll_all, info->selection_size, vp);
845 if (info->title)
846 gui_synclist_set_title(&lists, info->title, NOICON);
847 if (info->get_icon)
848 gui_synclist_set_icon_callback(&lists, info->get_icon);
849 if (info->get_talk)
850 gui_synclist_set_voice_callback(&lists, info->get_talk);
852 gui_synclist_hide_selection_marker(&lists, info->hide_selection);
854 if (info->action_callback)
855 info->action_callback(ACTION_REDRAW, &lists);
857 if (info->get_name == NULL)
858 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
859 else
860 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
862 gui_synclist_select_item(&lists, info->start_selection);
864 gui_synclist_draw(&lists);
865 gui_synclist_speak_item(&lists);
867 while(1)
869 gui_syncstatusbar_draw(&statusbars, true);
870 list_do_action(CONTEXT_STD, info->timeout,
871 &lists, &action, LIST_WRAP_UNLESS_HELD);
873 /* We must yield in this case or no other thread can run */
874 if (info->timeout == TIMEOUT_NOBLOCK)
875 yield();
877 if (info->action_callback)
879 action = info->action_callback(action, &lists);
880 if (info->get_name == NULL)
881 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
883 if (action == ACTION_STD_CANCEL)
884 break;
885 else if ((action == ACTION_REDRAW) ||
886 (old_line_count != simplelist_line_count))
888 if (info->get_name == NULL)
889 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
890 gui_synclist_draw(&lists);
891 if (action != ACTION_NONE)
892 gui_synclist_speak_item(&lists);
893 old_line_count = simplelist_line_count;
895 else if(default_event_handler(action) == SYS_USB_CONNECTED)
896 return true;
898 talk_shutup();
899 return false;
902 void simplelist_info_init(struct simplelist_info *info, char* title,
903 int count, void* data)
905 info->title = title;
906 info->count = count;
907 info->selection_size = 1;
908 info->hide_selection = false;
909 info->scroll_all = false;
910 info->timeout = HZ/10;
911 info->start_selection = 0;
912 info->action_callback = NULL;
913 info->get_icon = NULL;
914 info->get_name = NULL;
915 info->get_talk = NULL;
916 info->callback_data = data;