Class: Rubygoo::TextField

Inherits:
Widget
  • Object
show all
Defined in:
lib/rubygoo/text_field.rb

Constant Summary collapse

SPACE =
" "

Constants inherited from Widget

Widget::DEFAULT_PARAMS

Instance Attribute Summary collapse

Attributes inherited from Widget

#app, #container, #enabled, #focus_priority, #focussed, #goo_id, #h, #mouse_over, #opts, #padding_left, #padding_top, #parent, #relative, #visible, #w, #x, #y

Instance Method Summary collapse

Methods inherited from Widget

#_draw, #_focus, #_key_pressed, #_key_released, #_mouse_down, #_mouse_drag, #_mouse_dragging, #_mouse_motion, #_mouse_up, #_unfocus, #_update, #contains?, #disable, #enable, #enabled?, #focus, #focussed?, #get, #get_color, goo_prop, #hide, inherited, #key_released, #modal?, #mouse_enter, #mouse_exit, #mouse_motion, #mouse_over?, #removed, #show, #tab_to?, #theme_property, #unfocus, #update, #update_rect, #visible?

Methods included from Inflector

#camelize, #classify, #constantize, #dasherize, #demodulize, #foreign_key, #humanize, #inflections, #ordinalize, #pluralize, #singularize, #tableize, #titleize, #underscore

Constructor Details

#initialize(text, opts = {}) ⇒ TextField

Returns a new instance of TextField.



6
7
8
9
10
11
12
13
14
15
# File 'lib/rubygoo/text_field.rb', line 6

def initialize(text, opts={})
  super opts
  @text = text

  @max_length = opts[:max_length]
  @min_length = opts[:min_length]
  @min_length ||= 1

  @key_handlers = {}
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/rubygoo/text_field.rb', line 5

def text
  @text
end

Instance Method Details

#addedObject



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
# File 'lib/rubygoo/text_field.rb', line 31

def added()
  font = theme_property :font
  @font_size = theme_property :font_size
  @color = theme_property :color
  @fg_color = theme_property :fg_color
  @bg_color = theme_property :bg_color
  @bg_select_color = theme_property :bg_select_color
  @fg_select_color = theme_property :fg_select_color
  @focus_color = theme_property :focus_color
  @border_color = theme_property :border_color

  @font_file = File.join(@app.theme_dir,font)

  # we have the default from widget, we want to set the
  # default to 6 chars wide and 1 char tall
  # wow, this is REALLY bad, but I think M is the widest char
  size = @app.renderer.size_text "M"*@min_length, @font_file, @font_size
  @min_w = size[0]
  @min_h = size[1]

  if @max_length
  size = @app.renderer.size_text "M"*@max_length, @font_file, @font_size
    @max_w = size[0]
    @max_h = size[1]
  end

  set_text @text
end

#delete_selectedObject



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rubygoo/text_field.rb', line 208

def delete_selected()
  return if @select_pos == @caret_pos
  if @caret_pos > @select_pos
    @caret_pos, @select_pos = @select_pos, @caret_pos
  end

  @text = @text.slice(0, @caret_pos) + 
    @text.slice(@select_pos,@text.length-@select_pos)
  render
  @select_pos = @caret_pos
end

#draw(adapter) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubygoo/text_field.rb', line 93

def draw(adapter)
  x1 = @rect[0] - 2
  y1 = @rect[1] - 2
  x2 = @rect[2] + x1 + 4
  y2 = @rect[3] + y1 + 4

  adapter.fill x1, y1, x2, y2, @fg_color
  adapter.fill x1-2, y1-2, x2+2, y2+2, @bg_color
  if @border_color
    adapter.draw_box x1, y1, x2, y2, @border_color
  end
  defaultY = @app.renderer.size_text(@text.slice(0,1),@font_file,@font_size)[1]

  if @focussed
    caretX = @app.renderer.size_text(@text.slice(0,@caret_pos),@font_file,@font_size)[0]
    unless @select_pos.nil?
      # draw selection highlight
      selectX = @app.renderer.size_text(@text.slice(0,@select_pos),@font_file,@font_size)[0]
      selectX0 = [caretX, selectX].min
      selectX1 = [caretX, selectX].max
      if selectX0 < selectX1
        # TODO cache this height
        x = x1+1+selectX0
        y = y1+1
        adapter.fill x, y, x+selectX1-selectX0, y+defaultY, @bg_select_color
      end
    end
  end

  unless @text.nil? or @text.empty?
    @rendered_text = @app.renderer.render_text @text, @font_file, @font_size, @color
    adapter.draw_image @rendered_text, x1+1, y1+1, @color
  end

  # draw caret        
  if @focussed
    x = x1+1+caretX
    y = y1+1
    adapter.fill x, y, x+1, y+defaultY, @fg_select_color
  end

  # don't really need this??
  return @rect
end

#find_mouse_position(pos) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubygoo/text_field.rb', line 148

def find_mouse_position(pos)
  # put hit position in window relative coords
  x = pos[0] - @rect[0]
  y = pos[1] - @rect[1]

  # find the horizontal position within the text by binary search
  l,r = 0, @text.length
  c = 0
  while l < r
    c = (l + r + 1) / 2
    w = @app.renderer.size_text(@text.slice(l,c-l), @font_file, @font_size)[0]
    if x >= w
      l = c
      x -= w
    else
      if r == c
        if x > w / 2
          l = l + 1
        end
        break
      end

      r = c
    end
  end
  return l                
end

#key_pressed(event) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/rubygoo/text_field.rb', line 220

def key_pressed(event)
  return if not @focussed
  @dragging = false

  mods = event.data[:mods]
  handlers = @key_handlers[event.data[:key]]
  unless handlers.nil?
    for handler in handlers
      handler.call event
    end

    render
    return
  end

  case event.data[:key]
  when K_LEFT
    if @caret_pos > 0
      @caret_pos -= 1
    end
    if mods.include? K_LCTRL or mods.include? K_RCTRL
      while @caret_pos > 0 and @text.slice(@caret_pos - 1,1) == SPACE
        @caret_pos -= 1
      end
      while @caret_pos > 0 and not @text.slice(@caret_pos - 1,1) == SPACE
        @caret_pos -= 1
      end
    end

    unless mods.include? K_LSHIFT or mods.include? K_RSHIFT
      @select_pos = @caret_pos
    end

  when K_RIGHT
    if @caret_pos < @text.length
      @caret_pos += 1
    end
    if mods.include? K_LCTRL or mods.include? K_RCTRL
      while @caret_pos < @text.length and not @text.slice(@caret_pos,1) == SPACE
        @caret_pos += 1
      end
      while @caret_pos < @text.length and @text.slice(@caret_pos,1) == SPACE
        @caret_pos += 1
      end
    end
    unless mods.include? K_LSHIFT or mods.include? K_RSHIFT
      @select_pos = @caret_pos
    end

  when K_HOME
    @caret_pos = 0
    unless mods.include? K_LSHIFT or mods.include? K_RSHIFT
      @select_pos = @caret_pos
    end

  when K_END
    @caret_pos = @text.length
    unless mods.include? K_LSHIFT or mods.include? K_RSHIFT
      @select_pos = @caret_pos
    end

  when K_BACKSPACE
    if @select_pos != @caret_pos
      delete_selected()
    elsif @caret_pos > 0
      @text = @text.slice(0,@caret_pos-1) + @text.slice(@caret_pos,@text.length-@caret_pos)
      @caret_pos -= 1
      @select_pos = @caret_pos
    end

  when K_DELETE
    if @select_pos != @caret_pos
      delete_selected()
    elsif @caret_pos < @text.length
      @text = @text.slice(0,@caret_pos) + @text.slice(@caret_pos+1,@text.length-@caret_pos-1)
      @select_pos = @caret_pos
    end

  when *KEY2ASCII.keys
    # add regular text to the box
    return if @text.size == @max_length
    if @caret_pos == @select_pos
      event_string = event.data[:string]
      unless event_string.nil? or event_string == ""
        @text = @text.slice(0,@caret_pos) + event_string + @text.slice(@caret_pos,@text.length-@caret_pos)
        @caret_pos += 1
      end
    else
      positions = [@caret_pos,@select_pos]
      min = positions.min
      max = positions.max
      @text = @text.slice(0,min) + event.data[:string] + @text.slice(max,@text.length-max)
      @caret_pos = min + 1
    end

    @select_pos = @caret_pos
  end

  render
end

#mouse_down(event) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/rubygoo/text_field.rb', line 176

def mouse_down(event)
  x = event.data[:x]
  y = event.data[:y]

  self.app.focus self

  @caret_pos = find_mouse_position([x,y])
  @select_pos = @caret_pos
  render
end

#mouse_drag(event) ⇒ Object



194
195
196
197
198
199
# File 'lib/rubygoo/text_field.rb', line 194

def mouse_drag(event)
  x = event.data[:x]
  y = event.data[:y]
  @caret_pos = find_mouse_position([x,y])
  render
end

#mouse_dragging(event) ⇒ Object



187
188
189
190
191
192
# File 'lib/rubygoo/text_field.rb', line 187

def mouse_dragging(event)
  x = event.data[:x]
  y = event.data[:y]
  @caret_pos = find_mouse_position([x,y])
  render
end

#mouse_up(event) ⇒ Object



201
202
203
204
205
206
# File 'lib/rubygoo/text_field.rb', line 201

def mouse_up(event)
  x = event.data[:x]
  y = event.data[:y]
  @caret_pos = find_mouse_position([x,y])
  render
end

#on_focusObject



143
144
145
146
# File 'lib/rubygoo/text_field.rb', line 143

def on_focus()
  @select_pos = @text.length
  @caret_pos = @text.length
end

#on_key(*keys, &block) ⇒ Object

Allows for registering callbacks for various key presses. note: this will override default behavior such as K_RIGHT and such Example: text_field.on_key K_RETURN, K_KP_ENTER do |evt|

puts "BOO-YAH"

end



24
25
26
27
28
29
# File 'lib/rubygoo/text_field.rb', line 24

def on_key(*keys, &block)
  for key in keys
    @key_handlers[key] ||= []
    @key_handlers[key] << block
  end
end

#on_unfocusObject



138
139
140
141
# File 'lib/rubygoo/text_field.rb', line 138

def on_unfocus()
  @caret_pos = 0
  @select_pos = 0
end

#renderObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubygoo/text_field.rb', line 60

def render()
  if @text.empty?
    w = [@w,@min_w].max + @padding_left
    h = [@h,@min_h].max + @padding_top
    @rendered_text = nil
    @rect = Rect.new [@x,@y,w,h]
  else
    @rendered_text = @app.renderer.render_text @text, @font_file, @font_size, @color
    w = [@rendered_text.width,@min_w].max + @padding_left
    h = [@rendered_text.height,@min_h].max + @padding_top
    if @max_length
      w = [w,@max_w].min
      h = [h,@max_h].min
    end
    @rect = Rect.new [@x,@y,w,h]
  end
end

#set_text(text) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubygoo/text_field.rb', line 78

def set_text(text)
  if text.nil?
    @text = ""
    @caret_pos = 0
    @select_pos = 0
  else
    @text = text
    @caret_pos = text.length
    @select_pos = @caret_pos
  end

  render

end