diff options
Diffstat (limited to 'apps/plugins/textviewer/tv_screen.c')
-rw-r--r-- | apps/plugins/textviewer/tv_screen.c | 468 |
1 files changed, 0 insertions, 468 deletions
diff --git a/apps/plugins/textviewer/tv_screen.c b/apps/plugins/textviewer/tv_screen.c deleted file mode 100644 index ce2d82d59f..0000000000 --- a/apps/plugins/textviewer/tv_screen.c +++ /dev/null | |||
@@ -1,468 +0,0 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Gilles Roux, 2003 Garrett Derner | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "plugin.h" | ||
22 | #include "tv_screen.h" | ||
23 | #include "tv_settings.h" | ||
24 | |||
25 | static int draw_columns; /* number of (pixel) columns available for text */ | ||
26 | static int par_indent_spaces; /* number of spaces to indent first paragraph */ | ||
27 | |||
28 | #ifdef HAVE_LCD_BITMAP | ||
29 | static struct font *pf; | ||
30 | static int header_height = 0; | ||
31 | static int footer_height = 0; | ||
32 | #endif | ||
33 | |||
34 | void viewer_init_screen(void) | ||
35 | { | ||
36 | #ifdef HAVE_LCD_BITMAP | ||
37 | /* initialize fonts */ | ||
38 | pf = rb->font_get(FONT_UI); | ||
39 | draw_columns = display_columns = LCD_WIDTH; | ||
40 | #else | ||
41 | /* REAL fixed pitch :) all chars use up 1 cell */ | ||
42 | display_lines = 2; | ||
43 | draw_columns = display_columns = 11; | ||
44 | par_indent_spaces = 2; | ||
45 | #endif | ||
46 | } | ||
47 | |||
48 | void viewer_finalize_screen(void) | ||
49 | { | ||
50 | change_font(rb->global_settings->font_file); | ||
51 | } | ||
52 | |||
53 | void viewer_draw(void) | ||
54 | { | ||
55 | } | ||
56 | |||
57 | void viewer_top(void) | ||
58 | { | ||
59 | /* Read top of file into buffer | ||
60 | and point screen pointer to top */ | ||
61 | if (file_pos != 0) | ||
62 | { | ||
63 | rb->splash(0, "Loading..."); | ||
64 | |||
65 | file_pos = 0; | ||
66 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ | ||
67 | fill_buffer(0, buffer, buffer_size); | ||
68 | } | ||
69 | |||
70 | screen_top_ptr = buffer; | ||
71 | cpage = 1; | ||
72 | cline = 1; | ||
73 | } | ||
74 | |||
75 | void viewer_bottom(void) | ||
76 | { | ||
77 | unsigned char *line_begin; | ||
78 | unsigned char *line_end; | ||
79 | |||
80 | rb->splash(0, "Loading..."); | ||
81 | |||
82 | if (last_screen_top_ptr) | ||
83 | { | ||
84 | cpage = lpage; | ||
85 | cline = 1; | ||
86 | screen_top_ptr = last_screen_top_ptr; | ||
87 | file_pos = last_file_pos; | ||
88 | fill_buffer(file_pos, buffer, buffer_size); | ||
89 | buffer_end = BUFFER_END(); | ||
90 | return; | ||
91 | } | ||
92 | |||
93 | line_end = screen_top_ptr; | ||
94 | |||
95 | while (!BUFFER_EOF() || !BUFFER_OOB(line_end)) | ||
96 | { | ||
97 | get_next_line_position(&line_begin, &line_end, NULL); | ||
98 | if (line_end == NULL) | ||
99 | break; | ||
100 | |||
101 | increment_current_line(); | ||
102 | if (cline == 1) | ||
103 | screen_top_ptr = line_end; | ||
104 | } | ||
105 | lpage = cpage; | ||
106 | cline = 1; | ||
107 | last_screen_top_ptr = screen_top_ptr; | ||
108 | last_file_pos = file_pos; | ||
109 | buffer_end = BUFFER_END(); | ||
110 | } | ||
111 | |||
112 | static void increment_current_line(void) | ||
113 | { | ||
114 | if (cline < display_lines) | ||
115 | cline++; | ||
116 | else if (cpage < MAX_PAGE) | ||
117 | { | ||
118 | cpage++; | ||
119 | cline = 1; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | static void decrement_current_line(void) | ||
124 | { | ||
125 | if (cline > 1) | ||
126 | cline--; | ||
127 | else if (cpage > 1) | ||
128 | { | ||
129 | cpage--; | ||
130 | cline = display_lines; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | static void viewer_line_scroll_up(void) | ||
135 | { | ||
136 | unsigned char *p; | ||
137 | |||
138 | p = find_prev_line(screen_top_ptr); | ||
139 | if (p == NULL && !BUFFER_BOF()) { | ||
140 | read_and_synch(-1); | ||
141 | p = find_prev_line(screen_top_ptr); | ||
142 | } | ||
143 | if (p != NULL) | ||
144 | screen_top_ptr = p; | ||
145 | |||
146 | decrement_current_line(); | ||
147 | } | ||
148 | |||
149 | static void viewer_line_scroll_down(void) | ||
150 | { | ||
151 | if (cpage == lpage) | ||
152 | return; | ||
153 | |||
154 | if (next_line_ptr != NULL) | ||
155 | screen_top_ptr = next_line_ptr; | ||
156 | |||
157 | increment_current_line(); | ||
158 | } | ||
159 | |||
160 | static void viewer_scroll_to_top_line(void) | ||
161 | { | ||
162 | int line; | ||
163 | |||
164 | for (line = cline; line > 1; line--) | ||
165 | viewer_scroll_up(); | ||
166 | } | ||
167 | |||
168 | void viewer_scroll_up(int mode) | ||
169 | { | ||
170 | int i; | ||
171 | int line_count = 1; | ||
172 | struct viewer_preference *prefs = viewer_get_preference(); | ||
173 | |||
174 | if ((mode == VIEWER_SCROLL_PAGE) || | ||
175 | (mode == VIEWER_SCROLL_PREFS && prefs->scroll_mode == PAGE)) | ||
176 | { | ||
177 | #ifdef HAVE_LCD_BITMAP | ||
178 | line_count = display_lines - ((prefs->page_mode==OVERLAP)? 1:0); | ||
179 | #else | ||
180 | line_count = display_lines; | ||
181 | #endif | ||
182 | } | ||
183 | |||
184 | for (i = 0; i < line_count; i++) | ||
185 | viewer_line_scroll_up(); | ||
186 | } | ||
187 | |||
188 | void viewer_scroll_down(int mode) | ||
189 | { | ||
190 | struct viewer_preference *prefs = viewer_get_preference(); | ||
191 | |||
192 | if ((mode == VIEWER_SCROLL_PAGE) || | ||
193 | (mode == VIEWER_SCROLL_PREFS && prefs->scroll_mode == PAGE)) | ||
194 | { | ||
195 | /* Page down */ | ||
196 | if (next_screen_ptr != NULL) | ||
197 | { | ||
198 | screen_top_ptr = next_screen_to_draw_ptr; | ||
199 | if (cpage < MAX_PAGE) | ||
200 | cpage++; | ||
201 | } | ||
202 | } | ||
203 | else | ||
204 | viewer_line_scroll_down(); | ||
205 | } | ||
206 | |||
207 | #ifdef HAVE_LCD_BITMAP | ||
208 | static void viewer_scrollbar(void) { | ||
209 | int items, min_shown, max_shown, sb_begin_y, sb_height; | ||
210 | |||
211 | items = (int) file_size; /* (SH1 int is same as long) */ | ||
212 | min_shown = (int) file_pos + (screen_top_ptr - buffer); | ||
213 | |||
214 | if (next_screen_ptr == NULL) | ||
215 | max_shown = items; | ||
216 | else | ||
217 | max_shown = min_shown + (next_screen_ptr - screen_top_ptr); | ||
218 | |||
219 | sb_begin_y = header_height; | ||
220 | sb_height = LCD_HEIGHT - header_height - footer_height; | ||
221 | |||
222 | rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],0, sb_begin_y, | ||
223 | SCROLLBAR_WIDTH-1, sb_height, | ||
224 | items, min_shown, max_shown, VERTICAL); | ||
225 | } | ||
226 | |||
227 | static void viewer_show_header(void) | ||
228 | { | ||
229 | struct viewer_preference *prefs = viewer_get_preference(); | ||
230 | |||
231 | if (prefs->header_mode == HD_SBAR || prefs->header_mode == HD_BOTH) | ||
232 | rb->gui_syncstatusbar_draw(rb->statusbars, true); | ||
233 | |||
234 | if (prefs->header_mode == HD_PATH || prefs->header_mode == HD_BOTH) | ||
235 | rb->lcd_putsxy(0, header_height - pf->height, file_name); | ||
236 | } | ||
237 | |||
238 | static void viewer_show_footer(void) | ||
239 | { | ||
240 | struct viewer_preference *prefs = viewer_get_preference(); | ||
241 | |||
242 | if (prefs->footer_mode == FT_SBAR || prefs->footer_mode == FT_BOTH) | ||
243 | rb->gui_syncstatusbar_draw(rb->statusbars, true); | ||
244 | |||
245 | if (prefs->footer_mode == FT_PAGE || prefs->footer_mode == FT_BOTH) | ||
246 | { | ||
247 | unsigned char buf[12]; | ||
248 | |||
249 | if (cline == 1) | ||
250 | rb->snprintf(buf, sizeof(buf), "%d", cpage); | ||
251 | else | ||
252 | rb->snprintf(buf, sizeof(buf), "%d - %d", cpage, cpage+1); | ||
253 | |||
254 | rb->lcd_putsxy(0, LCD_HEIGHT - footer_height, buf); | ||
255 | } | ||
256 | } | ||
257 | |||
258 | static bool need_scrollbar(void) | ||
259 | { | ||
260 | struct viewer_preference *prefs = viewer_get_preference(); | ||
261 | |||
262 | return prefs->need_scrollbar; | ||
263 | } | ||
264 | |||
265 | static void init_need_scrollbar(void) { | ||
266 | draw_columns = need_scrollbar()? display_columns-SCROLLBAR_WIDTH : display_columns; | ||
267 | par_indent_spaces = draw_columns/(5*glyph_width(' ')); | ||
268 | calc_max_width(); | ||
269 | } | ||
270 | |||
271 | static void init_header_and_footer(void) | ||
272 | { | ||
273 | struct viewer_preference *prefs = viewer_get_preference(); | ||
274 | |||
275 | header_height = 0; | ||
276 | footer_height = 0; | ||
277 | if (rb->global_settings->statusbar == STATUSBAR_TOP) | ||
278 | { | ||
279 | if (prefs->header_mode == HD_SBAR || prefs->header_mode == HD_BOTH) | ||
280 | header_height = STATUSBAR_HEIGHT; | ||
281 | |||
282 | if (prefs->footer_mode == FT_SBAR) | ||
283 | prefs->footer_mode = FT_NONE; | ||
284 | else if (prefs->footer_mode == FT_BOTH) | ||
285 | prefs->footer_mode = FT_PAGE; | ||
286 | } | ||
287 | else if (rb->global_settings->statusbar == STATUSBAR_BOTTOM) | ||
288 | { | ||
289 | if (prefs->footer_mode == FT_SBAR || prefs->footer_mode == FT_BOTH) | ||
290 | footer_height = STATUSBAR_HEIGHT; | ||
291 | |||
292 | if (prefs->header_mode == HD_SBAR) | ||
293 | prefs->header_mode = HD_NONE; | ||
294 | else if (prefs->header_mode == HD_BOTH) | ||
295 | prefs->header_mode = HD_PATH; | ||
296 | } | ||
297 | else /* STATUSBAR_OFF || STATUSBAR_CUSTOM */ | ||
298 | { | ||
299 | if (prefs->header_mode == HD_SBAR) | ||
300 | prefs->header_mode = HD_NONE; | ||
301 | else if (prefs->header_mode == HD_BOTH) | ||
302 | prefs->header_mode = HD_PATH; | ||
303 | |||
304 | if (prefs->footer_mode == FT_SBAR) | ||
305 | prefs->footer_mode = FT_NONE; | ||
306 | else if (prefs->footer_mode == FT_BOTH) | ||
307 | prefs->footer_mode = FT_PAGE; | ||
308 | } | ||
309 | |||
310 | if (prefs->header_mode == HD_NONE || prefs->header_mode == HD_PATH || | ||
311 | prefs->footer_mode == FT_NONE || prefs->footer_mode == FT_PAGE) | ||
312 | rb->gui_syncstatusbar_draw(rb->statusbars, false); | ||
313 | |||
314 | if (prefs->header_mode == HD_PATH || prefs->header_mode == HD_BOTH) | ||
315 | header_height += pf->height; | ||
316 | if (prefs->footer_mode == FT_PAGE || prefs->footer_mode == FT_BOTH) | ||
317 | footer_height += pf->height; | ||
318 | |||
319 | display_lines = (LCD_HEIGHT - header_height - footer_height) / pf->height; | ||
320 | |||
321 | lpage = 0; | ||
322 | last_file_pos = 0; | ||
323 | last_screen_top_ptr = NULL; | ||
324 | } | ||
325 | |||
326 | void change_font(unsigned char *font) | ||
327 | { | ||
328 | unsigned char buf[MAX_PATH]; | ||
329 | struct viewer_preference *prefs = viewer_get_preference(); | ||
330 | |||
331 | if (font == NULL || *font == '\0') | ||
332 | return; | ||
333 | |||
334 | if (rb->strcmp(prefs->font, rb->global_settings->font_file) == 0) | ||
335 | return; | ||
336 | |||
337 | rb->snprintf(buf, MAX_PATH, "%s/%s.fnt", FONT_DIR, font); | ||
338 | if (rb->font_load(NULL, buf) < 0) | ||
339 | rb->splash(HZ/2, "font load failed."); | ||
340 | } | ||
341 | #else | ||
342 | #define change_font(f) | ||
343 | #endif | ||
344 | |||
345 | static void calc_page(void) | ||
346 | { | ||
347 | int i; | ||
348 | unsigned char *line_begin; | ||
349 | unsigned char *line_end; | ||
350 | off_t sfp; | ||
351 | unsigned char *sstp; | ||
352 | struct viewer_preference *prefs = viewer_get_preference(); | ||
353 | |||
354 | rb->splash(0, "Calculating page/line number..."); | ||
355 | |||
356 | /* add reading page to bookmarks */ | ||
357 | viewer_add_last_read_bookmark(); | ||
358 | |||
359 | rb->qsort(bookmarks, bookmark_count, sizeof(struct bookmark_info), | ||
360 | bm_comp); | ||
361 | |||
362 | cpage = 1; | ||
363 | cline = 1; | ||
364 | file_pos = 0; | ||
365 | screen_top_ptr = buffer; | ||
366 | buffer_end = BUFFER_END(); | ||
367 | |||
368 | fill_buffer(file_pos, buffer, buffer_size); | ||
369 | line_end = line_begin = buffer; | ||
370 | |||
371 | for (i = 0; i < bookmark_count; i++) | ||
372 | { | ||
373 | sfp = bookmarks[i].file_position; | ||
374 | sstp = buffer; | ||
375 | |||
376 | while ((line_begin > sstp || sstp >= line_end) || | ||
377 | (file_pos > sfp || sfp >= file_pos + BUFFER_END() - buffer)) | ||
378 | { | ||
379 | get_next_line_position(&line_begin, &line_end, NULL); | ||
380 | if (line_end == NULL) | ||
381 | break; | ||
382 | |||
383 | next_line_ptr = line_end; | ||
384 | |||
385 | if (sstp == buffer && | ||
386 | file_pos <= sfp && sfp < file_pos + BUFFER_END() - buffer) | ||
387 | sstp = sfp - file_pos + buffer; | ||
388 | |||
389 | increment_current_line(); | ||
390 | } | ||
391 | |||
392 | decrement_current_line(); | ||
393 | bookmarks[i].page = cpage; | ||
394 | bookmarks[i].line = cline; | ||
395 | bookmarks[i].file_position = file_pos + (line_begin - buffer); | ||
396 | increment_current_line(); | ||
397 | } | ||
398 | |||
399 | /* remove reading page's bookmark */ | ||
400 | for (i = 0; i < bookmark_count; i++) | ||
401 | { | ||
402 | if (bookmarks[i].flag & BOOKMARK_LAST) | ||
403 | { | ||
404 | int screen_pos; | ||
405 | int screen_top; | ||
406 | |||
407 | screen_pos = bookmarks[i].file_position; | ||
408 | screen_top = screen_pos % buffer_size; | ||
409 | file_pos = screen_pos - screen_top; | ||
410 | screen_top_ptr = buffer + screen_top; | ||
411 | |||
412 | cpage = bookmarks[i].page; | ||
413 | cline = bookmarks[i].line; | ||
414 | bookmarks[i].flag ^= BOOKMARK_LAST; | ||
415 | buffer_end = BUFFER_END(); | ||
416 | |||
417 | fill_buffer(file_pos, buffer, buffer_size); | ||
418 | |||
419 | if (bookmarks[i].flag == 0) | ||
420 | viewer_remove_bookmark(i); | ||
421 | |||
422 | if (prefs->scroll_mode == PAGE && cline > 1) | ||
423 | viewer_scroll_to_top_line(); | ||
424 | break; | ||
425 | } | ||
426 | } | ||
427 | } | ||
428 | |||
429 | static int col_limit(int col) | ||
430 | { | ||
431 | if (col < 0) | ||
432 | col = 0; | ||
433 | else | ||
434 | if (col >= max_width) | ||
435 | col = max_width - draw_columns; | ||
436 | |||
437 | return col; | ||
438 | } | ||
439 | |||
440 | void viewer_scroll_left(int mode) | ||
441 | { | ||
442 | if (mode == VIEWER_SCROLL_COLUMN) | ||
443 | { | ||
444 | /* Scroll left one column */ | ||
445 | col -= glyph_width('o'); | ||
446 | } | ||
447 | else | ||
448 | { | ||
449 | /* Screen left */ | ||
450 | col -= draw_columns; | ||
451 | } | ||
452 | col = col_limit(col); | ||
453 | } | ||
454 | |||
455 | void viewer_scroll_right(int mode) | ||
456 | { | ||
457 | if (mode == VIEWER_SCROLL_COLUMN) | ||
458 | { | ||
459 | /* Scroll right one column */ | ||
460 | col += glyph_width('o'); | ||
461 | } | ||
462 | else | ||
463 | { | ||
464 | /* Screen right */ | ||
465 | col += draw_columns; | ||
466 | } | ||
467 | col = col_limit(col); | ||
468 | } | ||