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
'text.
'GET TEXT ==============================
'All
nChars = SendMessage( hSci, %SCI_GetLength, 0, 0) 'get #chars
SendMessage hSci, %SCI_GetText , nChar, StrPTR(txt) 'retrieve #chars into txt
---Or---
Control Get Text hDlg, %ID_Sci To txt 'PowerBASIC commands
'Line
nChars = SendMessage( hSci, %SCI_LineLength , LineNum, 0) 'get line width
txt = String(nLen," ") + $Nul 'set buffer length
SendMessage hSci, %SCI_GetLine , 0, StrPTR(txt) 'get line text
'Current Line
nChars = SendMessage( hSci, %SCI_GetCurLine, 0, 0) 'get current line width
txt = String(nChars," ") + $Nul 'set buffer width
SendMessage hSci, %SCI_GetCurLine, nChars, StrPTR(txt) 'put line text in buffer
'Single Character
SendMessage hSci, %SCI_GetCharAt, iPos, 0 'get single character at iPos
'zero if iPos is negative or past end of document
'Get Range of Text (iStartPos to iEndPos)
Local TR as Sci_TextRange
TR.chrg.cpmin = iStart 'first character to get
TR.chrg.cpmax = iEnd 'last character to get
SendMessage hSci, %SCI_GetTextRange, 0, VarPTR(TR) 'get range of text
txt = TR.@lpstrText
'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
'SET TEXT =============================
'All
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt) 'Scintilla commands
---Or---
Control Set Text hDlg, %ID_Sci, txt 'PowerBASIC commands
'Replace Selection
SendMessage hSci, %SCI_ReplaceSel, 0, StrPTR(txt)
'Clear All
SendMessage hSci, %SCI_ClearAll, 0, 0
---Or---
Control Set Text hDlg, %ID_Sci, "" 'PowerBASIC commands
'Append
SendMessage hSci, %SCI_AppendText, iLen, StrPTR(txt) 'iLen <= Len(txt)
'Add at Position
SendMessage hSci, %SCI_InsertText, iLen, StrPTR(txt) 'iLen <= Len(txt), adds at curpos, curpos is moved
SendMessage hSci, %SCI_AddText, iPos, StrPTR(txt) 'all txt is added at iPos, curpos moved if at insertion point
'SIZE OPERATIONS ======================
'Get Text Width/Height
iWidth = SendMessage( hSci, %SCI_TextWidth, iStyle, StrPTR(txt)) 'in pixels
iHeight = SendMessage( hSci, %SCI_TextHeight, iLine, 0) 'in pixels
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Debug Error On 'catch array/pointer errors - OFF in production
#Debug Display On 'display untrapped errors - OFF in production#Include "Win32API.inc"
#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, "Get Text", 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
Local TR as Sci_TextRange, txt As String
Local txtZ as Asciiz * 200
TR.chrg.cpmin = 0 'first character to get
TR.chrg.cpmax = 15 'last character to get
TR.lpstrText = VarPTR(txtZ)
SendMessage hSci, %SCI_GetTextRange, 0, VarPTR(TR) 'get range of text
? TR.@lpstrText
End Sub
Sub TestB
SendMessage hSci, %SCI_ClearAll, 0, 0
End Sub
'gbs_00654
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm