From 314425ba13992a511b2993ace4531d951066eb30 Mon Sep 17 00:00:00 2001 From: Lars Kanis <kanis@comcard.de> Date: Thu, 13 Mar 2014 17:07:04 +0100 Subject: [PATCH] Add an example for overloading onCmdPasteSel to allow different behaviour on text pasted from the clipboard. --- examples/table.rb | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/table.rb b/examples/table.rb index 914a7b7..2d4f044 100755 --- a/examples/table.rb +++ b/examples/table.rb @@ -7,6 +7,28 @@ include Fox class TableWindow < FXMainWindow + + class MyFXTable < Fox::FXTable + include Responder + def initialize(*args, &block) + super + FXMAPFUNC(SEL_COMMAND, FXTable::ID_PASTE_SEL, 'onCmdPasteSel') + end + def onCmdPasteSel(sender, sel, ptr) + data = getDNDData(FROM_CLIPBOARD, FXTable.utf8Type) + if data && anythingSelected? + rows_cols = data.split("\n").map{|l| l.split("\t") } + rows_cols.each.with_index(selStartRow) do |cols, row_nr| + cols.each.with_index(selStartColumn) do |text, col_nr| + setItemText( row_nr, col_nr, text) + end + end + max_cols = rows_cols.inject(0){|s,cols| [s, cols.length].max } + selectRange(selStartRow, selStartRow+rows_cols.length-1, selStartColumn, selStartColumn+max_cols-1) + end + end + end + def initialize(app) # Call the base class initializer first super(app, "Table Widget Test", :opts => DECOR_ALL) @@ -32,8 +54,9 @@ class TableWindow < FXMainWindow frame = FXVerticalFrame.new(contents, FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, :padding => 0) - # Table - @table = FXTable.new(frame, + # Instead of the usual FXTable, we use a derived version, that allows us + # to paste "," into fields. A "," is used as column separator otherwise. + @table = MyFXTable.new(frame, :opts => TABLE_COL_SIZABLE|TABLE_ROW_SIZABLE|LAYOUT_FILL_X|LAYOUT_FILL_Y, :padding => 2) -- GitLab