PDA

View Full Version : [TUTORIAL] Creating a GUI with SLib



SpiffyJr
08-20-2010, 11:35 AM
How to create a GUI with slib
Lots of people are coming out with new scripts and a common question is how to go about incorporating a GUI. I wrote some very nifty stuff into slib that you can incorporate into your own scripts to help reduce the learning curve. The following will give a brief example on creating a GUI using slib.

Requirements

Text-editor
slib installed (;repo download slib ;slib)




# A few flags so we know what buttons were clicked
done = false
save = false

# We need a place to store our settings
@@settings = CharSettings.to_hash

# Create a SGtk window
gtk = SGui.new("Hello World", @@settings)

# Gtk.queue is an error handler provided by Tillmen to prevent Gemstone
# from crashing when you make an error
Gtk.queue {
# Create a VBox to pack content onto and then a table to place our widgets
# See: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk::VBox
# See: http://ruby-gnome2.sourceforge.jp/hiki.cgi?Gtk::Table

# This is the main VBox
# See the documentation above for details on the arguments
vb = Gtk::VBox.new(false)
vb.set_border_width(5)

# Add the VBox to the SGui window
gtk.window.add(vb)

# This is the primary table with our widgets
# See the documentation above for details on the arguments
mn_tbl = Gtk::Table.new(10, 6, false)

# Let's add some widgets to the main table
# First, an entry box with label
# add_entry(table, row, column, label, variable name, default value, local/global, tooltip)
gtk.add_entry(mn_tbl, 0, 0, 'Entry box:', 'entry', @@settings['entry'], :local, 'I am a test entry box')

# Checkbutton, same arguments as the entry
gtk.add_checkbutton(mn_tbl, 1, 1, 'Checkbutton!', 'check', @@settings['check'], :local, 'I am a checkbutton!')

# This is a secondary table we place some buttons in
btn_tbl = Gtk::Table.new(1, 4, false)

# Add two buttons to the button table for saving/closing
# gtk.add_button(table, row, column, label, variable name)
gtk.add_button(btn_tbl, 0, 0, '_Save', 'save')
gtk.add_button(btn_tbl, 0, 1, '_Close', 'close')

# Connect "signals" to the buttons so that we know something happened
gtk.button['close'].signal_connect('clicked') { done = true }
gtk.button['save'].signal_connect('clicked') { save = true }

# Pack the tables onto the VBox
vb.pack_start(mn_tbl)
vb.pack_start(btn_tbl)

# When the main window is closed set done to true so we exit
gtk.window.signal_connect('delete_event') { done = true }

# Show the GUI window
gtk.window.show_all
}

# This makes sure to clean up the GUI window in case the script closes for some reason
# destory_window is a SGui method I created. Information on the window such as
# position and size are saved and then automatically used the next time your script
# starts up.
before_dying { gtk.destroy_window(@@settings) }

# Run until we get a signal to disconnect or the
# close button is pressed
loop {
# If the save button was checked run our save routine
if save
# smsg(message, type = :mono, :bold, or :all)
smsg '-- helloworld: Settings saved', :all

# save is a SGui method to copy everything from the entries, buttons,
# checkbuttons, and combobox's to settings automatically.
gtk.save

save = false
end

# The window was exited or the close button was clicked
break if done
sleep 0.10
}