TScintilla
Fonte: source/classes/scintila.prg
Hierarchy: TScintilla → TControl
TScintilla wraps the Scintilla source code editing component (scintilla.org) for use within FiveWin applications. It provides a full-featured code editor with syntax highlighting, code folding, line numbering, bookmarks, search/replace, undo/redo, and support for multiple lexers including Harbour/xHarbour syntax.
SCINTILLA Command
@ nRow, nCol SCINTILLA oSci SIZE nW, nH OF oWnd FONT oFont LEXER cLexer
Key DATA Members
| DATA | Type | Default | Description |
|---|---|---|---|
cFileName | Character | Currently loaded file path | |
cLexer | Character | 0 | Lexer identifier for syntax highlighting |
lMargLin | Logical | .F. | Show line number margin |
lFolding | Logical | .F. | Enable code folding margin |
lMarking | Logical | .F. | Show bookmark/symbol margin |
lModified | Logical | .F. | Whether the document has unsaved changes |
bModified | Block | Codeblock evaluated when the document modification state changes | |
cCKeyw1..cCKeyw5 | Array | Syntax color arrays: each is { nForeColor, nBackColor, nCase } for keyword levels 1–5 |
Methods
| Method | Description |
|---|---|
New( nRow, nCol, nWidth, nHeight, oWnd, nClrT, nClrP, cLex, cDll, bSetup ) | Constructor. Loads the SciLexer DLL and creates the Scintilla window. |
OpenFile( cFileName ) | Load a file into the editor. Detects UTF-8 BOM automatically. |
Save() | Save the current content to disk. Prompts for filename if none is set. |
Open() | Show a file-open dialog and load the selected file. |
SetText( cText ) | Replace the entire editor content with a string. |
GetText() | Return the entire editor content as a string. |
GetLine( nLine ) | Return the text of a specific 1-based line number. |
GetLineCount() | Return the total number of lines in the document. |
FindText( cText, lForward ) | Search for text forward or backward from the current position. |
SetFont( oFont ) | Change the editor font at runtime. |
Undo() | Undo the last editing action. |
Redo() | Redo a previously undone action. |
Copy() | Copy the current selection to the clipboard. |
Cut() | Cut the current selection to the clipboard. |
Paste() | Paste clipboard content at the cursor position. |
SelectAll() | Select the entire document content. |
Print( oFont ) | Print the document using TReport with optional line numbers. |
MarkerAdd( nLine, nMarker ) | Add a marker (bookmark) at the specified line. |
FoldAllContract() | Collapse all foldable code regions. |
FoldAllExpand() | Expand all folded code regions. |
SetColor( nClrText, nClrPane ) | Set the default text and background colors. |
Example: Code Editor
#include "FiveWin.ch"
function Main()
local oWnd, oSci, oFont
DEFINE FONT oFont NAME "Consolas" SIZE 0, -12
DEFINE WINDOW oWnd TITLE "Source Editor" ;
SIZE 800, 600
@ 0, 0 SCINTILLA oSci SIZE 800, 570 OF oWnd ;
FONT oFont LEXER ""
oSci:lMargLin := .T.
oSci:lFolding := .T.
oSci:lMarking := .T.
oSci:SetUp()
// Toolbar buttons
@ 575, 10 BUTTON "&Open" SIZE 50, 20 ;
ACTION oSci:Open()
@ 575, 65 BUTTON "&Save" SIZE 50, 20 ;
ACTION oSci:Save()
@ 575, 120 BUTTON "&Find" SIZE 50, 20 ;
ACTION oSci:DlgFindText()
@ 575, 175 BUTTON "&Undo" SIZE 50, 20 ;
ACTION oSci:Undo()
@ 575, 230 BUTTON "&Redo" SIZE 50, 20 ;
ACTION oSci:Redo()
@ 575, 620 BUTTON "E&xit" SIZE 50, 20 ;
ACTION oWnd:End()
ACTIVATE WINDOW oWnd ;
VALID ( oSci:End(), oFont:End(), .T. )
return nil
Lexer Configuration
TScintilla supports syntax highlighting via configurable keyword color sets and token-specific color arrays:
| DATA | Type | Description |
|---|---|---|
cCKeyw1..cCKeyw5 | Array | Keyword color sets: { nForeColor, nBackColor, nCase } for keyword levels 1–5 |
cCString | Array | String literal color: { nForeColor, nBackColor, nCase } |
cCNumber | Array | Numeric literal color: { nForeColor, nBackColor, nCase } |
cCOperator | Array | Operator color: { nForeColor, nBackColor, nCase } |
cCComment | Array | Comment color (supports multi-line): { nForeColor, nBackColor, nCase } |
Each color array contains three elements:
- nForeColor — Foreground (text) color as an RGB numeric value.
- nBackColor — Background color as an RGB numeric value.
- nCase — Case mode:
SC_CASE_MIXED,SC_CASE_UPPER, orSC_CASE_LOWER.
Default colors for Harbour/xHarbour syntax are defined as class data members:
DATA cCComment INIT { CLR_GRAY, CLR_WHITE, SC_CASE_MIXED }
DATA cCString INIT { CLR_HRED, CLR_WHITE, SC_CASE_MIXED }
DATA cCNumber INIT { CLR_HBLUE, CLR_WHITE, SC_CASE_MIXED }
DATA cCOperator INIT { CLR_HBLACK, CLR_WHITE, SC_CASE_MIXED }
DATA cCKeyw1 INIT { METRO_ORANGE,CLR_WHITE, SC_CASE_MIXED }
DATA cCKeyw2 INIT { CLR_HGREEN, CLR_WHITE, SC_CASE_MIXED }
DATA cCKeyw3 INIT { CLR_HPURPLE, CLR_WHITE, SC_CASE_MIXED }
DATA cCKeyw4 INIT { CLR_HCYAN, CLR_WHITE, SC_CASE_MIXED }
DATA cCKeyw5 INIT { CLR_HRED, CLR_WHITE, SC_CASE_MIXED }
File Encoding Modes
TScintilla supports multiple file encoding modes for reading and saving documents
in different character encodings. These are controlled by the nModeSave
and nModeLoad data members along with the lBom flag.
| Mode | Constant | Description |
|---|---|---|
| 0 | nModeLoad := 0 | Normal (ANSI/CP1252) encoding |
| 1 | nModeLoad := 1 / nModeSave := 1 | UTF-8 with BOM |
| 2 | nModeLoad := 2 / nModeSave := 2 | UTF-8 without BOM |
| 3 | nModeLoad := 3 / nModeSave := 3 | UTF-16 Little Endian |
nModeLoadcontrols how the file is interpreted when opened. Mode 0 reads as ANSI; modes 1–3 decode the appropriate Unicode format.nModeSavecontrols how the document is written back to disk.lBom(logical) controls whether a Byte Order Mark is written at the beginning of the file on save. Relevant for UTF-8 and UTF-16 modes.- When
OpenFile()loads a document, UTF-8 BOM is detected automatically regardless ofnModeLoad.
Editor Control Comparison
| Control | Source File | Features |
|---|---|---|
TEdit | edit.prg | Basic edit, multi-line memo mode, no RTF support |
TGet | tget.prg | Single-line input, picture clauses, input validation |
TMultiGet | mget.prg | Multi-line text, no formatting, basic editing |
TRichEdit | trichedi.prg | RTF 2.0 (riched20.dll), basic font/color formatting |
TRichEdit5 | triched5.prg | RTF 5.0 (msftedit.dll), tables, images, PDF export |
TTxtEdit | ttxtedit.prg | Source code display, syntax highlighting, line numbers |
TScintilla | scintila.prg | Full IDE-class editor, code folding, lexers, bookmarks, search/replace, multi-encoding |
Notes
- TScintilla requires the Scintilla DLL (
SciLexer.dllfor 32-bit orSciLex64.dllfor 64-bit) at runtime. The DLL is loaded dynamically viaLoadLibrary. - Harbour/xHarbour syntax highlighting is activated by passing
"Harbour"or the corresponding lexer number as thecLexparameter. - Use
cCKeyw1throughcCKeyw5to customize syntax colors. Each is an array of{ nForeColor, nBackColor, nCaseMode }. - The
lModifiedflag andbModifiedcodeblock let you track unsaved changes, e.g., to update a window title with a modified indicator. - The class provides built-in dialogs for find/replace (
DlgFindText(),DlgReplace()), go-to-line (DlgGoToLine()), and character insertion (DlgInsChar()). - Margins are configured via
nMargLines,nMargSymbol,nMargFold, andnMargText. CallSetUp()after changing margin settings.