Module: Knj::Gtk2

Defined in:
lib/knj/gtk2.rb

Defined Under Namespace

Modules: Cb, Tv Classes: Menu, StatusWindow

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.form(paras) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
# File 'lib/knj/gtk2.rb', line 165

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



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/knj/gtk2.rb', line 255

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



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/knj/gtk2.rb', line 241

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



11
12
13
14
15
16
17
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
# File 'lib/knj/gtk2.rb', line 11

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.to_s
  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.to_s
        end
        
        count += 1
      end
    else
      raise "Unhandeled class: " + items.class.to_s
    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
  
  if image
    box.pack_start(image)
  end
  
  if msg
    box.pack_start(Gtk::Label.new(msg))
  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
  
  response = dialog.run
  
  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.to_s
  end
end

.translate(builderob) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/knj/gtk2.rb', line 153

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(p1, p2 = "warning", p3 = "Warning") ⇒ Object



7
8
9
# File 'lib/knj/gtk2.rb', line 7

def msgbox(p1, p2 = "warning", p3 = "Warning")
  return Knj::Gtk2.msgbox(p1, p2, p3)
end