Class: Canis::Button

Inherits:
Widget show all
Defined in:
lib/canis/core/widgets/rwidget.rb

Overview

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.

Since:

  • 1.2.0

Direct Known Subclasses

Link, ToggleButton

Instance Attribute Summary

Attributes inherited from Widget

#_object_created, #col_offset, #config, #curpos, #focussed, #form, #handler, #id, #key_label, #parent_component, #row_offset, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#action_manager, #bgcolor, #color, #color_pair, #destroy, #focus, #focusable, #focusable?, #hide, #init_vars, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #rowcol, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #unbind_key

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rb_getstr, #warn

Methods included from Utils

#ORIG_process_key, #ORIGbind_key, #ORIGkeycode_tos, #_process_key, #bind_composite_mapping, #bind_key, #bind_keys, #check_composite_mapping, #create_logger, #define_key, #define_prefix_command, #execute_mapping, #get_attrib, #get_color, #key, #key_tos, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #xxxbind_composite_mapping

Methods included from ConfigSetup

#config_setup, #variable_set

Methods included from EventHandler

#bind, #event?, #event_list, #fire_handler, #fire_property_change, #register_events

Constructor Details

#initialize(form, config = {}, &block) ⇒ Button

Returns a new instance of Button.

Since:

  • 1.2.0



3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
# 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

Class Method Details

.button_layout(buttons, row, startcol = 0, cols = Ncurses.COLS-1, gap = 5) ⇒ Object

temporary method, shoud be a proper class

Since:

  • 1.2.0



3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
# 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

Instance Method Details

#action(a) ⇒ Object

set button based on Action

Since:

  • 1.2.0



3106
3107
3108
3109
3110
# 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_hotkeyObject

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

Since:

  • 1.2.0



3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
# 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) ⇒ Object

command of button (invoked on press, hotkey, space) added args 2008-12-20 19:22

Since:

  • 1.2.0



3260
3261
3262
# File 'lib/canis/core/widgets/rwidget.rb', line 3260

def command *args, &block
  bind :PRESS, *args, &block
end

#default_button(tf = nil) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 1.2.0



3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
# 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

#fireObject

fires PRESS event of button

Since:

  • 1.2.0



3264
3265
3266
3267
# 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

#getvalueObject

Since:

  • 1.2.0



3192
3193
3194
3195
# File 'lib/canis/core/widgets/rwidget.rb', line 3192

def getvalue
  #@text_variable.nil? ? @text : @text_variable.get_value(@name)
  @text
end

#getvalue_for_paintObject

ensure text has been passed or action

Since:

  • 1.2.0



3198
3199
3200
3201
3202
# 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) ⇒ Object

Button

Since:

  • 1.2.0



3281
3282
3283
# File 'lib/canis/core/widgets/rwidget.rb', line 3281

def handle_key ch
  super
end

#map_keysObject

Since:

  • 1.2.0



3271
3272
3273
3274
3275
3276
3277
3278
# 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) ⇒ Object

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

Since:

  • 1.2.0



3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
# 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

#repaintObject

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

Since:

  • 1.2.0



3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
# 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 #[email protected]
        x=c+uline #[email protected]
        #
        # 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?Boolean

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

Returns:

  • (Boolean)

Since:

  • 1.2.0



3269
# File 'lib/canis/core/widgets/rwidget.rb', line 3269

def selected?; false; end

#text(*val) ⇒ Object

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

Since:

  • 1.2.0



3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
# 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