c83c6080bde1fd8c4077ad04254e618d0c0bc5b8
[kugel-rb.git] / apps / plugins / text_viewer / tv_window.c
blobc83c6080bde1fd8c4077ad04254e618d0c0bc5b8
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;
55 #endif
57 static void tv_draw_bookmarks(const struct tv_screen_pos *top_pos)
59 struct tv_screen_pos bookmarks[TV_MAX_BOOKMARKS];
60 int disp_bookmarks[TV_MAX_BOOKMARKS];
61 int count = tv_get_bookmark_positions(bookmarks);
62 int disp_count = 0;
63 int line;
65 while (count--)
67 line = (bookmarks[count].page - top_pos->page) * display_lines
68 + (bookmarks[count].line - top_pos->line);
69 if (line >= 0 && line < display_lines)
70 disp_bookmarks[disp_count++] = line;
73 tv_show_bookmarks(disp_bookmarks, disp_count);
76 void tv_draw_window(void)
78 struct tv_screen_pos pos;
79 const unsigned char *text_buf;
80 int line;
81 int size = 0;
83 tv_copy_screen_pos(&pos);
85 tv_start_display();
87 tv_read_start(cur_window, (cur_column > 0));
89 for (line = 0; line < display_lines; line++)
91 if (!tv_get_next_line(&text_buf))
92 break;
94 tv_draw_text(line, text_buf, cur_column);
97 size = tv_read_end();
99 #ifdef HAVE_LCD_BITMAP
100 tv_show_scrollbar(cur_window, cur_column, pos.file_pos, size);
101 tv_show_header();
102 tv_show_footer(&pos);
103 #endif
104 tv_draw_bookmarks(&pos);
106 tv_update_display();
107 tv_end_display();
110 bool tv_traverse_lines(void)
112 int i;
113 bool res = true;
115 tv_read_start(0, false);
116 for (i = 0; i < display_lines; i++)
118 if (!tv_get_next_line(NULL))
120 res = false;
121 break;
124 tv_read_end();
125 return res;
128 static void tv_change_preferences(const struct tv_preferences *oldp)
130 #ifndef HAVE_LCD_BITMAP
131 (void)oldp;
132 #else
133 static bool font_changing = false;
134 const unsigned char *font_str;
135 bool need_vertical_scrollbar;
136 struct tv_preferences new_prefs;
137 tv_copy_preferences(&new_prefs);
139 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
141 /* change font */
142 if (font_changing || rb->strcmp(font_str, preferences->font_name))
144 font_changing = true;
145 if (!tv_set_font(preferences->font_name))
147 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
148 tv_set_preferences(&new_prefs);
149 return;
153 font_changing = false;
154 #endif
156 #ifdef HAVE_LCD_BITMAP
157 col_width = 2 * rb->font_get_width(preferences->font, ' ');
158 #else
159 col_width = 1;
160 #endif
162 if (cur_window >= preferences->windows)
163 cur_window = 0;
165 /* change viewport */
166 tv_change_viewport();
168 #ifdef HAVE_LCD_BITMAP
169 need_vertical_scrollbar = false;
170 tv_set_layout(col_width, need_vertical_scrollbar);
171 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
172 tv_seek_top();
173 tv_set_read_conditions(preferences->windows, window_width);
174 if (tv_traverse_lines() && preferences->vertical_scrollbar)
176 need_vertical_scrollbar = true;
177 tv_set_layout(col_width, need_vertical_scrollbar);
178 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
180 tv_seek_top();
181 #else
182 tv_set_layout(col_width);
183 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
184 #endif
186 window_columns = window_width / col_width;
187 cur_column = 0;
189 tv_set_read_conditions(preferences->windows, window_width);
191 tv_init_display();
192 #ifdef HAVE_LCD_BITMAP
193 tv_init_scrollbar(tv_get_total_text_size(), need_vertical_scrollbar);
194 #endif
197 bool tv_init_window(unsigned char **buf, size_t *size)
199 tv_add_preferences_change_listner(tv_change_preferences);
200 return tv_init_text_reader(buf, size);
203 void tv_finalize_window(void)
205 tv_finalize_text_reader();
207 #ifdef HAVE_LCD_BITMAP
208 /* restore font */
209 if (rb->strcmp(rb->global_settings->font_file, preferences->font_name))
211 tv_set_font(rb->global_settings->font_file);
214 /* undo viewport */
215 tv_undo_viewport();
216 #endif
219 void tv_move_window(int window_delta, int column_delta)
221 cur_window += window_delta;
222 cur_column += column_delta;
224 if (cur_window < 0)
226 cur_window = 0;
227 cur_column = 0;
229 else if (cur_window >= preferences->windows)
231 cur_window = preferences->windows - 1;
232 cur_column = 0;
235 if (cur_column < 0)
237 if (cur_window == 0)
238 cur_column = 0;
239 else
241 cur_window--;
242 cur_column = window_columns - 1;
245 else
247 if (cur_window == preferences->windows - 1)
248 cur_column = 0;
249 else if (cur_column >= window_columns)
251 cur_window++;
252 cur_column = 0;