Class: Canis::Field

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

Overview

Text edit field NOTE: width is the length of the display whereas maxlen is the maximum size that the value can take. Thus, maxlen can exceed width. Currently, maxlen defaults to width which defaults to 20. NOTE: Use text(val) to set value, and text() to retrieve value

Example

f = Field.new @form, text: "Some value", row: 10, col: 2

Field introduces an event :CHANGE which is fired for each character deleted or inserted TODO: some methods should return self, so chaining can be done. Not sure if the return value of the

fire_handler is being checked.
NOTE: i have just added repain_required check in Field before repaint
this may mean in some places field does not paint. repaint_require will have to be set
to true in those cases. this was since field was overriding a popup window that was not modal.

Since:

  • 1.2.0

Direct Known Subclasses

LabeledField

Instance Attribute Summary collapse

Attributes inherited from Widget

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

Instance Method Summary collapse

Methods inherited from Widget

#action_manager, #bgcolor, #changed, #click, #color, #color_pair, #command, #destroy, #enter, #focus, #focusable, #focusable?, #getvalue_for_paint, #hide, #leave, #move, #override_graphic, #process_key, #property_set, #remove, #repaint_all, #repaint_required, #rowcol, #set_form, #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 = nil, config = {}, &block) ⇒ Field

# so can be nil if accessed early 2011-12-8

Since:

  • 1.2.0



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

def initialize form=nil, config={}, &block
  @form = form
  @buffer = String.new
  #@type=config.fetch("type", :varchar)
  @row = 0
  @col = 0
  @editable = true
  @focusable = true
  #@event_args = {}             # arguments passed at time of binding, to use when firing event
  map_keys 
  init_vars
  register_events(:CHANGE)
  super
  @width ||= 20
  @maxlen ||= @width
end

Instance Attribute Details

#aboveObject

for numeric fields, specify lower or upper limit of entered value

Since:

  • 1.2.0



2271
2272
2273
# File 'lib/canis/core/widgets/rwidget.rb', line 2271

def above
  @above
end

#belowObject

for numeric fields, specify lower or upper limit of entered value

Since:

  • 1.2.0



2271
2272
2273
# File 'lib/canis/core/widgets/rwidget.rb', line 2271

def below
  @below
end

#bufferObject (readonly)

actual buffer being used for storage

Since:

  • 1.2.0



2264
2265
2266
# File 'lib/canis/core/widgets/rwidget.rb', line 2264

def buffer
  @buffer
end

#datatypeObject

this accesses the field created or passed with set_label attr_reader :label this is the class of the field set in text(), so value is returned in same class

Examples:

: Fixnum, Integer, Float

Since:

  • 1.2.0



2290
2291
2292
# File 'lib/canis/core/widgets/rwidget.rb', line 2290

def datatype
  @datatype
end

#field_colObject (readonly)

column on which field printed, usually the same as col unless label used. Required by History to popup field history.

Since:

  • 1.2.0



2296
2297
2298
# File 'lib/canis/core/widgets/rwidget.rb', line 2296

def field_col
  @field_col
end

#original_valueObject (readonly)

value on entering field

Since:

  • 1.2.0



2291
2292
2293
# File 'lib/canis/core/widgets/rwidget.rb', line 2291

def original_value
  @original_value
end

#overwrite_modeObject

true or false INSERT OVERWRITE MODE

Since:

  • 1.2.0



2292
2293
2294
# File 'lib/canis/core/widgets/rwidget.rb', line 2292

def overwrite_mode
  @overwrite_mode
end

Instance Method Details

#_set_buffer(value) ⇒ Object

set value of Field fires CHANGE handler Please don’t use this directly, use text This name is from ncurses field, added underscore to emphasize not to use

Since:

  • 1.2.0



2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
# File 'lib/canis/core/widgets/rwidget.rb', line 2434

def _set_buffer value   #:nodoc:
  @repaint_required = true
  @datatype = value.class
  @delete_buffer = @buffer.dup
  @buffer = value.to_s.dup
  # don't allow setting of value greater than maxlen
  @buffer = @buffer[0,@maxlen] if @maxlen && @buffer.length > @maxlen
  @curpos = 0
  # hope @delete_buffer is not overwritten
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, @delete_buffer)     # 2010-09-11 13:01 
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :INSERT, 0, @buffer)     # 2010-09-11 13:01 
  self # 2011-10-2 
end

#addcol(num) ⇒ Object

add a column to cursor position. Field

Since:

  • 1.2.0



2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
# File 'lib/canis/core/widgets/rwidget.rb', line 2698

def addcol num
  if num < 0
    if @form.col <= @col + @col_offset
     # $log.debug " error trying to cursor back #{@form.col}"
      return -1
    end
  elsif num > 0
    if @form.col >= @col + @col_offset + @width
  #    $log.debug " error trying to cursor forward #{@form.col}"
      return -1
    end
  end
  @form.addcol num
end

#cursor_backwardObject

$log.debug “ crusor FORWARD cp:#@curpos pcol:#@pcol b.l:#Canis::Field.@[email protected] d_l:#@display_length fc:#Canis::Field.@[email protected]

Since:

  • 1.2.0



2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
# File 'lib/canis/core/widgets/rwidget.rb', line 2658

def cursor_backward
  if @curpos > 0
    @curpos -= 1
    if @pcol > 0 and @form.col == @col + @col_offset
      @pcol -= 1
    end
    addcol -1
  elsif @pcol > 0 #  added 2008-11-26 23:05 
    @pcol -= 1   
  end
 #   $log.debug " crusor back cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
=begin
# this is perfect if not scrolling, but now needs changes
  if @curpos > 0
    @curpos -= 1
    addcol -1
  end
=end
end

#cursor_endObject

goto end of field, “end” is a keyword so could not use it.

Since:

  • 1.2.0



2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
# File 'lib/canis/core/widgets/rwidget.rb', line 2612

def cursor_end
  blen = @buffer.rstrip.length
  if blen < @width
    set_form_col blen
  else
    # there is a problem here FIXME. 
    @pcol = blen-@width
    #set_form_col @width-1
    set_form_col blen
  end
  @curpos = blen # this is position in array where editing or motion is to happen regardless of what you see
                 # regardless of pcol (panning)
  #  $log.debug " crusor END cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@width} fc:#{@form.col}"
  #set_form_col @buffer.length
end

#cursor_forwardObject

Since:

  • 1.2.0



2649
2650
2651
2652
2653
2654
2655
2656
2657
# File 'lib/canis/core/widgets/rwidget.rb', line 2649

def cursor_forward
  if @curpos < @buffer.length 
    if addcol(1)==-1  # go forward if you can, else scroll
      @pcol += 1 if @pcol < @width 
    end
    @curpos += 1
  end
 # $log.debug " crusor FORWARD cp:#{@curpos} pcol:#{@pcol} b.l:#{@buffer.length} d_l:#{@display_length} fc:#{@form.col}"
end

#cursor_homeObject

position cursor at start of field

Since:

  • 1.2.0



2605
2606
2607
2608
2609
# File 'lib/canis/core/widgets/rwidget.rb', line 2605

def cursor_home
  @curpos = 0
  @pcol = 0
  set_form_col 0
end

#delete_at(index = @curpos) ⇒ Object

Since:

  • 1.2.0



2410
2411
2412
2413
2414
2415
2416
2417
# File 'lib/canis/core/widgets/rwidget.rb', line 2410

def delete_at index=@curpos
  return -1 if !@editable 
  char = @buffer.slice!(index,1)
  #$log.debug " delete at #{index}: #{@buffer.length}: #{@buffer}"
  @modified = true
  #fire_handler :CHANGE, self    # 2008-12-09 14:51 
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos, self, :DELETE, 0, char)     # 2010-09-11 13:01 
end

#delete_curr_charObject

# this is perfect if not scrolling, but now needs changes

if @curpos > 0
  @curpos -= 1
  addcol -1
end

Since:

  • 1.2.0



2677
2678
2679
2680
2681
# File 'lib/canis/core/widgets/rwidget.rb', line 2677

def delete_curr_char
  return -1 unless @editable
  delete_at
  set_modified 
end

#delete_eolObject

Since:

  • 1.2.0



2640
2641
2642
2643
2644
2645
2646
2647
2648
# File 'lib/canis/core/widgets/rwidget.rb', line 2640

def delete_eol
  return -1 unless @editable
  pos = @curpos-1
  @delete_buffer = @buffer[@curpos..-1]
  # if pos is 0, pos-1 becomes -1, end of line!
  @buffer = pos == -1 ? "" : @buffer[0..pos]
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :DELETE, 0, @delete_buffer)
  return @delete_buffer
end

#delete_prev_charObject

Since:

  • 1.2.0



2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
# File 'lib/canis/core/widgets/rwidget.rb', line 2682

def delete_prev_char
  return -1 if !@editable 
  return if @curpos <= 0
  # if we've panned, then unpan, and don't move cursor back
  # Otherwise, adjust cursor (move cursor back as we delete)
  adjust = true
  if @pcol > 0
    @pcol -= 1
    adjust = false
  end
  @curpos -= 1 if @curpos > 0
  delete_at
  addcol -1 if adjust # move visual cursor back
  set_modified 
end

#getvalueObject

converts back into original type

changed to convert on 2009-01-06 23:39

Since:

  • 1.2.0



2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
# File 'lib/canis/core/widgets/rwidget.rb', line 2449

def getvalue
  dt = @datatype || String
  case dt.to_s
  when "String"
    return @buffer
  when "Fixnum"
    return @buffer.to_i
  when "Float"
    return @buffer.to_f
  else
    return @buffer.to_s
  end
end

#handle_key(ch) ⇒ Object

field

Since:

  • 1.2.0



2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
# File 'lib/canis/core/widgets/rwidget.rb', line 2579

def handle_key ch
  @repaint_required = true 
  #map_keys unless @keys_mapped # moved to init
  case ch
  when 32..126
    #$log.debug("FIELD: ch #{ch} ,at #{@curpos}, buffer:[#{@buffer}] bl: #{@buffer.to_s.length}")
    putc ch
  when 27 # cannot bind it
    #text @original_value 
    # commented above and changed 2014-05-12 - 20:05 I think above creates positioning issues. TEST XXX
    restore_original_value
  else
    ret = super
    return ret
  end
  0 # 2008-12-16 23:05 without this -1 was going back so no repaint
end

#in_range?(val) ⇒ Boolean

checks field against valid_range, above and below , returning true if it passes set attributes, false if it fails any one.

Returns:

  • (Boolean)

Since:

  • 1.2.0



2746
2747
2748
2749
2750
2751
# File 'lib/canis/core/widgets/rwidget.rb', line 2746

def in_range?( val )
  val = val.to_i
  (@above.nil? or val > @above) and
    (@below.nil? or val < @below) and
    (@valid_range.nil? or @valid_range.include?(val))
end

#init_varsObject

Since:

  • 1.2.0



2316
2317
2318
2319
2320
2321
2322
2323
# File 'lib/canis/core/widgets/rwidget.rb', line 2316

def init_vars
  @pcol = 0                    # needed for horiz scrolling
  @curpos = 0                  # current cursor position in buffer
                               # this is the index where characters are put or deleted
  #                            # when user edits
  @modified = false
  @repaint_required = true
end

#label(*val) ⇒ Object

Since:

  • 1.2.0



2493
2494
2495
2496
# File 'lib/canis/core/widgets/rwidget.rb', line 2493

def label *val
  return @label if val.empty?
  raise "Field does not allow setting of label. Please use LabeledField instead with lcol for label column"
end

#map_keysObject

Since:

  • 1.2.0



2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
# File 'lib/canis/core/widgets/rwidget.rb', line 2561

def map_keys
  return if @keys_mapped
  bind_key(FFI::NCurses::KEY_LEFT, :cursor_backward )
  bind_key(FFI::NCurses::KEY_RIGHT, :cursor_forward )
  bind_key(FFI::NCurses::KEY_BACKSPACE, :delete_prev_char )
  bind_key(127, :delete_prev_char )
  bind_key(330, :delete_curr_char )
  bind_key(?\C-a, :cursor_home )
  bind_key(?\C-e, :cursor_end )
  bind_key(?\C-k, :delete_eol )
  bind_key(?\C-_, :undo_delete_eol )
  #bind_key(27){ text @original_value }
  bind_key(?\C-g, 'revert'){ text @original_value } # 2011-09-29 V1.3.1 ESC did not work
  @keys_mapped = true
end

#modified?Boolean

overriding widget, check for value change

2009-01-18 12:25

Returns:

  • (Boolean)

Since:

  • 1.2.0



2764
2765
2766
# File 'lib/canis/core/widgets/rwidget.rb', line 2764

def modified?
  getvalue() != @original_value
end

#on_enterObject

save original value on enter, so we can check for modified.

2009-01-18 12:25 
 2011-10-9 I have changed to take @buffer since getvalue returns a datatype
 and this causes a crash in set_original on cursor forward.

Since:

  • 1.2.0



2756
2757
2758
2759
2760
# File 'lib/canis/core/widgets/rwidget.rb', line 2756

def on_enter
  #@original_value = getvalue.dup rescue getvalue
  @original_value = @buffer.dup # getvalue.dup rescue getvalue
  super
end

#on_leaveObject

upon leaving a field returns false if value not valid as per values or valid_regex 2008-12-22 12:40 if null_allowed, don’t validate, but do fire_handlers

Since:

  • 1.2.0



2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
# File 'lib/canis/core/widgets/rwidget.rb', line 2715

def on_leave
  val = getvalue
  #$log.debug " FIELD ON LEAVE:#{val}. #{@values.inspect}"
  valid = true
  if val.to_s.empty? && @null_allowed
    #$log.debug " empty and null allowed"
  else
    if !@values.nil?
      valid = @values.include? val
      raise FieldValidationException, "Field value (#{val}) not in values: #{@values.join(',')}" unless valid
    end
    if !@valid_regex.nil?
      valid = @valid_regex.match(val.to_s)
      raise FieldValidationException, "Field not matching regex #{@valid_regex}" unless valid
    end
    # added valid_range for numerics 2011-09-29 
    if !in_range?(val)
      raise FieldValidationException, "Field not matching range #{@valid_range}, above #{@above} or below #{@below}  "
    end
  end
  # here is where we should set the forms modified to true - 2009-01
  if modified?
    set_modified true
  end
  # if super fails we would have still set modified to true
  super
  #return valid
end

#position_labelObject

FIXME this may not work since i have disabled -1, now i do not set row and col

Since:

  • 1.2.0



2498
2499
2500
2501
2502
2503
2504
# File 'lib/canis/core/widgets/rwidget.rb', line 2498

def position_label
  $log.debug "XXX: LABEL row #{@label.row}, #{@label.col} "
  @label.row  @row unless @label.row #if @label.row == -1
  @label.col  @col-(@label.name.length+1) unless @label.col #if @label.col == -1
  @label.label_for(self) # this line got deleted when we redid stuff !
  $log.debug "   XXX: LABEL row #{@label.row}, #{@label.col} "
end

#putc(c) ⇒ Object

TODO : sending c>=0 allows control chars to go. Should be >= ?A i think.

Since:

  • 1.2.0



2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
# File 'lib/canis/core/widgets/rwidget.rb', line 2394

def putc c
  if c >= 0 and c <= 127
    ret = putch c.chr
    if ret == 0
      if addcol(1) == -1  # if can't go forward, try scrolling
        # scroll if exceeding display len but less than max len
        if @curpos > @width && @curpos <= @maxlen
          @pcol += 1 if @pcol < @width 
        end
      end
      set_modified 
      return 0 # 2010-09-11 12:59 else would always return -1
    end
  end
  return -1
end

#putch(char) ⇒ Fixnum

add a char to field, and validate NOTE: this should return self for chaining operations and throw an exception if disabled or exceeding size

Parameters:

  • a (char)

    character to add

Returns:

  • (Fixnum)

    0 if okay, -1 if not editable or exceeding length

Since:

  • 1.2.0



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
2390
# File 'lib/canis/core/widgets/rwidget.rb', line 2363

def putch char
  return -1 if !@editable 
  return -1 if !@overwrite_mode && (@buffer.length >= @maxlen)
  blen = @buffer.length
  if @chars_allowed != nil
    return if char.match(@chars_allowed).nil?
  end
  # added insert or overwrite mode 2010-03-17 20:11 
  oldchar = nil
  if @overwrite_mode
    oldchar = @buffer[@curpos] 
    @buffer[@curpos] = char
  else
    @buffer.insert(@curpos, char)
  end
  oldcurpos = @curpos
  #$log.warn "XXX:  FIELD CURPOS #{@curpos} blen #{@buffer.length} " #if @curpos > blen
  @curpos += 1 if @curpos < @maxlen
  @modified = true
  #$log.debug " FIELD FIRING CHANGE: #{char} at new #{@curpos}: bl:#{@buffer.length} buff:[#{@buffer}]"
  # i have no way of knowing what change happened and what char was added deleted or changed
  #fire_handler :CHANGE, self    # 2008-12-09 14:51 
  if @overwrite_mode
    fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :DELETE, 0, oldchar) # 2010-09-11 12:43 
  end
  fire_handler :CHANGE, InputDataEvent.new(oldcurpos,@curpos, self, :INSERT, 0, char) # 2010-09-11 12:43 
  0
end

#repaintObject

Note that some older widgets like Field repaint every time the form.repaint + is called, whether updated or not. I can’t remember why this is, but + currently I’ve not implemented events with these widgets. 2010-01-03 15:00

Since:

  • 1.2.0



2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
# File 'lib/canis/core/widgets/rwidget.rb', line 2510

def repaint
  return unless @repaint_required  # 2010-11-20 13:13 its writing over a window i think TESTING
  if @label_unattached
    alert "came here unattachd"
    @label.set_form(@form)
    @label_unattached = nil
  end
  if @label_unplaced
    alert "came here unplaced"
    position_label
    @label_unplaced = nil
  end
  #@bgcolor ||= $def_bg_color
  #@color   ||= $def_fg_color
  _color = color()
  _bgcolor = bgcolor()
  $log.debug("repaint FIELD: #{id}, #{name}, #{row} #{col},pcol:#{@pcol},  #{focusable} st: #{@state} ")
  @width = 1 if width == 0
  printval = getvalue_for_paint().to_s # added 2009-01-06 23:27 
  printval = mask()*printval.length unless @mask.nil?
  if !printval.nil? 
    if printval.length > width # only show maxlen
      printval = printval[@pcol..@pcol+width-1] 
    else
      printval = printval[@pcol..-1]
    end
  end

  acolor = @color_pair || get_color($datacolor, _color, _bgcolor)
  if @state == :HIGHLIGHTED
    _bgcolor = @highlight_bgcolor || _bgcolor
    _color = @highlight_color || _color
    acolor = get_color(acolor, _color, _bgcolor)
  end
  @graphic = @form.window if @graphic.nil? ## cell editor listbox hack 
  #$log.debug " Field g:#{@graphic}. r,c,displen:#{@row}, #{@col}, #{@width} c:#{@color} bg:#{@bgcolor} a:#{@attr} :#{@name} "
  r = row
  c = col
  @graphic.printstring r, c, sprintf("%-*s", width, printval), acolor, attr()
  @field_col = c
  @repaint_required = false
end

#restore_original_valueObject

silently restores value without firing handlers, use if exception and you want old value

Since:

  • 1.4.0 2011-10-2



2421
2422
2423
2424
2425
2426
2427
2428
# File 'lib/canis/core/widgets/rwidget.rb', line 2421

def restore_original_value
  @buffer = @original_value.dup
  # earlier commented but trying again, since i am getting IndexError in insert 2188
  # Added next 3 lines to fix issue, now it comes back to beginning.
  cursor_home

  @repaint_required = true
end

#set_focusable(tf) ⇒ Object

deprecated set or unset focusable

Since:

  • 1.2.0



2555
2556
2557
2558
# File 'lib/canis/core/widgets/rwidget.rb', line 2555

def set_focusable(tf)
  $log.warn "pls don't use, deprecated. use focusable(boolean)"
  focusable tf
end

#set_form_col(col1 = @curpos) ⇒ Object

sets the visual cursor on the window at correct place added here since we need to account for pcol. 2011-12-7 NOTE be careful of curpos - pcol being less than 0

Since:

  • 1.2.0



2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
# File 'lib/canis/core/widgets/rwidget.rb', line 2630

def set_form_col col1=@curpos
  @curpos = col1 || 0 # NOTE we set the index of cursor here
  c = @col + @col_offset + @curpos - @pcol
  min = @col + @col_offset
  max = min + @width
  c = min if c < min
  c = max if c > max
  #$log.debug " #{@name} FIELD set_form_col #{c}, curpos #{@curpos}  , #{@col} + #{@col_offset} pcol:#{@pcol} "
  setrowcol nil, c
end

#set_label(label) ⇒ Object

create a label linked to this field Typically one passes a Label, but now we can pass just a String, a label is created. This differs from label in positioning. The Field is printed on row and col and the label before it. Thus, all fields are aligned on column, however you must leave adequate columns for the label to be printed to the left of the field.

NOTE: 2011-10-20 when field attached to some container, label won’t be attached In such cases, use just label() not set_label(). FIXME this may not work since i have disabled -1, now i do not set row and col 2011-11-5

Parameters:

  • label (Label, String)

    object to be associated with this field

Returns:

  • label created which can be further customized.

Since:

  • 1.2.0



2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
# File 'lib/canis/core/widgets/rwidget.rb', line 2474

def set_label label
  # added case for user just using a string
  case label
  when String
    # what if no form at this point
    @label_unattached = true unless @form
    label = Label.new @form, {:text => label}
  end
  @label = label
  # in the case of app it won't be set yet FIXME
  # So app sets label to 0 and t his won't trigger
  # can this be delayed to when paint happens XXX
  if @row
    position_label
  else
    @label_unplaced = true
  end
  label
end

#text(*val) ⇒ Object Also known as: default

Set the value in the field.

Parameters:

  • if

    none given, returns value existing

  • value (can be int, float, String)

Returns:

  • self

Since:

  • 1.2.0



2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
# File 'lib/canis/core/widgets/rwidget.rb', line 2773

def text(*val)
  if val.empty?
    return getvalue()
  else
    return unless val # added 2010-11-17 20:11, dup will fail on nil
    return unless val[0]
    # 2013-04-20 - 19:02 dup failing on fixnum, set_buffer does a dup
    # so maybe i can do without it here
    #s = val[0].dup
    s = val[0]
    _set_buffer(s)
  end
end

#text=(val) ⇒ Object

Since:

  • 1.2.0



2787
2788
2789
2790
2791
2792
# File 'lib/canis/core/widgets/rwidget.rb', line 2787

def text=(val)
  return unless val # added 2010-11-17 20:11, dup will fail on nil
  # will bomb on integer or float etc !!
  #_set_buffer(val.dup)
  _set_buffer(val)
end

#type(*val) ⇒ Object Also known as: chars_allowed

NOTE: earlier there was some confusion over type, chars_allowed and datatype Now type and chars_allowed are merged into one. If you pass a symbol such as :integer, :float or Float Fixnum then some

standard chars_allowed will be used. Otherwise you may pass a regexp.

Parameters:

  • symbol

    :integer, :float, :alpha, :alnum, Float, Fixnum, Numeric, Regexp

Since:

  • 1.2.0



2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
# File 'lib/canis/core/widgets/rwidget.rb', line 2331

def type *val
  return @chars_allowed if val.empty?

  dtype = val[0]
  #return self if @chars_allowed # disallow changing
  if dtype.is_a? Regexp 
    @chars_allowed = dtype
    return self
  end
  dtype = dtype.to_s.downcase.to_sym if dtype.is_a? String
  case dtype # missing to_sym would have always failed due to to_s 2011-09-30 1.3.1
  when :integer, Fixnum, Integer
    @chars_allowed = /\d/
  when :numeric, :float, Numeric, Float
    @chars_allowed = /[\d\.]/ 
  when :alpha
    @chars_allowed = /[a-zA-Z]/ 
  when :alnum
    @chars_allowed = /[a-zA-Z0-9]/ 
  else
    raise ArgumentError, "Field type: invalid datatype specified. Use :integer, :numeric, :float, :alpha, :alnum "
  end
  self
end

#undo_delete_eolObject

does an undo on delete_eol, not a real undo

Since:

  • 1.2.0



2597
2598
2599
2600
2601
2602
# File 'lib/canis/core/widgets/rwidget.rb', line 2597

def undo_delete_eol
  return if @delete_buffer.nil?
  #oldvalue = @buffer
  @buffer.insert @curpos, @delete_buffer 
  fire_handler :CHANGE, InputDataEvent.new(@curpos,@curpos+@delete_buffer.length, self, :INSERT, 0, @delete_buffer)     # 2010-09-11 13:01 
end