TextField/Area caret (insertion point) color ?

  1. 4 years ago

    Hi,

    I have a transparent TextField but unfortunately the caret is set to white when using a transparent one.
    I've found some cocoa declares and suggestion using fieldEditor and setInsertionPoint but I am not able to get the fieldEditor
    object nor I understand what this is.
    Do you know a simple way to change the color of the caret ?

    I would suggest setting the InsertionPointColor in the gotFocus event, since the editor is shared by all text controls in the window and the setting won't likely hold when focus changes.
    You get a reference to the field editor from the window.

    Here's a quick sample..

    Sub GotFocus()
      declare Function NSClassFromString lib "Foundation" (className as CFStringRef) as ptr
      declare Function fieldEditorforObject lib "AppKit" selector "fieldEditor:forObject:" (Obj as ptr, create as Boolean, target as ptr) as ptr
      declare Function blackColor lib "AppKit" selector "blackColor" (Obj as ptr) as ptr
      declare sub setInsertionPointColor lib "AppKit" selector "setInsertionPointColor:" (obj as ptr, clr as ptr)
      
      dim editor as ptr=fieldEditorforObject( ptr(self.Handle) ,true ,ptr(me.Handle) )
      setInsertionPointColor( editor, blackColor( NSClassFromString("NSColor") ) )
      
    End Sub
  2. Michel B

    6 Jan 2016 Pre-Release Testers, Xojo Pro
    Edited 4 years ago

    If I remember right, there is no way to change the I-Beam caret.

    I bet one of our geniuses will promptly post a magical declare. However, you could also create your own caret by inserting a colored vertical bar | &u007C or a broken bar &u00A6 at SelStart, then remove it when SelStart changes and insert it again. Or remove it when the TextField loses focus.

    Seems relatively easy to do in the SelChange event. Since the white caret is not very visible, it could do the trick.

  3. Tim P

    6 Jan 2016 Pre-Release Testers Rochester, NY

    I don't know why Will deleted his post, but it was to something along the lines of InsertionPointColor. Sounded right to me.
    I thought though, that the caret was automatically colored by OS X relative to the color underneath it. ie, you put it over something dark and it turns white.

  4. jim m

    6 Jan 2016 Pre-Release Testers, Xojo Pro Answer piDog.com

    I would suggest setting the InsertionPointColor in the gotFocus event, since the editor is shared by all text controls in the window and the setting won't likely hold when focus changes.
    You get a reference to the field editor from the window.

    Here's a quick sample..

    Sub GotFocus()
      declare Function NSClassFromString lib "Foundation" (className as CFStringRef) as ptr
      declare Function fieldEditorforObject lib "AppKit" selector "fieldEditor:forObject:" (Obj as ptr, create as Boolean, target as ptr) as ptr
      declare Function blackColor lib "AppKit" selector "blackColor" (Obj as ptr) as ptr
      declare sub setInsertionPointColor lib "AppKit" selector "setInsertionPointColor:" (obj as ptr, clr as ptr)
      
      dim editor as ptr=fieldEditorforObject( ptr(self.Handle) ,true ,ptr(me.Handle) )
      setInsertionPointColor( editor, blackColor( NSClassFromString("NSColor") ) )
      
    End Sub
  5. Edited 4 years ago

    Thank Jim! I was so close to solve it myself but had the TextField in a container. This means you need to get the window from self.window.handle instead of self.handle. Gosh! :)

    declare sub setInsertionPointColor lib CocoaLib selector "setInsertionPointColor:" (obj_id as Ptr, aColor as Ptr)
    declare function blackColor lib CocoaLib selector "blackColor" (ob_id As Ptr) As Ptr
    declare function fieldEditorForObject lib CocoaLib selector "fieldEditor:forObject:" (ob_id As Ptr, flag as boolean, f as ptr) As Ptr

    dim e as ptr = fieldEditorForObject( ptr(self.Window.Handle), true, ptr(me.Handle) )
    if e<>nil then
    setInsertionPointColor( e, blackColor(NSClassFromString("NSColor")) )
    end if

    As an extension for the TextField:

    Sub caretColor(extends t as TextField, assigns c as color)
      declare sub setInsertionPointColor lib CocoaLib selector "setInsertionPointColor:" (obj_id as Ptr, aColor as Ptr)
      declare function blackColor lib CocoaLib selector "blackColor" (ob_id As Ptr) As Ptr
      declare function fieldEditorForObject lib CocoaLib selector "fieldEditor:forObject:" (ob_id As Ptr, flag as boolean, f as ptr) As Ptr
      declare function colorFromRGBA lib CocoaLib selector "colorWithCalibratedRed:green:blue:alpha:" (class_id as Ptr, red as CGFloat, green as CGFloat, blue as CGFloat, alpha as CGFloat) as Ptr
      declare Function NSClassFromString Lib CocoaLib (aClassName as CFStringRef) As Ptr
      
      dim col as ptr = colorFromRGBA(NSClassFromString("NSColor"),c.Red/255,c.Green/255,c.Blue/255,(255-c.alpha)/255 )
      if col<>nil then
        dim e as ptr = fieldEditorForObject( ptr(t.TrueWindow.Handle), true, ptr(t.Handle) )
        if e<>nil then
          setInsertionPointColor( e, col )
        end if
      end if
    End Sub
  6. 5 months ago

    Dave S

    Jul 17 San Diego, California USA

    is there a Windows version of this?

  7. Dave S

    Jul 18 San Diego, California USA

    It seems my cursor would vanish under certain circumstances on macOS, and reseting the color solved it.

    This issue did not (so far) seem to occur under Windows.... but my app is haveing other problems there :)

or Sign Up to reply!