cocoa and carbon cursor

Mac OSX specific forum
mestnyi
Addict
Addict
Posts: 1001
Joined: Mon Nov 25, 2013 6:41 am

cocoa and carbon cursor

Post by mestnyi »

https://developer.mozilla.org/ru/docs/Web/CSS/cursor
https://developer.apple.com/documentati ... 000326-SW7
why doesn't it work in cocoa I-beam cursor? found the reason

Code: Select all

result = CocoaMessage(0, 0, "NSCursor IBeamCursor")
[/b]

Code: Select all

Global setCursor = #PB_Cursor_Hand

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_MacOS
    
    #kThemeArrowCursor                   = 0
    #kThemeIBeamCursor                   = 4
    #kThemePointingHandCursor            = 10
    
    ImportC ""
      SetAnimatedThemeCursor(CursorType.i, AnimationStep.i)
      SetThemeCursor(CursorType.i)
    EndImport
    
    Procedure GetCursor( )
      Protected result.i
      
      Select CocoaMessage(0, 0, "NSCursor currentCursor")
        Case CocoaMessage(0, 0, "NSCursor arrowCursor") : result = #PB_Cursor_Default
        Case CocoaMessage(0, 0, "NSCursor pointingHandCursor") : result = #PB_Cursor_Hand
      EndSelect 
      
      ProcedureReturn result
    EndProcedure
    
    Procedure SetCursor( cursor.i )
      Protected result
      
      setCursor = cursor
      
      Select cursor
        Case #PB_Cursor_Default : result = CocoaMessage(0, 0, "NSCursor arrowCursor")
        Case #PB_Cursor_Busy 
          SetAnimatedThemeCursor( 7, 0 )
          
        Case #PB_Cursor_IBeam ; : result = CocoaMessage(0, 0, "NSCursor iBeamCursor") ; not work
          SetThemeCursor( #kThemeIBeamCursor )
          
        Case #PB_Cursor_Hand : result = CocoaMessage(0, 0, "NSCursor pointingHandCursor")
      EndSelect 
      
      If result
       ; SetGadgetAttribute( EventGadget( ), #PB_Canvas_Cursor, cursor )
       CocoaMessage(0, result, "set")
      EndIf
      
      ProcedureReturn cursor
    EndProcedure
    
CompilerEndSelect


OpenWindow(2, 450, 380, 328, 328, "window_1", #PB_Window_SystemMenu)
  Busy = CanvasGadget(#PB_Any, 8, 8, 56, 56)
  IBeam = CanvasGadget(#PB_Any, 72, 8, 56, 56)
  Hand = CanvasGadget(#PB_Any, 136, 8, 56, 56)
  
  
  Define dragButtons, getCursor =- 1
Repeat 
  event = WaitWindowEvent( )
  
  If event = #PB_Event_Gadget
    Select EventType( )
      Case #PB_EventType_LeftButtonDown
        dragButtons = 1
        
      Case #PB_EventType_LeftButtonUp
        dragButtons = 0
        If GetCursor( ) = setCursor 
          SetCursor( #PB_Cursor_Default )
        EndIf
          
      Case #PB_EventType_MouseMove
        ;If dragButtons ; GetGadgetAttribute( EventGadget( ), #PB_Canvas_Buttons )
          If GetCursor( ) <> setCursor And setCursor <> #PB_Cursor_Busy
            Debug " update cursor - "+GetGadgetAttribute( EventGadget( ), #PB_Canvas_Cursor )
            ; SetGadgetAttribute( EventGadget( ), #PB_Canvas_Cursor, setCursor )
            SetCursor( setCursor )
          EndIf
        ;EndIf
        
      Case #PB_EventType_MouseEnter
        
        Select EventGadget( )
          Case Busy : SetCursor( #PB_Cursor_Busy )
          Case IBeam : SetCursor( #PB_Cursor_IBeam )
          Case Hand : SetCursor( #PB_Cursor_Hand )
            
        EndSelect
        
        ; SetGadgetAttribute( EventGadget( ), #PB_Canvas_Cursor, setCursor )
       
      Case #PB_EventType_MouseLeave
        If Not dragButtons
          SetCursor( #PB_Cursor_Default )
        EndIf
        
    EndSelect
  EndIf
  
  If event =- 1
    If getCursor <> GetCursor( )
      getCursor = GetCursor( )
      Debug " get "+getCursor
    EndIf
  EndIf
  
Until event = #PB_Event_CloseWindow
Last edited by mestnyi on Sun Apr 10, 2022 8:40 pm, edited 1 time in total.
mestnyi
Addict
Addict
Posts: 1001
Joined: Mon Nov 25, 2013 6:41 am

Re: cocoa and carbon cursor

Post by mestnyi »

What amazing things are happening? Using SetWindowTitle() resets the cursor.

Code: Select all

Structure s_buttons
  x.i
  y.i
  width.i
  height.i
  color.i
EndStructure

Global NewList buttons.s_buttons( )
#kThemeArrowCursor                   = 0
#kThemeResizeLeftCursor              = 15
#kThemeResizeRightCursor             = 16
#kThemeResizeLeftRightCursor         = 17

ImportC ""
  SetAnimatedThemeCursor(CursorType.i, AnimationStep.i)
  SetThemeCursor(CursorType.i)
  CGCursorIsVisible( )
EndImport

Procedure addButtons( x, y , color)
  AddElement(buttons( ))
  buttons()\x = x
  buttons()\y = y
  buttons()\width = 30
  buttons()\height = 30
  buttons()\color = color
EndProcedure

Procedure darwButtons( )
  StartDrawing(CanvasOutput(1))
  ForEach buttons( )
    Box(buttons()\x,buttons()\y,buttons()\width,buttons()\height, buttons()\color)
  Next
  StopDrawing()
EndProcedure

Macro is_at_box_( _position_x_, _position_y_, _size_width_, _size_height_, _mouse_x_, _mouse_y_ )
  Bool( _mouse_x_ >= _position_x_ And _mouse_x_ < ( _position_x_ + _size_width_ ) And ( _position_x_ + _size_width_ ) > 0 And 
        _mouse_y_ >= _position_y_ And _mouse_y_ < ( _position_y_ + _size_height_ ) And ( _position_y_ + _size_height_ ) > 0 )
EndMacro


Procedure HitTest( mouse_x, mouse_y )
  ForEach buttons( )
    If is_at_box_( buttons()\x, buttons()\y, buttons()\width, buttons()\height, mouse_x, mouse_y )
      ProcedureReturn ListIndex(buttons()) + 1
    EndIf
  Next
EndProcedure

If OpenWindow(0, 200, 200, 300, 150, "", #PB_Window_ScreenCentered)
  CanvasGadget  (1, 10, 10, 280, 130)
  addButtons( 50,50, RGB(245, 112, 44) )
  addButtons( 100,50, RGB(44, 245, 111) )
  addButtons( 150,50, RGB(225, 44, 245) )
  darwButtons( )
  
  Define lastEnter
  Repeat
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget
        Select EventType()
            
          Case #PB_EventType_MouseMove
            enter = HitTest(GetGadgetAttribute(1, #PB_Canvas_MouseX), GetGadgetAttribute(1, #PB_Canvas_MouseY) )
            
            If lastEnter <> enter
              lastEnter = enter
              
              If enter
                If enter = 1
                  SetThemeCursor(#kThemeResizeLeftCursor)
                EndIf
                If enter = 2
                  SetThemeCursor(#kThemeResizeLeftRightCursor)
                EndIf
                If enter = 3
                  SetThemeCursor(#kThemeResizeRightCursor)
                EndIf
                
                ; comment\uncomment to see 
                ; why remove cursor ????????????????????
                SetWindowTitle( EventWindow( ), Str(enter)+" - "+Str(Random(255)))
                
              ElseIf Not GetGadgetAttribute(1, #PB_Canvas_Buttons)
                SetThemeCursor(#kThemeArrowCursor)
              EndIf
              
            EndIf
        EndSelect
        
    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf
Post Reply