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
'can be retrieved from a Scintilla control. This snippets covers retrieving count
'and position information.
'COUNT ================================
'#Characters in documents
nChars = SendMessage( hSci, %SCI_GetTextLength, 0, 0)
'#Chars in specified line
nChars = SendMessage( hSci, %SCI_LineLength, LineNum, 0)
'#Chars in current line (where caret is found)
nChars = SendMessage( hSci, %SCI_GetCurLine, 0, 0)
'#Lines in documents
nLines = SendMessage( hSci, %SCI_GetLineCount, 0, 0)
'#Lines visible in control (may includes a partially visible line at bottom of control
nLines = SendMessage( hSci, %SCI_LinesOnScreen, 0, 0)
'POSITION ==============================
'Caret: Position (current position)
iCaretPos = SendMessage( hSci, %SCI_GetCurrentPos, 0, 0) 'get caret position
'Char#: 1st/last char# in line
iStart = SendMessage( hSci, %SCI_PositionFromLIne, iLine, 0) 'position of 1st char in line
iEnd = SendMessage( hSci, %SCI_GetLineEndPosition, iLine, 0) 'postion of last char in line
'Char#: start/end position of word at specified position
iStart = SendMessage hSci(, %SCI_WordStartPostion, iPos, 1) 'position of start of word
iEnd = SendMessage hSci(, %SCI_WordEndPosition, iPos, 1) 'postion of end of word
'Char#: 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#: start/end of selection
iStart = SendMessage( hSci, %SCI_GetSelectionStart, 0, 0) 'iStart < iEnd
iEnd = SendMessage( hSci, %SCI_GetSelectionEnd, 0, 0) 'iStart < iEnd
'Line#: first visible line
iLine = SendMessage( hSci, %SCI_GetFirstVisibleLine, 0, 0)
'Line#: line containing character position
iLine = SendMessage( hSci, %SCI_LineFromPosition, iPos, 0) 'line that contains iCaretPos
'Line#: current line
iCaretPos = SendMessage( hSci, %SCI_GetCurrentPos, 0, 0) 'position of caret
iCurLine = SendMessage( hSci, %SCI_LineFromPosition, iCaretPos, 0) 'line that contains iCaretPos
'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, "Chars", 10,10,70,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Lines", 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
MsgBox "Characters in document: " + Str$(SendMessage(hSci, %SCI_GetTextLength, 0, 0))
End Sub
Sub TestB
MsgBox "Lines in document: " + Str$(SendMessage(hSci, %SCI_GetLineCount, 0, 0))
End Sub
'gbs_00652
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm