Date: 02-16-2022
Return to Index
created by gbSnippets
'When a user is moving through source code and the caret is at a brace,
'Scintilla can be used to highlight the brace and it's matching brace.
'Given a starting position, Scintilla can find the position of a corresponding
'matching brace. The brace pairs supported are (), [], {}, and <>. With the
'two positions in hand, Scintilla offers a command that will set both positions
'to the style STYLE_BraceHighlight. There is also a style STYLE_BraceBad that
'can be used if no matching brace is found.
'In the case of finding a matching brace, a match only occurs if the style of the
'matching brace is the same as the starting brace or if the matching brace is
'beyond the end of styling.
'Primary Code:
'Set highlighting color (default values are Style_Default, so must change)
SendMessage hSci, %SCI_StyleSetFore, %Style_BraceLight, %Red 'color with match
SendMessage hSci, %SCI_StyleSetFore, %Style_BadBrace, %Blue 'color with no match
'Get positioning of matching brace
iPosB = SendMessage( hSci, %SCI_BraceMatch, iPosA, 0)
'Set color of both brace characters
SendMessage hSci, %SCI_BraceHighlight, iPosA, iPosB
'Compilable Example: (Jose Includes)
'This example shows highlighting when matching braces are found. Delete
'the right-mose brace and it will also show the highlighting when there is
'no matching brace.
#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
Global iPosA, iPosB As Long
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, "Match", 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 sin(25) = 2 Then" + $CrLf + " 'do nothing" + $Crlf
txt = txt + "Else" + $crlf + " x = Rnd(1,2)" + $crlf + "End If" + Chr$(0)
SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
SendMessage hSci, %SCI_StyleSetFore, %Style_BraceLight, %Red 'set brace highlighting color
SendMessage hSci, %SCI_StyleSetBack, %Style_BraceLight, %Yellow 'set brace highlighting color
SendMessage hSci, %SCI_StyleSetFore, %Style_BraceBad, %Green 'set brace bad color
SendMessage hSci, %SCI_StyleSetBack, %Style_BraceBad, %Red 'set brace bad color
End Sub
Sub TestA
iPosA = 6
iPosB = SendMessage( hSci, %SCI_BraceMatch, iPosA, 0)
If iPosB = -1 Then
SendMessage hSci, %SCI_BraceBadlight, iPosA, iPosB
? "No natching brace found!"
Else
SendMessage hSci, %SCI_BraceHighlight, iPosA, iPosB
? "Matching brace found!" + Str$(iPosA) + Str$(iPosB)
End If
End Sub
Sub TestB
SendMessage hSci, %SCI_BraceBadLight, -1, 0
End Sub
'gbs_00658
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm