Problems with popup menu in wxNotebook and wxGrid

Hi,

Currently I work on a spreasheet like application. I have implemented a context menu in wxGrid (cut,copy, paste) and wxNotebook(insert, delete, remove new sheet). Strange thing is that all my commands work, but only on second time. e.g when I right click on the wxGrid/wxNotebook, menu appears, I do some command - nothing happens.
When I do it second time, it works. And it works afterwards all right ... What might be wrong?

Code example:

class MyNotebook(wxNotebook):
  def __init__(self, parent):
    wxNotebook.__init__(self, parent, -1, pos = wxDefaultPosition, size = wxDefaultSize, style=wxBOTTOM)

    self.Bind(EVT_RIGHT_DOWN, self.OnContextMenu)

  def OnContextMenu(self, event):
    g = self.GetSelection()
    c, d = event.GetPosition() # workaround
    a, b = self.GetSizeTuple()
    e, f = self.HitTest((c, b-d))

    if e < 0:
      return

    if g != e and e >= 0:
      self.SetSelection(e)
  
    contextmenu = wxMenu()
    contextmenu.Append(11, "Insert")
    contextmenu.Append(12, "Remove")
    self.PopupMenu(contextmenu, (c, b-d))
          contextmenu.Destroy()

    self.Bind(EVT_MENU, self.OnInsertSheet, id = 11)
    EVT_MENU(self, 12, self.OnDeleteSheet)

  def OnInsertSheet(self, event):
                wxBell() # beeps only on second time

  def OnDeleteSheet(self, event):
      wxBell()

Thanks,

jan bodnar

PS: there might be a bug in wxNotebook widget. when you create a wxNotebook with style wxBOTTOM and implement a popupmenu the classical way -self.PopupMenu(menu, event.GetPosition())- the popup menu appears wrongly very high. therefore I did a workaround.

···

________
Vsetci sa raz zideme na http://blog.sme.sk

jan bodnar wrote:

Hi,

Currently I work on a spreasheet like application. I have implemented a context menu in wxGrid (cut,copy, paste) and wxNotebook(insert, delete, remove new sheet). Strange thing is that all my commands work, but only on second time. e.g when I right click on the wxGrid/wxNotebook, menu appears, I do some command - nothing happens.
When I do it second time, it works. And it works afterwards all right ... What might be wrong?

Code example:

    self.PopupMenu(contextmenu, (c, b-d))
          contextmenu.Destroy()

    self.Bind(EVT_MENU, self.OnInsertSheet, id = 11)
    EVT_MENU(self, 12, self.OnDeleteSheet)

You don't bind the menu event to a handler until after you popup the menu. self.PopupMenu is modal so it doesn't return until after a menu item has been selected or the menu has been dismissed. So you need to bind the menu event handlers before you popup the menu, and you probably only want to bind them once otherwise you'll be adding a new binding to the event table each time you show the menu.

PS: there might be a bug in wxNotebook widget. when you create a wxNotebook with style wxBOTTOM and implement a popupmenu the classical way -self.PopupMenu(menu, event.GetPosition())- the popup menu appears wrongly very high. therefore I did a workaround.

Which platform and version?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!