Commit FS#11799 by Alexander Meshcheryakov. Improves the text viewer plugin to write...
[kugel-rb.git] / apps / plugins / text_viewer / tv_action.c
blob3a0d89533184e67ca309de7f1910ab0b9b79fc92
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Gilles Roux
11 * 2003 Garrett Derner
12 * 2010 Yoshihisa Uchida
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
24 #include "tv_action.h"
25 #include "tv_bookmark.h"
26 #include "tv_pager.h"
27 #include "tv_screen_pos.h"
28 #include "tv_settings.h"
29 #include "tv_window.h"
31 bool bookmarks_changed = false;
32 bool scrolled = false;
34 bool tv_init_action(unsigned char **buf, size_t *size)
36 /* initialize bookmarks and window modules */
37 return tv_init_bookmark(buf, size) && tv_init_window(buf, size);
40 static void tv_finalize_action(void)
42 /* finalize bookmark modules */
43 tv_finalize_bookmark();
45 /* finalize window modules */
46 tv_finalize_window();
49 void tv_exit(void)
51 /* save preference and bookmarks */
52 DEBUGF("preferences_changed=%d\tbookmarks_changed=%d\tscrolled=%d\n",preferences_changed,bookmarks_changed,scrolled);
53 if ( preferences_changed || bookmarks_changed
54 #ifndef HAVE_DISK_STORAGE
55 || scrolled
56 #endif
59 DEBUGF("Saving settings\n");
60 if (!tv_save_settings())
61 rb->splash(HZ, "Can't save preferences and bookmarks");
63 else
64 DEBUGF("Skip saving settings\n");
66 /* finalize modules */
67 tv_finalize_action();
70 bool tv_load_file(const unsigned char *file)
72 /* load the preferences and bookmark */
73 if (!tv_load_settings(file))
74 return false;
76 /* select to read the page */
77 tv_select_bookmark();
79 return true;
82 void tv_draw(void)
84 struct tv_screen_pos pos;
86 tv_copy_screen_pos(&pos);
87 tv_draw_window();
88 if (pos.line == 0)
89 tv_new_page();
91 tv_move_screen(pos.page, pos.line, SEEK_SET);
94 void tv_scroll_up(unsigned mode)
96 int offset_page = 0;
97 int offset_line = -1;
99 if ((mode == TV_VERTICAL_SCROLL_PAGE) ||
100 (mode == TV_VERTICAL_SCROLL_PREFS && preferences->vertical_scroll_mode == VS_PAGE))
102 offset_page--;
103 #ifdef HAVE_LCD_BITMAP
104 offset_line = (preferences->overlap_page_mode)? 1:0;
105 #endif
107 tv_move_screen(offset_page, offset_line, SEEK_CUR);
108 scrolled = true;
111 void tv_scroll_down(unsigned mode)
113 int offset_page = 0;
114 int offset_line = 1;
116 if ((mode == TV_VERTICAL_SCROLL_PAGE) ||
117 (mode == TV_VERTICAL_SCROLL_PREFS && preferences->vertical_scroll_mode == VS_PAGE))
119 offset_page++;
120 #ifdef HAVE_LCD_BITMAP
121 offset_line = (preferences->overlap_page_mode)? -1:0;
122 #endif
124 tv_move_screen(offset_page, offset_line, SEEK_CUR);
125 scrolled = true;
128 void tv_scroll_left(unsigned mode)
130 int offset_window = 0;
131 int offset_column = 0;
133 if ((mode == TV_HORIZONTAL_SCROLL_COLUMN) ||
134 (mode == TV_HORIZONTAL_SCROLL_PREFS && preferences->horizontal_scroll_mode == HS_COLUMN))
136 /* Scroll left one column */
137 offset_column--;
139 else
141 /* Scroll left one window */
142 offset_window--;
144 tv_move_window(offset_window, offset_column);
145 scrolled = true;
148 void tv_scroll_right(unsigned mode)
150 int offset_window = 0;
151 int offset_column = 0;
153 if ((mode == TV_HORIZONTAL_SCROLL_COLUMN) ||
154 (mode == TV_HORIZONTAL_SCROLL_PREFS && preferences->horizontal_scroll_mode == HS_COLUMN))
156 /* Scroll right one column */
157 offset_column++;
159 else
161 /* Scroll right one window */
162 offset_window++;
164 tv_move_window(offset_window, offset_column);
165 scrolled = true;
168 void tv_top(void)
170 tv_move_screen(0, 0, SEEK_SET);
173 void tv_bottom(void)
175 tv_move_screen(0, 0, SEEK_END);
176 if (preferences->vertical_scroll_mode == VS_PAGE)
177 tv_move_screen(0, -tv_get_screen_pos()->line, SEEK_CUR);
180 unsigned tv_menu(void)
182 unsigned res;
183 struct tv_screen_pos cur_pos;
184 off_t cur_file_pos = tv_get_screen_pos()->file_pos;
186 res = tv_display_menu();
188 if (res == TV_MENU_RESULT_EXIT_MENU)
190 tv_convert_fpos(cur_file_pos, &cur_pos);
192 tv_move_screen(cur_pos.page, cur_pos.line, SEEK_SET);
194 else if (res == TV_MENU_RESULT_MOVE_PAGE)
195 res = TV_MENU_RESULT_EXIT_MENU;
197 return res;
200 void tv_add_or_remove_bookmark(void)
202 tv_toggle_bookmark();
203 bookmarks_changed = true;