FS#12756 by Marek Salaba - update Czech translation
[maemo-rb.git] / apps / plugins / text_viewer / tv_display.c
blob3376ccc548a2884ab4830063e681eadac3dc5dd8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "tv_display.h"
23 #include "tv_preferences.h"
26 * display layout
28 * when is defined HAVE_LCD_BITMAP
29 * +-------------------------+
30 * |statusbar (1) |
31 * +-------------------------+
32 * |filename (2) |
33 * +---+---------------------+
34 * |s | |
35 * |c | |
36 * |r | draw area |
37 * |o | |
38 * |l | |
39 * |l | |
40 * |b | |
41 * |a | |
42 * |r | |
43 * |(3)| |
44 * +---+---------------------+
45 * | |scrollbar (4) |
46 * +---+---------------------+
47 * |page (5) |
48 * +-------------------------+
49 * |statusbar (6) |
50 * +-------------------------+
52 * (1) displays when rb->global_settings->statusbar == STATUSBAR_TOP.
53 * (2) displays when preferences->header_mode is HD_PATH.
54 * (3) displays when preferences->vertical_scrollbar is SB_ON.
55 * (4) displays when preferences->horizontal_scrollbar is SB_ON.
56 * (5) displays when preferences->footer_mode is FT_PAGE.
57 * (6) displays when rb->global_settings->statusbar == STATUSBAR_BOTTOM.
60 * when isn't defined HAVE_LCD_BITMAP
61 * +---+---------------------+
62 * | | |
63 * |(7)| draw area |
64 * | | |
65 * +---+---------------------+
66 * (7) bookmark
69 #define TV_SCROLLBAR_WIDTH rb->global_settings->scrollbar_width
70 #define TV_SCROLLBAR_HEIGHT 4
72 #ifndef HAVE_LCD_BITMAP
73 #define TV_BOOKMARK_ICON 0xe101
74 #endif
76 struct tv_rect {
77 int x;
78 int y;
79 int w;
80 int h;
83 static struct viewport vp_info;
84 static struct viewport vp_text;
85 static bool is_initialized_vp = false;
87 static struct screen* display;
89 /* layout */
90 #ifdef HAVE_LCD_BITMAP
91 static struct tv_rect header;
92 static struct tv_rect footer;
93 static struct tv_rect horizontal_scrollbar;
94 static struct tv_rect vertical_scrollbar;
95 #else
96 static struct tv_rect bookmark;
97 #endif
99 static bool show_horizontal_scrollbar;
100 static bool show_vertical_scrollbar;
102 static int display_columns;
103 static int display_rows;
105 static int col_width = 1;
106 static int row_height = 1;
108 static int totalsize;
110 #ifdef HAVE_LCD_BITMAP
112 static void tv_show_header(void)
114 if (preferences->header_mode)
115 display->putsxy(header.x, header.y, preferences->file_name);
118 static void tv_show_footer(const struct tv_screen_pos *pos)
120 unsigned char buf[12];
122 if (preferences->footer_mode)
124 if (pos->line == 0)
125 rb->snprintf(buf, sizeof(buf), "%d", pos->page + 1);
126 else
127 rb->snprintf(buf, sizeof(buf), "%d - %d", pos->page + 1, pos->page + 2);
128 display->putsxy(footer.x, footer.y + 1, buf);
132 static void tv_show_scrollbar(int window, int col, off_t cur_pos, int size)
134 int items;
135 int min_shown;
136 int max_shown;
138 if (show_horizontal_scrollbar)
140 items = preferences->windows * display_columns;
141 min_shown = window * display_columns + col;
142 max_shown = min_shown + display_columns;
144 rb->gui_scrollbar_draw(display,
145 horizontal_scrollbar.x, horizontal_scrollbar.y + 1,
146 horizontal_scrollbar.w, TV_SCROLLBAR_HEIGHT,
147 items, min_shown, max_shown, HORIZONTAL);
150 if (show_vertical_scrollbar)
152 items = (int) totalsize;
153 min_shown = (int) cur_pos;
154 max_shown = min_shown + size;
156 rb->gui_scrollbar_draw(display,
157 vertical_scrollbar.x, vertical_scrollbar.y,
158 TV_SCROLLBAR_WIDTH, vertical_scrollbar.h,
159 items, min_shown, max_shown, VERTICAL);
163 #endif
165 void tv_init_scrollbar(off_t total, bool show_scrollbar)
167 totalsize = total;
168 show_horizontal_scrollbar = (show_scrollbar && preferences->horizontal_scrollbar);
169 show_vertical_scrollbar = (show_scrollbar && preferences->vertical_scrollbar);
172 void tv_show_bookmarks(const int *rows, int count)
174 #ifdef HAVE_LCD_BITMAP
175 display->set_viewport(&vp_text);
176 display->set_drawmode(DRMODE_COMPLEMENT);
177 #endif
179 while (count--)
181 #ifdef HAVE_LCD_BITMAP
182 display->fillrect(0, rows[count] * row_height,
183 vp_text.width, row_height);
184 #else
185 display->putchar(bookmark.x, bookmark.y + rows[count], TV_BOOKMARK_ICON);
186 #endif
188 #ifdef HAVE_LCD_BITMAP
189 display->set_drawmode(DRMODE_SOLID);
190 display->set_viewport(&vp_info);
191 #endif
194 void tv_update_extra(int window, int col, const struct tv_screen_pos *pos, int size)
196 #ifdef HAVE_LCD_BITMAP
197 tv_show_scrollbar(window, col, pos->file_pos, size);
198 tv_show_header();
199 tv_show_footer(pos);
200 #else
201 (void)window;
202 (void)col;
203 (void)pos;
204 (void)size;
205 #endif
208 void tv_draw_text(int row, const unsigned char *text, int offset)
210 int xpos = -offset * col_width;
211 int text_width;
213 if (row < 0 || row >= display_rows)
214 return;
216 if (preferences->alignment == AL_RIGHT)
218 display->getstringsize(text, &text_width, NULL);
219 xpos += ((offset > 0)? vp_text.width * 2 : vp_text.width) - text_width;
222 display->set_viewport(&vp_text);
223 #ifdef HAVE_LCD_BITMAP
224 display->putsxy(xpos, row * row_height, text);
225 #else
226 display->puts(xpos, row, text);
227 #endif
228 display->set_viewport(&vp_info);
231 void tv_start_display(void)
233 display->set_viewport(&vp_info);
234 #ifdef HAVE_LCD_BITMAP
235 display->set_drawmode(DRMODE_SOLID);
236 #endif
238 #if LCD_DEPTH > 1
239 rb->lcd_set_backdrop(NULL);
240 #endif
241 display->clear_viewport();
244 void tv_end_display(void)
246 display->update_viewport();
247 display->set_viewport(NULL);
250 void tv_set_layout(bool show_scrollbar)
252 #ifdef HAVE_LCD_BITMAP
253 int scrollbar_width = (show_scrollbar && preferences->vertical_scrollbar)?
254 TV_SCROLLBAR_WIDTH + 1 : 0;
255 int scrollbar_height = (show_scrollbar && preferences->horizontal_scrollbar)?
256 TV_SCROLLBAR_HEIGHT + 1 : 0;
258 row_height = rb->font_get(preferences->font_id)->height;
260 header.x = 0;
261 header.y = 0;
262 header.w = vp_info.width;
263 header.h = (preferences->header_mode)? row_height + 1 : 0;
265 footer.x = 0;
266 footer.w = vp_info.width;
267 footer.h = (preferences->footer_mode)? row_height + 1 : 0;
268 footer.y = vp_info.height - footer.h;
270 horizontal_scrollbar.x = scrollbar_width;
271 horizontal_scrollbar.y = footer.y - scrollbar_height;
272 horizontal_scrollbar.w = vp_info.width - scrollbar_width;
273 horizontal_scrollbar.h = scrollbar_height;
275 vertical_scrollbar.x = 0;
276 vertical_scrollbar.y = header.y + header.h;
277 vertical_scrollbar.w = scrollbar_width;
278 vertical_scrollbar.h = footer.y - vertical_scrollbar.y - scrollbar_height;
280 vp_info.font = preferences->font_id;
281 vp_text = vp_info;
282 vp_text.x += horizontal_scrollbar.x;
283 vp_text.y += vertical_scrollbar.y;
284 vp_text.width = horizontal_scrollbar.w;
285 vp_text.height = vertical_scrollbar.h;
286 #else
287 (void) show_scrollbar;
289 row_height = 1;
291 bookmark.x = 0;
292 bookmark.y = 0;
293 bookmark.w = 1;
294 bookmark.h = vp_info.height;
296 vp_text = vp_info;
297 vp_text.x += 1;
298 vp_text.width -= 1;
299 #endif
301 display_columns = vp_text.width / col_width;
302 display_rows = vp_text.height / row_height;
305 void tv_get_drawarea_info(int *width, int *cols, int *rows)
307 *width = vp_text.width;
308 *cols = display_columns;
309 *rows = display_rows;
312 static void tv_change_viewport(void)
314 #ifdef HAVE_LCD_BITMAP
315 bool show_statusbar = preferences->statusbar;
317 if (is_initialized_vp)
318 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
319 else
320 is_initialized_vp = true;
322 rb->viewportmanager_theme_enable(SCREEN_MAIN, show_statusbar, &vp_info);
323 vp_info.flags &= ~VP_FLAG_ALIGNMENT_MASK;
324 display->set_viewport(&vp_info);
325 #else
326 if (!is_initialized_vp)
328 rb->viewport_set_defaults(&vp_info, SCREEN_MAIN);
329 is_initialized_vp = true;
331 #endif
334 #ifdef HAVE_LCD_BITMAP
335 static bool tv_set_font(const unsigned char *font)
337 unsigned char path[MAX_PATH];
338 if (font != NULL && *font != '\0')
340 rb->snprintf(path, MAX_PATH, "%s/%s.fnt", FONT_DIR, font);
341 if (preferences->font_id >= 0 &&
342 (preferences->font_id != rb->global_status->font_id[SCREEN_MAIN]))
343 rb->font_unload(preferences->font_id);
344 tv_change_fontid(rb->font_load(path));
345 if (preferences->font_id < 0)
347 rb->splash(HZ/2, "font load failed");
348 return false;
351 vp_text.font = preferences->font_id;
352 return true;
354 #endif
356 static int tv_change_preferences(const struct tv_preferences *oldp)
358 #ifdef HAVE_LCD_BITMAP
359 static bool font_changing = false;
360 const unsigned char *font_str;
361 struct tv_preferences new_prefs;
363 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
365 /* change font */
366 if (font_changing || rb->strcmp(font_str, preferences->font_name))
368 if (!tv_set_font(preferences->font_name))
371 * tv_set_font(rb->global_settings->font_file) doesn't fail usually.
372 * if it fails, a fatal problem occurs in Rockbox.
374 if (!rb->strcmp(preferences->font_name, rb->global_settings->font_file))
375 return TV_CALLBACK_ERROR;
377 font_changing = true;
378 tv_copy_preferences(&new_prefs);
379 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
381 return (tv_set_preferences(&new_prefs))? TV_CALLBACK_STOP : TV_CALLBACK_ERROR;
383 col_width = 2 * rb->font_get_width(rb->font_get(preferences->font_id), ' ');
384 font_changing = false;
386 #else
387 (void)oldp;
388 #endif
389 tv_change_viewport();
390 return TV_CALLBACK_OK;
393 bool tv_init_display(unsigned char **buf, size_t *size)
395 (void)buf;
396 (void)size;
398 display = rb->screens[SCREEN_MAIN];
399 display->clear_viewport();
401 tv_add_preferences_change_listner(tv_change_preferences);
403 return true;
406 void tv_finalize_display(void)
408 #ifdef HAVE_LCD_BITMAP
409 /* restore font */
410 if (preferences->font_id >= 0 &&
411 (preferences->font_id != rb->global_status->font_id[SCREEN_MAIN]))
413 rb->font_unload(preferences->font_id);
416 /* undo viewport */
417 if (is_initialized_vp)
418 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
419 #endif
422 bool tv_exist_scrollbar(void)
424 #ifdef HAVE_LCD_BITMAP
425 return true;
426 #else
427 return false;
428 #endif