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

Colorizing edit control

, 4 Sep 2002
Rate this:
Please Sign up or sign in to vote.
A colorizing text edit control, with full undo/redo, clipboard support, etc.
<!-- Download Links --> <!-- Article image -->

Sample Image - ColorEdit.gif

<!-- Add the rest of your HTML here -->

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. Smile | :) 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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 3 Pinmemberbuyong26-Sep-11 14:51 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 5 Sep 2002
Article Copyright 2001 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid