Class: Empty_Text_Field_Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/Empty_Text_Field_Handler.rb

Overview

Copyright © 2004, 2005 Martin Ankerl Shows a grey text in an FXTextField if the user did not enter any input. This is a nice way to give the user more information about what to enter into a text field, without the need of additional space in the GUI.

Instance Method Summary collapse

Constructor Details

#initialize(textField, myText) ⇒ Empty_Text_Field_Handler

Create a new handler for the specified textfield, with the given text. From now on you have to use the created object to get and set text, not the textfield or this handler would come out of sync



9
10
11
12
13
14
15
16
17
# File 'lib/Empty_Text_Field_Handler.rb', line 9

def initialize(textField, myText)
  @textField = textField
  @myText = myText
  @isEmpty = true
  onTextFieldFocusOut
  # create connections
  @textField.connect(SEL_FOCUSIN, method(:onTextFieldFocusIn))
  @textField.connect(SEL_FOCUSOUT, method(:onTextFieldFocusOut))
end

Instance Method Details

#empty?Boolean

Check if textfield is empty (no user input).

Returns:

  • (Boolean)


20
21
22
# File 'lib/Empty_Text_Field_Handler.rb', line 20

def empty?
  @isEmpty
end

#setFocusObject

Set focus to the textfield.



41
42
43
# File 'lib/Empty_Text_Field_Handler.rb', line 41

def setFocus
  @textField.setFocus
end

#textObject

Get the textfield’s text, if the user has entered something.



32
33
34
35
36
37
38
# File 'lib/Empty_Text_Field_Handler.rb', line 32

def text
  if empty? && !@inside
    ""
  else
    @textField.text
  end
end

#text=(newText) ⇒ Object

Set new text for the textfield



25
26
27
28
29
# File 'lib/Empty_Text_Field_Handler.rb', line 25

def text=(newText)
  onTextFieldFocusIn
  @textField.text = newText.to_s
  onTextFieldFocusOut
end