Lazarus

Programming => Operating Systems => Linux => Topic started by: fedkad on January 27, 2022, 11:25:15 am

Title: Cursor shape does not change unless I add Application.ProcessMessages
Post by: fedkad on January 27, 2022, 11:25:15 am
Create a new project with an empty form. Add a button and put the following code to the OnClick event:

Code: Pascal  [Select][+][-]
  1.   Screen.BeginWaitCursor;
  2.   sleep(2000);
  3.   Screen.EndWaitCursor;

On Windows it will act as expected. However, on Linux the cursor shape will not change. You have to change the Linux code to something like this to work:

Code: Pascal  [Select][+][-]
  1.   Screen.BeginWaitCursor;
  2.   Application.ProcessMessages;
  3.   sleep(2000);
  4.   Screen.EndWaitCursor;

Why?
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: Zvoni on January 27, 2022, 12:03:42 pm
Why?
[SARCASM ON]
Because this has nothing to do with installing Lazarus/FPC on Linux?
[SARCASM OFF]
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: trev on January 27, 2022, 12:08:10 pm
Moving from Linux > Installation sub-forum to OS > Linux sub-forum ... just for context for Zvoni's response :)
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: fedkad on January 27, 2022, 12:41:16 pm
Thanks for moving the topic and sorry for posting to wrong section!  ::) O:-)
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: Zvoni on January 27, 2022, 01:56:49 pm
As for the original question: https://lists.lazarus-ide.org/pipermail/lazarus/2011-April/192333.html
Quote
The LCL is not a winapi emulator.
Code: Pascal  [Select][+][-]
  1. Screen.BeginWaitCursor;
  2. {$IFDEF LINUX}Application.ProcessMessages;{$ENDIF}
  3. sleep(2000);
  4. Screen.EndWaitCursor;
  5.  
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: fedkad on January 27, 2022, 02:22:50 pm
As for the original question: https://lists.lazarus-ide.org/pipermail/lazarus/2011-April/192333.html
Quote
The LCL is not a winapi emulator.
Code: Pascal  [Select][+][-]
  1. Screen.BeginWaitCursor;
  2. {$IFDEF LINUX}Application.ProcessMessages;{$ENDIF}
  3. sleep(2000);
  4. Screen.EndWaitCursor;
  5.  

That is what I am using right now. But, I just wanted to reduce those {$ifdef Linux}Application.ProcessMessages;{$endif}s in my program.
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: Zvoni on January 27, 2022, 02:40:27 pm
Well then. The link above has the answer: the LCL is not a winapi-emulator.
On windows, the WinAPI itself makes sure the screen-object is updated (and that way showing the change of cursor)
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: Josh on January 27, 2022, 02:56:17 pm
Hi

To save all the if def you could ceate a proc;
Code: Pascal  [Select][+][-]
  1. procedure Do_Application_Processes_By_Os;inline;
  2. begin
  3.   {$ifdef linux}
  4.   Application.ProcessMessages;
  5.   {$endif}
  6. end;

then you could add the code into your routines;
Code: Pascal  [Select][+][-]
  1. Screen.BeginWaitCursor;
  2. Do_Application_Processes_By_Os;
  3. sleep(2000);
  4. Screen.EndWaitCursor;

or create youself the set cursor routine;

Code: Pascal  [Select][+][-]
  1. procedure Do_BeginWaitCursor;inline;
  2. begin
  3.   // under windows cursor state is set immediately by winapi
  4.   // under linux you must call processmessage to force the state change to happen.
  5.   Screen.BeginWaitCursor;
  6.   {$ifdef linux}
  7.   Application.ProcessMessages;
  8.   {$endif}
  9. end;
  10.  
  11. procedure Do_EndWaitCursor;inline;
  12. begin
  13.   // under windows cursor state is set immediately by winapi
  14.   // under linux you must call processmessage to force the state change to happen.
  15.   Screen.EndWaitCursor;
  16.   {$ifdef linux}
  17.   Application.ProcessMessages;
  18.   {$endif}
  19. end;
  20.  
  21. ......
  22.  
  23. Do_BeginWaitCursor;
  24. sleep(2000);
  25. Do_EndWaitCursor;
  26.  

   
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: zeljko on January 27, 2022, 03:16:27 pm
Create a new project with an empty form. Add a button and put the following code to the OnClick event:

Code: Pascal  [Select][+][-]
  1.   Screen.BeginWaitCursor;
  2.   sleep(2000);
  3.   Screen.EndWaitCursor;

On Windows it will act as expected. However, on Linux the cursor shape will not change. You have to change the Linux code to something like this to work:

Code: Pascal  [Select][+][-]
  1.   Screen.BeginWaitCursor;
  2.   Application.ProcessMessages;
  3.   sleep(2000);
  4.   Screen.EndWaitCursor;

Why?

What widgetset under linux ? I'm using qt/qt5 and haven't spotted such problem.
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: MarkMLl on January 27, 2022, 03:26:43 pm
What widgetset under linux ? I'm using qt/qt5 and haven't spotted such problem.

That being a massively-important omission from the OP.

Calling Application.ProcessMessages is unavoidable if one wants the UI to be immediately-responsive to minor changes.

Code: Pascal  [Select][+][-]
  1.       Screen.BeginWaitCursor;
  2.       sleep(2000);    // SUSPENDS ENTIRE PROGRAM: DOES NOT CALL APM
  3.       Screen.EndWaitCursor;
  4.  

In any event, one has to be careful overusing APM: if it's in code that's called- even indirectly- via Synchronize() then it can go recursive.

MarkMLl
Title: Re: Cursor shape does not change unless I add Application.ProcessMessages
Post by: fedkad on January 27, 2022, 05:02:43 pm
What widgetset under linux ? I'm using qt/qt5 and haven't spotted such problem.

I am using GTK2. That info was already given in my signature.

Josh's proposal seems to be the closest solution to my problem. Thanks.

Sleep was actually a placeholder for a group a statements that do file and/or network IO and may take a few seconds to complete.
TinyPortal © 2005-2018