Class: Wx::SF::TextShape

Inherits:
RectShape show all
Defined in:
lib/wx/shapes/shapes/text_shape.rb

Direct Known Subclasses

EditTextShape

Defined Under Namespace

Modules: DEFAULT

Instance Method Summary collapse

Methods inherited from RectShape

#create_handles, #do_begin_handle, #fit_to_children, #get_border_point, #get_bounding_box, #get_rect_size, #on_begin_handle, #on_handle, #scale, #set_border, #set_fill, #set_rect_size

Methods inherited from Shape

#accept_child, #accept_connection, #accept_currently_dragged_shapes, #accept_src_neighbour, #accept_trg_neighbour, #activate, #active?, #add_child_shape, #add_connection_point, #add_handle, #add_style, #ancestor?, #clear_accepted_childs, #clear_accepted_connections, #clear_accepted_src_neighbours, #clear_accepted_trg_neighbours, component, component_shapes, #contains?, #contains_style, #create_handles, #descendant?, #do_alignment, #does_not_accept_children?, #draw, #draw_selected, #fit_to_children, #get_absolute_position, #get_accepted_children, #get_accepted_connections, #get_accepted_src_neighbours, #get_accepted_trg_neighbours, #get_assigned_connections, #get_border_point, #get_bounding_box, #get_center, #get_child_shapes, #get_children, #get_children_recursively, #get_complete_bounding_box, #get_connection_point, #get_connection_points, #get_custom_dock_point, #get_diagram, #get_grand_parent_shape, #get_h_align, #get_h_border, #get_handle, #get_handles, #get_hover_colour, #get_nearest_connection_point, #get_neighbours, #get_parent_absolute_position, #get_parent_canvas, #get_parent_shape, #get_relative_position, #get_shape_canvas, #get_style, #get_user_data, #get_v_align, #get_v_border, #has_children, #has_selected_parent?, #include_child_shape?, #inside?, #inspect, #intersects?, #is_child_accepted, #is_connection_accepted, #is_managed, #is_manager, #is_src_neighbour_accepted, #is_trg_neighbour_accepted, lines_intersection, #lines_intersection, #move_by, #move_to, #on_begin_drag, #on_begin_handle, #on_child_dropped, #on_dragging, #on_end_drag, #on_end_handle, #on_handle, #on_import, #on_key, #on_left_click, #on_left_double_click, #on_mouse_enter, #on_mouse_leave, #on_mouse_over, #on_right_click, #on_right_double_click, #refresh, #refresh_rect, #remove_connection_point, #remove_handle, #remove_style, #scale, #scale_children, #select, #selected?, #set_custom_dock_point, #set_diagram, #set_h_align, #set_h_border, #set_hover_colour, #set_parent_shape, #set_relative_position, #set_style, #set_user_data, #set_v_align, #set_v_border, #show, #show_handles, #to_s, #visible?

Constructor Details

#initialize(pos = Shape::DEFAULT::POSITION, txt = DEFAULT::TEXT, diagram: nil) ⇒ TextShape

Constructor.

Parameters:

  • pos (Wx::RealPoint, Wx::Point) (defaults to: Shape::DEFAULT::POSITION)

    Initial position

  • txt (String) (defaults to: DEFAULT::TEXT)

    Text content

  • diagram (Wx::SF::Diagram) (defaults to: nil)

    parent diagram



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wx/shapes/shapes/text_shape.rb', line 33

def initialize(pos = Shape::DEFAULT::POSITION, txt = DEFAULT::TEXT, diagram: nil)
  super(pos, Wx::RealPoint.new, diagram: diagram)
  @font = nil
  @scaled_font = nil

  @line_height = 12

  @text_color = nil
  @text = txt

  @rect_size = Wx::RealPoint.new

  update_rect_size
end

Instance Method Details

#deserialize_finalizeself (protected)

Deserialize attributes and recalculate rectangle size afterwards.

Returns:

  • (self)


357
358
359
360
# File 'lib/wx/shapes/shapes/text_shape.rb', line 357

def deserialize_finalize
  update_rect_size
  self
end

#do_on_handle(handle) ⇒ Object (protected)

Handle’s shape specific actions on handling handle events. The function can be overridden if necessary.

Parameters:

See Also:



216
217
218
219
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
# File 'lib/wx/shapes/shapes/text_shape.rb', line 216

def do_on_handle(handle)
  # HINT: overload it for custom actions...
  prev_size = get_rect_size

  # perform standard operations
  case handle.get_type
  when Shape::Handle::TYPE::LEFT
    on_left_handle(handle)
  when Shape::Handle::TYPE::RIGHT
    on_right_handle(handle)
  when Shape::Handle::TYPE::TOP
    on_top_handle(handle)
  when Shape::Handle::TYPE::BOTTOM
    on_bottom_handle(handle)
  end

  new_size = @rect_size

  sx = new_size.x / prev_size.x
  sy = new_size.y / prev_size.y
  scale(sx, sy)

  case handle.get_type
  when Shape::Handle::TYPE::LEFT
    dx = @rect_size.x - prev_size.x
    move_by(-dx, 0)
    @child_shapes.each { |shape| shape.move_by(-dx, 0) }

  when Shape::Handle::TYPE::TOP
    dy = @rect_size.y - prev_size.y
    move_by(0, -dy)
    @child_shapes.each { |shape| shape.move_by(0, -dy) }
  end
end

#draw_highlighted(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



270
271
272
273
# File 'lib/wx/shapes/shapes/text_shape.rb', line 270

def draw_highlighted(dc)
  super
  draw_text_content(dc)
end

#draw_hover(dc) ⇒ Object (protected)

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



261
262
263
264
# File 'lib/wx/shapes/shapes/text_shape.rb', line 261

def draw_hover(dc)
  super
  draw_text_content(dc)
end

#draw_normal(dc) ⇒ Object (protected)

Draw the shape in the normal way. The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



253
254
255
256
# File 'lib/wx/shapes/shapes/text_shape.rb', line 253

def draw_normal(dc)
  super
  draw_text_content(dc)
end

#draw_shadow(dc) ⇒ Object (protected)

Draw shadow under the shape. The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shadow will be drawn to



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/wx/shapes/shapes/text_shape.rb', line 277

def draw_shadow(dc)
  # HINT: overload it for custom actions...

  curr_color = @text_color
  @text_color = get_parent_canvas.get_shadow_fill.get_colour
  offset = get_parent_canvas.get_shadow_offset

  move_by(offset)
  draw_text_content(dc)
  move_by(-offset.x, -offset.y)

  @text_color = curr_color
end

#draw_text_content(dc) ⇒ Object (protected)

Draw text shape.

Parameters:

  • dc (Wx::DC)

    Device context where the text shape will be drawn to



329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/wx/shapes/shapes/text_shape.rb', line 329

def draw_text_content(dc)
  dc.with_brush(fill) do
    dc.set_background_mode(Wx::BrushStyle::BRUSHSTYLE_TRANSPARENT.to_i)
    dc.set_text_foreground(text_colour)
    dc.with_font(@scaled_font || font) do
      pos = get_absolute_position
      # draw all text lines
      @text.split("\n").each_with_index do |line, i|
        dc.draw_text(line, pos.x.to_i, pos.y.to_i + i*@line_height)
      end
    end
  end
end

#get_borderWx::Pen Also known as: border

Get current border style.

Returns:



102
103
104
# File 'lib/wx/shapes/shapes/text_shape.rb', line 102

def get_border
  @border || (@diagram&.shape_canvas ? @diagram.shape_canvas.text_border : DEFAULT.text_border)
end

#get_fillWx::Brush Also known as: fill

Get current fill style.

Returns:



95
96
97
# File 'lib/wx/shapes/shapes/text_shape.rb', line 95

def get_fill
  @fill || (@diagram&.shape_canvas ? @diagram.shape_canvas.text_fill : DEFAULT.text_fill)
end

#get_fontWx::Font Also known as: font

Get text font.

Returns:



73
74
75
# File 'lib/wx/shapes/shapes/text_shape.rb', line 73

def get_font
  @font || (@diagram&.shape_canvas ? @diagram.shape_canvas.text_font : DEFAULT.font)
end

#get_textString Also known as: text

Get text.

Returns:

  • (String)

    Current text content



88
89
90
# File 'lib/wx/shapes/shapes/text_shape.rb', line 88

def get_text
  @text
end

#get_text_colourWx::Colour Also known as: text_colour

Get text color.

Returns:



116
117
118
# File 'lib/wx/shapes/shapes/text_shape.rb', line 116

def get_text_colour
  @text_color || (@diagram&.shape_canvas ? @diagram.shape_canvas.text_colour : DEFAULT.text_color)
end

#get_text_extentWx::Size

Returns size of current text using current font.

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/wx/shapes/shapes/text_shape.rb', line 129

def get_text_extent
  w = -1
  h = -1
  if get_parent_canvas
    cur_font = @scaled_font || font
    if ShapeCanvas.gc_enabled?
      Wx::GraphicsContext.draw_on(get_parent_canvas) do |gc|
        # calculate text extent
        hd = -1
        e = 0

        gc.set_font(cur_font, Wx::BLACK)

        # we must use split string to inspect all lines of possible multiline text
        h = 0
        @text.split("\n").each do |line|
          wd, hd, d, e = gc.get_text_extent(line)
          h += (hd + e).to_i
          w = (wd + d).to_i if (wd + d) > w
        end
        @line_height = (hd + e).to_i

        gc.set_font(Wx::NULL_FONT, Wx::BLACK)
      end
    else
      get_parent_canvas.paint do |dc|
        dc.set_font(cur_font)
        w, h, @line_height = dc.get_multi_line_text_extent(@text)
        dc.set_font(Wx::NULL_FONT)
      end
    end
  else
    w = @rect_size.x.to_i
    h = @rect_size.y.to_i
    @line_height = (@rect_size.y/@text.split("\n").size).to_i
  end

  Wx::Size.new(w, h)
end

#on_bottom_handle(handle) ⇒ Object (protected)

Parameters:



321
322
323
324
325
# File 'lib/wx/shapes/shapes/text_shape.rb', line 321

def on_bottom_handle(handle)
  # HINT: overload it for custom actions...

  @rect_size.y = (handle.get_position.y - get_absolute_position.y)
end

#on_left_handle(handle) ⇒ Object (protected)

Parameters:



294
295
296
297
298
# File 'lib/wx/shapes/shapes/text_shape.rb', line 294

def on_left_handle(handle)
  # HINT: overload it for custom actions...

  @rect_size.x -= (handle.get_position.x - get_absolute_position.x)
end

#on_right_handle(handle) ⇒ Object (protected)

Parameters:



312
313
314
315
316
# File 'lib/wx/shapes/shapes/text_shape.rb', line 312

def on_right_handle(handle)
  # HINT: overload it for custom actions...

  @rect_size.x = (handle.get_position.x - get_absolute_position.x)
end

#on_top_handle(handle) ⇒ Object (protected)

Parameters:



303
304
305
306
307
# File 'lib/wx/shapes/shapes/text_shape.rb', line 303

def on_top_handle(handle)
  # HINT: overload it for custom actions...

  @rect_size.y -= (handle.get_position.y - get_absolute_position.y)
end

#scale_rectangle(x, y) ⇒ Object (protected)

Scale the rectangle size for this shape.

Parameters:

  • x (Float)

    Horizontal scale factor

  • y (Float)

    Vertical scale factor



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/wx/shapes/shapes/text_shape.rb', line 187

def scale_rectangle(x, y)
  if x == 1.0
    s = y
  elsif y == 1.0
    s = x
  elsif x >= y
    s = x
  else
    s = y
  end

  cur_font = @scaled_font || font

  size = cur_font.get_point_size * s
  size = 5 if size < 5

  if size == font.point_size
    @scaled_font = nil
  else
    @scaled_font ||= font.dup
    @scaled_font.set_point_size(size.to_i) unless size == @scaled_font.get_point_size
  end
  update_rect_size
end

#serialize_text_colour(*val) ⇒ Object (protected)

(de-)serialize text colour; allows for nil values



344
345
346
347
# File 'lib/wx/shapes/shapes/text_shape.rb', line 344

def serialize_text_colour(*val)
  @text_color = val.first unless val.empty?
  @text_color
end

#serialize_text_font(*val) ⇒ Object (protected)

(de-)serialize text colour; allows for nil values



350
351
352
353
# File 'lib/wx/shapes/shapes/text_shape.rb', line 350

def serialize_text_font(*val)
  @font = val.first unless val.empty?
  @font
end

#set_text_font(font) ⇒ Object #set_text_font(font_info) ⇒ Object #set_text_font(pointSize, family, style, weight, underline = false, faceName = (''), encoding = Wx::FontEncoding::FONTENCODING_DEFAULT) ⇒ Object Also known as: font=

Set text font.

Overloads:

  • #set_text_font(font) ⇒ Object

    Parameters:

  • #set_text_font(font_info) ⇒ Object

    Parameters:

    • font_info (Wx::FontInfo)
  • #set_text_font(pointSize, family, style, weight, underline = false, faceName = (''), encoding = Wx::FontEncoding::FONTENCODING_DEFAULT) ⇒ Object

    Parameters:

    • pointSize (Integer)

      Size in points. See Font#initialize.

    • family (Wx::FontFamily)

      The font family. See Font#initialize.

    • style (Wx::FontStyle)

      One of FontStyle::FONTSTYLE_NORMAL, FontStyle::FONTSTYLE_SLANT and FontStyle::FONTSTYLE_ITALIC. See Font#initialize.

    • weight (Wx::FontWeight)

      Font weight. One of the FontWeight enumeration values. See Font#initialize.

    • underline (Boolean) (defaults to: false)

      The value can be true or false. See Font#initialize.

    • faceName (String) (defaults to: (''))

      An optional string specifying the face name to be used. See Font#initialize.

    • encoding (Wx::FontEncoding) (defaults to: Wx::FontEncoding::FONTENCODING_DEFAULT)

      An encoding which may be one of the enumeration values of FontEncoding. See Font#initialize.



61
62
63
64
65
66
67
68
# File 'lib/wx/shapes/shapes/text_shape.rb', line 61

def set_font(*args)
  @font = if args.size == 1 && Wx::Font === args.first
            args.first
          else
            Wx::Font.new(*args)
          end
  update_rect_size
end

#set_text(txt) ⇒ Object Also known as: text=

Set text.

Parameters:

  • txt (String)

    Text content



80
81
82
83
# File 'lib/wx/shapes/shapes/text_shape.rb', line 80

def set_text(txt)
  @text = txt
  update_rect_size
end

#set_text_colour(col) ⇒ Object Also known as: text_colour=

Set text color.

Parameters:



109
110
111
# File 'lib/wx/shapes/shapes/text_shape.rb', line 109

def set_text_colour(col)
  @text_color = Wx::Colour === col ? col : Wx::Colour.new(col)
end

#update(recurse = true) ⇒ Object

Update shape (align all child shapes and resize it to fit them)



122
123
124
125
# File 'lib/wx/shapes/shapes/text_shape.rb', line 122

def update(recurse = true)
  update_rect_size
  super
end

#update_rect_sizeObject

Updates rectangle size for this shape.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/wx/shapes/shapes/text_shape.rb', line 170

def update_rect_size
  tsize = get_text_extent

  if tsize.is_fully_specified
    tsize.width = 1 if tsize.width <= 0
    tsize.height = 1 if tsize.height <= 0

    @rect_size.x = tsize.width.to_f
    @rect_size.y = tsize.height.to_f
  end
end