Class: Wx::SF::ContentCtrl

Inherits:
TextCtrl
  • Object
show all
Defined in:
lib/wx/shapes/shapes/edit_text_shape.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, id, parent_shape, content, pos, size, style) ⇒ ContentCtrl

Constructor.

Parameters:

  • parent (Wx::Window)

    Pointer to the parent window

  • id (Integer)

    ID of the text control window

  • parent_shape (Wx::SF::EditTextShape)

    Pointer to the parent editable text shape

  • content (String)

    Initial content of the text control

  • pos (Wx::Point)

    Initial position

  • size (Wx::Size)

    Initial size

  • style (Integer)

    Window style



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wx/shapes/shapes/edit_text_shape.rb', line 21

def initialize(parent, id, parent_shape, content, pos, size, style)
  super(parent, id, content, pos, size, Wx::TE_PROCESS_ENTER | Wx::TE_PROCESS_TAB | Wx::NO_BORDER | style)
  @parent = parent
  @parent_shape = parent_shape
  @prev_content = content

  evt_kill_focus :on_kill_focus
  evt_key_down :on_key_down

  set_insertion_point_end
  if @parent_shape
    # update the font size in accordance to the canvas scale
    font = @parent_shape.get_font.dup
    font.set_point_size((font.get_point_size * @parent_shape.get_parent_canvas.get_scale).to_i)

    set_font(font)
    set_background_colour(Wx::Colour.new(200, 200, 200))
    set_focus
  end
end

Instance Method Details

#on_key_down(event) ⇒ Object (protected)

Event handler called if the key was pressed in the text control.

Parameters:

  • event (Wx::KeyEvent)

    Reference to the event class instance



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wx/shapes/shapes/edit_text_shape.rb', line 78

def on_key_down(event)
  case event.get_key_code
  when Wx::K_ESCAPE
    quit(CANCEL_TEXT_CHANGES)
  when Wx::K_TAB
    quit(APPLY_TEXT_CHANGES)
  when Wx::K_RETURN
    # enter new line if SHIFT key was pressed together with the ENTER key
    if Wx::get_key_state(Wx::K_SHIFT)
      event.skip
    else
      quit(APPLY_TEXT_CHANGES)
    end
  else
    event.skip
  end
end

#on_kill_focus(_event) ⇒ Object (protected)

Parameters:

  • _event (Wx::FocusEvent)

    Reference to the event class instance



72
73
74
# File 'lib/wx/shapes/shapes/edit_text_shape.rb', line 72

def on_kill_focus(_event)
  # noop
end

#quit(apply = APPLY_TEXT_CHANGES) ⇒ Object

Parameters:

  • apply (Boolean) (defaults to: APPLY_TEXT_CHANGES)

    If true then changes made in edited text will be applied on text shape, otherwise it will be canceled



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wx/shapes/shapes/edit_text_shape.rb', line 44

def quit(apply = APPLY_TEXT_CHANGES)
  self.hide

  if @parent_shape
    @parent_shape.send(:set_text_ctrl, nil)
    @parent_shape.set_style(@parent_shape.send(:get_current_state))

    # save canvas state if the textctrl content has changed...
    if apply && @prev_content != get_value
      @parent_shape.set_text(get_value)
      @prev_content = get_value

      # inform parent shape canvas about text change...
      @parent_shape.get_parent_canvas.on_text_change(@parent_shape)
      @parent_shape.get_parent_canvas.save_canvas_state
    end

    @parent_shape.update
    @parent_shape.get_parent_canvas.refresh
  end

  self.destroy
end