Glader: quick .glade -> Ruby

L

Leslie Viljoen

Here's a tiny script I wrote to go the last mile when generating Ruby
programs to make
use of Glade forms. The ruby-glade-create-template program that comes in the
libglade2-ruby.18 package creates an example program that will be
overwritten each
time the script is re-run.

Glader makes a script that uses the example program, allowing you to
repeatedly save
your .glade file, convert it and "run" it to see changes.

I hope that makes sense to *someone*


Example session:
----------------------
lesliev@lesliev-laptop:~/glade_test/calbox$ ./glader.rb layout.glade
Making class file (layout_glade.rb)...
Making main file (layout.rb)...

lesliev@lesliev-laptop:~/glade_test/calbox$ ruby layout.rb (to play
with the form)

...now you can edit and re-save your .glade form and quickly regen
without overwriting any of your changes to layout.rb by running glader again.


Glader:
---------

#!/usr/bin/ruby -w

# This class is a quick way of creating a runnable Ruby program from
# a single Glade form.
#
class Glader
def initialize(glade_filename)
raise ArgumentError.new("File missing: #{glade_filename}") if
!File.exist?(glade_filename)
raise ArgumentError.new("Not a .glade file: #{glade_filename}") if
glade_filename !~ /.*\.glade/

@glade_filename = glade_filename
@base_filename = File.basename(@glade_filename, ".glade")
@class_filename = @base_filename + "_glade.rb"
end

# Runs the ruby-glade-create-template script to create a file with a
# class that corresponds to the form. This can be called repeatedly
# to keep creating the class every time the .glade file changes.
#
def make_class
`/usr/bin/ruby-glade-create-template #{@glade_filename} > #{@class_filename}`
@class_filename
end

# Creates a main program that uses the glade class created with
# _make_class_ and opens the window with the form. It will do nothing
# if the main program file already exists, since the user is likely
# to add code to this file.
#
def make_main
main_filename = @base_filename + '.rb'

if(File.exist?(main_filename))
raise ArgumentError.new("#{main_filename} already exists, skipping")
else
File.open(main_filename, "w") {|f| f.write generate_main}
end
main_filename
end

private
def generate_main()
class_name = File.read(@class_filename).scan(/class (.*)/).to_s
window_id = File.read(@glade_filename).scan(/class="GtkWindow"
id="(\w*)">/).to_s

main =<<END

require '#{@base_filename}_glade'

class #{class_name}
def show
@glade["#{window_id}"].show
end

#if this signal exists, exit nicely
#
def on_#{window_id}_destroy(widget)
Gtk.main_quit
end
end

form = #{class_name}.new("#{@glade_filename}", nil, "Application")
form.show

Gtk.main

END
end
end

if __FILE__ == $0
if ARGV.length != 1
puts
puts "This script creates a class and a main program that shows a
window using"
puts "that class from a .glade file. It can be used to quickly
generate a functional Ruby"
puts "program from a .glade file"
puts
puts "Usage: #{__FILE__} <file.glade>"
puts
exit
end

glader = Glader.new(ARGV[0])
print "Making class file (#{glader.make_class})...\n"

begin
print "Making main file (#{glader.make_main})...\n"
rescue => e
puts e.message
end
puts

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top