| 1 |
# ncurses_mini_window.rb: the class definition of |
| 2 |
# Edmaru::Ncurses::NcursesMiniWindow |
| 3 |
# |
| 4 |
# Copyright (C) 2007 Takashi Nakamoto |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License version 2 as |
| 8 |
# published by the Free Software Foundation. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
# 02110-1301 USA. |
| 19 |
# |
| 20 |
|
| 21 |
require "mini_window.rb" |
| 22 |
require "ncurses/ncurses_window.rb" |
| 23 |
|
| 24 |
module Edmaru::Ncurses |
| 25 |
class NcursesMiniWindow |
| 26 |
include Edmaru::MiniWindow |
| 27 |
|
| 28 |
#Nothing to do. |
| 29 |
def init_ui |
| 30 |
end |
| 31 |
|
| 32 |
#Allocate memory for this window. |
| 33 |
# |
| 34 |
#=== Arguments |
| 35 |
# |
| 36 |
#_x_ :: The left edge position in the parent view. |
| 37 |
#_y_ :: The top edge position in the parent view. |
| 38 |
#_col_ :: The max column size allocated for this window. |
| 39 |
#_row_ :: The max line size allocated for this window. |
| 40 |
def init_ncurses_window(x, y, col, row) |
| 41 |
@win = Ncurses::newwin(row, col, y, x) |
| 42 |
@max_row = @allocated_row = row |
| 43 |
@allocated_column = col |
| 44 |
|
| 45 |
#The right edge is reserved for an arrow mark that indicates whether |
| 46 |
#the line is wrapped. |
| 47 |
@max_column = col - 1 |
| 48 |
end |
| 49 |
|
| 50 |
def ncurses_move_resize(x, y, col, row) |
| 51 |
return if @win == nil |
| 52 |
|
| 53 |
@win.delwin |
| 54 |
|
| 55 |
if @p_start_row != nil |
| 56 |
@p_start_row += (@allocated_row - row) |
| 57 |
@p_start_row = 0 if @p_start_row < 0 |
| 58 |
end |
| 59 |
|
| 60 |
init_ncurses_window(x, y, col, row) |
| 61 |
refresh |
| 62 |
end |
| 63 |
|
| 64 |
#Refresh hte cursor. Move the cursor to the correct position. |
| 65 |
def refresh_cursor |
| 66 |
return if @alerting |
| 67 |
return if @offscreen == nil |
| 68 |
@p_start_row = 0 if @p_start_row == nil |
| 69 |
|
| 70 |
#There is no need to update cursor if this window is not |
| 71 |
#focused. |
| 72 |
return if !@focused |
| 73 |
|
| 74 |
p_cursor_row = 0 |
| 75 |
if @cursor.column == 0 |
| 76 |
p_cursor_col = 0 |
| 77 |
elsif (@cursor.column % @max_column) == 0 && |
| 78 |
@cursor.column == @buffer.lines[@cursor.row].size |
| 79 |
p_cursor_row += (@cursor.column / @max_column - 1) |
| 80 |
p_cursor_col = @max_column |
| 81 |
else |
| 82 |
p_cursor_row += (@cursor.column / @max_column) |
| 83 |
p_cursor_col = @cursor.column % @max_column |
| 84 |
end |
| 85 |
|
| 86 |
if (p_cursor_row - @p_start_row) >= @max_row |
| 87 |
# scroll down |
| 88 |
@p_start_row += 1 |
| 89 |
refresh |
| 90 |
return |
| 91 |
elsif (p_cursor_row - @p_start_row) < 0 && @p_start_row > 0 |
| 92 |
# scroll up |
| 93 |
@p_start_row -= 1 |
| 94 |
@p_start_row = 0 if @p_start_row < 0 |
| 95 |
|
| 96 |
refresh |
| 97 |
return |
| 98 |
end |
| 99 |
|
| 100 |
@win.move(p_cursor_row - @p_start_row, p_cursor_col) |
| 101 |
|
| 102 |
@win.refresh |
| 103 |
end |
| 104 |
|
| 105 |
#Redraw this window. |
| 106 |
def refresh |
| 107 |
return if @alerting |
| 108 |
|
| 109 |
@offscreen = Array.new |
| 110 |
@p_start_row = 0 if @p_start_row == nil |
| 111 |
|
| 112 |
physical_row = 0 |
| 113 |
line = @buffer.lines[0].clone |
| 114 |
|
| 115 |
while(true) |
| 116 |
if line.size == 0 |
| 117 |
@offscreen.push("") |
| 118 |
break |
| 119 |
elsif line.size > @max_column |
| 120 |
@offscreen.push(line.slice!(0 ... @max_column) + "\\") |
| 121 |
else |
| 122 |
@offscreen.push(line) |
| 123 |
break |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
if @offscreen.size != @max_row |
| 128 |
if @view.request_resize(self, @offscreen.size) |
| 129 |
return |
| 130 |
end |
| 131 |
end |
| 132 |
|
| 133 |
for window_row in 0...@max_row |
| 134 |
row = @p_start_row + window_row |
| 135 |
|
| 136 |
if row >= @offscreen.size || @offscreen[row].size == 0 |
| 137 |
@win.mvaddstr(window_row, 0, " " * @allocated_column) |
| 138 |
else |
| 139 |
@win.mvaddstr(window_row, 0, @offscreen[row]) |
| 140 |
if @offscreen[row].size < @allocated_column |
| 141 |
@win.addstr(" " * (@allocated_column - @offscreen[row].size)) |
| 142 |
end |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
@win.refresh |
| 147 |
|
| 148 |
@view.focused_window.refresh_cursor |
| 149 |
end |
| 150 |
|
| 151 |
#Show alert message tempoarily. |
| 152 |
# |
| 153 |
#=== Argument |
| 154 |
#_message_ :: The message to be shown. |
| 155 |
def show_alert(message) |
| 156 |
@alerting = true |
| 157 |
message = message.clone |
| 158 |
|
| 159 |
tmp_offscreen = Array.new |
| 160 |
|
| 161 |
while(true) |
| 162 |
if message.size > @max_column |
| 163 |
tmp_offscreen.push(message.slice!(0 ... @max_column) + "\\") |
| 164 |
else |
| 165 |
tmp_offscreen.push(message) |
| 166 |
break |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
for row in 0...@max_row |
| 171 |
if row >= tmp_offscreen.size || tmp_offscreen[row].size == 0 |
| 172 |
@win.mvaddstr(row, 0, " " * @allocated_column) |
| 173 |
else |
| 174 |
@win.mvaddstr(row, 0, tmp_offscreen[row]) |
| 175 |
if tmp_offscreen[row].size < @allocated_column |
| 176 |
@win.addstr(" " * (@allocated_column - tmp_offscreen[row].size)) |
| 177 |
end |
| 178 |
end |
| 179 |
end |
| 180 |
|
| 181 |
@win.refresh |
| 182 |
|
| 183 |
@view.focused_window.refresh_cursor if @view.focused_window != self |
| 184 |
end |
| 185 |
|
| 186 |
def hide_alert |
| 187 |
@alerting = false |
| 188 |
refresh |
| 189 |
end |
| 190 |
|
| 191 |
def ncurses_allocated_row |
| 192 |
@allocated_row |
| 193 |
end |
| 194 |
|
| 195 |
def ncurses_allocated_column |
| 196 |
@allocated_column |
| 197 |
end |
| 198 |
|
| 199 |
#Free allocated memory for this window. |
| 200 |
def terminate_ui |
| 201 |
@win.delwin |
| 202 |
end |
| 203 |
end |
| 204 |
end |