![]() |
Desktop Development »
Edit Controls »
General
Advanced
Crystal Edit - syntax coloring text editorBy Andrei StcherbatchenkoA set of classes that provide an expandable framework for the syntax coloring text editor. |
VC6, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

CCrystalTextBuffer class is responsible for storing lines, loading and
saving text to a file. To simplify Undo/Redo command implementations, every editing operation
is split into a sequence of 'insert text' and 'delete text' actions. Accordingly,
CView-derived classes are only intended to react only on this primitive operations.
CCrystalTextView class is the framework for text viewing window.
It derives from CView, and it provides text painting code, overridable functions for
syntax highlighting, different kinds of text selections, cursor movements, Find common dialog etc.
However, it's not allowed to perform any changes to the text.
CCrystalTextView-derived views are usually used with CCrystalTextBuffer object. Once such
a view is connected to the CCrystalTextBuffer object, it is capable to track changes made
to the text. (Obviously, any number of views can be connected to a single CCrystalTextBuffer object
at the same time. This is useful, when we need to use the editor in the dynamic splitter as shown
on the figure above).
CCrystalEditView class is derived from CCrystalTextView class. Unlike its ansector,
which is only able to display a text and update the view when it is needed, it has functions
to perform all sorts of editing, including drag-and-drop and Replace dialog. Note, that the view
does not make the changes in the text directly, instead, it transforms the command into a sequence of primitive
operations described above, and delegates them to the CCrystalTextBuffer object. Once the changes are
made, the CCrystalTextBuffer object updates all views connected to it.
CCrystalTextBuffer exists within the CDocument object. You must
provide a way to connect views to the object (the best place for it is CView::OnInitialUpdate
handler). In most cases, you will also need to override SetModified method to keep 'dirty' flag
of the document up-to-date. Consider the following sample code:
class CSampleDoc : public CDocument { // code omitted // Attributes public: class CSampleTextBuffer : public CCrystalTextBuffer { private: CSampleDoc *m_pOwnerDoc; public: CSampleTextBuffer(CSampleDoc *pDoc) { m_pOwnerDoc = pDoc; }; virtual void SetModified(BOOL bModified = TRUE) { m_pOwnerDoc->SetModifiedFlag(bModified); }; }; CSampleTextBuffer m_xTextBuffer; };
CCrystalTextView objects can exist without a buffer class, in that case it must provide
its own storage for lines (binded to another storage object, for example) and mechanisms for
updating the view when text content changes. Whether are you using CCrystalTextBuffer object or
not, you will always need to derive your class from CCrystalTextView.
CCrystalTextView cannot exist without CCrystalTextBuffer object.
CCrystalEditView (or CCrystalTextView) with the CCrystalTextBuffer object, you
must go through the following steps:
CCrystalEditView (or CCrystalTextView).
LocateTextBuffer member function. After that, your view class
declaration will look like this:
class CSampleView : public CCrystalEditView { // code omitted protected: virtual CCrystalTextBuffer *LocateTextBuffer(); }
and the implementation will look like this:
CCrystalTextBuffer *CSampleView::LocateTextBuffer()
{
CSampleDoc *pDoc = (CSampleDoc *) GetDocument();
return &pDoc->m_xTextBuffer;
}
LoadFromFile method of CCrystalTextBuffer class.
To save the text to file, call SaveToFile. Remember, you must call
InitNew or LoadFromFile member function before using the object;
and FreeAll function before deleting it.
All parsing is concentrated in a single method of CCrystalTextView class, declared as follows:
virtual DWORD ParseLine(DWORD dwCookie, int nLineIndex, TEXTBLOCK *pBuf, int &nActualItems); struct TEXTBLOCK { int m_nCharPos; // Offset from beginning of the line int m_nColorIndex; // Type of the block being defined: COLORINDEX_NORMALTEXT, // COLORINDEX_KEYWORD, COLORINDEX_COMMENT, etc. };This method should parse the line specified by its zero-based number (nLineIndex) and split it into the blocks of text. Each block is provided with the character position and its color.
To understand why we need last four cases, consider the following C++ code snippet:
// This is the continuous double-slash comment.\ you see, it is really continuous ! #define MESSAGE "And this is continuous preprocessor directive.\n"\ "And this is its second line."
This approach can minimize amount of information, that we need to keep within the view object. Actually,
we must preserve only the information that must be passed from one line to another. Moreover, to
increase parsing speed, sometimes ParseLine member is called with NULL as pBuf parameter.
In that case, the function is called only to calculate the cookie, and that can be made much faster.
For more information, look in the demo project, which includes parser for the C++ language.
CDocument object. The view must take the text from
this array. The view class declaration will look like this:
protected: virtual int GetLineCount(); virtual int GetLineLength(int nLineIndex); virtual LPCTSTR GetLineChars(int nLineIndex);
And implementation will look like this:
int CSampleView::GetLineCount() { // Please note that we must always return at least 1 line. // Even empty text has a single *empty* line! CSampleDoc *pDoc = (CSampleDoc *) GetDocument(); return pDoc->m_strarrLines.GetSize(); } int CSampleView::GetLineLength(int nLineIndex) { CSampleDoc *pDoc = (CSampleDoc *) GetDocument(); return pDoc->m_strarrLines[nLineIndex].GetLength(); } LPCTSTR CSampleView::GetLineChars(int nLineIndex) { CSampleDoc *pDoc = (CSampleDoc *) GetDocument(); return pDoc->m_strarrLines[nLineIndex]; }
The demo project includes parsing methods and the keyword set for C/C++ language. It was originally built using MS Developer Studio 5.0 SP3.
Posted at CodeProject.com with permission of Andrei Stcherbatchenko.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Jan 2000 Editor: Chris Maunder |
Copyright 2000 by Andrei Stcherbatchenko Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |