changing mouse cursor

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: changing mouse cursor

Post by Shardik »

By analysing which WindowEvents PB is reporting during an event loop
with WaitWindowEvent(), I found out that while moving the mouse a
-1 event is reported. Consequently I was able to simplify my above
code example by removing the EventHandler and repaint the cursor on
every event with -1:

Code: Select all

EnableExplicit

ImportC ""
  SetAnimatedThemeCursor(CursorType.L, AnimationStep.L)
  SetThemeCursor(CursorType.L)
EndImport

#kEventClassMouse = 'mous'
#kEventMouseMoved = 5
#kThemeArrowCursor = 0
#kThemeWatchCursor = 7
#kThemeSpinningCursor = 14

Define AnimationStep.L
Define CursorID.L
Define CursorIsAnimated.L
Define NumAnimationSteps.L
Define WindowEvent.L

OpenWindow(0, 200, 100, 250, 120, "Display animated wait cursor")
ButtonGadget(0, 10, 20, 230, 20, "Change cursor to spinning wheel")
ButtonGadget(1, 10, 50, 230, 20, "Change cursor to running watch")
ButtonGadget(2, 10, 80, 230, 20, "Restore default cursor")
DisableGadget(2, #True)

Repeat
  WindowEvent = WaitWindowEvent()

  Select WindowEvent
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          CursorID = #kThemeSpinningCursor
          NumAnimationSteps = 4
          CursorIsAnimated = #True
          AddWindowTimer(0, 0, 150)
          DisableGadget(0, #True)
          DisableGadget(1, #True)
          DisableGadget(2, #False)
        Case 1
          CursorID = #kThemeWatchCursor
          NumAnimationSteps = 8
          CursorIsAnimated = #True
          AddWindowTimer(0, 0, 150)
          DisableGadget(0, #True)
          DisableGadget(1, #True)
          DisableGadget(2, #False)
        Case 2
          CursorIsAnimated = #False
          RemoveWindowTimer(0, 0)
          SetThemeCursor(#kThemeArrowCursor)
          DisableGadget(0, #False)
          DisableGadget(1, #False)
          DisableGadget(2, #True)
      EndSelect
    Case #PB_Event_Timer
      If EventTimer() = 0
        SetAnimatedThemeCursor(CursorID, AnimationStep)
        AnimationStep = (AnimationStep + 1) % NumAnimationSteps
      EndIf
    Case -1
      If CursorIsAnimated
        SetAnimatedThemeCursor(CursorID, AnimationStep)
      Else
        SetThemeCursor(#kThemeArrowCursor)
      EndIf
  EndSelect
ForEver
And the shortest example of a non-changing wait cursor can even be
reduced to this:

Code: Select all

ImportC ""
  SetThemeCursor(CursorType.L)
EndImport

#kThemeArrowCursor = 0
#kThemeWatchCursor = 7

OpenWindow(0, 200, 100, 200, 180, "Display Wait cursor")
ButtonGadget(0, 40, 70, 120, 20, "Change cursor")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0
        If IsWaitCursor
          SetThemeCursor(#kThemeArrowCursor)
          IsWaitCursor = #False
        Else
          SetThemeCursor(#kThemeWatchCursor)
          IsWaitCursor = #True
        EndIf
      EndIf
    Case -1
      If IsWaitCursor
        SetThemeCursor(#kThemeWatchCursor)
      EndIf
  EndSelect
ForEver
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: changing mouse cursor

Post by WilliamL »

Shardik,

It crossed my mind to check for the -1 event and just change the cursor back to my choice. Somehow it seemed inefficient but it does seem to work fine. I think your last example could be worked into a program with a global for CursorID & AnimationStep (and GetActiveWindow for timer) and just set as needed. I'd have to try it to see if it was that simple.

Showing how to handle the steps in an animated cursor is also helpful.
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply