Class: RubyCurses::Button

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

Overview

action 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. 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

Link, 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

#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #parent_component, #row_offset, #rows_panned, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Widget

#changed, #click, #destroy, #enter, #event_list, #focus, #get_preferred_size, #height, #height=, #hide, #init_vars, #leave, #modified?, #move, #on_enter, #on_leave, #override_graphic, #printstring, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #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, #warn

Methods included from Utils

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

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

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

Returns a new instance of Button.



2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
# File 'lib/rbcurse/rwidget.rb', line 2276

def initialize form, config={}, &block
  require 'rbcurse/ractionevent'
  @focusable = true
  @editable = false
  @handler={} # event handler
  @event_args ||= {}
  @_events ||= []
  @_events.push :PRESS
  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



2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
# File 'lib/rbcurse/rwidget.rb', line 2435

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


2293
2294
2295
2296
2297
# File 'lib/rbcurse/rwidget.rb', line 2293

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



2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/rbcurse/rwidget.rb', line 2333

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 ##  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



2393
2394
2395
2396
# File 'lib/rbcurse/rwidget.rb', line 2393

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

#fireObject

fires PRESS event of button



2398
2399
2400
2401
2402
2403
# File 'lib/rbcurse/rwidget.rb', line 2398

def fire
  $log.debug "firing PRESS #{text}"
  # why the .... am i passing form ? Pass a ActionEvent with source, text() and getvalue()
  #fire_handler :PRESS, @form  changed on 2010-09-12 19:22 
  fire_handler :PRESS, ActionEvent.new(self, :PRESS, text)
end

#getvalueObject



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

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

#getvalue_for_paintObject

ensure text has been passed or action



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

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



2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
# File 'lib/rbcurse/rwidget.rb', line 2406

def handle_key ch
  case ch
  when FFI::NCurses::KEY_LEFT, FFI::NCurses::KEY_UP
    return :UNHANDLED
    #  @form.select_prev_field
  when FFI::NCurses::KEY_RIGHT, FFI::NCurses::KEY_DOWN
    return :UNHANDLED
    #  @form.select_next_field
  when FFI::NCurses::KEY_ENTER, 10, 13, 32  # added space bar also
    if respond_to? :fire
      fire
    end
  else
    if $key_map == :vim
      case ch
      when ?j.getbyte(0)
        @form.window.ungetch(KEY_DOWN)
        return 0
      when ?k.getbyte(0)
        @form.window.ungetch(KEY_UP)
        return 0
      end

    end
    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, ??



2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
# File 'lib/rbcurse/rwidget.rb', line 2319

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 ##  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

#repaintObject

button



2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
# File 'lib/rbcurse/rwidget.rb', line 2355

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
    # NOTE: please override both (if using a string), or else it won't work 
    @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 
    @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)
      #$log.debug " mvchgat UNDERLI r= #{r} - #{@graphic.top} c #{c} c+x #{c+uline}- #{@graphic.left} #{@graphic} "
      #$log.debug "  HACK in next line related to UNDERLINES -graphic.top"
      # if the char is not found don't print it
      if uline
        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
end

#text(*val) ⇒ Object

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



2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
# File 'lib/rbcurse/rwidget.rb', line 2300

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