gtkbuilder 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,9 @@ module Builder
2
2
 
3
3
  module Gtk
4
4
 
5
+ ## Defines a new Gtk window
6
+ # @param block A block that will be passed a builder proxy object
7
+ # @return the created window
5
8
  def self.new(*args, &block)
6
9
  proxy = Builder::Gtk::WindowProxy.new(*args)
7
10
  block.call(proxy) if block
@@ -2,10 +2,13 @@ require 'proxies/layout.rb'
2
2
 
3
3
  module Builder::Gtk
4
4
 
5
+ ## Proxies both VBox and HBox
5
6
  class BoxProxy
6
7
 
7
8
  include LayoutProxy
8
9
 
10
+ ## Creates a new proxy for the given VBox or HBox widget
11
+ # The default packing is @pack_start(widget, false, false, 0)@.
9
12
  def initialize(widget)
10
13
  @widget = widget
11
14
  @data = {
@@ -14,16 +17,25 @@ module Builder::Gtk
14
17
  }
15
18
  end
16
19
 
20
+ ## Sets the pack mode to pack_start.
21
+ # The arguments are passed to pack_start for every
22
+ # following widget definition.
17
23
  def start(*args, &block)
18
24
  return _pack(:start, args, block)
19
25
  end
20
26
 
27
+ ## Sets the pack mode to pack_end
28
+ # The arguments are passed to pack_end for every
29
+ # following widget definition.
21
30
  def end(*args, &block)
22
31
  return _pack(:end, args, block)
23
32
  end
24
33
 
25
34
  private
26
35
 
36
+ ## Common function for both pack modes.
37
+ # The arguments are passed to the pack function for every
38
+ # following widget definition.
27
39
  def _pack(position, args, block)
28
40
  @data = {
29
41
  :position => position,
@@ -33,6 +45,7 @@ module Builder::Gtk
33
45
  return self
34
46
  end
35
47
 
48
+ ## Actually packs the given widget using the saved position and arguments.
36
49
  def _pack_widget(widget)
37
50
  @widget.send("pack_#{@data[:position]}", widget, *@data[:args])
38
51
  end
@@ -2,13 +2,32 @@ require 'gtk2'
2
2
 
3
3
  module Builder::Gtk
4
4
 
5
+ ## Base class for all widgets that can have children
5
6
  module LayoutProxy
6
7
 
8
+ ## The widget that is being proxied
7
9
  attr_accessor :widget
8
10
 
11
+ ## Handles constructing Gtk widgets
12
+ #
13
+ # Example:
14
+ #
15
+ # proxy.entry --> Gtk::Entry.new
16
+ # proxy.vbox(true, 10) --> Gtk::VBox.new(true, 10)
17
+ # proxy.drawing_area --> Gtk::DrawingArea.new
18
+ #
19
+ # @param name The name of the widget in snake case
20
+ # @param args The arguments to pass to the constructor of
21
+ # the Gtk widget
22
+ # @param block A block that will be executed with the created widget
23
+ # or a proxy if one is available.
9
24
  def method_missing(name, *args, &block)
10
- widget = _get_widget(name, args, &block)
11
- proxy = _get_proxy(name, widget) || widget
25
+ if @widget.respond_to?(name)
26
+ return @widget.__send__(name, *args, &block)
27
+ end
28
+
29
+ widget = _get_widget(name, args)
30
+ proxy = _get_proxy(name, widget)
12
31
  block.call(proxy) if block
13
32
  _pack_widget(widget)
14
33
  return self
@@ -16,13 +35,17 @@ module Builder::Gtk
16
35
 
17
36
  private
18
37
 
19
- def _get_widget(name, args, &block)
38
+ ## Tries to create a Gtk widget with the given name.
39
+ # All arguments are forwarded to the widget.
40
+ def _get_widget(name, args)
20
41
  class_name = _find_gtk_class(name)
21
- raise RuntimeError.new("no widget found for name #{name}") unless class_name
42
+ raise RuntimeError.new("no widget found for name `#{name}'") unless class_name
22
43
  klass = ::Gtk.const_get(class_name)
23
- return klass.new(*args, &block)
44
+ return klass.new(*args)
24
45
  end
25
46
 
47
+ ## Tries to find a Gtk widget for the given name
48
+ # @return the found widget class or nil.
26
49
  def _find_gtk_class(name)
27
50
  name = name.to_s.gsub("_", "").downcase
28
51
  return ::Gtk.constants.find do |constant|
@@ -30,17 +53,22 @@ module Builder::Gtk
30
53
  end
31
54
  end
32
55
 
56
+ ## Creates a proxy for the given widget with the given name.
57
+ # If no proxy can be created, returns the widget itself.
33
58
  def _get_proxy(name, widget)
34
59
  case name.to_s
35
- when "winodw" then return WindowvboxProxy.new(widget)
60
+ when "window" then return WindowvboxProxy.new(widget)
36
61
  when "vbox" then return BoxProxy.new(widget)
37
62
  when "hbox" then return BoxProxy.new(widget)
38
- else return nil
63
+ else return widget
39
64
  end
40
65
  end
41
66
 
67
+ ## Must be reimplemented by subclasses to actually pack the
68
+ # given widget.
69
+ # Raises an exception by default.
42
70
  def _pack_widget(widget)
43
- raise RuntimeError.new("cannot pack inside this widget")
71
+ raise RuntimeError.new("cannot pack inside this widget. This is a bug in the gtkbuilder implementation")
44
72
  end
45
73
 
46
74
  end
@@ -2,16 +2,19 @@ require 'proxies/layout.rb'
2
2
 
3
3
  module Builder::Gtk
4
4
 
5
+ ## Proxies a window.
5
6
  class WindowProxy
6
7
 
7
8
  include LayoutProxy
8
9
 
10
+ ## Creates a new window proxy and a new window.
9
11
  def initialize(*args)
10
12
  @widget = ::Gtk::Window.new(*args)
11
13
  end
12
14
 
13
15
  private
14
16
 
17
+ ## Packs the given widget into the window.
15
18
  def _pack_widget(widget)
16
19
  @widget.add(widget)
17
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gtkbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-20 00:00:00.000000000 Z
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Builder::Gtk provides a DSL for generating Gtk2 windows.
14
+ description: Builder::Gtk let's you build Gtk windows using a simple DSL
15
15
  email:
16
16
  - github@rottenrei.be
17
17
  executables: []
@@ -47,5 +47,5 @@ rubyforge_project:
47
47
  rubygems_version: 1.8.11
48
48
  signing_key:
49
49
  specification_version: 3
50
- summary: Easy creation of Gtk windows
50
+ summary: Easy way to generate simple Gtk GUIs
51
51
  test_files: []