CUltraPadWnd - A Simple Syntax Coloring Control Based on the MFC CWnd Class
An article on a simple syntax coloring control

Introduction
This article is about the simple CWnd
derived "CEdit-like" control, the CUltraPadWnd
, with the syntax coloring option. This class also has the code completion feature, based on the pre-defined keyword set. There is a similar implementation here on The Code Project here. Also, an example of using Scintilla, an open source editor, can be found here. All these controls are written to provide the user with a possibility to have a cool looking text editor (or a code editor) control inside her/his project.
Background
I was before (and still do) using Microsoft Visual Studio v6.0 IDE when writing my programs. It has a very nice code editor with syntax coloring and code completion features for the C/C++ programming language. It also has many other features that help a programmer to do her/his everyday work. I liked this, so I decided to try to simulate it in my own version of the "general code editor". I was planning to use the CEdit
or CRichEdit
classes but using the generic MFC CWnd
class sounded like a greater challenge. So, the class presented in this article was written completely from the beginning, using the CWnd
class as the starting point. This means that the keyboard and the mouse events had to be handled correctly. I have decided to use the MFC (although it would not be so hard to make it a non-MFC dependent) just for the sake of the C++ programming population that is actually using MFC. As a test project, I have selected the dialog-based application so the user can see how to implement the custom message routing.
Using the Code
It is very simple to use the CUltraPadWnd
class in the custom project. Since it is the CWnd
-derived class, it is created like any other CWnd
object using the Create()
method. See below:
//
// Include the header file
#include "UltraPadWnd.h"
//
// Declare the class member
CUltraPadWnd m_UltraPadWnd;
// Create the control
RECT rect = {100, 100, 300, 300};
m_UltraPadWnd.Create(WS_EX_CLIENTEDGE, WS_CHILD |
WS_CLIPSIBLINGS | WS_VISIBLE, rect, m_pParentWnd, IDC_ULTRAPAD1);
//
There are many public
methods, specific to this control, that can be used. Here are all of them with comments:
// Loads the text from the ANSI, UTF-8 or UNICODE file from disk
BOOL Load(LPTSTR lpszFilePath);
// Saves the containing text to the UNICODE file on disk
BOOL Save(LPTSTR lpszFilePath);
// Adds text at the current caret position
void AddText(LPTSTR lpszText, int iLen);
// Deletes the text in the defined range
void DeleteText(int iStartLine, int iStartChar, int iEndLine, int iEndChar);
// Retrieves the text buffer
LPTSTR GetTextBuffer();
// Gets the total length of the text buffer
int GetBufferLen();
// Retrieves the text from the specified line
void GetTextFromLine(int iLineIndex, LPTSTR lpszText, int iLen);
// Sets the current line position the caret will be in
void SetCurrentLine(int index);
// Gets the current line position the caret is in
int GetCurrentLine();
// Sets the current character position the caret will be at
void SetCurrentChar(int index);
// Gets the current character position the caret is at
int GetCurrentChar();
// Sets the custom TABs size (in characters)
void SetTABs(int iTABs);
// Gets the current TABs size (in characters)
int GetTABs();
// Ensures the caret is visible
void EnsureVisible();
// Sets the selection range
void SetSelection(int iStartLine, int iStartChar, int iEndLine, int iEndChar);
// Gets the current selection range
void GetSelection(int& iStartLine, int& iStartChar, int& iEndLine, int& iEndChar);
// Gets the total number of lines in text
int GetNumberLines();
// Sets the background color of the control
void SetBackgroundColor(COLORREF cBgColor);
// Gets the current background color of the control
COLORREF GetBackgroundColor();
There are some specific public
methods, connected with the syntax coloring and code completion features. Here are all of them with comments:
// Sets the keyword text color
void SetKeywordColor(COLORREF cKeywordColor);
// Gets the current keyword text color
COLORREF GetKeywordColor();
// Sets the keyword set
void SetKeywords(LPTSTR lpszKeywords, int iLen, BOOL bCaseSensitive);
// Sets the constant text color
void SetConstantColor(COLORREF cConstantColor);
// Gets the current constant text color
COLORREF GetConstantColor();
// Sets the constant set
void SetConstants(LPTSTR lpszConstants, int iLen, BOOL bCaseSensitive);
// Sets the comment text color
void SetCommentColor(COLORREF cCommentColor);
// Gets the current comment text color
COLORREF GetCommentColor();
// Sets the "literal text" text color
void SetTextColor(COLORREF cTextColor);
// Gets the current "literal text" text color
COLORREF GetTextColor();
// Sets the syntax coloring mode
void SetSyntaxColoring(SYNTAX_COLORING_TYPE syntaxColoring);
// Gets the current syntax coloring mode
SYNTAX_COLORING_TYPE GetSyntaxColoring();
There are also some specific public
methods, connected with the custom user-defined line drawing. Here are all of them with comments:
// Sets custom, user defined, line-drawing procedure
void SetLineDrawProc(LPLINEDRAWPROC lpfnLineDrawProc);
// Calls the default line drawing procedure
void DefaultLineDrawProc
(LPTSTR lpszText, int iLen, RECT rect, int iSelectionStart, int iSelectionEnd);
There are five types of syntax coloring embedded inside this control (C, C++, HTML, JSCRIPT, SQL) in this release. Here are all of them:
SCT_NONE
SCT_C_ANSI
SCT_C_PLUS_PLUS
SCT_HTML
SCT_JSCRIPT
SCT_SQL
But, the user is free to implement the custom syntax coloring using the methods described. The code completion feature is enabled when the user presses CTRL+SPACE keys on the keyboard.
To Be Done
The following items need to be implemented/upgraded in the later releases:
- Undo/Redo operations
- Text saving procedure (different text encoding like ANSI, UTF-8, etc.)
- Text line drawing procedure (enhanced text drawing)
- Additional syntax coloring and color completion languages/scripts (i.e. VB, Java, C#, etc.)
- Clipboard text copy/paste procedure (enhanced)
- Everything else I (or other people) find useful...
Points of Interest
I had a lot of fun writing this code. I hope that other developers will find it useful for their work too.
History
- 13th August, 2008:
CUltraPadWnd
version 1.0