Date: 02-16-2022
Return to Index
created by gbSnippets
'The Scintilla control uses the following numbering conventions:
' - lines are numbered 0 to nLines-1 (nLines is # of lines in the document)
' - characters are numbered 0 to nLen-1 (nLen is # of characters in the document)
' - positions refer to a character or the gap before that characters.
' - position 0 is before the first character
' - position nLen is after the last character
'A caret goes between characters, or after the last character, so it can be
'in position 0 to nLen
'A selection goes from an anchor to the current pos (caret). A selection may be
'made such that the anchor is a greater value then the current position, such as
'by dragging the mouse from a high position character to low position character.
'Primary Code:
'Text, positions, counts and settings are the basic kinds of information that
'describes the content of a Scintilla control. This snippets covers retrieving/setting
'selected text.
'SET SELECTION (TEXT) ========================
'Select None
SendMessage hSci, %SCI_SetSel, -1, iCurPos 'sets anchor=curpos
---Or---
SendMessage hSci, %SCI_GoToPos, iPos, 0 'no selection (set anchor=curpos=iPos)
---Or---
SendMessage hSci, %SCI_GoToLine, iLine, 0 'no selection (set anchor=curpos=1stCharofLine)
'Select All
SendMessage hSci, %SCI_SelectAll
'Select to End-of-Document
SendMessage hSci, %SCI_SetSel, iAnchor, -1 'anchor to end-of-doc is selected
'Select Specified Range
SendMessage hSci, %SCI_SetSel, iAnchor, iCurPos 'set start/end of selection
---Or---
SendMessage hSci, %SCI_SetSelectionStart, iStart, 0 'creates selection, rearrange anchor/curpos, caret not scrolled
SendMessage hSci, %SCI_SetSelectionEnd, iEnd, 0 'creates selection, rearrange anchor/curpos, caret not scrolled
---Or---
SendMessage hSci, %SCI_SetAnchor, iStart, 0 'creates selection, caret not scrolled into view
SendMessage hSci, %SCI_SetCurpos, iEnd, 0 'creates selection, caret not scrolled into view
'Hide Selection
SendMessage hSci, %SCI_HideSelection, 1, 0 '1=hide, 0=visible
'GET SELECTION (TEXT) ===========================
'Get Selected Text
iLen = SendMessage( hSci, %SCI_GetSelText, 0, 0) 'get required buffer length
txt = String(iLen," ") + Chr$(0) 'size the buffer
SendMessage hSci, %SCI_GetSelText, 0, StrPTR(txt) 'get the text
'POSITION/COUNT =================================
'Char#: get length of selection
iChars = SendMessage( hSci, %SCI_GetSelText, 0, 0) '#chars selected
'Char#: get anchor/current of selection
iAnchor = SendMessage( hSci, %SCI_GetAnchor, 0, 0) 'anchor may be > curpos
iCurPos = SendMessage( hSci, %SCI_GetCurrentPos, 0, 0) 'curpos may be < anchor
'Char#: get start/end of selection
iStart = SendMessage( hSci, %SCI_GetSelectionStart, 0, 0) 'Scintilla adjusts so that iStart < iEnd
iEnd = SendMessage( hSci, %SCI_GetSelectionEnd, 0, 0) 'Scintilla adjusts so that iStart < iEnd
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "scintilla_gb.inc"
%ID_Sci = 1000 : %ID_BtnA = 1001 : %ID_BtnB = 1002
Global hDlg, hSci, hLib As DWord
Function PBMain() As Long
hLib = LoadLibrary("SCILEXER.DLL")
Dialog New Pixels, 0, "Scintilla Example",300,300,300,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_BtnA, "Select", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Clear", 10,40,70,20, %WS_Child Or %WS_Visible
Control Add "Scintilla", hDlg, %ID_Sci, "", 100,10,180,130, %WS_Child Or %WS_Visible
Control Handle hDlg, %ID_Sci To hSci 'get handle to Scintilla window
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local txt As String
txt = "Select Case var$ 'first line" + $CrLf + "End Select 'last line" + Chr$(0)
Select Case CB.Msg
Case %WM_InitDialog
InitializeScintilla
PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
Case %WM_Command
Select Case CB.Ctl
Case %ID_BtnA : TestA
Case %ID_BtnB : TestB
End Select
Case %WM_Size
Control Set Size hDlg, %ID_Sci, Lo(Word, CB.lParam)-110, Hi(Word, CB.lParam)-20
Case %WM_Destroy
If hLib Then FreeLibrary hLib 'free the Scintilla library
End Select
End Function
Sub InitializeScintilla
Local txt As String
txt = "If x = 2 Then" + $CrLf + " 'do nothing" + $Crlf
txt = txt + "Else" + $crlf + " x = 0" + $crlf + "End If" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
End Sub
Sub TestA
SendMessage hSci, %SCI_SetSel, 47, -1 'anchor to end-of-doc is selectedEnd Sub
End Sub
Sub TestB
SendMessage hSci, %SCI_GoToPos, 0, 0 'no selection (set anchor=curpos=0)
Control Set Focus hDlg, %ID_Sci
End Sub
'gbs_00653
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm