Class: Interphase::ListView

Inherits:
Widget
  • Object
show all
Defined in:
lib/interphase/widgets/list_view.rb

Overview

A list view which can be populated with objects.

Direct Known Subclasses

SimpleListView

Instance Attribute Summary collapse

Attributes inherited from Widget

#gtk_instance, #name, #parent

Instance Method Summary collapse

Methods inherited from Widget

#destroy, #method_missing, #on, #respond_to_missing?, #show, #size

Constructor Details

#initialize(columns, **options, &block) ⇒ ListView

Create a new list view.

columns

The columns which this list view has, as an Array of String objects.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/interphase/widgets/list_view.rb', line 14

def initialize(columns, **options, &block)
  @store = Gtk::ListStore.new(*[String] * columns.length)

  super(Gtk::TreeView.new(@store), options, &block)

  # Init columns
  columns.each_with_index do |col, index|
    renderer = Gtk::CellRendererText.new
    new_col = Gtk::TreeViewColumn.new(col[0], renderer, text: index)
    gtk_instance.append_column(new_col)
  end

  @rows = Interphase::Helpers::Observable.new([]) { refresh_rows }

  refresh_rows
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Interphase::Widget

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows


9
10
11
# File 'lib/interphase/widgets/list_view.rb', line 9

def rows
  @rows
end

Instance Method Details

#refresh_rowsObject

Refreshes the contents of the list view according to its rows. This is called automatically upon mutating #rows.


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/interphase/widgets/list_view.rb', line 33

def refresh_rows
  @store.clear

  # Insert the rows
  @rows.each do |data_row|
    store_row = @store.append

    # Basically a memcpy
    data_row.each_with_index do |item, index|
      store_row[index] = item
    end
  end
end