A Markaby-like Builder for GTK

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

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. [email protected] ]------------------+
| |
| Mobile:(+36 20)356 9656 |
| |
± “Olyan lángész vagyok, hogy poroltóval kellene járnom!” -+


This 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/

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 :wink:
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 :wink:

Cheers,
Markus


This 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/