# A Markaby-like Builder for GTK

**URL:** <https://www.ruby-forum.com/t/a-markaby-like-builder-for-gtk/103310>\
**Category:** Ruby-Gnome 2\
**Created:** [May 4, 2007, 2:09am UTC](https://www.ruby-forum.com/t/a-markaby-like-builder-for-gtk/103310 "2007-05-04T02:09:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [May 4, 2007, 2:09am UTC](https://www.ruby-forum.com/t/a-markaby-like-builder-for-gtk/103310/1 "2007-05-04T02:09:23Z")

</div>

Hi!

I just had the idea to create a Builder for Gtk, similar to Markaby and  
REXML.  
It turns out all this method\_missing and instance\_eval isn’t really as  
hard as I  
thought, and I’ve already come up with a solution that works really nice  
(see  
the attached file for the code and examples).

Does anybody have some more ideas to improve this and maybe make it a  
little bit  
more cleaner?

Cheers,  
Markus

---

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [May 4, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/a-markaby-like-builder-for-gtk/103310/2 "2007-05-04T16:37:50Z")

</div>

Hi!

I’ve just read the comment in the code.

You wrote:

You can actually call #build on any Gtk::Widget:

```
b = Gtk::Button.new
b.build do
  add h_box {
    pack_start image(Gtk::Stock::CLOSE)
    pack_start label("close me")
  }
end

```

What will this snippet do? Calling build on a Button?

Reading the comment it seems, that your scripts can do more things,  
than the little example snippet. Can you post an example with  
comments? It would be helpful.

I would prefer building with an own class like: Gtk::Builder, and than  
you can write class names as usual (is this possible?).

–  
±[Gergely K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) ]------------------+  
| |  
| Mobile:(+36 20)356 9656 |  
| |  
± “Olyan lángész vagyok, hogy poroltóval kellene járnom!” -+

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![subroutine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@subroutine](https://www.ruby-forum.com/u/subroutine)\
**Post date:** [May 4, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/a-markaby-like-builder-for-gtk/103310/3 "2007-05-04T19:35:05Z")

</div>

On Fri, May 04, 2007 at 04:37:01PM +0200, Gergely K. wrote:

> ```
> pack_start label("close me")
> }
> end
> 
> ```
> 
> What will this snippet do? Calling build on a Button?

This will add a HBox with an Image and a Label inside it to the button.  
#build just calls instance\_eval on the widget, and #method\_missing  
handles  
the automagic widget creation.  
I first made it only with Gtk.build, but then I realized that it works  
for  
widgets too by simply including the Builder::Proxy, so I just put it in.

> Reading the comment it seems, that your scripts can do more things,  
> than the little example snippet. Can you post an example with  
> comments? It would be helpful.

I just wrote this yesterday late at night, so bear with me 😉  
I think I’ll try to convert some of my GTK programs to this syntax  
and see how it works out.

> I would prefer building with an own class like: Gtk::Builder, and than  
> you can write class names as usual (is this possible?).

Creating normal classes iside the block works as usual, though you have  
to  
call #build on every widget to do further nesting, and you won’t get the  
automatic property-setting and signal-connecting, since you’d have to do  
this in the widget constructor.

To me, the point of this whole thing was that I _didn’t_ have to call  
Gtk::\*.new for every widget, and that I can quickly create a GUI without  
all the usual filler.

Anyway, here’s an example using classes directly:

b = Gtk::Builder.new  
b.build do  
Gtk::Window.new.build {  
b = Gtk::Button.new(‘click me’)  
b.signal\_connect(‘clicked’) { Gtk.main\_quit }  
add b  
show\_all  
}  
end  
Gtk.main

As you see, this doesn’t give you a lot over the normal way, except  
maybe  
that the code structure equals the widget hierarchy.

I’m not really sure yet if this builder syntax will be efficient in more  
complex programs, but it definitely was fun to write 😉

Cheers,  
Markus

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
