lib/knj/gtk2_tv.rb in knjrbfw-0.0.41 vs lib/knj/gtk2_tv.rb in knjrbfw-0.0.42

- old
+ new

@@ -1,32 +1,89 @@ module Knj::Gtk2::Tv def self.init(tv, columns) - args = [] - columns.each do |pair| - args << String + ret = { + :renderers => [] + } + + model_args = [] + columns.each do |args| + if args.is_a?(String) + args = {:type => :string, :title => args} + end + + if args[:type] == :string + model_args << String + elsif args[:type] == :toggle + model_args << Integer + elsif args[:type] == :combo + model_args << String + else + raise "Invalid type: '#{args[:type]}'." + end end - list_store = Gtk::ListStore.new(*args) + list_store = Gtk::ListStore.new(*model_args) tv.model = list_store count = 0 - columns.each do |col_title| - renderer = Gtk::CellRendererText.new - col = Gtk::TreeViewColumn.new(col_title, renderer, :text => count) - col.resizable = true - tv.append_column(col) + columns.each do |args| + if args.is_a?(String) + args = {:type => :string, :title => args} + end + + if args[:type] == :string + renderer = Gtk::CellRendererText.new + col = Gtk::TreeViewColumn.new(args[:title], renderer, :text => count) + col.resizable = true + tv.append_column(col) + elsif args[:type] == :toggle + renderer = Gtk::CellRendererToggle.new + col = Gtk::TreeViewColumn.new(args[:title], renderer, :active => count) + tv.append_column(col) + elsif args[:type] == :combo + renderer = Gtk::CellRendererCombo.new + renderer.text_column = 0 + + col = Gtk::TreeViewColumn.new(args[:title]) + col.pack_start(renderer, false) + col.add_attribute(renderer, :text, count) + + renderer.model = args[:model] if args.key?(:model) + tv.append_column(col) + else + raise "Invalid type: '#{args[:type]}'." + end + count += 1 + + ret[:renderers] << renderer end + + return ret end def self.append(tv, data) iter = tv.model.append count = 0 data.each do |value| - iter[count] = value.to_s + col = tv.columns[count] + renderer = col.cell_renderers.first + + if renderer.is_a?(Gtk::CellRendererText) + iter[count] = value.to_s + elsif renderer.is_a?(Gtk::CellRendererToggle) + iter[count] = Knj::Strings.yn_str(value, 1, 0) + elsif renderer.is_a?(Gtk::CellRendererCombo) + iter[count] = value.to_s + else + raise "Unknown renderer: '#{renderer.class.name}'." + end + count += 1 end + + return {:iter => iter} end def self.sel(tv) selected = tv.selection.selected_rows \ No newline at end of file