class Canis::Button

action buttons Use text to pass the string to be printed on the button An ampersand is understaod to denote a shortcut and will map Alt-char to that button's FIRE event Alternative, +mnemonic(char)+ can also be used. In the config hash, ':hotkey' may be passed which maps the character itself to the button's FIRE. This is for menulinks, and maybe a form that has only buttons.

NOTE: When firing event, an ActionEvent will be passed as the first parameter, followed by anything you may have passed when binding, or calling the command() method.

- Action: may have to listen to Action property changes so enabled, name etc change can be reflected

2011-11-26 : define button as default, so it can show differently and also fire on ENTER trying out behavior change. space to fire current button, ENTER for default button which has > Name < look.

Public Class Methods

button_layout(buttons, row, startcol=0, cols=Ncurses.COLS-1, gap=5) click to toggle source

temporary method, shoud be a proper class

# File lib/canis/core/widgets/rwidget.rb, line 3320
def self.button_layout buttons, row, startcol=0, cols=Ncurses.COLS-1, gap=5
  col = startcol
  buttons.each_with_index do |b, ix|
    $log.debug " BUTTON #{b}: #{b.col} "
    b.row = row
    b.col col
    $log.debug " after BUTTON #{b}: #{b.col} "
    len = b.text.length + gap
    col += len
  end
end
new(form, config={}) click to toggle source
Calls superclass method Canis::Widget::new
# File lib/canis/core/widgets/rwidget.rb, line 3088
def initialize form, config={}, &block
  require 'canis/core/include/ractionevent'
  @focusable = true
  @editable = false
  # hotkey denotes we should bind the key itself not alt-key (for menulinks)
  @hotkey = config.delete(:hotkey) 
  register_events([:PRESS, :FORM_ATTACHED])
  @default_chars = ['> ', ' <'] 
  super


  @surround_chars ||= ['[ ', ' ]'] 
  @col_offset = @surround_chars[0].length 
  @text_offset = 0
  map_keys
end

Public Instance Methods

action(a) click to toggle source

set button based on Action

# File lib/canis/core/widgets/rwidget.rb, line 3106
def action a
  text a.name
  mnemonic a.mnemonic unless a.mnemonic.nil?
  command { a.call }
end
bind_hotkey() click to toggle source

bind hotkey to form keys. added 2008-12-15 20:19 use ampersand in name or underline IS THIS USED ??

# File lib/canis/core/widgets/rwidget.rb, line 3159
def bind_hotkey
  alert "bind_hotkey was called in button"
  if @form.nil? 
    if @underline
      bind(:FORM_ATTACHED){ bind_hotkey }
    end
    return
  end
  _value = @text || getvalue # hack for Togglebutton FIXME
  $log.debug " bind hot #{_value} #{@underline}"
  ch = _value[@underline,1].downcase()[0].ord ##  1.9  2009-10-05 18:55  TOTEST
  @mnemonic = _value[@underline,1]
  # meta key
  mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0))
  @form.bind_key(mch, "hotkey for button #{self.text}" ) { |_form, _butt| self.fire }
end
command(*args, &block) click to toggle source
command of button (invoked on press, hotkey, space)

added args 2008-12-20 19:22

# File lib/canis/core/widgets/rwidget.rb, line 3260
def command *args, &block
  bind :PRESS, *args, &block
end
default_button(tf=nil) click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3175
def default_button tf=nil
  return @default_button unless tf
  raise ArgumentError, "default button must be true or false" if ![false,true].include? tf
  unless @form
    bind(:FORM_ATTACHED){ default_button(tf) }
    return self
  end
  $log.debug "XXX:  BUTTON DEFAULT setting to true : #{tf} "
  @default_button = tf
  if tf
    @surround_chars = @default_chars
    @form.bind_key(13, "fire #{self.text} ") { |_form, _butt| self.fire }
  else
    # i have no way of reversing the above
  end
end
fire() click to toggle source

fires PRESS event of button

# File lib/canis/core/widgets/rwidget.rb, line 3264
def fire
  #$log.debug "firing PRESS #{text}"
  fire_handler :PRESS, ActionEvent.new(self, :PRESS, text)
end
getvalue() click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3192
def getvalue
  #@text_variable.nil? ? @text : @text_variable.get_value(@name)
  @text
end
getvalue_for_paint() click to toggle source

ensure text has been passed or action

# File lib/canis/core/widgets/rwidget.rb, line 3198
def getvalue_for_paint
  ret = getvalue
  @text_offset = @surround_chars[0].length
  @surround_chars[0] + ret + @surround_chars[1]
end
handle_key(ch) click to toggle source

Button

Calls superclass method Canis::Widget#handle_key
# File lib/canis/core/widgets/rwidget.rb, line 3281
def handle_key ch
  super
end
map_keys() click to toggle source
# File lib/canis/core/widgets/rwidget.rb, line 3271
def map_keys
  return if @keys_mapped
  bind_key(32, "fire") { fire } if respond_to? :fire
  if $key_map_type == :vim
    bind_key( key("j"), "down") { @form.window.ungetch(KEY_DOWN) }
    bind_key( key("k"), "up") { @form.window.ungetch(KEY_UP) }
  end
end
mnemonic(char=nil) click to toggle source

set mnemonic for button, this is a hotkey that triggers fire upon pressing Alt+char

# File lib/canis/core/widgets/rwidget.rb, line 3137
def mnemonic char=nil
  return @mnemonic unless char  # added 2011-11-24 so caller can get mne

  unless @form
    # we have some processing for when a form is attached, registering a hotkey
    bind(:FORM_ATTACHED) { mnemonic char }
    return self # added 2014-03-23 - 22:59 so that we can chain methods
  end
  @mnemonic = char
  ch = char.downcase()[0].ord ##  1.9
  # meta key
  ch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0)) unless @hotkey
  $log.debug " #{self} setting MNEMO to #{char} #{ch}, #{@hotkey} "
  _t = self.text || self.name || "Unnamed #{self.class} "
  @form.bind_key(ch, "hotkey for button #{_t} ") { |_form, _butt| self.fire }
  return self # added 2015-03-23 - 22:59 so that we can chain methods
end
repaint() click to toggle source

FIXME 2014-05-31 since form checks for highlight color and sets repaint on on_enter, we shoul not set it.

but what if it is set at form level ?
 also it is not correct to set colors now that form's defaults are taken
# File lib/canis/core/widgets/rwidget.rb, line 3207
    def repaint  # button

      #@bgcolor ||= $def_bg_color
      #@color   ||= $def_fg_color
        $log.debug("BUTTON repaint : #{self}  r:#{@row} c:#{@col} , #{@color} , #{@bgcolor} , #{getvalue_for_paint}" )
        r,c = @row, @col #rowcol include offset for putting cursor
        # NOTE: please override both (if using a string), or else it won't work
        #  These are both colorpairs not colors ??? 2014-05-31 - 11:58
        _highlight_color = @highlight_color || $reversecolor
        _highlight_bgcolor = @highlight_bgcolor || 0
        _bgcolor = bgcolor()
        _color = color()
        if @state == :HIGHLIGHTED
          _bgcolor = @state==:HIGHLIGHTED ? _highlight_bgcolor : _bgcolor
          _color = @state==:HIGHLIGHTED ? _highlight_color : _color
        elsif selected? # only for certain buttons lie toggle and radio
          _bgcolor = @selected_bgcolor || _bgcolor
          _color   = @selected_color || _color
        end
        $log.debug "XXX: button #{text}   STATE is #{@state} color #{_color} , bg: #{_bgcolor} "
        if _bgcolor.is_a?( Integer) && _color.is_a?( Integer)
          # i think this means they are colorpairs not colors, but what if we use colors on the 256 scale ?
          #  i don;t like this at all.
        else
          _color = get_color($datacolor, _color, _bgcolor)
        end
        value = getvalue_for_paint
        $log.debug("button repaint :#{self} r:#{r} c:#{c} col:#{_color} bg #{_bgcolor} v: #{value} ul #{@underline} mnem #{@mnemonic} datacolor #{$datacolor} ")
        len = @width || value.length
        @graphic = @form.window if @graphic.nil? ## cell editor listbox hack
        @graphic.printstring r, c, "%-*s" % [len, value], _color, attr()
#       @form.window.mvchgat(y=r, x=c, max=len, Ncurses::A_NORMAL, bgcolor, nil)
        # in toggle buttons the underline can change as the text toggles
        if @underline || @mnemonic
          uline = @underline && (@underline + @text_offset) ||  value.index(@mnemonic) || 
            value.index(@mnemonic.swapcase)
          # if the char is not found don't print it
          if uline
            y=r #-@graphic.top
            x=c+uline #-@graphic.left
            #
            # NOTE: often values go below zero since root windows are defined
            # with 0 w and h, and then i might use that value for calcaluting
            #
            $log.error "XXX button underline location error #{x} , #{y} " if x < 0 or c < 0
            raise " #{r} #{c}  #{uline} button underline location error x:#{x} , y:#{y}. left #{@graphic.left} top:#{@graphic.top} " if x < 0 or c < 0
            @graphic.mvchgat(y, x, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, _color, nil)
          end
        end
    end
selected?() click to toggle source

for campatibility with all buttons, will apply to radio buttons mostly

# File lib/canis/core/widgets/rwidget.rb, line 3269
def selected?; false; end
text(*val) click to toggle source

button: sets text, checking for ampersand, uses that for hotkey and underlines

# File lib/canis/core/widgets/rwidget.rb, line 3113
def text(*val)
  if val.empty?
    return @text
  else
    s = val[0].dup
    s = s.to_s if !s.is_a? String  # 2009-01-15 17:32
    if (( ix = s.index('&')) != nil)
      s.slice!(ix,1)
      @underline = ix #unless @form.nil? # this setting a fake underline in messageboxes
      @text = s # mnemo needs this for setting description
      mnemonic s[ix,1]
    end
    @text = s
  end
  return self 
end