wx_sugar 0.1.19 → 0.1.20

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.
@@ -66,10 +66,13 @@ module Arranger
66
66
  return @current_sizer
67
67
  else
68
68
  begin
69
- return self.get_sizer
70
- rescue RuntimeError
69
+ if @current_sizer = self.get_sizer
70
+ return @current_sizer
71
+ end
72
+ rescue RuntimeError # Some wxRuby versions would error on get_sizer
71
73
  end
72
74
  end
75
+
73
76
  @current_sizer = Wx::BoxSizer.new(Wx::VERTICAL)
74
77
  self.set_sizer(@current_sizer)
75
78
  @current_sizer
@@ -1,3 +1,3 @@
1
1
  module WxSugar
2
- VERSION = '0.1.19'
2
+ VERSION = '0.1.20'
3
3
  end
@@ -4,56 +4,56 @@ require 'ostruct'
4
4
  # Provides templated output of a Ruby subclass from an XRC specification
5
5
  # of a ruby Frame, Dialog or Panel class.
6
6
  class Outputter
7
- # Valid options are :app_name and :module_name
8
- attr_accessor :options, :klass
7
+ # Valid options are :app_name and :module_name
8
+ attr_accessor :options, :klass
9
9
 
10
- # Creates a new outputter from the XRCClass +xrc_class+
11
- def initialize(xrc_class, options = {})
12
- @klass = xrc_class
13
- @options = options
14
- end
10
+ # Creates a new outputter from the XRCClass +xrc_class+
11
+ def initialize(xrc_class, options = {})
12
+ @klass = xrc_class
13
+ @options = options
14
+ end
15
15
 
16
- # Writes the ruby code to the file or stream +io+
17
- def output(io = $stdout)
18
- tpl = ERB.new(TEMPLATE)
19
- io.puts( tpl.result(binding) )
20
- end
16
+ # Writes the ruby code to the file or stream +io+
17
+ def output(io = $stdout)
18
+ tpl = ERB.new(TEMPLATE)
19
+ io.puts( tpl.result(binding) )
20
+ end
21
21
 
22
- # Returns the fully-qualified (including module namespace prefix) for
23
- # +name+.
24
- def fq_name(name)
25
- options[:namespace] ? "#{options[:namespace]}::#{name}" : name
26
- end
22
+ # Returns the fully-qualified (including module namespace prefix) for
23
+ # +name+.
24
+ def fq_name(name)
25
+ options[:namespace] ? "#{options[:namespace]}::#{name}" : name
26
+ end
27
27
 
28
- # Returns the options object
29
- def opts
30
- OpenStruct.new(options)
31
- end
28
+ # Returns the options object
29
+ def opts
30
+ OpenStruct.new(options)
31
+ end
32
32
 
33
- # Utility function to format the attr_readers line so it doesn't wrap
34
- # in editors
35
- def clean_id_attr_readers(str, start_len = 15, one_per_line = false)
36
- new_string = ''
37
-
38
- if one_per_line
39
- str.each(',') do | x |
40
- new_string << x
41
- new_string << "\n" + " " * (start_len-2)
42
- end
43
- else
44
- wanted_length = 72
45
- current_line_length = start_len
46
- str.each(',') do |x|
47
- if (current_line_length + x.length) > wanted_length
48
- new_string << "\n" + " " * (start_len-2)
49
- current_line_length = start_len
50
- end
51
- new_string << x
52
- current_line_length += x.length
53
- end
54
- end
55
- return new_string
56
- end
33
+ # Utility function to format the attr_readers line so it doesn't wrap
34
+ # in editors
35
+ def clean_id_attr_readers(str, start_len = 15, one_per_line = false)
36
+ new_string = ''
37
+
38
+ if one_per_line
39
+ str.each(',') do | x |
40
+ new_string << x
41
+ new_string << "\n" + " " * (start_len-2)
42
+ end
43
+ else
44
+ wanted_length = 72
45
+ current_line_length = start_len
46
+ str.each(',') do |x|
47
+ if (current_line_length + x.length) > wanted_length
48
+ new_string << "\n" + " " * (start_len-2)
49
+ current_line_length = start_len
50
+ end
51
+ new_string << x
52
+ current_line_length += x.length
53
+ end
54
+ end
55
+ return new_string
56
+ end
57
57
  end
58
58
 
59
59
 
@@ -72,39 +72,42 @@ end
72
72
  # Generated at: <%= Time.now %>
73
73
 
74
74
  class <%= fq_name(klass.sub_class) %> < <%= klass.superclass %>
75
- <% if not klass.controls.empty? %><% ids = klass.controls.map { | ctrl | ":#{ctrl.name.downcase}" }.join(', ') %>
76
- attr_reader <%= clean_id_attr_readers(ids) %>
77
- <% end %>
78
- def initialize(parent = nil)
79
- super()
80
- xml = Wx::XmlResource.get
81
- xml.flags = 2 # Wx::XRC_NO_SUBCLASSING
82
- xml.init_all_handlers
83
- xml.load("<%= klass.file_name %>")
84
- xml.<%= klass.load_func %>(self, parent, "<%= klass.base_id %>")
75
+ <% if not klass.controls.empty? %><% ids = klass.controls.map { | ctrl | ":#{ctrl.name.downcase}" }.join(', ') %>
76
+ attr_reader <%= clean_id_attr_readers(ids) %>
77
+ <% end %>
78
+ def initialize(parent = nil)
79
+ super()
80
+ xml = Wx::XmlResource.get
81
+ xml.flags = 2 # Wx::XRC_NO_SUBCLASSING
82
+ xml.init_all_handlers
83
+ xml.load("<%= klass.file_name %>")
84
+ xml.<%= klass.load_func %>(self, parent, "<%= klass.base_id %>")
85
85
 
86
- finder = lambda do | x |
87
- int_id = Wx::xrcid(x)
88
- begin
89
- Wx::Window.find_window_by_id(int_id, self) || int_id
90
- # Temporary hack to work around regression in 1.9.2; remove
91
- # begin/rescue clause in later versions
92
- rescue RuntimeError
93
- int_id
94
- end
95
- end
96
- <% klass.controls.each do | ctrl | %>
97
- @<%= ctrl.name.downcase %> = finder.call("<%= ctrl.name %>")<% if ctrl.sub_class %>
98
- @<%= ctrl.name.downcase %>.extend(<%= fq_name(ctrl.sub_class) %>)<% end %><% end %>
99
- end
86
+ finder = lambda do | x |
87
+ int_id = Wx::xrcid(x)
88
+ begin
89
+ Wx::Window.find_window_by_id(int_id, self) || int_id
90
+ # Temporary hack to work around regression in 1.9.2; remove
91
+ # begin/rescue clause in later versions
92
+ rescue RuntimeError
93
+ int_id
94
+ end
95
+ end
96
+ <% klass.controls.each do | ctrl | %>
97
+ @<%= ctrl.name.downcase %> = finder.call("<%= ctrl.name %>")<% if ctrl.sub_class %>
98
+ @<%= ctrl.name.downcase %>.extend(<%= fq_name(ctrl.sub_class) %>)<% end %><% end %>
99
+ if self.class.method_defined? "on_init"
100
+ self.on_init()
101
+ end
102
+ end
100
103
  end
101
104
 
102
105
  <% if opts.app_name %>
103
106
  class <%= opts.app_name %> < Wx::App
104
- def on_init
105
- f = <%= opts.module_name %>::<%= fq_name(klass.sub_class) %>.new
106
- f.show
107
- end
107
+ def on_init
108
+ f = <%= opts.module_name %>::<%= fq_name(klass.sub_class) %>.new
109
+ f.show
110
+ end
108
111
  end
109
112
  <%= opts.app_name %>.new.main_loop
110
113
  <% end %>
@@ -0,0 +1,121 @@
1
+ # container module
2
+ module XRC2Ruby
3
+ # Whether the ruby code output should include hooks for ruby-gettext,
4
+ # used for internationalisation of applications.
5
+ def self.use_gettext
6
+ @gettext
7
+ end
8
+
9
+ # Sets whether gettext hooks should be included.
10
+ def self.use_gettext=(gettext)
11
+ @gettext = gettext
12
+ end
13
+
14
+ # Load definitions that know how to output Ruby code
15
+ requires = %w|object window parent sizers containers toplevelwindows
16
+ static buttons text lists range books menu toolbar|
17
+ requires.each do | req_file |
18
+ require "wx_sugar/xrc/xrc2ruby_types/#{req_file}"
19
+ end
20
+
21
+ # Use REXML stream parsing
22
+ require 'rexml/streamlistener'
23
+ require 'rexml/parsers/baseparser'
24
+ require 'rexml/parsers/streamparser'
25
+
26
+ # The parser goes through the XRC file and identifies top-level
27
+ # frames/dialogs and their child windows. Each window is turned into
28
+ # an instance of an ObjectType class; sizers and sizer settings are
29
+ # also stored. Each ObjectType stores the parameters associated with
30
+ # that specific type of window, and then can later output the wxRuby
31
+ # code to create that window.
32
+ class Parser
33
+ include REXML::StreamListener
34
+ include ObjectTypes
35
+
36
+ # The stack of all objects found - pushed onto when the tag opens,
37
+ # popped off when the tag closes
38
+ attr_reader :stack
39
+
40
+ def initialize
41
+ @stack = []
42
+ end
43
+
44
+ # The most recent parent
45
+ def last_parent
46
+ @stack.reverse.find { | obj | obj.kind_of? Parent }
47
+ end
48
+
49
+ # The most recent sizer
50
+ def last_sizer
51
+ @stack.reverse.find { | obj | obj.kind_of? Sizer }
52
+ end
53
+
54
+ # Process a tag opening
55
+ def tag_start(tag, attrs)
56
+ case tag
57
+ when 'resource'
58
+ # Top-level tag, ignore
59
+ when 'object' # Some kind of object
60
+ case attrs['class']
61
+ # A set of sizer settings
62
+ when 'sizeritem'
63
+ stack.push( SizerItem.new(last_sizer) )
64
+
65
+ # Some "special" object not corresponding to
66
+ when 'panewindow', 'tool', 'separator'
67
+ special = ObjectTypes.const_get(attrs['class'].upcase)
68
+ stack.push( special.new(last_parent, attrs['name']) )
69
+ # An ordinary window
70
+ when /^wx/
71
+ # Look up the correct representation for this object type
72
+ kls = ObjectTypes.const_get(attrs['class'][2..-1])
73
+ # Create and link to parent
74
+ win = kls.new( last_parent,
75
+ attrs["class"],
76
+ attrs["subclass"],
77
+ attrs["name"])
78
+
79
+ if last_parent
80
+ last_parent.add_child(win)
81
+ end
82
+
83
+ # Associate this with the containing sizer item, if there is one
84
+ if stack.last.kind_of?(SizerItem)
85
+ stack.last.window = win
86
+ end
87
+ stack.push(win)
88
+ else
89
+ Kernel.raise "Unexpected object of type '#{attrs['class']}' found"
90
+ end
91
+ else # All other tags are type-specific settings for the current tag
92
+ @next_setting = "#{tag}="
93
+ end
94
+ end
95
+
96
+ # When text is encountered, if non-empty it should be the value for
97
+ # a setting (eg <label> within a wxButton tag)
98
+ def text(text)
99
+ # Ignore empty
100
+ return if text =~ /\A\s*\z/
101
+ if @next_setting
102
+ @stack.last.send(@next_setting, text)
103
+ end
104
+ @next_setting = nil
105
+ end
106
+
107
+ # Process a tag ending
108
+ def tag_end(tag)
109
+ case tag
110
+ when 'object'
111
+ if stack.last.kind_of?(SizerItem)
112
+ last_parent.size_child(@stack.last)
113
+ end
114
+ if stack.length == 1
115
+ puts stack.first.output
116
+ end
117
+ @stack.pop
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module XRC2Ruby::ObjectTypes
2
+
3
+ end
@@ -0,0 +1,40 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Several similar control classes have a standard Window constructor
3
+ # with a text label; they can all share the same class
4
+ class Labelled < Window
5
+ translatable_string_init_arg(:label)
6
+ end
7
+
8
+ # These are the Wx classes that share the constructor
9
+ class Button < Labelled; end
10
+ class ToggleButton < Labelled; end
11
+ class CheckBox < Labelled; end
12
+ class RadioButton < Labelled; end
13
+
14
+ class BitmapButton < Window
15
+ attr_accessor :focus, :selected, :disabled, :hover
16
+ init_arg(:bitmap) do | val |
17
+ "Wx::Bitmap.new('#{val}')"
18
+ end
19
+
20
+ # Call extra methods to set up images for alternate states, if these
21
+ # have been defined in the XRC file.
22
+ def setup
23
+ [ :focus, :selected, :disabled, :hover ].inject('') do | out, extra_img |
24
+ if send(extra_img)
25
+ out << "#{var_name}.bitmap_#{extra_img} = "
26
+ out << " Wx::Bitmap.new('#{send(extra_img)}')\n"
27
+ end
28
+ out
29
+ end
30
+ end
31
+ end
32
+
33
+ class SpinButton < Window
34
+ attr_accessor :min, :max, :value
35
+ def setup
36
+ "#{var_name}.set_range(#{min}, #{max})\n" +
37
+ "#{var_name}.value = #{value}\n"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,105 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ class Container < Window
3
+ include Parent
4
+ end
5
+ # These don't need any special setup
6
+ class Panel < Container; end
7
+ class SashWindow < Container; end
8
+ class SashLayoutWindow < Container; end
9
+
10
+ class ScrolledWindow < Container
11
+ attr_accessor :ppux, :ppuy, :unitsx, :unitsy
12
+ def setup
13
+ "#{var_name}.set_scrollbars(#{ppux}, #{ppuy}, #{unitsx}, #{unitsy})"
14
+ end
15
+ end
16
+
17
+ # This has several options
18
+ class SplitterWindow < Container
19
+ attr_accessor :orientation, :sashpos, :minsize, :gravity
20
+
21
+ def output
22
+ # Fiddly - we have to have created the child windows before we can
23
+ # split them in the
24
+ base = super
25
+
26
+ unless children
27
+ Kernel.raise "SplitterWindow must have children to split"
28
+ end
29
+ child_wins = children.grep(Window)
30
+ if child_wins.length != 2
31
+ Kernel.raise "SplitterWindow must have exactly two children"
32
+ end
33
+
34
+ child_args = "#{child_wins[0].var_name}, #{child_wins[1].var_name}"
35
+ if orientation == 'vertical'
36
+ base << "#{var_name}.split_vertically(#{child_args})\n"
37
+ else
38
+ base << "#{var_name}.split_horizontally(#{child_args})\n"
39
+ end
40
+ base
41
+ end
42
+
43
+ def setup
44
+ setup = ""
45
+ if minsize
46
+ setup << "#{var_name}.minimum_pane_size = #{minsize}\n"
47
+ end
48
+ if sashpos
49
+ setup << "#{var_name}.sash_position = #{sashpos}\n"
50
+ end
51
+ if gravity
52
+ setup << "#{var_name}.sash_gravity = #{gravity}\n"
53
+ end
54
+
55
+ setup
56
+ end
57
+ end
58
+
59
+ class CollapsiblePane < Container
60
+ translatable_string_init_arg(:label)
61
+ attr_accessor :collapsed
62
+ def setup
63
+ if collapsed.to_i.zero?
64
+ "#{var_name}.expand"
65
+ else
66
+ "#{var_name}.collapse"
67
+ end
68
+ end
69
+
70
+ def pane=(pane)
71
+ @children = [pane]
72
+ end
73
+ end
74
+
75
+ # Special class for the interior pane of a CollapsiblePane; not
76
+ # represented by a "real" Window in wxRuby, instead accessed via
77
+ # CollapsiblePane#get_pane
78
+ class PANEWINDOW < Container
79
+ attr_reader :coll_pane
80
+ def initialize(coll_pane, an_id = nil)
81
+ @coll_pane = coll_pane
82
+ @coll_pane.pane = self
83
+ end
84
+
85
+ def var_name
86
+ "#{coll_pane.var_name}.pane"
87
+ end
88
+
89
+ def output
90
+ "\n" + child_output
91
+ end
92
+ end
93
+
94
+ class StatusBar < Container
95
+ attr_accessor :fields, :widths
96
+ def setup
97
+ setup = "#{var_name}.fields_count = #{fields}"
98
+ if widths
99
+ setup << "\n#{var_name}.status_widths = [ #{widths} ]"
100
+ end
101
+ setup << "\n#{parent.var_name}.status_bar = #{var_name}"
102
+ setup
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,45 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Several similar classes with list of strings
3
+ class ControlWithItems < Window
4
+ attr_accessor :selection
5
+
6
+ init_arg(:choices) do | val |
7
+ if XRC2Ruby.use_gettext
8
+ items = val.map { | c | "_(" + c.inspect + ")" }
9
+ else
10
+ items = val.map { | c | c.inspect }
11
+ end
12
+ "[ " + items.join(", ") + " ]"
13
+ end
14
+
15
+ def initialize(*args)
16
+ super
17
+ @choices = []
18
+ end
19
+
20
+ def item=(item)
21
+ @choices << item
22
+ end
23
+
24
+ def setup
25
+ self.selection ? "#{var_name}.selection = #{selection}" : ""
26
+ end
27
+ end
28
+ # The actual Wx classes of this type
29
+ class ListBox < ControlWithItems; end
30
+ class CheckListBox < ControlWithItems; end
31
+ class Choice < ControlWithItems; end
32
+ class ComboBox < ControlWithItems; end
33
+ class BitmapComboBox < ControlWithItems; end
34
+ class ListCtrl < Window; end
35
+
36
+
37
+ class RadioBox < ControlWithItems
38
+ translatable_string_init_arg(:label)
39
+ # The XRC element is called "dimension" but the wxRuby arg is called
40
+ # "major_dimension" - so make interchangeable
41
+ init_arg(:major_dimension) { | val | val }
42
+ alias :dimension :major_dimension
43
+ alias :dimension= :major_dimension=
44
+ end
45
+ end
@@ -0,0 +1,86 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ class MenuBar < Window
3
+ include Parent
4
+ def output
5
+ "#{var_name} = Wx::MenuBar.new\n" +
6
+ child_output +
7
+ "#{parent.var_name}.menu_bar = #{var_name}\n"
8
+ end
9
+ end
10
+
11
+ class Menu < Window
12
+ include Parent
13
+ attr_accessor :label
14
+
15
+ def output
16
+ if XRC2Ruby.use_gettext
17
+ the_label = "_(" + label.inspect + ")"
18
+ else
19
+ the_label = label.inspect
20
+ end
21
+ # A standard menu attached to menubar; NB - call to append must
22
+ # come after all menu items have been created
23
+ if parent.kind_of?(MenuBar)
24
+ "#{var_name} = Wx::Menu.new\n" +
25
+ child_output +
26
+ "#{parent.var_name}.append(#{var_name}, #{the_label})\n"
27
+ # If a submenu
28
+ elsif parent.kind_of?(Menu)
29
+ output_as_submenu(the_label)
30
+ else
31
+ Kernel.raise("Menu shouldn't be attached to a #{parent.class}")
32
+ end
33
+ end
34
+
35
+ def output_as_submenu(label)
36
+
37
+ menu_item = MenuItem.new(parent, 'wxMenuItem', 'wxMenuItem', '')
38
+ menu_item.label = self.label
39
+ menu_item.submenu = self.var_name
40
+ submenu = "#{var_name} = Wx::Menu.new\n" + child_output + menu_item.output
41
+ end
42
+ end
43
+
44
+ class MenuItem < Object
45
+ attr_accessor :label, :checkable, :radio, :checked, :help, :submenu
46
+
47
+ def initialize(parent, klass, subclass, name)
48
+ super
49
+ # stock id
50
+ if name.sub!(/^wx/, "Wx::")
51
+ @menu_id = name
52
+ end
53
+ end
54
+
55
+ def output
56
+ @help ||= ''
57
+
58
+ # What type of menu item?
59
+ if radio
60
+ item_type = "Wx::ITEM_RADIO"
61
+ elsif checkable
62
+ item_type = "Wx::ITEM_CHECK"
63
+ else
64
+ item_type = "Wx::ITEM_NORMAL"
65
+ end
66
+
67
+ # Using a stock id
68
+ if @menu_id
69
+ m_i = "#{var_name} = Wx::MenuItem.new(#{parent.var_name}, #{@menu_id})"
70
+ else
71
+ if XRC2Ruby.use_gettext
72
+ the_label = "_(" + label.inspect + ")"
73
+ the_help = "_(" + help.inspect + ")"
74
+ else
75
+ the_label = label.inspect
76
+ the_help = help.inspect
77
+ end
78
+ the_submenu = submenu || "nil"
79
+ m_i = "#{var_name} = Wx::MenuItem.new(#{parent.var_name}, Wx::ID_ANY,
80
+ #{the_label}, #{the_help},
81
+ #{item_type}, #{the_submenu})"
82
+ end
83
+ m_i + "\n" + "#{parent.var_name}.append_item(#{var_name})"
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,39 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Anything deriving from Wx::Object
3
+ class Object
4
+ attr_accessor :centered
5
+
6
+ def inspect
7
+ "<#{self.class}>"
8
+ end
9
+ def self.next_id
10
+ @id_counter ||= 0
11
+ @id_counter += 1
12
+ end
13
+
14
+ attr_reader :parent, :win_class, :sub_class, :name
15
+ def initialize(parent, win_class, sub_class = nil, var_name = nil)
16
+ @parent = parent
17
+ @win_class = win_class.gsub(/^wx/, "Wx::")
18
+ @sub_class = sub_class
19
+
20
+ # A named and externally accessible item
21
+ if var_name and not var_name.empty? and
22
+ var_name !~ /^ID_/ and var_name !~ /^wx/
23
+ @name = "@#{var_name.downcase}"
24
+ # Or just a local name
25
+ else
26
+ @local_id = Object.next_id
27
+ end
28
+ end
29
+
30
+ def var_name
31
+ @name || "#{self.class.name[/\w+$/].downcase}_#{@local_id}"
32
+ end
33
+
34
+ def output
35
+ # Should be defined in subclasses
36
+ Kernel.raise "Shouldn't be called"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,59 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Module used by all Windows which can contain other windows,
3
+ # optionally arranging those windows with a Sizer
4
+ module Parent
5
+ attr_reader :children, :sizer_items, :main_sizer
6
+
7
+ def add_child(child)
8
+ @children ||= []
9
+ @children << child
10
+ if child.kind_of?(Sizer) and not main_sizer
11
+ @main_sizer = child
12
+ end
13
+ end
14
+
15
+ # Associates a sizer_item with this parent; keep a map of which
16
+ # settings apply to which child
17
+ def size_child(sizer_item)
18
+ @sizers ||= {}
19
+ @sizers[sizer_item.window] = sizer_item
20
+ end
21
+
22
+ def output
23
+ super + child_output
24
+ end
25
+
26
+ # Returns a string containing the ruby code needed to instantiate
27
+ # all the child windows of this container, as well as arranging them
28
+ # in any sizer.
29
+ def child_output
30
+ output = ''
31
+ if children
32
+ children.each_with_index do | child, i |
33
+ output += child.output + "\n"
34
+ if @sizers and ( sizer_item = @sizers[child] )
35
+ output += sizer_item.output
36
+ end
37
+ end
38
+ end
39
+
40
+ if self.main_sizer
41
+ output << "#{var_name}.sizer = #{main_sizer.var_name}"
42
+ end
43
+ output
44
+ end
45
+
46
+ # Collects all the contained windows (recursively) that have
47
+ # user-defined names that should be exposed externally. Used by
48
+ # top-level windows to create a list of attribute readers to access
49
+ # named controls within the window.
50
+ def named_windows
51
+ return [] unless children
52
+ children.inject([]) do | names, child |
53
+ names << child.name if child.name
54
+ names += child.named_windows if child.kind_of?(Parent)
55
+ names
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,32 @@
1
+ module XRC2Ruby::ObjectTypes
2
+
3
+ class ScrollBar < Window
4
+ attr_accessor :value, :range, :thumbsize, :pagesize
5
+ def setup
6
+ "#{var_name}.set_scrollbar(#{value}, #{thumbsize}, #{range}, #{pagesize})"
7
+ end
8
+ end
9
+ class SpinCtrl < Window
10
+ init_arg(:min) { | val | val }
11
+ init_arg(:max) { | val | val }
12
+ # This is called "value" in XRC but "initial" as a constructor
13
+ # keyword arg
14
+ init_arg(:initial) { | val | val }
15
+ alias :value :initial
16
+ alias :value= :initial=
17
+ end
18
+
19
+ class Slider < Window
20
+ # These are called "xxx_value" as keyword constructor arguments, but
21
+ # simply min/max in XRC
22
+ init_arg(:min_value) { | val | val }
23
+ alias :min :min_value
24
+ alias :min= :min_value=
25
+ init_arg(:max_value) { | val | val }
26
+ alias :max :max_value
27
+ alias :max= :max_value=
28
+
29
+ init_arg(:value) { | val | val }
30
+ end
31
+ end
32
+
@@ -0,0 +1,39 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Represents the settings for a particular Window added to a
3
+ # container's sizer; corresponds to the <object class="sizeritem"> tag
4
+ # in XRC.
5
+ class SizerItem
6
+ attr_reader :flag
7
+ attr_accessor :border, :option, :window, :sizer
8
+
9
+ # Create a new sizer item contained within the sizer +sizer+, set up
10
+ # default sizer options
11
+ def initialize(sizer)
12
+ @sizer = sizer
13
+ @option = 0
14
+ @flag = 0
15
+ @border = 0
16
+ end
17
+
18
+ def flag=(flag)
19
+ @flag = flag.gsub(/wx/, "Wx::")
20
+ end
21
+
22
+ # Output a Sizer#add statement, adding +window+ to +sizer+
23
+ def output
24
+ "#{sizer.var_name}.add(#{window.var_name}, #{option}, #{flag}, #{border})\n"
25
+ end
26
+ end
27
+
28
+ # Actual sizers
29
+ class Sizer < Object
30
+ BASE_NAME = "sizer"
31
+ end
32
+
33
+ class BoxSizer < Sizer
34
+ attr_accessor :orient
35
+ def output
36
+ "#{var_name} = Wx::BoxSizer.new(#{orient.gsub(/^wx/, "Wx::")})\n"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ class StaticText < Window
3
+ translatable_string_init_arg(:label)
4
+ end
5
+
6
+ class StaticBox < Window
7
+ translatable_string_init_arg(:label)
8
+ end
9
+
10
+ class StaticBitmap < Window
11
+ init_arg(:label) { | val | "Wx::Bitmap.new('#{val}')" }
12
+ end
13
+
14
+ # No extra args needed
15
+ class StaticLine < Window; end
16
+
17
+ class Gauge < Window
18
+ attr_accessor :value
19
+ init_arg(:range) { | val | val }
20
+ def setup
21
+ value ? "#{var_name}.value = #{value}\n" : ""
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ class TextCtrl < Window
3
+ translatable_string_init_arg(:value)
4
+ end
5
+ class SearchCtrl < Window
6
+ translatable_string_init_arg(:value)
7
+ end
8
+ class StyledTextCtrl < Window
9
+ end
10
+ end
@@ -0,0 +1,86 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ class ToolBar < Window
3
+ include Parent
4
+ attr_accessor :dontattachtoframe
5
+ def initialize(*args)
6
+ super
7
+ @tools = []
8
+ end
9
+
10
+ def add_tool(tool)
11
+ @tools << tool
12
+ end
13
+
14
+ def setup
15
+ @tools.inject('') { | str, tool | str + tool.output }
16
+ end
17
+ end
18
+
19
+ # Not a real wxRuby class; individual tools are created by calls to
20
+ # add_tool and friends.
21
+ class TOOL
22
+ attr_reader :toolbar, :var_name
23
+ attr_accessor :bitmap, :bitmap2, :label,
24
+ :radio, :toggle,
25
+ :help, :longhelp, :tooltip
26
+
27
+ def initialize(toolbar, an_id)
28
+ @toolbar = toolbar
29
+ @var_name = an_id.downcase
30
+ if an_id.sub!(/^wx/, "Wx::")
31
+ @stock_id = an_id
32
+ end
33
+ @toolbar.add_tool(self)
34
+ end
35
+
36
+ def output
37
+ @label ||= ''
38
+ @help ||= ''
39
+ @longhelp ||= ''
40
+
41
+ # Normal bitmap
42
+ if bitmap
43
+ bmp1 = "Wx::Bitmap.new(#{self.bitmap.inspect}, Wx::BITMAP_TYPE_PNG)"
44
+ else
45
+ bmp1 = "Wx::NULL_BITMAP"
46
+ end
47
+
48
+ # Disabled bitmap
49
+ if bitmap2
50
+ bmp2 = "Wx::Bitmap.new(#{self.bitmap2.inspect}, Wx::BITMAP_TYPE_PNG)"
51
+ else
52
+ bmp2 = "Wx::NULL_BITMAP"
53
+ end
54
+
55
+ # What type of tool?
56
+ if radio
57
+ tooltype = "Wx::ITEM_RADIO"
58
+ elsif toggle
59
+ tooltype = "Wx::ITEM_CHECK"
60
+ else
61
+ tooltype = "Wx::ITEM_NORMAL"
62
+ end
63
+ if @stock_id
64
+ the_id = @stock_id
65
+ else
66
+ the_id = "Wx::ID_ANY"
67
+ end
68
+ # This is the most general form of add_tool
69
+ "#{var_name} = #{toolbar.var_name}.add_tool(#{the_id},
70
+ #{label.inspect},
71
+ #{bmp1},
72
+ #{bmp2},
73
+ #{tooltype},
74
+ #{help.inspect},
75
+ #{longhelp.inspect})\n"
76
+
77
+ end
78
+ end
79
+
80
+ class SEPARATOR
81
+ attr_reader :toolbar
82
+ def initialize(toolbar_or_menu, an_id = nil)
83
+ @toolbar = toolbar_or_menu
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,29 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # All windows which may be top-level containers (eg Frames,
3
+ # Dialogs). In particular, they may become named ruby subclasses
4
+ class TopLevelWindow < Window
5
+ include Parent
6
+ attr_accessor :title
7
+ def output
8
+ if not named_windows.empty?
9
+ attr_reader = "attr_reader " +
10
+ named_windows.map { | name | name.tr('@', ':') }.join(', ')
11
+ end
12
+
13
+ @name = "self"
14
+ # get child widgets code and indent to four spaces
15
+ "class #{sub_class} < #{win_class}
16
+ #{attr_reader}" +
17
+ ( XRC2Ruby.use_gettext ? "include GetText\n" : "" ) + "
18
+ def initialize(parent)
19
+ super(parent, #{args})
20
+ " + child_output.gsub(/^/, ' ') + "
21
+ end
22
+ end"
23
+ end
24
+ end
25
+
26
+ class Dialog < TopLevelWindow; end
27
+ class Frame < TopLevelWindow; end
28
+ class MiniFrame < TopLevelWindow; end
29
+ end
@@ -0,0 +1,128 @@
1
+ module XRC2Ruby::ObjectTypes
2
+ # Module used to manage keyword arguments
3
+ module InitArgs
4
+ def init_args
5
+ @init_args
6
+ end
7
+
8
+ # Method for defining an element called +arg_name+ which is passed
9
+ # as part of the wxRuby constructor for a class
10
+ def init_arg(arg_name, &block)
11
+ attr_writer arg_name
12
+ define_method(arg_name) do
13
+ if val = instance_variable_get("@#{arg_name}")
14
+ block.call(val)
15
+ end
16
+ end
17
+ @init_args ||= {}
18
+ @init_args[arg_name] = block
19
+ end
20
+
21
+ # Method for defining a string ctor arg that can be translated using
22
+ # ruby-gettext, if that option is in use. For example - mneu items,
23
+ # button labels.
24
+ def translatable_string_init_arg(arg_name)
25
+ if XRC2Ruby.use_gettext
26
+ init_arg(arg_name) { | val | "_(" + val.inspect + ")" }
27
+ else
28
+ init_arg(arg_name) { | val | val.inspect }
29
+ end
30
+ end
31
+
32
+ def inherited(kls)
33
+ @init_args.each { | arg, proc | kls.init_arg(arg, &proc) }
34
+ end
35
+ end
36
+
37
+ class Window < Object
38
+ BASE_NAME = "window"
39
+ self.extend InitArgs
40
+
41
+ # Standard constructor arguments
42
+ init_arg(:size) { | val | "Wx::Size.new(#{val})" }
43
+ init_arg(:point) { | val | "Wx::Point.new(#{val})" }
44
+ init_arg(:style) { | val | val.gsub(/wx/, "Wx::") }
45
+
46
+ # Tooltip and help text, applicable to all windows
47
+ attr_accessor :tooltip, :help, :hidden, :enabled, :fg, :bg
48
+
49
+ def output
50
+ # The base constructor
51
+ if parent
52
+ base = "#{var_name} = #{win_class}.new(#{parent.var_name}"
53
+ base << ( args.empty? ? ")" : ",\n#{args})" )
54
+ else
55
+ base = "#{var_name} = #{win_class}.new(nil"
56
+ base << ( args.empty? ? ")" : ",\n#{args})" )
57
+ end
58
+ # Re-indent the keyword arguments
59
+ indent = base.index("(") + 1
60
+ base.gsub!(/\n:/, "\n" + (" " * indent ) + ":") || base
61
+ base << "\n"
62
+
63
+ # If help text is defined
64
+ if self.help
65
+ base << "#{var_name}.help_text = '#{self.help}'\n"
66
+ end
67
+
68
+ # If a tooltip is defined
69
+ if self.tooltip
70
+ base << "#{var_name}.tool_tip = '#{self.tooltip}'\n"
71
+ end
72
+
73
+ # If foreground colour is specified, convert hex->int and set
74
+ if self.fg
75
+ colour = self.bg.scan(/[0-9a-fA-F]{2}/).map { | e | e.to_i(16) }
76
+ base << "#{var_name}.background_colour = "
77
+ base << "Wx::Colour.new(#{colour.join(', ')})\n"
78
+ end
79
+
80
+ # If background colour is specified, convert hex->int and set
81
+ if self.bg
82
+ colour = self.bg.scan(/[0-9a-fA-F]{2}/).map { | e | e.to_i(16) }
83
+ base << "#{var_name}.foreground_colour = "
84
+ base << "Wx::Colour.new(#{colour.join(', ')})\n"
85
+ end
86
+
87
+ # If explicitly disabled or enabled
88
+ if self.enabled
89
+ if self.enabled.to_i.zero?
90
+ base << "#{var_name}.disable\n"
91
+ else
92
+ base << "#{var_name}.enable\n"
93
+ end
94
+ end
95
+
96
+ # If explicitly shown or hidden
97
+ if self.hidden
98
+ if self.hidden.to_i.zero?
99
+ base << "#{var_name}.hide\n"
100
+ else
101
+ base << "#{var_name}.show\n"
102
+ end
103
+ end
104
+
105
+ base << setup
106
+ base
107
+ end
108
+
109
+ # Returns the constructor arguments as a Ruby string
110
+ def args
111
+ defined_args = []
112
+ self.class.init_args.keys.each do | arg |
113
+ if arg_val = send(arg)
114
+ defined_args << ":#{arg} => #{arg_val}"
115
+ end
116
+ end
117
+ defined_args.join(",\n")
118
+ end
119
+
120
+ # Some classes define XRC property elements that are set in
121
+ # wxWidgets by method calls rather than passed as part of the
122
+ # constructor. This method can be overridden in subclasses to return
123
+ # a string with those arguments.
124
+ def setup
125
+ ''
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,51 @@
1
+ module WindowTypes
2
+ class Window < Object
3
+ def self.init_args
4
+ @init_args
5
+ end
6
+
7
+ def self.init_arg(arg_name, &block)
8
+ attr_writer arg_name
9
+ define_method(arg_name) do
10
+ if val = instance_variable_get("@#{arg_name}")
11
+ block.call(val)
12
+ end
13
+ end
14
+ @init_args ||= {}
15
+ @init_args[arg_name] = block
16
+ end
17
+
18
+ def self.inherited(kls)
19
+ @init_args.each { | arg, proc | kls.init_arg(arg, &proc) }
20
+ end
21
+
22
+ init_arg(:size) do | val |
23
+ "Wx::Size.new(#{val})"
24
+ # "[ #{val} ]"
25
+ end
26
+
27
+ init_arg(:point) do | val |
28
+ "[ #{val} ]"
29
+ end
30
+
31
+ init_arg(:style) do | val |
32
+ val.gsub(/wx/, "Wx::")
33
+ end
34
+
35
+ def args
36
+ defined_args = []
37
+ self.class.init_args.keys.each do | arg |
38
+ if arg_val = send(arg)
39
+ defined_args << ":#{arg} => #{arg_val}"
40
+ end
41
+ end
42
+ defined_args.join(",\n")
43
+ end
44
+ end
45
+
46
+ class Button < Window
47
+ init_arg(:label) do | val |
48
+ "'#{val}'"
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,72 +1,96 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: wx_sugar
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.19
7
- date: 2007-11-08 00:00:00 +00:00
8
- summary: Syntax extensions for WxRuby.
9
- require_paths:
10
- - lib
11
- email: alex@pressure.to
12
- homepage: http://www.pressure.to/qda/
13
- rubyforge_project: weft-qda
14
- description: Ruby-ifies the ruby API for WxRuby.
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.8.1
24
- version:
4
+ version: 0.1.20
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Alex Fenton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-15 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby-ifies the ruby API for WxRuby.
17
+ email: alex@pressure.to
18
+ executables:
19
+ - xrcise
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
25
  - bin/xrcise
33
26
  - lib/wx_sugar
34
- - lib/wx_sugar.rb
35
- - lib/wx_sugar/wx_classes
36
- - lib/wx_sugar/xrc
37
- - lib/wx_sugar/layout.rb
38
- - lib/wx_sugar/delayed_constructors.rb
27
+ - lib/wx_sugar/all.rb
39
28
  - lib/wx_sugar/class_definitions.rb
29
+ - lib/wx_sugar/delayed_constructors.rb
40
30
  - lib/wx_sugar/enumerable_controls.rb
41
- - lib/wx_sugar/menu.rb
42
- - lib/wx_sugar/all.rb
43
- - lib/wx_sugar/itemdata.rb
44
31
  - lib/wx_sugar/event_connector.rb
45
- - lib/wx_sugar/wx_classes.rb
46
- - lib/wx_sugar/xrc.rb
32
+ - lib/wx_sugar/itemdata.rb
33
+ - lib/wx_sugar/layout.rb
34
+ - lib/wx_sugar/menu.rb
47
35
  - lib/wx_sugar/version.rb
48
- - lib/wx_sugar/wx_classes/listctrl.rb
36
+ - lib/wx_sugar/wx_classes
49
37
  - lib/wx_sugar/wx_classes/colour.rb
38
+ - lib/wx_sugar/wx_classes/control_with_items.rb
39
+ - lib/wx_sugar/wx_classes/listctrl.rb
50
40
  - lib/wx_sugar/wx_classes/position.rb
51
- - lib/wx_sugar/wx_classes/window.rb
52
41
  - lib/wx_sugar/wx_classes/size.rb
53
- - lib/wx_sugar/wx_classes/control_with_items.rb
42
+ - lib/wx_sugar/wx_classes/window.rb
43
+ - lib/wx_sugar/wx_classes.rb
44
+ - lib/wx_sugar/xrc
45
+ - lib/wx_sugar/xrc/outputter.rb
54
46
  - lib/wx_sugar/xrc/xml_class.rb
55
47
  - lib/wx_sugar/xrc/xml_resource.rb
56
- - lib/wx_sugar/xrc/outputter.rb
48
+ - lib/wx_sugar/xrc/xrc2ruby.rb
49
+ - lib/wx_sugar/xrc/xrc2ruby_types
50
+ - lib/wx_sugar/xrc/xrc2ruby_types/books.rb
51
+ - lib/wx_sugar/xrc/xrc2ruby_types/buttons.rb
52
+ - lib/wx_sugar/xrc/xrc2ruby_types/containers.rb
53
+ - lib/wx_sugar/xrc/xrc2ruby_types/lists.rb
54
+ - lib/wx_sugar/xrc/xrc2ruby_types/menu.rb
55
+ - lib/wx_sugar/xrc/xrc2ruby_types/object.rb
56
+ - lib/wx_sugar/xrc/xrc2ruby_types/parent.rb
57
+ - lib/wx_sugar/xrc/xrc2ruby_types/range.rb
58
+ - lib/wx_sugar/xrc/xrc2ruby_types/sizers.rb
59
+ - lib/wx_sugar/xrc/xrc2ruby_types/static.rb
60
+ - lib/wx_sugar/xrc/xrc2ruby_types/text.rb
61
+ - lib/wx_sugar/xrc/xrc2ruby_types/toolbar.rb
62
+ - lib/wx_sugar/xrc/xrc2ruby_types/toplevelwindows.rb
63
+ - lib/wx_sugar/xrc/xrc2ruby_types/window.rb
64
+ - lib/wx_sugar/xrc/xrc2ruby_types/windows.rb
65
+ - lib/wx_sugar/xrc.rb
66
+ - lib/wx_sugar.rb
57
67
  - samples/sugar-sample.rb
58
68
  - LICENCE
59
- test_files: []
60
-
69
+ has_rdoc: true
70
+ homepage: http://www.pressure.to/qda/
71
+ post_install_message:
61
72
  rdoc_options: []
62
73
 
63
- extra_rdoc_files: []
64
-
65
- executables:
66
- - xrcise
67
- extensions: []
68
-
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.8.1
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
69
88
  requirements: []
70
89
 
71
- dependencies: []
90
+ rubyforge_project: weft-qda
91
+ rubygems_version: 1.0.1
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: Syntax extensions for WxRuby.
95
+ test_files: []
72
96