lib/wx/shapes/shapes/control_shape.rb in wxruby3-shapes-0.9.0.pre.beta.3 vs lib/wx/shapes/shapes/control_shape.rb in wxruby3-shapes-0.9.5
- old
+ new
@@ -121,50 +121,41 @@
# Defaults
module DEFAULT
CONTROLOFFSET = 0
PROCESSEVENTS = EVTPROCESSING::KEY2CANVAS | EVTPROCESSING::MOUSE2CANVAS
- MODFILL = Wx::Brush.new(Wx::BLUE, Wx::BrushStyle::BRUSHSTYLE_BDIAGONAL_HATCH) if Wx::App.is_main_loop_running
- Wx.add_delayed_constant(self, :MODFILL) { Wx::Brush.new(Wx::BLUE, Wx::BrushStyle::BRUSHSTYLE_BDIAGONAL_HATCH) }
- MODBORDER = Wx::Pen.new(Wx::BLUE, 1, Wx::PenStyle::PENSTYLE_SOLID) if Wx::App.is_main_loop_running
- Wx.add_delayed_constant(self, :MODBORDER) { Wx::Pen.new(Wx::BLUE, 1, Wx::PenStyle::PENSTYLE_SOLID) }
+ class << self
+ def mod_fill; @mod_fill ||= Wx::Brush.new(Wx::BLUE, Wx::BrushStyle::BRUSHSTYLE_BDIAGONAL_HATCH); end
+ def mod_border; @mod_border ||= Wx::Pen.new(Wx::BLUE, 1, Wx::PenStyle::PENSTYLE_SOLID); end
+ def ctrl_fill; @ctrl_fill ||= Wx::TRANSPARENT_BRUSH.dup; end
+ def ctrl_border; @ctrl_border ||= Wx::TRANSPARENT_PEN.dup; end
+ end
end
- property :event_processing, :control_offset, :mod_fill, :mod_border
+ property :event_processing, :control_offset
+ property({ mod_fill: :serialize_mod_fill, mod_border: :serialize_mod_border }, optional: true)
- # @overload initialize()
- # Default constructor.
- # @overload initialize(pos, size, diagram)
- # User constructor.
- # @param [Wx::Window] ctrl managed GUI control
- # @param [Wx::RealPoint] pos Initial position
- # @param [Wx::RealPoint] size Initial size
- # @param [Wx::SF::Diagram] diagram parent diagram
- def initialize(*args)
- if args.empty?
- super
- @control = nil
- else
- ctrl = args.shift
- super(*args)
- set_control(ctrl)
- end
+ # Constructor.
+ # @param [Wx::RealPoint] pos Initial position
+ # @param [Wx::RealPoint] size Initial size
+ # @param [Wx::Window] control managed GUI control
+ # @param [Wx::SF::Diagram] diagram parent diagram
+ def initialize(pos = Shape::DEFAULT::POSITION, size = RectShape::DEFAULT::SIZE, control: nil, diagram: nil)
+ super(pos, size, diagram: diagram)
+ set_control(control)
add_style(Shape::STYLE::PROCESS_DEL)
@process_events = DEFAULT::PROCESSEVENTS
- @mod_fill = DEFAULT::MODFILL
- @mod_border = DEFAULT::MODBORDER
+ @mod_fill = nil
+ @mod_border = nil
@control_offset = DEFAULT::CONTROLOFFSET
@event_sink = EventSink.new(self)
@prev_parent = nil
@prev_style = 0
@prev_fill = nil
@prev_border = nil
-
- @fill = Wx::TRANSPARENT_BRUSH
- @border = Wx::TRANSPARENT_PEN
end
# Set managed GUI control.
# @param [Wx::Window] ctrl existing manager GUI control
# @param [Boolean] fit true if the control shape should be resized in accordance to the given GUI control
@@ -209,10 +200,24 @@
update_control
end
end
+ # Get current fill style.
+ # @return [Wx::Brush] Current brush
+ def get_fill
+ @fill || (@diagram&.shape_canvas ? @diagram.shape_canvas.control_fill : DEFAULT.ctrl_fill)
+ end
+ alias :fill :get_fill
+
+ # Get current border style.
+ # @return [Wx::Pen] Current pen
+ def get_border
+ @border || (@diagram&.shape_canvas ? @diagram.shape_canvas.control_border : DEFAULT.ctrl_border)
+ end
+ alias :border :get_border
+
# Get managed GUI control.
# @return [Wx::Window] the GUI control
def get_control
@control
end
@@ -230,31 +235,50 @@
def get_event_processing
@process_events
end
# Set control shape's background style used during its modification.
- # @param [Wx::Brush] brush Reference to used brush
- def set_mod_fill(brush)
- @mod_fill = brush
+ # @overload set_mod_fill(brush)
+ # @param [Wx::Brush] brush
+ # @overload set_mod_fill(color, style=Wx::BrushStyle::BRUSHSTYLE_SOLID)
+ # @param [Wx::Colour,Symbol,String] color brush color
+ # @param [Wx::BrushStyle] style
+ # @overload set_mod_fill(stipple_bitmap)
+ # @param [Wx::Bitmap] stipple_bitmap
+ def set_mod_fill(*args)
+ @mod_fill = if args.size == 1 && Wx::Brush === args.first
+ args.first
+ else
+ Wx::Brush.new(*args)
+ end
end
# Get control shape's background style used during its modification.
# @return [Wx::Brush] Used brush
def get_mod_fill
- @mod_fill
+ @mod_fill || (@diagram&.shape_canvas ? @diagram.shape_canvas.control_mod_fill : DEFAULT.mod_fill)
end
# Set control shape's border style used during its modification.
- # @param [Wx::Pen] pen Reference to used pen
+ # @overload set_mod_border(pen)
+ # @param [Wx::Pen] pen
+ # @overload set_mod_border(color, width=1, style=Wx::PenStyle::PENSTYLE_SOLID)
+ # @param [Wx::Colour,String,Symbol] color
+ # @param [Integer] width
+ # @param [Wx::PenStyle] style
def set_mod_border(pen)
- @mod_border = pen
+ @mod_border = if args.size == 1 && Wx::Pen === args.first
+ args.first
+ else
+ Wx::Pen.new(*args)
+ end
end
# Get control shape's border style used during its modification.
# @return [Wx::Pen] Used pen
def get_mod_border
- @mod_border
+ @mod_border || (@diagram&.shape_canvas ? @diagram.shape_canvas.control_mod_border : DEFAULT.mod_border)
end
# Set control shape's offset (a gap between the shape's border and managed GUI control).
# @param [Integer] offset Offset size
def set_control_offset(offset)
@@ -313,11 +337,11 @@
# 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).
# @param [Float] x Horizontal scale factor
# @param [Float] y Vertical scale factor
# @param children true if the shape's children should be scaled as well, otherwise the shape will be updated after scaling via update() function.
- def scale(x, y, children = WITHCHILDREN)
+ def scale(x, y, children: WITHCHILDREN)
super
update_control
end
# Move the shape to the given absolute position. The function can be overridden if necessary.
@@ -335,11 +359,11 @@
super
update_control
end
# Update shape (align all child shapes an resize it to fit them)
- def update
+ def update(recurse = true)
super
update_control
end
# Resize the shape to bound all child shapes. The function can be overridden if necessary.
@@ -363,11 +387,11 @@
# The function is called by the framework (by the shape canvas).
# @param [Wx::Point] pos Current mouse position
# @see ShapeCanvas
def on_begin_drag(pos)
@prev_fill = @fill
- @fill = @mod_fill
+ @fill = get_mod_fill
canvas = get_parent_canvas
if canvas
@prev_style = canvas.get_style
canvas.remove_style(ShapeCanvas::STYLE::DND)
end
@@ -404,13 +428,13 @@
#
# The function is called by the framework (by the shape canvas).
# @param [Shape::Handle] handle Reference to dragged handle
def on_begin_handle(handle)
@prev_border = @border
- @border = @mod_border
+ @border = get_mod_border
@prev_fill = @fill
- @fill = @mod_fill
+ @fill = get_mod_fill
if @control
@control.hide
@control.disconnect(Wx::ID_ANY, Wx::ID_ANY, Wx::EVT_SIZE)
end
@@ -474,9 +498,21 @@
set_control(nil)
if @diagram
@diagram.remove_shape(self, false)
end
end
+ end
+
+ # (de-)serialize mod_fill; allows for nil values
+ def serialize_mod_fill(*val)
+ @mod_fill = val.first unless val.empty?
+ @mod_fill
+ end
+
+ # (de-)serialize mod_fill; allows for nil values
+ def serialize_mod_border(*val)
+ @mod_border = val.first unless val.empty?
+ @mod_border
end
end
end