Module: GladeGUI

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#builderGtk::Builder

Returns The builder that holds references to everything in the glade form.

Returns:

  • (Gtk::Builder)

    The builder that holds references to everything in the glade form.



89
90
91
# File 'lib/GladeGUI.rb', line 89

def builder
  @builder
end

Instance Method Details

#buttonCancel__clicked(*a) ⇒ Object

Convenience method so you can just make a button named “buttonCancel” and it will work. This method isn’t called in code, its triggered by a user clicking a button named “buttonCancel”.



417
418
419
# File 'lib/GladeGUI.rb', line 417

def buttonCancel__clicked(*a)
  @builder[:window1].close
end

#extract_key(widget) ⇒ String

retrieves the key inside a glade control name. Useful when handling events where the control name is returned, and you need to match it to an actual hash.

Examples:

# Widget in glade form has name of "button[5]"
var = @builder["button[5]"]
extract_key(var) => "5"

Parameters:

  • widget (String)

    Name for the widget in the glade form.

Returns:

  • (String)

    the key value.



440
441
442
# File 'lib/GladeGUI.rb', line 440

def extract_key(widget)
  widget.builder_name.scan(/\[(.+?)\]/).flatten[0]
end

#get_glade_active_record(obj) ⇒ Object



467
468
469
470
471
472
473
# File 'lib/GladeGUI.rb', line 467

def get_glade_active_record(obj) 
  return if not defined? obj.attributes
  obj.attributes.each_pair do |key, val|
    new_val = get_control_value(key, obj)
    obj.send("#{key}=", new_val) unless new_val.nil?  
  end
end

#get_glade_all(obj = self) ⇒ Object

This method is the most useful method to retreive values from a glade form. It will

populate from active_record fields and instance variables.  It will simply 
call both of these methods:

 get_glade_active_record()
 get_glade_variables()

So, to retreive all the values of a form back into your ActiveRecord object and instance variables, simply call the set_glade_all() method instead.


213
214
215
216
# File 'lib/GladeGUI.rb', line 213

def get_glade_all(obj = self)
  get_glade_active_record(obj)
  get_glade_variables(obj)
end

#get_glade_variables(obj = self) ⇒ Object

Populates your instance variables from the glade form. This works for Gtk:Button, Gtk::Entry, Gtk::Label and Gtk::Checkbutton. So instead of having to assign instance variable:

You can write one line of code:

The optional parameter is seldom used because you usually want the glade form to populate from the calling class. If you passed another object, the form would populate from it.

Examples:

@name = @builder["name"].text 
@address = @builder["address"].text 
@eamil = @builder["email"].text 
@phone = @builder["phone"].text 
get_glade_variables()

Parameters:

  • obj (Object) (defaults to: self)

    Any object with instance variables with same names as in galde form.

Returns:

  • none



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/GladeGUI.rb', line 322

def get_glade_variables(obj = self)
  obj.instance_variables.each do |var_name|
    next if var_name == :@builder  # or var_name == :@top_level_window
    var = obj.instance_variable_get(var_name)
    var_name = var_name.to_s.gsub("@", "")  #fix for ruby 1.9 giving symbols
    if var.is_a? Hash
      var.each_pair do |key, val|
        if glade_value = get_control_value("#{var_name}[#{key.to_s}]", obj)
          var[key] = glade_value
        end 
      end
      obj.instance_variable_set("@"+ var_name, var)
    elsif var.is_a? Array
      var.each_index do |i|
        if glade_value = get_control_value("#{var_name}[#{i.to_s}]", obj)
          var[i] = glade_value
        end
      end
      obj.instance_variable_set("@"+ var_name, var)
    else
      glade_value = get_control_value(var_name, obj)
      obj.instance_variable_set("@"+ var_name, glade_value) unless glade_value.nil?
    end
  end
end

#load_gladeObject

This will Load the glade form according to the naming convention:

MyClass.rb => MyClass.glade.

It will create a Gtk::Builder object from your glade file. The Gtk::Builder object is stored in the instance variable, @builder. You can get a reference to any of the widgets on the glade form by using the @builder object:

@example:

name_entry_box = @builder["ui_name_ent"]
  or
@builder["ui_name_ent"].text = "This will show in the window!"

Normally, you should give your widgets names of instance variables: i.e. @email so they can be autoloaded when the glade form is shown using the #show_glade method. For example, the value of the @email vaiable would be loaded into a Gtk:Entry named “email” in your glade form. It saves you from having to do this:

@example: @builder.text = @email



134
135
136
137
138
139
# File 'lib/GladeGUI.rb', line 134

def load_glade() 
  caller__FILE__ = my_class_file_path()    
  file_name = File.join(File.split(caller__FILE__)[0] , "glade", my_class_name(self) + ".glade")
  @builder = Gtk::Builder.new(file: file_name)
  @builder.connect_signals{ |handle| method(handle) }
end

#parse_signalsObject

Connects gtk’s signals to your methods according to the naming convention widget__signal. For example,

when you place a button called "button1" in your glade form, and declare a method called 
"button1__clicked", they aren't connected to each other.  Clicking on the button does nothing
and the method never gets called.  After running parse_signals(), the "clicked" signal is
connected to the method named "button1__clicked" so when the user clicks the button, the method is called.

Remember that it will enforce the naming convention: name__signal (two underscores).

Examples:

methods

button1__clicked(*args)
self__row_activated(*args)
instance_variable__key_press_event(*args)


157
158
159
160
161
162
163
164
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
# File 'lib/GladeGUI.rb', line 157

def parse_signals()
  meths = self.class.instance_methods().select { |m| m.name.include?("__") } 
  meths.each do |meth|
    meth = meth.to_s #bug fix ruby 1.9 gives stmbol
    glade_name, signal_name = *(meth.split("__"))
    next if (signal_name.to_s == "" or glade_name.to_s == "") #covers nil
#      if @builder
      @builder.objects.each do |obj|
        next unless obj.respond_to?(:builder_name)
        if obj.builder_name == glade_name or obj.builder_name =~ /^(?:#{my_class_name(self)}\.|)#{glade_name}\[[a-zA-Z\d_-]+\]$/ #arrays
          obj.signal_connect(signal_name) { |*args| method(meth.to_sym).call(*args) } 
        end
#        end
    end
    obj = glade_name == "self" ? self : self.instance_variable_get("@" + glade_name)
    obj ||= eval(glade_name) if respond_to?(glade_name) and method(glade_name.to_sym).arity == 0 # no arguments!
    if obj.respond_to?("signal_connect")
      obj.signal_connect(signal_name) { |*args| method(meth.to_sym).call(*args) }
    end
  end
  # now parse instance variables looking for "self__"
  instance_variables.each do |var|
    obj = self.instance_variable_get(var)
    meths = obj.class.instance_methods().select { |m| m.name.include?("self__") } 
    meths.each do |meth|
      meth = meth.to_s
      signal_name = meth.split("__")[1]
      if obj.respond_to?("signal_connect")
        obj.signal_connect(signal_name) { |*args| obj.method(meth.to_sym).call(*args) }
      end  
    end
  end  
end

#set_drag_drop(hash) ⇒ Object

drag_to() will make it so you can drag-n-drop the source_widget onto the target widget.

You may pass a reference to a widget object, or a String that gives the name of the
widget on your glade form. So, it functions the same as this statement:

    widget_source.drag_to(widget_target)

It also functions the same as this statement:

    @builder["widget_source"].drag_to(@builder["widget_target"])


102
103
104
105
106
107
108
109
# File 'lib/GladeGUI.rb', line 102

def set_drag_drop(hash)
  hash.each do |key,val|
    src = key.is_a?(Gtk::Widget) ? key : @builder[key]
    target = @builder[val]
    src.extend(VR::Draggable) unless src.is_a?(VR::Draggable)
    src.add_target_widget(target)
  end
end

#set_glade_active_record(obj = self) ⇒ Object

Populates the glade form from the fields of an ActiveRecord object. So instead of having to assign each widget a value: You can write one line of code: The optional parameter is seldom used because you usually want the glade form to populate from the calling class. If you passed another object, the form would populate from it.

Examples:

@builder["name"].text = @name
@builder["address"].text = @address
@builder["email"].text = @eamil
@builder["phone"].text = @phone
set_glade_active_record()

Parameters:

  • obj (ActiveRecord::Base) (defaults to: self)

    Any activerecod base object.

Returns:

  • none



462
463
464
465
# File 'lib/GladeGUI.rb', line 462

def set_glade_active_record(obj = self)
  return if not defined? obj.attributes
  obj.attributes.each_pair { |key, val| fill_control(my_class_name(obj) + "." + key, val) }
end

#set_glade_all(obj = self) ⇒ Object

This method is the most useful method to populate a glade form. It will

populate from active_record fields and instance variables.  It will simply 
call both of these methods:

 set_glade_active_record()
 set_glade_variables()

So, to set all the values of a form, simply call the set_glade_all() method instead.


200
201
202
203
# File 'lib/GladeGUI.rb', line 200

def set_glade_all(obj = self) 
  set_glade_active_record(obj)
  set_glade_variables(obj)
end

#set_glade_hash(hash) ⇒ Object

Matches names in glade form to keys in a Hash.

Parameters:

  • hash (Hash)

    The hash with keys that match the names in the glade form.



222
223
224
225
# File 'lib/GladeGUI.rb', line 222

def set_glade_hash(hash)
  return unless hash.is_a?(Hash)
  hash.each { |key,val| fill_control( key.to_s, val.to_s) }
end

#set_glade_variables(obj = self) ⇒ Object

Populates the glade form from the instance variables of the class. So instead of having to assign each widget a value:

@builder["name"].text = @name
@builder["address"].text = @address
@builder["email"].text = @eamil
@builder["phone"].text = @phone

you can write one line of code:

set_glade_variables()

The optional parameter is seldom used because you usually want the glade form to populate from the calling class. If you passed another object, the form would populate from it.

obj - type Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/GladeGUI.rb', line 245

def set_glade_variables(obj = self)
  obj.instance_variables.each do |name|
    name = name.to_s #ruby 1.9 passes symbol!
    v = obj.instance_variable_get(name)
    name = name.gsub('@', '')
    if v.is_a?(Array) 
      v.each_index do |i|
        fill_control("#{name}[#{i.to_s}]", v[i] )
      end
    elsif v.is_a?(Hash)
      v.each_pair do |key, val|
        fill_control("#{name}[#{key.to_s}]", val)
      end        
    else
      fill_control(name, v)
    end
  end
end

#show_glade(parent = nil) ⇒ Object

The method you call to show the glade form.

It loads the glade form, sets all the form’s widgets to your instance variables, connects all your methods to their signals, and starts Gtk.main loop if necessary.

Parameters:

  • parent (Object #builder) (defaults to: nil)

    Optional parent that this window will always be on top of.

Returns:

  • nothing.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/GladeGUI.rb', line 386

def show_glade(parent = nil)
  load_glade()
  if @builder[:window1].nil? 
    alert "Error:  You need to name your window, 'window1' in glade.  Edit glade file."
    return
  end
#    @builder[:window1].screen = parent.screen
  if parent then
      @builder[:window1].transient_for = parent.builder[:window1]
  end
  @builder[:window1].show_all
  before_show() if respond_to? :before_show
  parse_signals()
  set_glade_all()
#     @top_level_window = Gtk.main_level == 0
  @builder.class.send(:attr_accessor, "top_level_window")
  @builder.top_level_window = Gtk.main_level == 0
#     if Gtk.main_level == 0
#       @builder[:window1].modal = true
#     end 
  Gtk.main if  @builder[:window1].modal? or @builder.top_level_window 
end

#try_to_select_text_in_combobox(cb, text) ⇒ Object



289
290
291
292
293
294
295
296
297
298
# File 'lib/GladeGUI.rb', line 289

def try_to_select_text_in_combobox(cb, text)
  i = 0
  cb.model.each do |model, path, iter|
    if iter[0] == text
      cb.active = i
      return
    end  
    i = i + 1
  end
end

#window1__destroy(*args) ⇒ Object

Called when window is destroyed when you execute: @builder[:window1].destroy It manages the Gtk.main loop for all the windows.



411
412
413
# File 'lib/GladeGUI.rb', line 411

def window1__destroy(*args)
  Gtk.main_quit if @builder["window1"].modal? or @builder.top_level_window 
end