Lazarus

Programming => LCL => Topic started by: linuxfan on August 17, 2019, 11:34:45 am

Title: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: linuxfan on August 17, 2019, 11:34:45 am
Hello, I am writing a fairly complicated program which exhibits two strange things.

The first is that I have a form which, in its OnShow(), puts a few TImage into a scrollbox and assigns, to each image, the OnClick event.

When asked to, the form comes up as expected, but the first click on a Timage does not fire. The second click and subsequent ones instead fire ok. If, after the form comes up, I click a button on the form it works; and after then, also the OnClick()s of the images work.

It is really the first time that I see something like this. I am using Lazarus 2.0.2 (fpc 3.0.4) on a linux 64 bit. Does anybody have seen something similar?

Also, the same program, has in its main window several popup menus. They work as expected but, after a variable time of interaction with the user, they ALL stop to popup when asked to (the rest of the program keep to work well). By saying "popup when asked to" I mean that all of them are written in the Popup property of their related control, so the right mouse button should pop up them. To debug, I've put a button on the form which explicitly ask a popup menu to show, which also works, but when the program enters the faulty state, that button too stops to show the popup menu (it still executes all its duties, just the popup is asked to show but it does not).

For this last issue, I will try to step debug into Menus.pas (I've set a breakpoint, but it is ignored, I will see how to solve). But for the other problem (first onclick() skipped) I have no clue; if somebody knows something, please say.

Anyway, Lazarus is fantastic!  :)

Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: wp on August 17, 2019, 12:02:16 pm
I followed your steps for the first-click-issue and created a small demo project. But I cannot reproduce the issue (I'm on Windows, though): the messagebox  which is supposed to be displayed in the image's OnClick handler does appear after the first click.

So, please see what is different in your case and modifiy the project accordingly. Or better, write a demo project on your own to give us something to reproduce the error. Without a compilable demo it is almost impossible to give you a hint on what is wrong.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: howardpc on August 17, 2019, 12:26:10 pm
I followed your steps for the first-click-issue and created a small demo project. But I cannot reproduce the issue (I'm on Windows, though)
I'm on 64-bit linux and cannot reproduce your issue either.
As wp says: without compilable  code that demonstrates the issue we can only guess what might be wrong with your project.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: jamie on August 17, 2019, 08:57:09 pm
Since the days of Delphi, it has never been a good idea to do any form of GUI editing while in the
onShow event. You should be doing non related GUI setups in there.

 Why not use the OnCreate event of the form to set all of those?

 In anycase, what you can do if you still thing you must, is Post a Message to the forms Handle so that it comes around a little later. Add a message handler for a LM_USER+? and when that message is received you can then call a method that sets all of that up.

 This is more or less a synchronized approach.

 PostMessage(MyFOrm.handle, LM_USER+SOmeNumber, Optional, Optional);

In the form,

 MyMessage(var T:Tmessage); Message LM_USER+SomeNUmber;

Implement MyMessage method somewhere.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: linuxfan on August 18, 2019, 05:25:47 pm
Thank you all for the quick reply. The support of Lazarus is excellent.

In my first post I didn't add the code because I suspect the bug is difficult to reproduce when the form is taken out of its context, and the whole program, to be usable, has to be set up. There are several steps to do.

I thought to ask only to see if someone else did already see something like that.

The sendmessage trick could help to solve or understand what is going on.

I will investigate more and let you know.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: Thaddy on August 18, 2019, 05:38:33 pm
Technically you can do this in OnShow, but you have to use the parent beginupdate/endupdate during create.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: linuxfan on August 19, 2019, 11:01:28 am
Here I am with the some tests result.

I tried to write an event handler for creating the images, in order not to burden the FormShow() procedure: I've put in the FormShow a PostMessage(... LM_USER+1 ...), and wrote a handler to create the images. The result is the same. Indeed, the program was working under Delphi (yes, I have converted it).

Then I played a little with PostMessage() and CallDefaultHandler, trying to fake the LCL into thinking that a mouse click were done by the user, but it didn.t work. The result is always the same: I must click with the mouse anywhere in the form, on any active area; after that, the image receives the onclick. Clicking on a disabled button, on a scrollbat, using the keyboard, minimize and restore the window... nothing works: I have to click.

I attach the whole project... basically it is a kind of file browser, able to build a database of thumbnails from movies/clips. Using this database, the program can try to find duplicate or similar clips.

To use the program, the user needs at least a video clip (mpeg, avi, mp4 or whatever), mplayer installed, and a directory to store thumbs. When the program runs, use the menu Options/Preferences to tell the program where to find things.

Then, use the upper left controls: "Go root", browse to find a video clip, with the right button call its popupmenu and choose "Create thumbnails for single file". After creation, invoke from the same menu "View cached frames". A window opens where a few thumbnails are visible. The first click on any image is ignored. I added a useless OnMouseDown() to the upper scrollbox, for testing; it exhibits the same behavior, the first click does not fire.

I suppose the LCL, at least in my context, has some variable stuck somewhere which does not reset at the right time. The first click does the job, but I can not simulate it.

Let's see what is happening, if someone has time to do this not-so-simple-test.

Thank you...

Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: linuxfan on August 19, 2019, 11:28:34 am
Quick update: in the FormShow() of fmCheck, the creation of images is commented out in the file attached. Sorry for this, please de-comment them to have the program work correctly.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: claudioya on August 11, 2020, 02:06:43 am
Hello,

I reproduced this error, in my case in TreeView control. See attachment, runned in Linux Mint 19.2 Cinnamon 4.2.4, Lazarus 2.0.6 FPC 3.0.4 X86_64-linux-gtk2.

For prove:
- Right click in the ListView control, will showed popupmenu
- If you click in TreeView after cancel popupmenu (click in other part of ListView), the first click not work. If do the same in ListView, you see that it work.

Thanks.
Title: Re: First OnClick / OnDblClick not firing on form (also Popup menus stop working)
Post by: jamie on August 11, 2020, 04:20:26 am
I think you are suffering from a Focus issue..

when changing forms for example, the focus needs to be set there first.

Its not a good idea to have a control take action when the parent of that control is not active yet. The parent should become active first and then your clicks on the child controls should be monitored.

 This does not work for all cases I have noticed because there are times I don't want  child controls to become active if the parent isn't yet active. I want the parent (form) to first come forward before any clicks get sent to its child controls..

 This may have a lot to do with the Z order of forms.


TinyPortal © 2005-2018