Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations) (download)
Mon Apr 9 17:45:05 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 7766 byte(s)
Wrapping long lines for Ncurses. Added new actions; "backspace", "delete".

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 6 @view = view
41 bluedwarf 20 @config = config
42 bluedwarf 9 @cursor = Cursor.new(0, 0)
43 bluedwarf 17 @modeline = Edmaru::SYSTEM_MODELINE.new(self)
44 bluedwarf 9
45 bluedwarf 15 init_ui
46 bluedwarf 4 end
47    
48 bluedwarf 6 #The system specific initialization for this window.
49     #
50     #=== Warning
51     #This method *SHOULD* be overrided in derived classes.
52 bluedwarf 15 def init_ui
53 bluedwarf 5 end
54    
55 bluedwarf 9 #Discard the current buffer and set the specified new buffer.
56     #
57 bluedwarf 18 #=== Warning
58     #This method *MUST* *NOT* be overrided in derived classes.
59     #
60 bluedwarf 9 #=== Argument
61     #_new_buffer_ :: The buffer to be shown in this window.
62     def buffer=(new_buffer)
63     if @buffer != nil
64     @buffer.unlink(self)
65     end
66 bluedwarf 4
67 bluedwarf 9 #Linked buffer to this window.
68     @buffer = new_buffer
69     @buffer.link(self)
70     refresh
71 bluedwarf 4 end
72    
73 bluedwarf 9 def buffer
74     @buffer
75 bluedwarf 4 end
76    
77 bluedwarf 15 def modeline
78     @modeline
79     end
80    
81 bluedwarf 18 #Insert the specified string to the buffer shown in this window.
82     #
83     #=== Warning
84     #This method *MUST* *NOT* be overrided in derived classes.
85     #
86     #=== Argument
87     #_str_ :: String to be inserted.
88     #_move_cursor_ :: _true_ to cause the cursor move to the end of the
89     #inserted string.
90     def insert_at_cursor(str, move_cursor = true)
91     #Insert to the buffer.
92     @buffer.insert(@cursor.row, @cursor.column, str)
93    
94     #Move the cursor to the end of inserted string.
95     if move_cursor
96     str.size.times{ |n|
97     cursor_forward
98     }
99     end
100    
101     refresh
102     end
103    
104     #Move the cursor forward.
105     #
106     #=== Argument
107     #_n_ :: the number of times to forward the cursor.
108     #
109     #=== Warning
110     #This method *MUST* *NOT* be overrided in derived classes.
111     def cursor_forward(n = 1)
112     n.times{
113     if @cursor.column >= @buffer.lines[@cursor.row].size &&
114     @cursor.row == (@buffer.lines.size - 1)
115     #ToDo: Alert that the cursor is at the end of this buffer.
116     @view.beep
117     elsif @cursor.column >= @buffer.lines[@cursor.row].size
118     #Go to the begin of the next line
119     @cursor.row += 1
120     @cursor.column = 0
121     else
122     @cursor.column += 1
123     end
124     }
125    
126     refresh_cursor
127     end
128    
129     #Move the cursor backward.
130     #
131     #=== Argument
132     #_n_ :: the number of times to backward the cursor.
133     #
134     #=== Warning
135     #This method *MUST* *NOT* be overrided in derived classes.
136     def cursor_backward(n = 1)
137     n.times{
138     if @cursor.column == 0 && @cursor.row == 0
139     #ToDo: Alert that the cursor is at the begin of this buffer.
140     @view.beep
141     elsif @cursor.column == 0
142     @cursor.row -= 1
143     @cursor.column = @buffer.lines[@cursor.row].size
144     else
145     @cursor.column -= 1
146     end
147     }
148    
149     refresh_cursor
150     end
151    
152     #Move the cursor to the begin of the current line.
153     #
154     #=== Warning
155     #This method *MUST* *NOT* be overrided in derived classes.
156     def cursor_goto_line_head
157     @cursor.column = 0
158     refresh_cursor
159     end
160    
161     #Move the cursor to the end of the current line.
162     #
163     #=== Warning
164     #This method *MUST* *NOT* be overrided in derived classes.
165     def cursor_goto_line_tail
166     @cursor.column = buffer.lines[@cursor.row].size
167     refresh_cursor
168     end
169    
170 bluedwarf 19 #Move the cursor to the next line.
171     #
172     #=== Warning
173     #This method *MUST* *NOT* be overrided in derived classes.
174     def cursor_goto_next_line
175     if @cursor.row == (@buffer.lines.size - 1)
176     #ToDo: Alert that the cursor is in the last line.
177     @view.beep
178     else
179     @cursor.row += 1
180     if @buffer.lines[@cursor.row].size < @cursor.column
181     @cursor.column = @buffer.lines[@cursor.row].size
182     end
183 bluedwarf 21
184     refresh_cursor
185 bluedwarf 19 end
186     end
187    
188     #Move the cursor to the previous line.
189     #
190     #=== Warning
191     #This method *MUST* *NOT* be overrided in derived classes.
192     def cursor_goto_previous_line
193     if @cursor.row == 0
194     #ToDo: Alert that the cursor is in the previous line.
195     @view.beep
196     else
197     @cursor.row -= 1
198     if @buffer.lines[@cursor.row].size < @cursor.column
199     @cursor.column = @buffer.lines[@cursor.row].size
200     end
201 bluedwarf 21
202     refresh_cursor
203 bluedwarf 19 end
204 bluedwarf 21 end
205 bluedwarf 19
206 bluedwarf 21 #Remove a character just in front of the cursor and move the cursor
207     #backward.
208     #
209     #=== Warning
210     #This method *MUST* *NOT* be overrided in derived classes.
211     def backspace
212     if @cursor.column == 0 && @cursor.row == 0
213     #ToDo: Alert that the cursor is at the begin of the current buffer.
214     @view.beep
215     elsif @cursor.column == 0
216     #Concatenate the current lien into the previous line.
217     current_line = @buffer.lines[@cursor.row]
218     prev_line_size = @buffer.lines[@cursor.row - 1].size
219    
220     @buffer.lines[@cursor.row - 1] << current_line
221     @buffer.lines.delete_at(@cursor.row)
222    
223     @cursor.column = prev_line_size
224     @cursor.row -= 1
225    
226     refresh
227     else
228     line = @buffer.lines[@cursor.row]
229     @buffer.lines[@cursor.row] =
230     line[0, @cursor.column - 1] + line[@cursor.column .. -1]
231     @cursor.column -= 1
232     refresh
233     end
234 bluedwarf 19 end
235    
236 bluedwarf 21 #Remove a character beneath the cursor.
237     #
238     #=== Warning
239     #This method *MUST* *NOT* be overrided in derived classes.
240     def delete
241     if @cursor.column >= @buffer.lines[@cursor.row].size &&
242     @cursor.row == (@buffer.lines.size - 1)
243     #ToDo: Alert that the cursor is at the end of this buffer.
244     @view.beep
245     elsif @cursor.column >= @buffer.lines[@cursor.row].size
246     #Concatenate the next line behind the current line.
247     @buffer.lines[@cursor.row] << @buffer.lines[@cursor.row + 1]
248     @buffer.lines.delete_at(@cursor.row + 1)
249     refresh
250     else
251     line = @buffer.lines[@cursor.row]
252     @buffer.lines[@cursor.row] =
253     line[0, @cursor.column] + line[@cursor.column + 1 .. -1]
254     refresh
255     end
256     end
257    
258 bluedwarf 18 #Redraw the cursor.
259     #
260     #=== Warning
261     #This method *SHOULD* be overrided in derived clases.
262     def refresh_cursor
263     end
264    
265 bluedwarf 9 #Redraw this window.
266     #
267     #=== Warning
268     #This method *SHOULD* be overrided in derived clases.
269 bluedwarf 4 def refresh
270     end
271    
272 bluedwarf 6 #Terminate this view instance.
273     #
274     #=== Warning
275 bluedwarf 7 #This method *MUST* *NOT* be overrided in derived classes.
276 bluedwarf 4 def terminate
277 bluedwarf 7 @modeline.terminate
278    
279     terminate_ui
280 bluedwarf 4 end
281 bluedwarf 7
282     #Free system specific resources.
283     #
284     #=== Warning
285     #This method *SHOULD* be overrided in derived classes.
286     def terminate_ui
287     end
288 bluedwarf 4 end
289     end

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