class DropdownWidget

Attributes

active[R]
options[R]
value[R]

Public Class Methods

new(screen, value, x, y, w) click to toggle source
# File lib/widget/dropdown-widget.rb, line 32
def initialize(screen, value, x, y, w)
    @active  = false
    @screen  = screen
    @value   = value
    @x       = x
    @y       = y
    @w       = w
    @mode    = :normal
    @options = [value]
    @sel     = 0
end

Public Instance Methods

active=(a) click to toggle source
# File lib/widget/dropdown-widget.rb, line 6
def active=(a)
    if(!a)
        @mode = :normal
    end
    @active = a
end
draw() click to toggle source
# File lib/widget/dropdown-widget.rb, line 44
def draw
    if(@mode == :normal)
        @screen.setpos(@y,@x)
        @screen.attron Curses::A_BOLD if @active
        @screen.addstr(@value)
        @screen.attroff Curses::A_BOLD if @active
    elsif(@mode == :selecting)
        sel_width = @w-2
        @screen.setpos(@y,@x)
        @screen.addstr("-"*@w)

        @options.each_with_index do |opt, idx|
            @screen.setpos(@y+idx+1,@x)
            padded_str = " "*sel_width
            opt.chars.each_with_index do |chr, idx|
                padded_str[idx] = chr
            end
            @screen.attron  Curses::A_BOLD if idx == @sel
            @screen.addstr "|"+padded_str+"|"
            @screen.attroff Curses::A_BOLD if idx == @sel
        end

        @screen.setpos(@y+@options.length+1,@x)
        @screen.addstr("-"*@w)
    end
end
fix_sel() click to toggle source

Fix the selection when either the value or options change

# File lib/widget/dropdown-widget.rb, line 24
def fix_sel
    @options.each_with_index do |o, idx|
        if(@value == o)
            @sel = idx
        end
    end
end
handle(chr) click to toggle source
# File lib/widget/dropdown-widget.rb, line 71
def handle(chr)
    if(chr == Curses::KEY_RIGHT)
        @mode  = :selecting
        @value = @options[@sel]
        true
    elsif(chr == Curses::KEY_LEFT)
        @mode = :normal
        true
    elsif(chr == Curses::KEY_DOWN && @mode == :selecting)
        @sel   = [@sel+1, @options.length-1].min
        @value = @options[@sel]
        true
    elsif(chr == Curses::KEY_UP && @mode == :selecting)
        @sel   = [@sel-1, 0].max
        @value = @options[@sel]
        true
    else
        false
    end
end
options=(o) click to toggle source
# File lib/widget/dropdown-widget.rb, line 13
def options=(o)
    @options = o
    fix_sel
end
value=(v) click to toggle source
# File lib/widget/dropdown-widget.rb, line 18
def value=(v)
    @value = v
    fix_sel
end