Menu destroyed on dialog close

Post bugreports for the Mac OSX version here
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Menu destroyed on dialog close

Post by wombats »

If a menu is opened on a dialog, it's then destroyed when the dialog is closed. It works okay on Windows and Qt, but UnbindMenuEvent causes a crash on Gtk. I haven't tested if it's the same with just a normal window rather than a dialog.

Code: Select all

EnableExplicit

Global event, quit

Declare OpenDialog(parent)

Runtime Enumeration
  #Window
  #Dialog
  #Menu
  #XML
  #Button
EndEnumeration

Procedure OnButtonClick()
  OpenDialog(#Window)
EndProcedure

Runtime Procedure OnTreeRightClick()
  DisplayPopupMenu(#Menu, WindowID(DialogWindow(#Dialog)))
EndProcedure

Procedure OnMenuEvent()
  MessageRequester("", "Menu item clicked")
EndProcedure

Procedure OnWindowClosed()
  quit = #True
EndProcedure

Procedure OnDialogClosed()
  UnbindMenuEvent(#Menu, 0, @OnMenuEvent())
  FreeDialog(#Dialog)
EndProcedure

OpenWindow(#Window, 100, 100, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)

BindEvent(#PB_Event_CloseWindow, @OnWindowClosed(), #Window)

ButtonGadget(#Button, 10, 10, 100, 25, "Open dialog")

BindGadgetEvent(#Button, @OnButtonClick())

If CreatePopupMenu(#Menu)
  MenuItem(0, "Item")
EndIf

Procedure OpenDialog(parent)
 
  Protected xml$
 
  xml$ + "<window id='#PB_Any' name='test' text='test' minwidth='500' minheight='400' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>"
  xml$ + "  <tree onrightclick='OnTreeRightClick()'/>"
  xml$ + "</window>"
 
  If ParseXML(#XML, XML$) And XMLStatus(#XML) = #PB_XML_Success
   
    If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #XML, "test", 0, 0, 0, 0, WindowID(parent))
     
      BindEvent(#PB_Event_CloseWindow, @OnDialogClosed(), DialogWindow(#Dialog))
     
      BindMenuEvent(#Menu, 0, @OnMenuEvent())
     
    Else  
      Debug "Dialog error: " + DialogError(#Dialog)
    EndIf
  Else
    Debug "XML error: " + XMLError(#XML) + " (Line: " + XMLErrorLine(#XML) + ")"
  EndIf
 
EndProcedure

Repeat
  event = WaitWindowEvent()
Until quit
User avatar
Piero
Addict
Addict
Posts: 862
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Menu destroyed on dialog close

Post by Piero »

Not a bug…
Also, I don't think you need UnbindMenuEvent or FreeMenu if you use FreeDialog…

Code: Select all

EnableExplicit

Global event, quit
Declare OpenDialog(parent)

Runtime Enumeration
   #Window
   #Dialog
   #Menu
   #XML
   #Button
EndEnumeration

Procedure OnButtonClick()
   OpenDialog(#Window)
EndProcedure

Runtime Procedure OnTreeRightClick()
   DisplayPopupMenu(#Menu, WindowID(DialogWindow(#Dialog)))
EndProcedure

Procedure OnMenuEvent()
   MessageRequester("", "Menu item clicked")
EndProcedure

Procedure OnWindowClosed()
   quit = #True
EndProcedure

Procedure OnDialogClosed()
;  UnbindMenuEvent(#Menu, 0, @OnMenuEvent())
;  FreeMenu(#Menu)
   FreeDialog(#Dialog)
EndProcedure

OpenWindow(#Window, 100, 100, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
BindEvent(#PB_Event_CloseWindow, @OnWindowClosed(), #Window)

ButtonGadget(#Button, 10, 10, 100, 25, "Open dialog")
BindGadgetEvent(#Button, @OnButtonClick())

Procedure OpenDialog(parent)
   Protected xml$
   xml$ + "<window id='#PB_Any' name='test' text='test' minwidth='500' minheight='400' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>"
   xml$ + "  <tree onrightclick='OnTreeRightClick()'/>"
   xml$ + "</window>"
   If ParseXML(#XML, XML$) And XMLStatus(#XML) = #PB_XML_Success
      If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #XML, "test", 0, 0, 0, 0, WindowID(parent))
         If CreatePopupMenu(#Menu)
            MenuItem(0, "Item")
            BindMenuEvent(#Menu, 0, @OnMenuEvent())
         EndIf
         BindEvent(#PB_Event_CloseWindow, @OnDialogClosed(), DialogWindow(#Dialog))
      Else  
         Debug "Dialog error: " + DialogError(#Dialog)
      EndIf
   Else
      Debug "XML error: " + XMLError(#XML) + " (Line: " + XMLErrorLine(#XML) + ")"
   EndIf
EndProcedure

Repeat
   event = WaitWindowEvent()
Until quit or EventMenu() = #PB_Menu_Quit ; for ⌘Q on Mac
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: Menu destroyed on dialog close

Post by wombats »

You're right, but the menu is created before the dialog is ever created. The issue only happens after DisplayPopupMenu is used. What if the menu is used in two places? The dialog destroys it. I do know that we can create the menu each time they are needed, of course.
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Menu destroyed on dialog close

Post by Fred »

A popupmenu shouldn't be destroyed indeed.
User avatar
Piero
Addict
Addict
Posts: 862
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Menu destroyed on dialog close

Post by Piero »

I'm confused…
So this IS a bug… you need to build a popup for the dialog every single time………
You cannot recycle (possibly adding or graying out stuff) an existing popup…
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Menu destroyed on dialog close

Post by mk-soft »

I can confirm the bug.
Under macOS, the pop-up menu is destroyed in FreeDialog.

I have adapted Piero's code for this.

Code: Select all

EnableExplicit

Global event, quit
Declare OpenDialog(parent)

Runtime Enumeration
   #Window
   #Dialog
   #Menu
   #XML
   #Button
EndEnumeration

Procedure OnButtonClick()
   OpenDialog(#Window)
EndProcedure

Runtime Procedure OnTreeRightClick()
  Debug "OnTree ... IsMenu = " + IsMenu(#Menu)
   DisplayPopupMenu(#Menu, WindowID(DialogWindow(#Dialog)))
EndProcedure

Procedure OnMenuEvent()
   MessageRequester("", "Menu item clicked")
EndProcedure

Procedure OnWindowClosed()
   quit = #True
EndProcedure

Procedure OnDialogClosed()
;  UnbindMenuEvent(#Menu, 0, @OnMenuEvent())
;  FreeMenu(#Menu)
   FreeDialog(#Dialog)
EndProcedure

Procedure InitPopupMenus()
  If CreatePopupMenu(#Menu)
    MenuItem(0, "Item")
    BindMenuEvent(#Menu, 0, @OnMenuEvent())
 EndIf
EndProcedure

Procedure OpenDialog(parent)
   Protected xml$
   xml$ + "<window id='#PB_Any' name='test' text='test' minwidth='500' minheight='400' flags='#PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget'>"
   xml$ + "  <tree onrightclick='OnTreeRightClick()'/>"
   xml$ + "</window>"
   If ParseXML(#XML, XML$) And XMLStatus(#XML) = #PB_XML_Success
      If CreateDialog(#Dialog) And OpenXMLDialog(#Dialog, #XML, "test", 0, 0, 0, 0, WindowID(parent))
         BindEvent(#PB_Event_CloseWindow, @OnDialogClosed(), DialogWindow(#Dialog))
      Else  
         Debug "Dialog error: " + DialogError(#Dialog)
      EndIf
   Else
      Debug "XML error: " + XMLError(#XML) + " (Line: " + XMLErrorLine(#XML) + ")"
   EndIf
EndProcedure

OpenWindow(#Window, 100, 100, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget)
BindEvent(#PB_Event_CloseWindow, @OnWindowClosed(), #Window)

ButtonGadget(#Button, 10, 10, 100, 25, "Open dialog")
BindGadgetEvent(#Button, @OnButtonClick())

InitPopupMenus()

Repeat
   event = WaitWindowEvent()
Until quit Or EventMenu() = #PB_Menu_Quit ; for ⌘Q on Mac
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply