| 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 |
@view = view |
| 41 |
@config = config |
| 42 |
@cursor = Cursor.new(0, 0) |
| 43 |
@modeline = Edmaru::SYSTEM_MODELINE.new(self) |
| 44 |
|
| 45 |
init_ui |
| 46 |
end |
| 47 |
|
| 48 |
#The system specific initialization for this window. |
| 49 |
# |
| 50 |
#=== Warning |
| 51 |
#This method *SHOULD* be overrided in derived classes. |
| 52 |
def init_ui |
| 53 |
end |
| 54 |
|
| 55 |
#Discard the current buffer and set the specified new buffer. |
| 56 |
# |
| 57 |
#=== Warning |
| 58 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 59 |
# |
| 60 |
#=== 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 |
|
| 67 |
#Linked buffer to this window. |
| 68 |
@buffer = new_buffer |
| 69 |
@buffer.link(self) |
| 70 |
refresh |
| 71 |
end |
| 72 |
|
| 73 |
def buffer |
| 74 |
@buffer |
| 75 |
end |
| 76 |
|
| 77 |
def modeline |
| 78 |
@modeline |
| 79 |
end |
| 80 |
|
| 81 |
#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 |
#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 |
end |
| 184 |
|
| 185 |
refresh_cursor |
| 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 |
end |
| 202 |
|
| 203 |
refresh_cursor |
| 204 |
end |
| 205 |
|
| 206 |
#Redraw the cursor. |
| 207 |
# |
| 208 |
#=== Warning |
| 209 |
#This method *SHOULD* be overrided in derived clases. |
| 210 |
def refresh_cursor |
| 211 |
end |
| 212 |
|
| 213 |
#Redraw this window. |
| 214 |
# |
| 215 |
#=== Warning |
| 216 |
#This method *SHOULD* be overrided in derived clases. |
| 217 |
def refresh |
| 218 |
end |
| 219 |
|
| 220 |
#Terminate this view instance. |
| 221 |
# |
| 222 |
#=== Warning |
| 223 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 224 |
def terminate |
| 225 |
@modeline.terminate |
| 226 |
|
| 227 |
terminate_ui |
| 228 |
end |
| 229 |
|
| 230 |
#Free system specific resources. |
| 231 |
# |
| 232 |
#=== Warning |
| 233 |
#This method *SHOULD* be overrided in derived classes. |
| 234 |
def terminate_ui |
| 235 |
end |
| 236 |
end |
| 237 |
end |