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

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

By , 16 Aug 2008
 

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

License

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

About the Author

darkoman
Software Developer (Senior) Elektromehanika d.o.o. Nis
Serbia Serbia
Member
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.

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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionwhere is the the code completion feature?memberarcgisrs15 Aug '12 - 16:14 
I'm very sorry.
I do not find the code completion feature in this software.
But author say it has this feather, and in the picture also show this feature.
Who can tell me where is the code completion feature.
AnswerRe: where is the the code completion feature?memberdarkoman15 Aug '12 - 23:15 
Hello,
 
please use CTRL+SPACE keyboard combination.
 

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

BugColorized bugmemberdon-and-home16 Aug '11 - 21:07 
Good project, but I have little comment:
 
It is incorrect colorized comments like this:
 
something...
 
/****
*****/ <- it don't stop coloring here. It waits " */" with space before
 
something...
 
/* words */
<- only stop when " */"
 
something...
 

 
Same picture with text coloring.
QuestionWhat is the simplest way to use this control or class in a CRichEditView SDI ?memberMichael B Pliam23 Jun '11 - 7:48 
I would like to use your code in a CRichEditView SDI. Have you or anyone tried to do this? What is the simplest way to go at it ? Thanks for sharing the nice work.
Generalsome modify ideasmemberfanrongqi29 May '11 - 21:23 
1.I think the control can match keyword and the variable which declare by user is more useful.
Add the function to implement match variable is not very difficult.
2.I want to match word when I input any char ,not press the Ctrl. I modify the function AddText()but It works bad.
GeneralGood job [modified]memberhfut_short27 Mar '10 - 20:10 
It is the ctrl that I need now,thanks of your hard work;
And I had fixed some bugs(such as Unicode word 、Leak),If someone need, contact me at haha8681@126.com
 
Best wishes
modified on Sunday, March 28, 2010 3:46 AM

QuestionUpdates?mentorHans Dietrich21 Jan '10 - 0:24 
Are there any updates for this?
 
Best wishes,
Hans
 

[Hans Dietrich Software]

GeneralThanks [modified]memberramesh_sit9222 Jul '09 - 21:45 
Nice Appplication Smile | :)
but better if u provide hyperlink detection..
it looks gr8
 
modified on Thursday, July 23, 2009 4:18 AM

Generalvery goodmemberjsliuyun21 Jul '09 - 1:33 
very good
GeneralSome bugs...memberdr.justice18 Mar '09 - 8:36 
There's a leak in DeleteText, where a temporary buffer is allocated but never deleted. There's also problems with the caret behaving improperly when the window doesn't have focus, disabling the window doesn't gray it, calling SetCurrentChar updates the current line, not the current char (I guess a copy/paste error), in AddText() a temporary buffer is allocated but delete'd, but it should be delete[]'d.
 
And this is just the stuff I found while not even really paying attention...
 
Overall works OK, but it definitely needs a good pass of QA.
GeneralRe: Some bugs...memberhfut_short27 Mar '10 - 4:29 
thanks for your carefully work.
GeneralNice workmemberkartsam11 Nov '08 - 18:08 
The editor is nice. Good work.
 
One thing is I am not able to change the font. The text position is not proper for other fonts.
 
sdgsdfg

GeneralJust in timememberHumayun Kabir Hemoo25 Oct '08 - 3:16 
Just when I was looking for such a thing. Thanks
 
Md. Humayuon Kabir Hemoo

General[Message Removed]memberstonber4 Oct '08 - 1:42 
Spam message removed
Generalhelp me!memberMember 50919225 Sep '08 - 4:56 
why my project not show the list?
Generalbug report!member_Nal_Ra8 Sep '08 - 20:18 
i typed some unicode characters in the UtraPad,but the cursor appear in the middle of the characters not the end.i copied the unicode characters in the UtraPad and pasted.some messy code show! and UtraPad crashed when i typed or copied unicode characters continuously.
 

best regards
 
_Nal_Ra
GeneralRe: bug report!memberhfut_short27 Mar '10 - 4:33 
I got the same problem when I type some chinese ,I think it's better derive the ctrl from CRichEditctrl.
 
best regards
Generalgood stuff! [modified]member_Nal_Ra8 Sep '08 - 19:15 
great article and great code!
 
some suggestions:
1. draw margins and show linenumber on the left.
2. highlight full line where the cursor stay.
3. select full line when doubleclick the end of one line(now it just work when doubleclick in the middle of one line).
 

best Regards
 
_Nal_Ra
 
modified on Tuesday, September 9, 2008 1:32 AM

GeneralExcellent!mvpHans Dietrich2 Sep '08 - 5:12 
I'm going to use this one!
 
Some suggestions:
  1. Add Save/Load Profile functions - maybe using xml files. This would save all the colors, constants, etc.
  2. I'm going to need a read-only mode - is this hard to implement?
  3. Include a release-mode exe in the download.
Thanks for sharing!
 

GeneralRe: Excellent!memberdarkoman2 Sep '08 - 5:47 
Hello,
 

thank you for your comments.
 
1. Done (will added by the end of this week).
2. Not hard at all (will added by the end of this week).
3. OK (will added by the end of this week).
 

 
Best regards,
Darkoman
 
"Avaritia est radix omnium malorum..."

GeneralUseful resourcesmemberykachanov18 Aug '08 - 17:13 
Rich text editor Neatpad - the most interesting tutorial which I have ever seen.
GeneralRe: Useful resourcesmemberdarkoman18 Aug '08 - 20:52 
Hello,
 
you are right...
These tutorials are just great !!!
 

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 16 Aug 2008
Article Copyright 2008 by darkoman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid