Mouse Icon

Mac OSX specific forum
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 203
Joined: Thu Dec 28, 2023 9:04 pm

Mouse Icon

Post by rndrei »

How can I disable the cursor icon above editorgadget() and put my own icon?
Here is an example of Shardik, but when you hover over editorgadget, it becomes standard.

Code: Select all

Define Hotspot.NSPoint
OpenWindow(0, 200, 100, 290, 300, "Display custom cursor")
ButtonGadget(0, WindowWidth(0) / 2 - 120, 40, 240, 25, "Change cursor to custom image")
EditorGadget(1,50,150,100,100)

UsePNGImageDecoder()

If LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/World.png")
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
          Hotspot\x = 4
          Hotspot\y = 4
          NewCursor = CocoaMessage(0, 0, "NSCursor alloc")
          CocoaMessage(0, NewCursor, "initWithImage:", ImageID(0), "hotSpot:@", @Hotspot)
          CocoaMessage(0, NewCursor, "set")
        EndIf
    EndSelect
  ForEver
EndIf
User avatar
Shardik
Addict
Addict
Posts: 2074
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Mouse Icon

Post by Shardik »

rndrei wrote: How can I disable the cursor icon above editorgadget() and put my own icon?
The EditorGadget forces the cursor to always be displayed as an insertion cursor. In order to display a custom cursor, you have to subclass the EditorGadget (internally a NSTextView) and check in a callback for all mouse move events. In each mouse move event you have to set your custom cursor. I have tested the following example successfully on MacOS 15.7.1 'Seqouia' with PB 6.21 and PB 6.30 Beta 4:

Code: Select all

EnableExplicit

#NSMouseMoved = 5

UsePNGImageDecoder()

Define CustomCursor.I
Define Hotspot.NSPoint
Define SubclassedEditorGadget.I

Procedure SubclassGadget(GadgetID.I, NewClassName.S)
  Protected GadgetClass.I = CocoaMessage(0, GadgetID(GadgetID), "class")
  Protected NewGadgetClass.I

  NewGadgetClass = objc_allocateClassPair_(GadgetClass, NewClassName, 0)
  objc_registerClassPair_(NewGadgetClass)
  object_setClass_(GadgetID(GadgetID), NewGadgetClass)

  ProcedureReturn NewGadgetClass
EndProcedure

ProcedureC EditorEventCallback(Object.I, Selector.I, Event.I)
  Shared CustomCursor.I

  If CocoaMessage(0, Event, "type") = #NSMouseMoved
    CocoaMessage(0, CustomCursor, "set")
  EndIf
EndProcedure

OpenWindow(0, 200, 100, 320, 140, "Display custom cursor above editor")
EditorGadget(0, 90, 20, 140, 100)

; ----- Subclass EditorGadget and define callback for cursor moving
SubclassedEditorGadget = SubclassGadget(0, "SubclassedEditorGadget")
class_addMethod_(SubclassedEditorGadget, sel_registerName_("mouseMoved:"),
  @EditorEventCallback(), "v@:@")

If LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/World.png")
  ; ----- Create custom cursor
  Hotspot\x = 4
  Hotspot\y = 4
  CustomCursor = CocoaMessage(0, 0, "NSCursor alloc")
  CocoaMessage(0, CustomCursor,
    "initWithImage:", ImageID(0),
    "hotSpot:@", @Hotspot)
  
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Collection of cross-platform examples with API functions to extend PureBasic
User avatar
Piero
Addict
Addict
Posts: 1102
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Mouse Icon

Post by Piero »

Shardik wrote: Thu Oct 30, 2025 10:28 pmexample
I'm not much into cocoa, but THANKS for all your chocolates! :D
User avatar
Shardik
Addict
Addict
Posts: 2074
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Mouse Icon

Post by Shardik »

Piero wrote: I'm not much into cocoa, but THANKS for all your chocolates! :D
You are welcome to enjoy my chocolates! :D
Collection of cross-platform examples with API functions to extend PureBasic
Post Reply