Class: Gtk3assist::Combobox

Inherits:
Object
  • Object
show all
Defined in:
lib/gtk3assist_combobox.rb

Overview

This class contains code than can optimize productivity when coding comboboxes.

Constant Summary collapse

ARGS_ALLOWED =

An array containing allowed arguments for the constructor.

[:cb]
ARGS_ALLOWED_ADD_ITEM =

An array containing allowed arguments for the ‘add_item’-method.

[:id, :title]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Combobox

Constructor.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gtk3assist_combobox.rb', line 13

def initialize(args = {})
  raise "'args' was not a hash." if !args.is_a?(Hash)
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ARGS_ALLOWED.include?(key)
  end
  
  @objs = {}
  
  if args[:cb]
    @cb = args[:cb]
  else
    @cb = Gtk::ComboBox.new
  end
  
  @rend = Gtk::CellRendererText.new
  @cb.pack_start(@rend, false)
  @cb.add_attribute(@rend, "text", 0)
  
  @model = Gtk::ListStore.new([GObject::TYPE_STRING, GObject::TYPE_STRING])
  @cb.set_model(@model)
  @cb.show
end

Instance Attribute Details

#cbObject (readonly)

The combobox-widget that this object handels.



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

def cb
  @cb
end

Instance Method Details

#add_item(args) ⇒ Object

Adds a new item to the combobox with ID and title.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gtk3assist_combobox.rb', line 37

def add_item(args)
  raise "'args' was not a hash." if !args.is_a?(Hash)
  args.each do |key, val|
    raise "Invalid argument: '#{key}'." if !ARGS_ALLOWED_ADD_ITEM.include?(key)
  end
  
  raise "No ':title' was given." if !args[:title]
  raise "No ':id' was given." if !args[:id]
  
  iter = @model.append
  @model.set_value(iter, 0, args[:title])
  
  @objs[args[:id]] = {:iter => iter}
end

#items(args = nil, &blk) ⇒ Object

Enumerates over every single item in the combobox.



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
# File 'lib/gtk3assist_combobox.rb', line 73

def items(args = nil, &blk)
  enum = Enumerator.new do |y|
    if args and args[:selected]
      iter_cur = @cb.get_active_iter.last
    else
      iter_cur = @model.iter_first.last
    end
    
    while iter_cur
      match = true
      
      if match
        y << {
          :data => {
            :title => @model.get_value(iter_cur, 0).get_string
          }
        }
      end
      
      break if !@model.iter_next(iter_cur) or (args and args[:selected])
    end
  end
  
  if blk
    enum.each(&blk)
  else
    return enum
  end
end

#selObject

Returns the selected item.



68
69
70
# File 'lib/gtk3assist_combobox.rb', line 68

def sel
  return self.items(:selected => true).next
end

#sel_id(args) ⇒ Object

Selects a certain item by id.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gtk3assist_combobox.rb', line 53

def sel_id(args)
  raise "'args' was not a hash." if !args.is_a?(Hash)
  raise "No ':id' was given." if !args.key?(:id)
  
  @objs.each do |id, data|
    if id == args[:id]
      @cb.set_active_iter(data[:iter])
      return nil
    end
  end
  
  raise "Could not find item by that id: '#{args[:id]}'."
end