text viewer: display functions more changes.
[kugel-rb.git] / apps / plugins / text_viewer / tv_display.c
blob0bbaf42cfeadbd6e2159b136f83c7977433dc132
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 "tv_display.h"
22 #include "tv_preferences.h"
25 * display layout
27 * when is defined HAVE_LCD_BITMAP
28 * +-------------------------+
29 * |statusbar (1) |
30 * +-------------------------+
31 * |filename (2) |
32 * +---+---------------------+
33 * |s | |
34 * |c | |
35 * |r | draw area |
36 * |o | |
37 * |l | |
38 * |l | |
39 * |b | |
40 * |a | |
41 * |r | |
42 * |(3)| |
43 * +---+---------------------+
44 * | |scrollbar (4) |
45 * +---+---------------------+
46 * |page (5) |
47 * +-------------------------+
48 * |statusbar (6) |
49 * +-------------------------+
51 * (1) displays when rb->global_settings->statusbar == STATUSBAR_TOP.
52 * (2) displays when preferences->header_mode is HD_PATH.
53 * (3) displays when preferences->vertical_scrollbar is SB_ON.
54 * (4) displays when preferences->horizontal_scrollbar is SB_ON.
55 * (5) displays when preferences->footer_mode is FT_PAGE.
56 * (6) displays when rb->global_settings->statusbar == STATUSBAR_BOTTOM.
59 * when isn't defined HAVE_LCD_BITMAP
60 * +---+---------------------+
61 * | | |
62 * |(7)| draw area |
63 * | | |
64 * +---+---------------------+
65 * (7) bookmark
68 #define TV_SCROLLBAR_WIDTH rb->global_settings->scrollbar_width
69 #define TV_SCROLLBAR_HEIGHT 4
71 #ifndef HAVE_LCD_BITMAP
72 #define TV_BOOKMARK_ICON 0xe101
73 #endif
75 struct tv_rect {
76 int x;
77 int y;
78 int w;
79 int h;
82 static struct viewport vp_info;
83 static bool is_initialized_vp = false;
85 #ifdef HAVE_LCD_BITMAP
86 static int drawmode = DRMODE_SOLID;
87 static int totalsize;
88 static bool show_vertical_scrollbar;
89 #endif
91 static struct screen* display;
93 /* layout */
94 #ifdef HAVE_LCD_BITMAP
95 static struct tv_rect header;
96 static struct tv_rect footer;
97 static struct tv_rect horizontal_scrollbar;
98 static struct tv_rect vertical_scrollbar;
99 #else
100 static struct tv_rect bookmark;
101 #endif
102 static struct tv_rect drawarea;
104 static int display_columns;
105 static int display_rows;
107 static int col_width = 1;
108 static int row_height = 1;
110 #ifdef HAVE_LCD_BITMAP
112 void tv_show_header(void)
114 unsigned header_mode = header_mode;
116 if (preferences->header_mode == HD_PATH)
117 display->putsxy(header.x, header.y, preferences->file_name);
120 void tv_show_footer(const struct tv_screen_pos *pos)
122 unsigned char buf[12];
123 unsigned footer_mode = preferences->footer_mode;
125 if (footer_mode == FT_PAGE)
127 if (pos->line == 0)
128 rb->snprintf(buf, sizeof(buf), "%d", pos->page + 1);
129 else
130 rb->snprintf(buf, sizeof(buf), "%d - %d", pos->page + 1, pos->page + 2);
131 display->putsxy(footer.x, footer.y + 1, buf);
135 void tv_init_scrollbar(off_t total, bool show_scrollbar)
137 totalsize = total;
138 show_vertical_scrollbar = show_scrollbar;
141 void tv_show_scrollbar(int window, int col, off_t cur_pos, int size)
143 int items;
144 int min_shown;
145 int max_shown;
147 if (preferences->horizontal_scrollbar)
149 items = preferences->windows * display_columns;
150 min_shown = window * display_columns + col;
151 max_shown = min_shown + display_columns;
153 rb->gui_scrollbar_draw(display,
154 horizontal_scrollbar.x, horizontal_scrollbar.y + 1,
155 horizontal_scrollbar.w, TV_SCROLLBAR_HEIGHT,
156 items, min_shown, max_shown, HORIZONTAL);
159 if (show_vertical_scrollbar)
161 items = (int) totalsize;
162 min_shown = (int) cur_pos;
163 max_shown = min_shown + size;
165 rb->gui_scrollbar_draw(display,
166 vertical_scrollbar.x, vertical_scrollbar.y,
167 TV_SCROLLBAR_WIDTH, vertical_scrollbar.h,
168 items, min_shown, max_shown, VERTICAL);
172 #endif
174 void tv_show_bookmarks(const int *rows, int count)
176 #ifdef HAVE_LCD_BITMAP
177 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
178 #endif
180 while (count--)
182 #ifdef HAVE_LCD_BITMAP
183 display->fillrect(drawarea.x, drawarea.y + rows[count] * row_height,
184 drawarea.w, row_height);
185 #else
186 display->putchar(bookmark.x, drawarea.y + rows[count], TV_BOOKMARK_ICON);
187 #endif
191 void tv_draw_text(int row, const unsigned char *text, int offset)
193 int xpos = -offset * col_width;
194 int text_width;
196 if (row < 0 || row >= display_rows)
197 return;
199 if (preferences->alignment == AL_RIGHT)
201 display->getstringsize(text, &text_width, NULL);
202 xpos += ((offset > 0)? drawarea.w * 2 : drawarea.w) - text_width;
205 #ifdef HAVE_LCD_BITMAP
206 display->putsxy(drawarea.x + xpos, drawarea.y + row * row_height, text);
207 #else
208 display->puts(drawarea.x + xpos, drawarea.y + row, text);
209 #endif
212 void tv_start_display(void)
214 display->set_viewport(&vp_info);
215 #ifdef HAVE_LCD_BITMAP
216 drawmode = rb->lcd_get_drawmode();
217 rb->lcd_set_drawmode(DRMODE_SOLID);
218 #endif
220 #if LCD_DEPTH > 1
221 rb->lcd_set_backdrop(NULL);
222 #endif
223 display->clear_viewport();
226 void tv_end_display(void)
228 #ifdef HAVE_LCD_BITMAP
229 rb->lcd_set_drawmode(drawmode);
230 #endif
231 display->set_viewport(NULL);
234 void tv_update_display(void)
236 display->update_viewport();
239 void tv_set_layout(bool show_scrollbar)
241 #ifdef HAVE_LCD_BITMAP
242 int scrollbar_width = (show_scrollbar)? TV_SCROLLBAR_WIDTH + 1 : 0;
243 int scrollbar_height = (preferences->horizontal_scrollbar)? TV_SCROLLBAR_HEIGHT + 1 : 0;
245 row_height = preferences->font->height;
247 header.x = 0;
248 header.y = 1;
249 header.w = vp_info.width;
250 header.h = (preferences->header_mode == HD_PATH)? row_height + 1 : 0;
252 footer.x = 0;
253 footer.w = vp_info.width;
254 footer.h = (preferences->footer_mode == FT_PAGE)? row_height + 1 : 0;
255 footer.y = vp_info.height - 1 - footer.h;
257 drawarea.x = scrollbar_width;
258 drawarea.y = header.y + header.h;
259 drawarea.w = vp_info.width - scrollbar_width;
260 drawarea.h = footer.y - drawarea.y - scrollbar_height;
262 horizontal_scrollbar.x = drawarea.x;
263 horizontal_scrollbar.y = footer.y - scrollbar_height;
264 horizontal_scrollbar.w = drawarea.w;
265 horizontal_scrollbar.h = scrollbar_height;
267 vertical_scrollbar.x = 0;
268 vertical_scrollbar.y = drawarea.y;
269 vertical_scrollbar.w = scrollbar_width;
270 vertical_scrollbar.h = drawarea.h;
271 #else
272 (void) show_scrollbar;
274 row_height = 1;
276 bookmark.x = 0;
277 bookmark.y = 0;
278 bookmark.w = 1;
279 bookmark.h = vp_info.height;
281 drawarea.x = 1;
282 drawarea.y = 0;
283 drawarea.w = vp_info.width - 1;
284 drawarea.h = vp_info.height;
285 #endif
287 display_columns = drawarea.w / col_width;
288 display_rows = drawarea.h / row_height;
291 void tv_get_drawarea_info(int *width, int *cols, int *rows)
293 *width = drawarea.w;
294 *cols = display_columns;
295 *rows = display_rows;
298 #ifdef HAVE_LCD_BITMAP
299 static void tv_undo_viewport(void)
301 if (is_initialized_vp)
302 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
305 static void tv_change_viewport(void)
307 struct viewport vp;
309 if (is_initialized_vp)
310 tv_undo_viewport();
311 else
312 is_initialized_vp = true;
314 rb->viewportmanager_theme_enable(SCREEN_MAIN, preferences->statusbar, &vp);
315 vp_info = vp;
316 vp_info.flags &= ~VP_FLAG_ALIGNMENT_MASK;
319 static bool tv_set_font(const unsigned char *font)
321 unsigned char path[MAX_PATH];
323 if (font != NULL && *font != '\0')
325 rb->snprintf(path, MAX_PATH, "%s/%s.fnt", FONT_DIR, font);
326 if (rb->font_load(NULL, path) < 0)
328 rb->splash(HZ/2, "font load failed");
329 return false;
332 return true;
334 #endif
336 static void tv_change_preferences(const struct tv_preferences *oldp)
338 #ifdef HAVE_LCD_BITMAP
339 static bool font_changing = false;
340 const unsigned char *font_str;
341 struct tv_preferences new_prefs;
343 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
345 /* change font */
346 if (font_changing || rb->strcmp(font_str, preferences->font_name))
348 if (!tv_set_font(preferences->font_name))
350 font_changing = true;
351 tv_copy_preferences(&new_prefs);
352 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
353 tv_set_preferences(&new_prefs);
354 font_changing = false;
356 col_width = 2 * rb->font_get_width(preferences->font, ' ');
359 tv_change_viewport();
360 #else
361 (void)oldp;
363 if (!is_initialized_vp)
365 rb->viewport_set_defaults(&vp_info, SCREEN_MAIN);
366 is_initialized_vp = true;
368 #endif
371 bool tv_init_display(unsigned char **buf, size_t *size)
373 (void)buf;
374 (void)size;
376 display = rb->screens[SCREEN_MAIN];
377 display->clear_viewport();
379 tv_add_preferences_change_listner(tv_change_preferences);
381 return true;
384 void tv_finalize_display(void)
386 #ifdef HAVE_LCD_BITMAP
387 /* restore font */
388 if (rb->strcmp(rb->global_settings->font_file, preferences->font_name))
390 tv_set_font(rb->global_settings->font_file);
393 /* undo viewport */
394 tv_undo_viewport();
395 #endif