Don't stop scrolling for the whole display, but only in the actual list parent viewport.
[kugel-rb.git] / apps / gui / bitmap / list.c
blob046621e8f3d613155a45a7d4796263aa45468eed
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 static bool draw_title(struct screen *display, struct gui_synclist *list)
67 const int screen = display->screen_type;
68 int style = STYLE_DEFAULT;
69 if (!list_display_title(list, screen))
70 return false;
71 title_text[screen] = *(list->parent[screen]);
72 title_text[screen].height = font_get(title_text[screen].font)->height;
74 if (list->title_icon != Icon_NOICON && global_settings.show_icons)
76 struct viewport title_icon = title_text[screen];
77 title_icon.width = get_icon_width(screen)
78 + ICON_PADDING*2;
79 title_icon.x += ICON_PADDING;
81 title_text[screen].width -= title_icon.width;
82 title_text[screen].x += title_icon.width;
84 display->set_viewport(&title_icon);
85 screen_put_icon(display, 0, 0, list->title_icon);
87 #ifdef HAVE_LCD_COLOR
88 if (list->title_color >= 0)
90 style |= (STYLE_COLORED|list->title_color);
92 #endif
93 display->set_viewport(&title_text[screen]);
94 display->puts_scroll_style(0, 0, list->title, style);
95 return true;
98 void list_draw(struct screen *display, struct gui_synclist *list)
100 struct viewport list_icons;
101 int start, end, line_height, style, i;
102 const int screen = display->screen_type;
103 const int icon_width = get_icon_width(screen) + ICON_PADDING;
104 const bool show_cursor = !global_settings.cursor_style &&
105 list->show_selection_marker;
106 struct viewport *parent = (list->parent[screen]);
107 #ifdef HAVE_LCD_COLOR
108 unsigned char cur_line = 0;
109 #endif
110 int item_offset;
111 bool show_title;
112 line_height = font_get(parent->font)->height;
113 display->set_viewport(parent);
114 display->clear_viewport();
115 display->scroll_stop(parent);
116 list_text[screen] = *parent;
117 if ((show_title = draw_title(display, list)))
119 list_text[screen].y += line_height;
120 list_text[screen].height -= line_height;
123 start = list->start_item[screen];
124 end = start + viewport_get_nb_lines(&list_text[screen]);
126 /* draw the scrollbar if its needed */
127 if (global_settings.scrollbar &&
128 viewport_get_nb_lines(&list_text[screen]) < list->nb_items)
130 struct viewport vp;
131 vp = list_text[screen];
132 vp.width = SCROLLBAR_WIDTH;
133 list_text[screen].width -= SCROLLBAR_WIDTH;
134 if(global_settings.scrollbar == SCROLLBAR_LEFT)
135 list_text[screen].x += SCROLLBAR_WIDTH;
136 vp.height = line_height *
137 viewport_get_nb_lines(&list_text[screen]);
138 vp.x = parent->x;
139 if(global_settings.scrollbar == SCROLLBAR_RIGHT)
140 vp.x += list_text[screen].width;
141 display->set_viewport(&vp);
142 gui_scrollbar_draw(display, 0, 0, SCROLLBAR_WIDTH-1,
143 vp.height, list->nb_items,
144 list->start_item[screen],
145 list->start_item[screen] + end-start,
146 VERTICAL);
148 else if (show_title)
150 /* shift everything right a bit... */
151 if(global_settings.scrollbar == SCROLLBAR_LEFT)
153 list_text[screen].width -= SCROLLBAR_WIDTH;
154 list_text[screen].x += SCROLLBAR_WIDTH;
158 /* setup icon placement */
159 list_icons = list_text[screen];
160 int icon_count = global_settings.show_icons &&
161 (list->callback_get_item_icon != NULL) ? 1 : 0;
162 if (show_cursor)
163 icon_count++;
164 if (icon_count)
166 list_icons.width = icon_width * icon_count;
167 list_text[screen].width -=
168 list_icons.width + ICON_PADDING;
169 list_text[screen].x +=
170 list_icons.width + ICON_PADDING;
173 for (i=start; i<end && i<list->nb_items; i++)
175 /* do the text */
176 unsigned const char *s;
177 char entry_buffer[MAX_PATH];
178 unsigned char *entry_name;
179 int text_pos = 0;
180 s = list->callback_get_item_name(i, list->data, entry_buffer,
181 sizeof(entry_buffer));
182 entry_name = P2STR(s);
183 display->set_viewport(&list_text[screen]);
184 style = STYLE_DEFAULT;
185 /* position the string at the correct offset place */
186 int item_width,h;
187 display->getstringsize(entry_name, &item_width, &h);
188 item_offset = gui_list_get_item_offset(list, item_width,
189 text_pos, display,
190 &list_text[screen]);
192 #ifdef HAVE_LCD_COLOR
193 /* if the list has a color callback */
194 if (list->callback_get_item_color)
196 int color = list->callback_get_item_color(i, list->data);
197 /* if color selected */
198 if (color >= 0)
200 style |= STYLE_COLORED|color;
203 #endif
204 if(i >= list->selected_item && i < list->selected_item
205 + list->selected_size && list->show_selection_marker)
206 {/* The selected item must be displayed scrolling */
207 if (global_settings.cursor_style == 1
208 #ifdef HAVE_REMOTE_LCD
209 /* the global_settings.cursor_style check is here to make
210 * sure if they want the cursor instead of bar it will work
212 || (display->depth < 16 && global_settings.cursor_style)
213 #endif
216 /* Display inverted-line-style */
217 style = STYLE_INVERT;
219 #ifdef HAVE_LCD_COLOR
220 else if (global_settings.cursor_style == 2)
222 /* Display colour line selector */
223 style = STYLE_COLORBAR;
225 else if (global_settings.cursor_style == 3)
227 /* Display gradient line selector */
228 style = STYLE_GRADIENT;
230 /* Make the lcd driver know how many lines the gradient should
231 cover and current line number */
232 /* number of selected lines */
233 style |= NUMLN_PACK(list->selected_size);
234 /* current line number, zero based */
235 style |= CURLN_PACK(cur_line);
236 cur_line++;
238 #endif
239 /* if the text is smaller than the viewport size */
240 if (item_offset> item_width
241 - (list_text[screen].width - text_pos))
243 /* don't scroll */
244 display->puts_style_offset(0, i-start, entry_name,
245 style, item_offset);
247 else
249 display->puts_scroll_style_offset(0, i-start, entry_name,
250 style, item_offset);
253 else
255 if (list->scroll_all)
256 display->puts_scroll_style_offset(0, i-start, entry_name,
257 style, item_offset);
258 else
259 display->puts_style_offset(0, i-start, entry_name,
260 style, item_offset);
262 /* do the icon */
263 if (list->callback_get_item_icon && global_settings.show_icons)
265 display->set_viewport(&list_icons);
266 screen_put_icon_with_offset(display, show_cursor?1:0,
267 (i-start),show_cursor?ICON_PADDING:0,0,
268 list->callback_get_item_icon(i, list->data));
269 if (show_cursor && i >= list->selected_item &&
270 i < list->selected_item + list->selected_size)
272 screen_put_icon(display, 0, i-start, Icon_Cursor);
275 else if (show_cursor && i >= list->selected_item &&
276 i < list->selected_item + list->selected_size)
278 display->set_viewport(&list_icons);
279 screen_put_icon(display, 0, (i-start), Icon_Cursor);
282 display->set_viewport(parent);
283 display->update_viewport();
284 display->set_viewport(NULL);
287 #if defined(HAVE_TOUCHSCREEN)
288 /* This needs to be fixed if we ever get more than 1 touchscreen on a target. */
289 static bool scrolling=false;
291 static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
292 int y)
294 int screen = screens[SCREEN_MAIN].screen_type;
295 int nb_lines = viewport_get_nb_lines(&list_text[screen]);
296 if (nb_lines < gui_list->nb_items)
298 scrolling = true;
300 int scrollbar_size = nb_lines*
301 font_get(gui_list->parent[screen]->font)->height;
302 int actual_y = y - list_text[screen].y;
304 int new_selection = (actual_y * gui_list->nb_items)
305 / scrollbar_size;
307 int start_item = new_selection - nb_lines/2;
308 if(start_item < 0)
309 start_item = 0;
310 else if(start_item > gui_list->nb_items - nb_lines)
311 start_item = gui_list->nb_items - nb_lines;
313 gui_list->start_item[screen] = start_item;
314 gui_synclist_select_item(gui_list, new_selection);
316 return ACTION_REDRAW;
319 return ACTION_NONE;
322 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
324 short x, y;
325 int button = action_get_touchscreen_press(&x, &y);
326 int line;
327 struct screen *display = &screens[SCREEN_MAIN];
328 int screen = display->screen_type;
329 if (button == BUTTON_NONE)
330 return ACTION_NONE;
331 if (x<list_text[screen].x)
333 /* Top left corner is GO_TO_ROOT */
334 if (y<list_text[SCREEN_MAIN].y)
336 if (button == BUTTON_REL)
337 return ACTION_STD_MENU;
338 else if (button == (BUTTON_REPEAT|BUTTON_REL))
339 return ACTION_STD_CONTEXT;
340 else
341 return ACTION_NONE;
343 /* Scroll bar */
344 else if(global_settings.scrollbar == SCROLLBAR_LEFT)
345 return gui_synclist_touchscreen_scrollbar(gui_list, y);
347 else
349 if(x>list_text[screen].x+list_text[screen].width &&
350 global_settings.scrollbar == SCROLLBAR_RIGHT)
351 return gui_synclist_touchscreen_scrollbar(gui_list, y);
353 /* |--------------------------------------------------------|
354 * | Description of the touchscreen list interface: |
355 * |--------------------------------------------------------|
356 * | Pressing an item will select it and "enter" it. |
357 * | |
358 * | Pressing and holding your pen down will scroll through |
359 * | the list of items. |
360 * | |
361 * | Pressing and holding your pen down on a single item |
362 * | will bring up the context menu of it. |
363 * |--------------------------------------------------------|
365 if (y > list_text[screen].y || button & BUTTON_REPEAT)
367 int line_height, actual_y;
369 actual_y = y - list_text[screen].y;
370 line_height = font_get(gui_list->parent[screen]->font)->height;
371 line = actual_y / line_height;
373 /* Pressed below the list*/
374 if (gui_list->start_item[screen]+line >= gui_list->nb_items)
375 return ACTION_NONE;
377 /* Pressed a border */
378 if(UNLIKELY(actual_y % line_height == 0))
379 return ACTION_NONE;
381 if (line != (gui_list->selected_item - gui_list->start_item[screen])
382 && button ^ BUTTON_REL)
384 if(button & BUTTON_REPEAT)
385 scrolling = true;
387 gui_synclist_select_item(gui_list, gui_list->start_item[screen]
388 + line);
390 return ACTION_REDRAW;
393 /* This has the same effect as the icons do when the scrollbar
394 is on the left (ie eliminate the chances an user enters/starts
395 an item when he wanted to use the scrollbar, due to touchscreen
396 dead zones)
398 if(global_settings.scrollbar == SCROLLBAR_RIGHT &&
399 x > list_text[screen].x + list_text[screen].width -
400 get_icon_width(SCREEN_MAIN))
401 return ACTION_NONE;
403 if (button == (BUTTON_REPEAT|BUTTON_REL))
405 if(!scrolling)
407 /* Pen was hold on the same line as the
408 * previously selected one
409 * => simulate long button press
411 return ACTION_STD_CONTEXT;
413 else
415 /* Pen was moved across several lines and then released on
416 * this one
417 * => do nothing
419 scrolling = false;
420 return ACTION_NONE;
423 else if(button == BUTTON_REL &&
424 line == gui_list->selected_item - gui_list->start_item[screen])
426 /* Pen was released on either the same line as the previously
427 * selected one or an other one
428 * => simulate short press
430 return ACTION_STD_OK;
432 else
433 return ACTION_NONE;
435 /* Everything above the items is cancel */
436 else if (y < list_text[screen].y && button == BUTTON_REL)
437 return ACTION_STD_CANCEL;
439 return ACTION_NONE;
441 #endif