Alright, it seems I got scroll_stop() wrong. Using the viewport that's set before...
[kugel-rb.git] / apps / gui / bitmap / list.c
blob94870538d6113c75be9c48eb6d7b2765fd75d946
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 display->scroll_stop(&title_text[screen]);
70 if (!list_display_title(list, screen))
71 return false;
72 title_text[screen] = *(list->parent[screen]);
73 title_text[screen].height = font_get(title_text[screen].font)->height;
75 if (list->title_icon != Icon_NOICON && global_settings.show_icons)
77 struct viewport title_icon = title_text[screen];
78 title_icon.width = get_icon_width(screen)
79 + ICON_PADDING*2;
80 title_icon.x += ICON_PADDING;
82 title_text[screen].width -= title_icon.width;
83 title_text[screen].x += title_icon.width;
85 display->set_viewport(&title_icon);
86 screen_put_icon(display, 0, 0, list->title_icon);
88 #ifdef HAVE_LCD_COLOR
89 if (list->title_color >= 0)
91 style |= (STYLE_COLORED|list->title_color);
93 #endif
94 display->set_viewport(&title_text[screen]);
95 display->puts_scroll_style(0, 0, list->title, style);
96 return true;
99 void list_draw(struct screen *display, struct gui_synclist *list)
101 struct viewport list_icons;
102 int start, end, line_height, style, i;
103 const int screen = display->screen_type;
104 const int icon_width = get_icon_width(screen) + ICON_PADDING;
105 const bool show_cursor = !global_settings.cursor_style &&
106 list->show_selection_marker;
107 struct viewport *parent = (list->parent[screen]);
108 #ifdef HAVE_LCD_COLOR
109 unsigned char cur_line = 0;
110 #endif
111 int item_offset;
112 bool show_title;
113 line_height = font_get(parent->font)->height;
114 display->set_viewport(parent);
115 display->clear_viewport();
116 display->scroll_stop(&list_text[screen]);
117 list_text[screen] = *parent;
118 if ((show_title = draw_title(display, list)))
120 list_text[screen].y += line_height;
121 list_text[screen].height -= line_height;
124 start = list->start_item[screen];
125 end = start + viewport_get_nb_lines(&list_text[screen]);
127 /* draw the scrollbar if its needed */
128 if (global_settings.scrollbar &&
129 viewport_get_nb_lines(&list_text[screen]) < list->nb_items)
131 struct viewport vp;
132 vp = list_text[screen];
133 vp.width = SCROLLBAR_WIDTH;
134 list_text[screen].width -= SCROLLBAR_WIDTH;
135 if(global_settings.scrollbar == SCROLLBAR_LEFT)
136 list_text[screen].x += SCROLLBAR_WIDTH;
137 vp.height = line_height *
138 viewport_get_nb_lines(&list_text[screen]);
139 vp.x = parent->x;
140 if(global_settings.scrollbar == SCROLLBAR_RIGHT)
141 vp.x += list_text[screen].width;
142 display->set_viewport(&vp);
143 gui_scrollbar_draw(display, 0, 0, SCROLLBAR_WIDTH-1,
144 vp.height, list->nb_items,
145 list->start_item[screen],
146 list->start_item[screen] + end-start,
147 VERTICAL);
149 else if (show_title)
151 /* shift everything right a bit... */
152 if(global_settings.scrollbar == SCROLLBAR_LEFT)
154 list_text[screen].width -= SCROLLBAR_WIDTH;
155 list_text[screen].x += SCROLLBAR_WIDTH;
159 /* setup icon placement */
160 list_icons = list_text[screen];
161 int icon_count = global_settings.show_icons &&
162 (list->callback_get_item_icon != NULL) ? 1 : 0;
163 if (show_cursor)
164 icon_count++;
165 if (icon_count)
167 list_icons.width = icon_width * icon_count;
168 list_text[screen].width -=
169 list_icons.width + ICON_PADDING;
170 list_text[screen].x +=
171 list_icons.width + ICON_PADDING;
174 for (i=start; i<end && i<list->nb_items; i++)
176 /* do the text */
177 unsigned const char *s;
178 char entry_buffer[MAX_PATH];
179 unsigned char *entry_name;
180 int text_pos = 0;
181 s = list->callback_get_item_name(i, list->data, entry_buffer,
182 sizeof(entry_buffer));
183 entry_name = P2STR(s);
184 display->set_viewport(&list_text[screen]);
185 style = STYLE_DEFAULT;
186 /* position the string at the correct offset place */
187 int item_width,h;
188 display->getstringsize(entry_name, &item_width, &h);
189 item_offset = gui_list_get_item_offset(list, item_width,
190 text_pos, display,
191 &list_text[screen]);
193 #ifdef HAVE_LCD_COLOR
194 /* if the list has a color callback */
195 if (list->callback_get_item_color)
197 int color = list->callback_get_item_color(i, list->data);
198 /* if color selected */
199 if (color >= 0)
201 style |= STYLE_COLORED|color;
204 #endif
205 if(i >= list->selected_item && i < list->selected_item
206 + list->selected_size && list->show_selection_marker)
207 {/* The selected item must be displayed scrolling */
208 if (global_settings.cursor_style == 1
209 #ifdef HAVE_REMOTE_LCD
210 /* the global_settings.cursor_style check is here to make
211 * sure if they want the cursor instead of bar it will work
213 || (display->depth < 16 && global_settings.cursor_style)
214 #endif
217 /* Display inverted-line-style */
218 style = STYLE_INVERT;
220 #ifdef HAVE_LCD_COLOR
221 else if (global_settings.cursor_style == 2)
223 /* Display colour line selector */
224 style = STYLE_COLORBAR;
226 else if (global_settings.cursor_style == 3)
228 /* Display gradient line selector */
229 style = STYLE_GRADIENT;
231 /* Make the lcd driver know how many lines the gradient should
232 cover and current line number */
233 /* number of selected lines */
234 style |= NUMLN_PACK(list->selected_size);
235 /* current line number, zero based */
236 style |= CURLN_PACK(cur_line);
237 cur_line++;
239 #endif
240 /* if the text is smaller than the viewport size */
241 if (item_offset> item_width
242 - (list_text[screen].width - text_pos))
244 /* don't scroll */
245 display->puts_style_offset(0, i-start, entry_name,
246 style, item_offset);
248 else
250 display->puts_scroll_style_offset(0, i-start, entry_name,
251 style, item_offset);
254 else
256 if (list->scroll_all)
257 display->puts_scroll_style_offset(0, i-start, entry_name,
258 style, item_offset);
259 else
260 display->puts_style_offset(0, i-start, entry_name,
261 style, item_offset);
263 /* do the icon */
264 if (list->callback_get_item_icon && global_settings.show_icons)
266 display->set_viewport(&list_icons);
267 screen_put_icon_with_offset(display, show_cursor?1:0,
268 (i-start),show_cursor?ICON_PADDING:0,0,
269 list->callback_get_item_icon(i, list->data));
270 if (show_cursor && i >= list->selected_item &&
271 i < list->selected_item + list->selected_size)
273 screen_put_icon(display, 0, i-start, Icon_Cursor);
276 else if (show_cursor && i >= list->selected_item &&
277 i < list->selected_item + list->selected_size)
279 display->set_viewport(&list_icons);
280 screen_put_icon(display, 0, (i-start), Icon_Cursor);
283 display->set_viewport(parent);
284 display->update_viewport();
285 display->set_viewport(NULL);
288 #if defined(HAVE_TOUCHSCREEN)
289 /* This needs to be fixed if we ever get more than 1 touchscreen on a target. */
290 static bool scrolling=false;
292 static int gui_synclist_touchscreen_scrollbar(struct gui_synclist * gui_list,
293 int y)
295 int screen = screens[SCREEN_MAIN].screen_type;
296 int nb_lines = viewport_get_nb_lines(&list_text[screen]);
297 if (nb_lines < gui_list->nb_items)
299 scrolling = true;
301 int scrollbar_size = nb_lines*
302 font_get(gui_list->parent[screen]->font)->height;
303 int actual_y = y - list_text[screen].y;
305 int new_selection = (actual_y * gui_list->nb_items)
306 / scrollbar_size;
308 int start_item = new_selection - nb_lines/2;
309 if(start_item < 0)
310 start_item = 0;
311 else if(start_item > gui_list->nb_items - nb_lines)
312 start_item = gui_list->nb_items - nb_lines;
314 gui_list->start_item[screen] = start_item;
315 gui_synclist_select_item(gui_list, new_selection);
317 return ACTION_REDRAW;
320 return ACTION_NONE;
323 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
325 short x, y;
326 int button = action_get_touchscreen_press(&x, &y);
327 int line;
328 struct screen *display = &screens[SCREEN_MAIN];
329 int screen = display->screen_type;
330 if (button == BUTTON_NONE)
331 return ACTION_NONE;
332 if (x<list_text[screen].x)
334 /* Top left corner is GO_TO_ROOT */
335 if (y<list_text[SCREEN_MAIN].y)
337 if (button == BUTTON_REL)
338 return ACTION_STD_MENU;
339 else if (button == (BUTTON_REPEAT|BUTTON_REL))
340 return ACTION_STD_CONTEXT;
341 else
342 return ACTION_NONE;
344 /* Scroll bar */
345 else if(global_settings.scrollbar == SCROLLBAR_LEFT)
346 return gui_synclist_touchscreen_scrollbar(gui_list, y);
348 else
350 if(x>list_text[screen].x+list_text[screen].width &&
351 global_settings.scrollbar == SCROLLBAR_RIGHT)
352 return gui_synclist_touchscreen_scrollbar(gui_list, y);
354 /* |--------------------------------------------------------|
355 * | Description of the touchscreen list interface: |
356 * |--------------------------------------------------------|
357 * | Pressing an item will select it and "enter" it. |
358 * | |
359 * | Pressing and holding your pen down will scroll through |
360 * | the list of items. |
361 * | |
362 * | Pressing and holding your pen down on a single item |
363 * | will bring up the context menu of it. |
364 * |--------------------------------------------------------|
366 if (y > list_text[screen].y || button & BUTTON_REPEAT)
368 int line_height, actual_y;
370 actual_y = y - list_text[screen].y;
371 line_height = font_get(gui_list->parent[screen]->font)->height;
372 line = actual_y / line_height;
374 /* Pressed below the list*/
375 if (gui_list->start_item[screen]+line >= gui_list->nb_items)
376 return ACTION_NONE;
378 /* Pressed a border */
379 if(UNLIKELY(actual_y % line_height == 0))
380 return ACTION_NONE;
382 if (line != (gui_list->selected_item - gui_list->start_item[screen])
383 && button ^ BUTTON_REL)
385 if(button & BUTTON_REPEAT)
386 scrolling = true;
388 gui_synclist_select_item(gui_list, gui_list->start_item[screen]
389 + line);
391 return ACTION_REDRAW;
394 /* This has the same effect as the icons do when the scrollbar
395 is on the left (ie eliminate the chances an user enters/starts
396 an item when he wanted to use the scrollbar, due to touchscreen
397 dead zones)
399 if(global_settings.scrollbar == SCROLLBAR_RIGHT &&
400 x > list_text[screen].x + list_text[screen].width -
401 get_icon_width(SCREEN_MAIN))
402 return ACTION_NONE;
404 if (button == (BUTTON_REPEAT|BUTTON_REL))
406 if(!scrolling)
408 /* Pen was hold on the same line as the
409 * previously selected one
410 * => simulate long button press
412 return ACTION_STD_CONTEXT;
414 else
416 /* Pen was moved across several lines and then released on
417 * this one
418 * => do nothing
420 scrolling = false;
421 return ACTION_NONE;
424 else if(button == BUTTON_REL &&
425 line == gui_list->selected_item - gui_list->start_item[screen])
427 /* Pen was released on either the same line as the previously
428 * selected one or an other one
429 * => simulate short press
431 return ACTION_STD_OK;
433 else
434 return ACTION_NONE;
436 /* Everything above the items is cancel */
437 else if (y < list_text[screen].y && button == BUTTON_REL)
438 return ACTION_STD_CANCEL;
440 return ACTION_NONE;
442 #endif