class Knj::Gtk2
Contains various methods for doing stuff quick using the Gtk2-extension.
Public Class Methods
const_missing(name)
click to toggle source
Autoloader.
# File lib/knj/gtk2.rb, line 4 def self.const_missing(name) require "#{$knjpath}gtk2_#{name.to_s.downcase}" return Knj::Gtk2.const_get(name) if Knj::Gtk2.const_defined?(name) raise "Could not load constant: '#{name}'." end
form(paras)
click to toggle source
Makes a Gtk::Table based on the given arguments.
Examples¶ ↑
Knj::Gtk2.form([{"type" => "text", "name" => "txtname", "title" => _("Name")}]) #=> {"table" => <Gtk::Table>, "objects" => <Array>}
# File lib/knj/gtk2.rb, line 203 def self.form(paras) table = Gtk::Table.new(paras.length, 2) table.row_spacings = 4 table.column_spacings = 4 top = 0 objects = {} paras.each do |item| if !item["type"] if item["name"][0..2] == "txt" or item["name"][0..2] == "tex" item["type"] = "text" elsif item["name"][0..2] == "sel" item["type"] = "select" elsif item["name"][0..2] == "che" item["type"] = "check" else raise "Could not figure out type for: '#{item["name"]}'." end end if item["type"] == "text" or item["type"] == "password" label = Gtk::Label.new(item["title"]) label.xalign = 0 text = Gtk::Entry.new if item["type"] == "password" text.visibility = false end if item["default"] text.text = item["default"] end table.attach(label, 0, 1, top, top + 1, Gtk::FILL, Gtk::FILL) table.attach(text, 1, 2, top, top + 1, Gtk::EXPAND | Gtk::FILL, Gtk::SHRINK) objects[item["name"]] = { "type" => "text", "object" => text } elsif item["type"] == "check" check = Gtk::CheckButton.new(item["title"]) table.attach(check, 0, 2, top, top + 1, Gtk::EXPAND | Gtk::FILL, Gtk::SHRINK) objects[item["name"]] = { "type" => "check", "object" => check } elsif item["type"] == "select" label = Gtk::Label.new(item["title"]) label.xalign = 0 cb = Gtk::ComboBox.new cb.init(item["opts"]) table.attach(label, 0, 1, top, top + 1, Gtk::FILL, Gtk::FILL) table.attach(cb, 1, 2, top, top + 1, Gtk::EXPAND | Gtk::FILL, Gtk::SHRINK) objects[item["name"]] = { "type" => "text", "object" => cb } else raise "Unknown type: '#{item["type"]}'." end top += 1 end return { "table" => table, "objects" => objects } end
form_getval(object)
click to toggle source
Returns the value of an object regardless of that type the object is.
Examples¶ ↑
Knj::Gtk2.form_getval(text_obj) #=> "Hejsa" Knj::Gtk2.form_getval(checkbox_obj) #=> "1"
# File lib/knj/gtk2.rb, line 301 def self.form_getval(object) if object.is_a?(Gtk::Entry) return object.text elsif object.is_a?(Gtk::CheckButton) if object.active? return "1" else return "0" end elsif object.is_a?(Gtk::ComboBox) sel = object.sel return sel["text"] else raise "Unknown object: '#{object.class.name}'." end end
form_setval(object, val)
click to toggle source
Takes a given object and sets its value.
Examples¶ ↑
Knj::Gtk2.form_setval(text_obj, "Hejsa") Knj::Gtk2.form_setval(checkbox_obj, 1)
# File lib/knj/gtk2.rb, line 283 def self.form_setval(object, val) if object.is_a?(Gtk::Entry) object.text = val.to_s elsif object.is_a?(Gtk::CheckButton) if val.to_s == "1" object.active = true else object.active = false end elsif object.is_a?(Gtk::ComboBox) object.sel = val.to_s end end
msgbox(*args, &block)
click to toggle source
Alias for self.msgbox.
# File lib/knj/gtk2.rb, line 11 def self.msgbox(*args, &block) return Knj::Gtk2.msgbox(*args, &block) end
translate(builderob)
click to toggle source
Takes a Gtk::Builder-object and runs labels and titles through GetText.gettext
in order to translate them.
Examples¶ ↑
Knj::Gtk2.translate(builder_obj)
# File lib/knj/gtk2.rb, line 188 def self.translate(builderob) builderob.objects.each do |object| class_str = object.class.to_s if object.is_a?(Gtk::Label) or object.is_a?(Gtk::Button) object.label = GetText.gettext(object.label) if !object.label.to_s.strip.empty? elsif object.is_a?(Gtk::Window) object.title = GetText.gettext(object.title) if !object.title.to_s.strip.empty? end end end