Syntax coloring Editor

I wrote really simple color syntax editor based on RICHEDIT control. I spent only one day on this work (ok one and half), so it's far from ideal, but the result is suitable for editing small scripts in VBScript or JScript (this is a part of my work on a script host application)..

How to use this control:

  1. Insert a RICHEDIT control into a dialog resource. Add a member variable to appropriate the CDialog or CFormView derived class and subclass the control in the OnInitDialog function. Or create the CTWScriptEdit control then you need it.
  2. To minimize any flickering set the style of the parent window (usually the dialog) to WS_CLIPCHILDREN.
  3. Call the Initialize() method of the CTWScriptEdit. (This sets default attributes of the text).

How to set parameters of the language:

  • void SetCaseSensitive(BOOL bSensitive);
    Is language case sensitive?
  • void SetChangeCase(BOOL bChange);
    Force case for keywords?
  • void SetSLComment(TCHAR chComment, TCHAR chComment2 = 0);
    Set character(s) for comment ('\'' for VBScsipt, '\\', '\\' for JScript)
  • void SetSLComment(LPCTSTR lpszComment);
    Set keyword for comment ("Rem" for VBScript) must be in the keyword list too
  • void SetStringQuotes(LPCTSTR lpszStrQ);
    Determines string quotation character(s) ("\"")
  • void AddKeywords(LPCTSTR lpszKwd);
    Adds keywords
  • void AddConstants(LPCTSTR lpszKwd);
    Adds constants
  • void SetKeywordColor(COLORREF clr, BOOL bBold);
    Sets color and bold attribute for keywords
  • void SetConstantColor(COLORREF clr, BOOL bBold);
    Sets color and bold attribute for constants
  • void SetCommentColor(COLORREF clr, BOOL bBold);
    Sets color and bold attribute for comment
  • void SetNumberColor(COLORREF clr, BOOL bBold);
    Sets color and bold attribute for numbers
  • void SetStringColor(COLORREF clr, BOOL bBold);
    Sets color and bold attribute for strings

The attached sample project is a simple dialog based application. All described steps in the source code are outlined by TWSCRIPTEDIT -> <- TWSCRIPTEDIT comment pair.

Comments on the Source Code:

The Interesting code sits in the CTWScriptEdit control (TWScriptEdit.h and TWScriptEdit.cpp). This control is derived from CRichEditCtrl. The whole miracle occurs in the EN_PROTECTED and EN_CHANGE notification handlers. All text in the control has the CFE_PROTECTED flag, so before any change the OnProtected handler is called. After the change, the OnChange handler is called. This enables us to determine the affected lines that need reformatting. Real formatting occurs in FormatTextRange() which parses the range of text and changes necessary attributes by calling helper functions. The control correctly handles WM_SETTEXT and EM_RELACELSEL, so it's possible to change text programmatically.

Known Problems:

The Undo buffer is affected during formatting, so the undo function works incorrectly.

Pasting RTF text from clipboard into the control retains font, size and others attributes of pasted text. This can be corrected in SetFormatRange() method - just check and change all attributes.

I am sure that I am better at the C++ language than in English, so my description is short, but I hope that source code answers all your questions.

Added VBScriptEditor project shows how to implement coloring features in CRichEditView derivation.

Download source for the CVBScriptEditDlg and the CVBScriptEdit.



Comments

  • shooooooooooo

    Posted by shehab el shebeny on 10/31/2012 09:43am

    ana shoooooooooo ya nas

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds