I already gave you here the hint to take a look into my list of cross-platform API examples. I know that it's somewhat hidden but the admins and mods don't seem to think it important enough to move that posting to a more prominent place or even make it sticky...
In that list you would have found links to code examples which get and set the cursor in a StringGadget. The code example thankfully linked to by c4s does even a bit more because it not only gets or sets the cursor but also returns selected text or does select text.
The example from my cross-platform list is a bit outdated because the Mac part uses the Carbon framework which is deprecated since PB 5.20. I therefore modified it for the current Cocoa framework on MacOS X:
Code: Select all
EnableExplicit
Procedure SetCursorPosition(StringGadgetID.I, CursorPosition.I)
SetActiveGadget(StringGadgetID)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(StringGadgetID), #EM_SETSEL, CursorPosition, CursorPosition)
CompilerCase #PB_OS_Linux
gtk_editable_set_position_(GadgetID(StringGadgetID), CursorPosition)
CompilerCase #PB_OS_MacOS
Protected Range.NSRange
Protected TextView.I
Range\location = CursorPosition
Range\length = 0
TextView = CocoaMessage(0, GadgetID(StringGadgetID), "currentEditor")
If TextView
CocoaMessage(0, TextView, "setSelectedRange:@", @Range)
EndIf
CompilerEndSelect
EndProcedure
Procedure.I GetCursorPosition(StringGadgetID.I)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ProcedureReturn SendMessage_(GadgetID(StringGadgetID), #EM_GETSEL, 0, 0) >> 16
CompilerCase #PB_OS_Linux
ProcedureReturn gtk_editable_get_position_(GadgetID(StringGadgetID))
CompilerCase #PB_OS_MacOS
Protected CursorPosition.I
Protected TextView.I = CocoaMessage(0, GadgetID(StringGadgetID), "currentEditor")
If TextView
CursorPosition = CocoaMessage(0, CocoaMessage(0,
CocoaMessage(0, TextView, "selectedRanges"),
"objectAtIndex:", 0), "rangeValue")
EndIf
ProcedureReturn CursorPosition
CompilerEndSelect
EndProcedure
OpenWindow(0, 270, 100, 350, 70, "StringGadget")
StringGadget(0, 10, 20, WindowWidth(0) - 20, 25,
"The quick brown fox jumps over the lazy dog.")
SetCursorPosition(0, 16) ; Set cursor in front of 'fox'
Debug "Current cursor postion: " + Str(GetCursorPosition(0))
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow