summaryrefslogtreecommitdiff
path: root/apps/plugins/textviewer/textviewer.c
blob: 289c741f968f74013d0a3d088851fc902c381ca8 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Gilles Roux
 *               2003 Garrett Derner
 *               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 "plugin.h"
#include "textviewer.h"
#include "tv_bookmark.h"
#include "tv_button.h"
#include "tv_menu.h"
#include "tv_readtext.h"
#include "tv_screen.h"
#include "tv_settings.h"

PLUGIN_HEADER

static bool viewer_init(const unsigned char *file)
{
    viewer_init_screen();
    viewer_init_buffer();

    if (!viewer_open(file))
        return false;

    /* Init mac_text value used in processing buffer */
    mac_text = false;

    return true;
}

static void viewer_exit(void *parameter)
{
    (void)parameter;

    /* save preference and bookmarks */
    if (!viewer_save_settings())
        rb->splash(HZ, "Can't save preference and bookmarks.");

    viewer_close();
    viewer_finalize_screen();
}

enum plugin_status plugin_start(const void* file)
{
    int button, i, ok;
    int lastbutton = BUTTON_NONE;
    bool autoscroll = false;
    long old_tick;

    old_tick = *rb->current_tick;

    if (!file)
        return PLUGIN_ERROR;

    ok = viewer_init(file);
    if (!ok) {
        rb->splash(HZ, "Error opening file.");
        return PLUGIN_ERROR;
    }

    viewer_load_settings(); /* load the preferences and bookmark */

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

    viewer_draw();

    while (!done) {

        if (autoscroll)
        {
            if(old_tick <= *rb->current_tick - (110-prefs.autoscroll_speed*10))
            {
                viewer_scroll_down(VIEWER_SCROLL_LINE);
                viewer_draw();
                old_tick = *rb->current_tick;
            }
        }

        button = rb->button_get_w_tmo(HZ/10);

        switch (button) {
            case VIEWER_MENU:
#ifdef VIEWER_MENU2
            case VIEWER_MENU2:
#endif
                viewer_menu();
                break;

            case VIEWER_AUTOSCROLL:
#ifdef VIEWER_AUTOSCROLL_PRE
                if (lastbutton != VIEWER_AUTOSCROLL_PRE)
                    break;
#endif
                autoscroll = !autoscroll;
                break;

            case VIEWER_SCROLL_UP:
            case VIEWER_SCROLL_UP | BUTTON_REPEAT:
#ifdef VIEWER_SCROLL_UP2
            case VIEWER_SCROLL_UP2:
            case VIEWER_SCROLL_UP2 | BUTTON_REPEAT:
#endif
                viewer_scroll_up(VIEWER_SCROLL_PREFS);
                viewer_draw();
                old_tick = *rb->current_tick;
                break;

            case VIEWER_SCROLL_DOWN:
            case VIEWER_SCROLL_DOWN | BUTTON_REPEAT:
#ifdef VIEWER_PAGE_DOWN2
            case VIEWER_SCROLL_DOWN2:
            case VIEWER_SCROLL_DOWN2 | BUTTON_REPEAT:
#endif
                viewer_scroll_down(VIEWER_SCROLL_PREFS);
                viewer_draw();
                old_tick = *rb->current_tick;
                break;

            case VIEWER_SCREEN_LEFT:
            case VIEWER_SCREEN_LEFT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE)
                {
                    /* Screen left */
                    viewer_scroll_left(VIEWER_SCROLL_SCREEN);
                }
                else {   /* prefs.view_mode == NARROW */
                    /* Top of file */
                    viewer_top();
                }
                viewer_draw();
                break;

            case VIEWER_SCREEN_RIGHT:
            case VIEWER_SCREEN_RIGHT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE)
                {
                    /* Screen right */
                    viewer_scrol_right(VIEWER_SCROLL_SCREEN);
                }
                else {   /* prefs.view_mode == NARROW */
                    /* Bottom of file */
                    viewer_bottom();
                }
                viewer_draw();
                break;

#ifdef VIEWER_LINE_UP
            case VIEWER_LINE_UP:
            case VIEWER_LINE_UP | BUTTON_REPEAT:
                /* Scroll up one line */
                viewer_scroll_up(VIEWER_SCROLL_LINE);
                viewer_draw();
                old_tick = *rb->current_tick;
                break;

            case VIEWER_LINE_DOWN:
            case VIEWER_LINE_DOWN | BUTTON_REPEAT:
                /* Scroll down one line */
                viewer_scroll_down(VIEWER_SCROLL_LINE);
                viewer_draw();
                old_tick = *rb->current_tick;
                break;
#endif
#ifdef VIEWER_COLUMN_LEFT
            case VIEWER_COLUMN_LEFT:
            case VIEWER_COLUMN_LEFT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE)
                {
                    viewer_scroll_left(VIEWER_SCROLL_COLUMN);
                    viewer_draw();
                }
                break;

            case VIEWER_COLUMN_RIGHT:
            case VIEWER_COLUMN_RIGHT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE)
                {
                    viewer_scroll_right(VIEWER_SCROLL_COLUMN);
                    viewer_draw();
                }
                break;
#endif

#ifdef VIEWER_RC_QUIT
            case VIEWER_RC_QUIT:
#endif
            case VIEWER_QUIT:
#ifdef VIEWER_QUIT2
            case VIEWER_QUIT2:
#endif
                viewer_exit(NULL);
                done = true;
                break;

            case VIEWER_BOOKMARK:
                viewer_add_remove_bookmark(cpage, cline);
                viewer_draw();
                break;

            default:
                if (rb->default_event_handler_ex(button, viewer_exit, NULL)
                    == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
                break;
        }
        if (button != BUTTON_NONE)
        {
            lastbutton = button;
            rb->yield();
        }
    }
    return PLUGIN_OK;
}