Module: Knj::Gtk2

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, StatusWindow, Window

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object

Autoloader.



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



194
195
196
197
198
199
200
201
202
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
# File 'lib/knj/gtk2.rb', line 194

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”



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/knj/gtk2.rb', line 292

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)



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/knj/gtk2.rb', line 274

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”



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/knj/gtk2.rb', line 18

def self.msgbox(paras, type = "warning", title = nil)
  if paras.is_a?(Array)
    msg = paras[0]
    title = paras[2]
    type = paras[1]
  elsif paras.is_a?(Hash)
    msg = paras["msg"]
    title = paras["title"]
    type = paras["type"]
  elsif paras.is_a?(String) or paras.is_a?(Integer)
    msg = paras
  else
    raise "Cant handle the parameters: '#{paras.class.name}'."
  end
  
  type = "info" if !type
  
  if !title
    if type == "yesno"
      title = "Question"
    elsif type == "info"
      title = "Message"
    else
      title = "Warning"
    end
  end
  
  close_sig = "close"
  cancel_sig = "cancel"
  ok_sig = "ok"
  yes_sig = "yes"
  no_sig = "no"
  
  box = Gtk::HBox.new
  
  if type == "yesno"
    button1 = [Gtk::Stock::YES, Gtk::Dialog::RESPONSE_YES]
    button2 = [Gtk::Stock::NO, Gtk::Dialog::RESPONSE_NO]
    
    image = Gtk::Image.new(Gtk::Stock::DIALOG_QUESTION, Gtk::IconSize::DIALOG)
  elsif type == "warning"
    button1 = [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
    image = Gtk::Image.new(Gtk::Stock::DIALOG_WARNING, Gtk::IconSize::DIALOG)
  elsif type == "info"
    button1 = [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
    image = Gtk::Image.new(Gtk::Stock::DIALOG_INFO, Gtk::IconSize::DIALOG)
  elsif type == "list"
    close_sig = false
    cancel_sig = false
    
    button1 = [Gtk::Stock::OK, Gtk::Dialog::RESPONSE_OK]
    
    tv = Gtk::TreeView.new
    tv.init([_("ID"), _("Title")])
    tv.columns[0].visible = false
    
    if paras["items"].is_a?(Hash)
      paras["items"].each do |key, value|
        tv.append([key, value])
      end
    elsif paras["items"].is_a?(Array)
      count = 0
      paras["items"].each do |element|
        if element.respond_to?("id") and element.respond_to?("title")
          tv.append([count.to_s, element.title])
        else
          raise "Could not handle object in array: '#{element.class.name}'."
        end
        
        count += 1
      end
    else
      raise "Unhandeled class: '#{items.class.name}'."
    end
    
    sw = Gtk::ScrolledWindow.new
    sw.add(tv)
    
    box.pack_start(sw)
  else
    raise "No such mode: '#{type}'."
  end
  
  if button1 and button2
    dialog = Gtk::Dialog.new(title, nil, Gtk::Dialog::MODAL, button1, button2)
  else
    dialog = Gtk::Dialog.new(title, nil, Gtk::Dialog::MODAL, button1)
  end
  
  box.pack_start(image) if image
  
  if msg
    label = Gtk::Label.new(msg)
    label.selectable = true
    box.pack_start(label)
  end
  
  box.spacing = 15
  dialog.border_width = 5
  dialog.vbox.add(box)
  dialog.has_separator = false
  dialog.show_all
  
  if type == "list"
    dialog.set_size_request(250, 370)
    tv.grab_focus
  end
  
  if paras.is_a?(Hash) and paras["transient_for"]
    dialog.transient_for = paras["transient_for"]
  end
  
  do_run = true
  do_run = false if paras.is_a?(Hash) and paras.key?("run") and !paras["run"]
  
  if do_run
    response = dialog.run
  else
    #Connect the one button to close the window.
    dialog.children[0].children[1].children[0].signal_connect("clicked") do
      dialog.destroy
    end
    
    return false
  end
  
  if type == "list"
    sel = tv.sel
    
    if sel and sel[0]
      if paras["items"].is_a?(Array) and paras["items"].length > 0 and sel and sel[0]
        trala = sel[0].to_i
        ok_sig = paras["items"][sel[0].to_i]
      else
        ok_sig = sel[0]
      end
    else
      ok_sig = false
    end
  end
  
  dialog.destroy
  
  if response == Gtk::Dialog::RESPONSE_OK
    return ok_sig
  elsif response == Gtk::Dialog::RESPONSE_YES
    return yes_sig
  elsif response == Gtk::Dialog::RESPONSE_NO
    return no_sig
  elsif response == Gtk::Dialog::RESPONSE_CANCEL
    return cancel_sig
  elsif response == Gtk::Dialog::RESPONSE_CLOSE or response == Gtk::Dialog::RESPONSE_DELETE_EVENT
    return close_sig
  else
    raise "Unknown response: '#{response}'."
  end
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)



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/knj/gtk2.rb', line 179

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)
    elsif object.is_a?(Gtk::Window)
      object.title = GetText.gettext(object.title)
    end
  end
end

Instance Method Details

#msgbox(*args, &block) ⇒ Object

Alias for self.msgbox.



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

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