summaryrefslogtreecommitdiff
path: root/apps/plugins/text_viewer/tv_display.c
blob: 8d85ae2a72af5ce90baa0a4c443a0fd8bef056ef (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Yoshihisa Uchida
 *
 * 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 "tv_display.h"
#include "tv_preferences.h"

/*
 * display layout
 *
 * when is defined HAVE_LCD_BITMAP
 * +-------------------------+
 * |statusbar (1)            |
 * +-------------------------+
 * |filename  (2)            |
 * +---+---------------------+
 * |s  |                     |
 * |c  |                     |
 * |r  | draw area           |
 * |o  |                     |
 * |l  |                     |
 * |l  |                     |
 * |b  |                     |
 * |a  |                     |
 * |r  |                     |
 * |(3)|                     |
 * +---+---------------------+
 * |   |scrollbar (4)        |
 * +---+---------------------+
 * |page (5)                 |
 * +-------------------------+
 * |statusbar (6)            |
 * +-------------------------+
 *
 * (1) displays when rb->global_settings->statusbar == STATUSBAR_TOP.
 * (2) displays when preferences->header_mode is HD_PATH.
 * (3) displays when preferences->vertical_scrollbar is SB_ON.
 * (4) displays when preferences->horizontal_scrollbar is SB_ON.
 * (5) displays when preferences->footer_mode is FT_PAGE.
 * (6) displays when rb->global_settings->statusbar == STATUSBAR_BOTTOM.
 *
 *
 * when isn't defined HAVE_LCD_BITMAP
 * +---+---------------------+
 * |   |                     |
 * |(7)| draw area           |
 * |   |                     |
 * +---+---------------------+
 * (7) bookmark
 */

#define TV_SCROLLBAR_WIDTH  rb->global_settings->scrollbar_width
#define TV_SCROLLBAR_HEIGHT 4

#ifndef HAVE_LCD_BITMAP
#define TV_BOOKMARK_ICON 0xe101
#endif

struct tv_rect {
    int x;
    int y;
    int w;
    int h;
};

static struct viewport vp_info;
static bool is_initialized_vp = false;

#ifdef HAVE_LCD_BITMAP
static int drawmode = DRMODE_SOLID;
static int totalsize;
static bool show_vertical_scrollbar;
#endif

static struct screen* display;

/* layout */
#ifdef HAVE_LCD_BITMAP
static struct tv_rect header;
static struct tv_rect footer;
static struct tv_rect horizontal_scrollbar;
static struct tv_rect vertical_scrollbar;
#else
static struct tv_rect bookmark;
#endif
static struct tv_rect drawarea;

static int display_columns;
static int display_rows;

static int col_width;
static int row_height;

#ifdef HAVE_LCD_BITMAP

void tv_show_header(void)
{
    unsigned header_mode = header_mode;

    if (preferences->header_mode == HD_PATH)
        display->putsxy(header.x, header.y, preferences->file_name);
}

void tv_show_footer(const struct tv_screen_pos *pos)
{
    unsigned char buf[12];
    unsigned footer_mode = preferences->footer_mode;

    if (footer_mode == FT_PAGE)
    {
        if (pos->line == 0)
            rb->snprintf(buf, sizeof(buf), "%d", pos->page + 1);
        else
            rb->snprintf(buf, sizeof(buf), "%d - %d", pos->page + 1, pos->page + 2);
        display->putsxy(footer.x, footer.y + 1, buf);
    }
}

void tv_init_scrollbar(off_t total, bool show_scrollbar)
{
    totalsize = total;
    show_vertical_scrollbar = show_scrollbar;
}

void tv_show_scrollbar(int window, int col, off_t cur_pos, int size)
{
    int items;
    int min_shown;
    int max_shown;

    if (preferences->horizontal_scrollbar)
    {
        items     = preferences->windows * display_columns;
        min_shown = window * display_columns + col;
        max_shown = min_shown + display_columns;

        rb->gui_scrollbar_draw(display,
                               horizontal_scrollbar.x, horizontal_scrollbar.y + 1,
                               horizontal_scrollbar.w, TV_SCROLLBAR_HEIGHT,
                               items, min_shown, max_shown, HORIZONTAL);
    }

    if (show_vertical_scrollbar)
    {
        items     = (int) totalsize;
        min_shown = (int) cur_pos;
        max_shown = min_shown + size;

        rb->gui_scrollbar_draw(display,
                               vertical_scrollbar.x, vertical_scrollbar.y,
                               TV_SCROLLBAR_WIDTH, vertical_scrollbar.h,
                               items, min_shown, max_shown, VERTICAL);
    }
}

void tv_fillrect(int col, int row, int rows)
{
    display->fillrect(drawarea.x + col * col_width, drawarea.y + row * row_height,
                      drawarea.w - col * col_width, rows * row_height);
}

void tv_set_drawmode(int mode)
{
    rb->lcd_set_drawmode(mode);
}

#endif

void tv_draw_text(int row, const unsigned char *text, int offset)
{
    int xpos = -offset * col_width;
    int text_width;

    if (row < 0 || row >= display_rows)
        return;

    if (preferences->alignment == AL_RIGHT)
    {
        display->getstringsize(text, &text_width, NULL);
        xpos += ((offset > 0)? drawarea.w * 2 : drawarea.w) - text_width;
    }

#ifdef HAVE_LCD_BITMAP
    display->putsxy(drawarea.x + xpos, drawarea.y + row * row_height, text);
#else
    display->puts(drawarea.x + xpos, drawarea.y + row, text);
#endif
}

#ifndef HAVE_LCD_BITMAP
void tv_put_bookmark_icon(int row)
{
    display->putchar(bookmark.x, drawarea.y + row, TV_BOOKMARK_ICON);
}
#endif

void tv_init_display(void)
{
    display = rb->screens[SCREEN_MAIN];
    display->clear_viewport();
}

void tv_start_display(void)
{
    display->set_viewport(&vp_info);
#ifdef HAVE_LCD_BITMAP
    drawmode = rb->lcd_get_drawmode();
#endif
}

void tv_end_display(void)
{
#ifdef HAVE_LCD_BITMAP
    rb->lcd_set_drawmode(drawmode);
#endif
    display->set_viewport(NULL);
}

void tv_clear_display(void)
{
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif
    display->clear_viewport();
}

void tv_update_display(void)
{
    display->update_viewport();
}

#ifdef HAVE_LCD_BITMAP
void tv_set_layout(int col_w, bool show_scrollbar)
#else
void tv_set_layout(int col_w)
#endif
{
#ifdef HAVE_LCD_BITMAP
    int scrollbar_width  = (show_scrollbar)?                    TV_SCROLLBAR_WIDTH  + 1 : 0;
    int scrollbar_height = (preferences->horizontal_scrollbar)? TV_SCROLLBAR_HEIGHT + 1 : 0;

    row_height = preferences->font->height;

    header.x = 0;
    header.y = 1;
    header.w = vp_info.width;
    header.h = (preferences->header_mode == HD_PATH)? row_height + 1 : 0;

    footer.x = 0;
    footer.w = vp_info.width;
    footer.h = (preferences->footer_mode == FT_PAGE)? row_height + 1 : 0;
    footer.y = vp_info.height - 1 - footer.h;

    drawarea.x = scrollbar_width;
    drawarea.y = header.y + header.h;
    drawarea.w = vp_info.width - scrollbar_width;
    drawarea.h = footer.y - drawarea.y - scrollbar_height;

    horizontal_scrollbar.x = drawarea.x;
    horizontal_scrollbar.y = footer.y - scrollbar_height;
    horizontal_scrollbar.w = drawarea.w;
    horizontal_scrollbar.h = scrollbar_height;

    vertical_scrollbar.x = 0;
    vertical_scrollbar.y = drawarea.y;
    vertical_scrollbar.w = scrollbar_width;
    vertical_scrollbar.h = drawarea.h;
#else
    row_height = 1;

    bookmark.x = 0;
    bookmark.y = 0;
    bookmark.w = 1;
    bookmark.h = vp_info.height;

    drawarea.x = 1;
    drawarea.y = 0;
    drawarea.w = vp_info.width - 1;
    drawarea.h = vp_info.height;
#endif
    col_width = col_w;

    display_columns = drawarea.w / col_width;
    display_rows    = drawarea.h / row_height;
}

void tv_get_drawarea_info(int *width, int *cols, int *rows)
{
    *width = drawarea.w;
    *cols  = display_columns;
    *rows  = display_rows;
}

void tv_change_viewport(void)
{
#ifdef HAVE_LCD_BITMAP
    struct viewport vp;

    if (is_initialized_vp)
        tv_undo_viewport();
    else
        is_initialized_vp = true;

    rb->viewportmanager_theme_enable(SCREEN_MAIN, preferences->statusbar, &vp);
    vp_info = vp;
    vp_info.flags &= ~VP_FLAG_ALIGNMENT_MASK;

#else
    if (!is_initialized_vp)
    {
        rb->viewport_set_defaults(&vp_info, SCREEN_MAIN);
        is_initialized_vp = true;
    }
#endif
}

void tv_undo_viewport(void)
{
#ifdef HAVE_LCD_BITMAP
    if (is_initialized_vp)
        rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
#endif
}