summaryrefslogtreecommitdiff
path: root/apps/plugins/text_viewer/tv_window.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/text_viewer/tv_window.c')
-rw-r--r--apps/plugins/text_viewer/tv_window.c394
1 files changed, 394 insertions, 0 deletions
diff --git a/apps/plugins/text_viewer/tv_window.c b/apps/plugins/text_viewer/tv_window.c
new file mode 100644
index 0000000000..43a24ca4bc
--- /dev/null
+++ b/apps/plugins/text_viewer/tv_window.c
@@ -0,0 +1,394 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Gilles Roux
11 * 2003 Garrett Derner
12 * 2010 Yoshihisa Uchida
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
21 *
22 ****************************************************************************/
23#include "plugin.h"
24#include "tv_bookmark.h"
25#include "tv_preferences.h"
26#include "tv_screen_pos.h"
27#include "tv_text_reader.h"
28#include "tv_window.h"
29
30#define TV_WINDOWS_PER_SCREEN 2
31
32#define TV_SCROLLBAR_WIDTH rb->global_settings->scrollbar_width
33
34#ifndef HAVE_LCD_BITMAP
35#define TV_BOOKMARK_ICON 0xe101
36#endif
37
38#ifdef HAVE_LCD_BITMAP
39static int header_height;
40static int footer_height;
41static bool need_scrollbar = false;
42#endif
43
44static int start_width;
45static int display_lines;
46
47static int window_width;
48static int window_columns;
49static int col_width;
50
51static int max_windows;
52
53static int cur_window;
54static int cur_column;
55
56static const struct tv_preferences *prefs = NULL;
57
58#ifdef HAVE_LCD_BITMAP
59static bool tv_set_font(const unsigned char *font)
60{
61 unsigned char path[MAX_PATH];
62
63 if (font != NULL && *font != '\0')
64 {
65 rb->snprintf(path, MAX_PATH, "%s/%s.fnt", FONT_DIR, font);
66 if (rb->font_load(NULL, path) < 0)
67 {
68 rb->splash(HZ/2, "font load failed");
69 return false;
70 }
71 }
72 return true;
73}
74
75static void tv_check_header_and_footer(void)
76{
77 struct tv_preferences new_prefs;
78
79 tv_copy_preferences(&new_prefs);
80
81 if (rb->global_settings->statusbar != STATUSBAR_TOP)
82 {
83 if (new_prefs.header_mode == HD_SBAR)
84 new_prefs.header_mode = HD_NONE;
85 else if (new_prefs.header_mode == HD_BOTH)
86 new_prefs.header_mode = HD_PATH;
87 }
88 if (rb->global_settings->statusbar != STATUSBAR_BOTTOM)
89 {
90 if (new_prefs.footer_mode == FT_SBAR)
91 new_prefs.footer_mode = FT_NONE;
92 else if (new_prefs.footer_mode == FT_BOTH)
93 new_prefs.footer_mode = FT_PAGE;
94 }
95 tv_set_preferences(&new_prefs);
96}
97
98static void tv_show_header(void)
99{
100 if (prefs->header_mode == HD_SBAR || prefs->header_mode == HD_BOTH)
101 rb->gui_syncstatusbar_draw(rb->statusbars, true);
102
103 if (prefs->header_mode == HD_PATH || prefs->header_mode == HD_BOTH)
104 rb->lcd_putsxy(0, header_height - prefs->font->height, prefs->file_name);
105}
106
107static void tv_show_footer(const struct tv_screen_pos *pos)
108{
109 unsigned char buf[12];
110
111 if (prefs->footer_mode == FT_SBAR || prefs->footer_mode == FT_BOTH)
112 rb->gui_syncstatusbar_draw(rb->statusbars, true);
113
114 if (prefs->footer_mode == FT_PAGE || prefs->footer_mode == FT_BOTH)
115 {
116 if (pos->line == 0)
117 rb->snprintf(buf, sizeof(buf), "%d", pos->page + 1);
118 else
119 rb->snprintf(buf, sizeof(buf), "%d - %d", pos->page + 1, pos->page + 2);
120
121 rb->lcd_putsxy(0, LCD_HEIGHT - footer_height, buf);
122 }
123}
124
125static void tv_show_scrollbar(off_t cur_pos, int size)
126{
127 int items;
128 int min_shown;
129 int max_shown;
130 int sb_begin_y;
131 int sb_height;
132
133 if (!need_scrollbar)
134 return;
135
136 items = (int) tv_get_total_text_size();
137 min_shown = (int) cur_pos;
138
139 max_shown = min_shown + size;
140
141 sb_begin_y = header_height;
142 sb_height = LCD_HEIGHT - header_height - footer_height;
143
144 rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],0, sb_begin_y,
145 TV_SCROLLBAR_WIDTH-1, sb_height,
146 items, min_shown, max_shown, VERTICAL);
147}
148
149static int tv_calc_display_lines(void)
150{
151 header_height = (prefs->header_mode == HD_SBAR || prefs->header_mode == HD_BOTH)?
152 STATUSBAR_HEIGHT : 0;
153
154 footer_height = (prefs->footer_mode == FT_SBAR || prefs->footer_mode == FT_BOTH)?
155 STATUSBAR_HEIGHT : 0;
156
157 if (prefs->header_mode == HD_NONE || prefs->header_mode == HD_PATH ||
158 prefs->footer_mode == FT_NONE || prefs->footer_mode == FT_PAGE)
159 rb->gui_syncstatusbar_draw(rb->statusbars, false);
160
161 if (prefs->header_mode == HD_PATH || prefs->header_mode == HD_BOTH)
162 header_height += prefs->font->height;
163
164 if (prefs->footer_mode == FT_PAGE || prefs->footer_mode == FT_BOTH)
165 footer_height += prefs->font->height;
166
167 return (LCD_HEIGHT - header_height - footer_height) / prefs->font->height;
168}
169#endif
170
171static void tv_show_bookmarks(const struct tv_screen_pos *top_pos)
172{
173 struct tv_screen_pos bookmarks[TV_MAX_BOOKMARKS];
174 int count = tv_get_bookmark_positions(bookmarks);
175 int line;
176
177#ifdef HAVE_LCD_BITMAP
178 rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
179#endif
180 while (count--)
181 {
182 line = (bookmarks[count].page - top_pos->page) * display_lines
183 + (bookmarks[count].line - top_pos->line);
184 if (line >= 0 && line < display_lines)
185 {
186#ifdef HAVE_LCD_BITMAP
187 rb->lcd_fillrect(start_width, header_height + line * prefs->font->height,
188 window_width, prefs->font->height);
189#else
190 rb->lcd_putc(start_width - 1, line, TV_BOOKMARK_ICON);
191#endif
192 }
193 }
194#ifdef HAVE_LCD_BITMAP
195 rb->lcd_set_drawmode(DRMODE_SOLID);
196#endif
197}
198
199void tv_draw_window(void)
200{
201 struct tv_screen_pos pos;
202 const unsigned char *line_buf;
203 int line;
204 int offset = cur_column * col_width;
205 int size = 0;
206 int line_width;
207 int draw_width = (max_windows - cur_window) * LCD_WIDTH - offset;
208 int dx = start_width - offset;
209
210 tv_copy_screen_pos(&pos);
211 rb->lcd_clear_display();
212
213 if (prefs->alignment == LEFT)
214 tv_read_start(cur_window, (cur_column > 0));
215 else
216 tv_read_start(0, prefs->view_mode == WIDE);
217
218 for (line = 0; line < display_lines; line++)
219 {
220 if (!tv_get_next_line(&line_buf))
221 break;
222
223 if (prefs->alignment == RIGHT)
224 {
225 rb->lcd_getstringsize(line_buf, &line_width, NULL);
226 dx = draw_width - line_width;
227 }
228
229#ifdef HAVE_LCD_BITMAP
230 rb->lcd_putsxy(dx, header_height + line * prefs->font->height, line_buf);
231#else
232 rb->lcd_puts(dx, line, line_buf);
233#endif
234 }
235
236 size = tv_read_end();
237
238 tv_show_bookmarks(&pos);
239
240#ifdef HAVE_LCD_BITMAP
241 tv_show_scrollbar(pos.file_pos, size);
242 tv_show_header();
243 tv_show_footer(&pos);
244#endif
245
246 rb->lcd_update();
247}
248
249bool tv_traverse_lines(void)
250{
251 int i;
252 bool res = true;
253
254 tv_read_start(0, false);
255 for (i = 0; i < display_lines; i++)
256 {
257 if (!tv_get_next_line(NULL))
258 {
259 res = false;
260 break;
261 }
262 }
263 tv_read_end();
264 return res;
265}
266
267static void tv_change_preferences(const struct tv_preferences *oldp)
268{
269#ifdef HAVE_LCD_BITMAP
270 static bool is_executing = false;
271
272 is_executing = true;
273
274 /* change font */
275 if (oldp == NULL || rb->strcmp(oldp->font_name, prefs->font_name))
276 {
277 if (!tv_set_font(prefs->font_name))
278 {
279 struct tv_preferences new_prefs = *prefs;
280 const unsigned char *font_str;
281
282 font_str = (oldp == NULL)? rb->global_settings->font_file : oldp->font_name;
283
284 if (!tv_set_font(font_str) && oldp != NULL)
285 {
286 font_str = rb->global_settings->font_file;
287 tv_set_font(new_prefs.font_name);
288 }
289
290 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
291 tv_set_preferences(&new_prefs);
292 }
293 }
294
295 /* calculates display lines */
296 tv_check_header_and_footer();
297 display_lines = tv_calc_display_lines();
298
299 if (!is_executing)
300 return;
301
302 is_executing = false;
303#else
304 (void)oldp;
305
306 /* REAL fixed pitch :) all chars use up 1 cell */
307 display_lines = 2;
308#endif
309
310#ifdef HAVE_LCD_BITMAP
311 col_width = 2 * rb->font_get_width(prefs->font, ' ');
312#else
313 col_width = 1;
314#endif
315
316 max_windows = (prefs->view_mode == NARROW)? 1: TV_WINDOWS_PER_SCREEN;
317 if (cur_window >= max_windows)
318 cur_window = 0;
319
320 window_width = LCD_WIDTH;
321#ifdef HAVE_LCD_BITMAP
322 need_scrollbar = false;
323 start_width = 0;
324 tv_seek_top();
325 tv_set_read_conditions(max_windows, window_width);
326 if (tv_traverse_lines() && prefs->scrollbar_mode)
327 {
328 need_scrollbar = true;
329 start_width = TV_SCROLLBAR_WIDTH;
330 }
331 tv_seek_top();
332#else
333 start_width = 1;
334#endif
335 window_width -= start_width;
336 window_columns = window_width / col_width;
337
338 cur_column = 0;
339
340 tv_set_read_conditions(max_windows, window_width);
341}
342
343bool tv_init_window(unsigned char *buf, size_t bufsize, size_t *used_size)
344{
345 tv_add_preferences_change_listner(tv_change_preferences);
346 if (!tv_init_text_reader(buf, bufsize, used_size))
347 return false;
348
349 prefs = tv_get_preferences();
350 return true;
351}
352
353void tv_finalize_window(void)
354{
355 tv_finalize_text_reader();
356}
357
358void tv_move_window(int window_delta, int column_delta)
359{
360 cur_window += window_delta;
361 cur_column += column_delta;
362
363 if (cur_window < 0)
364 {
365 cur_window = 0;
366 cur_column = 0;
367 }
368 else if (cur_window >= max_windows)
369 {
370 cur_window = max_windows - 1;
371 cur_column = 0;
372 }
373
374 if (cur_column < 0)
375 {
376 if (cur_window == 0)
377 cur_column = 0;
378 else
379 {
380 cur_window--;
381 cur_column = window_columns - 1;
382 }
383 }
384 else
385 {
386 if (cur_window == max_windows - 1)
387 cur_column = 0;
388 else if (cur_column >= window_columns)
389 {
390 cur_window++;
391 cur_column = 0;
392 }
393 }
394}