wxruby 1.9.1-powerpc-darwin8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (204) hide show
  1. data/lib/wx.rb +42 -0
  2. data/lib/wx/accessors.rb +52 -0
  3. data/lib/wx/classes/app.rb +25 -0
  4. data/lib/wx/classes/artprovider.rb +31 -0
  5. data/lib/wx/classes/bitmap.rb +23 -0
  6. data/lib/wx/classes/choice.rb +4 -0
  7. data/lib/wx/classes/clientdc.rb +13 -0
  8. data/lib/wx/classes/colour.rb +47 -0
  9. data/lib/wx/classes/combobox.rb +4 -0
  10. data/lib/wx/classes/evthandler.rb +828 -0
  11. data/lib/wx/classes/font.rb +118 -0
  12. data/lib/wx/classes/grid.rb +130 -0
  13. data/lib/wx/classes/helpcontroller.rb +5 -0
  14. data/lib/wx/classes/htmlhelpcontroller.rb +5 -0
  15. data/lib/wx/classes/htmlwindow.rb +6 -0
  16. data/lib/wx/classes/icon.rb +14 -0
  17. data/lib/wx/classes/image.rb +14 -0
  18. data/lib/wx/classes/listbox.rb +4 -0
  19. data/lib/wx/classes/locale.rb +28 -0
  20. data/lib/wx/classes/object.rb +7 -0
  21. data/lib/wx/classes/paintdc.rb +12 -0
  22. data/lib/wx/classes/point.rb +48 -0
  23. data/lib/wx/classes/previewframe.rb +13 -0
  24. data/lib/wx/classes/rect.rb +5 -0
  25. data/lib/wx/classes/size.rb +49 -0
  26. data/lib/wx/classes/styledtextctrl.rb +92 -0
  27. data/lib/wx/classes/textctrl.rb +14 -0
  28. data/lib/wx/classes/texturlevent.rb +6 -0
  29. data/lib/wx/classes/timer.rb +69 -0
  30. data/lib/wx/classes/window.rb +42 -0
  31. data/lib/wx/classes/xmlresource.rb +16 -0
  32. data/lib/wx/keyword_ctors.rb +219 -0
  33. data/lib/wx/keyword_defs.rb +455 -0
  34. data/lib/wx/version.rb +3 -0
  35. data/lib/wxruby2.bundle +0 -0
  36. data/samples/SAMPLES-LICENSE.TXT +18 -0
  37. data/samples/aui/aui.rb +1353 -0
  38. data/samples/bigdemo/About.rbw +39 -0
  39. data/samples/bigdemo/ColorPanel.rbw +25 -0
  40. data/samples/bigdemo/GridSimple.rbw +80 -0
  41. data/samples/bigdemo/MDIDemo.rbw +59 -0
  42. data/samples/bigdemo/PopupMenu.rbw +151 -0
  43. data/samples/bigdemo/ShapedWindow.rbw +135 -0
  44. data/samples/bigdemo/Sizers.rbw +545 -0
  45. data/samples/bigdemo/bigdemo.rb +826 -0
  46. data/samples/bigdemo/demoTemplate.rbw +37 -0
  47. data/samples/bigdemo/helpfile.htb +0 -0
  48. data/samples/bigdemo/icons/Test 015.jpg +0 -0
  49. data/samples/bigdemo/icons/Test 015.png +0 -0
  50. data/samples/bigdemo/icons/choice.bmp +0 -0
  51. data/samples/bigdemo/icons/choice.xpm +27 -0
  52. data/samples/bigdemo/icons/combo.bmp +0 -0
  53. data/samples/bigdemo/icons/combo.xpm +27 -0
  54. data/samples/bigdemo/icons/copy.xpm +25 -0
  55. data/samples/bigdemo/icons/cut.xpm +24 -0
  56. data/samples/bigdemo/icons/gauge.bmp +0 -0
  57. data/samples/bigdemo/icons/gauge.xpm +27 -0
  58. data/samples/bigdemo/icons/help.xpm +25 -0
  59. data/samples/bigdemo/icons/list.bmp +0 -0
  60. data/samples/bigdemo/icons/list.xpm +27 -0
  61. data/samples/bigdemo/icons/mondrian.ico +0 -0
  62. data/samples/bigdemo/icons/mondrian.xpm +44 -0
  63. data/samples/bigdemo/icons/new.xpm +24 -0
  64. data/samples/bigdemo/icons/ogl.ico +0 -0
  65. data/samples/bigdemo/icons/ogl.xpm +45 -0
  66. data/samples/bigdemo/icons/open.xpm +26 -0
  67. data/samples/bigdemo/icons/paste.bmp +0 -0
  68. data/samples/bigdemo/icons/paste.xpm +38 -0
  69. data/samples/bigdemo/icons/pointy.png +0 -0
  70. data/samples/bigdemo/icons/preview.xpm +26 -0
  71. data/samples/bigdemo/icons/print.xpm +26 -0
  72. data/samples/bigdemo/icons/radio.bmp +0 -0
  73. data/samples/bigdemo/icons/radio.xpm +27 -0
  74. data/samples/bigdemo/icons/robert.xpm +415 -0
  75. data/samples/bigdemo/icons/ruby.png +0 -0
  76. data/samples/bigdemo/icons/sashtest.ico +0 -0
  77. data/samples/bigdemo/icons/save.xpm +25 -0
  78. data/samples/bigdemo/icons/smiles.bmp +0 -0
  79. data/samples/bigdemo/icons/smiles.xpm +39 -0
  80. data/samples/bigdemo/icons/smiley.ico +0 -0
  81. data/samples/bigdemo/icons/smiley.xpm +42 -0
  82. data/samples/bigdemo/icons/stattext.xpm +24 -0
  83. data/samples/bigdemo/icons/test2.bmp +0 -0
  84. data/samples/bigdemo/icons/test2.png +0 -0
  85. data/samples/bigdemo/icons/test2.xpm +79 -0
  86. data/samples/bigdemo/icons/text.bmp +0 -0
  87. data/samples/bigdemo/icons/text.xpm +27 -0
  88. data/samples/bigdemo/icons/tog1.bmp +0 -0
  89. data/samples/bigdemo/icons/tog1.xpm +38 -0
  90. data/samples/bigdemo/icons/tog2.bmp +0 -0
  91. data/samples/bigdemo/icons/tog2.xpm +38 -0
  92. data/samples/bigdemo/icons/wxwin.ico +0 -0
  93. data/samples/bigdemo/icons/wxwin16x16.png +0 -0
  94. data/samples/bigdemo/icons/wxwin16x16.xpm +25 -0
  95. data/samples/bigdemo/icons/wxwin32x32.png +0 -0
  96. data/samples/bigdemo/icons/wxwin48x48.png +0 -0
  97. data/samples/bigdemo/run.rb +94 -0
  98. data/samples/bigdemo/tips.txt +7 -0
  99. data/samples/bigdemo/utils.rb +12 -0
  100. data/samples/bigdemo/wxArtProvider.rbw +285 -0
  101. data/samples/bigdemo/wxBitmapButton.rbw +64 -0
  102. data/samples/bigdemo/wxButton.rbw +66 -0
  103. data/samples/bigdemo/wxCalendarCtrl.rbw +72 -0
  104. data/samples/bigdemo/wxCheckBox.rbw +52 -0
  105. data/samples/bigdemo/wxCheckListBox.rbw +77 -0
  106. data/samples/bigdemo/wxChoice.rbw +49 -0
  107. data/samples/bigdemo/wxChoicebook.rbw +80 -0
  108. data/samples/bigdemo/wxColourDialog.rbw +34 -0
  109. data/samples/bigdemo/wxComboBox.rbw +79 -0
  110. data/samples/bigdemo/wxCursor.rbw +140 -0
  111. data/samples/bigdemo/wxDialog.rbw +92 -0
  112. data/samples/bigdemo/wxDirDialog.rbw +32 -0
  113. data/samples/bigdemo/wxDragImage.rbw +74 -0
  114. data/samples/bigdemo/wxFileDialog.rbw +39 -0
  115. data/samples/bigdemo/wxFileDialog_Save.rbw +38 -0
  116. data/samples/bigdemo/wxFindReplaceDialog.rbw +85 -0
  117. data/samples/bigdemo/wxFontDialog.rbw +176 -0
  118. data/samples/bigdemo/wxFrame.rbw +55 -0
  119. data/samples/bigdemo/wxGauge.rbw +73 -0
  120. data/samples/bigdemo/wxGenericDirCtrl.rbw +78 -0
  121. data/samples/bigdemo/wxGrid.rbw +68 -0
  122. data/samples/bigdemo/wxHtmlHelpController.rbw +57 -0
  123. data/samples/bigdemo/wxListBox.rbw +142 -0
  124. data/samples/bigdemo/wxListCtrl_virtual.rbw +109 -0
  125. data/samples/bigdemo/wxMDIWindows.rbw +52 -0
  126. data/samples/bigdemo/wxMenu.rbw +238 -0
  127. data/samples/bigdemo/wxMessageDialog.rbw +30 -0
  128. data/samples/bigdemo/wxMiniFrame.rbw +74 -0
  129. data/samples/bigdemo/wxMultipleChoiceDialog.rbw +34 -0
  130. data/samples/bigdemo/wxNotebook.rbw +138 -0
  131. data/samples/bigdemo/wxProgressDialog.rbw +45 -0
  132. data/samples/bigdemo/wxRadioBox.rbw +74 -0
  133. data/samples/bigdemo/wxRadioButton.rbw +127 -0
  134. data/samples/bigdemo/wxSashWindow.rbw +155 -0
  135. data/samples/bigdemo/wxScrolledMessageDialog.rbw +59 -0
  136. data/samples/bigdemo/wxScrolledWindow.rbw +196 -0
  137. data/samples/bigdemo/wxSingleChoiceDialog.rbw +35 -0
  138. data/samples/bigdemo/wxSlider.rbw +44 -0
  139. data/samples/bigdemo/wxSpinButton.rbw +52 -0
  140. data/samples/bigdemo/wxSpinCtrl.rbw +53 -0
  141. data/samples/bigdemo/wxSplitterWindow.rbw +65 -0
  142. data/samples/bigdemo/wxStaticBitmap.rbw +53 -0
  143. data/samples/bigdemo/wxStaticText.rbw +57 -0
  144. data/samples/bigdemo/wxStatusBar.rbw +128 -0
  145. data/samples/bigdemo/wxTextCtrl.rbw +151 -0
  146. data/samples/bigdemo/wxTextEntryDialog.rbw +34 -0
  147. data/samples/bigdemo/wxToggleButton.rbw +51 -0
  148. data/samples/bigdemo/wxToolBar.rbw +133 -0
  149. data/samples/bigdemo/wxTreeCtrl.rbw +192 -0
  150. data/samples/calendar/calendar.rb +275 -0
  151. data/samples/caret/caret.rb +296 -0
  152. data/samples/caret/mondrian.xpm +44 -0
  153. data/samples/controls/controls.rb +1140 -0
  154. data/samples/controls/get_item_sample.rb +87 -0
  155. data/samples/controls/icons/choice.xpm +27 -0
  156. data/samples/controls/icons/combo.xpm +27 -0
  157. data/samples/controls/icons/gauge.xpm +27 -0
  158. data/samples/controls/icons/list.xpm +27 -0
  159. data/samples/controls/icons/radio.xpm +27 -0
  160. data/samples/controls/icons/stattext.xpm +24 -0
  161. data/samples/controls/icons/text.xpm +27 -0
  162. data/samples/controls/mondrian.ico +0 -0
  163. data/samples/controls/mondrian.xpm +44 -0
  164. data/samples/controls/test2.bmp +0 -0
  165. data/samples/dialogs/dialogs.rb +724 -0
  166. data/samples/dialogs/tips.txt +18 -0
  167. data/samples/etc/activation.rb +108 -0
  168. data/samples/etc/choice.rb +72 -0
  169. data/samples/etc/miniframe.rb +84 -0
  170. data/samples/etc/sash.rb +135 -0
  171. data/samples/etc/scrollwin.rb +116 -0
  172. data/samples/etc/system_settings.rb +258 -0
  173. data/samples/etc/wizard.rb +81 -0
  174. data/samples/event/event.rb +184 -0
  175. data/samples/grid/grid.rb +201 -0
  176. data/samples/html/html.rb +251 -0
  177. data/samples/images/Thumbs.db +0 -0
  178. data/samples/images/images.rb +48 -0
  179. data/samples/images/paperclip.png +0 -0
  180. data/samples/listbook/listbook.rb +183 -0
  181. data/samples/listbook/listbook.xrc +370 -0
  182. data/samples/mdi/mdi.rb +87 -0
  183. data/samples/minimal/minimal.rb +83 -0
  184. data/samples/minimal/mondrian.png +0 -0
  185. data/samples/minimal/nothing.rb +46 -0
  186. data/samples/minimal/text.rb +35 -0
  187. data/samples/printing/mondrian.ico +0 -0
  188. data/samples/printing/mondrian.xpm +44 -0
  189. data/samples/printing/printing.rb +484 -0
  190. data/samples/text/mondrian.ico +0 -0
  191. data/samples/text/mondrian.xpm +44 -0
  192. data/samples/text/scintilla.rb +174 -0
  193. data/samples/text/textctrl.rb +124 -0
  194. data/samples/text/unicode.rb +238 -0
  195. data/samples/text/utf8.txt +15 -0
  196. data/samples/treectrl/icon1.xpm +79 -0
  197. data/samples/treectrl/icon2.xpm +53 -0
  198. data/samples/treectrl/icon3.xpm +79 -0
  199. data/samples/treectrl/icon4.xpm +43 -0
  200. data/samples/treectrl/icon5.xpm +79 -0
  201. data/samples/treectrl/treectrl.rb +1210 -0
  202. data/samples/xrc/samples.xrc +46 -0
  203. data/samples/xrc/xrc_sample.rb +107 -0
  204. metadata +263 -0
@@ -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
@@ -0,0 +1,6 @@
1
+ class Wx::TextUrlEvent
2
+ # Returns the string URL that is being interacted with in this event
3
+ def get_url
4
+ get_event_object.get_value[get_url_start .. get_url_end]
5
+ end
6
+ end
@@ -0,0 +1,69 @@
1
+ # Class allowing periodic or timed events to be fired
2
+ class Wx::Timer
3
+ # This class can be linked to an owner - an instance of a class
4
+ # derived from EvtHandler which will receive Timer events. However,
5
+ # event if a Wx::Timer is attached to a Wx::Window, it is (unlike most
6
+ # classes) NOT automatically deleted when the window is destroyed. If
7
+ # the Timer continues ticking, it will send events to the
8
+ # now-destroyed window, causing segfaults. So the little acrobatics
9
+ # below set up a hook when a Timer's owner is set, and then ensure the
10
+ # timer is stopped when the window is destroyed.
11
+
12
+ # Redefine initialize
13
+ wx_init = self.instance_method(:initialize)
14
+ define_method(:initialize) do | *args |
15
+ setup_owner_destruction_hook(args[0])
16
+ wx_init.bind(self).call(*args)
17
+ end
18
+
19
+ # Redefine set_owner
20
+ wx_set_owner = self.instance_method(:set_owner)
21
+ define_method(:set_owner) do | *args |
22
+ setup_owner_destruction_hook(args[0])
23
+ wx_set_owner.bind(self).call(*args)
24
+ end
25
+
26
+ private
27
+ # This method notes in Ruby the ownership of the timer, from both
28
+ # sides, and sets up an event hook if needed for the window's
29
+ # destruction.
30
+ def setup_owner_destruction_hook(new_owner)
31
+ this_timer = self
32
+
33
+ # Class-wide list of global (unowned) timers
34
+ @@__unowned_timers__ ||= []
35
+
36
+ # remove from list of previous owner
37
+ if @__owner__
38
+ @__owner__.instance_eval { @__owned_timers__.delete(this_timer) }
39
+ end
40
+
41
+ # If becoming global unowned timer, add to list of those timers
42
+ if not new_owner
43
+ @__owner__ = nil
44
+ @@__unowned_timers__ << self
45
+ return
46
+ end
47
+
48
+ # Otherwise, if previously unowned, remove from global owned
49
+ @@__unowned_timers__.delete(self)
50
+ @__owner__ = new_owner
51
+
52
+ # Then add to list of new owner, setting destructor hook if required
53
+ new_owner.instance_eval do
54
+ if not @__owned_timers__
55
+ @__owned_timers__ = []
56
+ unless self.kind_of?(Wx::App) # Don't set up hook on App
57
+ evt_window_destroy do | evt |
58
+ # If it's the owning window being destroyed...
59
+ if evt.get_event_object == self
60
+ @__owned_timers__.each { | timer | timer.stop }
61
+ end
62
+ evt.skip
63
+ end
64
+ end
65
+ end
66
+ @__owned_timers__ << this_timer
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright 2004-2007 by Kevin Smith
2
+ # released under the MIT-style wxruby2 license
3
+
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
+
9
+ # Recursively searches all windows below +self+ and returns the first
10
+ # window which has the id +an_id+. This corresponds to the find_window
11
+ # method method in WxWidgets when called with an integer.
12
+ def find_window_by_id(an_id)
13
+ Wx::Window.find_window_by_id(an_id, self)
14
+ end
15
+
16
+ # Searches all windows below +self+ and returns the first window which
17
+ # has the name +a_name+ This corresponds to the find_window method method
18
+ # in WxWidgets when called with an string.
19
+ def find_window_by_name(a_name)
20
+ Wx::Window.find_window_by_name(a_name, self)
21
+ end
22
+
23
+ # Searches all windows below +self+ and returns the first window which
24
+ # has the label +a_label+.
25
+ def find_window_by_label(a_label)
26
+ Wx:Window.find_window_by_label(a_label, self)
27
+ end
28
+
29
+ alias :__old_evt_paint :evt_paint
30
+ # This modified version of evt_paint sets a variable indicating that a
31
+ # paint event is being handled just before running the event
32
+ # handler. This ensures that any call to Window#paint within the
33
+ # handler will supply a Wx::PaintDC (see swig/Window.i).
34
+ def evt_paint(&block)
35
+ wrapped_block = proc do | event |
36
+ instance_variable_set("@__painting__", true)
37
+ block.call(event)
38
+ remove_instance_variable("@__painting__")
39
+ end
40
+ __old_evt_paint(&wrapped_block)
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ class Wx::XmlResource
2
+ # The standard .load method returns a boolean indicating success or
3
+ # failure. Failure might result from bad XML, or a non-existent
4
+ # file. In ruby, in these circumstances, it's more natural to raise an
5
+ # Exception than expect the user to test the return value.
6
+ wx_load = self.instance_method(:load)
7
+ define_method(:load) do | fname |
8
+ result = wx_load.bind(self).call(fname)
9
+ if not result
10
+ Kernel.raise( RuntimeError,
11
+ "Failed to load XRC from '#{fname}'; " +
12
+ "check the file exists and is valid XML")
13
+ end
14
+ fname
15
+ end
16
+ end
@@ -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
+