Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show 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 # 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)
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 next line.
200 #
201 #=== Warning
202 #This method *MUST* *NOT* be overrided in derived classes.
203 def cursor_goto_next_line
204 @view.hide_alert
205
206 if @cursor.row == (@buffer.lines.size - 1)
207 @view.show_alert("The last line")
208 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
214 refresh_cursor
215 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 @view.hide_alert
224
225 if @cursor.row == 0
226 @view.show_alert("The first line")
227 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
233 refresh_cursor
234 end
235 end
236
237 #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 @view.hide_alert
244
245 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 @view.show_alert("Beginning of buffer")
251 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 @view.show_alert(e.to_s)
263 end
264 end
265
266 #Remove a character beneath the cursor.
267 #
268 #=== Warning
269 #This method *MUST* *NOT* be overrided in derived classes.
270 def delete_at_cursor
271 @view.hide_alert
272
273 begin
274 @buffer.delete_at(@cursor.row, @cursor.column)
275 rescue EndOfBufferException => e
276 @view.show_alert(e.to_s)
277 rescue ReadOnlyException => e
278 @view.show_alert(e.to_s)
279 end
280 end
281
282 #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 @view.hide_alert
289
290 begin
291 @view.push_to_kill_ring(@buffer.delete_line(@cursor.row, @cursor.column))
292 rescue EdmaruException => e
293 @view.show_alert(e.to_s)
294 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 @view.hide_alert
303 insert_at_cursor(@view.yank_from_kill_ring)
304 end
305
306 #Scroll down this window.
307 #
308 #=== Warning
309 #This method *SHOULD* be overrided in derived clases.
310 def scroll_down
311 end
312
313 #Redraw the cursor.
314 #
315 #=== Warning
316 #This method *SHOULD* be overrided in derived clases.
317 def refresh_cursor
318 end
319
320 #Redraw this window.
321 #
322 #=== Warning
323 #This method *SHOULD* be overrided in derived clases.
324 def refresh
325 end
326
327 #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 #Terminate this view instance.
348 #
349 #=== Warning
350 #This method *MUST* *NOT* be overrided in derived classes.
351 def terminate
352 @modeline.terminate
353
354 terminate_ui
355 end
356
357 #Free system specific resources.
358 #
359 #=== Warning
360 #This method *SHOULD* be overrided in derived classes.
361 def terminate_ui
362 end
363 end
364 end

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