Forum: wxRuby mouse click event at grid

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.
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-03-31 06:43
(Received via mailing list)
Dear all,

how do we create an mouse click event binding for a grid?
I look over the docs and cannot find any clue about it.
google did not help either.

Thank you
Regards
Hendra
06f6780c99d4a8dd71f2b474082ea9ce?d=identicon&s=25 Alex Fenton (Guest)
on 2009-03-31 08:45
(Received via mailing list)
hendra kusuma wrote:
> how do we create an mouse click event binding for a grid?
> I look over the docs and cannot find any clue about it.
> google did not help either.

evt_grid_cell_left_click

Documented here: http://docs.wxwidgets.org/stable/wx_wxgrid.html#wxgrid

Sorry, this appears to be missing from wxRuby docs. If in doubt, it can
be worth searching the wxWidgets docs as a backup. Just prepend 'wx' to
the class name for searching (eg wxGrid).

With any widget, you can also use the lower-level mouse event handlers,
eg grid.evt_left_up

alex
3396e4a3df8a840faec520af8555a400?d=identicon&s=25 Mario Steele (Guest)
on 2009-03-31 08:55
(Received via mailing list)
Hello Hendra,

You should be able to monitor evt_left_down, and evt_left_up for a click
event.  The actual detecting of a Click, would require that in
evt_left_down, you store the mouse position, and the time at which the
event
was generated, then on evt_left_up, take the current time, and subtract
the
time at which your program received evt_left_down, to see if it's within
a
normal time frame to qualify it as a click, and you would want to make
sure
that the mouse position matched the first one, then fire off your method
you
want to execute when a click occurs on the grid.

Take a look at the Event sample folder, for a way to do this, through a
custom event handling system.

hth,

Mario

2009/3/30 hendra kusuma <penguinroad@gmail.com>
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-04-01 08:33
(Received via mailing list)
On Tue, Mar 31, 2009 at 1:25 PM, Alex Fenton <alex@pressure.to> wrote:

>
> Sorry, this appears to be missing from wxRuby docs. If in doubt, it can be
> worth searching the wxWidgets docs as a backup. Just prepend 'wx' to the
> class name for searching (eg wxGrid).
>
> With any widget, you can also use the lower-level mouse event handlers, eg
> grid.evt_left_up
>
> alex


Thanks
It works, with an unexpected behavior thought
While I can catch the click event now,
 mouse click won't move the selection to clicked cell
anyway to fix this?
06f6780c99d4a8dd71f2b474082ea9ce?d=identicon&s=25 Alex Fenton (Guest)
on 2009-04-01 12:46
(Received via mailing list)
hendra kusuma wrote:
> On Tue, Mar 31, 2009 at 1:25 PM, Alex Fenton <alex@pressure.to
> <mailto:alex@pressure.to>> wrote:
>
>
>     With any widget, you can also use the lower-level mouse event
>     handlers, eg grid.evt_left_up
>

>
> It works, with an unexpected behavior thought
> While I can catch the click event now,
>  mouse click won't move the selection to clicked cell
> anyway to fix this?

If you trap an event like this, you must call "event.skip" in the event
handler if you want normal processing of the event (eg moving the
selection on a click) to proceed.

a
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-04-02 05:46
(Received via mailing list)
On Wed, Apr 1, 2009 at 5:36 PM, Alex Fenton <alex@pressure.to> wrote:

>
>> It works, with an unexpected behavior thought
>> While I can catch the click event now,
>>  mouse click won't move the selection to clicked cell
>> anyway to fix this?
>>
>
> If you trap an event like this, you must call "event.skip" in the event
> handler if you want normal processing of the event (eg moving the selection
> on a click) to proceed.
>

you mean something looks like this?
evt_grid_cell_left_click() { do_click() }

def do_click()
  puts 'ok'
  event.skip
end
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-04-02 06:47
(Received via mailing list)
On Thu, Apr 2, 2009 at 10:15 AM, hendra kusuma
<penguinroad@gmail.com>wrote:

>>>    With any widget, you can also use the lower-level mouse event
>> If you trap an event like this, you must call "event.skip" in the event
> end
>

Ups, Not working

Can you give me suggestion on my problem?
This is what I want to do

I fill datagrid with all data from a table
first column is boolean, an is set with set_col_format_bool (appear as
checkbox)
I set all other columns to readonly
so when I click the first column, it will change the value (true or
false)
and update the table (the primary key is at 2nd column)

current state
I click cell on first column, move focus to clicked-cell
click the cell again, cell change to textbox, I can change the value
there

expected behavior
click cell on first column, and it toggle the value between checked and
unchecked

Thanks
Hendra
3ed7378b8a83d2952488d4edcd22c406?d=identicon&s=25 Stephen Belcher (Guest)
on 2009-04-02 13:52
(Received via mailing list)
I believe there are a couple of problems.  First, you haven't actually
passed the GridEvent to your handler.  Second, it seems you only want to
call event.skip if event.get_col is 0 (I've never used a Grid, I'm
assuming
the cols and rows are 0-indexed); It sounds like you want normal event
processing to stop if they simply check the checkbox in the first
column, ie
the cell doesn't get highlighted afterwards, and then normal event
processing to continue on any other column.  I think the code you want
should probably look more like this:

evt_grid_cell_left_click() { |e| do_click(e) } # pass GridEvent as e

def do_click(event) # get the event passed earlier
  puts 'ok'
  event.skip unless event.get_col == 0 # do normal processing except on
1st
col
end

Also, since evt_grid_cell_left_click doesn't take an id parameter, it
seems
you'll probably have to put that entire chunk of code inside of a class
declaration for the Grid itself (either extending Grid or the variable
referencing it).  Otherwise wxRuby won't know what control you're trying
to
catch an event on.  If there's some other way around that, someone
please
correct me.

Hope this helps,
--Steve

2009/4/1 hendra kusuma <penguinroad@gmail.com>
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-04-03 04:21
(Received via mailing list)
2009/4/2 Stephen Belcher <sycobuny@malkier.net>

>
> catch an event on.  If there's some other way around that, someone please
> correct me.
>
> Hope this helps,
> --Steve


Thank you
This almost help me
Yes, I can catch the click on 1st column now, but the another problem
arise
the pointer does not move to the clicked cell
This is what in my mind, when the 1st column is clicked

def clicked(event)
  if event.get_col == 0
    pk = @grid.get_cell_value(@grid.get_grid_cursor_row,1)
    val = @grid.get_cell_value(@grid.get_grid_cursor_row,0)
    if val == '1'
      sql = "update table set val =0 where pk = #{pk}"
      @grid.set_cell_value(@grid.get_grid_cursor_row,0, '0')
    else
      sql = "update table set val =1 where pk = #{pk}"
      @grid.set_cell_value(@grid.get_grid_cursor_row,0, '1')
    end
      @conn.execute(sql)
  else
    event.skip
  end
end

but @grid.get_grid_cursor_row
will return previous row, not clicked row

What to do now?
also,
I cannot find the syntax  event.get_col in documentation
can you point me to the doc page? It would be very helpful

Many Thanks
Hendra
2aa32559134aa3d596fb749f8ffccde7?d=identicon&s=25 hendra kusuma (Guest)
on 2009-04-03 05:31
(Received via mailing list)
On Fri, Apr 3, 2009 at 9:05 AM, hendra kusuma <penguinroad@gmail.com>
wrote:

>> processing to continue on any other column.  I think the code you want
>> Also, since evt_grid_cell_left_click doesn't take an id parameter, it
> Thank you
>       sql = "update table set val =0 where pk = #{pk}"
>
>
I did it
I found the doc page (Wx::GridEvent),
figure out how to use event object
and finally did it
Now my grid work just the way I want it

the final source code looks like this
    def clicked(event)
        if event.get_col == 0
            row = event.get_row
            @grid.set_grid_cursor(row, 0)
            pk = @grid.get_cell_value(row,1)
            val = @grid.get_cell_value(row,0)
            if val == '0'
                @dataset.filter(:no => pk).update(:print => 1)
                @grid.set_cell_value(row,0,'1')
            else
                @dataset.filter(:no => pk).update(:print => 0)
                @grid.set_cell_value(row,0,'0')
            end
        else
            event.skip
        end
    end

Many thanks for all your help

Regards
Hendra
This topic is locked and can not be replied to.