16be8845ef7c07ed4b203c333a3e096254a7c69e
[kugel-rb.git] / apps / plugins / text_viewer / tv_window.c
blob16be8845ef7c07ed4b203c333a3e096254a7c69e
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_show_bookmarks(const struct tv_screen_pos *top_pos)
59 struct tv_screen_pos bookmarks[TV_MAX_BOOKMARKS];
60 int count = tv_get_bookmark_positions(bookmarks);
61 int line;
63 #ifdef HAVE_LCD_BITMAP
64 tv_set_drawmode(DRMODE_COMPLEMENT);
65 #endif
67 while (count--)
69 line = (bookmarks[count].page - top_pos->page) * display_lines
70 + (bookmarks[count].line - top_pos->line);
71 if (line >= 0 && line < display_lines)
73 #ifdef HAVE_LCD_BITMAP
74 tv_fillrect(0, line, 1);
75 #else
76 tv_put_bookmark_icon(line);
77 #endif
82 void tv_draw_window(void)
84 struct tv_screen_pos pos;
85 const unsigned char *text_buf;
86 int line;
87 int size = 0;
89 tv_copy_screen_pos(&pos);
91 tv_start_display();
93 #ifdef HAVE_LCD_BITMAP
94 tv_set_drawmode(DRMODE_SOLID);
95 #endif
96 tv_clear_display();
98 tv_read_start(cur_window, (cur_column > 0));
100 for (line = 0; line < display_lines; line++)
102 if (!tv_get_next_line(&text_buf))
103 break;
105 tv_draw_text(line, text_buf, cur_column);
108 size = tv_read_end();
110 #ifdef HAVE_LCD_BITMAP
111 tv_show_scrollbar(cur_window, cur_column, pos.file_pos, size);
112 tv_show_header();
113 tv_show_footer(&pos);
114 #endif
115 tv_show_bookmarks(&pos);
117 tv_update_display();
118 tv_end_display();
121 bool tv_traverse_lines(void)
123 int i;
124 bool res = true;
126 tv_read_start(0, false);
127 for (i = 0; i < display_lines; i++)
129 if (!tv_get_next_line(NULL))
131 res = false;
132 break;
135 tv_read_end();
136 return res;
139 static void tv_change_preferences(const struct tv_preferences *oldp)
141 #ifndef HAVE_LCD_BITMAP
142 (void)oldp;
143 #else
144 static bool font_changing = false;
145 const unsigned char *font_str;
146 bool need_vertical_scrollbar;
147 struct tv_preferences new_prefs;
148 tv_copy_preferences(&new_prefs);
150 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
152 /* change font */
153 if (font_changing || rb->strcmp(font_str, preferences->font_name))
155 font_changing = true;
156 if (!tv_set_font(preferences->font_name))
158 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
159 tv_set_preferences(&new_prefs);
160 return;
164 font_changing = false;
165 #endif
167 #ifdef HAVE_LCD_BITMAP
168 col_width = 2 * rb->font_get_width(preferences->font, ' ');
169 #else
170 col_width = 1;
171 #endif
173 if (cur_window >= preferences->windows)
174 cur_window = 0;
176 /* change viewport */
177 tv_change_viewport();
179 #ifdef HAVE_LCD_BITMAP
180 need_vertical_scrollbar = false;
181 tv_set_layout(col_width, need_vertical_scrollbar);
182 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
183 tv_seek_top();
184 tv_set_read_conditions(preferences->windows, window_width);
185 if (tv_traverse_lines() && preferences->vertical_scrollbar)
187 need_vertical_scrollbar = true;
188 tv_set_layout(col_width, need_vertical_scrollbar);
189 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
191 tv_seek_top();
192 #else
193 tv_set_layout(col_width);
194 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
195 #endif
197 window_columns = window_width / col_width;
198 cur_column = 0;
200 tv_set_read_conditions(preferences->windows, window_width);
202 tv_init_display();
203 #ifdef HAVE_LCD_BITMAP
204 tv_init_scrollbar(tv_get_total_text_size(), need_vertical_scrollbar);
205 #endif
208 bool tv_init_window(unsigned char **buf, size_t *size)
210 tv_add_preferences_change_listner(tv_change_preferences);
211 return tv_init_text_reader(buf, size);
214 void tv_finalize_window(void)
216 tv_finalize_text_reader();
218 #ifdef HAVE_LCD_BITMAP
219 /* restore font */
220 if (rb->strcmp(rb->global_settings->font_file, preferences->font_name))
222 tv_set_font(rb->global_settings->font_file);
225 /* undo viewport */
226 tv_undo_viewport();
227 #endif
230 void tv_move_window(int window_delta, int column_delta)
232 cur_window += window_delta;
233 cur_column += column_delta;
235 if (cur_window < 0)
237 cur_window = 0;
238 cur_column = 0;
240 else if (cur_window >= preferences->windows)
242 cur_window = preferences->windows - 1;
243 cur_column = 0;
246 if (cur_column < 0)
248 if (cur_window == 0)
249 cur_column = 0;
250 else
252 cur_window--;
253 cur_column = window_columns - 1;
256 else
258 if (cur_window == preferences->windows - 1)
259 cur_column = 0;
260 else if (cur_column >= window_columns)
262 cur_window++;
263 cur_column = 0;