Mouse click event at grid


#1

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


#2

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 removed_email_address@domain.invalid


#3

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


#4

On Tue, Mar 31, 2009 at 1:25 PM, Alex F. removed_email_address@domain.invalid 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?


#5

On Wed, Apr 1, 2009 at 5:36 PM, Alex F. removed_email_address@domain.invalid 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


#6

hendra kusuma wrote:

On Tue, Mar 31, 2009 at 1:25 PM, Alex F. <removed_email_address@domain.invalid
mailto:removed_email_address@domain.invalid> 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


#7

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 removed_email_address@domain.invalid


#8

On Thu, Apr 2, 2009 at 10:15 AM, hendra kusuma
removed_email_address@domain.invalidwrote:

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


#9

2009/4/2 Stephen B. removed_email_address@domain.invalid

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


#10

On Fri, Apr 3, 2009 at 9:05 AM, hendra kusuma removed_email_address@domain.invalid
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