On Sun, Jun 8, 2008 at 4:19 PM, Victor R. [email protected]
wrote:
Tristin: I already tried the suggestion of FxRuby plus other GUIs. The
j). I got the msg:
or get tired. Although I am already getting tired. This is not supposed to
be this difficult. But dealing with Java nothing is ever simple.
Leslie: Does Glade provides a drag/drop widget creation feature? I went to
glade.gnome.org but could not find anything about it.
Glade is a form builder, it allows you to drop containers onto forms
and Gtk widgets
into those containers. This is not the same as with Netbeans or Visual
Studio,
but you can get the same results and in some ways the system is better
- it takes better care of how forms change when text is translated or
fonts changed.
You may find it has a learning curve in the beginning, but only until
you understand
how things work, after that it’s quick and easy.
I’m putting together a 5-minute tutorial but I’ll include the text
portion below
to get you started. The tutorial uses the “glader” script which I’ll
include at the bottom.
Warning: I haven’t tried glader in Windows yet!
Starting out with Glade and Ruby
Open glade.
- Add a new window, common → border width: 10
- Add a vbox to the window (2 items), general → spacing: 10
- Add a hbox to the bottom area, general → spacing: 10
- Right-click hbox in tree, add parent, alignment
- Put a button in each of the two bottom blocks
- On the alignment container, horizonal scale: 0, vertical scale: 0,
horizontal alignment: 1
- On the alignment container, expand: no, fill: no
If expand is no, the area does not take on more space when the parent
grows.
If expand is yes, the area will grow with its parent, and ‘fill’
controls how it grows.
If ‘fill’ is yes, extra space is added around the widgets, otherwise
the extra space
goes inside the widget and the widget itself grows.
- Add a table in the top space
- table, packing → expand: no, general → row spacing: 10, column
spacing: 10
Before saving, click on window1, signals → gtkobject → destroy and
select
“on_window1_destroy” from the combo.
Now save as “designation” and run “./glader designation.glade” from
the command line.
After generation, run “ruby designation.rb”
Note that when you resize, everything still looks good except the
three labels, which
shouldn’t grow horizontally like that.
Also, since you added the on_window1_destroy signal, you can close the
window and your
Ruby program will exit nicely (glader wrote a handler for that).
Return to Glade.
- label1, packing → fill: deselect expand
- Do the same for the other two labels
- Save
Now re-run designation.rb. You don’t have to regenerate since the class
and main
file will not change, only the form definition (glade) file. If you
resize you’ll
see that the labels now stay small and only the text fields and combo
boxes grow,
which is nice.
----------------------- glader.rb
#!/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=“\w+”
id="(\w)">$/).flatten[0]
main =<<END
require ‘#{@base_filename}_glade’
class #{class_name}
def form
@glade[“#{window_id}”]
end
def show
form.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