|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
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; };
Using CCrystalTextView or CCrystalEditView with buffer classTo useCCrystalEditView (or CCrystalTextView) with the CCrystalTextBuffer object, you
must go through the following steps:
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.
Parsing and syntax coloringAll parsing is concentrated in a single method of 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. For the sake of an efficiency, the internal view implementation preserves the result of parsing each line. dwCookie parameter means the result of parsing the previous line. Really, this is the minimum of the information, needed to restart the parser from the indicated line. For example, when parsing C++ code, you'll have to pass the following set of flags as dwCookie parameter:
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 For more information, look in the demo project, which includes parser for the C++ language. Using CCrystalTextView without buffer classIn that case, we are using it just as text viewer, and we need to provide the storage for lines. Suppose, we have an array of strings in theCDocument 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]; } Known drawbacks and limitations
If you decide to use this codeYou are free to use or modify this code to the following restrictions:
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.
|
||||||||||||||||||||||