Add 2008 to the copyright notice.
[Rockbox.git] / apps / gui / list.c
blobbc21976449aee6687ab9c0f7a2c7da891390bb7d
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"
41 #ifdef HAVE_LCD_CHARCELLS
42 #define SCROLL_LIMIT 1
43 #else
44 #define SCROLL_LIMIT (nb_lines<3?1:2)
45 #endif
47 /* The minimum number of pending button events in queue before starting
48 * to limit list drawing interval.
50 #define FRAMEDROP_TRIGGER 6
52 #ifdef HAVE_LCD_BITMAP
53 static int offset_step = 16; /* pixels per screen scroll step */
54 /* should lines scroll out of the screen */
55 static bool offset_out_of_view = false;
56 #endif
57 static struct gui_synclist* last_list_displayed;
59 #define SHOW_LIST_TITLE ((gui_list->title != NULL) && \
60 (display->nb_lines > 2))
62 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
63 int offset);
66 * Initializes a scrolling list
67 * - gui_list : the list structure to initialize
68 * - callback_get_item_name : pointer to a function that associates a label
69 * to a given item number
70 * - data : extra data passed to the list callback
71 * - scroll_all :
72 * - selected_size :
74 void gui_synclist_init(struct gui_synclist * gui_list,
75 list_get_name callback_get_item_name,
76 void * data,
77 bool scroll_all,
78 int selected_size
81 int i;
82 gui_list->callback_get_item_icon = NULL;
83 gui_list->callback_get_item_name = callback_get_item_name;
84 gui_list->callback_speak_item = NULL;
85 gui_list_set_nb_items(gui_list, 0);
86 gui_list->selected_item = 0;
87 FOR_NB_SCREENS(i)
89 gui_list->start_item[i] = 0;
90 gui_list->last_displayed_start_item[i] = -1 ;
91 #ifdef HAVE_LCD_BITMAP
92 gui_list->offset_position[i] = 0;
93 #endif
95 gui_list->limit_scroll = false;
96 gui_list->data=data;
97 gui_list->scroll_all=scroll_all;
98 gui_list->selected_size=selected_size;
99 gui_list->title = NULL;
100 gui_list->title_width = 0;
101 gui_list->title_icon = Icon_NOICON;
103 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
104 gui_list->show_selection_marker = true;
105 gui_list->last_displayed_selected_item = -1 ;
107 #ifdef HAVE_LCD_COLOR
108 gui_list->title_color = -1;
109 gui_list->callback_get_item_color = NULL;
110 #endif
113 /* this toggles the selection bar or cursor */
114 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
116 lists->show_selection_marker = !hide;
120 #ifdef HAVE_LCD_BITMAP
121 static int gui_list_get_item_offset(struct gui_synclist * gui_list, int item_width,
122 int text_pos, struct screen * display)
124 int item_offset;
126 if (offset_out_of_view)
128 item_offset = gui_list->offset_position[display->screen_type];
130 else
132 /* if text is smaller then view */
133 if (item_width <= display->width - text_pos)
135 item_offset = 0;
137 else
139 /* if text got out of view */
140 if (gui_list->offset_position[display->screen_type] >
141 item_width - (display->width - text_pos))
142 item_offset = item_width - (display->width - text_pos);
143 else
144 item_offset = gui_list->offset_position[display->screen_type];
148 return item_offset;
150 #endif
153 * Draws the list on the attached screen
154 * - gui_list : the list structure
156 static void gui_list_draw_smart(struct gui_synclist *gui_list, struct screen * display)
158 int text_pos;
159 bool draw_icons = (gui_list->callback_get_item_icon != NULL && global_settings.show_icons);
160 bool draw_cursor;
161 int i;
162 int lines;
163 static int last_lines[NB_SCREENS] = {0};
164 #ifdef HAVE_LCD_BITMAP
165 int item_offset;
166 int old_margin = display->getxmargin();
167 #endif
168 int start, end;
169 bool partial_draw = false;
171 #ifdef HAVE_LCD_BITMAP
172 display->setfont(FONT_UI);
173 gui_textarea_update_nblines(display);
174 #endif
175 /* Speed up UI by drawing the changed contents only. */
176 if (gui_list == last_list_displayed
177 && gui_list->last_displayed_start_item[display->screen_type] == gui_list->start_item[display->screen_type]
178 && gui_list->selected_size == 1)
180 partial_draw = true;
183 lines = display->nb_lines - SHOW_LIST_TITLE;
184 if (last_lines[display->screen_type] != lines)
186 gui_list_select_at_offset(gui_list, 0);
187 last_lines[display->screen_type] = lines;
190 if (partial_draw)
192 end = gui_list->last_displayed_selected_item - gui_list->start_item[display->screen_type];
193 i = gui_list->selected_item - gui_list->start_item[display->screen_type];
194 if (i < end )
196 start = i;
197 end++;
199 else
201 start = end;
202 end = i + 1;
205 else
207 gui_textarea_clear(display);
208 start = 0;
209 end = display->nb_lines;
210 gui_list->last_displayed_start_item[display->screen_type] = gui_list->start_item[display->screen_type];
211 last_list_displayed = gui_list;
214 gui_list->last_displayed_selected_item = gui_list->selected_item;
216 /* position and draw the list title & icon */
217 if (SHOW_LIST_TITLE && !partial_draw)
219 if (gui_list->title_icon != NOICON && draw_icons)
221 screen_put_icon(display, 0, 0, gui_list->title_icon);
222 #ifdef HAVE_LCD_BITMAP
223 text_pos = get_icon_width(display->screen_type)+2; /* pixels */
224 #else
225 text_pos = 1; /* chars */
226 #endif
228 else
230 text_pos = 0;
233 #ifdef HAVE_LCD_BITMAP
234 int title_style = STYLE_DEFAULT;
235 #ifdef HAVE_LCD_COLOR
236 if (gui_list->title_color >= 0)
238 title_style |= STYLE_COLORED;
239 title_style |= gui_list->title_color;
241 #endif
242 screen_set_xmargin(display, text_pos); /* margin for title */
243 item_offset = gui_list_get_item_offset(gui_list, gui_list->title_width,
244 text_pos, display);
245 if (item_offset > gui_list->title_width - (display->width - text_pos))
246 display->puts_style_offset(0, 0, gui_list->title,
247 title_style, item_offset);
248 else
249 display->puts_scroll_style_offset(0, 0, gui_list->title,
250 title_style, item_offset);
251 #else
252 display->puts_scroll(text_pos, 0, gui_list->title);
253 #endif
256 /* Adjust the position of icon, cursor, text for the list */
257 #ifdef HAVE_LCD_BITMAP
258 gui_textarea_update_nblines(display);
259 bool draw_scrollbar;
261 draw_scrollbar = (global_settings.scrollbar &&
262 lines < gui_list->nb_items);
264 draw_cursor = !global_settings.cursor_style &&
265 gui_list->show_selection_marker;
266 text_pos = 0; /* here it's in pixels */
267 if(draw_scrollbar || SHOW_LIST_TITLE) /* indent if there's
268 a title */
270 text_pos += SCROLLBAR_WIDTH;
272 if(draw_cursor)
273 text_pos += get_icon_width(display->screen_type) + 2;
275 if(draw_icons)
276 text_pos += get_icon_width(display->screen_type) + 2;
277 #else
278 draw_cursor = true;
279 if(draw_icons)
280 text_pos = 2; /* here it's in chars */
281 else
282 text_pos = 1;
283 #endif
285 #ifdef HAVE_LCD_BITMAP
286 screen_set_xmargin(display, text_pos); /* margin for list */
287 #endif
289 if (SHOW_LIST_TITLE)
291 start++;
292 if (end < display->nb_lines)
293 end++;
296 #ifdef HAVE_LCD_COLOR
297 unsigned char cur_line = 0;
298 #endif
299 for (i = start; i < end; i++)
301 unsigned char *s;
302 char entry_buffer[MAX_PATH];
303 unsigned char *entry_name;
304 int current_item = gui_list->start_item[display->screen_type] +
305 (SHOW_LIST_TITLE ? i-1 : i);
307 /* When there are less items to display than the
308 * current available space on the screen, we stop*/
309 if(current_item >= gui_list->nb_items)
310 break;
311 s = gui_list->callback_get_item_name(current_item,
312 gui_list->data,
313 entry_buffer);
314 entry_name = P2STR(s);
316 #ifdef HAVE_LCD_BITMAP
317 int style = STYLE_DEFAULT;
318 /* position the string at the correct offset place */
319 int item_width,h;
320 display->getstringsize(entry_name, &item_width, &h);
321 item_offset = gui_list_get_item_offset(gui_list, item_width,
322 text_pos, display);
323 #endif
325 #ifdef HAVE_LCD_COLOR
326 /* if the list has a color callback */
327 if (gui_list->callback_get_item_color)
329 int color = gui_list->callback_get_item_color(current_item,
330 gui_list->data);
331 /* if color selected */
332 if (color >= 0)
334 style |= STYLE_COLORED;
335 style |= color;
338 #endif
340 if(gui_list->show_selection_marker &&
341 current_item >= gui_list->selected_item &&
342 current_item < gui_list->selected_item + gui_list->selected_size)
343 {/* The selected item must be displayed scrolling */
344 #ifdef HAVE_LCD_BITMAP
345 if (global_settings.cursor_style == 1
346 #ifdef HAVE_REMOTE_LCD
347 || display->screen_type == SCREEN_REMOTE
348 #endif
351 /* Display inverted-line-style */
352 style |= STYLE_INVERT;
354 #ifdef HAVE_LCD_COLOR
355 else if (global_settings.cursor_style == 2)
357 /* Display colour line selector */
358 style |= STYLE_COLORBAR;
360 else if (global_settings.cursor_style == 3)
362 /* Display gradient line selector */
363 style = STYLE_GRADIENT;
365 /* Make the lcd driver know how many lines the gradient should
366 cover and current line number */
367 /* number of selected lines */
368 style |= NUMLN_PACK(gui_list->selected_size);
369 /* current line number, zero based */
370 style |= CURLN_PACK(cur_line);
371 cur_line++;
373 #endif
374 else /* if (!global_settings.cursor_style) */
376 if (current_item % gui_list->selected_size != 0)
377 draw_cursor = false;
379 /* if the text is smaller than the viewport size */
380 if (item_offset > item_width - (display->width - text_pos))
382 /* don't scroll */
383 display->puts_style_offset(0, i, entry_name,
384 style, item_offset);
386 else
388 display->puts_scroll_style_offset(0, i, entry_name,
389 style, item_offset);
391 #else
392 display->puts_scroll(text_pos, i, entry_name);
393 #endif
395 if (draw_cursor)
397 screen_put_icon_with_offset(display, 0, i,
398 (draw_scrollbar || SHOW_LIST_TITLE)?
399 SCROLLBAR_WIDTH: 0,
400 0, Icon_Cursor);
403 else
404 {/* normal item */
405 if(gui_list->scroll_all)
407 #ifdef HAVE_LCD_BITMAP
408 display->puts_scroll_style_offset(0, i, entry_name,
409 style, item_offset);
410 #else
411 display->puts_scroll(text_pos, i, entry_name);
412 #endif
414 else
416 #ifdef HAVE_LCD_BITMAP
417 display->puts_style_offset(0, i, entry_name,
418 style, item_offset);
419 #else
420 display->puts(text_pos, i, entry_name);
421 #endif
424 /* Icons display */
425 if(draw_icons)
427 enum themable_icons icon;
428 icon = gui_list->callback_get_item_icon(current_item, gui_list->data);
429 if(icon > Icon_NOICON)
431 #ifdef HAVE_LCD_BITMAP
432 int x = draw_cursor?1:0;
433 int x_off = (draw_scrollbar || SHOW_LIST_TITLE) ? SCROLLBAR_WIDTH: 0;
434 screen_put_icon_with_offset(display, x, i,
435 x_off, 0, icon);
436 #else
437 screen_put_icon(display, 1, i, icon);
438 #endif
443 #ifdef HAVE_LCD_BITMAP
444 /* Draw the scrollbar if needed*/
445 if(draw_scrollbar)
447 int y_start = gui_textarea_get_ystart(display);
448 if (SHOW_LIST_TITLE)
449 y_start += display->char_height;
450 int scrollbar_y_end = display->char_height *
451 lines + y_start;
452 gui_scrollbar_draw(display, 0, y_start, SCROLLBAR_WIDTH-1,
453 scrollbar_y_end - y_start, gui_list->nb_items,
454 gui_list->start_item[display->screen_type],
455 gui_list->start_item[display->screen_type] + lines, VERTICAL);
458 screen_set_xmargin(display, old_margin);
459 #endif
461 gui_textarea_update(display);
465 * Force a full screen update.
467 void gui_synclist_draw(struct gui_synclist *gui_list)
469 int i;
470 FOR_NB_SCREENS(i)
472 last_list_displayed = NULL;
473 gui_list_draw_smart(gui_list, &screens[i]);
479 /* sets up the list so the selection is shown correctly on the screen */
480 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
481 enum screen_type screen)
483 struct screen *display = &screens[screen];
484 int nb_lines = display->nb_lines - SHOW_LIST_TITLE;
485 int difference = gui_list->selected_item - gui_list->start_item[screen];
487 /* edge case,, selected last item */
488 if (gui_list->selected_item == gui_list->nb_items -1)
490 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
492 /* selected first item */
493 else if (gui_list->selected_item == 0)
495 gui_list->start_item[screen] = 0;
497 else if (difference < SCROLL_LIMIT) /* list moved up */
499 if (global_settings.scroll_paginated)
501 if (gui_list->start_item[screen] > gui_list->selected_item)
502 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)*nb_lines;
504 else
506 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT;
507 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
508 gui_list->start_item[screen] = MAX(0, temp);
511 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
513 int bottom = gui_list->nb_items - nb_lines;
514 /* always move the screen if selection isnt "visible" */
515 if (gui_list->show_selection_marker == false)
517 if (bottom < 0)
518 bottom = 0;
519 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
520 2*gui_list->selected_size);
522 else if (global_settings.scroll_paginated)
524 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
525 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
527 else
529 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
530 int temp = MAX(0, top_of_screen);
531 gui_list->start_item[screen] = MIN(bottom, temp);
536 * Selects an item in the list
537 * - gui_list : the list structure
538 * - item_number : the number of the item which will be selected
540 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
542 int i;
543 if( item_number > gui_list->nb_items-1 || item_number < 0 )
544 return;
545 gui_list->selected_item = item_number;
546 FOR_NB_SCREENS(i)
547 gui_list_put_selection_on_screen(gui_list, i);
550 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
551 int offset)
553 int new_selection;
554 if (gui_list->selected_size > 1)
556 offset *= gui_list->selected_size;
557 /* always select the first item of multi-line lists */
558 offset -= offset%gui_list->selected_size;
560 new_selection = gui_list->selected_item + offset;
561 if (new_selection >= gui_list->nb_items)
563 gui_list->selected_item = gui_list->limit_scroll ?
564 gui_list->nb_items - gui_list->selected_size : 0;
566 else if (new_selection < 0)
568 gui_list->selected_item = gui_list->limit_scroll ?
569 0 : gui_list->nb_items - gui_list->selected_size;
571 else if (gui_list->show_selection_marker == false)
573 /* NOTE: this part doesnt work as well as it used to, the problem is
574 we want to scroll the lists seperatly but we only have one
575 selected item now, I dont think this is such a big deal though */
576 int i, nb_lines, screen_top;
577 FOR_NB_SCREENS(i)
579 struct screen *display = &screens[i];
580 nb_lines = display->nb_lines - SHOW_LIST_TITLE;
581 if (offset > 0)
583 screen_top = gui_list->nb_items-nb_lines;
584 if (screen_top < 0)
585 screen_top = 0;
586 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
587 gui_list->selected_size);
589 else
591 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
592 gui_list->selected_size);
595 return;
597 else gui_list->selected_item += offset;
598 gui_synclist_select_item(gui_list, gui_list->selected_item);
602 * Adds an item to the list (the callback will be asked for one more item)
603 * - gui_list : the list structure
605 void gui_synclist_add_item(struct gui_synclist * gui_list)
607 gui_list->nb_items++;
608 /* if only one item in the list, select it */
609 if(gui_list->nb_items == 1)
610 gui_list->selected_item = 0;
614 * Removes an item to the list (the callback will be asked for one less item)
615 * - gui_list : the list structure
617 void gui_synclist_del_item(struct gui_synclist * gui_list)
619 int i;
620 if(gui_list->nb_items > 0)
622 if (gui_list->selected_item == gui_list->nb_items-1)
623 gui_list->selected_item--;
624 FOR_NB_SCREENS(i)
626 gui_textarea_update_nblines(&screens[i]);
627 int nb_lines = screens[i].nb_lines;
628 int dist_start_from_end = gui_list->nb_items
629 - gui_list->start_item[i] - 1;
631 /* scroll the list if needed */
632 if( (dist_start_from_end < nb_lines) && (gui_list->start_item[i] != 0) )
633 gui_list->start_item[i]--;
635 gui_list->nb_items--;
639 #ifdef HAVE_LCD_BITMAP
640 void gui_list_screen_scroll_step(int ofs)
642 offset_step = ofs;
645 void gui_list_screen_scroll_out_of_view(bool enable)
647 if (enable)
648 offset_out_of_view = true;
649 else
650 offset_out_of_view = false;
652 #endif /* HAVE_LCD_BITMAP */
655 * Set the title and title icon of the list. Setting title to NULL disables
656 * both the title and icon. Use NOICON if there is no icon.
658 void gui_synclist_set_title(struct gui_synclist * gui_list,
659 char * title, enum themable_icons icon)
661 gui_list->title = title;
662 gui_list->title_icon = icon;
663 if (title) {
664 #ifdef HAVE_LCD_BITMAP
665 int i;
666 FOR_NB_SCREENS(i)
667 screens[i].getstringsize(title, &gui_list->title_width, NULL);
668 #else
669 gui_list->title_width = strlen(title);
670 #endif
671 } else {
672 gui_list->title_width = 0;
677 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
679 #ifdef HAVE_LCD_BITMAP
680 int i;
681 #endif
682 lists->nb_items = nb_items;
683 #ifdef HAVE_LCD_BITMAP
684 FOR_NB_SCREENS(i)
686 lists->offset_position[i] = 0;
688 #endif
690 int gui_synclist_get_nb_items(struct gui_synclist * lists)
692 return lists->nb_items;
694 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
696 return lists->selected_item;
698 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
699 list_get_icon icon_callback)
701 lists->callback_get_item_icon = icon_callback;
704 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
705 list_speak_item voice_callback)
707 lists->callback_speak_item = voice_callback;
710 static void gui_synclist_select_next_page(struct gui_synclist * lists,
711 enum screen_type screen)
713 gui_list_select_at_offset(lists, screens[screen].nb_lines);
716 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
717 enum screen_type screen)
719 gui_list_select_at_offset(lists, -screens[screen].nb_lines);
722 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
724 lists->limit_scroll = scroll;
727 #ifdef HAVE_LCD_BITMAP
729 * Makes all the item in the list scroll by one step to the right.
730 * Should stop increasing the value when reaching the widest item value
731 * in the list.
733 static void gui_synclist_scroll_right(struct gui_synclist * lists)
735 int i;
736 FOR_NB_SCREENS(i)
738 /* FIXME: This is a fake right boundry limiter. there should be some
739 * callback function to find the longest item on the list in pixels,
740 * to stop the list from scrolling past that point */
741 lists->offset_position[i]+=offset_step;
742 if (lists->offset_position[i] > 1000)
743 lists->offset_position[i] = 1000;
748 * Makes all the item in the list scroll by one step to the left.
749 * stops at starting position.
751 static void gui_synclist_scroll_left(struct gui_synclist * lists)
753 int i;
754 FOR_NB_SCREENS(i)
756 lists->offset_position[i]-=offset_step;
757 if (lists->offset_position[i] < 0)
758 lists->offset_position[i] = 0;
761 #endif /* HAVE_LCD_BITMAP */
763 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
765 list_speak_item *cb = lists->callback_speak_item;
766 if(cb && gui_synclist_get_nb_items(lists) != 0)
768 int sel = gui_synclist_get_sel_pos(lists);
769 talk_shutup();
770 /* If we got a repeating key action, or we have just very
771 recently started talking, then we want to stay silent for a
772 while until things settle. Likewise if we already had a
773 pending scheduled announcement not yet due: we need to
774 reschedule it. */
775 if(repeating
776 || (lists->scheduled_talk_tick
777 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
778 || (lists->last_talked_tick
779 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
781 lists->scheduled_talk_tick = current_tick +HZ/4;
782 return;
783 } else {
784 lists->scheduled_talk_tick = 0; /* work done */
785 cb(sel, lists->data);
786 lists->last_talked_tick = current_tick;
790 void gui_synclist_speak_item(struct gui_synclist * lists)
791 /* The list user should call this to speak the first item on entering
792 the list, and whenever the list is updated. */
794 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
795 talk_id(VOICE_EMPTY_LIST, true);
796 else _gui_synclist_speak_item(lists, false);
799 extern intptr_t get_action_data(void);
801 #if defined(HAVE_TOUCHPAD)
802 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
803 unsigned gui_synclist_do_touchpad(struct gui_synclist * gui_list)
805 short x,y;
806 unsigned button = action_get_touchpad_press(&x, &y);
807 int line;
808 struct screen *display = &screens[SCREEN_MAIN];
809 if (button == BUTTON_NONE)
810 return ACTION_NONE;
811 if (x<SCROLLBAR_WIDTH)
813 /* top left corner is hopefully GO_TO_ROOT */
814 if (y<STATUSBAR_HEIGHT)
816 if (button == BUTTON_REL)
817 return ACTION_STD_MENU;
818 else if (button == BUTTON_REPEAT)
819 return ACTION_STD_CONTEXT;
820 else
821 return ACTION_NONE;
823 /* scroll bar */
824 else
826 int new_selection, nb_lines;
827 int height, size;
828 nb_lines = display->nb_lines - SHOW_LIST_TITLE;
829 if (nb_lines < gui_list->nb_items)
831 height = nb_lines * display->char_height;
832 size = height*nb_lines / gui_list->nb_items;
833 new_selection = (y*(gui_list->nb_items-nb_lines))/(height-size);
834 gui_synclist_select_item(gui_list, new_selection);
835 nb_lines /= 2;
836 if (new_selection - gui_list->start_item[SCREEN_MAIN] > nb_lines)
838 new_selection = gui_list->start_item[SCREEN_MAIN]+nb_lines;
840 gui_list->start_item[SCREEN_MAIN] = new_selection;
841 return ACTION_REDRAW;
845 else
847 if (button != BUTTON_REL && button != BUTTON_REPEAT)
849 if (global_settings.statusbar)
850 y -= STATUSBAR_HEIGHT;
851 if (SHOW_LIST_TITLE)
852 y -= display->char_height;
853 line = y / display->char_height;
854 if (line != gui_list->selected_item - gui_list->start_item[SCREEN_MAIN])
855 gui_synclist_select_item(gui_list, gui_list->start_item[SCREEN_MAIN]+line);
856 return ACTION_REDRAW;
858 /* title or statusbar is cancel */
859 if (global_settings.statusbar)
861 if (y < STATUSBAR_HEIGHT && !SHOW_LIST_TITLE )
862 return ACTION_STD_CANCEL;
863 y -= STATUSBAR_HEIGHT;
865 /* title goes up one level */
866 if (SHOW_LIST_TITLE)
868 if (y < display->char_height)
869 return ACTION_STD_CANCEL;
870 y -= display->char_height;
872 /* pressing an item will select it.
873 pressing the selected item will "enter" it */
874 line = y / display->char_height;
875 if (line != gui_list->selected_item - gui_list->start_item[SCREEN_MAIN])
877 if (gui_list->start_item[SCREEN_MAIN]+line > gui_list->nb_items)
878 return ACTION_NONE;
879 gui_synclist_select_item(gui_list, gui_list->start_item[SCREEN_MAIN]+line);
882 if (button == BUTTON_REPEAT)
883 return ACTION_STD_CONTEXT;
884 else
885 return ACTION_STD_OK;
887 return ACTION_NONE;
889 #endif
891 bool gui_synclist_do_button(struct gui_synclist * lists,
892 unsigned *actionptr, enum list_wrap wrap)
894 int action = *actionptr;
895 #ifdef HAVE_LCD_BITMAP
896 static bool scrolling_left = false;
897 #endif
899 #ifdef HAVE_SCROLLWHEEL
900 int next_item_modifier = button_apply_acceleration(get_action_data());
901 #else
902 static int next_item_modifier = 1;
903 static int last_accel_tick = 0;
905 if (global_settings.list_accel_start_delay)
907 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
908 int accel_wait = global_settings.list_accel_wait * HZ/2;
910 if (get_action_statuscode(NULL)&ACTION_REPEAT)
912 if (!last_accel_tick)
913 last_accel_tick = current_tick + start_delay;
914 else if (current_tick >=
915 last_accel_tick + accel_wait)
917 last_accel_tick = current_tick;
918 next_item_modifier++;
921 else if (last_accel_tick)
923 next_item_modifier = 1;
924 last_accel_tick = 0;
927 #endif
929 #if defined(HAVE_TOUCHPAD)
930 if (action == ACTION_TOUCHPAD)
931 action = *actionptr = gui_synclist_do_touchpad(lists);
932 #endif
934 switch (wrap)
936 case LIST_WRAP_ON:
937 gui_synclist_limit_scroll(lists, false);
938 break;
939 case LIST_WRAP_OFF:
940 gui_synclist_limit_scroll(lists, true);
941 break;
942 case LIST_WRAP_UNLESS_HELD:
943 if (action == ACTION_STD_PREVREPEAT ||
944 action == ACTION_STD_NEXTREPEAT ||
945 action == ACTION_LISTTREE_PGUP ||
946 action == ACTION_LISTTREE_PGDOWN)
947 gui_synclist_limit_scroll(lists, true);
948 else gui_synclist_limit_scroll(lists, false);
949 break;
952 switch (action)
954 case ACTION_REDRAW:
955 gui_synclist_draw(lists);
956 return true;
958 #ifdef HAVE_VOLUME_IN_LIST
959 case ACTION_LIST_VOLUP:
960 global_settings.volume += 2;
961 /* up two because the falthrough brings it down one */
962 case ACTION_LIST_VOLDOWN:
963 global_settings.volume--;
964 setvol();
965 return true;
966 #endif
967 case ACTION_STD_PREV:
968 case ACTION_STD_PREVREPEAT:
969 gui_list_select_at_offset(lists, -next_item_modifier);
970 #ifndef HAVE_SCROLLWHEEL
971 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
972 #endif
973 gui_synclist_draw(lists);
974 _gui_synclist_speak_item(lists,
975 action == ACTION_STD_PREVREPEAT
976 || next_item_modifier >1);
977 yield();
978 *actionptr = ACTION_STD_PREV;
979 return true;
981 case ACTION_STD_NEXT:
982 case ACTION_STD_NEXTREPEAT:
983 gui_list_select_at_offset(lists, next_item_modifier);
984 #ifndef HAVE_SCROLLWHEEL
985 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
986 #endif
987 gui_synclist_draw(lists);
988 _gui_synclist_speak_item(lists,
989 action == ACTION_STD_NEXTREPEAT
990 || next_item_modifier >1);
991 yield();
992 *actionptr = ACTION_STD_NEXT;
993 return true;
995 #ifdef HAVE_LCD_BITMAP
996 case ACTION_TREE_PGRIGHT:
997 gui_synclist_scroll_right(lists);
998 gui_synclist_draw(lists);
999 return true;
1000 case ACTION_TREE_ROOT_INIT:
1001 /* After this button press ACTION_TREE_PGLEFT is allowed
1002 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
1003 keymaps as a repeated button press (the same as the repeated
1004 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
1005 button press */
1006 if (lists->offset_position[0] == 0)
1008 scrolling_left = false;
1009 *actionptr = ACTION_STD_CANCEL;
1010 return true;
1012 *actionptr = ACTION_TREE_PGLEFT;
1013 case ACTION_TREE_PGLEFT:
1014 if(!scrolling_left && (lists->offset_position[0] == 0))
1016 *actionptr = ACTION_STD_CANCEL;
1017 return false;
1019 gui_synclist_scroll_left(lists);
1020 gui_synclist_draw(lists);
1021 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
1022 skipping to root */
1023 return true;
1024 #endif
1025 /* for pgup / pgdown, we are obliged to have a different behaviour depending
1026 * on the screen for which the user pressed the key since for example, remote
1027 * and main screen doesn't have the same number of lines */
1028 case ACTION_LISTTREE_PGUP:
1030 int screen =
1031 #ifdef HAVE_REMOTE_LCD
1032 get_action_statuscode(NULL)&ACTION_REMOTE ?
1033 SCREEN_REMOTE :
1034 #endif
1035 SCREEN_MAIN;
1036 gui_synclist_select_previous_page(lists, screen);
1037 gui_synclist_draw(lists);
1038 _gui_synclist_speak_item(lists, false);
1039 yield();
1040 *actionptr = ACTION_STD_NEXT;
1042 return true;
1044 case ACTION_LISTTREE_PGDOWN:
1046 int screen =
1047 #ifdef HAVE_REMOTE_LCD
1048 get_action_statuscode(NULL)&ACTION_REMOTE ?
1049 SCREEN_REMOTE :
1050 #endif
1051 SCREEN_MAIN;
1052 gui_synclist_select_next_page(lists, screen);
1053 gui_synclist_draw(lists);
1054 _gui_synclist_speak_item(lists, false);
1055 yield();
1056 *actionptr = ACTION_STD_PREV;
1058 return true;
1060 if(lists->scheduled_talk_tick
1061 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
1062 /* scheduled postponed item announcement is due */
1063 _gui_synclist_speak_item(lists, false);
1064 return false;
1067 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
1068 /* Returns the lowest of timeout or the delay until a postponed
1069 scheduled announcement is due (if any). */
1071 if(lists->scheduled_talk_tick)
1073 long delay = lists->scheduled_talk_tick -current_tick +1;
1074 /* +1 because the trigger condition uses TIME_AFTER(), which
1075 is implemented as strictly greater than. */
1076 if(delay < 0)
1077 delay = 0;
1078 if(timeout > delay || timeout == TIMEOUT_BLOCK)
1079 timeout = delay;
1081 return timeout;
1084 bool list_do_action(int context, int timeout,
1085 struct gui_synclist *lists, int *action,
1086 enum list_wrap wrap)
1087 /* Combines the get_action() (with possibly overridden timeout) and
1088 gui_synclist_do_button() calls. Returns the list action from
1089 do_button, and places the action from get_action in *action. */
1091 timeout = list_do_action_timeout(lists, timeout);
1092 *action = get_action(context, timeout);
1093 return gui_synclist_do_button(lists, action, wrap);
1096 /* Simple use list implementation */
1097 static int simplelist_line_count = 0;
1098 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
1099 /* set the amount of lines shown in the list */
1100 void simplelist_set_line_count(int lines)
1102 if (lines < 0)
1103 lines = 0;
1104 else if (lines > SIMPLELIST_MAX_LINES)
1105 lines = SIMPLELIST_MAX_LINES;
1106 simplelist_line_count = 0;
1108 /* get the current amount of lines shown */
1109 int simplelist_get_line_count(void)
1111 return simplelist_line_count;
1113 /* add/edit a line in the list.
1114 if line_number > number of lines shown it adds the line, else it edits the line */
1115 void simplelist_addline(int line_number, const char *fmt, ...)
1117 va_list ap;
1119 if (line_number > simplelist_line_count)
1121 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
1122 line_number = simplelist_line_count++;
1123 else
1124 return;
1126 va_start(ap, fmt);
1127 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
1128 va_end(ap);
1131 static char* simplelist_static_getname(int item, void * data, char *buffer)
1133 (void)data; (void)buffer;
1134 return simplelist_text[item];
1136 bool simplelist_show_list(struct simplelist_info *info)
1138 struct gui_synclist lists;
1139 int action, old_line_count = simplelist_line_count;
1140 char* (*getname)(int item, void * data, char *buffer);
1141 if (info->get_name)
1142 getname = info->get_name;
1143 else
1144 getname = simplelist_static_getname;
1145 gui_synclist_init(&lists, getname, info->callback_data,
1146 info->scroll_all, info->selection_size);
1147 if (info->title)
1148 gui_synclist_set_title(&lists, info->title, NOICON);
1149 if (info->get_icon)
1150 gui_synclist_set_icon_callback(&lists, info->get_icon);
1151 if (info->get_talk)
1152 gui_synclist_set_voice_callback(&lists, info->get_talk);
1154 gui_synclist_hide_selection_marker(&lists, info->hide_selection);
1156 if (info->action_callback)
1157 info->action_callback(ACTION_REDRAW, &lists);
1159 if (info->get_name == NULL)
1160 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
1161 else
1162 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
1164 gui_synclist_select_item(&lists, info->start_selection);
1166 gui_synclist_draw(&lists);
1167 gui_synclist_speak_item(&lists);
1169 while(1)
1171 gui_syncstatusbar_draw(&statusbars, true);
1172 list_do_action(CONTEXT_STD, info->timeout,
1173 &lists, &action, LIST_WRAP_UNLESS_HELD);
1175 /* We must yield in this case or no other thread can run */
1176 if (info->timeout == TIMEOUT_NOBLOCK)
1177 yield();
1179 if (info->action_callback)
1181 action = info->action_callback(action, &lists);
1182 if (info->get_name == NULL)
1183 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
1185 if (action == ACTION_STD_CANCEL)
1186 break;
1187 else if ((action == ACTION_REDRAW) ||
1188 (old_line_count != simplelist_line_count))
1190 if (info->get_name == NULL)
1191 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
1192 gui_synclist_draw(&lists);
1193 if (action != ACTION_NONE)
1194 gui_synclist_speak_item(&lists);
1195 old_line_count = simplelist_line_count;
1197 else if(default_event_handler(action) == SYS_USB_CONNECTED)
1198 return true;
1200 talk_shutup();
1201 return false;
1204 void simplelist_info_init(struct simplelist_info *info, char* title,
1205 int count, void* data)
1207 info->title = title;
1208 info->count = count;
1209 info->selection_size = 1;
1210 info->hide_selection = false;
1211 info->scroll_all = false;
1212 info->timeout = HZ/10;
1213 info->start_selection = 0;
1214 info->action_callback = NULL;
1215 info->get_icon = NULL;
1216 info->get_name = NULL;
1217 info->get_talk = NULL;
1218 info->callback_data = data;