Develop and Download Open Source Software

Browse Subversion Repository

Contents of /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: 9899 byte(s)
Faster scroll function.

1 # window.rb: the module definition of Edmaru::Window.
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 "cursor.rb"
21 require "modeline.rb"
22
23 module Edmaru
24
25 #Abstract class to implement system specific window.
26 module Window
27
28 #Construct a window instance.
29 #
30 #=== Arguments
31 #_view_ :: The parent view.
32 #_config_ :: An instance of Edmaru::ConfigurationManager.
33 #
34 #=== Warning
35 #This method *MUST* *NOT* be overrided in derived classes.
36 #
37 #=== Return
38 #An initialized instance of Window.
39 def initialize(view, config)
40 @buffer = nil
41 @view = view
42 @config = config
43 @cursor = Cursor.new(0, 0) # logical cursor
44 @modeline = Edmaru::SYSTEM_MODELINE.new(self, config)
45 @start_line = 0
46 @focused = false
47
48 init_ui
49 end
50
51 #The system specific initialization for this window.
52 #
53 #=== Warning
54 #This method *SHOULD* be overrided in derived classes.
55 def init_ui
56 end
57
58 #Discard the current buffer and set the specified new buffer.
59 #
60 #=== Warning
61 #This method *MUST* *NOT* be overrided in derived classes.
62 #
63 #=== Argument
64 #_new_buffer_ :: The buffer to be shown in this window.
65 def buffer=(new_buffer)
66 if @buffer != nil
67 @buffer.unlink(self)
68 end
69
70 #Linked buffer to this window.
71 @buffer = new_buffer
72 @buffer.link(self)
73 @cursor.row = @cursor.column = 0
74 refresh
75
76 # @modelin is nil, if this window is the mini window.
77 if @modeline != nil
78 @modeline.set_element_text("buffer-name", @buffer.name)
79 end
80 end
81
82 def buffer
83 @buffer
84 end
85
86 def modeline
87 @modeline
88 end
89
90 #Remove all characters from this buffer.
91 #
92 #=== Warning
93 #This method *MUST* *NOT* be overrided in derived classes.
94 #
95 def clear
96 @cursor.row = @cursor.column = 0
97 @buffer.clear
98 end
99
100 #Insert the specified string to the buffer shown in this window.
101 #
102 #=== Warning
103 #This method *MUST* *NOT* be overrided in derived classes.
104 #
105 #=== Argument
106 #_str_ :: String to be inserted.
107 #_move_cursor_ :: _true_ to cause the cursor move to the end of the
108 #inserted string.
109 def insert_at_cursor(str, move_cursor = true)
110 @view.hide_alert
111
112 #Insert to the buffer.
113 begin
114 @buffer.insert(@cursor.row, @cursor.column, str)
115
116 #Move the cursor to the end of inserted string.
117 cursor_forward(str.size) if move_cursor
118 rescue ReadOnlyException => e
119 @view.show_alert(e.to_s)
120 end
121 end
122
123 #Move the cursor forward.
124 #
125 #=== Argument
126 #_n_ :: the number of times to forward the cursor.
127 #
128 #=== Warning
129 #This method *MUST* *NOT* be overrided in derived classes.
130 def cursor_forward(n = 1)
131 @view.hide_alert
132
133 n.times{
134 if @cursor.column >= @buffer.lines[@cursor.row].size &&
135 @cursor.row == (@buffer.lines.size - 1)
136 refresh_cursor
137 @view.show_alert("End of buffer")
138 return
139 elsif @cursor.column >= @buffer.lines[@cursor.row].size
140 #Go to the begin of the next line
141 @cursor.row += 1
142 @cursor.column = 0
143 else
144 @cursor.column += 1
145 end
146 }
147
148 refresh_cursor
149 end
150
151 #Move the cursor backward.
152 #
153 #=== Argument
154 #_n_ :: the number of times to backward the cursor.
155 #
156 #=== Warning
157 #This method *MUST* *NOT* be overrided in derived classes.
158 def cursor_backward(n = 1)
159 @view.hide_alert
160
161 n.times{
162 if @cursor.column == 0 && @cursor.row == 0
163 refresh_cursor
164 @view.show_alert("Beginning of buffer")
165 return
166 elsif @cursor.column == 0
167 @cursor.row -= 1
168 @cursor.column = @buffer.lines[@cursor.row].size
169 else
170 @cursor.column -= 1
171 end
172 }
173
174 refresh_cursor
175 end
176
177 #Move the cursor to the begin of the current line.
178 #
179 #=== Warning
180 #This method *MUST* *NOT* be overrided in derived classes.
181 def cursor_goto_line_head
182 @view.hide_alert
183
184 @cursor.column = 0
185 refresh_cursor
186 end
187
188 #Move the cursor to the end of the current line.
189 #
190 #=== Warning
191 #This method *MUST* *NOT* be overrided in derived classes.
192 def cursor_goto_line_tail
193 @view.hide_alert
194
195 @cursor.column = @buffer.lines[@cursor.row].size
196 refresh_cursor
197 end
198
199 #Move the cursor to the beginning of the buffer.
200 #
201 #=== Warning
202 #This method *SHOULD* be overrided in derived classes.
203 def cursor_goto_beginning
204 @view.hide_alert
205
206 @cursor.row = @cursor.column = 0
207 refresh_cursor
208 end
209
210 #Move the cursor to the end of the buffer.
211 #
212 #=== Warning
213 #This method *SHOULD* be overrided in derived classes.
214 def cursor_goto_end
215 @view.hide_alert
216
217 @cursor.row = @buffer.lines.size - 1
218 @cursor.column = @buffer.lines[@cursor.row].size
219 refresh_cursor
220 end
221
222 #Move the cursor to the next line.
223 #
224 #=== Warning
225 #This method *MUST* *NOT* be overrided in derived classes.
226 def cursor_goto_next_line
227 @view.hide_alert
228
229 if @cursor.row == (@buffer.lines.size - 1)
230 @view.show_alert("The last line")
231 else
232 @cursor.row += 1
233 if @buffer.lines[@cursor.row].size < @cursor.column
234 @cursor.column = @buffer.lines[@cursor.row].size
235 end
236
237 refresh_cursor
238 end
239 end
240
241 #Move the cursor to the previous line.
242 #
243 #=== Warning
244 #This method *MUST* *NOT* be overrided in derived classes.
245 def cursor_goto_previous_line
246 @view.hide_alert
247
248 if @cursor.row == 0
249 @view.show_alert("The first line")
250 else
251 @cursor.row -= 1
252 if @buffer.lines[@cursor.row].size < @cursor.column
253 @cursor.column = @buffer.lines[@cursor.row].size
254 end
255
256 refresh_cursor
257 end
258 end
259
260 #Remove a character just in front of the cursor and move the cursor
261 #backward.
262 #
263 #=== Warning
264 #This method *MUST* *NOT* be overrided in derived classes.
265 def backspace
266 @view.hide_alert
267
268 pre_cursor = @cursor.clone
269
270 begin
271 if @cursor.column == 0 && @cursor.row == 0
272 #ToDo: Alert that the cursor is at the begin of the current buffer.
273 @view.show_alert("Beginning of buffer")
274 elsif @cursor.column == 0 # && @cursor.row != 0
275 #Concatenate the current lien into the previous line.
276 @buffer.delete_at(@cursor.row -= 1,
277 @cursor.column = @buffer.lines[@cursor.row].size)
278 refresh_cursor
279 else
280 @buffer.delete_at(@cursor.row, @cursor.column -= 1)
281 refresh_cursor
282 end
283 rescue ReadOnlyException => e
284 @cursor = pre_cursor
285 @view.show_alert(e.to_s)
286 end
287 end
288
289 #Remove a character beneath the cursor.
290 #
291 #=== Warning
292 #This method *MUST* *NOT* be overrided in derived classes.
293 def delete_at_cursor
294 @view.hide_alert
295
296 begin
297 @buffer.delete_at(@cursor.row, @cursor.column)
298 rescue EndOfBufferException => e
299 @view.show_alert(e.to_s)
300 rescue ReadOnlyException => e
301 @view.show_alert(e.to_s)
302 end
303 end
304
305 #Remove the string in the current line behind the cursor and push it to
306 #the kill ring.
307 #
308 #=== Warning
309 #This method *MUST* *NOT* be overrided in derived classes.
310 def kill_line
311 @view.hide_alert
312
313 begin
314 @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
315 rescue EdmaruException => e
316 @view.show_alert(e.to_s)
317 end
318 end
319
320 #Get a certain string from kill ring and insert it at the cursor position.
321 #
322 #=== Warning
323 #This method *MUST* *NOT* be overrided in derived classes.
324 def yank
325 @view.hide_alert
326 insert_at_cursor(@view.yank_from_kill_ring)
327 end
328
329 #Scroll down this window.
330 #
331 #=== Warning
332 #This method *SHOULD* be overrided in derived clases.
333 def scroll_down
334 end
335
336 #Scroll up this window.
337 #
338 #=== Warning
339 #This method *SHOULD* be overrided in derived clases.
340 def scroll_up
341 end
342
343 #Redraw the cursor.
344 #
345 #=== Warning
346 #This method *SHOULD* be overrided in derived clases.
347 def refresh_cursor
348 end
349
350 #Redraw this window.
351 #
352 #=== Warning
353 #This method *SHOULD* be overrided in derived clases.
354 def refresh
355 end
356
357 #Focus on this window.
358 #
359 #=== Warning
360 #This method *SHOULD* *NOT* be overrided in derived classes.
361 def focus_in
362 @focused = true
363
364 refresh_cursor
365 end
366
367 #Focus out from this window.
368 #
369 #=== Warning
370 #This method *SHOULD* *NOT* be overrided in derived classes.
371 def focus_out
372 @focused = false
373
374 refresh_cursor
375 end
376
377 #Terminate this view instance.
378 #
379 #=== Warning
380 #This method *MUST* *NOT* be overrided in derived classes.
381 def terminate
382 @modeline.terminate
383
384 terminate_ui
385 end
386
387 #Free system specific resources.
388 #
389 #=== Warning
390 #This method *SHOULD* be overrided in derived classes.
391 def terminate_ui
392 end
393 end
394 end

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