I need some cross plattform function that allows me to select text and place cursor in a StringGadget. It also should be able to return the selection.
Here is my version for Windows. If you already did something before or are familiar with CocoaMessage() on MacOS, can you please insert your code at the TODO comments or at least give me a link to further information about how to get?
Code: Select all
EnableExplicit
; Sets the selection in a StringGadget() independent of Operating System
; If Start.i < 1, every selectionis removed.
; If Length.i = 0, only the cursor is set.
Procedure SetStringGadgetSelection(GadgetID.i, Start.i, Length.i)
  If Start.i > 0 And Length.i > -1
    ; Set selection
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(GadgetID.i), #EM_SETSEL, Start.i - 1, Start.i + Length.i - 1)  
      CompilerCase #PB_OS_Linux
        ; TODO
      CompilerCase #PB_OS_MacOS
        ; TODO
    CompilerEndSelect
  Else
    ; Deselect all
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(GadgetID.i), #EM_SETSEL, -1, 0)
      CompilerCase #PB_OS_Linux
        ; TODO
      CompilerCase #PB_OS_MacOS
        ; TODO
    CompilerEndSelect
  EndIf
EndProcedure
Procedure.i GetStringGadgetSelectionStart(GadgetID.i)
  ; Get selection
  Protected Start.i = 0
  Protected Stop.i = 0
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(GadgetID.i), #EM_GETSEL, @Start.i, @Stop.i)
      ProcedureReturn Start.i + 1
    CompilerCase #PB_OS_Linux
      ; TODO
    CompilerCase #PB_OS_MacOS
      ; TODO
  CompilerEndSelect
EndProcedure
Procedure.i GetStringGadgetSelectionLength(GadgetID.i)
  ; Get selection
  Protected Start.i = 0
  Protected Stop.i = 0
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(GadgetID.i), #EM_GETSEL, @Start.i, @Stop.i)
      ProcedureReturn Stop.i - Start.i
    CompilerCase #PB_OS_Linux
      ; TODO
    CompilerCase #PB_OS_MacOS
      ; TODO
  CompilerEndSelect
EndProcedure
If OpenWindow(0, 200, 300, 195, 160, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  Define strGadgID.i = StringGadget(#PB_Any, 10, 10, 180, 20, "Testcontent")
  
  SetActiveGadget(strGadgID.i)
  
  SetStringGadgetSelection(strGadgID.i, 3, 4) ; mark 4 characters beginning with the 3rd character
  
  Debug "Cursor before " + Str(GetStringGadgetSelectionStart(strGadgID.i))
  Debug "Marked characters: " + Str(GetStringGadgetSelectionLength(strGadgID.i))
  
  Repeat
    Define Event.i = WaitWindowEvent()
    If Event.i = #PB_Event_CloseWindow
      Define Quit.i = 1
    EndIf
  Until Quit.i = 1
EndIf
EndKukulkan




