Develop and Download Open Source Software

Browse Subversion Repository

Contents of /ncurses/ncurses_window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (show annotations) (download)
Sun Apr 22 17:45:20 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 8531 byte(s)
Faster scroll function.

1 # ncurses_window.rb: the class definition of Edmaru::Ncurses::NcursesWindow
2 #
3 # Copyright (C) 2007 Takashi Nakamoto
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301 USA.
18 #
19
20 require "window.rb"
21
22 module Edmaru::Ncurses
23 class NcursesWindow
24 include Edmaru::Window
25
26 #Nothing to do.
27 def init_ui
28 end
29
30 #Allocate memory for this window.
31 #
32 #=== Arguments
33 #
34 #_x_ :: The left edge position in the parent view.
35 #_y_ :: The top edge position in the parent view.
36 #_col_ :: The max column size allocated for this window.
37 #_row_ :: The max line size allocated for this window excluding
38 #ModeLine's line size (usually 1).
39 def init_ncurses_window(x, y, col, row)
40 @win = Ncurses::newwin(row - 1, col, y, x)
41 @max_row = @allocated_row = row - 1
42 @allocated_column = col
43 @p_start_row = 0 # The row of physical position that becomes
44 # the first line in screen.
45
46 #The right edge is reserved for an arrow mark that indicates whether
47 #the line is wrapped.
48 @max_column = col - 1
49
50 @modeline.init_ncurses_modeline(0, y + row - 1, col)
51 @modeline.refresh
52 end
53
54 def ncurses_move_resize(x, y, col, row)
55 return if @win == nil
56
57 @win.delwin
58
59 @modeline.terminate
60
61 init_ncurses_window(x, y, col, row)
62 refresh
63 end
64
65 def cursor_logical_to_physical(l_cursor)
66 p_cursor = Edmaru::Cursor.new
67
68 if l_cursor.row == 0
69 if l_cursor.column == 0
70 p_cursor.row = p_cursor.column = 0
71 elsif (l_cursor.column % @max_column) == 0 &&
72 l_cursor.column == @buffer.lines[0].size
73 p_cursor.row = l_cursor.column / @max_column - 1
74 p_cursor.column = @max_column
75 else
76 p_cursor.row = l_cursor.column / @max_column
77 p_cursor.column = l_cursor.column % @max_column
78 end
79 else
80 p_cursor.row = 0
81
82 l_cursor.row.times{ |n|
83 if @buffer.lines[n].size == 0
84 p_cursor.row += 1
85 elsif (@buffer.lines[n].size % @max_column) == 0
86 p_cursor.row += (@buffer.lines[n].size / @max_column)
87 else
88 p_cursor.row += (@buffer.lines[n].size / @max_column + 1)
89 end
90 }
91
92 if l_cursor.column == 0
93 p_cursor.column = 0
94 elsif (l_cursor.column & @max_column) == 0 &&
95 l_cursor.column == @buffer.lines[l_cursor.row].size
96 p_cursor.row += (l_cursor.column / @max_column - 1)
97 p_cursor.column = @max_column
98 else
99 p_cursor.row += (l_cursor.column / @max_column)
100 p_cursor.column = l_cursor.column % @max_column
101 end
102 end
103
104 return p_cursor
105 end
106
107 def cursor_physical_to_logical(p_cursor)
108 l_cursor = Edmaru::Cursor.new
109
110 line_num = @buffer.lines.size
111
112 for n in 0...line_num
113 l_cursor.row = n
114 l_cursor.column = 0
115 start_p_cursor = cursor_logical_to_physical(l_cursor)
116
117 l_cursor.column = @buffer.lines[n].size
118 end_p_cursor = cursor_logical_to_physical(l_cursor)
119
120 if start_p_cursor.row <= p_cursor.row &&
121 end_p_cursor.row >= p_cursor.row
122 l_cursor.column = 0
123 l_cursor.column +=
124 (p_cursor.row - start_p_cursor.row) * @max_column
125 l_cursor.column += p_cursor.column
126 return l_cursor
127 end
128 end
129
130 return nil
131 end
132
133 #Refresh hte cursor. Move the cursor to the correct position.
134 def refresh_cursor
135 return if @offscreen == nil
136
137 #There is no need to update cursor if this window is not
138 #focused.
139 return if !@focused
140
141 p_cursor = cursor_logical_to_physical(@cursor)
142
143 if (p_cursor.row - @p_start_row) >= @max_row
144 #ToDo: scroll to the appropriate position at once
145 scroll_down
146 elsif (p_cursor.row - @p_start_row) < 0 && @p_start_row > 0
147 #ToDo: scroll to the appropriate position at once
148 scroll_up
149 end
150
151 @win.move(p_cursor.row - @p_start_row, p_cursor.column)
152
153 @modeline.set_element_text("cursor-position",
154 "(#{@cursor.row},#{@cursor.column})")
155
156 @win.refresh
157 end
158
159 #Redraw this window.
160 def refresh
161 @offscreen = Array.new
162 @p_start_row = 0 if @p_start_row == nil
163
164 @buffer.lines.each{ |line|
165 line = line.clone
166
167 while(true)
168 if line.size == 0
169 @offscreen.push("")
170 break
171 elsif line.size > @max_column
172 @offscreen.push(line.slice!(0 ... @max_column) + "\\")
173 else
174 @offscreen.push(line)
175 break
176 end
177 end
178 }
179
180 for window_row in 0...@max_row
181 row = @p_start_row + window_row
182
183 if row >= @offscreen.size || @offscreen[row].size == 0
184 @win.mvaddstr(window_row, 0, " " * @allocated_column)
185 else
186 @win.mvaddstr(window_row, 0, @offscreen[row])
187 if @offscreen[row].size < @allocated_column
188 @win.addstr(" " * (@allocated_column - @offscreen[row].size))
189 end
190 end
191 end
192
193 @win.refresh
194
195 @view.focused_window.refresh_cursor
196 end
197
198 #Move the cursor to the beginning of the buffer.
199 def cursor_goto_beginning
200 @view.hide_alert
201
202 @cursor.row = @cursor.column = 0
203 @p_start_row = 0
204 refresh
205 end
206
207 #Move the cursor to the end of the buffer.
208 def cursor_goto_end
209 @view.hide_alert
210
211 @cursor.row = @buffer.lines.size - 1
212 @cursor.column = @buffer.lines[@cursor.row].size
213 p_cursor = cursor_logical_to_physical(@cursor)
214
215 @p_start_row = p_cursor.row - @max_row + 2
216 @p_start_row = 0 if @p_start_row < 0
217 refresh
218 end
219
220 #Scroll down this window.
221 def scroll_down
222 @view.hide_alert
223
224 end_l_cursor = Edmaru::Cursor.new
225 end_l_cursor.row = @buffer.lines.size - 1
226 end_l_cursor.column = @buffer.lines[end_l_cursor.row].size
227
228 end_p_cursor = cursor_logical_to_physical(end_l_cursor)
229 end_p_row = end_p_cursor.row
230
231 if @p_start_row + @max_row > end_p_row
232 @view.show_alert("End of buffer")
233 return
234 end
235
236 prev_p_start_row = @p_start_row
237 if @max_row != 1
238 @p_start_row += (@max_row / 2).to_i
239 else
240 @p_start_row += 1
241 end
242
243 if @p_start_row > end_p_row
244 @p_start_row = prev_p_start_row
245 end
246
247 p_cursor = cursor_logical_to_physical(@cursor)
248 if p_cursor.row < @p_start_row
249 if @max_row != 1
250 p_cursor.row += (@max_row / 2).to_i
251 else
252 p_cursor.row += 1
253 end
254 p_cursor.column = 0
255 l_cursor = cursor_physical_to_logical(p_cursor)
256 if l_cursor != nil
257 @cursor = l_cursor
258 else
259 @cursor = end_l_cursor
260 end
261 end
262
263 refresh
264 end
265
266 #Scroll up this window
267 def scroll_up
268 @view.hide_alert
269
270 if @p_start_row == 0
271 @view.show_alert("Beginning of buffer")
272 return
273 end
274
275 if @max_row != 1
276 @p_start_row -= (@max_row / 2)
277 else
278 @p_start_row -= 1
279 end
280 @p_start_row = 0 if @p_start_row < 0
281
282 p_cursor = cursor_logical_to_physical(@cursor)
283 if p_cursor.row >= @p_start_row + @max_row
284 if @max_row != 1
285 p_cursor.row -= (@max_row / 2).to_i
286 else
287 p_cursor.row -= 1
288 end
289 p_cursor.column = 0
290 l_cursor = cursor_physical_to_logical(p_cursor)
291 if l_cursor != nil
292 @cursor = l_cursor
293 else
294 @cursor.row = @cursor.column = 0
295 end
296 end
297
298 refresh
299 end
300
301 def ncurses_allocated_row
302 @allocated_row
303 end
304
305 def ncurses_allocated_column
306 @allocated_column
307 end
308
309 #Free allocated memory for this window.
310 def terminate_ui
311 @win.delwin
312 end
313 end
314 end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26