diff --git a/lib/wx/core/colour.rb b/lib/wx/core/colour.rb index 7e978ff0..7a68b1dd 100644 --- a/lib/wx/core/colour.rb +++ b/lib/wx/core/colour.rb @@ -6,37 +6,45 @@ # Copyright 2004-2007, wxRuby development team # released under the MIT-like wxRuby2 license -class Wx::Colour - # Redefine the initialize method so it raises an exception if an - # invalid colour value is given. This might be an unknown colour - # string (eg 'dark blue') or out-of-bounds integer values (<0 or >255) - wx_init = self.instance_method(:initialize) - define_method(:initialize) do | *args | - begin - wx_init.bind(self).call(*args) - # Invalid integer values raise SWIG 'no matching func' - rescue ArgumentError, TypeError - Kernel.raise ArgumentError, "Invalid colour values #{args.inspect}" +require_relative 'ext' + +module Wx + + Wx.add_delayed_constant(self, :TRANSPARENT_COLOUR, 'Wx::Colour.new(0, 0, 0, Wx::ALPHA_TRANSPARENT)') { Wx::Colour.new(0, 0, 0, Wx::ALPHA_TRANSPARENT) } + + class Colour + # Redefine the initialize method so it raises an exception if an + # invalid colour value is given. This might be an unknown colour + # string (eg 'dark blue') or out-of-bounds integer values (<0 or >255) + wx_init = self.instance_method(:initialize) + define_method(:initialize) do | *args | + begin + wx_init.bind(self).call(*args) + # Invalid integer values raise SWIG 'no matching func' + rescue ArgumentError, TypeError + Kernel.raise ArgumentError, "Invalid colour values #{args.inspect}" + end end - end - # Missing Standard colour - Wx::MAGENTA = new(255, 0, 255) - - # Colours are equal to one another if they have the same red, green - # and blue intensity, and the same alpha - def ==(other) - case other - when Wx::Colour - [ self.red, self.green, self.blue, self.alpha ] == - [ other.red, other.green, other.blue, other.alpha ] - else - false + # Missing Standard colour + Wx::MAGENTA = new(255, 0, 255) + + # Colours are equal to one another if they have the same red, green + # and blue intensity, and the same alpha + def ==(other) + case other + when Wx::Colour + [ self.red, self.green, self.blue, self.alpha ] == + [ other.red, other.green, other.blue, other.alpha ] + else + false + end end - end - # More informative output for inspect etc - def to_s - "#" + # More informative output for inspect etc + def to_s + "#" + end end + end diff --git a/lib/wx/doc/colour.rb b/lib/wx/doc/colour.rb new file mode 100644 index 00000000..abc3d2e0 --- /dev/null +++ b/lib/wx/doc/colour.rb @@ -0,0 +1,12 @@ +# :stopdoc: +# Copyright (c) 2023 M.J.N. Corino, The Netherlands +# +# This software is released under the MIT license. +# :startdoc: + + +module Wx + + TRANSPARENT_COLOUR = Wx::Colour.new(0, 0, 0, Wx::ALPHA_TRANSPARENT) + +end diff --git a/lib/wx/doc/gc_dc.rb b/lib/wx/doc/gc_dc.rb index c8c24b57..013014e3 100644 --- a/lib/wx/doc/gc_dc.rb +++ b/lib/wx/doc/gc_dc.rb @@ -13,6 +13,9 @@ class GCDC < Wx::DC # Creates a Wx::GCDC instance for target and # passes the instance to the given block to draw on. + # @overload draw_on() + # @yieldparam [Wx::GCDC] dc GCDC instance to draw on + # @return [Object] result from block # @overload draw_on(dc) # @param [Wx::WindowDC,Wx::MemoryDC] target DC to draw on # @yieldparam [Wx::GCDC] dc GCDC instance to draw on @@ -23,10 +26,16 @@ class GCDC < Wx::DC # @return [Object] result from block # @wxrb_require USE_PRINTING_ARCHITECTURE,WXMSW|WXOSX|USE_GTKPRINT # @overload draw_on(gc) + # Note that the context will continue using the same font, pen and brush as before until #set_font, #set_pen + # or #set_brush is explicitly called to change them. This means that the code can use this wxDC-derived object + # to work using pens and brushes with alpha component, for example (which normally isn't supported by Wx::DC API), + # but it also means that the return values of #get_font, #get_pen and #get_brush won't really correspond to the + # actually used objects because they simply can't represent them anyhow. If you wish to avoid such discrepancy, + # you need to call the setter methods to bring Wx::DC and Wx::GraphicsContext font, pen and brush in sync with each other. # @param [Wx::GraphicsContext] gc GraphicsContext to draw on # @yieldparam [Wx::GCDC] dc GCDC instance to draw on # @return [Object] result from block - def self.draw_on(arg) end + def self.draw_on(*arg) end end end diff --git a/rakelib/lib/director/dc.rb b/rakelib/lib/director/dc.rb index f8e03334..783fd10a 100644 --- a/rakelib/lib/director/dc.rb +++ b/rakelib/lib/director/dc.rb @@ -36,7 +36,8 @@ def setup 'wxDC::GetLogicalOrigin(wxCoord *,wxCoord *) const', 'wxDC::GetHandle' ] - spec.disable_proxies + spec.disable_proxies + spec.disown 'wxGraphicsContext *ctx' spec.rename_for_ruby({ 'GetDimensions' => 'wxDC::GetSize(wxCoord *, wxCoord *) const', 'GetDimensionsMM' => 'wxDC::GetSizeMM(wxCoord *, wxCoord *) const', diff --git a/rakelib/lib/director/derived_dc.rb b/rakelib/lib/director/derived_dc.rb index 9f217557..51e0b5a7 100644 --- a/rakelib/lib/director/derived_dc.rb +++ b/rakelib/lib/director/derived_dc.rb @@ -69,6 +69,7 @@ def setup spec.ignore 'wxPaintDC::wxPaintDC' when 'wxMemoryDC' spec.items << 'wxBufferedDC' << 'wxBufferedPaintDC' << 'wxAutoBufferedPaintDC' + spec.gc_as_untracked %w[wxBufferedDC wxBufferedPaintDC wxAutoBufferedPaintDC] spec.make_abstract 'wxMemoryDC' spec.make_abstract 'wxBufferedDC' spec.make_abstract 'wxBufferedPaintDC' @@ -302,6 +303,24 @@ def setup __HEREDOC end spec.add_extend_code 'wxGCDC', <<~__HEREDOC + static VALUE draw_on() + { + if (!wxRuby_IsAppRunning()) + rb_raise(rb_eRuntimeError, "A running Wx::App is required."); + VALUE rc = Qnil; + if (rb_block_given_p ()) + { + // Somehow there seems to be a problem with the Ruby GCDC value + // being GC-ed unless we block GC for the duration of the block + // execution. Unclear why. We have similar code for other objects + // where this issue does not come up. + wxGCDC gc_dc; + wxGCDC* dc_ptr = &gc_dc; + VALUE rb_dc = SWIG_NewPointerObj(SWIG_as_voidptr(dc_ptr), SWIGTYPE_p_wxGCDC, 0); + rc = rb_yield(rb_dc); + } + return rc; + } static VALUE draw_on(const wxWindowDC& dc) { if (!wxRuby_IsAppRunning()) @@ -356,7 +375,8 @@ def setup } return rc; } - __HEREDOC + __HEREDOC + spec.disown 'wxGraphicsContext *gc' spec.ignore 'wxGCDC::wxGCDC(const wxEnhMetaFileDC &)' when 'wxScaledDC' spec.items.clear # wxRuby extension; no XML docs diff --git a/rakelib/lib/director/graphics_context.rb b/rakelib/lib/director/graphics_context.rb index 6dad28cb..0877e606 100644 --- a/rakelib/lib/director/graphics_context.rb +++ b/rakelib/lib/director/graphics_context.rb @@ -29,12 +29,13 @@ def setup 'wxGraphicsContext::CreateFromNativeHDC', 'wxGraphicsContext::CreateFromUnknownDC', 'wxGraphicsContext::GetNativeContext', - 'wxGraphicsContext::Create', + 'wxGraphicsContext::Create(const wxEnhMetaFileDC &)', 'wxGraphicsContext::CreateMatrix(const wxAffineMatrix2DBase &) const', 'wxGraphicsContext::DrawLines(size_t, const wxPoint2DDouble *, wxPolygonFillMode)', 'wxGraphicsContext::StrokeLines(size_t, const wxPoint2DDouble *)', 'wxGraphicsContext::StrokeLines (size_t, const wxPoint2DDouble *, const wxPoint2DDouble *)' spec.ignore_unless(Config::AnyOf.new('WXMSW', 'WXOSX', 'USE_GTKPRINT'), 'wxGraphicsContext::Create(const wxPrinterDC &)') + spec.new_object 'wxGraphicsContext::Create' spec.add_header_code <<~__HEREDOC // special free funcs are needed to clean up Dashes array if it has been // set; wxWidgets does not do this automatically so will leak if not diff --git a/rakelib/lib/director/graphics_object.rb b/rakelib/lib/director/graphics_object.rb index c2db082e..3992eb24 100644 --- a/rakelib/lib/director/graphics_object.rb +++ b/rakelib/lib/director/graphics_object.rb @@ -34,6 +34,7 @@ def setup spec.ignore 'wxGraphicsRenderer::GetGDIPlusRenderer', 'wxGraphicsRenderer::GetDirect2DRenderer' end + spec.new_object 'wxGraphicsRenderer::CreateContext' # Deal with GraphicsMatrix#get method spec.map_apply 'double *OUTPUT' => [ 'wxDouble *a', 'wxDouble *b', 'wxDouble *c', 'wxDouble *d', diff --git a/rakelib/lib/director/window.rb b/rakelib/lib/director/window.rb index 4fb9170e..41cf0ef1 100644 --- a/rakelib/lib/director/window.rb +++ b/rakelib/lib/director/window.rb @@ -151,13 +151,8 @@ def setup { VALUE rc = Qnil; wxAutoBufferedPaintDC dc(ptr); - #if wxALWAYS_NATIVE_DOUBLE_BUFFER - wxPaintDC* ptr_dc = &dc; - VALUE r_class = rb_const_get(mWxCore, rb_intern("PaintDC")); - #else - wxMemoryDC* ptr_dc = &dc; - VALUE r_class = rb_const_get(mWxCore, rb_intern("MemoryDC")); - #endif + wxAutoBufferedPaintDC* ptr_dc = &dc; + VALUE r_class = rb_const_get(mWxCore, rb_intern("AutoBufferedPaintDC")); swig_type_info* swig_type = wxRuby_GetSwigTypeForClass(r_class); VALUE rb_dc = SWIG_NewPointerObj(SWIG_as_voidptr(ptr_dc), swig_type, 0); rc = rb_yield(rb_dc);