# Glader: quick .glade -\> Ruby

**URL:** <https://www.ruby-forum.com/t/glader-quick-glade-ruby/139829>\
**Category:** Ruby\
**Created:** [June 7, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/glader-quick-glade-ruby/139829 "2008-06-07T19:36:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![leslieviljoen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3d9bf3/32.png) [@leslieviljoen](https://www.ruby-forum.com/u/leslieviljoen)\
**Post date:** [June 7, 2008, 7:36pm UTC](https://www.ruby-forum.com/t/glader-quick-glade-ruby/139829/1 "2008-06-07T19:36:09Z")

</div>

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
