Class: Canis::Button
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.
Direct Known Subclasses
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
-
.button_layout(buttons, row, startcol = 0, cols = Ncurses.COLS-1, gap = 5) ⇒ Object
temporary method, shoud be a proper class.
Instance Method Summary collapse
-
#action(a) ⇒ Object
set button based on Action.
-
#bind_hotkey ⇒ Object
bind hotkey to form keys.
-
#command(*args, &block) ⇒ Object
command of button (invoked on press, hotkey, space) added args 2008-12-20 19:22.
- #default_button(tf = nil) ⇒ Object
-
#fire ⇒ Object
fires PRESS event of button.
- #getvalue ⇒ Object
-
#getvalue_for_paint ⇒ Object
ensure text has been passed or action.
-
#handle_key(ch) ⇒ Object
Button.
-
#initialize(form, config = {}, &block) ⇒ Button
constructor
A new instance of Button.
- #map_keys ⇒ Object
-
#mnemonic(char = nil) ⇒ Object
set mnemonic for button, this is a hotkey that triggers
fireupon pressing Alt+char. -
#repaint ⇒ Object
FIXME 2014-05-31 since form checks for highlight color and sets repaint on on_enter, we shoul not set it.
-
#selected? ⇒ Boolean
for campatibility with all buttons, will apply to radio buttons mostly.
-
#text(*val) ⇒ Object
button: sets text, checking for ampersand, uses that for hotkey and underlines.
Methods inherited from Widget
#action_manager, #bgcolor, #changed, #click, #color, #color_pair, #destroy, #enter, #focus, #focusable, #focusable?, #hide, #init_vars, #leave, #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
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.
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3082 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
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3314 def self. , row, startcol=0, cols=Ncurses.COLS-1, gap=5 col = startcol .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
3100 3101 3102 3103 3104 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3100 def action a text a.name mnemonic a.mnemonic unless a.mnemonic.nil? command { a.call } end |
#bind_hotkey ⇒ Object
bind hotkey to form keys. added 2008-12-15 20:19 use ampersand in name or underline IS THIS USED ??
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3153 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
3254 3255 3256 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3254 def command *args, &block bind :PRESS, *args, &block end |
#default_button(tf = nil) ⇒ Object
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3169 def tf=nil return unless tf raise ArgumentError, "default button must be true or false" if ![false,true].include? tf unless @form bind(:FORM_ATTACHED){ (tf) } return self end $log.debug "XXX: BUTTON DEFAULT setting to true : #{tf} " = 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 ⇒ Object
fires PRESS event of button
3258 3259 3260 3261 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3258 def fire #$log.debug "firing PRESS #{text}" fire_handler :PRESS, ActionEvent.new(self, :PRESS, text) end |
#getvalue ⇒ Object
3186 3187 3188 3189 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3186 def getvalue #@text_variable.nil? ? @text : @text_variable.get_value(@name) @text end |
#getvalue_for_paint ⇒ Object
ensure text has been passed or action
3192 3193 3194 3195 3196 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3192 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
3275 3276 3277 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3275 def handle_key ch super end |
#map_keys ⇒ Object
3265 3266 3267 3268 3269 3270 3271 3272 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3265 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
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3131 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 ⇒ Object
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
3201 3202 3203 3204 3205 3206 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 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3201 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?( Fixnum) && _color.is_a?( Fixnum) # 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
3263 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3263 def selected?; false; end |
#text(*val) ⇒ Object
button: sets text, checking for ampersand, uses that for hotkey and underlines
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 |
# File 'lib/canis/core/widgets/rwidget.rb', line 3107 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 |