Needing a quick and dirty colorizing edit control, I went to all of my favorite MFC source sites.
Unfortunately, most of what I found was based on
CView - not what I needed. I was sad.
Then, I found Randy More's "Syntax Coloring Text Edit Window Class" on
CodeGuru. This was a start. Sadly,
it had serious problems. So, I sat down and started hacking. Three days later, after replacing at least
90% of the original code, I decided I had something good enough to use. So, here it is.
What it does
- Text editing. That's right, you can use it to edit text.
- Syntax highlighting. Well, more specifically, it can color the text in any way you tell it to.
It's up to you (the programmer) to tell it how to color things. It has built-in "keyword" support,
but anything more complex than simple word matching is something you'll have to do yourself (which
is no big surprise, since there's no way I could possibly know all the different syntaxes people
might want to use this with). The sample code shows how to do some simple things aside from just the
- Full undo/redo support. Just like the VC++ editor, you can undo all the way back to the start and
redo all undo's from the last edit.
- Auto scroll support.
- Flicker-free scrolling
- Variable character and line spacing
- Full select, copy, cut, paste support, with keyboard accelerators and a context menu.
- Mouse wheel support. Vertical only.
What it doesn't do
- Variable-width font support. This only works with fixed width fonts. I needed a code editor,
not a word processor. Adding variable width font support would be a huge undertaking and I'm not interested
in doing it.
- It's not
CEdit. Nope, it's a different control altogether. It's not derived from
it's not a drop-in replacement. It can do many of the same things, but it does them differently. For example,
this is a line-based editor; so to set the selection range, you need to specify lines, not just characters
- Boring scroll bars. Scroll bar thumb sizing could use some work.
- Unicode. I don't need it.
- There might be bugs! Yep, you heard it here first - there might be a bug or two in here. Feel free to let me
know if you find anything. Even better, feel free to fix it and tell me what you did. I'll keep the source updated.
What does the sample do?
- Load and save a text file.
- Keywords. The sample has a small keyword file ("while", "if", "else", and "for"). These words will be highlighted automatically.
- The sample colorizer shows off some special syntax handling for the following symbols:
|# text||any line that starts with "#" is a comment
|@+||must be the only text on the line||blue, but red if there is any other text on the line|
|@-||must be the only text on the line||blue, but red if there is any other text on the line|
|@:text||must be at the start of the line||red|
How to use it in your app
- Add these files to your project:
- ColorEditWnd.cpp, .h
- Colorizer.cpp, .h
- ScriptText.cpp, .h
- UndoFrame.cpp, .h
IDR_COLOREDITCTX context menu from the sample into your app.
IDR_EDIT_ACCELS accelerators from the sample into your app.
ColorEditWnd* member to your dialog class.
Add a new resource ID for the control. Go to the resource editor, right-click on your project's resource file.
Choose "Resource Symbols". Add a new symbol
Initialize the object in your dialog's
m_pColorWnd = new ColorEditWnd(
this, //parent window
frameRect, //initial size and position
ID_EDIT_WND, //id value
iTabSize, //size of a tab
iFontSize, //font size * 10 (I.E. 100 = 10)
csFontName); //the font name
Delete the object in the dialog destructor
Well, almost... You also need to write a custom
Colorizer object to handle your specific syntax
coloring needs. :) In the sample this is demonstrated by the
SampleColorizer class. It does the syntax
coloring as described above, and calls the base class (
CColorizer) to handle keyword coloring.
Here are a few of the interesting member functions:
// clear and set text to pInText
void LoadText(const char *pInText);
// get a single string copy of the contents
void UnloadText(CString &pText) const;
// number of text lines in the buffer
// set selection from char1, line1 to char2, line2
void SetSelection(int charPos1, int line1, int charPos2, int line2);
// replace the current selection with this text
void ReplaceSelText(const char *pText);
// en/disable keyboard accelerators
void UseKeyboardAccelerators(bool b);
Oct 01, 01 - First release
Oct 04, 01 - Many fixes, improvements and changes: better scrolling, better selection,
EN_* events to the
parent, better caret handling, ability to dynamically change colorizers and hilight colors, corrected the comments to give
proper credit to Randy More and not Keith Rule, and much much more.
Oct 10, 01 - Performance speedup in
LoadText, tweak to
Oct 15, 01 - Select word on double click. Minor fix to remove selected (cursor position at end). More utility functions
Oct 22, 01 - Shift-select range checking fix for end-of-text situations.
Oct 30, 01 - Proportional scrollbar sizes, using std::set for keyword lookup,
Create(...) method, etc..
Jan 6, 02 - Removed some flicker.
Jan 9, 02 - Added a function to force a re-colorize of the text
Jan 11, 02 - fixed pasting problem. Removed some more flicker.
Mar 13, 02 - Using MemDC, thanks to Robert Bouwens.
Sep 5, 02 - Updated source code