Prepare new maemo release
[maemo-rb.git] / apps / plugins / text_viewer / tv_window.c
blob15db75260edd0b846fc1973bbce6633af050f266
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;
35 static int cur_window;
36 static int cur_column;
38 static void tv_draw_bookmarks(const struct tv_screen_pos *top_pos)
40 struct tv_screen_pos bookmarks[TV_MAX_BOOKMARKS];
41 int disp_bookmarks[TV_MAX_BOOKMARKS];
42 int count = tv_get_bookmark_positions(bookmarks);
43 int disp_count = 0;
44 int line;
46 while (count--)
48 line = (bookmarks[count].page - top_pos->page) * display_lines
49 + (bookmarks[count].line - top_pos->line);
50 if (line >= 0 && line < display_lines)
51 disp_bookmarks[disp_count++] = line;
54 tv_show_bookmarks(disp_bookmarks, disp_count);
57 void tv_draw_window(void)
59 struct tv_screen_pos pos;
60 const unsigned char *text_buf;
61 int line;
62 int size = 0;
64 tv_copy_screen_pos(&pos);
66 tv_start_display();
68 tv_read_start(cur_window, (cur_column > 0));
70 for (line = 0; line < display_lines; line++)
72 if (!tv_get_next_line(&text_buf))
73 break;
75 tv_draw_text(line, text_buf, cur_column);
78 size = tv_read_end();
80 tv_draw_bookmarks(&pos);
82 tv_update_extra(cur_window, cur_column, &pos, size);
84 tv_end_display();
87 bool tv_traverse_lines(void)
89 int i;
90 bool res = true;
92 tv_read_start(0, false);
93 for (i = 0; i < display_lines; i++)
95 if (!tv_get_next_line(NULL))
97 res = false;
98 break;
101 tv_read_end();
102 return res;
105 static int tv_change_preferences(const struct tv_preferences *oldp)
107 bool need_scrollbar = false;
109 (void)oldp;
111 tv_set_layout(need_scrollbar);
112 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
114 if (tv_exist_scrollbar())
116 tv_seek_top();
117 tv_set_read_conditions(preferences->windows, window_width);
118 if (tv_traverse_lines() &&
119 (preferences->vertical_scrollbar || preferences->horizontal_scrollbar))
121 need_scrollbar = true;
122 tv_set_layout(need_scrollbar);
123 tv_get_drawarea_info(&window_width, &window_columns, &display_lines);
125 tv_seek_top();
126 tv_init_scrollbar(tv_get_total_text_size(), need_scrollbar);
129 if (cur_window >= preferences->windows)
130 cur_window = 0;
132 cur_column = 0;
134 tv_set_read_conditions(preferences->windows, window_width);
135 return TV_CALLBACK_OK;
138 bool tv_init_window(unsigned char **buf, size_t *size)
140 tv_add_preferences_change_listner(tv_change_preferences);
141 return tv_init_display(buf, size) && tv_init_text_reader(buf, size);
144 void tv_finalize_window(void)
146 tv_finalize_text_reader();
147 tv_finalize_display();
150 void tv_move_window(int window_delta, int column_delta)
152 cur_window += window_delta;
153 cur_column += column_delta;
155 if (cur_window < 0)
157 cur_window = 0;
158 cur_column = 0;
160 else if (cur_window >= preferences->windows)
162 cur_window = preferences->windows - 1;
163 cur_column = 0;
166 if (cur_column < 0)
168 if (cur_window == 0)
169 cur_column = 0;
170 else
172 cur_window--;
173 cur_column = window_columns - 1;
176 else
178 if (cur_window == preferences->windows - 1)
179 cur_column = 0;
180 else if (cur_column >= window_columns)
182 cur_window++;
183 cur_column = 0;