TRichEdit / TScintilla

Rich text editing with RTF formatting, and source code editing with syntax highlighting

graph TD TControl --> TRichEdit TControl --> TScintilla TRichEdit --> RTF["RTF Formatting
Bold, Italic, Colors"] TRichEdit --> OLE["OLE Objects
Embedded Pictures"] TRichEdit --> FIND["Find / Replace"] TRichEdit --> PRINT["Print / Preview / PDF"] TScintilla --> SYNTAX["Syntax Highlighting
Harbour, C, PRG"] TScintilla --> FOLD["Code Folding"] TScintilla --> MARGIN["Margins
Line Numbers, Bookmarks"] TScintilla --> AUTO["Autocomplete
Call Tips"] style TRichEdit fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3 style TScintilla fill:#1c2129,stroke:#58a6ff,stroke-width:2px,color:#e6edf3

TRichEdit

Source: source/classes/trichedi.prg  |  Parent: TControl

TRichEdit wraps the Windows RichEdit20A control, providing a full-featured RTF editor. It supports bold/italic/underline formatting, font and color changes, paragraph alignment, bullet lists, embedded images via OLE, find/replace, zoom, undo/redo, and print/preview/PDF export.

Properties

PropertyTypeDescription
cFileNameStringCurrent RTF file path
nRTFSizeNumericMaximum text size limit
lURLLogicalEnable automatic URL detection and clickable links
lRE30LogicalUsing RichEdit 3.0+ (supports zoom, redo, advanced typography)
lReadOnlyLogicalRead-only mode -- editing disabled
lHighlightLogicalEnable syntax highlighting (for code editing)
aKeywords1ArrayPrimary highlighted keywords
aKeywords2ArraySecondary highlighted keywords
cSeparatorsStringToken separator characters for highlighting
nClrNumberNumericColor for numeric literals
nClrStringNumericColor for string literals
nClrCommentNumericColor for comments

Key Methods

MethodDescription
New( nTop, nLeft, bSetGet, oWnd, nWidth, nHeight, oFont, lPixel, cMsg, lHScroll, lReadOnly, bWhen, bValid, bChanged, lDesign, lHighlight, cFileName, nRTFSize, lNoURL, lNoScroll, lNoBorder )Constructor
ReDefine( nId, bSetGet, oWnd, ... )Redefine from resource
Text Formatting
SetBold( lOnOff )Toggle bold on selected text
SetItalic( lOnOff )Toggle italic
SetUnderline( lOnOff )Toggle underline
SetStrikeOut( lOnOff )Toggle strikethrough
SetFontName( cName )Set font face for selection
SetFontSize( nSize )Set font size for selection
SetTextColor( nRGB )Set text color for selection
SetHighLight( nColor )Set background highlight color for selection
SetBkGndColor( nRGB )Set the entire control's background color
SetAlign( nAlign )Paragraph alignment: PFA_LEFT (1), PFA_RIGHT (2), PFA_CENTER (3), PFA_JUSTIFY (4)
SetBullet( lOnOff )Toggle bullet list
SetNumbering( lOnOff )Toggle numbered list
SetIndent( nLeft, nRight, nFirst )Set paragraph indentation
Clipboard & Editing
Copy()Copy selection to clipboard
Cut()Cut selection to clipboard
Paste()Paste from clipboard
Del()Delete selection
SelectAll()Select all text
UnDo()Undo last action
ReDo()Redo last undone action
CanUndo() / CanRedo()Check if undo/redo is available
CanCopy() / CanCut() / CanPaste()Check clipboard operation availability
Find & Navigation
Find( cFind, lDown, lCase, lWord )Search for text. Returns position or -1.
ReplaceSel( lUnDo, cText )Replace the current selection with new text
GoToLine( nLine )Scroll to and position cursor at a specific line
GetRow() / GetCol()Get current cursor row and column
GetLineCount()Total number of lines
GetLine( nLine )Get text of a specific line
Len()Total character count
File I/O
LoadFromRTFFile( cFileName )Load an RTF file into the control
SaveToRTFFile( cFileName )Save content as RTF file
LoadRTF( cRTF )Load RTF from a string
SaveAsRTF()Return content as an RTF string
cText( cText )Get or set plain text content
OLE & Pictures
InsertPicture( cFile, nSizeX, nSizeY )Embed an image from a file
InsertBitmap( hBitmap, nSizeX, nSizeY )Embed a bitmap by handle
Print & Export
Print( cName )Print the content
Preview( cName )Show print preview
SaveToPDF( cName, cFile, lView )Export to PDF
Zoom
SetZoom( nNumerator, nDenominator )Set zoom level (e.g. SetZoom(2, 1) = 200%)
GetZoom()Get current zoom as {nNumerator, nDenominator}

Command Syntax

@ nTop, nLeft RICHEDIT [ oRTF VAR ] uVar ;
   [ OF oWnd ] ;
   [ SIZE nWidth, nHeight ] ;
   [ FONT oFont ] ;
   [ PIXEL ] ;
   [ MESSAGE cMsg ] ;
   [ HSCROLL ] ;
   [ READONLY | NO MODIFY ] ;
   [ WHEN bWhen ] ;
   [ VALID bValid ] ;
   [ ON CHANGE bChange ] ;
   [ HIGHLIGHT ] ;
   [ FILE cFileName ] ;
   [ RTFSIZE nRTFSize ] ;
   [ NO URL ] ;
   [ NO SCROLL ] ;
   [ NOBORDER ]

REDEFINE RICHEDIT [ oRTF VAR ] uVar ;
   ID nId ;
   [ OF oWnd ] ;
   [ FONT oFont ] ;
   [ READONLY ] ;
   [ HIGHLIGHT ] ;
   [ FILE cFileName ] ;
   [ WHEN bWhen ] ;
   [ VALID bValid ] ;
   [ ON CHANGE bChange ]

Example: RTF Editor

#include "FiveWin.ch"
#include "RichEdit.ch"

FUNCTION RichEditDemo()
   LOCAL oDlg, oRTF, cText := ""

   DEFINE DIALOG oDlg TITLE "Rich Text Editor" SIZE 600, 450

   @ 0.5, 0.5 RICHEDIT oRTF VAR cText OF oDlg ;
      SIZE 280, 180 PIXEL

   // Toolbar buttons for formatting
   @ 8, 0.5 BUTTON "Bold"      OF oDlg SIZE 50, 14 ;
      ACTION oRTF:SetBold( .T. )
   @ 8, 5   BUTTON "Italic"    OF oDlg SIZE 50, 14 ;
      ACTION oRTF:SetItalic( .T. )
   @ 8, 9.5 BUTTON "Underline" OF oDlg SIZE 60, 14 ;
      ACTION oRTF:SetUnderline( .T. )

   // Load and save
   @ 10, 0.5 BUTTON "Open RTF" OF oDlg SIZE 60, 14 ;
      ACTION ( cFile := cGetFile( "*.rtf", "Open RTF" ), ;
               If( !Empty(cFile), oRTF:LoadFromRTFFile( cFile ), ) )

   @ 10, 6  BUTTON "Save RTF" OF oDlg SIZE 60, 14 ;
      ACTION oRTF:SaveToRTFFile( cGetNewFile( "*.rtf", "Save RTF" ) )

   @ 10, 12 BUTTON "Print" OF oDlg SIZE 50, 14 ;
      ACTION oRTF:Print( "RichEdit Document" )

   ACTIVATE DIALOG oDlg CENTERED
RETURN NIL

TScintilla

Source: source/classes/scintila.prg  |  Parent: TControl

TScintilla wraps the Scintilla editor component, providing a professional source code editor with syntax highlighting, code folding, line numbers, bookmarks, brace matching, autocomplete, and multi-document support. It is the editor engine used in the FiveWin IDE.

Properties

PropertyTypeDescription
cFileNameStringCurrent file path being edited
cLexerStringSyntax lexer (e.g. "harbour", "cpp")
cLangStringLanguage identifier
lMargLinLogicalShow line number margin
lFoldingLogicalEnable code folding
lMarkingLogicalEnable bookmark margin
lMessageLogicalShow message/build margin
nMargLinesNumericWidth of line number margin
nMargSymbolNumericWidth of symbol/bookmark margin
nMargFoldNumericWidth of folding margin
nWidthTabNumericTab width in characters
nSpacLinNumericExtra line spacing
lVirtSpaceLogicalAllow cursor in virtual space beyond line end
cFontSStringEditor font face name
nHFontNumericEditor font height
lModifiedLogicalDocument has unsaved changes
bModifiedBlockCallback when modification state changes
lUtf8LogicalDocument is UTF-8 encoded
lSaveBakLogicalCreate .bak backup on save
aKeyArrayCustom key bindings: { { nKey, bAction }, ... }
lAutoWordsLogicalEnable autocomplete word suggestions
lHistClipLogicalClipboard history support

Syntax Highlighting Colors

PropertyDescription
cCCommentBlock comment color { nFore, nBack, nCase }
cCCommentLinLine comment color
cCStringString literal color
cCNumberNumeric literal color
cCOperatorOperator color
cCKeyw1 .. cCKeyw5Keyword group colors (1-5)
cCBracesMatched brace color
cCBraceBadUnmatched brace color
cCIdentifIdentifier color
nColorSelectionText selection foreground color
nColorSelectionBText selection background color

Key Methods

MethodDescription
New( nRow, nCol, nWidth, nHeight, oWnd, nClrT, nClrP, cLex, cDll, bSetup, lLoad, cPathApli )Constructor. Creates the Scintilla editor.
Text Operations
GetText()Get the entire document text
AddText( cText )Append text at the current position
GetLine( nLine )Get text of a specific line
GetLineCount()Total number of lines
GetSelText()Get the selected text
GetCurrentPos()Get cursor position (character offset)
GetCurrentLineNumber()Get current line number
GetModify()Check if document has unsaved changes
Clipboard
Copy() / Cut() / Paste()Standard clipboard operations
CopyLine()Copy the entire current line
CopyRange( nStart, nEnd )Copy a character range
CanUndo() / CanRedo()Check undo/redo availability
Navigation
GoLine( nLine )Go to a specific line (1-based)
GoHome() / GoEol()Go to start/end of line
GoUp() / GoDown()Move cursor up/down one line
GoLeft() / GoRight()Move cursor left/right one character
GoAtEnd()Go to end of document
Find & Replace
FindText( cText, lForward )Search for text forward or backward
FindNext() / FindPrev()Find next/previous occurrence of last search
DlgFindText()Show find dialog
DlgReplace()Show find/replace dialog
DlgGoToLine()Show go-to-line dialog
Code Folding
FoldAllContract()Collapse all foldable regions
FoldAllExpand()Expand all foldable regions
FoldAllToggle()Toggle all folding
File I/O
AddFile( lFin, lHome )Load a file into the editor
CloseF( lEnd )Close the current file
Brace Matching
BraceHighlight( nPos1, nPos2 )Highlight matching braces
BraceBadlight( nPos1, nPos2 )Highlight unmatched braces
BraceMatch( nPos )Find the matching brace position
Call Tips & Autocomplete
CallTipShow( nPosStart, cText )Show a calltip popup
CallTipCancel()Hide the calltip
CallTipActive()Check if a calltip is visible
Custom Keys
AddKey( nKey, bAction )Add a custom key binding

Example: Source Code Editor

#include "FiveWin.ch"

FUNCTION CodeEditor()
   LOCAL oWnd, oSci

   DEFINE WINDOW oWnd TITLE "Code Editor"

   // Create Scintilla editor filling the window
   oSci := TScintilla():New( 0, 0, 800, 600, oWnd, ;
      CLR_BLACK, CLR_WHITE, "harbour" )

   // Load a source file
   oSci:cFileName := "myapp.prg"
   oSci:AddFile()

   // Custom key bindings
   oSci:AddKey( VK_F5, { || CompileAndRun() } )
   oSci:AddKey( VK_F3, { || oSci:FindNext() } )

   ACTIVATE WINDOW oWnd ;
      ON RESIZE ( oSci:Move( 0, 0, nWidth, nHeight, .T. ) )
RETURN NIL

Example: TScintilla with Custom Colors

oSci := TScintilla():New( 0, 0, 800, 600, oWnd, ;
   CLR_BLACK, RGB( 30, 30, 30 ), "harbour" )

// Dark theme colors
oSci:cCComment    := { CLR_GRAY,    RGB(30,30,30), SC_CASE_MIXED }
oSci:cCString     := { CLR_HRED,    RGB(30,30,30), SC_CASE_MIXED }
oSci:cCNumber     := { CLR_RED,     RGB(30,30,30), SC_CASE_MIXED }
oSci:cCKeyw1      := { CLR_CYAN,    RGB(30,30,30), SC_CASE_MIXED }
oSci:cCKeyw2      := { CLR_YELLOW,  RGB(30,30,30), SC_CASE_MIXED }
oSci:nColorSelection  := RGB( 255, 255, 255 )
oSci:nColorSelectionB := RGB( 60, 100, 160 )

TRichEdit vs TScintilla

FeatureTRichEditTScintilla
Primary useRich text / document editingSource code editing
RTF formattingFull supportNo
Syntax highlightingBasic (keywords only)Full lexer-based
Code foldingNoYes
Line numbersManualBuilt-in margin
Brace matchingNoYes
AutocompleteNoYes
Embedded imagesOLE objectsNo
Print / PDFBuilt-inVia report class
Windows dependencyBuilt-in RichEdit DLLRequires SciLexer.dll

Tips

See Also