Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC
Article

Colorizing edit control

Rate me:
Please Sign up or sign in to vote.
4.92/5 (32 votes)
4 Sep 20024 min read 367.4K   10.4K   89   87
A colorizing text edit control, with full undo/redo, clipboard support, etc.

Sample Image - ColorEdit.gif

Introduction

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 keyword matching.
  • 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 CEdit and 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 like with CEdit.
  • 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:
    SymbolRuleColor
    # textany line that starts with "#" is a comment green
    @+must be the only text on the lineblue, but red if there is any other text on the line
    @-must be the only text on the lineblue, but red if there is any other text on the line
    @:textmust be at the start of the linered

How to use it in your app

  1. Add these files to your project:
    • ColorEditWnd.cpp, .h
    • Colorizer.cpp, .h
    • ScriptText.cpp, .h
    • UndoFrame.cpp, .h

  2. Copy the IDR_COLOREDITCTX context menu from the sample into your app.

  3. Copy the IDR_EDIT_ACCELS accelerators from the sample into your app.

  4. Add a ColorEditWnd* member to your dialog class.

  5. 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 ID_EDIT_WND.

  6. Initialize the object in your dialog's OnInitDialog:

    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
  7. Delete the object in the dialog destructor

That's it!

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.

API

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
int GetLineCount();
// 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);

That's it.

Enjoy responsibly!

History

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 OnVScroll (w/SB_THUMBTRACK)
Oct 15, 01 - Select word on double click. Minor fix to remove selected (cursor position at end). More utility functions (GetLine, SetLine, etc.).
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
QuestionWords are invisible Pin
Member 1071124018-May-14 3:54
Member 1071124018-May-14 3:54 
AnswerRe: Words are invisible Pin
Chris Losinger18-May-14 4:02
professionalChris Losinger18-May-14 4:02 
GeneralRe: Words are invisible Pin
Member 1071124018-May-14 15:14
Member 1071124018-May-14 15:14 
GeneralMy vote of 5 Pin
lecaicp10-Mar-13 21:47
lecaicp10-Mar-13 21:47 
GeneralMy vote of 3 Pin
buyong26-Sep-11 14:51
buyong26-Sep-11 14:51 
GeneralDoModal Pin
anby12329-Dec-08 17:50
anby12329-Dec-08 17:50 
GeneralSelection "Colorizing edit control" Pin
Member 287643913-Dec-07 7:25
Member 287643913-Dec-07 7:25 
GeneralShow cursor Pin
r.teox24-Jul-07 22:11
r.teox24-Jul-07 22:11 
GeneralOn VC8.0 [modified] Pin
JUNO_NICE25-Jun-07 21:55
JUNO_NICE25-Jun-07 21:55 
GeneralRe: On VC8.0 Pin
Tk43127-Jun-07 11:41
Tk43127-Jun-07 11:41 
GeneralProbably bug Pin
r.teox2-Apr-07 23:12
r.teox2-Apr-07 23:12 
GeneralFind and Replace functions Pin
r.teox14-Mar-07 2:13
r.teox14-Mar-07 2:13 
GeneralRe: Find and Replace functions Pin
Chris Losinger14-Mar-07 2:45
professionalChris Losinger14-Mar-07 2:45 
QuestionProblem with 'ò' Pin
r.teox8-Mar-07 1:20
r.teox8-Mar-07 1:20 
AnswerRe: Problem with 'ò' Pin
Chris Losinger8-Mar-07 1:25
professionalChris Losinger8-Mar-07 1:25 
GeneralExtra CR/LF Pin
Judge Gazza5-Oct-05 9:19
Judge Gazza5-Oct-05 9:19 
GeneralRe: Extra CR/LF Pin
Judge Gazza5-Oct-05 9:33
Judge Gazza5-Oct-05 9:33 
QuestionWhat about long files Pin
Flower3035-Dec-04 6:49
Flower3035-Dec-04 6:49 
AnswerRe: What about long files Pin
Chris Losinger5-Dec-04 7:09
professionalChris Losinger5-Dec-04 7:09 
GeneralHere is what I done! Pin
Flower3035-Dec-04 7:55
Flower3035-Dec-04 7:55 
GeneralRe: What about long files Pin
D. de Kerf14-Mar-05 22:50
D. de Kerf14-Mar-05 22:50 
GeneralMultiLine View Pin
AbouTony21-Apr-04 19:59
AbouTony21-Apr-04 19:59 
GeneralRe: MultiLine View Pin
Chris Losinger22-Apr-04 1:23
professionalChris Losinger22-Apr-04 1:23 
Questioncan not support chinese char!! Pin
whelk14-Apr-04 2:43
whelk14-Apr-04 2:43 
AnswerRe: can not support chinese char!! Pin
Chris Losinger14-Apr-04 3:08
professionalChris Losinger14-Apr-04 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.