Grid#get_selected_rows not working?


#1

Working with wxGrid. I can’t seem to get any results for
#get_selection_rows. The array is alwasy empty. I ended up having to
something tricky:

  rows = []  # @grid.get_selection_rows (not working)
  top = @grid.get_selection_block_top_left.map{ |r,c| r }
  bot = @grid.get_selection_block_bottom_right.map{ |r,c| r }
  top.each_with_index do |t, i|
    rows.concat((t..bot[i]).to_a)
  end

Did I miss something or is this in fact a bug?


#2

Hi Trans

Trans wrote:

Working with wxGrid. I can’t seem to get any results for
#get_selection_rows.

Hmm, it (get_selected_rows, there’s not a method get_selection_rows)
seems to work OK to me. I tweaked the grid sample line 27 so it reads:

evt_menu(1003) { @grid.select_row(1); p @grid.selected_rows }

And that returns [1].

However (any maybe this is where the confusion lies) Grid#selected_rows
only returns rows that have been selected as such, ie by clicking on the
row label, or programmatically with select_row.

Rows that have some cells, or even all cells selected by click-dragging
won’t be returned, because they’re not ‘selected-as-rows’. If that’s
what you need, then I guess you may have to resort to more complicated
strategies:

  rows = []  # @grid.get_selection_rows (not working)
  top = @grid.get_selection_block_top_left.map{ |r,c| r }
  bot = @grid.get_selection_block_bottom_right.map{ |r,c| r }
  top.each_with_index do |t, i|
    rows.concat((t..bot[i]).to_a)
  end

Note that if you want all rows where anything is selected, you’ll also
need to call get_selected_cells to get selections not in a block. See
below for a suggested useful method

Did I miss something or is this in fact a bug?

Not a bug, I think, but definitely not something very clear in the docs.
And the API for multiple selections is not exactly comfortable. In
another version we should probably add a simpler method like:

Returns an array of co-ordinates of every selected cell.

def get_all_selected_cells
cells = []
selection_block_top_left.zip(selection_block_bottom_right) do |
coords |
x1, y1, x2, y2 = coords.flatten
(x1…x2).each { | x | (y1…y2).each { | y | cells << [x, y] } }
end
selected_cells.each { | c | cells << c }
cells.sort!
cells
end

With this you could get all rows with any selection with just

all_selected_cells.map { | c | c.first }.uniq

a


#3

Trans wrote:

Hmm… that seems odd to me since I’m using row selection mode

@grid.set_selection_mode(1)

But yea, that seems to be the problem.

You didn’t mention that; it definitely seems odd to me that it works
that way with the row grid select mode on. But it seems to be intended:

http://trac.wxwidgets.org/ticket/2576
http://lists.wxwidgets.org/pipermail/wx-users/2007-February/097640.html

You could file a bug/patch for wxWidgets to request a fix.

def get_all_selected_cells

Yes, something easier to use would be very helpful.

Well, feel free to make use of it as it stands … with complex widgets
like Grid subclassing is often a neater way to organise code anyway.

cheers
a


#4

On Thu, Mar 12, 2009 at 6:42 PM, Alex F. removed_email_address@domain.invalid wrote:

 evt_menu(1003) { @grid.select_row(1); p @grid.selected_rows }

And that returns [1].

However (any maybe this is where the confusion lies) Grid#selected_rows only
returns rows that have been selected as such, ie by clicking on the row
label, or programmatically with select_row.

Hmm… that seems odd to me since I’m using row selection mode

@grid.set_selection_mode(1)

But yea, that seems to be the problem.

   end
version we should probably add a simpler method like:
  cells
end

With this you could get all rows with any selection with just

 all_selected_cells.map { | c | c.first }.uniq

Yes, something easier to use would be very helpful.

Thanks,
T.