Bugfix: Text styles are not drawmodes, so stop storing them in the vieport's drawmode...
[kugel-rb.git] / apps / gui / bitmap / list.c
blob47ecc1b9ca3736a64747caa385a04098ae6be752
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"
34 #include "action.h"
35 #include "screen_access.h"
36 #include "list.h"
37 #include "scrollbar.h"
38 #include "statusbar.h"
39 #include "lang.h"
40 #include "sound.h"
41 #include "misc.h"
42 #include "talk.h"
43 #include "viewport.h"
45 #define SCROLLBAR_WIDTH 6
46 #define ICON_PADDING 1
48 /* these are static to make scrolling work */
49 static struct viewport list_text[NB_SCREENS], title_text[NB_SCREENS];
51 int gui_list_get_item_offset(struct gui_synclist * gui_list, int item_width,
52 int text_pos, struct screen * display,
53 struct viewport *vp);
54 bool list_display_title(struct gui_synclist *list, enum screen_type screen);
56 /* Draw the list...
57 internal screen layout:
58 -----------------
59 |TI| title | TI is title icon
60 -----------------
61 | | | |
62 |S|I| | S - scrollbar
63 | | | items | I - icons
64 | | | |
65 ------------------
67 static bool draw_title(struct screen *display, struct gui_synclist *list)
69 const int screen = display->screen_type;
70 int style = STYLE_DEFAULT;
71 if (!list_display_title(list, screen))
72 return false;
73 title_text[screen] = *(list->parent[screen]);
74 title_text[screen].height = font_get(title_text[screen].font)->height;
76 if (list->title_icon != Icon_NOICON && global_settings.show_icons)
78 struct viewport title_icon = title_text[screen];
79 title_icon.width = get_icon_width(screen)
80 + ICON_PADDING*2;
81 title_icon.x += ICON_PADDING;
83 title_text[screen].width -= title_icon.width;
84 title_text[screen].x += title_icon.width;
86 display->set_viewport(&title_icon);
87 screen_put_icon(display, 0, 0, list->title_icon);
89 #ifdef HAVE_LCD_COLOR
90 if (list->title_color >= 0)
92 style |= (STYLE_COLORED|list->title_color);
94 #endif
95 display->set_viewport(&title_text[screen]);
96 display->puts_scroll_style(0, 0, list->title, style);
97 return true;
100 void list_draw(struct screen *display, struct gui_synclist *list)
102 struct viewport list_icons;
103 int start, end, line_height, style, i;
104 const int screen = display->screen_type;
105 const int icon_width = get_icon_width(screen) + ICON_PADDING;
106 const bool show_cursor = !global_settings.cursor_style &&
107 list->show_selection_marker;
108 struct viewport *parent = (list->parent[screen]);
109 #ifdef HAVE_LCD_COLOR
110 unsigned char cur_line = 0;
111 #endif
112 int item_offset;
113 bool show_title;
114 line_height = font_get(parent->font)->height;
115 display->set_viewport(parent);
116 display->clear_viewport();
117 display->stop_scroll();
118 list_text[screen] = *parent;
119 if ((show_title = draw_title(display, list)))
121 list_text[screen].y += line_height;
122 list_text[screen].height -= line_height;
125 start = list->start_item[screen];
126 end = start + viewport_get_nb_lines(&list_text[screen]);
128 /* draw the scrollbar if its needed */
129 if (global_settings.scrollbar &&
130 viewport_get_nb_lines(&list_text[screen]) < list->nb_items)
132 struct viewport vp;
133 vp = list_text[screen];
134 vp.width = SCROLLBAR_WIDTH;
135 list_text[screen].width -= SCROLLBAR_WIDTH;
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 display->set_viewport(&vp);
141 gui_scrollbar_draw(display, 0, 0, SCROLLBAR_WIDTH-1,
142 vp.height, list->nb_items,
143 list->start_item[screen],
144 list->start_item[screen] + end-start,
145 VERTICAL);
147 else if (show_title)
149 /* shift everything right a bit... */
150 list_text[screen].width -= SCROLLBAR_WIDTH;
151 list_text[screen].x += SCROLLBAR_WIDTH;
154 /* setup icon placement */
155 list_icons = list_text[screen];
156 int icon_count = global_settings.show_icons &&
157 (list->callback_get_item_icon != NULL) ? 1 : 0;
158 if (show_cursor)
159 icon_count++;
160 if (icon_count)
162 list_icons.width = icon_width * icon_count;
163 list_text[screen].width -=
164 list_icons.width + ICON_PADDING;
165 list_text[screen].x +=
166 list_icons.width + ICON_PADDING;
169 for (i=start; i<end && i<list->nb_items; i++)
171 /* do the text */
172 unsigned char *s;
173 char entry_buffer[MAX_PATH];
174 unsigned char *entry_name;
175 int text_pos = 0;
176 s = list->callback_get_item_name(i, list->data, entry_buffer,
177 sizeof(entry_buffer));
178 entry_name = P2STR(s);
179 display->set_viewport(&list_text[screen]);
180 style = STYLE_DEFAULT;
181 /* position the string at the correct offset place */
182 int item_width,h;
183 display->getstringsize(entry_name, &item_width, &h);
184 item_offset = gui_list_get_item_offset(list, item_width,
185 text_pos, display,
186 &list_text[screen]);
188 #ifdef HAVE_LCD_COLOR
189 /* if the list has a color callback */
190 if (list->callback_get_item_color)
192 int color = list->callback_get_item_color(i, list->data);
193 /* if color selected */
194 if (color >= 0)
196 style |= STYLE_COLORED|color;
199 #endif
200 if(i >= list->selected_item && i < list->selected_item
201 + list->selected_size && list->show_selection_marker)
202 {/* The selected item must be displayed scrolling */
203 if (global_settings.cursor_style == 1
204 #ifdef HAVE_REMOTE_LCD
205 /* the global_settings.cursor_style check is here to make
206 * sure if they want the cursor instead of bar it will work
208 || (display->depth < 16 && global_settings.cursor_style)
209 #endif
212 /* Display inverted-line-style */
213 style = STYLE_INVERT;
215 #ifdef HAVE_LCD_COLOR
216 else if (global_settings.cursor_style == 2)
218 /* Display colour line selector */
219 style = STYLE_COLORBAR;
221 else if (global_settings.cursor_style == 3)
223 /* Display gradient line selector */
224 style = STYLE_GRADIENT;
226 /* Make the lcd driver know how many lines the gradient should
227 cover and current line number */
228 /* number of selected lines */
229 style |= NUMLN_PACK(list->selected_size);
230 /* current line number, zero based */
231 style |= CURLN_PACK(cur_line);
232 cur_line++;
234 #endif
235 /* if the text is smaller than the viewport size */
236 if (item_offset> item_width
237 - (list_text[screen].width - text_pos))
239 /* don't scroll */
240 display->puts_style_offset(0, i-start, entry_name,
241 style, item_offset);
243 else
245 display->puts_scroll_style_offset(0, i-start, entry_name,
246 style, item_offset);
249 else
251 if (list->scroll_all)
252 display->puts_scroll_style_offset(0, i-start, entry_name,
253 style, item_offset);
254 else
255 display->puts_style_offset(0, i-start, entry_name,
256 style, item_offset);
258 /* do the icon */
259 if (list->callback_get_item_icon && global_settings.show_icons)
261 display->set_viewport(&list_icons);
262 screen_put_icon_with_offset(display, show_cursor?1:0,
263 (i-start),show_cursor?ICON_PADDING:0,0,
264 list->callback_get_item_icon(i, list->data));
265 if (show_cursor && i >= list->selected_item &&
266 i < list->selected_item + list->selected_size)
268 screen_put_icon(display, 0, i-start, Icon_Cursor);
271 else if (show_cursor && i >= list->selected_item &&
272 i < list->selected_item + list->selected_size)
274 display->set_viewport(&list_icons);
275 screen_put_icon(display, 0, (i-start), Icon_Cursor);
278 display->set_viewport(parent);
279 display->update_viewport();
280 display->set_viewport(NULL);
283 #if defined(HAVE_TOUCHSCREEN)
284 /* This needs to be fixed if we ever get more than 1 touchscreen on a target.
285 * This also assumes the whole screen is used, which is a bad assumption but
286 * fine until customizable lists comes in...
288 static bool scrolling=false;
290 unsigned gui_synclist_do_touchscreen(struct gui_synclist * gui_list)
292 short x, y;
293 int button = action_get_touchscreen_press(&x, &y);
294 int line;
295 struct screen *display = &screens[SCREEN_MAIN];
296 int screen = display->screen_type;
297 if (button == BUTTON_NONE)
298 return ACTION_NONE;
299 if (x<list_text[screen].x)
301 /* Top left corner is GO_TO_ROOT */
302 if (y<list_text[SCREEN_MAIN].y)
304 if (button == BUTTON_REL)
305 return ACTION_STD_MENU;
306 else if (button == (BUTTON_REPEAT|BUTTON_REL))
307 return ACTION_STD_CONTEXT;
308 else
309 return ACTION_NONE;
311 /* Scroll bar */
312 else
314 int nb_lines = viewport_get_nb_lines(&list_text[screen]);
315 if (nb_lines < gui_list->nb_items)
317 int scrollbar_size = nb_lines*
318 font_get(gui_list->parent[screen]->font)->height;
319 int actual_y = y - list_text[screen].y;
321 int new_selection = (actual_y * gui_list->nb_items)
322 / scrollbar_size;
324 int start_item = new_selection - nb_lines/2;
325 if(start_item < 0)
326 start_item = 0;
327 else if(start_item > gui_list->nb_items - nb_lines)
328 start_item = gui_list->nb_items - nb_lines;
330 gui_list->start_item[screen] = start_item;
331 gui_synclist_select_item(gui_list, new_selection);
333 return ACTION_REDRAW;
337 else
339 /* |--------------------------------------------------------|
340 * | Description of the touchscreen list interface: |
341 * |--------------------------------------------------------|
342 * | Pressing an item will select it and "enter" it. |
343 * | |
344 * | Pressing and holding your pen down will scroll through |
345 * | the list of items. |
346 * | |
347 * | Pressing and holding your pen down on a single item |
348 * | will bring up the context menu of it. |
349 * |--------------------------------------------------------|
351 if (y > list_text[screen].y || button & BUTTON_REPEAT)
353 int line_height, actual_y;
355 actual_y = y - list_text[screen].y;
356 line_height = font_get(gui_list->parent[screen]->font)->height;
357 line = actual_y / line_height;
359 /* Pressed below the list*/
360 if (gui_list->start_item[screen]+line >= gui_list->nb_items)
361 return ACTION_NONE;
363 /* Pressed a border */
364 if(UNLIKELY(actual_y % line_height == 0))
365 return ACTION_NONE;
367 if (line != (gui_list->selected_item - gui_list->start_item[screen])
368 && button ^ BUTTON_REL)
370 if(button & BUTTON_REPEAT)
371 scrolling = true;
373 gui_synclist_select_item(gui_list, gui_list->start_item[screen]
374 + line);
376 return ACTION_REDRAW;
379 if (button == (BUTTON_REPEAT|BUTTON_REL))
381 if(!scrolling)
383 /* Pen was hold on the same line as the
384 * previously selected one
385 * => simulate long button press
387 return ACTION_STD_CONTEXT;
389 else
391 /* Pen was moved across several lines and then released on
392 * this one
393 * => do nothing
395 scrolling = false;
396 return ACTION_NONE;
399 else if(button == BUTTON_REL &&
400 line == gui_list->selected_item - gui_list->start_item[screen])
402 /* Pen was released on either the same line as the previously
403 * selected one or an other one
404 * => simulate short press
406 return ACTION_STD_OK;
408 else
409 return ACTION_NONE;
411 /* Everything above the items is cancel */
412 else if (y < list_text[screen].y && button == BUTTON_REL)
413 return ACTION_STD_CANCEL;
415 return ACTION_NONE;
417 #endif