list: improvement to move and show selection in multi-line lists.
[kugel-rb.git] / apps / gui / bitmap / list.c
blob7680c876aa7d62b4f13de1ca14cd8ce71e444ba9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Jonathan Gordon
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 /* This file contains the code to draw the list widget on BITMAP LCDs. */
24 #include "config.h"
25 #include "lcd.h"
26 #include "font.h"
27 #include "button.h"
28 #include "sprintf.h"
29 #include "string.h"
30 #include "settings.h"
31 #include "kernel.h"
32 #include "system.h"
33 #include "file.h"
35 #include "action.h"
36 #include "screen_access.h"
37 #include "list.h"
38 #include "scrollbar.h"
39 #include "lang.h"
40 #include "sound.h"
41 #include "misc.h"
42 #include "viewport.h"
44 #define ICON_PADDING 1
46 /* these are static to make scrolling work */
47 static struct viewport list_text[NB_SCREENS], title_text[NB_SCREENS];
49 int gui_list_get_item_offset(struct gui_synclist * gui_list, int item_width,
50 int text_pos, struct screen * display,
51 struct viewport *vp);
52 bool list_display_title(struct gui_synclist *list, enum screen_type screen);
54 /* Draw the list...
55 internal screen layout:
56 -----------------
57 |TI| title | TI is title icon
58 -----------------
59 | | | |
60 |S|I| | S - scrollbar
61 | | | items | I - icons
62 | | | |
63 ------------------
65 Note: This image is flipped horizontally when the language is a
66 right-to-left one (Hebrew, Arabic)
68 static bool draw_title(struct screen *display, struct gui_synclist *list)
70 const int screen = display->screen_type;
71 int style = STYLE_DEFAULT;
72 struct viewport *title_text_vp = &title_text[screen];
74 display->scroll_stop(title_text_vp);
75 if (!list_display_title(list, screen))
76 return false;
77 *title_text_vp = *(list->parent[screen]);
78 title_text_vp->height = font_get(title_text_vp->font)->height;
80 if (list->title_icon != Icon_NOICON && global_settings.show_icons)
82 struct viewport title_icon = *title_text_vp;
84 title_icon.width = get_icon_width(screen) + ICON_PADDING * 2;
85 if (VP_IS_RTL(&title_icon))
87 title_icon.x += title_text_vp->width - title_icon.width;
89 else
91 title_text_vp->x += title_icon.width;
93 title_text_vp->width -= title_icon.width;
95 display->set_viewport(&title_icon);
96 screen_put_icon(display, 0, 0, list->title_icon);
98 #ifdef HAVE_LCD_COLOR
99 if (list->title_color >= 0)
101 style |= (STYLE_COLORED|list->title_color);
103 #endif
104 display->set_viewport(title_text_vp);
105 display->puts_scroll_style(0, 0, list->title, style);
106 return true;
109 void list_draw(struct screen *display, struct gui_synclist *list)
111 struct viewport list_icons;
112 int start, end, line_height, style, i;
113 const int screen = display->screen_type;
114 const int list_start_item = list->start_item[screen];
115 const int icon_width = get_icon_width(screen) + ICON_PADDING;
116 const bool scrollbar_in_left = (global_settings.scrollbar == SCROLLBAR_LEFT);
117 const bool show_cursor = !global_settings.cursor_style &&
118 list->show_selection_marker;
119 struct viewport *parent = (list->parent[screen]);
120 #ifdef HAVE_LCD_COLOR
121 unsigned char cur_line = 0;
122 #endif
123 int item_offset;
124 bool show_title;
125 struct viewport *list_text_vp = &list_text[screen];
127 line_height = font_get(parent->font)->height;
128 display->set_viewport(parent);
129 display->clear_viewport();
130 display->scroll_stop(list_text_vp);
131 *list_text_vp = *parent;
132 if ((show_title = draw_title(display, list)))
134 list_text_vp->y += line_height;
135 list_text_vp->height -= line_height;
138 start = list_start_item;
139 end = start + viewport_get_nb_lines(list_text_vp);
141 /* draw the scrollbar if its needed */
142 if (global_settings.scrollbar &&
143 viewport_get_nb_lines(list_text_vp) < list->nb_items)
145 struct viewport vp;
146 vp = *list_text_vp;
147 vp.width = SCROLLBAR_WIDTH;
148 vp.height = line_height * viewport_get_nb_lines(list_text_vp);
149 vp.x = parent->x;
150 list_text_vp->width -= SCROLLBAR_WIDTH;
151 if (scrollbar_in_left)
152 list_text_vp->x += SCROLLBAR_WIDTH;
153 else
154 vp.x += list_text_vp->width;
155 display->set_viewport(&vp);
156 gui_scrollbar_draw(display,
157 (scrollbar_in_left? 0: 1), 0, SCROLLBAR_WIDTH-1, vp.height,
158 list->nb_items, list_start_item, list_start_item + end-start,
159 VERTICAL);
161 else if (show_title)
163 /* shift everything a bit in relation to the title... */
164 if (!VP_IS_RTL(list_text_vp) && scrollbar_in_left)
166 list_text_vp->width -= SCROLLBAR_WIDTH;
167 list_text_vp->x += SCROLLBAR_WIDTH;
169 else if (VP_IS_RTL(list_text_vp) && !scrollbar_in_left)
171 list_text_vp->width -= SCROLLBAR_WIDTH;
175 /* setup icon placement */
176 list_icons = *list_text_vp;
177 int icon_count = (list->callback_get_item_icon != NULL) ? 1 : 0;
178 if (show_cursor)
179 icon_count++;
180 if (icon_count)
182 list_icons.width = icon_width * icon_count;
183 list_text_vp->width -= list_icons.width + ICON_PADDING;
184 if (VP_IS_RTL(&list_icons))
185 list_icons.x += list_text_vp->width + ICON_PADDING;
186 else
187 list_text_vp->x += list_icons.width + ICON_PADDING;
190 for (i=start; i<end && i<list->nb_items; i++)
192 /* do the text */
193 unsigned const char *s;
194 char entry_buffer[MAX_PATH];
195 unsigned char *entry_name;
196 int text_pos = 0;
197 s = list->callback_get_item_name(i, list->data, entry_buffer,
198 sizeof(entry_buffer));
199 entry_name = P2STR(s);
200 display->set_viewport(list_text_vp);
201 style = STYLE_DEFAULT;
202 /* position the string at the correct offset place */
203 int item_width,h;
204 display->getstringsize(entry_name, &item_width, &h);
205 item_offset = gui_list_get_item_offset(list, item_width, text_pos,
206 display, list_text_vp);
208 #ifdef HAVE_LCD_COLOR
209 /* if the list has a color callback */
210 if (list->callback_get_item_color)
212 int color = list->callback_get_item_color(i, list->data);
213 /* if color selected */
214 if (color >= 0)
216 style |= STYLE_COLORED|color;
219 #endif
220 if(i >= list->selected_item && i < list->selected_item
221 + list->selected_size && list->show_selection_marker)
222 {/* The selected item must be displayed scrolling */
223 if (global_settings.cursor_style == 1
224 #ifdef HAVE_REMOTE_LCD
225 /* the global_settings.cursor_style check is here to make
226 * sure if they want the cursor instead of bar it will work
228 || (display->depth < 16 && global_settings.cursor_style)
229 #endif
232 /* Display inverted-line-style */
233 style = STYLE_INVERT;
235 #ifdef HAVE_LCD_COLOR
236 else if (global_settings.cursor_style == 2)
238 /* Display colour line selector */
239 style = STYLE_COLORBAR;
241 else if (global_settings.cursor_style == 3)
243 /* Display gradient line selector */
244 style = STYLE_GRADIENT;
246 /* Make the lcd driver know how many lines the gradient should
247 cover and current line number */
248 /* number of selected lines */
249 style |= NUMLN_PACK(list->selected_size);
250 /* current line number, zero based */
251 style |= CURLN_PACK(cur_line);
252 cur_line++;
254 #endif
255 /* if the text is smaller than the viewport size */
256 if (item_offset> item_width - (list_text_vp->width - text_pos))
258 /* don't scroll */
259 display->puts_style_offset(0, i-start, entry_name,
260 style, item_offset);
262 else
264 display->puts_scroll_style_offset(0, i-start, entry_name,
265 style, item_offset);
268 else
270 if (list->scroll_all)
271 display->puts_scroll_style_offset(0, i-start, entry_name,
272 style, item_offset);
273 else
274 display->puts_style_offset(0, i-start, entry_name,
275 style, item_offset);
277 /* do the icon */
278 display->set_viewport(&list_icons);
279 if (list->callback_get_item_icon && global_settings.show_icons)
281 screen_put_icon_with_offset(display, show_cursor?1:0,
282 (i-start),show_cursor?ICON_PADDING:0,0,
283 list->callback_get_item_icon(i, list->data));
285 if (show_cursor && i >= list->selected_item &&
286 i < list->selected_item + list->selected_size)
288 screen_put_icon(display, 0, i-start, Icon_Cursor);
291 display->set_viewport(parent);
292 display->update_viewport();
293 display->set_viewport(NULL);
296 #if defined(HAVE_TOUCHSCREEN)
297 /* This needs to be fixed if we ever get more than 1 touchscreen on a target. */
298 static bool scrolling=false;
300 static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
301 int y)
303 const int screen = screens[SCREEN_MAIN].screen_type;
304 const int nb_lines = viewport_get_nb_lines(&list_text[screen]);
306 if (nb_lines < gui_list->nb_items)
308 scrolling = true;
310 int scrollbar_size = nb_lines*
311 font_get(gui_list->parent[screen]->font)->height;
312 int actual_y = y - list_text[screen].y;
314 int new_selection = (actual_y * gui_list->nb_items)
315 / scrollbar_size;
317 int start_item = new_selection - nb_lines/2;
318 if(start_item < 0)
319 start_item = 0;
320 else if(start_item > gui_list->nb_items - nb_lines)
321 start_item = gui_list->nb_items - nb_lines;
323 gui_list->start_item[screen] = start_item;
324 gui_synclist_select_item(gui_list, new_selection);
326 return ACTION_REDRAW;
329 return ACTION_NONE;
332 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
334 short x, y;
335 const int button = action_get_touchscreen_press(&x, &y);
336 int line;
337 const struct screen *display = &screens[SCREEN_MAIN];
338 const int screen = display->screen_type;
339 const int list_start_item = gui_list->start_item[screen];
340 const struct viewport *list_text_vp = &list_text[screen];
342 if (button == BUTTON_NONE)
343 return ACTION_NONE;
344 if (x > list_text_vp->x + list_text_vp->width)
346 /* wider than the list's viewport, ignore it */
347 return ACTION_NONE;
349 else if (x < list_text_vp->x)
351 /* Top left corner is GO_TO_ROOT */
352 if (y<list_text[SCREEN_MAIN].y)
354 if (button == BUTTON_REL)
355 return ACTION_STD_MENU;
356 else if (button == (BUTTON_REPEAT|BUTTON_REL))
357 return ACTION_STD_CONTEXT;
358 else
359 return ACTION_NONE;
361 /* Scroll bar */
362 else if(global_settings.scrollbar == SCROLLBAR_LEFT)
363 return gui_synclist_touchscreen_scrollbar(gui_list, y);
365 else
367 if (x > list_text_vp->x + list_text_vp->width &&
368 global_settings.scrollbar == SCROLLBAR_RIGHT)
369 return gui_synclist_touchscreen_scrollbar(gui_list, y);
371 /* |--------------------------------------------------------|
372 * | Description of the touchscreen list interface: |
373 * |--------------------------------------------------------|
374 * | Pressing an item will select it and "enter" it. |
375 * | |
376 * | Pressing and holding your pen down will scroll through |
377 * | the list of items. |
378 * | |
379 * | Pressing and holding your pen down on a single item |
380 * | will bring up the context menu of it. |
381 * |--------------------------------------------------------|
383 if (y > list_text_vp->y || button & BUTTON_REPEAT)
385 int line_height, actual_y;
387 actual_y = y - list_text_vp->y;
388 line_height = font_get(gui_list->parent[screen]->font)->height;
389 line = actual_y / line_height;
391 /* Pressed below the list*/
392 if (list_start_item + line >= gui_list->nb_items)
393 return ACTION_NONE;
395 /* Pressed a border */
396 if(UNLIKELY(actual_y % line_height == 0))
397 return ACTION_NONE;
399 if (line != (gui_list->selected_item - list_start_item)
400 && button ^ BUTTON_REL)
402 if(button & BUTTON_REPEAT)
403 scrolling = true;
405 gui_synclist_select_item(gui_list, list_start_item + line);
407 return ACTION_REDRAW;
410 /* This has the same effect as the icons do when the scrollbar
411 is on the left (ie eliminate the chances an user enters/starts
412 an item when he wanted to use the scrollbar, due to touchscreen
413 dead zones)
415 if(global_settings.scrollbar == SCROLLBAR_RIGHT &&
416 x > list_text_vp->x + list_text_vp->width -
417 get_icon_width(SCREEN_MAIN))
418 return ACTION_NONE;
420 if (button == (BUTTON_REPEAT|BUTTON_REL))
422 if(!scrolling)
424 /* Pen was hold on the same line as the
425 * previously selected one
426 * => simulate long button press
428 return ACTION_STD_CONTEXT;
430 else
432 /* Pen was moved across several lines and then released on
433 * this one
434 * => do nothing
436 scrolling = false;
437 return ACTION_NONE;
440 else if(button == BUTTON_REL &&
441 line == gui_list->selected_item - list_start_item)
443 /* Pen was released on either the same line as the previously
444 * selected one or an other one
445 * => simulate short press
447 return ACTION_STD_OK;
449 else
450 return ACTION_NONE;
452 /* Everything above the items is cancel */
453 else if (y < list_text_vp->y && button == BUTTON_REL)
454 return ACTION_STD_CANCEL;
456 return ACTION_NONE;
458 #endif