Class: Wx::SF::RectShape

Inherits:
Shape
  • Object
show all
Defined in:
lib/wx/shapes/shapes/rect_shape.rb

Defined Under Namespace

Modules: DEFAULT

Instance Method Summary collapse

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, #descendant?, #do_alignment, #does_not_accept_children?, #draw, #draw_selected, #get_absolute_position, #get_accepted_children, #get_accepted_connections, #get_accepted_src_neighbours, #get_accepted_trg_neighbours, #get_assigned_connections, #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_child_dropped, #on_dragging, #on_end_drag, #on_end_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_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, #update, #visible?

Constructor Details

#initialize(pos = Shape::DEFAULT::POSITION, size = DEFAULT::SIZE, diagram: nil) ⇒ RectShape

Constructor.

Parameters:



29
30
31
32
33
34
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 29

def initialize(pos = Shape::DEFAULT::POSITION, size = DEFAULT::SIZE, diagram: nil)
  super(pos, diagram: diagram)
  set_rect_size(size.to_real_point)
  @fill = nil
  @border = nil
end

Instance Method Details

#create_handlesObject

Function called by the framework responsible for creation of shape handles at the creation time. The function can be overridden if necessary.



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 140

def create_handles
  # HINT: overload it for custom actions...

  add_handle(Shape::Handle::TYPE::LEFTTOP)
  add_handle(Shape::Handle::TYPE::TOP)
  add_handle(Shape::Handle::TYPE::RIGHTTOP)
  add_handle(Shape::Handle::TYPE::RIGHT)
  add_handle(Shape::Handle::TYPE::RIGHTBOTTOM)
  add_handle(Shape::Handle::TYPE::BOTTOM)
  add_handle(Shape::Handle::TYPE::LEFTBOTTOM)
  add_handle(Shape::Handle::TYPE::LEFT)
  add_handle(Shape::Handle::TYPE::LEFTTOP)
end

#do_begin_handleObject (protected)

Handle action at handle drag beginning



234
235
236
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 234

def do_begin_handle
  # noop
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:



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
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 249

def do_on_handle(handle)
  case handle.type
  when Shape::Handle::TYPE::LEFT
    on_left_handle(handle)

  when Shape::Handle::TYPE::LEFTTOP
    on_left_handle(handle)
    on_top_handle(handle)

  when Shape::Handle::TYPE::LEFTBOTTOM
    on_left_handle(handle)
    on_bottom_handle(handle)

  when Shape::Handle::TYPE::RIGHT
    on_right_handle(handle)

  when Shape::Handle::TYPE::RIGHTTOP
    on_right_handle(handle)
    on_top_handle(handle)

  when Shape::Handle::TYPE::RIGHTBOTTOM
    on_right_handle(handle)
    on_bottom_handle(handle)

  when Shape::Handle::TYPE::TOP
    on_top_handle(handle)

  when Shape::Handle::TYPE::BOTTOM
    on_bottom_handle(handle)
  end
end

#draw_highlighted(dc) ⇒ Object (protected)

Draw the shape in the highlighted mode (another shape is dragged over this shape and this shape will accept the dragged one if it will be dropped on it). The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



310
311
312
313
314
315
316
317
318
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 310

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

  dc.with_pen(Wx::Pen.new(hover_colour, 2)) do
    dc.with_brush(fill) do
      dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
    end
  end
end

#draw_hover(dc) ⇒ Object (protected)

Draw the shape in the hover mode (the mouse cursor is above the shape). The function can be overridden if necessary.

Parameters:

  • dc (Wx::DC)

    Reference to device context where the shape will be drawn to



296
297
298
299
300
301
302
303
304
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 296

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

  dc.with_pen(Wx::Pen.new(hover_colour, 1)) do
    dc.with_brush(fill) do
      dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
    end
  end
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



283
284
285
286
287
288
289
290
291
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 283

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

  dc.with_pen(border) do
    dc.with_brush(fill) do
      dc.draw_rectangle(get_absolute_position.to_point, @rect_size.to_size)
    end
  end
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



322
323
324
325
326
327
328
329
330
331
332
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 322

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

  if fill.style != Wx::BrushStyle::BRUSHSTYLE_TRANSPARENT
    dc.with_pen(Wx::TRANSPARENT_PEN) do
      dc.with_brush(get_parent_canvas.get_shadow_fill) do
        dc.draw_rectangle((get_absolute_position + get_parent_canvas.get_shadow_offset).to_point, @rect_size.to_size)
      end
    end
  end
end

#fit_to_childrenObject

Resize the shape to bound all child shapes. The function can be overridden if necessary.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 177

def fit_to_children
  # HINT: overload it for custom actions...
  
  # get bounding box of the shape and children set to be inside it
  ch_bb = get_bounding_box
  shp_bb = ch_bb.dup

  @child_shapes.each do |child|
    if child.has_style?(STYLE::ALWAYS_INSIDE)
      child.get_complete_bounding_box(ch_bb, BBMODE::SELF | BBMODE::CHILDREN)
    end
  end

  unless ch_bb.empty?
    unless shp_bb.contains?(ch_bb)
      dx = ch_bb.left - shp_bb.left
      dy = ch_bb.top - shp_bb.top

      # resize parent shape
      shp_bb.union!(ch_bb)
      move_to(shp_bb.get_position.x, shp_bb.get_position.y)
      @rect_size = Wx::RealPoint.new(shp_bb.get_size.x.to_f, shp_bb.get_size.y.to_f)
      if has_style?(STYLE::EMIT_EVENTS)
        evt = ShapeEvent.new(EVT_SF_SHAPE_SIZE_CHANGED, self.object_id)
        evt.set_shape(self)
        get_parent_canvas.get_event_handler.process_event(evt)
      end

      # move its "1st level" children if necessary
      if dx < 0 || dy < 0
        @child_shapes.each do |child|
          child.move_by(dx.to_i.abs, 0) if dx < 0
          child.move_by(0, dy.to_i.abs) if dy < 0
        end
      end
    end
  end
end

#get_borderWx::Pen Also known as: border

Get current border style.

Returns:



78
79
80
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 78

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

#get_border_point(start, end_pt) ⇒ Wx::RealPoint

Get intersection point of the shape border and a line leading from ‘start’ point to ‘end’ point. The function can be overridden if necessary.

Parameters:

  • start (Wx::RealPoint)

    Starting point of the virtual intersection line

  • end_pt (Wx::RealPoint)

    Ending point of the virtual intersection line

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 115

def get_border_point(start, end_pt)
  # HINT: override it for custom actions ...
  start = start.to_real_point; end_pt.to_real_point

  # the function calculates intersection of line leading from the shape center to
  # given point with the shape's bounding box
  bb_rct = get_bounding_box

  intersection = Shape.lines_intersection(bb_rct.top_left.to_real,
                                          Wx::RealPoint.new(bb_rct.top_right.x + 1.0, bb_rct.top_right.y.to_f),
                                          start, end_pt)
  intersection ||= Shape.lines_intersection(Wx::RealPoint.new(bb_rct.top_right.x + 1.0, bb_rct.top_right.y.to_f),
                                            Wx::RealPoint.new(bb_rct.bottom_right.x + 1.0, bb_rct.bottom_right.y + 1.0),
                                            start, end_pt)
  intersection ||= Shape.lines_intersection(Wx::RealPoint.new(bb_rct.bottom_right.x + 1.0, bb_rct.bottom_right.y + 1.0),
                                            Wx::RealPoint.new(bb_rct.bottom_left.x.to_f, bb_rct.bottom_left.y + 1.0),
                                            start, end_pt)
  intersection ||= Shape.lines_intersection(Wx::RealPoint.new(bb_rct.bottom_left.x.to_f, bb_rct.bottom_left.y + 1),
                                            bb_rct.top_left.to_real,
                                            start, end_pt)
  intersection || get_center
end

#get_bounding_boxWx::Rect

Get shape’s bounding box. The function can be overridden if necessary.

Returns:



105
106
107
108
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 105

def get_bounding_box
  apos = get_absolute_position
  Wx::Rect.new([apos.x.to_i, apos.y.to_i], [@rect_size.x.to_i, @rect_size.y.to_i])
end

#get_fillWx::Brush Also known as: fill

Get current fill style.

Returns:



55
56
57
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 55

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

#get_rect_sizeWx::RealPoint Also known as: rect_size

Get the rectangle size.

Returns:



98
99
100
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 98

def get_rect_size
  @rect_size
end

#on_begin_handle(handle) ⇒ Object

Event handler called when the user started to drag the shape handle. The function can be overridden if necessary.

The function is called by the framework (by the shape canvas).

Parameters:



171
172
173
174
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 171

def on_begin_handle(handle)
  do_begin_handle
  super
end

#on_bottom_handle(handle) ⇒ Object (protected)

Event handler called during dragging of the bottom shape handle. The function can be overridden if necessary.

Parameters:



393
394
395
396
397
398
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 393

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

  @rect_size.y += handle.get_delta.y
  @rect_size.y = 1.0 if @rect_size.y < 1.0
end

#on_handle(handle) ⇒ Object

Event handler called during dragging of the shape handle. The function can be overridden if necessary.

The function is called by the framework (by the shape canvas).

Parameters:



159
160
161
162
163
164
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 159

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

  super
end

#on_left_handle(handle) ⇒ Object (protected)

Event handler called during dragging of the left shape handle. The function can be overridden if necessary.

Parameters:



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 347

def on_left_handle(handle)
  # HINT: overload it for custom actions...
  
  dx = handle.get_delta.x.to_f

  if (@rect_size.x - dx) < 1.0
    dx = @rect_size.x - 1.0
  end

  # update position of children
  unless has_style?(STYLE::LOCK_CHILDREN)
    @child_shapes.each do |child|
      child.move_by(-dx, 0) if child.get_h_align == HALIGN::NONE
    end
  end
  # update position and size of the shape
  @rect_size.x -= dx
  @relative_position.x += dx
end

#on_right_handle(handle) ⇒ Object (protected)

Event handler called during dragging of the right shape handle. The function can be overridden if necessary.

Parameters:



337
338
339
340
341
342
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 337

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

  @rect_size.x += handle.get_delta.x
  @rect_size.x = 1.0 if @rect_size.x < 1.0
end

#on_top_handle(handle) ⇒ Object (protected)

Event handler called during dragging of the top shape handle. The function can be overridden if necessary.

Parameters:



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 370

def on_top_handle(handle)
  # HINT: overload it for custom actions...
  
  dy = handle.get_delta.y.to_f

  if (@rect_size.y - dy) < 1.0
    dy = @rect_size.y - 1.0
  end

  # update position of children
  unless has_style?(STYLE::LOCK_CHILDREN)
    @child_shapes.each do |child|
      child.move_by(0, -dy) if child.get_v_align == VALIGN::NONE
    end
  end
  # update position and size of the shape
  @rect_size.y -= dy
  @relative_position.y += dy
end

#scale(x, y, children: WITHCHILDREN) ⇒ Object

Scale the shape size by in both directions. The function can be overridden if necessary (new implementation should call default one ore scale shape’s children manually if necessary).

Parameters:

  • x (Float)

    Horizontal scale factor

  • y (Float)

    Vertical scale factor

  • children (Boolean) (defaults to: WITHCHILDREN)

    true if the shape’s children should be scaled as well, otherwise the shape will be updated after scaling via update() function.



221
222
223
224
225
226
227
228
229
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 221

def scale(x, y, children: WITHCHILDREN)
  # HINT: overload it for custom actions...
  if x > 0 && y > 0
    scale_rectangle(x, y)

    # call default function implementation (needed for scaling of shape's children)
    super
  end
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



241
242
243
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 241

def scale_rectangle(x, y)
  set_rect_size(@rect_size.x * x, @rect_size.y * y)
end

#set_border(pen) ⇒ Object #set_border(color, width = 1, style = Wx::PenStyle::PENSTYLE_SOLID) ⇒ Object Also known as: border=

Set rectangle’s border style.

Overloads:

  • #set_border(pen) ⇒ Object

    Parameters:

  • #set_border(color, width = 1, style = Wx::PenStyle::PENSTYLE_SOLID) ⇒ Object

    Parameters:

    • color (Wx::Colour, String, Symbol)
    • width (Integer) (defaults to: 1)
    • style (Wx::PenStyle) (defaults to: Wx::PenStyle::PENSTYLE_SOLID)


67
68
69
70
71
72
73
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 67

def set_border(*args)
  @border = if args.size == 1 && Wx::Pen === args.first
              args.first
            else
              Wx::Pen.new(*args)
            end
end

#set_fill(brush) ⇒ Object #set_fill(color, style = Wx::BrushStyle::BRUSHSTYLE_SOLID) ⇒ Object #set_fill(stipple_bitmap) ⇒ Object Also known as: fill=

Set rectangle’s fill style.

Overloads:

  • #set_fill(brush) ⇒ Object

    Parameters:

  • #set_fill(color, style = Wx::BrushStyle::BRUSHSTYLE_SOLID) ⇒ Object

    Parameters:

    • color (Wx::Colour, Symbol, String)

      brush color

    • style (Wx::BrushStyle) (defaults to: Wx::BrushStyle::BRUSHSTYLE_SOLID)
  • #set_fill(stipple_bitmap) ⇒ Object

    Parameters:

    • stipple_bitmap (Wx::Bitmap)


44
45
46
47
48
49
50
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 44

def set_fill(*args)
  @fill = if args.size == 1 && Wx::Brush === args.first
            args.first
          else
            Wx::Brush.new(*args)
          end
end

#set_rect_size(x, y) ⇒ Object #set_rect_size(size) ⇒ Object Also known as: rect_size=

Set the rectangle size.

Overloads:

  • #set_rect_size(x, y) ⇒ Object

    Parameters:

    • x (Float)

      Horizontal size

    • y (Float)

      Vertical size

  • #set_rect_size(size) ⇒ Object

    Parameters:



89
90
91
92
93
# File 'lib/wx/shapes/shapes/rect_shape.rb', line 89

def set_rect_size(*args)
  x, y = args.size == 1 ? args.first.to_real_point : args
  # set new size while preventing 'invisible' shapes
  @rect_size = Wx::RealPoint.new([1.0, x].max, [1.0, y].max)
end