Module: GtkApp::ViewHelpers

Included in:
View
Defined in:
lib/gtk_app/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#build_listview(widget_name, columns, options = {}) {|iter, header, column, renderer| ... } ⇒ Object

Simple way to setup a listview.

Examples:

Build a simple listview.

@view.build_listview(:listThings, {id: Integer, name: String})

Parameters:

  • Widget (Symbol)

    name

  • (Hash)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :formatter (Proc)
  • (Array)

Yields:

  • (iter, header, column, renderer)

    Core listview elements are yielded for each column.

Yield Parameters:

  • iter (Integer)

    Column index

  • header (String)

    Column header text

  • column (Gtk::TreeViewColumn)

    The current column

  • renderer (Gtk::CellRenderer)

    Current column cell renderer

Raises:

  • (ExceptionClass)

    If an unhandled column data type is provided, an exception is raised.



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
# File 'lib/gtk_app/view_helpers.rb', line 19

def build_listview(widget_name, columns, options={}, &block)
  list = self.send(:"#{widget_name}")
  list.model = Gtk::ListStore.new(*columns.values)
  columns.each_with_index do |keyval, index|
    header, data_type = keyval
    renderer, attrs = case "#{data_type}".to_sym
    when :String, :Integer
      [Gtk::CellRendererText.new, text: index]
    when :TrueClass
      toggle = Gtk::CellRendererToggle.new
      toggle.signal_connect('toggled') do |widget, path|
        iter = list.model.get_iter(path)
        iter[index] = !iter[index]
      end
      [toggle, active: index]
    when :'Gtk::ListStore'
      _renderer = Gtk::CellRendererCombo.new
      model = Gtk::ListStore.new(String)
      _renderer.signal_connect("edited") do |cell, path, text|
        model.get_iter(path)[index] = text
      end
      [_renderer, text_column: 0, model: model, text: index, editable: index]
    else
      raise("GtkApp::View##{__method__} does not know how to handle " + 
        "'#{data_type}' data types.")
    end

    column = Gtk::TreeViewColumn.new("#{header}".titleize, renderer, attrs)
    column.visible = false if index == 0
    yield(index, header, column, renderer) if block_given?

    if options.key?(:formatter) && options[:formatter].is_a?(Proc)
      column.set_cell_data_func(renderer, &options[:formatter])
    end

    list.append_column(column)
  end
end

#desensitize(: lblFoo, : lblBar) ⇒ Object #desensitize(: cmbBar) ⇒ Object

Set widget(s) sensitivity property to false



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

def desensitize(*widgets)
  widgets.each { |w| self["#{w}"].sensitive = false }
end

#sensitize(: lblFoo, : txtFoo) ⇒ Object #sensitize(: cmbBar) ⇒ Object

Set widget(s) sensitivity property to true



61
62
63
# File 'lib/gtk_app/view_helpers.rb', line 61

def sensitize(*widgets)
  widgets.each { |w| self["#{w}"].sensitive = true }
end