lib/ppcurses/table_view.rb in ppcurses-0.1.1 vs lib/ppcurses/table_view.rb in ppcurses-0.1.2

- old
+ new

@@ -20,10 +20,12 @@ def initialize super origin = Point.new( 2, 2 ) setFrameOrigin(origin) + + @table_columns = [] end # A data source must implement a formal protocol # @@ -41,24 +43,55 @@ for i in 0..@data_source.number_of_rows_in_table(self)-1 x = @data_source.object_value_for(self, 0, i).length if x > width then width = x end end + # Add an extra line for the column header + # and ====== divider + height += 2 + sz = Size.new( width, height ) setFrameSize( sz ) end def display(screen) y = @frame.origin.y x = @frame.origin.x + + # display column header + screen.setpos(y,x) + @table_columns.each_with_index do |column,i| + screen.addstr(column.identifier.center(column.width)) + if i < @table_columns.length - 1 then screen.addstr(' | ') end + end + + y += 1 + screen.setpos(y,x) + # Display ================= divider + @table_columns.each_with_index do |column, i| + screen.addstr( ''.center(column.width, '=') ) + if i < @table_columns.length - 1 then screen.addstr('===') end + end + + y += 1 + for i in 0..@data_source.number_of_rows_in_table(self)-1 screen.setpos(y,x) screen.attron(Curses::A_REVERSE) if i == selected_row - screen.addstr(@data_source.object_value_for(self, 0, i) ) + + + @table_columns.each_with_index do |col,j| + col_str = @data_source.object_value_for(self, j, i) + display_string = col_str.ljust(col.width) + screen.addstr( display_string ) + if j < @table_columns.length - 1 then screen.addstr(' | ') end + end + + screen.attroff(Curses::A_REVERSE) if i == selected_row y += 1 end end @@ -90,13 +123,52 @@ end end + def number_of_columns + num_col = 0 + + while @data_source.object_value_for(self, num_col, 0) != nil do + num_col += 1 + end + + + num_col + end + + ## Column Management + + def add_table_column( col ) + col.table_view = self + @table_columns.push(col) + end + end +# ========================================================================================== +# +# Based loosely on ... +# https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableColumn_Class/index.html#//apple_ref/swift/cl/NSTableColumn +# +# The TableColumn class stores the display characteristics and identifier for a column +# in a TableView instance. A table column object determines the width of its column. +class TableColumn + attr_accessor :identifier + attr_accessor :width + attr_accessor :table_view + + def initialize( identifier, width = 5 ) + @identifier = identifier + @width = width + end + +end + + +# ========================================================================================== # Based on ... # # https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/index.html#//apple_ref/occ/intf/NSTableViewDataSource class SingleColumnDataSource @@ -112,14 +184,28 @@ end # Called by the table view to return the data object associated with # the specified row and column. def object_value_for(aTableview, column, row_index) + if column > 0 then + return nil + end + @values[row_index] end end - +# Values in the constructor is expected to be a two sided array +class MultipleColumnDataSource < SingleColumnDataSource + + # Called by the table view to return the data object associated with + # the specified row and column. + def object_value_for(aTableview, column, row_index) + @values[row_index][column] + end + + +end class TableViewDataSource def TableViewDataSource.from_string_array(values) SingleColumnDataSource.new(values)