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

CUltraPadWnd - A Simple Syntax Coloring Control Based on the MFC CWnd Class

Rate me:
Please Sign up or sign in to vote.
4.88/5 (30 votes)
16 Aug 2008CPOL3 min read 84.7K   3K   92   23
An article on a simple syntax coloring control
Image 1

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:

C++
//
// 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:

C++
// 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:

C++
// 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:

C++
// 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
He has a master degree in Computer Science at Faculty of Electronics in Nis (Serbia), and works as a C++/C# application developer for Windows platforms since 2001. He likes traveling, reading and meeting new people and cultures.

Comments and Discussions

 
GeneralVery good resource Pin
吴克亮20-Jan-21 14:40
吴克亮20-Jan-21 14:40 
Questionmemory leak and crash Pin
Narendra Kholiya13-Apr-17 4:23
Narendra Kholiya13-Apr-17 4:23 
Questionwhere is the the code completion feature? Pin
arcgisrs15-Aug-12 16:14
arcgisrs15-Aug-12 16:14 
AnswerRe: where is the the code completion feature? Pin
darkoman15-Aug-12 23:15
darkoman15-Aug-12 23:15 
Hello,

please use CTRL+SPACE keyboard combination.


Regards,
Darkoman
"Avaritia est radix omnium malorum..."

BugColorized bug Pin
don-and-home16-Aug-11 21:07
don-and-home16-Aug-11 21:07 
QuestionWhat is the simplest way to use this control or class in a CRichEditView SDI ? Pin
Michael B Pliam23-Jun-11 7:48
Michael B Pliam23-Jun-11 7:48 
Generalsome modify ideas Pin
fanrongqi29-May-11 21:23
fanrongqi29-May-11 21:23 
GeneralGood job [modified] Pin
hfut_short27-Mar-10 20:10
hfut_short27-Mar-10 20:10 
QuestionUpdates? Pin
Hans Dietrich21-Jan-10 0:24
mentorHans Dietrich21-Jan-10 0:24 
GeneralThanks [modified] Pin
ramesh_sit9222-Jul-09 21:45
ramesh_sit9222-Jul-09 21:45 
Generalvery good Pin
jsliuyun21-Jul-09 1:33
jsliuyun21-Jul-09 1:33 
GeneralSome bugs... Pin
dr.justice18-Mar-09 8:36
dr.justice18-Mar-09 8:36 
GeneralRe: Some bugs... Pin
hfut_short27-Mar-10 4:29
hfut_short27-Mar-10 4:29 
GeneralNice work Pin
Karthik TL11-Nov-08 18:08
professionalKarthik TL11-Nov-08 18:08 
GeneralJust in time Pin
Humayun Kabir Hemoo25-Oct-08 3:16
Humayun Kabir Hemoo25-Oct-08 3:16 
Generalhelp me! Pin
Member 50919225-Sep-08 4:56
Member 50919225-Sep-08 4:56 
Generalbug report! Pin
_Nal_Ra8-Sep-08 20:18
_Nal_Ra8-Sep-08 20:18 
GeneralRe: bug report! Pin
hfut_short27-Mar-10 4:33
hfut_short27-Mar-10 4:33 
Generalgood stuff! [modified] Pin
_Nal_Ra8-Sep-08 19:15
_Nal_Ra8-Sep-08 19:15 
GeneralExcellent! Pin
Hans Dietrich2-Sep-08 5:12
mentorHans Dietrich2-Sep-08 5:12 
GeneralRe: Excellent! Pin
darkoman2-Sep-08 5:47
darkoman2-Sep-08 5:47 
GeneralUseful resources Pin
ykachanov18-Aug-08 17:13
ykachanov18-Aug-08 17:13 
GeneralRe: Useful resources PinPopular
darkoman18-Aug-08 20:52
darkoman18-Aug-08 20:52 

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.