Sha256: 6d2dd41ac7bec46914767b426acf03545b6be9e13e5a918a00e56d2682c28e1f

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

# -*- encoding : utf-8 -*-

module PPCurses

  class ComboBox < View

    DOWN_ARROW = '∇'

    # Does the element have focus in the form?
    attr_accessor :selected


    def initialize(label, options)
      @label = label
      @options = options

      @display_width = 13
      @choice_made = false

      @options.each do |option|
        if option.length > @display_width
          @display_width = option.length
        end
      end

    end

    def show(screen)
      screen.attron(A_REVERSE) if @selected
      screen.addstr("#{@label}:")

      @combo_display_point = screen.cur_point

      screen.attroff(A_REVERSE) if @selected

      screen.addstr(display_string)
    end

    def height
      1
    end

    def set_curs_pos(screen)
      screen.curs_set(INVISIBLE)
    end

    # Return Value
    # The object in the receiver's internal item list corresponding to the last item selected
    # from the pop-up list, or nil if no item is selected.
    def object_value_of_selected_item
      if  @options_menu.nil?
        return nil
      end

      @options[@options_menu.selection]
    end

    def key_down(key)

      if key == ENTER

        if  @options_menu.nil?
          @options_menu = PPCurses::ChoiceMenu.new( @options )
        end

        @options_menu.set_origin(@combo_display_point)

        @options_menu.show
        @options_menu.menu_selection

        if @options_menu.pressed_enter
          @choice_made = true
        end

      end

    end
    
    def clear
      @choice_made = false
    end

    #------------------------------------------------
    protected
    def display_string
      if @choice_made
        return ' '+ "#{@options[@options_menu.selection].center(@display_width,'-')} #{DOWN_ARROW}"
      end

      ' ' + ' select '.center(@display_width,'-') + " #{DOWN_ARROW}"
    end



  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ppcurses-0.1.2 lib/ppcurses/form/combo_box.rb