Recent

Author Topic: Cursor shape does not change unless I add Application.ProcessMessages  (Read 2010 times)

fedkad

  • Full Member
  • ***
  • Posts: 158
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?
Lazarus 2.2.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #1 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]
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1977
  • Former Delphi 1-7, 10.2 user
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #2 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 :)
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 x86_64 Xcode 13.4
Lazarus 2.3, FPC 3.3.1 macOS 12.3.1 aarch64 Xcode 13.4

fedkad

  • Full Member
  • ***
  • Posts: 158
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #3 on: January 27, 2022, 12:41:16 pm »
Thanks for moving the topic and sorry for posting to wrong section!  ::) O:-)
Lazarus 2.2.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #4 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.  
« Last Edit: January 27, 2022, 01:58:51 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

fedkad

  • Full Member
  • ***
  • Posts: 158
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #5 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.
Lazarus 2.2.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

Zvoni

  • Hero Member
  • *****
  • Posts: 1185
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #6 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)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Josh

  • Hero Member
  • *****
  • Posts: 931
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #7 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.  

   
« Last Edit: January 27, 2022, 03:15:40 pm by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX.. 
Any posts made from 2015 will be based on Lazarus Trunk.

zeljko

  • Hero Member
  • *****
  • Posts: 1251
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #8 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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 4183
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #9 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

fedkad

  • Full Member
  • ***
  • Posts: 158
Re: Cursor shape does not change unless I add Application.ProcessMessages
« Reply #10 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.
« Last Edit: January 27, 2022, 05:06:14 pm by fedkad »
Lazarus 2.2.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

 

TinyPortal © 2005-2018