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