fix another bug from FS#10771 - list touchscreen handling needs to ignore presses...
[kugel-rb.git] / apps / gui / bitmap / list.c
blobe0b68c42a48e1289fcdfa63a1c425a25ca40edf3
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 int 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, VP_IS_RTL(&vp) ? 1 : 0, 0, SCROLLBAR_WIDTH-1, vp.height,
157 list->nb_items, list_start_item, list_start_item + end-start,
158 VERTICAL);
160 else if (show_title)
162 /* shift everything a bit in relation to the title... */
163 if (!VP_IS_RTL(list_text_vp) && scrollbar_in_left)
165 list_text_vp->width -= SCROLLBAR_WIDTH;
166 list_text_vp->x += SCROLLBAR_WIDTH;
168 else if (VP_IS_RTL(list_text_vp) && !scrollbar_in_left)
170 list_text_vp->width -= SCROLLBAR_WIDTH;
174 /* setup icon placement */
175 list_icons = *list_text_vp;
176 int icon_count = (list->callback_get_item_icon != NULL) ? 1 : 0;
177 if (show_cursor)
178 icon_count++;
179 if (icon_count)
181 list_icons.width = icon_width * icon_count;
182 list_text_vp->width -= list_icons.width + ICON_PADDING;
183 if (VP_IS_RTL(&list_icons))
184 list_icons.x += list_text_vp->width + ICON_PADDING;
185 else
186 list_text_vp->x += list_icons.width + ICON_PADDING;
189 for (i=start; i<end && i<list->nb_items; i++)
191 /* do the text */
192 unsigned const char *s;
193 char entry_buffer[MAX_PATH];
194 unsigned char *entry_name;
195 int text_pos = 0;
196 s = list->callback_get_item_name(i, list->data, entry_buffer,
197 sizeof(entry_buffer));
198 entry_name = P2STR(s);
199 display->set_viewport(list_text_vp);
200 style = STYLE_DEFAULT;
201 /* position the string at the correct offset place */
202 int item_width,h;
203 display->getstringsize(entry_name, &item_width, &h);
204 item_offset = gui_list_get_item_offset(list, item_width, text_pos,
205 display, list_text_vp);
207 #ifdef HAVE_LCD_COLOR
208 /* if the list has a color callback */
209 if (list->callback_get_item_color)
211 int color = list->callback_get_item_color(i, list->data);
212 /* if color selected */
213 if (color >= 0)
215 style |= STYLE_COLORED|color;
218 #endif
219 if(i >= list->selected_item && i < list->selected_item
220 + list->selected_size && list->show_selection_marker)
221 {/* The selected item must be displayed scrolling */
222 if (global_settings.cursor_style == 1
223 #ifdef HAVE_REMOTE_LCD
224 /* the global_settings.cursor_style check is here to make
225 * sure if they want the cursor instead of bar it will work
227 || (display->depth < 16 && global_settings.cursor_style)
228 #endif
231 /* Display inverted-line-style */
232 style = STYLE_INVERT;
234 #ifdef HAVE_LCD_COLOR
235 else if (global_settings.cursor_style == 2)
237 /* Display colour line selector */
238 style = STYLE_COLORBAR;
240 else if (global_settings.cursor_style == 3)
242 /* Display gradient line selector */
243 style = STYLE_GRADIENT;
245 /* Make the lcd driver know how many lines the gradient should
246 cover and current line number */
247 /* number of selected lines */
248 style |= NUMLN_PACK(list->selected_size);
249 /* current line number, zero based */
250 style |= CURLN_PACK(cur_line);
251 cur_line++;
253 #endif
254 /* if the text is smaller than the viewport size */
255 if (item_offset> item_width - (list_text_vp->width - text_pos))
257 /* don't scroll */
258 display->puts_style_offset(0, i-start, entry_name,
259 style, item_offset);
261 else
263 display->puts_scroll_style_offset(0, i-start, entry_name,
264 style, item_offset);
267 else
269 if (list->scroll_all)
270 display->puts_scroll_style_offset(0, i-start, entry_name,
271 style, item_offset);
272 else
273 display->puts_style_offset(0, i-start, entry_name,
274 style, item_offset);
276 /* do the icon */
277 display->set_viewport(&list_icons);
278 if (list->callback_get_item_icon && global_settings.show_icons)
280 screen_put_icon_with_offset(display, show_cursor?1:0,
281 (i-start),show_cursor?ICON_PADDING:0,0,
282 list->callback_get_item_icon(i, list->data));
284 if (show_cursor && i >= list->selected_item &&
285 i < list->selected_item + list->selected_size)
287 screen_put_icon(display, 0, i-start, Icon_Cursor);
290 display->set_viewport(parent);
291 display->update_viewport();
292 display->set_viewport(NULL);
295 #if defined(HAVE_TOUCHSCREEN)
296 /* This needs to be fixed if we ever get more than 1 touchscreen on a target. */
297 static bool scrolling=false;
299 static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
300 int y)
302 const int screen = screens[SCREEN_MAIN].screen_type;
303 const int nb_lines = viewport_get_nb_lines(&list_text[screen]);
305 if (nb_lines < gui_list->nb_items)
307 scrolling = true;
309 int scrollbar_size = nb_lines*
310 font_get(gui_list->parent[screen]->font)->height;
311 int actual_y = y - list_text[screen].y;
313 int new_selection = (actual_y * gui_list->nb_items)
314 / scrollbar_size;
316 int start_item = new_selection - nb_lines/2;
317 if(start_item < 0)
318 start_item = 0;
319 else if(start_item > gui_list->nb_items - nb_lines)
320 start_item = gui_list->nb_items - nb_lines;
322 gui_list->start_item[screen] = start_item;
323 gui_synclist_select_item(gui_list, new_selection);
325 return ACTION_REDRAW;
328 return ACTION_NONE;
331 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
333 short x, y;
334 const int button = action_get_touchscreen_press(&x, &y);
335 int line;
336 const struct screen *display = &screens[SCREEN_MAIN];
337 const int screen = display->screen_type;
338 const int list_start_item = gui_list->start_item[screen];
339 const struct viewport *list_text_vp = &list_text[screen];
341 if (button == BUTTON_NONE)
342 return ACTION_NONE;
343 if (x > list_text_vp->x + list_text_vp->width)
345 /* wider than the list's viewport, ignore it */
346 return ACTION_NONE;
348 else if (x < list_text_vp->x)
350 /* Top left corner is GO_TO_ROOT */
351 if (y<list_text[SCREEN_MAIN].y)
353 if (button == BUTTON_REL)
354 return ACTION_STD_MENU;
355 else if (button == (BUTTON_REPEAT|BUTTON_REL))
356 return ACTION_STD_CONTEXT;
357 else
358 return ACTION_NONE;
360 /* Scroll bar */
361 else if(global_settings.scrollbar == SCROLLBAR_LEFT)
362 return gui_synclist_touchscreen_scrollbar(gui_list, y);
364 else
366 if (x > list_text_vp->x + list_text_vp->width &&
367 global_settings.scrollbar == SCROLLBAR_RIGHT)
368 return gui_synclist_touchscreen_scrollbar(gui_list, y);
370 /* |--------------------------------------------------------|
371 * | Description of the touchscreen list interface: |
372 * |--------------------------------------------------------|
373 * | Pressing an item will select it and "enter" it. |
374 * | |
375 * | Pressing and holding your pen down will scroll through |
376 * | the list of items. |
377 * | |
378 * | Pressing and holding your pen down on a single item |
379 * | will bring up the context menu of it. |
380 * |--------------------------------------------------------|
382 if (y > list_text_vp->y || button & BUTTON_REPEAT)
384 int line_height, actual_y;
386 actual_y = y - list_text_vp->y;
387 line_height = font_get(gui_list->parent[screen]->font)->height;
388 line = actual_y / line_height;
390 /* Pressed below the list*/
391 if (list_start_item + line >= gui_list->nb_items)
392 return ACTION_NONE;
394 /* Pressed a border */
395 if(UNLIKELY(actual_y % line_height == 0))
396 return ACTION_NONE;
398 if (line != (gui_list->selected_item - list_start_item)
399 && button ^ BUTTON_REL)
401 if(button & BUTTON_REPEAT)
402 scrolling = true;
404 gui_synclist_select_item(gui_list, list_start_item + line);
406 return ACTION_REDRAW;
409 /* This has the same effect as the icons do when the scrollbar
410 is on the left (ie eliminate the chances an user enters/starts
411 an item when he wanted to use the scrollbar, due to touchscreen
412 dead zones)
414 if(global_settings.scrollbar == SCROLLBAR_RIGHT &&
415 x > list_text_vp->x + list_text_vp->width -
416 get_icon_width(SCREEN_MAIN))
417 return ACTION_NONE;
419 if (button == (BUTTON_REPEAT|BUTTON_REL))
421 if(!scrolling)
423 /* Pen was hold on the same line as the
424 * previously selected one
425 * => simulate long button press
427 return ACTION_STD_CONTEXT;
429 else
431 /* Pen was moved across several lines and then released on
432 * this one
433 * => do nothing
435 scrolling = false;
436 return ACTION_NONE;
439 else if(button == BUTTON_REL &&
440 line == gui_list->selected_item - list_start_item)
442 /* Pen was released on either the same line as the previously
443 * selected one or an other one
444 * => simulate short press
446 return ACTION_STD_OK;
448 else
449 return ACTION_NONE;
451 /* Everything above the items is cancel */
452 else if (y < list_text_vp->y && button == BUTTON_REL)
453 return ACTION_STD_CANCEL;
455 return ACTION_NONE;
457 #endif