john
john

Reputation:

ruby glade/gtkbuilder example?

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!

Upvotes: 3

Views: 6199

Answers (4)

Marc Quinton
Marc Quinton

Reputation: 31

Another working version, in object oriented form:

require 'gtk2'

class Builder < Gtk::Builder

def initialize xml
        super()

        self.add_from_string(xml)

    self['main'].set_window_position Gtk::Window::POS_CENTER
        self['main'].signal_connect('destroy') { Gtk.main_quit }
        self['main'].show_all

    self.connect_signals{ |handler| method(handler) }

end

def on_button_clicked w

    case w.label
            when 'quit'
                Gtk::main_quit
            else
                puts "# on_button_clicked : " + w.label

    end
    end

def on_main_destroy
        puts "# on_main_destroy"
        Gtk::main_quit
    end
end

if __FILE__ == $0

xml = <<EOI
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <object class="GtkWindow" id="main">
    <property name="can_focus">False</property>
    <child>
       <object class="GtkButton" id="quit">
        <property name="label">quit</property>
        <signal name="clicked" handler="on_button_clicked" swapped="no"/>
      </object>
    </child>
  </object>
</interface>
EOI


    Gtk.init
        builder = Builder.new(xml)
    Gtk.main

end

Upvotes: 3

andreadallera
andreadallera

Reputation: 61

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 'gtk2'
# require 'libglade2' #you don't need this anymore

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).

Upvotes: 6

Vadim Peretokin
Vadim Peretokin

Reputation: 2816

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.

Upvotes: 0

Jon
Jon

Reputation: 354

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

Upvotes: 3

Related Questions