Sha256: 5c27296b736d0960fcb4a1c601b8da9b67fb67dcf16fb5c398c9e09e43282ff6

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

require 'curses'

module PPCurses

  #noinspection RubyResolve
  class BaseMenu

    attr_accessor :menu_items
    attr_accessor :selection

    attr_accessor :side_wall_char
    attr_accessor :top_bot_wall_char

    #
    # Current base ruby is 1.9.2.  action_items could be an optional parameter
    # if the base was moved up to 2.0
    #
    def initialize(menu_items, action_items )
      @selection=0
      @max_menu_width = 0

      @side_wall_char = '|'
      @top_bot_wall_char = '-'

      sample = menu_items[0]

      case sample
        when String
          # Case 1: menu_items is a list of strings, with an associated action list
          build_menu_items(menu_items, action_items)
        else
          # Case 2: Received a list of MenuItem, or objects that implement
          # MenuItem protocol
          @menu_items=menu_items
      end

      find_max_menu_width

      create_window

    end

    def build_menu_items(menu_items, action_items)
      @menu_items = []

      (0...menu_items.length).each { |i|
        menu_item = MenuItem.new(menu_items[i])
        unless action_items.nil?
          menu_item.target= action_items[i].method(:execute)
        end

        @menu_items.push(menu_item)
      }
    end

    def find_max_menu_width
      @max_menu_width = 0
      (0...menu_items.length).each { |i|
        display = menu_items[i].display_string
        @max_menu_width = display.length if display.length > @max_menu_width
      }
    end

    def create_window
      w_height = @menu_items.length + 4
      w_width = @max_menu_width + 4
      @win = Window.new(w_height,w_width,(lines-w_height) / 2, (cols-w_width)/2)

      @win.timeout=-1
      # Enables reading arrow keys in getch
      @win.keypad(true)
    end

    def set_sub_menu(menu)
      @sub_menu = menu
    end

    def hide
      @win.clear
      @win.refresh

      @sub_menu.hide if @sub_menu
    end

    def selected_menu_name
      @menu_items[@selection].title
    end

    def close
      @win.close
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ppcurses-0.0.25 lib/ppcurses/menu/BaseMenu.rb
ppcurses-0.0.24 lib/ppcurses/menu/BaseMenu.rb
ppcurses-0.0.23 lib/ppcurses/menu/BaseMenu.rb