View Issue Details

IDProjectCategoryView StatusLast Update
0025415LazarusLCLpublic2021-05-13 15:03
ReporterAnton Kavalenka Assigned ToMichl  
Status assignedResolutionopen 
Product Version1.3 (SVN) 
Summary0025415: LCL: TPopupMenu inserted in TmainMenu disappear after being shown by another control
DescriptionTPopupMenu, inserted in TMainMenu (as collection of items) works until the popup menu shown by calling PopupMethod or show as context menu of control.
Steps To ReproduceClick "View" - Point to "Layout" -> submenu "Classes" will be shown.
Right-click the form - submenu "Classes" will be shown again.
Clich "View" - "Layout" submenu is hidden.
Additional Informationbehaviour is identical under i386-win32 and x86_64-gtk2
TagsNo tags attached.
Fixed in Revision
WidgetsetGTK 2, Win32/Win64
Attached Files


related to 0015614 closedPaul Ishenin TPopupMenu.Close() need rework 
related to 0030867 closedJuha Manninen Strange behaviour of a popupmenu for ComboBox, Memo and Panel 


Anton Kavalenka

2013-12-10 13:57

reporter (129,126 bytes)

Anton Kavalenka

2013-12-10 13:57


p1.PNG (7,783 bytes)   
p1.PNG (7,783 bytes)   

Anton Kavalenka

2013-12-10 13:57


p2.PNG (5,852 bytes)   
p2.PNG (5,852 bytes)   

Anton Kavalenka

2013-12-10 13:57


p3.PNG (5,973 bytes)   
p3.PNG (5,973 bytes)   

Juha Manninen

2013-12-10 19:40

developer   ~0071858

Example project is missing.

Anton Kavalenka

2013-12-10 21:37

reporter   ~0071863 - 1st file


2013-12-12 03:47

reporter   ~0071871

There are two issues here:
Firstly, a TMenuItem has a Parent TMenuItem - that is it can only have ONE parent. You are populating a list of a TPopupMenu (so when you call pmViewClasses.Items.Add(mi) the parent of your items becomes the popup menu). You are then miView.Add(pmViewClasses.Items) to add your popup menu to the view menu. You cannot do this as the menu items can't have different parents.

If you look at the LCL code where it eventually goes to (TMenuItem.Insert) you will notice it tries to catch already assigned menu items being re-assigned
  if Item.Parent <> nil then RaiseGDBException('Menu inserted twice');

The problem is you are adding pmViewClasses.Items to your View menu, which is actually the top level menu item of the popup and that has no parent. This is why your code doesn't generate an exception when you try and copy your menu items (it should) and where the Lazarus bug is.

The proper way to do this is to create a separate TMenuItem for each one in your popup and add these other ones to a sub-menu of your View menu (you could even use .Assign).

Looking at the Lazarus source I'm not sure how you would fix this so that it raises an exception because TPopupMenu.Items is a TMenuItem with Parent=nil.

Anton Kavalenka

2013-12-12 07:35

reporter   ~0071872

Thank you for analysis.

But it works both in Delphi 7 and XE2.

The idea in code:
You show menu entirely (with popup inserted) or part of menu (popup only).
Nothing inserted twice. Just show tree or branch of tree.

It works in Delphi, IMO, because Delphi allocates real HMenu handles immediately before showing menu, and does not cache them.

Anton Kavalenka

2013-12-16 13:23

reporter   ~0071923

Last edited: 2013-12-16 14:02

View 2 revisions

Adding the following code into OnClose handler of TpopupMenu
solves the problem.

procedure TForm1.pmViewClassesClose(Sender: TObject);

Btw current TmenuItem.CreateHandle and TmenuItem.DestroyHandle is very asymmetric.
CreateHandle checks if current menu item has the same parent and then allocates new handle, Destroyhandle releases all the handles unconditionally.

In any way handles are cached when menu closed, and destroyed on next .Popup() method call.


2016-11-13 21:22

developer   ~0095849

This bug is related to 0015614. I have no Qt or OS X to test the issue fixed there.

When I remove DestroyHandle and CreateHandle from the the added code (revision 23585), it works like in Delphi. Tested with 32 and 64bit Windows 7 and 32 and 64bit Linux Mint KDE GTK2.

I add the patch, to show you, what I've done (DON'T APPLY IT, UNTIL IT IS CLEAR, WHAT IT ALSO AFFECTS).
I add a more simple test.

@Zeljan Rikalo, maybe you can have a look at this issue on Qt and OS X?

Maybe the workaround from 0015614 could be done in the TWSPopupMenuClass(WidgetSetClass)?


2016-11-13 21:22

developer (540 bytes)   
Index: lcl/include/
--- lcl/include/	(revision 53363)
+++ lcl/include/	(working copy)
@@ -75,8 +75,9 @@
   if Items.Count = 0 then exit;
   ActivePopupMenu := Self;
-  DestroyHandle;
-  CreateHandle;
+// removed due to mantis #0025415
+//  DestroyHandle;
+//  CreateHandle;
   if Assigned(OnMenuPopupHandler) then OnMenuPopupHandler(Self);
   TWSPopupMenuClass(WidgetSetClass).Popup(Self, X, Y);
 end; (540 bytes)   


2016-11-13 21:23

developer (2,063 bytes)

Anton Kavalenka

2016-11-14 16:03

reporter   ~0095868

seems like the patch make it working properly under win32 and gtk2

Juha Manninen

2016-12-11 18:34

developer   ~0096706

Michl, you know the issue best. What code should be applied?


2016-12-11 20:26

developer   ~0096708

Till now, no code should be applied, because afaik a HandleNeeded should be implemented. I'll look at it.

Anton Kavalenka

2019-04-24 11:53

reporter   ~0115771

The attached example behaves even strangely under MacOS/X Cocoa. So it is LCL-wide problem,
See the screenshot
layout-mac.png (74,884 bytes)   
layout-mac.png (74,884 bytes)   

Anton Kavalenka

2019-04-26 05:25

reporter   ~0115818

Please, can somebody keen in Cocoa WS have a look?
W32 and GTK2 working fine
GTK3 and Cocoa is broken.

Anton Kavalenka

2019-04-26 05:31

reporter   ~0115819

In GTK3 hack with Items.RecreateHandle helps

Anton Kavalenka

2021-05-13 15:03

reporter   ~0130853

Issue still valid for LCLcocoa

Issue History

Date Modified Username Field Change
2013-12-10 13:57 Anton Kavalenka New Issue
2013-12-10 13:57 Anton Kavalenka File Added:
2013-12-10 13:57 Anton Kavalenka File Added: p1.PNG
2013-12-10 13:57 Anton Kavalenka File Added: p2.PNG
2013-12-10 13:57 Anton Kavalenka File Added: p3.PNG
2013-12-10 19:40 Juha Manninen Note Added: 0071858
2013-12-10 21:37 Anton Kavalenka Note Added: 0071863
2013-12-12 03:47 Reiner Note Added: 0071871
2013-12-12 07:35 Anton Kavalenka Note Added: 0071872
2013-12-16 13:23 Anton Kavalenka Note Added: 0071923
2013-12-16 14:02 Anton Kavalenka Note Edited: 0071923 View Revisions
2016-11-09 14:18 Juha Manninen Relationship added related to 0030867
2016-11-13 21:22 Michl Note Added: 0095849
2016-11-13 21:22 Michl File Added:
2016-11-13 21:23 Michl File Added:
2016-11-13 22:48 Juha Manninen Relationship added related to 0015614
2016-11-14 16:03 Anton Kavalenka Note Added: 0095868
2016-12-11 18:30 Juha Manninen Assigned To => Michl
2016-12-11 18:30 Juha Manninen Status new => assigned
2016-12-11 18:34 Juha Manninen Note Added: 0096706
2016-12-11 20:26 Michl Note Added: 0096708
2019-04-24 11:53 Anton Kavalenka File Added: layout-mac.png
2019-04-24 11:53 Anton Kavalenka Note Added: 0115771
2019-04-26 05:25 Anton Kavalenka Note Added: 0115818
2019-04-26 05:31 Anton Kavalenka Note Added: 0115819
2021-05-13 15:03 Anton Kavalenka Note Added: 0130853