【发布时间】:2013-07-17 20:33:13
【问题描述】:
我创建了一个类,我试图在 Windows 窗体上模拟 Richtextbox。这意味着当您将新数据添加到表单/richtextbox 时,它会添加到框/窗口的底部,其余部分会向上滚动一行。
我尝试启用scrollok(),但它似乎不想滚动。我不确定它是否被窃听或者我的实现方式是错误的。
class Textpad
attr_accessor :data, :name, :window
def initialize(name, height, width, startx, starty)
@data = []
@name = name
@height = height
@width = width
@startx = startx
@starty = starty
Ncurses.refresh
@window = Ncurses.newwin(height, width, starty, startx)
@window.scrollok true
@window.wrefresh
end
def add(packetid, username, message)
@data.push [Time.new.strftime('[%T]'), packetid, username, message]
@data.shift if @data.length > 500
end
def draw
Ncurses.init_pair(1, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK)
Ncurses.init_pair(2, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK)
Ncurses.init_pair(3, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK)
Ncurses.init_pair(4, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK)
@window.wclear
position = 0
@data.each do |timestamp, packetid, username, message|
case packetid
when '1005'
@window.mvwprintw(1*position, 1, "#{timestamp} «#{username}» #{message}")
@window.mvchgat(1*position, timestamp.length+2, 1, Ncurses::A_NORMAL, 3, NIL)
@window.mvchgat(1*position, timestamp.length+3+username.length, 1, Ncurses::A_NORMAL, 3, NIL) #colorize the symboles around the username
end
position += 1
end
@window.wrefresh
end
end
问题出在我的 Textpad 类的 draw 方法中。我可以用数百个条目填充 Textpad 类的数据数组,但只有数组的最顶部被写入(直到它到达窗口的底部)而没有滚动。我是否必须手动滚动屏幕或其他东西?从文档中它说它应该在光标到达底部并添加另一行时自动滚动。
【问题讨论】:
-
搞定了。显然
mvwprintw()没有移动光标或其他东西,所以我不得不切换到正常的wprintw(),这不是问题,因为我可以添加一个 \n 来换行它。唯一的缺点是我的mvchgat()函数现在缩进文本而不是为特定位置着色。