Code: Select all
EnableExplicit
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
ImportC ""
GetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer, *ActualSize)
SetControlData(ControlRef.L, ControlPartCode.L, TagName.L, BufferSize.L, *Buffer)
EndImport
#kControlEditTextPart = 5
#kControlEditTextSelectionTag = $73656C65 ; 'sele'
Structure ControlEditTextSelectionRec
SelStart.W
SelEnd.W
EndStructure
CompilerElse
Global NSRangeZero.NSRange
Procedure.i TextEditor(Gadget.i)
Protected TextField.i = GadgetID(Gadget)
Protected Window.i = CocoaMessage(0, TextField, "window")
ProcedureReturn CocoaMessage(0, Window, "fieldEditor:", #YES, "forObject:", TextField)
EndProcedure
CompilerEndIf
CompilerEndIf
; Sets the selection in a StringGadget() independent of Operating System
; If Start.i < 1, every selection is 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), #EM_SETSEL, Start.i - 1, Start.i + Length.i - 1)
CompilerCase #PB_OS_Linux
gtk_editable_select_region_(GadgetID(GadgetID), Start - 1, Start -1 + Length)
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected TextSelection.ControlEditTextSelectionRec
TextSelection\selStart = Start - 1
TextSelection\selEnd = Start -1 + Length
SetControlData(GadgetID(GadgetID), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
CompilerElse
Protected Range.NSRange\location = Start - 1 : Range\length = Length
CocoaMessage(0, TextEditor(GadgetID), "setSelectedRange:@", @Range)
CompilerEndIf
CompilerEndSelect
Else
; Deselect all
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
SendMessage_(GadgetID(GadgetID.i), #EM_SETSEL, -1, 0)
CompilerCase #PB_OS_Linux
gtk_editable_delete_selection_(GadgetID(GadgetID))
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
TextSelection\selStart = -1
TextSelection\selEnd = -1
SetControlData(GadgetID(GadgetID), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection)
CompilerElse
CocoaMessage(0, TextEditor(GadgetID), "setSelectedRange:@", @NSRangeZero)
CompilerEndIf
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
gtk_editable_get_selection_bounds_(GadgetID(GadgetID), @Start, @Stop)
ProcedureReturn Start + 1
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected TextSelection.ControlEditTextSelectionRec
GetControlData(GadgetID(GadgetID), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection.ControlEditTextSelectionRec, 0)
ProcedureReturn TextSelection\SelStart + 1
CompilerElse
Protected Range.NSRange
CocoaMessage(@Range, TextEditor(GadgetID), "selectedRange")
ProcedureReturn Range\location + 1
CompilerEndIf
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), #EM_GETSEL, @Start.i, @Stop.i)
ProcedureReturn Stop.i - Start.i
CompilerCase #PB_OS_Linux
gtk_editable_get_selection_bounds_(GadgetID(GadgetID), @Start, @Stop)
ProcedureReturn Stop - Start
CompilerCase #PB_OS_MacOS
CompilerIf #PB_Compiler_Version < 470 Or (#PB_Compiler_Version >= 500 And Subsystem("Carbon"))
Protected TextSelection.ControlEditTextSelectionRec
GetControlData(GadgetID(GadgetID), #kControlEditTextPart, #kControlEditTextSelectionTag, SizeOf(ControlEditTextSelectionRec), @TextSelection.ControlEditTextSelectionRec, 0)
ProcedureReturn TextSelection\SelEnd - TextSelection\SelStart
CompilerElse
Protected Range.NSRange
CocoaMessage(@Range, TextEditor(GadgetID), "selectedRange")
ProcedureReturn Range\length
CompilerEndIf
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
End
Code: Select all
#kControlEditTextSelectionTag = 'sele'
Code: Select all
#kControlEditTextSelectionTag = $73656C65 ; 'sele'