# GridCellNumberEditor peculiarities

**URL:** <https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919>\
**Category:** wxRuby\
**Created:** [February 22, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919 "2010-02-22T18:28:37Z")\
**Posts on this page:** 6\
**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 22, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/1 "2010-02-22T18:28:37Z")

</div>

Hi,

I’m using wxruby 2.0.1 w/ Ruby 1.8.7 on OS X 10.6.2.

So far I have successfully implemented a grid with bool, choice and text  
edit controls. To combine the edit code with my app, I have subclassed  
the editor controls to get hooks for the begin\_edit/end\_edit events.

This worked fine for the GridCellTextEditor and the  
GridCellChoiceEditor.

My problem is, that I can’t seem to get events for the  
GridCellNumberEditor. Find attached the modified grid example with  
subclassed controls.

I’m getting the begin\_edit/end\_edit outputs for the choice control but  
not for the number control.

To reproduce, start the application, click the choice editor and select  
a value. Note the console output that occurs on begin and end edit. Now  
click the number editor and change the number using the spin control.  
Notice how there is NO console output although the editor has been  
instantiated earlier.

What’s wrong? Is there another, preferable way of integrating a  
control’s events into an application to generate callbacks and allow for  
value vetoes?

Thanks in advance,  
Tony

---

<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 22, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/2 "2010-02-22T18:30:23Z")

</div>

here’s the code (I can’t seem to attach it)

```auto
begin
  require 'rubygems'
rescue LoadError
end
require 'wx'

class MyNumberEditor < Wx::GridCellNumberEditor
  def initialize(min, max)
    super
    puts "creating number editor"
  end

  def begin_edit row, col, grid
    puts "begin edit"
    super
  end

  def end_edit row, col, grid
    puts "end edit"
    super
  end

end

class MyChoiceEditor < Wx::GridCellChoiceEditor
  def initialize choices
    puts "choice init"
    super
  end

  def begin_edit(row, col, grid)
    puts "choice begin edit"
    super
  end

  def end_edit(row, col, grid)
    puts "choice end edit w/ value #{get_value()}"
    super
  end
end

class GridFrame < Wx::Frame
  def initialize(parent, id = -1, title = "MyFrame",
                  pos = Wx::DEFAULT_POSITION,
                  size = Wx::DEFAULT_SIZE,
                  style = Wx::DEFAULT_FRAME_STYLE)

    super(parent, id, title, pos, size, style)
    sizer = Wx::BoxSizer.new(Wx::VERTICAL)

    set_status_text(Wx::VERSION_STRING)
    make_grid(self)
    sizer.add(@grid, 1, Wx::ALL|Wx::GROW, 4)
    set_sizer(sizer)
  end

  def make_grid(panel)
    @grid = Wx::Grid.new(panel, -1)

    @grid.create_grid( 20, 12 )
    @grid.set_default_cell_background_colour(Wx::WHITE)

    editor = MyNumberEditor.new(7,23)
    @grid.set_cell_value(3, 1, 'Number editor below')
    @grid.set_cell_editor(4, 1, editor)

    editor = MyChoiceEditor.new(['foo', 'bar', 'baz'])
    @grid.set_cell_value(3, 3, 'Choice editor below')
    @grid.set_cell_editor(4, 3, editor)

    @grid.auto_size_row(3, true)
  end

end

class GridApp < Wx::App
  def on_init
    frame = GridFrame.new(nil, -1, "Grid Sample",
                         Wx::Point.new(10, 100),
                         Wx::Size.new(630,400))

    set_top_window(frame)
    frame.show()
  end
end

GridApp.new.main_loop()
```

---

<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 8, 2010, 2:30pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/3 "2010-03-08T14:30:02Z")

</div>

me again - I’m still struggling with the Grid code. Maybe this is a  
problem with my runtime environment?

I’m using wxruby 2.0.1 on Snow Leopard 10.6.2 using Ruby 1.8.7 and  
wxWidgets 2.8.10. Is this supposed to be a valid combination?

It seems that, with this combination the GridCellNumberEditor cannot be  
properly derived from.

When registered as an editor with a Grid, the constructor is called but,  
upon editing, neither the begin\_edit nor the end\_edit methods from the  
derived class are called. All of this, in spite a NumberCellEditor is  
properly displayed and the code also calls to TableBase’s set\_value()  
method when the editor closes.

What’s going on? Is this a bug or am I completely misled on the usage of  
this?

Thanks for any pointers in the right direction.

Tony.

* * *

#!/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::GridCellNumberEditor  
def initialize  
puts ‘my editor init’ # called upon app start  
super(0,100)  
end

def begin\_edit row, col, grid  
super  
puts “begin edit” # never gets called  
end

def end\_edit(row, col, grid)  
puts “end edit” # never gets called  
super  
end  
end

class GridFrame \< Wx::Frame  
def initialize()  
super(nil, :title =\> ‘Grid editor 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 8, 2010, 3:05pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/4 "2010-03-08T15:05:07Z")

</div>

> I’m using wxruby 2.0.1 on Snow Leopard 10.6.2 using Ruby 1.8.7 and  
> wxWidgets 2.8.10. Is this supposed to be a valid combination?
> 
> It seems that, with this combination the GridCellNumberEditor cannot be  
> properly derived from.

Update: I cross-checked this with Windows XP, wxWidgets 2.8.10 and Ruby  
1.8.6.

The behavior is identical (begin\_edit and end\_edit are NOT called)  
except that, with XP, using the mousewheel and accepting an input with  
“Enter” works out of the box - which it doesn’t on OS X. And that was  
exactly the reason why I tried to override begin\_edit and end\_edit.

Ouch.

---

<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:** [September 4, 2010, 12:58pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/5 "2010-09-04T12:58:12Z")

</div>

Hi Alex,

any news on this issue?

I’m still struggling to completely wrap my head around the CellEditors.  
Right now I’ve managed to get everything working half-way but one issue  
remains:

Leaving the Cell-Editor with Enter.

On Windows, this works with normal editors but not with numeric editors.  
On OSX this doesn’t work at all. Looking up the wxwidgets documentation,  
I found that there should be a virtual editor method called  
“handleReturn” (see [1]).

Attempts to implement a ruby method handle\_return doesn’t yield any  
result.

Can you please advise what would be the best (or any) way to process an  
enter key and to finalize cell editing?

Thanks,  
Tony

[1]

> **[wxGridCellEditor Class Reference | wxWidgets](https://docs.wxwidgets.org/trunk/classwx_grid_cell_editor.html#265a3c935b3d5c204c8c75149fa0cb74)**

Alex F. wrote:

> Tony M. wrote:
> 
> > me again - I’m still struggling with the Grid code. Maybe this is a  
> > problem with my runtime environment?
> > 
> > I’m using wxruby 2.0.1 on Snow Leopard 10.6.2 using Ruby 1.8.7 and  
> > wxWidgets 2.8.10. Is this supposed to be a valid combination?
> 
> 2.0.1 came out just before Snow Leopard was released, and I think we  
> need to update and rebuild for 10.6 - just haven’t got around to it  
> though I’ve been using 10.6 for a few months.
> 
> However, this sort of problem would almost certainly show up as big-fail  
> crash on startup, rather than minor behavioural differences.
> 
> > It seems that, with this combination the GridCellNumberEditor cannot be  
> > properly derived from.
> 
> I have a feeling that the concrete XXXEditor classes are not fully  
> inheritable from - they are designed as concrete final classes in C++.  
> But I’m not sure about this - will have a go with your sample later  
> today (also looking at your wheel question) and let you know
> 
> 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 8, 2010, 3:16pm UTC](https://www.ruby-forum.com/t/gridcellnumbereditor-peculiarities/183919/6 "2010-03-08T15:16:06Z")

</div>

Tony M. wrote:

> me again - I’m still struggling with the Grid code. Maybe this is a  
> problem with my runtime environment?
> 
> I’m using wxruby 2.0.1 on Snow Leopard 10.6.2 using Ruby 1.8.7 and  
> wxWidgets 2.8.10. Is this supposed to be a valid combination?

2.0.1 came out just before Snow Leopard was released, and I think we  
need to update and rebuild for 10.6 - just haven’t got around to it  
though I’ve been using 10.6 for a few months.

However, this sort of problem would almost certainly show up as big-fail  
crash on startup, rather than minor behavioural differences.

> It seems that, with this combination the GridCellNumberEditor cannot be  
> properly derived from.

I have a feeling that the concrete XXXEditor classes are not fully  
inheritable from - they are designed as concrete final classes in C++.  
But I’m not sure about this - will have a go with your sample later  
today (also looking at your wheel question) and let you know

alex
