Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (hide annotations) (download)
Thu Apr 19 17:54:27 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 9191 byte(s)
Improvement on Gtk mode.

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 17 @modeline = Edmaru::SYSTEM_MODELINE.new(self)
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 18 @cursor.column = buffer.lines[@cursor.row].size
196     refresh_cursor
197     end
198    
199 bluedwarf 19 #Move the cursor to the next line.
200     #
201     #=== Warning
202     #This method *MUST* *NOT* be overrided in derived classes.
203     def cursor_goto_next_line
204 bluedwarf 40 @view.hide_alert
205    
206 bluedwarf 19 if @cursor.row == (@buffer.lines.size - 1)
207 bluedwarf 40 @view.show_alert("The last line")
208 bluedwarf 19 else
209     @cursor.row += 1
210     if @buffer.lines[@cursor.row].size < @cursor.column
211     @cursor.column = @buffer.lines[@cursor.row].size
212     end
213 bluedwarf 21
214     refresh_cursor
215 bluedwarf 19 end
216     end
217    
218     #Move the cursor to the previous line.
219     #
220     #=== Warning
221     #This method *MUST* *NOT* be overrided in derived classes.
222     def cursor_goto_previous_line
223 bluedwarf 40 @view.hide_alert
224    
225 bluedwarf 19 if @cursor.row == 0
226 bluedwarf 40 @view.show_alert("The first line")
227 bluedwarf 19 else
228     @cursor.row -= 1
229     if @buffer.lines[@cursor.row].size < @cursor.column
230     @cursor.column = @buffer.lines[@cursor.row].size
231     end
232 bluedwarf 21
233     refresh_cursor
234 bluedwarf 19 end
235 bluedwarf 21 end
236 bluedwarf 19
237 bluedwarf 21 #Remove a character just in front of the cursor and move the cursor
238     #backward.
239     #
240     #=== Warning
241     #This method *MUST* *NOT* be overrided in derived classes.
242     def backspace
243 bluedwarf 40 @view.hide_alert
244    
245 bluedwarf 38 pre_cursor = @cursor.clone
246    
247     begin
248     if @cursor.column == 0 && @cursor.row == 0
249     #ToDo: Alert that the cursor is at the begin of the current buffer.
250 bluedwarf 40 @view.show_alert("Beginning of buffer")
251 bluedwarf 38 elsif @cursor.column == 0 # && @cursor.row != 0
252     #Concatenate the current lien into the previous line.
253     @buffer.delete_at(@cursor.row -= 1,
254     @cursor.column = @buffer.lines[@cursor.row].size)
255     refresh_cursor
256     else
257     @buffer.delete_at(@cursor.row, @cursor.column -= 1)
258     refresh_cursor
259     end
260     rescue ReadOnlyException => e
261     @cursor = pre_cursor
262 bluedwarf 40 @view.show_alert(e.to_s)
263 bluedwarf 21 end
264 bluedwarf 19 end
265    
266 bluedwarf 21 #Remove a character beneath the cursor.
267     #
268     #=== Warning
269     #This method *MUST* *NOT* be overrided in derived classes.
270 bluedwarf 33 def delete_at_cursor
271 bluedwarf 40 @view.hide_alert
272    
273 bluedwarf 33 begin
274     @buffer.delete_at(@cursor.row, @cursor.column)
275     rescue EndOfBufferException => e
276 bluedwarf 40 @view.show_alert(e.to_s)
277 bluedwarf 38 rescue ReadOnlyException => e
278 bluedwarf 40 @view.show_alert(e.to_s)
279 bluedwarf 21 end
280     end
281    
282 bluedwarf 26 #Remove the string in the current line behind the cursor and push it to
283     #the kill ring.
284     #
285     #=== Warning
286     #This method *MUST* *NOT* be overrided in derived classes.
287     def kill_line
288 bluedwarf 40 @view.hide_alert
289    
290 bluedwarf 33 begin
291     @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
292 bluedwarf 38 rescue EdmaruException => e
293 bluedwarf 40 @view.show_alert(e.to_s)
294 bluedwarf 26 end
295     end
296    
297     #Get a certain string from kill ring and insert it at the cursor position.
298     #
299     #=== Warning
300     #This method *MUST* *NOT* be overrided in derived classes.
301     def yank
302 bluedwarf 40 @view.hide_alert
303 bluedwarf 26 insert_at_cursor(@view.yank_from_kill_ring)
304     end
305    
306 bluedwarf 38 #Scroll down this window.
307     #
308     #=== Warning
309     #This method *SHOULD* be overrided in derived clases.
310     def scroll_down
311     end
312    
313 bluedwarf 18 #Redraw the cursor.
314     #
315     #=== Warning
316     #This method *SHOULD* be overrided in derived clases.
317     def refresh_cursor
318     end
319    
320 bluedwarf 9 #Redraw this window.
321     #
322     #=== Warning
323     #This method *SHOULD* be overrided in derived clases.
324 bluedwarf 4 def refresh
325     end
326    
327 bluedwarf 40 #Focus on this window.
328     #
329     #=== Warning
330     #This method *SHOULD* *NOT* be overrided in derived classes.
331     def focus_in
332     @focused = true
333    
334     refresh_cursor
335     end
336    
337     #Focus out from this window.
338     #
339     #=== Warning
340     #This method *SHOULD* *NOT* be overrided in derived classes.
341     def focus_out
342     @focused = false
343    
344     refresh_cursor
345     end
346    
347 bluedwarf 6 #Terminate this view instance.
348     #
349     #=== Warning
350 bluedwarf 7 #This method *MUST* *NOT* be overrided in derived classes.
351 bluedwarf 4 def terminate
352 bluedwarf 7 @modeline.terminate
353    
354     terminate_ui
355 bluedwarf 4 end
356 bluedwarf 7
357     #Free system specific resources.
358     #
359     #=== Warning
360     #This method *SHOULD* be overrided in derived classes.
361     def terminate_ui
362     end
363 bluedwarf 4 end
364     end

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