Develop and Download Open Source Software

Browse Subversion Repository

Contents of /window.rb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations) (download)
Tue Apr 10 13:07:50 2007 UTC (16 years, 11 months ago) by bluedwarf
File size: 7786 byte(s)
Improved the redraw method of Gtk.

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

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