Class: DropdownWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/widget/dropdown-widget.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screen, value, x, y, w) ⇒ DropdownWidget

Returns a new instance of DropdownWidget.



32
33
34
35
36
37
38
39
40
41
42
# 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

Instance Attribute Details

#activeObject

Returns the value of attribute active.



2
3
4
# File 'lib/widget/dropdown-widget.rb', line 2

def active
  @active
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/widget/dropdown-widget.rb', line 3

def options
  @options
end

#valueObject

Returns the value of attribute value.



4
5
6
# File 'lib/widget/dropdown-widget.rb', line 4

def value
  @value
end

Instance Method Details

#drawObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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_selObject

Fix the selection when either the value or options change



24
25
26
27
28
29
30
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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