Date: 02-16-2022
Return to Index
created by gbSnippets
'In addition to the editable text displayed in a Scintilla control, a programmer might
'want to display non-editable information into the document - such notes, comments, or
'even real-time content analysis.
'To support that need, Scintilla allows the programmer to associate a string of multi-line
'text with each line, and display that text below the line with it's own styling.
'How the strings are generated and when they are assigned is up to the programmer.
'A few other aspects of annoations:
' - annoations are not assigned a line number.
' - setting an annotation sends a SC_Mod_ChangeAnnotation message to the client
'Primary Code:
'To set the text, use the following message. Note that this is a multi-line example
'(the $crlf are used to separate lines)
txt = "annotation" + Chr$(10) + "example" + Chr$(0)
SendMessage hSci, %SCI_AnnotationSetText, iLine, StrPTR(txt)
'The style may be set to any existing style with this message.
SendMessage hSci, %SCI_AnnotationSetStyle, iLine, %Style_Default
'And finally, the visual properties
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Boxed, 0
'You can clear annoatation from all lines or just an individual line
SendMessage hSci, %SCI_AnnotationSetText, iLine, 0 'clear only iLine
SendMessage hSci, %SCI_AnnotationClearAll, 0, 0 'clear all annotations
'These visual aspects of annotations can be set
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Hidden, 0 'hide
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Standard, 0 'left justified
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Hidden, 0 'indented, boxed
'Finally, it may be useful to know how many lines an annotation requires
iLines = SendMessage hSci, %SCI_AnnotationGetLines, iLine, 0 'line count of an annoation
'I've found it necessary to redraw the control whenever annotations
'are changed, so use this PowerBASIC code after sending annotation
'messages to Scintilla
Control Redraw hDlg, %ID_Sci
'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, "Show Annotation", 10,10,90,20, %WS_Child Or %WS_Visible
Control Add Button, hDlg, %ID_BtnB, "Hide Annotation", 10,40,90,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 txt As String
txt = "annotation" + Chr$(10) + "example" + Chr$(0)
SendMessage hSci, %SCI_AnnotationSetText, 1, StrPTR(txt)
SendMessage hSci, %SCI_AnnotationSetStyle, 1, %Style_Default
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Boxed, 0
Control Redraw hDlg, %ID_Sci
End Sub
Sub TestB
SendMessage hSci, %SCI_AnnotationSetVisible, %Annotation_Hidden, 0
Control Redraw hDlg, %ID_Sci
End Sub
'gbs_00661
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm