views:

1402

answers:

3

hello

I was using ruby and glade2 to design the user interface for a while in the new version of glade3 i can use gtkbuilder format to generated xml file instead of libglade.

is there any example? i searched google but i had no luck!

+2  A: 

You're right on the lack of pre-written tutorials, but the usage of gtkbuilder in Ruby is almost identical to Python (same function names, call orders) so these might be of interest -

GTK::Builder module: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk%3A%3ABuilder

Python code:

import sys
import gtk

class TutorialTextEditor:

    def on_window_destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):

        builder = gtk.Builder()
        builder.add_from_file("tutorial.xml") 

        self.window = builder.get_object("window")
        builder.connect_signals(self)       

if __name__ == "__main__":
    editor = TutorialTextEditor()
    editor.window.show()
    gtk.main()

Source: http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html

Jon
A: 

It is the same really. Here is a glade example: http://snippets.dzone.com/posts/show/5251 substitute with the proper methods and you're set.

There is an IDE written in Ruby: http://github.com/danlucraft/redcar/tree/master but I wasn't able to find it's main file to begin with to see if it uses builder.

Vadi
Redcar is implemented on top of JRuby/SWT, so not at all GTK related.
Jacob
That's because they switched. At the time of writing my comment, it was relevant.
Vadi
+1  A: 

It's really simple: just create your GUI with Glade (and save it as GtkBuilder) and then use it in ruby with:

require 'rubygems'
require 'libglade2' #you still need this

builder = Gtk::Builder.new
builder.add_from_file(file)
builder.connect_signals {|handler| method(handler) }

the first line creates the Builder object, which is responsible of creating the Glib::Objects from your xml definition and also stores them for later use (you can call get_object(objname) on builder, it will return the widget defined with objname).

The second line actually loads your interface definition, where file is the path to your gtkbuilder file.

The third line is somewhat more obscure. connect_signals calls the block provided once for every signal you have defined in your interface. handler is just a string (the name of the signal). You are supposed to return a proc (or anything invokable with call) from the block: that block will be invoked everytime the signal defined by handler is fired. In this example the block is just returning a method with the same name as the signal (and, for simplicity's sake, it is assumed that there is a method for every one of the signals defined in the interface).

andreadallera