Lazarus

Programming => General => Topic started by: Talker on November 26, 2013, 02:31:08 pm

Title: Pop-up menu doesn't work on a form stored in DLL
Post by: Talker on November 26, 2013, 02:31:08 pm
Hi,

I've got a form in a DLL and there is a pop-up menu on that form. The pop-up menu doesn't work i.e. it doesn't appear when I right-click on the form...
Pop-up menus on a form that stored not in DLL works fine.

Is there any way to solve this?
Thanks!
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Talker on February 11, 2014, 08:03:38 am
Hello.
I still have not solved this problem... Can anybody give me a hint what to do?
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Mike.Cornflake on February 11, 2014, 10:01:39 am
Never tried it myself.  Any chance you can post some code so others can more easily help?
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: marcov on February 11, 2014, 11:58:56 am
With a DLL, you have two copies of everything. So all LCL and RTL state is duplicated.

IIRC popups (at least in delphi) had some global VCL/LCL lookup list, and probably the DLL ones are registered in DLL instance, not the main one.

This is why Delphi has packages for cross-package GUI. See e.g. http://wiki.freepascal.org/packages for a longer treatise.
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Talker on February 11, 2014, 01:19:32 pm
Never tried it myself.  Any chance you can post some code so others can more easily help?
Here is a simple project for example
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Mike.Cornflake on February 11, 2014, 02:00:53 pm
Excellent sample code, thanks for posting.  Might refer back to this if I need to write DLL code.

I can confirm that your issue exists, or at least on Windows 8 64 bit, using the win32 widgetset.  I presume you're also using the win32 widgetset?  And my confirming your issue doesn't help you much :-( 

I added a button to each form, then added manual popup code.  Confirmed that the popup menu doesn't appear even if triggered manually.

Went looking on the bug tracker, found the following:
http://mantis.freepascal.org/view.php?id=18624

Seems it's a known issue, and has been fixed for several other widgetsets, but not for Win32.  On a whim, I tried applying the patch in the bug issue, and sure enough, it appears to have resolved the issue.
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Michl on February 11, 2014, 02:02:55 pm
I've tested your project and it works fine. I only have to remove a not existing package "rxnew". Than I build FormInDLL.dll and start Project LoadMyDll. Both Button are working and show a new Form.

I've tested under Win7 64bit and Lazarus 1.0.14 32bit
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Mike.Cornflake on February 11, 2014, 02:05:49 pm
I've tested your project and it works fine. I only have to remove a not existing package "rxnew". Than I build FormInDLL.dll and start Project LoadMyDll. Both Button are working and show a new Form.

I've tested under Win7 64bit and Lazarus 1.0.14 32bit

The issue is not getting the form to show, but getting the popup menu to appear when you right click on the form.  Works on the main project form, but not on the DLL form.
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Michl on February 11, 2014, 02:12:07 pm
Arrgh, I misread the question - sorry!
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: engkin on February 11, 2014, 02:27:25 pm
The problem is with WidgetSet.AppHandle (or precisely  Win32WidgetSet.AppHandle):

Add InterfaceBase to your Uses section
Add a button to your DLL form. OnClick event for the button should have something like:
Code: [Select]
  WidgetSet.AppHandle := Self.Handle;
Try it!
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Mike.Cornflake on February 11, 2014, 02:31:14 pm
Quote
The problem is with WidgetSet.AppHandle (or precisely  Win32WidgetSet.AppHandle):

Add InterfaceBase to your Uses section
Add a button to your DLL form. OnClick event for the button should have something like:

Code: [Select]
  WidgetSet.AppHandle := Self.Handle;Try it!
That's pretty much the patch in a nutshell.  Didn't realise we could change AppHandle at the project level.  Nice :-)
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: engkin on February 11, 2014, 02:44:45 pm
I guess it takes a little bit more than that because a DLL might have a few forms so you need to replicate the code that uses WidgetSet.AppHandle with your own procedure  that takes the handle of the form that owns the TPopupMenu. I'm talking about:
Code: [Select]
class procedure TWin32WSPopupMenu.Popup(const APopupMenu: TPopupMenu; const X, Y: integer);
It calls TrackPopupMenuEx to show the pop-up menu. A custom procedure should do the same! and it is needed because AppHandle takes only one value:
Code: [Select]
procedure TWin32WidgetSet.SetAppHandle(const AValue: THandle);
begin
  // Do it only if handle is not yet created (for example for DLL initialization)
  // if handle is already created we can't reassign it
  if AppHandle = 0 then
    FAppHandle := AValue;
end;
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Michl on February 11, 2014, 02:46:28 pm
I still have build Lazarus 1.3 R43996 FPC 2.7.1 and manually added the patch from bugtracker. And now the Popup works fine. Just as a info.
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: engkin on February 11, 2014, 02:51:34 pm
I still have build Lazarus 1.3 R43996 FPC 2.7.1 and manually added the patch from bugtracker. And now the Popup works fine. Just as a info.
Care to share the bug tracking number?
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Cyrax on February 11, 2014, 02:57:31 pm
I didn't find the bug number in question but you can see change in here: http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=revision&root=lazarus&revision=43986
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Michl on February 11, 2014, 02:58:23 pm
...
http://mantis.freepascal.org/view.php?id=18624
...
or direct: http://mantis.freepascal.org/file_download.php?file_id=13162&type=bug (http://mantis.freepascal.org/file_download.php?file_id=13162&type=bug)
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: engkin on February 11, 2014, 06:39:55 pm
Thank you Mike.Cornflake, Cyrax and Michl.  :)

@Mike.Cornflake thank you again for finding that bug report.
Title: Re: Pop-up menu doesn't work on a form stored in DLL
Post by: Talker on February 12, 2014, 05:47:23 am
It works! Thank you so much!
TinyPortal © 2005-2018