text viewer: reworks screen access logics and some bugs fix.
[kugel-rb.git] / apps / plugins / text_viewer / tv_window.c
blobdf3951aa28a0d8b4233117d30d00d61a968d3273
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_bookmark.h"
25 #include "tv_display.h"
26 #include "tv_preferences.h"
27 #include "tv_screen_pos.h"
28 #include "tv_text_reader.h"
29 #include "tv_window.h"
31 static int window_width;
32 static int window_columns;
33 static int display_lines;
34 static int col_width;
36 static int cur_window;
37 static int cur_column;
39 #ifdef HAVE_LCD_BITMAP
40 static bool tv_set_font(const unsigned char *font)
42 unsigned char path[MAX_PATH];
44 if (font != NULL && *font != '\0')
46 rb->snprintf(path, MAX_PATH, "%s/%s.fnt", FONT_DIR, font);
47 if (rb->font_load(NULL, path) < 0)
49 rb->splash(HZ/2, "font load failed");
50 return false;
53 return true;
56 static bool tv_check_header_and_footer(struct tv_preferences *new_prefs)
58 bool change_prefs = false;
60 if (rb->global_settings->statusbar != STATUSBAR_TOP)
62 if (new_prefs->header_mode == HD_SBAR)
64 new_prefs->header_mode = HD_NONE;
65 change_prefs = true;
67 else if (new_prefs->header_mode == HD_BOTH)
69 new_prefs->header_mode = HD_PATH;
70 change_prefs = true;
73 if (rb->global_settings->statusbar != STATUSBAR_BOTTOM)
75 if (new_prefs->footer_mode == FT_SBAR)
77 new_prefs->footer_mode = FT_NONE;
78 change_prefs = true;
80 else if (new_prefs->footer_mode == FT_BOTH)
82 new_prefs->footer_mode = FT_PAGE;
83 change_prefs = true;
87 return change_prefs;
89 #endif
91 static void tv_show_bookmarks(const struct tv_screen_pos *top_pos)
93 struct tv_screen_pos bookmarks[TV_MAX_BOOKMARKS];
94 int count = tv_get_bookmark_positions(bookmarks);
95 int line;
97 #ifdef HAVE_LCD_BITMAP
98 tv_set_drawmode(DRMODE_COMPLEMENT);
99 #endif
101 while (count--)
103 line = (bookmarks[count].page - top_pos->page) * display_lines
104 + (bookmarks[count].line - top_pos->line);
105 if (line >= 0 && line < display_lines)
107 #ifdef HAVE_LCD_BITMAP
108 tv_fillrect(0, line, 1);
109 #else
110 tv_put_bookmark_icon(line);
111 #endif
116 void tv_draw_window(void)
118 struct tv_screen_pos pos;
119 const unsigned char *text_buf;
120 int line;
121 int size = 0;
123 tv_copy_screen_pos(&pos);
125 tv_start_display();
127 #ifdef HAVE_LCD_BITMAP
128 tv_set_drawmode(DRMODE_SOLID);
129 #endif
130 tv_clear_display();
132 tv_read_start(cur_window, (cur_column > 0));
134 for (line = 0; line < display_lines; line++)
136 if (!tv_get_next_line(&text_buf))
137 break;
139 tv_draw_text(line, text_buf, cur_column);
142 size = tv_read_end();
144 #ifdef HAVE_LCD_BITMAP
145 tv_show_scrollbar(cur_window, cur_column, pos.file_pos, size);
146 tv_show_header();
147 tv_show_footer(&pos);
148 #endif
149 tv_show_bookmarks(&pos);
151 tv_update_display();
152 tv_end_display();
155 bool tv_traverse_lines(void)
157 int i;
158 bool res = true;
160 tv_read_start(0, false);
161 for (i = 0; i < display_lines; i++)
163 if (!tv_get_next_line(NULL))
165 res = false;
166 break;
169 tv_read_end();
170 return res;
173 static void tv_change_preferences(const struct tv_preferences *oldp)
175 #ifndef HAVE_LCD_BITMAP
176 (void)oldp;
177 #else
178 static bool font_changing = false;
179 const unsigned char *font_str;
180 bool change_prefs = false;
181 bool need_vertical_scrollbar;
182 struct tv_preferences new_prefs;
183 tv_copy_preferences(&new_prefs);
185 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
187 /* change font */
188 if (font_changing || rb->strcmp(font_str, preferences->font_name))
190 font_changing = true;
191 if (!tv_set_font(preferences->font_name))
193 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
194 change_prefs = true;
198 if (tv_check_header_and_footer(&new_prefs) || change_prefs)
200 tv_set_preferences(&new_prefs);
201 return;
204 font_changing = false;
205 #endif
207 #ifdef HAVE_LCD_BITMAP
208 col_width = 2 * rb->font_get_width(preferences->font, ' ');
209 #else
210 col_width = 1;
211 #endif
213 if (cur_window >= preferences->windows)
214 cur_window = 0;
216 /* change viewport */
217 tv_change_viewport();
219 #ifdef HAVE_LCD_BITMAP
220 need_vertical_scrollbar = false;
221 tv_set_layout(col_width, need_vertical_scrollbar);
222 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
223 tv_seek_top();
224 tv_set_read_conditions(preferences->windows, window_width);
225 if (tv_traverse_lines() && preferences->vertical_scrollbar)
227 need_vertical_scrollbar = true;
228 tv_set_layout(col_width, need_vertical_scrollbar);
229 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
231 tv_seek_top();
232 #else
233 tv_set_layout(col_width);
234 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
235 #endif
237 window_columns = window_width / col_width;
238 cur_column = 0;
240 tv_set_read_conditions(preferences->windows, window_width);
242 tv_init_display();
243 #ifdef HAVE_LCD_BITMAP
244 tv_init_scrollbar(tv_get_total_text_size(), need_vertical_scrollbar);
245 #endif
248 bool tv_init_window(unsigned char **buf, size_t *size)
250 tv_add_preferences_change_listner(tv_change_preferences);
251 return tv_init_text_reader(buf, size);
254 void tv_finalize_window(void)
256 tv_finalize_text_reader();
258 #ifdef HAVE_LCD_BITMAP
259 /* restore font */
260 if (rb->strcmp(rb->global_settings->font_file, preferences->font_name))
262 tv_set_font(rb->global_settings->font_file);
265 /* undo viewport */
266 tv_undo_viewport();
267 #endif
270 void tv_move_window(int window_delta, int column_delta)
272 cur_window += window_delta;
273 cur_column += column_delta;
275 if (cur_window < 0)
277 cur_window = 0;
278 cur_column = 0;
280 else if (cur_window >= preferences->windows)
282 cur_window = preferences->windows - 1;
283 cur_column = 0;
286 if (cur_column < 0)
288 if (cur_window == 0)
289 cur_column = 0;
290 else
292 cur_window--;
293 cur_column = window_columns - 1;
296 else
298 if (cur_window == preferences->windows - 1)
299 cur_column = 0;
300 else if (cur_column >= window_columns)
302 cur_window++;
303 cur_column = 0;