Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/wx/core/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.included(klass)
functor_nm = scope.pop
code = <<~__CODE
def #{functor_nm}(*args, **kwargs, &block)
dlg = #{klass.name}.new(*args, **kwargs)
dlg = kwargs.empty? ? #{klass.name}.new(*args) : #{klass.name}.new(*args, **kwargs)
begin
if block_given?
return block.call(dlg)
Expand Down
15 changes: 12 additions & 3 deletions lib/wx/core/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,21 @@ def hash
end

def <=>(other)
this_x, this_y = to_ary
if Wx::Point === other
(x*y) <=> (other.x*other.y)
that_x, that_y = other.to_ary
elsif Array === other && other.size == 2
(x*y) <=> (other.first.to_i*other.last.to_i)
that_x, that_y = other
else
nil
return nil
end

if this_y < that_y
-1
elsif that_y < this_y
1
else
this_x <=> that_x
end
end

Expand Down
191 changes: 163 additions & 28 deletions lib/wx/core/sizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,71 @@

module Wx
class Sizer
# Generic method to add items, supporting positional and named
# Redefine #add and #insert methods to support positional and named
# arguments
ADD_ITEM_PARAMS = [Wx::Parameter[:index, -1],
Wx::Parameter[:proportion, 0],
Wx::Parameter[:flag, 0],
Wx::Parameter[:border, 0]]
SIZER_ADD_PARAMS = [Wx::Parameter[:proportion, 0],
Wx::Parameter[:flag, 0],
Wx::Parameter[:border, 0],
Wx::Parameter[:userData, nil]]

def add_item(item, *mixed_args)
wx_sizer_add = instance_method :add
wx_redefine_method :add do |*args, **kwargs|

begin
args = Wx::args_as_list(ADD_ITEM_PARAMS, *mixed_args)
rescue => err
err.set_backtrace(caller)
Kernel.raise err
end
if args.last.is_a?(Wx::SizerFlags) # using 'flags' variant?
wx_sizer_add.bind(self).call(*args) # no need for keyword scanning
else
full_args = []

full_args = []
full_args << args.shift # get first argument

# extract the width and the height in the case of a spacer
# defined as an array
if item.kind_of?(Array)
Kernel.raise ArgumentError,
"Invalid Sizer specification : [width, height] expected" if item.size != 2
full_args << item[0] << item[1]
else
full_args << item
unless full_args.first.is_a?(Wx::Window) || full_args.first.is_a?(Wx::Sizer)
full_args << args.shift # must be spacer variant, get height argument as well
end

begin
args = Wx::args_as_list(SIZER_ADD_PARAMS, *args, **kwargs)
rescue => err
err.set_backtrace(caller)
Kernel.raise err
end

# update the full arguments list with the optional arguments
full_args.concat(args)

# Call original add with full args
wx_sizer_add.bind(self).call(*full_args)
end

# update the full arguments list with the optional arguments (except index)
idx = args.shift
full_args.concat(args)
end

# Call add to append if default position
if idx == -1
add(*full_args)
wx_sizer_insert = instance_method :insert
wx_redefine_method :insert do |index, *args, **kwargs|

if args.last.is_a?(Wx::SizerFlags) # using 'flags' variant?
wx_sizer_insert.bind(self).call(index, *args) # no need for keyword scanning
else
insert(idx, *full_args)
full_args = []

full_args << args.shift # get first argument after index

unless full_args.first.is_a?(Wx::Window) || full_args.first.is_a?(Wx::Sizer)
full_args << args.shift # must be spacer variant, get height argument as well
end

begin
args = Wx::args_as_list(SIZER_ADD_PARAMS, *args, **kwargs)
rescue => err
err.set_backtrace(caller)
Kernel.raise err
end

# update the full arguments list with the optional arguments
full_args.concat(args)

# Call original insert with full args
wx_sizer_insert.bind(self).call(index, *full_args)
end

end

# Overload to provide Enumerator without block
Expand Down Expand Up @@ -169,6 +196,42 @@ class FlexGridSizer < GridSizer

class GridBagSizer < FlexGridSizer

# Redefine #add method to support positional and named
# arguments
GBSIZER_ADD_PARAMS = [Wx::Parameter[:span, Wx::DEFAULT_SPAN],
Wx::Parameter[:flag, 0],
Wx::Parameter[:border, 0],
Wx::Parameter[:userData, nil]]

wx_gbsizer_add = instance_method :add
wx_redefine_method :add do |*args, **kwargs|

full_args = []

full_args << args.shift # get first argument

unless full_args.first.is_a?(Wx::Window) || full_args.first.is_a?(Wx::Sizer)
full_args << args.shift # must be spacer variant, get height argument as well
end

# get mandatory pos arg
full_args << args.shift

begin
args = Wx::args_as_list(GBSIZER_ADD_PARAMS, *args, **kwargs)
rescue => err
err.set_backtrace(caller)
Kernel.raise err
end

# update the full arguments list with the optional arguments
full_args.concat(args)

# Call original add with full args
wx_gbsizer_add.bind(self).call(*full_args)

end

wx_initialize = instance_method :initialize
wx_redefine_method :initialize do |*args, &block|
wx_initialize.bind(self).call(*args)
Expand All @@ -186,4 +249,76 @@ class GridBagSizer < FlexGridSizer

end

class GBPosition

include Comparable

# make GBPosition usable for parallel assignments like `r, c = pos`
def to_ary
[row, col]
end

# Compare with another position value
def <=>(other)
this_row, this_col = to_ary
if Wx::GBPosition === other
that_row, that_col = other.to_ary
elsif Array === other and other.size == 2
that_row, that_col = other
else
return nil
end

if this_row < that_row
-1
elsif that_row < this_row
1
else
this_col <=> that_col
end
end

def eql?(other)
if other.instance_of?(self.class)
self == other
else
false
end
end

def hash
to_ary.hash
end

def dup
Wx::GBPosition.new(*self.to_ary)
end

end

class GBSpan

# make GBSpan usable for parallel assignments like `r, c = span`
def to_ary
[rowspan, colspan]
end

def eql?(other)
if other.instance_of?(self.class)
self == other
else
false
end
end

def hash
to_ary.hash
end

def dup
Wx::GBSpan.new(*self.to_ary)
end

end

end
6 changes: 3 additions & 3 deletions lib/wx/core/splash_screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Wx::SplashScreen
end

# now redefine the overridden ctor to account for deviating arglist
wx_redefine_method :initialize do |bitmap, splashstyle, milliseconds, parent = nil, *mixed_args, &block|
wx_redefine_method :initialize do |bitmap, splashstyle, milliseconds, parent = nil, *args, **kwargs, &block|
# no zero-args ctor for use with XRC!

real_args = begin
[ bitmap, splashstyle, milliseconds, parent ] + self.class.args_as_list(*mixed_args)
[ bitmap, splashstyle, milliseconds, parent ] + self.class.args_as_list(*args, **kwargs)
rescue => err
msg = "Error initializing #{self.inspect}\n"+
" : #{err.message} \n" +
"Provided are #{[ bitmap, splashstyle, milliseconds, parent ] + mixed_args} \n" +
"Provided are #{[ bitmap, splashstyle, milliseconds, parent ] + args + [kwargs]} \n" +
"Correct parameters for #{self.class.name}.new are:\n" +
self.class.describe_constructor(
":bitmap => (Wx::Bitmap)\n:splashstyle => (Integer)\n:milliseconds => (Integer)\n:parent => (Wx::Window)\n")
Expand Down
4 changes: 2 additions & 2 deletions lib/wx/core/toolbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Wx::ToolBar
Wx::Parameter[ :long_help, "" ],
Wx::Parameter[ :client_data, nil ] ]

def add_item(bitmap1, *mixed_args)
def add_item(bitmap1, *args, **kwargs)

begin
args = Wx::args_as_list(ADD_ITEM_PARAMS, *mixed_args)
args = Wx::args_as_list(ADD_ITEM_PARAMS, *args, **kwargs)
rescue => err
err.set_backtrace(caller)
Kernel.raise err
Expand Down
18 changes: 8 additions & 10 deletions lib/wx/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ module Wx
# structs containing the keyword name and default value for each
# possible argument. +mixed_args+ is an array which may optionally end
# with a set of named arguments
def self.args_as_list(param_spec, *mixed_args)
def self.args_as_list(param_spec, *args, **kwargs)

begin
# get keyword arguments from mixed args if supplied, else empty
kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
out_args = []
param_spec.each_with_index do | param, i |
# has supplied list arg or the keyword arg?
arg = mixed_args[i]
arg = kwa.delete(param.name) if arg.nil? && kwa.key?(param.name)
arg = args[i]
arg = kwargs.delete(param.name) if arg.nil? && kwargs.key?(param.name)
if Proc === param.default_or_proc
arg = param.default_or_proc.call(arg) # provides default or converts arg
elsif arg.nil?
Expand All @@ -37,13 +35,13 @@ def self.args_as_list(param_spec, *mixed_args)
out_args << arg
end
rescue
Kernel.raise ArgumentError,
"Bad arg composition of #{mixed_args.inspect}"
Kernel.raise ArgumentError,
"Bad arg composition of #{args.inspect}"
end

unless kwa.empty?
Kernel.raise ArgumentError,
"Unknown keyword argument(s) : #{kwa.keys.inspect}"
unless kwargs.empty?
Kernel.raise ArgumentError,
"Unknown keyword argument(s) : #{kwargs.keys.inspect}"
end

out_args
Expand Down
24 changes: 11 additions & 13 deletions lib/wx/keyword_ctors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,16 @@ def wx_ctor_params(*params)
end
end

def args_as_list(*mixed_args)
Wx::args_as_list(param_spec, *mixed_args)
end

def args_as_hash(*mixed_args)
kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
param_spec.zip(mixed_args) do | param, arg |
kwa[param.name] = arg if arg
if RUBY_VERSION < '3.0.0'
def args_as_list(*mixed_args)
Wx::args_as_list(param_spec, *mixed_args)
end
else
def args_as_list(*args, **kwargs)
Wx::args_as_list(param_spec, *args, **kwargs)
end
kwa
end

def describe_constructor(txt = '')
param_spec.inject(txt) do | desc, param |
if Proc === param.default_or_proc
Expand All @@ -198,19 +196,19 @@ def self.included(klass)
# The new definition of initialize; accepts a parent arg
# mixed_args, which may zero or more position args, optionally
# terminated with hash keyword args, and an optional block
wx_redefine_method :initialize do |parent = :default_ctor, *mixed_args, &block|
wx_redefine_method :initialize do |parent = :default_ctor, *args, **kwargs, &block|
# allow zero-args ctor for use with XRC
if parent == :default_ctor
pre_wx_kwctor_init
return
end

real_args = begin
[ parent ] + self.class.args_as_list(*mixed_args)
[ parent ] + self.class.args_as_list(*args, **kwargs)
rescue => err
msg = "Error initializing #{self.inspect}\n"+
" : #{err.message} \n" +
"Provided are #{[parent] + mixed_args} \n" +
"Provided are #{[parent] + args + [kwargs]} \n" +
"Correct parameters for #{self.class.name}.new are:\n" +
self.class.describe_constructor(":parent => (Wx::Window)\n")

Expand Down
6 changes: 3 additions & 3 deletions lib/wx/rtc/richtext_formatting_dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class << self
end

# now redefine the overridden ctor to account for deviating arglist
wx_redefine_method :initialize do |flags = nil, parent = nil, *mixed_args, &block|
wx_redefine_method :initialize do |flags = nil, parent = nil, *args, **kwargs, &block|
# allow zero-args ctor for use with XRC
if flags.nil?
pre_wx_kwctor_init
return
end

real_args = begin
[ flags, parent ] + self.class.args_as_list(*mixed_args)
[ flags, parent ] + self.class.args_as_list(*args, **kwargs)
rescue => err
msg = "Error initializing #{self.inspect}\n"+
" : #{err.message} \n" +
"Provided are #{[ flags, parent ] + mixed_args} \n" +
"Provided are #{[ flags, parent ] + args + [kwargs]} \n" +
"Correct parameters for #{self.class.name}.new are:\n" +
self.class.describe_constructor(
":flags => (Integer)\n:parent => (Wx::Window)\n")
Expand Down
Loading
Loading