wxruby 1.9.0-i686-darwin8.4.1 → 1.9.1-i686-darwin8.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # = WxSugar - Accessors
2
+ #
3
+ # The default WxRuby interface has lots and lots of methods like
4
+ #
5
+ # * get_position()
6
+ # * set_size(a_size)
7
+ # * is_checked()
8
+ #
9
+ # and so on. Methods that retrieve set, or query attributes of an object
10
+ # are more normally in Ruby called simply by the attribute name:
11
+ #
12
+ # * position()
13
+ # * size = a_size
14
+ # * checked?
15
+ #
16
+ # This extension creates an alias for every WxRuby instance method that
17
+ # begins with +get_+, +set_+ or +is_+. Note that if you are calling a
18
+ # 'setter' method on self, you must explicitly send the message to self:
19
+ #
20
+ # # set's self size to be 100px by 100px
21
+ # self.size = Wx::Size.new(100, 100)
22
+ # # only sets the value of a local variable 'size'
23
+ # size = Wx::Size.new
24
+
25
+ module WxRubyStyleAccessors
26
+ def method_missing(sym, *args)
27
+ case sym.to_s
28
+ when /^(.*)\=$/
29
+ meth = "set_#{$1}"
30
+ when /^(.*)\?$/
31
+ meth = "is_#{$1}"
32
+ else
33
+ meth = "get_#{sym}"
34
+ end
35
+ if respond_to?(meth)
36
+ send(meth, *args)
37
+ else
38
+ e = NoMethodError.new("undefined method '#{sym}' for #{self.inspect}")
39
+ e.set_backtrace(caller)
40
+ Kernel.raise e
41
+ end
42
+ end
43
+ end
44
+
45
+ all_classes = Wx::constants.collect { | c | Wx::const_get(c) }.grep(Class)
46
+
47
+ all_classes.each do | klass |
48
+ klass.class_eval do
49
+ include WxRubyStyleAccessors
50
+ extend WxRubyStyleAccessors
51
+ end
52
+ end
@@ -16,14 +16,17 @@ class Wx::EvtHandler
16
16
  EventType = Struct.new(:name, :arity, :const, :evt_class)
17
17
 
18
18
  # Fast look-up hash to map event type ids to ruby event classes
19
- EVENT_TYPE_MAPPING = {}
19
+ EVENT_TYPE_CLASS_MAP = {}
20
+ # Hash to look up EVT constants from symbol names of evt handler
21
+ # methods; used internally by disconnect (see EvtHandler.i)
22
+ EVENT_NAME_TYPE_MAP = {}
20
23
 
21
24
  # Given a Wx EventType id (eg Wx::EVT_MENU), returns a WxRuby Event
22
25
  # class which should be passed to event handler blocks. The actual
23
26
  # EVT_XXX constants themselves are in the compiled library, defined in
24
27
  # swig/classes/Event.i
25
28
  def self.event_class_for_type(id)
26
- if evt_klass = EVENT_TYPE_MAPPING[id]
29
+ if evt_klass = EVENT_TYPE_CLASS_MAP[id]
27
30
  return evt_klass
28
31
  else
29
32
  warn "No event class defined for event type #{id}"
@@ -31,6 +34,18 @@ class Wx::EvtHandler
31
34
  end
32
35
  end
33
36
 
37
+ # Given the symbol name of an evt_xxx handler method, returns the
38
+ # Integer Wx::EVT_XXX constant associated with that handler.
39
+ def self.event_type_for_name(name)
40
+ EVENT_NAME_TYPE_MAP[name]
41
+ end
42
+
43
+ # Given the Integer constant Wx::EVT_XXX, returns the convenience
44
+ # handler method name associated with that type of event.
45
+ def self.event_name_for_type(name)
46
+ EVENT_NAME_TYPE_MAP.index(name)
47
+ end
48
+
34
49
  # Public method to register the mapping of a custom event type
35
50
  # +konstant+ (which should be a unique integer; one will be created if
36
51
  # not supplied) to a custom event class +klass+. If +meth+ and +arity+
@@ -50,7 +65,9 @@ class Wx::EvtHandler
50
65
  # Registers the event type +ev_type+, which should be an instance of
51
66
  # the Struct class +Wx::EvtHandler::EventType+.
52
67
  def self.register_event_type(ev_type)
53
- EVENT_TYPE_MAPPING[ev_type.const] = ev_type.evt_class
68
+ EVENT_TYPE_CLASS_MAP[ev_type.const] = ev_type.evt_class
69
+ EVENT_NAME_TYPE_MAP[ev_type.name.intern] = ev_type.const
70
+
54
71
  unless ev_type.arity and ev_type.name
55
72
  return
56
73
  end
@@ -73,6 +90,11 @@ class Wx::EvtHandler
73
90
  end
74
91
  end
75
92
 
93
+ # Definitions for all event types that are part by core wxRuby. Events
94
+ # that are mapped to class Wx::Event are TODO as they are not
95
+ # currently wrapped by the correct class. All StyledTextCtrl
96
+ # (Scintilla) events with prefix EVT_STC are dealt with in the
97
+ # separate styledtextctrl.rb file.
76
98
  EVENT_DEFINITIONS = [
77
99
  EventType['evt_activate', 0,
78
100
  Wx::EVT_ACTIVATE,
@@ -791,34 +813,16 @@ class Wx::EvtHandler
791
813
  evt_mousewheel(&block)
792
814
  end
793
815
 
794
- # TODO
795
- # if defined?(Wx::Scintilla)
796
- # EVENT_TYPE_MAPPING.merge(
797
- # Wx::EVT_SCI_CHANGE => Wx::ScintillaEvent,
798
- # Wx::EVT_SCI_STYLENEEDED => Wx::ScintillaEvent,
799
- # Wx::EVT_SCI_CHARADDED => Wx::ScintillaEvent,
800
- # Wx::EVT_SCI_SAVEPOINTREACHED => Wx::ScintillaEvent,
801
- # Wx::EVT_SCI_SAVEPOINTLEFT => Wx::ScintillaEvent,
802
- # Wx::EVT_SCI_ROMODIFYATTEMPT => Wx::ScintillaEvent,
803
- # Wx::EVT_SCI_KEY => Wx::ScintillaEvent,
804
- # Wx::EVT_SCI_DOUBLECLICK => Wx::ScintillaEvent,
805
- # Wx::EVT_SCI_UPDATEUI => Wx::ScintillaEvent,
806
- # Wx::EVT_SCI_MODIFIED => Wx::ScintillaEvent,
807
- # Wx::EVT_SCI_MACRORECORD => Wx::ScintillaEvent,
808
- # Wx::EVT_SCI_MARGINCLICK => Wx::ScintillaEvent,
809
- # Wx::EVT_SCI_NEEDSHOWN => Wx::ScintillaEvent,
810
- # Wx::EVT_SCI_PAINTED => Wx::ScintillaEvent,
811
- # Wx::EVT_SCI_USERLISTSELECTION => Wx::ScintillaEvent,
812
- # Wx::EVT_SCI_URIDROPPED => Wx::ScintillaEvent,
813
- # Wx::EVT_SCI_DWELLSTART => Wx::ScintillaEvent,
814
- # Wx::EVT_SCI_DWELLEND => Wx::ScintillaEvent,
815
- # Wx::EVT_SCI_START_DRAG => Wx::ScintillaEvent,
816
- # Wx::EVT_SCI_DRAG_OVER => Wx::ScintillaEvent,
817
- # Wx::EVT_SCI_DO_DROP => Wx::ScintillaEvent,
818
- # Wx::EVT_SCI_ZOOM => Wx::ScintillaEvent,
819
- # Wx::EVT_SCI_HOTSPOT_CLICK => Wx::ScintillaEvent,
820
- # Wx::EVT_SCI_HOTSPOT_DCLICK => Wx::ScintillaEvent,
821
- # Wx::EVT_SCI_CALLTIP_CLICK => Wx::ScintillaEvent )
822
- # end
816
+ # convenience evt handler to listen to all scrollwin events
817
+ def evt_scrollwin(&block)
818
+ evt_scrollwin_top(&block)
819
+ evt_scrollwin_bottom(&block)
820
+ evt_scrollwin_lineup(&block)
821
+ evt_scrollwin_linedown(&block)
822
+ evt_scrollwin_pageup(&block)
823
+ evt_scrollwin_pagedown(&block)
824
+ evt_scrollwin_thumbtrack(&block)
825
+ evt_scrollwin_thumbrelease(&block)
826
+ end
823
827
  end
824
828
 
@@ -84,18 +84,18 @@ class Wx::Grid
84
84
  # This and the following methods do a bit of book-keeping - as rows
85
85
  # and columns are deleted and inserted, the position of the columns
86
86
  # and rows with stored editors and renderers may move.
87
- wx_insert_rows = self.instance_method(:insert_rows)
88
- define_method(:insert_rows) do | pos, num |
89
- wx_insert_rows.bind(self).call(pos, num)
87
+ alias :__insert_rows :insert_rows
88
+ def insert_rows(pos = 0, num = 1, update_labels = true)
89
+ __insert_rows(pos, num, update_labels)
90
90
  num.times { @__row_editors.insert(pos, nil) }
91
91
  num.times { @__row_renderers.insert(pos, nil) }
92
92
  num.times { @__cell_editors.insert(pos, []) }
93
93
  num.times { @__cell_renderers.insert(pos, []) }
94
94
  end
95
-
96
- wx_insert_cols = self.instance_method(:insert_cols)
97
- define_method(:insert_cols) do | pos, num |
98
- wx_insert_cols.bind(self).call(pos, num)
95
+
96
+ alias :__insert_cols :insert_cols
97
+ def insert_cols(pos = 0, num = 1, update_labels = true)
98
+ __insert_cols(pos, num, update_labels)
99
99
  num.times { @__col_editors.insert(pos, nil) }
100
100
  num.times { @__col_renderers.insert(pos, nil) }
101
101
  num.times do
@@ -106,18 +106,18 @@ class Wx::Grid
106
106
  end
107
107
  end
108
108
 
109
- wx_delete_rows = self.instance_method(:delete_rows)
110
- define_method(:delete_rows) do | pos, num |
111
- wx_delete_rows.bind(self).call(pos, num)
109
+ alias :__delete_rows :delete_rows
110
+ def delete_rows(pos = 0, num = 1, update_labels = true)
111
+ __delete_rows(pos, num, update_labels)
112
112
  @__row_editors.slice!(pos, num)
113
113
  @__row_renderers.slice!(pos, num)
114
114
  @__cell_editors.slice!(pos, num)
115
115
  @__cell_renderers.slice!(pos, num)
116
116
  end
117
117
 
118
- wx_delete_cols = self.instance_method(:delete_cols)
119
- define_method(:delete_cols) do | pos, num |
120
- wx_delete_cols.bind(self).call(pos, num)
118
+ alias :__delete_cols :delete_cols
119
+ def delete_cols(pos = 0, num = 1, update_labels = true)
120
+ __delete_cols(pos, num, update_labels)
121
121
  @__col_editors.slice!(pos, num)
122
122
  @__col_renderers.slice!(pos, num)
123
123
  num.times do
@@ -1,5 +1,48 @@
1
1
  class Wx::Point
2
+ # More informative output when converted to string
2
3
  def to_s
3
4
  "#<Wx::Point: (#{x}, #{y})>"
4
5
  end
6
+
7
+ # Return a new Wx::Point with the x and y parameters both divided by
8
+ # parameter +div+, which should be a Numeric
9
+ def /(div)
10
+ self.class.new( (get_x / div).to_i, (get_y / div).to_i )
11
+ end
12
+
13
+ # Return a new Wx::Point with the x and y values both multiplied by
14
+ # parameter +mul+, which should be a Numeric
15
+ def *(mul)
16
+ self.class.new( (get_x * mul).to_i, (get_y * mul).to_i )
17
+ end
18
+
19
+ # Return a new Wx::Point with the x and y values both reduced by
20
+ # parameter +arg+. If +arg+ is another Wx::Point, reduce x by the
21
+ # other's x and y by the other's y; if +arg+ is a numeric value,
22
+ # reduce x and y both by that value.
23
+ def -(arg)
24
+ case arg
25
+ when self.class
26
+ self.class.new( get_x - arg.get_x, get_y - arg.get_y )
27
+ when Numeric
28
+ self.class.new( (get_x - arg).to_i, (get_y - arg).to_i )
29
+ else
30
+ Kernel.raise TypeError, "Cannot add #{arg} to #{self.inspect}"
31
+ end
32
+ end
33
+
34
+ # Return a new Wx::Point with the x and y values both increased by
35
+ # parameter +arg+. If +arg+ is another Wx::Point, increase x by the
36
+ # other's x and y by the other's y; if +arg+ is a numeric value,
37
+ # increase both x and y by that value.
38
+ def +(arg)
39
+ case arg
40
+ when self.class
41
+ self.class.new( get_x + arg.get_x, get_y + arg.get_y )
42
+ when Numeric
43
+ self.class.new( (get_x + arg).to_i, (get_y + arg).to_i )
44
+ else
45
+ Kernel.raise TypeError, "Cannot add #{arg} to #{self.inspect}"
46
+ end
47
+ end
5
48
  end
@@ -2,4 +2,48 @@ class Wx::Size
2
2
  def to_s
3
3
  "#<Wx::Size: (#{get_width}, #{get_height})>"
4
4
  end
5
+
6
+ # Return a new Wx::Size with the width and height values both divided
7
+ # by parameter +div+, which should be a Numeric
8
+ def /(div)
9
+ self.class.new( (get_x / div).to_i, (get_y / div).to_i )
10
+ end
11
+
12
+ # Return a new Wx::Size with the width and height values both
13
+ # multiplied by parameter +mul+, which should be a Numeric
14
+ def *(mul)
15
+ self.class.new( (get_x * mul).to_i, (get_y * mul).to_i )
16
+ end
17
+
18
+ # Return a new Wx::Size with the width and height parameters both
19
+ # reduced by parameter +arg+. If +arg+ is another Wx::Size, reduce
20
+ # width by the other's width and height by the other's height; if
21
+ # +arg+ is a numeric value, reduce both width and height by that
22
+ # value.
23
+ def -(arg)
24
+ case arg
25
+ when self.class
26
+ self.class.new( get_x - arg.get_x, get_y - arg.get_y )
27
+ when Numeric
28
+ self.class.new( (get_x - arg).to_i, (get_y - arg).to_i )
29
+ else
30
+ Kernel.raise TypeError, "Cannot add #{arg} to #{self.inspect}"
31
+ end
32
+ end
33
+
34
+ # Return a new Wx::Size with the width and height parameters both
35
+ # increased by parameter +arg+. If +arg+ is another Wx::Size, increase
36
+ # width by the other's width and height by the other's height; if
37
+ # +arg+ is a numeric value, increase both width and height by that
38
+ # value.
39
+ def +(arg)
40
+ case arg
41
+ when self.class
42
+ self.class.new( get_x + arg.get_x, get_y + arg.get_y )
43
+ when Numeric
44
+ self.class.new( (get_x + arg).to_i, (get_y + arg).to_i )
45
+ else
46
+ Kernel.raise TypeError, "Cannot add #{arg} to #{self.inspect}"
47
+ end
48
+ end
5
49
  end
@@ -0,0 +1,92 @@
1
+ # Functionality here must be loaded first to add custom events
2
+ require 'wx/classes/evthandler'
3
+
4
+ # These event type constants will only be available and meaningful if
5
+ # Wx::StyledTextCtrl has been compiled into the library. If so, they
6
+ # need the below definitions for mapping, otherwise all the rest should
7
+ # be skipped
8
+ if defined?(Wx::StyledTextCtrl)
9
+ EventType = Wx::EvtHandler::EventType
10
+
11
+ STC_EVENT_TYPES = [
12
+ EventType['evt_stc_calltip_click', 1,
13
+ Wx::EVT_STC_CALLTIP_CLICK,
14
+ Wx::StyledTextEvent],
15
+ EventType['evt_stc_change', 1,
16
+ Wx::EVT_STC_CHANGE,
17
+ Wx::StyledTextEvent],
18
+ EventType['evt_stc_charadded', 1,
19
+ Wx::EVT_STC_CHARADDED,
20
+ Wx::StyledTextEvent],
21
+ EventType['evt_stc_doubleclick', 1,
22
+ Wx::EVT_STC_DOUBLECLICK,
23
+ Wx::StyledTextEvent],
24
+ EventType['evt_stc_do_drop', 1,
25
+ Wx::EVT_STC_DO_DROP,
26
+ Wx::StyledTextEvent],
27
+ EventType['evt_stc_drag_over', 1,
28
+ Wx::EVT_STC_DRAG_OVER,
29
+ Wx::StyledTextEvent],
30
+ EventType['evt_stc_dwellend', 1,
31
+ Wx::EVT_STC_DWELLEND,
32
+ Wx::StyledTextEvent],
33
+ EventType['evt_stc_dwellstart', 1,
34
+ Wx::EVT_STC_DWELLSTART,
35
+ Wx::StyledTextEvent],
36
+ EventType['evt_stc_hotspot_click', 1,
37
+ Wx::EVT_STC_HOTSPOT_CLICK,
38
+ Wx::StyledTextEvent],
39
+ EventType['evt_stc_hotspot_dclick', 1,
40
+ Wx::EVT_STC_HOTSPOT_DCLICK,
41
+ Wx::StyledTextEvent],
42
+ EventType['evt_stc_key', 1,
43
+ Wx::EVT_STC_KEY,
44
+ Wx::StyledTextEvent],
45
+ EventType['evt_stc_macrorecord', 1,
46
+ Wx::EVT_STC_MACRORECORD,
47
+ Wx::StyledTextEvent],
48
+ EventType['evt_stc_marginclick', 1,
49
+ Wx::EVT_STC_MARGINCLICK,
50
+ Wx::StyledTextEvent],
51
+ EventType['evt_stc_modified', 1,
52
+ Wx::EVT_STC_MODIFIED,
53
+ Wx::StyledTextEvent],
54
+ EventType['evt_stc_needshown', 1,
55
+ Wx::EVT_STC_NEEDSHOWN,
56
+ Wx::StyledTextEvent],
57
+ EventType['evt_stc_painted', 1,
58
+ Wx::EVT_STC_PAINTED,
59
+ Wx::StyledTextEvent],
60
+ EventType['evt_stc_romodifyattempt', 1,
61
+ Wx::EVT_STC_ROMODIFYATTEMPT,
62
+ Wx::StyledTextEvent],
63
+ EventType['evt_stc_savepointleft', 1,
64
+ Wx::EVT_STC_SAVEPOINTLEFT,
65
+ Wx::StyledTextEvent],
66
+ EventType['evt_stc_savepointreached', 1,
67
+ Wx::EVT_STC_SAVEPOINTREACHED,
68
+ Wx::StyledTextEvent],
69
+ EventType['evt_stc_start_drag', 1,
70
+ Wx::EVT_STC_START_DRAG,
71
+ Wx::StyledTextEvent],
72
+ EventType['evt_stc_styleneeded', 1,
73
+ Wx::EVT_STC_STYLENEEDED,
74
+ Wx::StyledTextEvent],
75
+ EventType['evt_stc_updateui', 1,
76
+ Wx::EVT_STC_UPDATEUI,
77
+ Wx::StyledTextEvent],
78
+ EventType['evt_stc_uridropped', 1,
79
+ Wx::EVT_STC_URIDROPPED,
80
+ Wx::StyledTextEvent],
81
+ EventType['evt_stc_userlistselection', 1,
82
+ Wx::EVT_STC_USERLISTSELECTION,
83
+ Wx::StyledTextEvent],
84
+ EventType['evt_stc_zoom', 1,
85
+ Wx::EVT_STC_ZOOM,
86
+ Wx::StyledTextEvent]
87
+ ]
88
+
89
+ STC_EVENT_TYPES.each do | ev_type |
90
+ Wx::EvtHandler.register_event_type(ev_type)
91
+ end
92
+ end
@@ -0,0 +1,14 @@
1
+ class Wx::TextCtrl
2
+ # Fix position_to_xy so it returns a two-element array - the internal
3
+ # version returns a three-element array with a Boolean that doesn't
4
+ # really make sense in Ruby
5
+ wx_position_to_xy = instance_method(:position_to_xy)
6
+ define_method(:position_to_xy) do | pos |
7
+ retval, x, y = wx_position_to_xy.bind(self).call(pos)
8
+ if retval
9
+ return [x, y]
10
+ else
11
+ return nil
12
+ end
13
+ end
14
+ end
@@ -2,6 +2,10 @@
2
2
  # released under the MIT-style wxruby2 license
3
3
 
4
4
  class Wx::Window
5
+ # Create a wx-specific name for get_id, to prevent confusion with
6
+ # ruby's (deprecated) Object#id
7
+ alias :wx_id :get_id
8
+
5
9
  # Recursively searches all windows below +self+ and returns the first
6
10
  # window which has the id +an_id+. This corresponds to the find_window
7
11
  # method method in WxWidgets when called with an integer.
@@ -0,0 +1,219 @@
1
+ # = WxRuby Extensions - Keyword Constructors
2
+ #
3
+ # The *Keyword Constructors* extension allows the use of Ruby hash-style
4
+ # keyword arguments in constructors of common WxWidgets Windows, Frame,
5
+ # Dialog and Control classes.
6
+ #
7
+ # == Introduction
8
+ #
9
+ # Building a GUI in WxWidgets involves lots of calls to +new+, but
10
+ # these methods often have long parameter lists. Often the default
11
+ # values for many of these parameters are correct. For example, if
12
+ # you're using a sizer-based layout, you usually don't want to specify a
13
+ # size for widgets, but you still have to type
14
+ #
15
+ # Wx::TreeCtrl.new( parent, -1, Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE,
16
+ # Wx::NO_BUTTONS )
17
+ #
18
+ # just to create a standard TreeCtrl with the 'no buttons' style. If you
19
+ # want to specify the 'NO BUTTONS' style, you can't avoid all the typing
20
+ # of DEFAULT_POSITION etc.
21
+ #
22
+ # == Basic Keyword Constructors
23
+ #
24
+ # With keyword_constructors, you could write the above as
25
+ #
26
+ # TreeCtrl.new(parent, :style => Wx::NO_BUTTONS)
27
+ #
28
+ # And it will assume you want the default id (-1), and the default size
29
+ # and position. If you want to specify an explicit size, you can do so:
30
+ #
31
+ # TreeCtrl.new(parent, :size => Wx::Size.new(100, 300))
32
+ #
33
+ # For brevity, this module also allows you to specify positions and
34
+ # sizes using a a two-element array:
35
+ #
36
+ # TreeCtrl.new(parent, :size => [100, 300])
37
+ #
38
+ # Similarly with position:
39
+ #
40
+ # TreeCtrl.new(parent, :pos => Wx::Point.new(5, 25))
41
+ #
42
+ # TreeCtrl.new(parent, :pos => [5, 25])
43
+ #
44
+ # You can have multiple keyword arguments:
45
+ #
46
+ # TreeCtrl.new(parent, :pos => [5, 25], :size => [100, 300] )
47
+ #
48
+ # == No ID required
49
+ #
50
+ # As with position and size, you usually don't want to deal with
51
+ # assigning unique ids to every widget and frame you create - it's a C++
52
+ # hangover that often seems clunky in Ruby. The *Event Connectors*
53
+ # extension allows you to set up event handling without having to use
54
+ # ids, and if no :id argument is supplied to a constructor, the default
55
+ # (-1) will be passed.
56
+ #
57
+ # There are occasions when a specific ID does need to be used - for
58
+ # example, to tell WxWidgets that a button is a 'stock' item, so that it
59
+ # can be displayed using platform-standard text and icon. To do this,
60
+ # simply pass an :id argument to the constructor - here, the system's
61
+ # standard 'preview' button
62
+ #
63
+ # Wx::Button.new(parent, :id => Wx::ID_PREVIEW)
64
+ #
65
+ # == Class-specific arguments
66
+ #
67
+ # The arguments :size, :pos and :style are common to many WxWidgets
68
+ # window classes. The +new+ methods of these classes also have
69
+ # parameters that are specific to those classes; for example, the text
70
+ # label on a button, or the initial value of a text control.
71
+ #
72
+ # Wx::Button.new(parent, :label => 'press me')
73
+ # Wx::TextCtrl.new(parent, :value => 'type some text here')
74
+ #
75
+ # The keyword names of these arguments can be found by looking at the
76
+ # WxRuby documentation, in the relevant class's +new+ method. You can
77
+ # also get a string description of the class's +new+ method parameters
78
+ # within Ruby by doing:
79
+ #
80
+ # puts Wx::TextCtrl.describe_constructor()
81
+ #
82
+ # This will print a list of the argument names expected by the class's
83
+ # +new+ method, and the correct type for them.
84
+ #
85
+ # == Mixing positional and keyword arguments
86
+ #
87
+ # To support existing code, and to avoid forcing the use of more verbose
88
+ # keyword-style arguments where they're not desired, you can mix
89
+ # positional and keyword arguments, omitting or including +id+s as
90
+ # desired.
91
+ #
92
+ # Wx::Button.new(parent, 'press me', :style => Wx::BU_RIGHT)
93
+
94
+ module Wx
95
+ module KeywordConstructor
96
+ module ClassMethods
97
+
98
+ # Common Wx constructor argument keywords, with their default values.
99
+ STANDARD_DEFAULTS = {
100
+ :id => -1,
101
+ :size => Wx::DEFAULT_SIZE,
102
+ :pos => Wx::DEFAULT_POSITION,
103
+ :style => 0,
104
+ :validator => Wx::DEFAULT_VALIDATOR,
105
+ :choices => [] # for Choice, ComboBox etc
106
+ }
107
+
108
+
109
+ # A named parameter in a Wx constructor parameter list
110
+ Parameter = Struct.new( :name, :default )
111
+
112
+ attr_writer :param_spec
113
+ def param_spec
114
+ @param_spec ||= []
115
+ end
116
+
117
+ attr_writer :param_flags
118
+ def param_flags
119
+ @param_flags ||= {}
120
+ end
121
+
122
+
123
+ # Adds a list of named parameters *params* to the parameter
124
+ # specification for this Wx class's constructor. Each parameter
125
+ # should be specified as a either a common known symbol, such as
126
+ # +:size+ or +:pos:+ or +:style:+ (corresponding to the common
127
+ # constructor arguments in WxWidgets API), or a single-key with the
128
+ # key the name of the argument, and the value a default value.
129
+ #
130
+ # Parameters should be specified in the order they occur in the Wx
131
+ # API constructor
132
+ def wx_ctor_params(*params)
133
+ self.param_spec += params.map do | param |
134
+ param.kind_of?(Hash) ? Parameter[*param.to_a.flatten] :
135
+ Parameter[param, STANDARD_DEFAULTS[param] ]
136
+ end
137
+ end
138
+
139
+ def args_as_list(*mixed_args)
140
+ # get keyword arguments from mixed args if supplied, else empty
141
+ kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
142
+ out_args = []
143
+ param_spec.zip(mixed_args) do | param, arg |
144
+ if arg # use the supplied list arg
145
+ out_args << arg
146
+ elsif kwa.key?(param.name) # use the keyword arg
147
+ out_args << kwa[param.name]
148
+ else # use the default argument
149
+ out_args << param.default
150
+ end
151
+ end
152
+ out_args
153
+ rescue
154
+ Kernel.raise ArgumentError,
155
+ "Bad arg composition of #{mixed_args.inspect}"
156
+ end
157
+
158
+ def args_as_hash(*mixed_args)
159
+ kwa = mixed_args.last.kind_of?(Hash) ? mixed_args.pop : {}
160
+ param_spec.zip(mixed_args) do | param, arg |
161
+ kwa[param.name] = arg if arg
162
+ end
163
+ kwa
164
+ end
165
+
166
+ def describe_constructor()
167
+ param_spec.inject("") do | desc, param |
168
+ desc << "#{param.name} (#{param.default.class.name})\n"
169
+ end
170
+ end
171
+ end
172
+
173
+ def self.included(klass)
174
+ klass.extend ClassMethods
175
+ klass.module_eval do
176
+
177
+ alias :pre_wx_kwctor_init :initialize
178
+
179
+ def initialize(parent = :default_ctor, *mixed_args)
180
+ # allow zero-args ctor for use with XRC
181
+ if parent == :default_ctor
182
+ pre_wx_kwctor_init()
183
+ return
184
+ end
185
+
186
+ # Allow classes to ignore :id argument in positional args
187
+ unless self.class < Wx::Dialog
188
+ if not mixed_args[0].kind_of?(Fixnum)
189
+ mixed_args.unshift(-1)
190
+ end
191
+ end
192
+
193
+ real_args = [ parent ] + self.class.args_as_list(*mixed_args)
194
+ begin
195
+ pre_wx_kwctor_init(*real_args)
196
+ rescue
197
+ msg = "Error initializing #{self.inspect} \n" +
198
+ "Sent parameters: #{real_args.inspect}\n" +
199
+ "Correct parameters are:\n" +
200
+ self.class.describe_constructor()
201
+ Kernel.raise ArgumentError, msg
202
+ end
203
+
204
+ yield self if block_given?
205
+ end
206
+ end
207
+
208
+ # Any class inheriting from a class including this module must have
209
+ # its own copy of the param_spec
210
+ def klass.inherited(sub_klass)
211
+ sub_klass.instance_variable_set(:@param_spec,
212
+ instance_variable_get(:@param_spec) )
213
+ sub_klass.instance_variable_set(:@param_flags,
214
+ instance_variable_get(:@param_flags) )
215
+ end
216
+ end
217
+ end
218
+ end
219
+