| 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 |
# |
| 33 |
#=== Warning |
| 34 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 35 |
# |
| 36 |
#=== Return |
| 37 |
#An initialized instance of Window. |
| 38 |
def initialize(view) |
| 39 |
@view = view |
| 40 |
@cursor = Cursor.new(0, 0) |
| 41 |
@modeline = Edmaru::SYSTEM_MODELINE.new(self) |
| 42 |
|
| 43 |
init_ui |
| 44 |
end |
| 45 |
|
| 46 |
#The system specific initialization for this window. |
| 47 |
# |
| 48 |
#=== Warning |
| 49 |
#This method *SHOULD* be overrided in derived classes. |
| 50 |
def init_ui |
| 51 |
end |
| 52 |
|
| 53 |
#Discard the current buffer and set the specified new buffer. |
| 54 |
# |
| 55 |
#=== Warning |
| 56 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 57 |
# |
| 58 |
#=== Argument |
| 59 |
#_new_buffer_ :: The buffer to be shown in this window. |
| 60 |
def buffer=(new_buffer) |
| 61 |
if @buffer != nil |
| 62 |
@buffer.unlink(self) |
| 63 |
end |
| 64 |
|
| 65 |
#Linked buffer to this window. |
| 66 |
@buffer = new_buffer |
| 67 |
@buffer.link(self) |
| 68 |
refresh |
| 69 |
end |
| 70 |
|
| 71 |
def buffer |
| 72 |
@buffer |
| 73 |
end |
| 74 |
|
| 75 |
def modeline |
| 76 |
@modeline |
| 77 |
end |
| 78 |
|
| 79 |
#Insert the specified string to the buffer shown in this window. |
| 80 |
# |
| 81 |
#=== Warning |
| 82 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 83 |
# |
| 84 |
#=== Argument |
| 85 |
#_str_ :: String to be inserted. |
| 86 |
#_move_cursor_ :: _true_ to cause the cursor move to the end of the |
| 87 |
#inserted string. |
| 88 |
def insert_at_cursor(str, move_cursor = true) |
| 89 |
#Insert to the buffer. |
| 90 |
@buffer.insert(@cursor.row, @cursor.column, str) |
| 91 |
|
| 92 |
#Move the cursor to the end of inserted string. |
| 93 |
if move_cursor |
| 94 |
str.size.times{ |n| |
| 95 |
cursor_forward |
| 96 |
} |
| 97 |
end |
| 98 |
|
| 99 |
refresh |
| 100 |
end |
| 101 |
|
| 102 |
#Move the cursor forward. |
| 103 |
# |
| 104 |
#=== Argument |
| 105 |
#_n_ :: the number of times to forward the cursor. |
| 106 |
# |
| 107 |
#=== Warning |
| 108 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 109 |
def cursor_forward(n = 1) |
| 110 |
n.times{ |
| 111 |
if @cursor.column >= @buffer.lines[@cursor.row].size && |
| 112 |
@cursor.row == (@buffer.lines.size - 1) |
| 113 |
#ToDo: Alert that the cursor is at the end of this buffer. |
| 114 |
@view.beep |
| 115 |
elsif @cursor.column >= @buffer.lines[@cursor.row].size |
| 116 |
#Go to the begin of the next line |
| 117 |
@cursor.row += 1 |
| 118 |
@cursor.column = 0 |
| 119 |
else |
| 120 |
@cursor.column += 1 |
| 121 |
end |
| 122 |
} |
| 123 |
|
| 124 |
refresh_cursor |
| 125 |
end |
| 126 |
|
| 127 |
#Move the cursor backward. |
| 128 |
# |
| 129 |
#=== Argument |
| 130 |
#_n_ :: the number of times to backward the cursor. |
| 131 |
# |
| 132 |
#=== Warning |
| 133 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 134 |
def cursor_backward(n = 1) |
| 135 |
n.times{ |
| 136 |
if @cursor.column == 0 && @cursor.row == 0 |
| 137 |
#ToDo: Alert that the cursor is at the begin of this buffer. |
| 138 |
@view.beep |
| 139 |
elsif @cursor.column == 0 |
| 140 |
@cursor.row -= 1 |
| 141 |
@cursor.column = @buffer.lines[@cursor.row].size |
| 142 |
else |
| 143 |
@cursor.column -= 1 |
| 144 |
end |
| 145 |
} |
| 146 |
|
| 147 |
refresh_cursor |
| 148 |
end |
| 149 |
|
| 150 |
#Move the cursor to the begin of the current line. |
| 151 |
# |
| 152 |
#=== Warning |
| 153 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 154 |
def cursor_goto_line_head |
| 155 |
@cursor.column = 0 |
| 156 |
refresh_cursor |
| 157 |
end |
| 158 |
|
| 159 |
#Move the cursor to the end of the current line. |
| 160 |
# |
| 161 |
#=== Warning |
| 162 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 163 |
def cursor_goto_line_tail |
| 164 |
@cursor.column = buffer.lines[@cursor.row].size |
| 165 |
refresh_cursor |
| 166 |
end |
| 167 |
|
| 168 |
#Redraw the cursor. |
| 169 |
# |
| 170 |
#=== Warning |
| 171 |
#This method *SHOULD* be overrided in derived clases. |
| 172 |
def refresh_cursor |
| 173 |
end |
| 174 |
|
| 175 |
#Redraw this window. |
| 176 |
# |
| 177 |
#=== Warning |
| 178 |
#This method *SHOULD* be overrided in derived clases. |
| 179 |
def refresh |
| 180 |
end |
| 181 |
|
| 182 |
#Terminate this view instance. |
| 183 |
# |
| 184 |
#=== Warning |
| 185 |
#This method *MUST* *NOT* be overrided in derived classes. |
| 186 |
def terminate |
| 187 |
@modeline.terminate |
| 188 |
|
| 189 |
terminate_ui |
| 190 |
end |
| 191 |
|
| 192 |
#Free system specific resources. |
| 193 |
# |
| 194 |
#=== Warning |
| 195 |
#This method *SHOULD* be overrided in derived classes. |
| 196 |
def terminate_ui |
| 197 |
end |
| 198 |
end |
| 199 |
end |