Class: RubyCurses::Button

Inherits:
Widget
  • Object
show all
Defined in:
lib/rbcurse/rwidget.rb

Overview

action buttons TODO: phasing out underline, and giving mnemonic and ampersand preference

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

Direct Known Subclasses

ToggleButton

Constant Summary

Constants included from Io

Io::ERROR_COLOR_PAIR, Io::FOOTER_COLOR_PAIR, Io::LINEONE, Io::MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary

Attributes inherited from Widget

#col_offset, #cols_panned, #config, #curpos, #ext_col_offset, #ext_row_offset, #form, #id, #parent_component, #row_offset, #rows_panned, #should_create_buffer, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#OLDbind_key, #buffer_to_screen, #buffer_to_window, #create_buffer, #destroy, #destroy_buffer, #focus, #get_buffer, #get_color, #get_preferred_size, #height, #height=, #hide, #init_vars, #is_double_buffered?, #modified?, #move, #on_enter, #on_leave, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #safe_create_buffer, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr

Methods included from Utils

#_process_key, #bind_key, #clean_string!, #get_color, #keycode_tos, #repeatm, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Methods included from DSL

#OLD_method_missing

Constructor Details

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

Returns a new instance of Button.



2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
# File 'lib/rbcurse/rwidget.rb', line 2224

def initialize form, config={}, &block
  @focusable = true
  @editable = false
  @handler={} # event handler
  @event_args ||= {}
  super
  @bgcolor ||= $datacolor 
  @color ||= $datacolor 
  @surround_chars ||= ['[ ', ' ]'] 
  @col_offset = @surround_chars[0].length 
end

Class Method Details

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

temporary method, shoud be a proper class



2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
# File 'lib/rbcurse/rwidget.rb', line 2373

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

2009-01-21 19:59


2238
2239
2240
2241
2242
# File 'lib/rbcurse/rwidget.rb', line 2238

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



2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
# File 'lib/rbcurse/rwidget.rb', line 2285

def bind_hotkey
  return if @underline.nil? or @form.nil?
  _value = @text || getvalue # hack for Togglebutton FIXME
  #_value = getvalue
  $log.debug " bind hot #{_value} #{@underline}"
  ch = _value[@underline,1].downcase()[0].ord ## XXX 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, self) { |_form, _butt| _butt.fire }
end

#command(*args, &block) ⇒ Object

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



2346
2347
2348
2349
# File 'lib/rbcurse/rwidget.rb', line 2346

def command *args, &block
  bind :PRESS, *args, &block
  $log.debug "#{text} bound PRESS"
end

#fireObject

fires PRESS event of button



2351
2352
2353
2354
# File 'lib/rbcurse/rwidget.rb', line 2351

def fire
  $log.debug "firing PRESS #{text}"
  fire_handler :PRESS, @form
end

#getvalueObject

end



2303
2304
2305
# File 'lib/rbcurse/rwidget.rb', line 2303

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

#getvalue_for_paintObject

ensure text has been passed or action



2308
2309
2310
2311
2312
# File 'lib/rbcurse/rwidget.rb', line 2308

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



2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
# File 'lib/rbcurse/rwidget.rb', line 2356

def handle_key ch
  case ch
  when KEY_LEFT, KEY_UP
    return :UNHANDLED
    #  @form.select_prev_field
  when KEY_RIGHT, KEY_DOWN
    return :UNHANDLED
    #  @form.select_next_field
  when KEY_ENTER, 10, 13, 32  # added space bar also
    if respond_to? :fire
      fire
    end
  else
    return :UNHANDLED
  end
end

#mnemonic(char) ⇒ Object

FIXME this will not work in messageboxes since no form available if already set mnemonic, then unbind_key, ??



2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
# File 'lib/rbcurse/rwidget.rb', line 2263

def mnemonic char
  $log.error " #{self} COULD NOT SET MNEMONIC since form NIL" if @form.nil?
  return if @form.nil?
  @mnemonic = char
  ch = char.downcase()[0].ord ## XXX 1.9 
  # meta key 
  mch = ?\M-a.getbyte(0) + (ch - ?a.getbyte(0))
  $log.debug " #{self} setting MNEMO to #{char} #{mch}"
  @form.bind_key(mch, self) { |_form, _butt| _butt.fire }
end

#OLDunderline(ix) ⇒ Object

Deprecated.

. use mnemonic or an ampersand in text.

which index to use as underline. Instead of using this to make a hotkey, I am thinking of giving this a new usage. If you wish to override the underline?



2278
2279
2280
2281
2282
# File 'lib/rbcurse/rwidget.rb', line 2278

def OLDunderline ix
  _value = @text || getvalue # hack for Togglebutton FIXME
  raise "#{self}: underline requires text to be set " if _value.nil?
  mnemonic _value[ix]
end

#repaintObject

button



2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/rbcurse/rwidget.rb', line 2313

def repaint  # button
    #$log.debug("BUTTon repaint : #{self}  r:#{@row} c:#{@col} #{getvalue_for_paint}" )
    r,c = @row, @col #rowcol include offset for putting cursor
    @highlight_foreground ||= $reversecolor
    @highlight_background ||= 0
    bgcolor = @state==:HIGHLIGHTED ? @highlight_background : @bgcolor
    color = @state==:HIGHLIGHTED ? @highlight_foreground : @color
    if bgcolor.is_a? String and color.is_a? String
      color = ColorMap.get_color(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}")
    len = @display_length || value.length
    @graphic = @form.window if @graphic.nil? ## cell editor listbox hack XXX fix in correct place
    @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.nil? or !@mnemonic.nil?
      uline = @underline && (@underline + @text_offset) ||  value.index(@mnemonic) || value.index(@mnemonic.swapcase)
      $log.debug " mvchgat UNDERLI r= #{r} - #{@graphic.top} c #{c} c+x #{c+uline}- #{@graphic.left} #{@graphic} "
      #$log.debug " XXX HACK in next line related to UNDERLINES -graphic.top"
      y=r #[email protected]
      x=c+uline #[email protected]
      if @graphic.window_type == :PAD
        x -= @graphic.left
        y -= @graphic.top
      end
      raise "button underline location error #{x} , #{y} " if x < 0 or c < 0
      @graphic.mvchgat(y, x, max=1, Ncurses::A_BOLD|Ncurses::A_UNDERLINE, color, nil)
    end
end

#text(*val) ⇒ Object

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



2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
# File 'lib/rbcurse/rwidget.rb', line 2245

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
      mnemonic s[ix,1]
    end
    @text = s
  end
end