text viewer: some modify text viewer's layout and preferences.
[kugel-rb.git] / apps / plugins / text_viewer / tv_display.c
blob253bb0b22283a0be6d22636ddc02e241b0527ba0
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;
108 static int row_height;
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 void tv_fillrect(int col, int row, int rows)
174 display->fillrect(drawarea.x + col * col_width, drawarea.y + row * row_height,
175 drawarea.w - col * col_width, rows * row_height);
178 void tv_set_drawmode(int mode)
180 rb->lcd_set_drawmode(mode);
183 #endif
185 void tv_draw_text(int row, const unsigned char *text, int offset)
187 int xpos = -offset * col_width;
188 int text_width;
190 if (row < 0 || row >= display_rows)
191 return;
193 if (preferences->alignment == AL_RIGHT)
195 display->getstringsize(text, &text_width, NULL);
196 xpos += ((offset > 0)? drawarea.w * 2 : drawarea.w) - text_width;
199 #ifdef HAVE_LCD_BITMAP
200 display->putsxy(drawarea.x + xpos, drawarea.y + row * row_height, text);
201 #else
202 display->puts(drawarea.x + xpos, drawarea.y + row, text);
203 #endif
206 #ifndef HAVE_LCD_BITMAP
207 void tv_put_bookmark_icon(int row)
209 display->putchar(bookmark.x, drawarea.y + row, TV_BOOKMARK_ICON);
211 #endif
213 void tv_init_display(void)
215 display = rb->screens[SCREEN_MAIN];
216 display->clear_viewport();
219 void tv_start_display(void)
221 display->set_viewport(&vp_info);
222 #ifdef HAVE_LCD_BITMAP
223 drawmode = rb->lcd_get_drawmode();
224 #endif
227 void tv_end_display(void)
229 #ifdef HAVE_LCD_BITMAP
230 rb->lcd_set_drawmode(drawmode);
231 #endif
232 display->set_viewport(NULL);
235 void tv_clear_display(void)
237 rb->lcd_set_backdrop(NULL);
238 display->clear_viewport();
241 void tv_update_display(void)
243 display->update_viewport();
246 #ifdef HAVE_LCD_BITMAP
247 void tv_set_layout(int col_w, bool show_scrollbar)
248 #else
249 void tv_set_layout(int col_w)
250 #endif
252 #ifdef HAVE_LCD_BITMAP
253 int scrollbar_width = (show_scrollbar)? TV_SCROLLBAR_WIDTH + 1 : 0;
254 int scrollbar_height = (preferences->horizontal_scrollbar)? TV_SCROLLBAR_HEIGHT + 1 : 0;
256 row_height = preferences->font->height;
258 header.x = 0;
259 header.y = 1;
260 header.w = vp_info.width;
261 header.h = (preferences->header_mode == HD_PATH)? row_height + 1 : 0;
263 footer.x = 0;
264 footer.w = vp_info.width;
265 footer.h = (preferences->footer_mode == FT_PAGE)? row_height + 1 : 0;
266 footer.y = vp_info.height - 1 - footer.h;
268 drawarea.x = scrollbar_width;
269 drawarea.y = header.y + header.h;
270 drawarea.w = vp_info.width - scrollbar_width;
271 drawarea.h = footer.y - drawarea.y - scrollbar_height;
273 horizontal_scrollbar.x = drawarea.x;
274 horizontal_scrollbar.y = footer.y - scrollbar_height;
275 horizontal_scrollbar.w = drawarea.w;
276 horizontal_scrollbar.h = scrollbar_height;
278 vertical_scrollbar.x = 0;
279 vertical_scrollbar.y = drawarea.y;
280 vertical_scrollbar.w = scrollbar_width;
281 vertical_scrollbar.h = drawarea.h;
282 #else
283 row_height = 1;
285 bookmark.x = 0;
286 bookmark.y = 0;
287 bookmark.w = 1;
288 bookmark.h = vp_info.height;
290 drawarea.x = 1;
291 drawarea.y = 0;
292 drawarea.w = vp_info.width - 1;
293 drawarea.h = vp_info.height;
294 #endif
295 col_width = col_w;
297 display_columns = drawarea.w / col_width;
298 display_rows = drawarea.h / row_height;
301 void tv_get_drawarea_info(int *width, int *cols, int *rows)
303 *width = drawarea.w;
304 *cols = display_columns;
305 *rows = display_rows;
308 void tv_change_viewport(void)
310 #ifdef HAVE_LCD_BITMAP
311 struct viewport vp;
313 if (is_initialized_vp)
314 tv_undo_viewport();
315 else
316 is_initialized_vp = true;
318 rb->viewportmanager_theme_enable(SCREEN_MAIN, preferences->statusbar, &vp);
319 vp_info = vp;
320 vp_info.flags &= ~VP_FLAG_ALIGNMENT_MASK;
322 #else
323 if (!is_initialized_vp)
325 rb->viewport_set_defaults(&vp_info, SCREEN_MAIN);
326 is_initialized_vp = true;
328 #endif
331 void tv_undo_viewport(void)
333 #ifdef HAVE_LCD_BITMAP
334 if (is_initialized_vp)
335 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
336 #endif