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 "UltraPadWnd.h"
CUltraPadWnd m_UltraPadWnd;
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:
BOOL Load(LPTSTR lpszFilePath);
BOOL Save(LPTSTR lpszFilePath);
void AddText(LPTSTR lpszText, int iLen);
void DeleteText(int iStartLine, int iStartChar, int iEndLine, int iEndChar);
LPTSTR GetTextBuffer();
int GetBufferLen();
void GetTextFromLine(int iLineIndex, LPTSTR lpszText, int iLen);
void SetCurrentLine(int index);
int GetCurrentLine();
void SetCurrentChar(int index);
int GetCurrentChar();
void SetTABs(int iTABs);
int GetTABs();
void EnsureVisible();
void SetSelection(int iStartLine, int iStartChar, int iEndLine, int iEndChar);
void GetSelection(int& iStartLine, int& iStartChar, int& iEndLine, int& iEndChar);
int GetNumberLines();
void SetBackgroundColor(COLORREF cBgColor);
COLORREF GetBackgroundColor();
There are some specific public
methods, connected with the syntax coloring and code completion features. Here are all of them with comments:
void SetKeywordColor(COLORREF cKeywordColor);
COLORREF GetKeywordColor();
void SetKeywords(LPTSTR lpszKeywords, int iLen, BOOL bCaseSensitive);
void SetConstantColor(COLORREF cConstantColor);
COLORREF GetConstantColor();
void SetConstants(LPTSTR lpszConstants, int iLen, BOOL bCaseSensitive);
void SetCommentColor(COLORREF cCommentColor);
COLORREF GetCommentColor();
void SetTextColor(COLORREF cTextColor);
COLORREF GetTextColor();
void SetSyntaxColoring(SYNTAX_COLORING_TYPE syntaxColoring);
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:
void SetLineDrawProc(LPLINEDRAWPROC lpfnLineDrawProc);
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