Syndicate: [full] [short]
1 of 1 article
Gtk and Rails: a strange meeting
10 Oct 05    [print link all ]

Well, I woke up this morning with a strange idea in my head. I was thinking about how cool Rails is, and how sad it is that so few people get to know it right now. I then remembered another nice library called Gtk, the Gnu version of a portable GUI toolkit, and I remembered the Ruby binding for it. Then I thought, what if you wrote something in rails that provided a Gtk-like interface. So that you could run ruby Gtk programs under a rails web application. It would have to dynamically generate the html that would best approximate the Gtk calls that have been issued. An hour and a half later, rtk was born: Download rtk.tar.gz source Here is the Two Buttons Ruby-Gnome example that this is based on. And here is the controller file where the GtkRails code is embedded:

class MulController < ApplicationController
  private
  def zputs(*args)
    args.each { |i|
      Gtk::Window.getMessageConsole.addmsg(i)
    }
  end
  def greeting(num)
      zputs "This is button #{num}"
  end
  public
  def ui
#    @w = @session['w']
    evt = @params['event']
    id = @params['id']
    Gtk::Signallable.send_signal(id, evt) if evt && id
    rt()
    render :action=>'rt'
  end
  def rt
    unless @w = Gtk::Window.getTopWindow
    # Build the GUI
    window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
    hbox   = Gtk::HBox.new(false,0)

    button1 = Gtk::Button.new("button 1")
    button2 = Gtk::Button.new("button 2")
    but3 = Gtk::Button.new("but3")

    hbox.pack_start button1, true, true, 0
    hbox.pack_start button2, true, true, 0

    window.add hbox
    window.add but3

    # Callbacks and configuration.

    window.set_title    "Two buttons"
    window.border_width 10
    window.signal_connect('delete_event') { false }
    window.signal_connect('destroy') { Gtk.main_quit }

    button1.signal_connect('clicked') { greeting(1) }
    button2.signal_connect('clicked') { greeting(2) }
    but3.signal_connect('clicked') { zputs "You pressed button 3!" }

    # All done.
    window.show_all
    Gtk.main

    @w = Gtk::Window.getTopWindow
    end
  end
end