Class: WindowBlessing::Widgets::TextField

Inherits:
WindowBlessing::Window show all
Defined in:
lib/window_blessing/widgets/text_field.rb

Constant Summary collapse

NON_NEGATIVE_INTEGER_VALIDATOR =
/^[0-9]*$/

Instance Attribute Summary collapse

Attributes inherited from WindowBlessing::Window

#name

Attributes included from WindowBlessing::Window::Drawing

#buffer, #redraw_areas

Attributes included from WindowBlessing::Window::ParentsAndChildren

#children, #parent

Attributes included from WindowBlessing::Window::Geometry

#area

Attributes included from WindowBlessing::Window::KeyboardFocus

#focused, #focused_child

Instance Method Summary collapse

Methods inherited from WindowBlessing::Window

attr_accessor_with_redraw, #inspect, #route_pointer_event

Methods included from WindowBlessing::Window::Drawing

#add_redraw_area, #clear_redraw_areas, #draw, #draw_internal, #redraw, #redraw_requested?, #request_redraw, #request_redraw_internal

Methods included from WindowBlessing::Window::ParentsAndChildren

#add_child, #each_child, #each_child_with_index, #parent_path, #path, #remove_child

Methods included from WindowBlessing::Window::Geometry

#internal_area, #loc, #loc=, #move_onscreen, #pointer_inside?, #size, #size=

Methods included from WindowBlessing::Window::KeyboardFocus

#blur, #blurred?, #focus, #focused?, #route_keyboard_event

Methods included from Evented

#event_manager, #handle_event, #on

Methods included from Tools

#buffer, #clone_value, #color, #fill_line, #gen_array2d, #gray_screen_color, #log, #overlapping_span, #overlay2d, #overlay_span, #range_length, #resize2d, #rgb_screen_color, #subarray2d, #window

Constructor Details

#initialize(rect, evented_value, options = {}) ⇒ TextField

Returns a new instance of TextField.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/window_blessing/widgets/text_field.rb', line 9

def initialize(rect, evented_value, options={})
  super rect
  @evented_value = evented_value = case evented_value
  when EventedVariable then evented_value
  when String then EventedVariable.new(evented_value)
  else raise ArgumentError.new "invalid text type #{evented_value.inspect}(#{evented_value.class})"
  end

  @validator = options[:validator]

  @fg = options[:fg] || Color.gray
  @bg = options[:bg] || Color.black
  @cursor_bg = options[:cursor_bg] || (@fg + @bg) / 2
  @cursor_pos = text.length
  request_redraw_internal

  on :pointer do |event|
    self.cursor_pos = event[:loc].x
  end

  evented_value.on :refresh do |event|
    request_redraw_internal
  end

  on :string_input do |event|
    p = cursor_pos
    s = event[:string]

    self.text = if p==0
      s + text
    elsif p==text.length
      text + s
    else
      text[0..p] + s + text[p+1..-1]
    end
    self.cursor_pos += s.length
  end

  on :key_press do |event|
    case event[:key]
    when :backspace then
      if cursor_pos > 0
        p = cursor_pos
        before = text
        self.text = if cursor_pos == 1
          text[1..-1]
        elsif cursor_pos == text.length
          self.text = text[0..-2]
        else
          self.text = text[0..p-2] + text[p..-1]
        end
        log " before = #{before.inspect} after = #{text.inspect}"
        self.cursor_pos -= 1
      end

    when :home      then self.cursor_pos = 0
    when :end       then self.cursor_pos = text.length
    when :left      then self.cursor_pos -= 1
    when :right     then self.cursor_pos += 1
    end
  end

  on :focus do
    request_redraw_internal
  end

  on :blur do
    request_redraw_internal
  end
end

Instance Attribute Details

#evented_valueObject

Returns the value of attribute evented_value.



7
8
9
# File 'lib/window_blessing/widgets/text_field.rb', line 7

def evented_value
  @evented_value
end

#validatorObject

Returns the value of attribute validator.



6
7
8
# File 'lib/window_blessing/widgets/text_field.rb', line 6

def validator
  @validator
end

Instance Method Details

#draw_backgroundObject



83
84
85
86
87
88
# File 'lib/window_blessing/widgets/text_field.rb', line 83

def draw_background
  @cursor_pos = bound(0, @cursor_pos, text.length)
  buffer.contents = text
  buffer.fill :fg => fg, :bg => bg
  buffer.fill :area => rect(point(cursor_pos,0),point(1,1)), :bg => cursor_bg if focused?
end

#textObject



80
# File 'lib/window_blessing/widgets/text_field.rb', line 80

def text; evented_value.get end

#text=(t) ⇒ Object



81
# File 'lib/window_blessing/widgets/text_field.rb', line 81

def text=(t); evented_value.set(t) if !validator || t[validator] end