Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 50 - (hide 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 bluedwarf 12 # window.rb: the module definition of Edmaru::Window.
2 bluedwarf 10 #
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 bluedwarf 4
20 bluedwarf 9 require "cursor.rb"
21 bluedwarf 5 require "modeline.rb"
22    
23 bluedwarf 4 module Edmaru
24 bluedwarf 5
25 bluedwarf 6 #Abstract class to implement system specific window.
26 bluedwarf 5 module Window
27    
28 bluedwarf 6 #Construct a window instance.
29 bluedwarf 5 #
30 bluedwarf 6 #=== Arguments
31     #_view_ :: The parent view.
32 bluedwarf 20 #_config_ :: An instance of Edmaru::ConfigurationManager.
33 bluedwarf 6 #
34     #=== Warning
35     #This method *MUST* *NOT* be overrided in derived classes.
36     #
37     #=== Return
38     #An initialized instance of Window.
39 bluedwarf 20 def initialize(view, config)
40 bluedwarf 24 @buffer = nil
41 bluedwarf 6 @view = view
42 bluedwarf 20 @config = config
43 bluedwarf 31 @cursor = Cursor.new(0, 0) # logical cursor
44 bluedwarf 41 @modeline = Edmaru::SYSTEM_MODELINE.new(self, config)
45 bluedwarf 30 @start_line = 0
46 bluedwarf 40 @focused = false
47 bluedwarf 9
48 bluedwarf 15 init_ui
49 bluedwarf 4 end
50    
51 bluedwarf 6 #The system specific initialization for this window.
52     #
53     #=== Warning
54     #This method *SHOULD* be overrided in derived classes.
55 bluedwarf 15 def init_ui
56 bluedwarf 5 end
57    
58 bluedwarf 9 #Discard the current buffer and set the specified new buffer.
59     #
60 bluedwarf 18 #=== Warning
61     #This method *MUST* *NOT* be overrided in derived classes.
62     #
63 bluedwarf 9 #=== 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 bluedwarf 4
70 bluedwarf 9 #Linked buffer to this window.
71     @buffer = new_buffer
72     @buffer.link(self)
73 bluedwarf 35 @cursor.row = @cursor.column = 0
74 bluedwarf 9 refresh
75 bluedwarf 38
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 bluedwarf 4 end
81    
82 bluedwarf 9 def buffer
83     @buffer
84 bluedwarf 4 end
85    
86 bluedwarf 15 def modeline
87     @modeline
88     end
89    
90 bluedwarf 28 #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 bluedwarf 18 #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 bluedwarf 40 @view.hide_alert
111    
112 bluedwarf 18 #Insert to the buffer.
113 bluedwarf 38 begin
114     @buffer.insert(@cursor.row, @cursor.column, str)
115 bluedwarf 18
116 bluedwarf 38 #Move the cursor to the end of inserted string.
117     cursor_forward(str.size) if move_cursor
118     rescue ReadOnlyException => e
119 bluedwarf 40 @view.show_alert(e.to_s)
120 bluedwarf 38 end
121 bluedwarf 18 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 bluedwarf 40 @view.hide_alert
132    
133 bluedwarf 18 n.times{
134     if @cursor.column >= @buffer.lines[@cursor.row].size &&
135     @cursor.row == (@buffer.lines.size - 1)
136 bluedwarf 37 refresh_cursor
137 bluedwarf 40 @view.show_alert("End of buffer")
138 bluedwarf 37 return
139 bluedwarf 18 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 bluedwarf 40 @view.hide_alert
160    
161 bluedwarf 18 n.times{
162     if @cursor.column == 0 && @cursor.row == 0
163 bluedwarf 37 refresh_cursor
164 bluedwarf 40 @view.show_alert("Beginning of buffer")
165 bluedwarf 37 return
166 bluedwarf 18 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 bluedwarf 40 @view.hide_alert
183    
184 bluedwarf 18 @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 bluedwarf 40 @view.hide_alert
194    
195 bluedwarf 44 @cursor.column = @buffer.lines[@cursor.row].size
196 bluedwarf 18 refresh_cursor
197     end
198    
199 bluedwarf 44 #Move the cursor to the beginning of the buffer.
200     #
201     #=== Warning
202 bluedwarf 50 #This method *SHOULD* be overrided in derived classes.
203 bluedwarf 44 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 bluedwarf 50 #This method *SHOULD* be overrided in derived classes.
214 bluedwarf 44 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 bluedwarf 19 #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 bluedwarf 40 @view.hide_alert
228    
229 bluedwarf 19 if @cursor.row == (@buffer.lines.size - 1)
230 bluedwarf 40 @view.show_alert("The last line")
231 bluedwarf 19 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 bluedwarf 21
237     refresh_cursor
238 bluedwarf 19 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 bluedwarf 40 @view.hide_alert
247    
248 bluedwarf 19 if @cursor.row == 0
249 bluedwarf 40 @view.show_alert("The first line")
250 bluedwarf 19 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 bluedwarf 21
256     refresh_cursor
257 bluedwarf 19 end
258 bluedwarf 21 end
259 bluedwarf 19
260 bluedwarf 21 #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 bluedwarf 40 @view.hide_alert
267    
268 bluedwarf 38 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 bluedwarf 40 @view.show_alert("Beginning of buffer")
274 bluedwarf 38 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 bluedwarf 40 @view.show_alert(e.to_s)
286 bluedwarf 21 end
287 bluedwarf 19 end
288    
289 bluedwarf 21 #Remove a character beneath the cursor.
290     #
291     #=== Warning
292     #This method *MUST* *NOT* be overrided in derived classes.
293 bluedwarf 33 def delete_at_cursor
294 bluedwarf 40 @view.hide_alert
295    
296 bluedwarf 33 begin
297     @buffer.delete_at(@cursor.row, @cursor.column)
298     rescue EndOfBufferException => e
299 bluedwarf 40 @view.show_alert(e.to_s)
300 bluedwarf 38 rescue ReadOnlyException => e
301 bluedwarf 40 @view.show_alert(e.to_s)
302 bluedwarf 21 end
303     end
304    
305 bluedwarf 26 #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 bluedwarf 40 @view.hide_alert
312    
313 bluedwarf 33 begin
314     @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
315 bluedwarf 38 rescue EdmaruException => e
316 bluedwarf 40 @view.show_alert(e.to_s)
317 bluedwarf 26 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 bluedwarf 40 @view.hide_alert
326 bluedwarf 26 insert_at_cursor(@view.yank_from_kill_ring)
327     end
328    
329 bluedwarf 38 #Scroll down this window.
330     #
331     #=== Warning
332     #This method *SHOULD* be overrided in derived clases.
333     def scroll_down
334     end
335    
336 bluedwarf 47 #Scroll up this window.
337     #
338     #=== Warning
339     #This method *SHOULD* be overrided in derived clases.
340     def scroll_up
341     end
342    
343 bluedwarf 18 #Redraw the cursor.
344     #
345     #=== Warning
346     #This method *SHOULD* be overrided in derived clases.
347     def refresh_cursor
348     end
349    
350 bluedwarf 9 #Redraw this window.
351     #
352     #=== Warning
353     #This method *SHOULD* be overrided in derived clases.
354 bluedwarf 4 def refresh
355     end
356    
357 bluedwarf 40 #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 bluedwarf 6 #Terminate this view instance.
378     #
379     #=== Warning
380 bluedwarf 7 #This method *MUST* *NOT* be overrided in derived classes.
381 bluedwarf 4 def terminate
382 bluedwarf 7 @modeline.terminate
383    
384     terminate_ui
385 bluedwarf 4 end
386 bluedwarf 7
387     #Free system specific resources.
388     #
389     #=== Warning
390     #This method *SHOULD* be overrided in derived classes.
391     def terminate_ui
392     end
393 bluedwarf 4 end
394     end

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