Class: Knj::Gtk2

Inherits:
Object show all
Defined in:
lib/knj/gtk2.rb

Overview

Contains various methods for doing stuff quick using the Gtk2-extension.

Defined Under Namespace

Modules: Cb, Tv Classes: Menu, Msgbox, StatusWindow, Window

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object

Autoloader.



4
5
6
7
8
# 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) ⇒ Object

Makes a Gtk::Table based on the given arguments.

Examples

Knj::Gtk2.form([=> “text”, “name” => “txtname”, “title” => _(“Name”)]) #=> => <Gtk::Table>, “objects” => <Array>



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# 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) ⇒ Object

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”



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# 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) ⇒ Object

Takes a given object and sets its value.

Examples

Knj::Gtk2.form_setval(text_obj, “Hejsa”) Knj::Gtk2.form_setval(checkbox_obj, 1)



283
284
285
286
287
288
289
290
291
292
293
294
295
# 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(paras, type = "warning", title = nil) ⇒ Object

Shows a dialog on the screen based on various arguments.

Examples

Knj::Gtk2.msgbox(“Message”, “Title”, “info”) Knj::Gtk2.msgbox(“Question”, “Title”, “yesno”) #=> “yes”|“no”|“cancel”|“close”



11
12
13
# File 'lib/knj/gtk2.rb', line 11

def self.msgbox(*args, &block)
  return Knj::Gtk2.msgbox(*args, &block)
end

.translate(builderob) ⇒ Object

Takes a Gtk::Builder-object and runs labels and titles through GetText.gettext in order to translate them.

Examples

Knj::Gtk2.translate(builder_obj)



188
189
190
191
192
193
194
195
196
197
198
# 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