# Mouse wheel events in a grid cell editor?

**URL:** <https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081>\
**Category:** wxRuby\
**Created:** [February 24, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081 "2010-02-24T20:39:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tony44](https://www.ruby-forum.com/u/tony44)\
**Post date:** [February 24, 2010, 8:39pm UTC](https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081/1 "2010-02-24T20:39:27Z")

</div>

I’m trying to do some advanced editing using Grids. I’ve created my own  
editors and updating view and model both works fine so far. Thanks to  
whoever made this possible. I like wxRuby more each day!

Now I’d like to catch mousewheel events in order to increment/decrement  
a grid cell editor.

I’ve tried installing a handler in begin\_edit of my editor and removing  
it again in end\_edit.

I’ve attempted to

(a) install an event handler on the grid and forward it to an  
editor-specific method

(b) install an event handler on the editor control

Neither approach worked. Any suggestions?

Thanks in advance,  
Tony

* * *

class MyEditor \< Wx::GridCellChoiceEditor  
def initialize  
super  
end

def begin\_edit row, col, grid  
get\_control().evt\_mousewheel {|e| on\_wheel e }  
grid.evt\_mousewheel {|e| on\_wheel e }  
end

def on\_wheel event  
p event  
end

def end\_edit row, col, grid  
# TODO: unsubscribe evt handler  
super  
end  
end

---

<div class="post-metadata">

**Author:** ![tony44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tony44](https://www.ruby-forum.com/u/tony44)\
**Post date:** [March 2, 2010, 12:15am UTC](https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081/2 "2010-03-02T00:15:38Z")

</div>

On 24/02/2010 19:39, Tony M. wrote:

> I’m trying to do some advanced editing using Grids. I’ve created my own  
> editors and updating view and model both works fine so far. Thanks to  
> whoever made this possible. I like wxRuby more each day!

Thanks - Wx::Grid is a really nice class - complex, but powerful.

> (b) install an event handler on the editor control

I would have expected (a) to work - mouse events are fired on whichever  
is the topmost control where the mouse is. For an editor control, the  
mouse pointer may not be on top of the control itself.

Does the evt\_mousewheel fire correctly if you have it as a constant  
event handler for the Grid?

You could try using the method Window#capture\_mouse to force mouse input  
to be directed to a particular window - either the grid or the editor  
control - to see if this helps.

Failing that, please post a complete, runnable but minimal example with  
the problem and I’ll see if I can help

alex

---

<div class="post-metadata">

**Author:** ![tony44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tony44](https://www.ruby-forum.com/u/tony44)\
**Post date:** [March 5, 2010, 7:53am UTC](https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081/3 "2010-03-05T07:53:44Z")

</div>

thanks for the reply. The problem appears to be that, once the editor is  
instantiated, the grid will receive the wheel events everywhere _except_  
where the editor is visible.

> I would have expected (a) to work - mouse events are fired on whichever  
> is the topmost control where the mouse is. For an editor control, the  
> mouse pointer may not be on top of the control itself.

it would appear that is the case - which may be why the grid receives  
the wheel events but not the editor.

> Does the evt\_mousewheel fire correctly if you have it as a constant  
> event handler for the Grid?

yes

> You could try using the method Window#capture\_mouse to force mouse input  
> to be directed to a particular window - either the grid or the editor  
> control - to see if this helps.

I will have to try that - digging into it now.

> Failing that, please post a complete, runnable but minimal example with  
> the problem and I’ll see if I can help

attached. I will let you know as soon as I found out anything regarding  
the capture\_mouse method.

tony

* * *

(I’m getting an “application error” from the webserver when I try to  
attach a file so I’m adding it here and reporting the bug to the  
webmaster)

#!/usr/bin/env arch -i386 ruby  
begin  
require ‘rubygems’  
rescue LoadError  
end  
require ‘wx’

class MyGridTable \< Wx::GridTableBase  
attr\_reader :cols, :rows  
def initialize(rows, cols)  
super()  
@rows = rows  
@cols = cols  
end

def get\_number\_rows  
@rows  
end

def get\_number\_cols  
@cols  
end

def get\_value(row, col)  
(col + @cols \* row).to\_s  
end

def get\_attr(row, col, attr\_kind)  
Wx::GridCellAttr.new  
end

def is\_empty\_cell(row, col)  
false  
end

def set\_value(x, y, val)  
puts “Attempting to change cell (#{x}, #{y}) to ‘#{val}’”  
end

def get\_type\_name(row, col)  
“FOOBAR”  
end  
end

class MyEditor \< Wx::GridCellChoiceEditor  
def initialize  
super([‘7’,‘13’,‘23’,‘42’])  
end

def begin\_edit row, col, grid  
puts “begin edit”  
grid.evt\_mousewheel {|e| on\_wheel e }  
super  
end

def on\_wheel event  
p event  
end

def end\_edit(row, col, grid)  
puts “end edit”  
# TODO: how to unsubscribe?  
super  
end  
end

class GridFrame \< Wx::Frame  
def initialize()  
super(nil, :title =\> ‘Mousewheel example’, :size =\> [600, 300])  
main\_sizer = Wx::VBoxSizer.new

```
@grid = Wx::Grid.new(self)

@grid.register_data_type( "FOOBAR",
                          Wx::GridCellStringRenderer.new,
                          MyEditor.new )

@grid.table = MyGridTable.new(10, 10)
main_sizer.add(@grid, 1, Wx::EXPAND|Wx::ALL, 5)
self.sizer = main_sizer

```

end  
end

Wx::App.run do  
GridFrame.new.show  
end

---

<div class="post-metadata">

**Author:** ![tony44](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dc4da7/32.png) [@tony44](https://www.ruby-forum.com/u/tony44)\
**Post date:** [March 7, 2010, 5:20pm UTC](https://www.ruby-forum.com/t/mouse-wheel-events-in-a-grid-cell-editor/184081/4 "2010-03-07T17:20:03Z")

</div>

> You could try using the method Window#capture\_mouse to force mouse input  
> to be directed to a particular window - either the grid or the editor  
> control - to see if this helps.

I’m a bit puzzled my how the capture\_mouse method might work on the grid  
cells.

Since the cell editors don’t subclass Wx::Window, the only chance would  
be to capture the mouse on the control itself and subsequently connect  
the control to the mouse event:

class MyEditor \< Wx::GridCellNumberEditor

def initialize  
super 0, 100  
end

def begin\_edit row, col, grid  
control = get\_control()  
control.capture\_mouse()  
connect(control.wx\_id, Wx::ID\_ANY, Wx::EVT\_MOUSEWHEEL) { |e| p e }  
super  
end

def end\_edit row, col, grid  
control = get\_control  
control.release\_mouse  
super  
end

end

… however this doesn’t do anything for me (no output). Please help.

Thanks,  
Tony
