Click here to Skip to main content
Click here to Skip to main content

Colorizing edit control

By , 4 Sep 2002
 

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

About the Author

Chris Losinger
Software Developer
United States United States
Member
Chris Losinger is the president of Smaller Animals Software, Inc..

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberlecaicp10 Mar '13 - 21:47 
GeneralMy vote of 3memberbuyong26 Sep '11 - 14:51 
GeneralDoModalmemberanby12329 Dec '08 - 17:50 
GeneralSelection "Colorizing edit control"memberMember 287643913 Dec '07 - 7:25 
GeneralShow cursormemberr.teox24 Jul '07 - 22:11 
GeneralOn VC8.0 [modified]memberJUNO_NICE25 Jun '07 - 21:55 
GeneralRe: On VC8.0memberTk43127 Jun '07 - 11:41 
GeneralProbably bugmemberr.teox2 Apr '07 - 23:12 
GeneralFind and Replace functionsmemberr.teox14 Mar '07 - 2:13 
GeneralRe: Find and Replace functionsmemberChris Losinger14 Mar '07 - 2:45 
QuestionProblem with 'ò'memberr.teox8 Mar '07 - 1:20 
AnswerRe: Problem with 'ò'memberChris Losinger8 Mar '07 - 1:25 
GeneralExtra CR/LFmemberHarleyDude5 Oct '05 - 9:19 
GeneralRe: Extra CR/LFmemberHarleyDude5 Oct '05 - 9:33 
QuestionWhat about long filesmemberFlower3035 Dec '04 - 6:49 
AnswerRe: What about long filesmemberChris Losinger5 Dec '04 - 7:09 
GeneralHere is what I done!memberFlower3035 Dec '04 - 7:55 
GeneralRe: What about long filesmemberRocky Racoon14 Mar '05 - 22:50 
GeneralMultiLine ViewsussAzzah21 Apr '04 - 19:59 
GeneralRe: MultiLine ViewmemberChris Losinger22 Apr '04 - 1:23 
sorry, no. this editor does not do line wrapping.
 
Cleek | Losinger Designs | ClickPic | ThumbNailer
Questioncan not support chinese char!!memberwhelk14 Apr '04 - 2:43 
AnswerRe: can not support chinese char!!memberChris Losinger14 Apr '04 - 3:08 
Generalwhat will i do in your source code? can you tell me,thanks a!memberwhelk15 Apr '04 - 15:55 
GeneralRe: what will i do in your source code? can you tell me,thanks a!memberChris Losinger15 Apr '04 - 15:58 
GeneralRe: can not support chinese char!!susscocu_chen21 Apr '04 - 11:29 
GeneralRe: can not support chinese char!!memberponey11 May '04 - 21:46 
Generalanother thing ...sussTomKat20 Nov '03 - 23:50 
Generalmemory leakmemberzivavi23 Oct '03 - 5:54 
GeneralSuggestion : Word Wrapmemberdvbchallenge29 Jan '03 - 13:28 
GeneralRe: Suggestion : Word WrapmemberChris Losinger10 Mar '03 - 15:35 
GeneralPort to WTLmemberNorm Almond13 Nov '02 - 21:34 
GeneralRe: Port to WTLmemberChris Losinger14 Nov '02 - 5:35 
GeneralRe: Port to WTLmemberfreehawk28 Jan '04 - 15:07 
Generalkeyword listmemberDarren Schroeder8 Sep '02 - 7:25 
GeneralRe: keyword listmemberJuan Carlos Cobas20 Feb '03 - 23:53 
GeneralRe: keyword listmemberDarren Schroeder21 Feb '03 - 1:47 
Questionvc7?memberDarren Schroeder7 Sep '02 - 16:54 
AnswerRe: vc7?memberChris Losinger7 Sep '02 - 19:43 
GeneralRe: vc7?memberDarren Schroeder8 Sep '02 - 3:21 
GeneralRe: vc7?memberhalka23 Dec '04 - 21:44 
GeneralSuggestionmemberDomenic [Geekn]5 Sep '02 - 4:28 
GeneralRe: SuggestionmemberChris Losinger5 Sep '02 - 4:31 
QuestionProblem using VC++ 5.0???sussTim Gard30 Aug '02 - 7:28 
AnswerRe: Problem using VC++ 5.0???memberChris Losinger30 Aug '02 - 7:44 
AnswerRe: Problem using VC++ 5.0???memberMichel Wassink5 Sep '02 - 7:49 
GeneralLocalization problem ...membernmeuret1 Apr '02 - 21:29 
GeneralBy any chance...memberDarren Schroeder18 Jan '02 - 8:13 
GeneralRe: By any chance...memberChris Losinger18 Jan '02 - 8:13 
QuestionSuggestion: ActiveX control?memberAnonymously11 Jan '02 - 0:49 
AnswerRe: Suggestion: ActiveX control?sussOlivier_c22 Jan '03 - 6:31 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 5 Sep 2002
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid