| 1 |
# ncurses_view.rb: the class definition of Edmaru::Ncurses::NcursesView |
| 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 "ncurses" |
| 21 |
require "ncurses/ncurses_color.rb" |
| 22 |
|
| 23 |
module Edmaru::Ncurses |
| 24 |
|
| 25 |
#Concrete class by Ncurses library |
| 26 |
class NcursesView |
| 27 |
include Edmaru::View |
| 28 |
|
| 29 |
#Initilizing Ncurses library for Edmaru. |
| 30 |
def init_ui |
| 31 |
# The standard initialization. |
| 32 |
Ncurses::initscr |
| 33 |
Ncurses::start_color |
| 34 |
Ncurses::raw |
| 35 |
Ncurses::stdscr.keypad(TRUE) |
| 36 |
Ncurses::noecho |
| 37 |
|
| 38 |
# set default color |
| 39 |
Edmaru::Ncurses::NcursesColor::init_colors(@config) |
| 40 |
|
| 41 |
# "refresh" should be called here to show other sub windows. |
| 42 |
Ncurses::refresh |
| 43 |
|
| 44 |
#This view's main loop continues as long as this flag is false. |
| 45 |
@exit_flag = false |
| 46 |
end |
| 47 |
|
| 48 |
#Initialize NCurses windows. |
| 49 |
def init_windows |
| 50 |
@windows[0].init_ncurses_window(0, 0, |
| 51 |
ncurses_allocated_column, |
| 52 |
ncurses_allocated_line - 1) |
| 53 |
|
| 54 |
@mini_window.init_ncurses_window(0, ncurses_allocated_line - 1, |
| 55 |
ncurses_allocated_column, 1) |
| 56 |
end |
| 57 |
|
| 58 |
|
| 59 |
#Total column size allocated for this view. |
| 60 |
def ncurses_allocated_column |
| 61 |
a1 = Array.new |
| 62 |
a2 = Array.new |
| 63 |
Ncurses::getmaxyx(Ncurses::stdscr, a1, a2) |
| 64 |
return a2[0] |
| 65 |
end |
| 66 |
|
| 67 |
#Total numbe of lines allocated for this view. |
| 68 |
def ncurses_allocated_line |
| 69 |
a1 = Array.new |
| 70 |
a2 = Array.new |
| 71 |
Ncurses::getmaxyx(Ncurses::stdscr, a1, a2) |
| 72 |
return a1[0] |
| 73 |
end |
| 74 |
|
| 75 |
def request_resize(window, row) |
| 76 |
if window == @mini_window |
| 77 |
max_mini_window_row = (ncurses_allocated_line * 0.2).to_i |
| 78 |
if max_mini_window_row <= 0 |
| 79 |
max_mini_window_row = 1 |
| 80 |
end |
| 81 |
|
| 82 |
if row <= max_mini_window_row && |
| 83 |
window.ncurses_allocated_row < row |
| 84 |
@windows[0].ncurses_move_resize(0, 0, |
| 85 |
ncurses_allocated_column, |
| 86 |
ncurses_allocated_line - row) |
| 87 |
@mini_window.ncurses_move_resize(0, ncurses_allocated_line - row, |
| 88 |
ncurses_allocated_column, row) |
| 89 |
return true |
| 90 |
elsif window.ncurses_allocated_row > row |
| 91 |
@windows[0].ncurses_move_resize(0, 0, |
| 92 |
ncurses_allocated_column, |
| 93 |
ncurses_allocated_line - row) |
| 94 |
@mini_window.ncurses_move_resize(0, ncurses_allocated_line - row, |
| 95 |
ncurses_allocated_column, row) |
| 96 |
return true |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
return false |
| 101 |
end |
| 102 |
|
| 103 |
#Set _exit_flag_ as true to exit the running main loop. |
| 104 |
# |
| 105 |
#=== Warning |
| 106 |
#This method doesn't exit the running main loop as soon as it is |
| 107 |
#called. |
| 108 |
def exit_main_loop |
| 109 |
@exit_flag = true |
| 110 |
end |
| 111 |
|
| 112 |
#Main loop to catch key events. |
| 113 |
def main_loop |
| 114 |
while true |
| 115 |
ch = Ncurses::getch |
| 116 |
|
| 117 |
case ch |
| 118 |
when 0x21..0x7f #Normal ASCII codes. |
| 119 |
@event_manager.handle("KeyPress:#{ch.chr}") |
| 120 |
when 0x01..0x1a #Control-a - Control-z |
| 121 |
tmp_ch = ch + "a"[0] - 1 |
| 122 |
@event_manager.handle("KeyPress:control-#{tmp_ch.chr}") |
| 123 |
when 0x1b |
| 124 |
@event_manager.handle("KeyPress:escape") |
| 125 |
when 0x20 |
| 126 |
@event_manager.handle("KeyPress:space") |
| 127 |
when Ncurses::KEY_DOWN |
| 128 |
@event_manager.handle("KeyPress:down-arrow") |
| 129 |
when Ncurses::KEY_UP |
| 130 |
@event_manager.handle("KeyPress:up-arrow") |
| 131 |
when Ncurses::KEY_LEFT |
| 132 |
@event_manager.handle("KeyPress:left-arrow") |
| 133 |
when Ncurses::KEY_RIGHT |
| 134 |
@event_manager.handle("KeyPress:right-arrow") |
| 135 |
when Ncurses::KEY_DC |
| 136 |
@event_manager.handle("KeyPress:delete") |
| 137 |
when Ncurses::KEY_BACKSPACE |
| 138 |
@event_manager.handle("KeyPress:backspace") |
| 139 |
else |
| 140 |
@event_manager.handle("KeyPress:#{ch}") |
| 141 |
end |
| 142 |
|
| 143 |
#Exit this main loop. |
| 144 |
if @exit_flag |
| 145 |
break |
| 146 |
end |
| 147 |
end |
| 148 |
end |
| 149 |
|
| 150 |
#Beep once. |
| 151 |
def beep |
| 152 |
Ncurses::beep |
| 153 |
end |
| 154 |
|
| 155 |
#Terminate this view instance and free allocated memory of Ncurses |
| 156 |
#library. |
| 157 |
def terminate_ui |
| 158 |
Ncurses::endwin |
| 159 |
end |
| 160 |
end |
| 161 |
end |