summaryrefslogtreecommitdiffstats
path: root/apps/plugins/lib/simple_viewer.c
blob: 4c05ee68c50e121197949bc2f239ad843e31f9a0 (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
/***************************************************************************
*             __________               __   ___.
*   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
*   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
*   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
*   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
*                     \/            \/     \/    \/            \/
* $Id$
*
* Copyright (C) 2010 Teruaki Kawashima
*
* 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 "config.h"
#include "plugin.h"
#include "pluginlib_actions.h"
#include "simple_viewer.h"
#include <ctype.h>


struct view_info {
#ifdef HAVE_LCD_BITMAP
    struct font* pf;
    struct viewport scrollbar_vp; /* viewport for scrollbar */
#endif
    struct viewport vp;
    const char *title;
    const char *text;   /* displayed text */
    int display_lines;  /* number of lines can be displayed */
    int line_count;     /* number of lines */
    int line;           /* current first line */
    int start;          /* possition of first line in text  */
};

static bool isbrchr(const unsigned char *str, int len)
{
    const unsigned char *p = "!,-.:;? 、。!,.:;?―";
    if (isspace(*str))
        return true;

    while(*p)
    {
        int n = rb->utf8seek(p, 1);
        if (len == n && !rb->strncmp(p, str, len))
            return true;
        p += n;
    }
    return false;
}

static const char* get_next_line(const char *text, struct view_info *info)
{
    const char *ptr = text;
    const char *space = NULL;
    int total, n, w;
    total = 0;
    while(*ptr)
    {
        unsigned short ch;
        n = ((intptr_t)rb->utf8decode(ptr, &ch) - (intptr_t)ptr);
        if (rb->is_diacritic(ch, NULL))
            w = 0;
        else
            w = rb->font_get_width(info->pf, ch);
        if (isbrchr(ptr, n))
            space = ptr+(isspace(*ptr) || total + w <= info->vp.width? n: 0);
        if (*ptr == '\n')
        {
            ptr += n;
            break;
        }
        if (total + w > info->vp.width)
            break;
        ptr += n;
        total += w;
    }
    return *ptr && space? space: ptr;
}

static void calc_line_count(struct view_info *info)
{
    const char *ptr = info->text;
    int i = 0;
#ifdef HAVE_LCD_BITMAP
    bool scrollbar = false;
#endif

    while (*ptr)
    {
        ptr = get_next_line(ptr, info);
        i++;
#ifdef HAVE_LCD_BITMAP
        if (!scrollbar && i > info->display_lines)
        {
            ptr = info->text;
            i = 0;
            info->scrollbar_vp = info->vp;
            info->scrollbar_vp.width = rb->global_settings->scrollbar_width;
            info->vp.width -= info->scrollbar_vp.width;
            if (rb->global_settings->scrollbar != SCROLLBAR_RIGHT)
                info->vp.x = info->scrollbar_vp.width;
            else
                info->scrollbar_vp.x = info->vp.width;
            scrollbar = true;
        }
#endif
    }
    info->line_count = i;
}

static void calc_first_line(struct view_info *info, int line)
{
    const char *ptr = info->text;
    int i = 0;

    if (line > info->line_count - info->display_lines)
        line = info->line_count - info->display_lines;
    if (line < 0)
        line = 0;

    if (info->line <= line)
    {
        ptr += info->start;
        i = info->line;
    }
    while (*ptr && i < line)
    {
        ptr = get_next_line(ptr, info);
        i++;
    }
    info->start = ptr - info->text;
    info->line = i;
}

static int init_view(struct view_info *info,
                     const char *title, const char *text)
{
    rb->viewport_set_defaults(&info->vp, SCREEN_MAIN);
#ifdef HAVE_LCD_BITMAP
    info->pf = rb->font_get(FONT_UI);
    info->display_lines = info->vp.height / info->pf->height;
#else
    info->display_lines = info->vp.height;
#endif

    info->title = title;
    info->text = text;
    info->line_count = 0;
    info->line = 0;
    info->start = 0;

#ifdef HAVE_LCD_BITMAP
    /* no title for small screens. */
    if (info->display_lines < 4)
    {
        info->title = NULL;
    }
    else
    {
        info->display_lines--;
        info->vp.y += info->pf->height;
        info->vp.height -= info->pf->height;
    }
#endif

    calc_line_count(info);
    return 0;
}

static void draw_text(struct view_info *info)
{
#ifdef HAVE_LCD_BITMAP
#define OUTPUT_SIZE LCD_WIDTH+1
#else
#define OUTPUT_SIZE LCD_WIDTH*3+1
#endif
    static char output[OUTPUT_SIZE];
    const char *text, *ptr;
    int max_show, line;
    struct screen* display = rb->screens[SCREEN_MAIN];

    /* clear screen */
    display->clear_display();

#ifdef HAVE_LCD_BITMAP
    /* display title. */
    if(info->title)
    {
        display->set_viewport(NULL);
        display->puts(0, 0, info->title);
    }
#endif

    max_show = MIN(info->line_count - info->line, info->display_lines);
    text = info->text + info->start;

    display->set_viewport(&info->vp);
    for (line = 0; line < max_show; line++)
    {
        int len;
        ptr = get_next_line(text, info);
        len = ptr-text;
        while(len > 0 && isspace(text[len-1]))
            len--;
        rb->memcpy(output, text, len);
        output[len] = 0;
        display->puts(0, line, output);
        text = ptr;
    }
#ifdef HAVE_LCD_BITMAP
    if (info->line_count > info->display_lines)
    {
        display->set_viewport(&info->scrollbar_vp);
        rb->gui_scrollbar_draw(display, (info->scrollbar_vp.width? 0: 1), 0,
                info->scrollbar_vp.width - 1, info->scrollbar_vp.height,
                info->line_count, info->line, info->line + max_show,
                VERTICAL);
    }
#endif

    display->set_viewport(NULL);
    display->update();
}

static void scroll_up(struct view_info *info, int n)
{
    if (info->line <= 0)
        return;

    calc_first_line(info, info->line-n);
    draw_text(info);
}

static void scroll_down(struct view_info *info, int n)
{
    if (info->line + info->display_lines >= info->line_count)
        return;

    calc_first_line(info, info->line+n);
    draw_text(info);
}

static void scroll_to_top(struct view_info *info)
{
    if (info->line <= 0)
        return;

    calc_first_line(info, 0);
    draw_text(info);
}

static void scroll_to_bottom(struct view_info *info)
{
    if (info->line + info->display_lines >= info->line_count)
        return;

    calc_first_line(info, info->line_count - info->display_lines);
    draw_text(info);
}

int view_text(const char *title, const char *text)
{
    struct view_info info;
    const struct button_mapping *view_contexts[] = {
        pla_main_ctx,
    };
    int button;

    init_view(&info, title, text);
    draw_text(&info);

    /* wait for keypress */
    while(1)
    {
        button = pluginlib_getaction(TIMEOUT_BLOCK, view_contexts,
                                     ARRAYLEN(view_contexts));
        switch (button)
        {
        case PLA_UP:
        case PLA_UP_REPEAT:
#ifdef HAVE_SCROLLWHEEL
        case PLA_SCROLL_BACK:
        case PLA_SCROLL_BACK_REPEAT:
#endif
            scroll_up(&info, 1);
            break;
        case PLA_DOWN:
        case PLA_DOWN_REPEAT:
#ifdef HAVE_SCROLLWHEEL
        case PLA_SCROLL_FWD:
        case PLA_SCROLL_FWD_REPEAT:
#endif
            scroll_down(&info, 1);
            break;
        case PLA_LEFT:
            scroll_up(&info, info.display_lines);
            break;
        case PLA_RIGHT:
            scroll_down(&info, info.display_lines);
            break;
        case PLA_LEFT_REPEAT:
            scroll_to_top(&info);
            break;
        case PLA_RIGHT_REPEAT:
            scroll_to_bottom(&info);
            break;
        case PLA_EXIT:
        case PLA_CANCEL:
            return PLUGIN_OK;
        default:
            if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                return PLUGIN_USB_CONNECTED;
            break;
        }
   }

    return PLUGIN_OK;
}