Forum: wxRuby Grid#get_selected_rows not working?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thomas S. (Guest)
on 2009-03-12 21:00
(Received via mailing list)
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?
Alex F. (Guest)
on 2009-03-13 01:18
(Received via mailing list)
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
Thomas S. (Guest)
on 2009-03-13 02:42
(Received via mailing list)
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.
Alex F. (Guest)
on 2009-03-13 10:08
(Received via mailing list)
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...

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
This topic is locked and can not be replied to.