Use Crystaledit to make a text editor

zhaozj2021-02-12  132

The Package Consists of Three Main Classes:

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 ,hen ic 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.usually,

CcrystaltextBuffer EXISTS WITHIN THE

CDocument Object. You Must Provide a Way To Connect Views to The Object (The Best Place for It Iscview :: 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.

Using ccrystaltextview or ccrystaleditView with buffer classto us

CcrystaleditView (OR

Ccrystaltextview) with the

CcrystaltextBuffer Object, You Must Go Through The Following Steps:

.. Derive your class from CCrystalEditView (or CCrystalTextView) Override 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;} That's all From this point, view and buffer objects will work together To load!. Text from the file, Simply CallloadFromFile method of

CcrystaltextBuffer Class. To save the text to file, call

Savetofile. Remetr, You Must Call

INITNEW OR

LoadFromfile Member Function Before Using the Object; And

FreeAll Function Before DeleTing IT.

Parsing and syntax coloring

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 girl 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 International 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 asdwCookie parameter.:

. Extended comment (/ * * /) flag This is absolutely needed because C has multiple-line comments Continuous double-slash comment;. Continuous preprocessor directive; Continuous string constant; Continuous character constant.

TO Understand Why We need last four case, consider the following c code snippet:

// this is the continuous double, it is it is real company! #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 this can be made much much.

For More Information, Look in The Demo Project, Which Includes Parser for The C Language.

Using CcrystaltextView WITHOUT BUFFER CLASSIN THATVIEW WITHOUT BUFFER CLASSIN THAT AS TEXT VIEWER, AND WE NEED TO PROVIDE The Storage for Lines. Suppose, WE Have An Array of Strings in The

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; 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];

How to join your own project?

1. Unfold all files below a Editor directory.

2. Add all * .h, *. CPP, *. InL files through the add file;

3, add a ccrystaleditView's inheritance class c ** view, then modify CWINAPP :: initstance

Add a c ** view class in the template;

4, modify CDocument inherited c ** doc, then add the following code to c ** doc.h:

class CSampleTextBuffer: public CCrystalTextBuffer {private: CTProLibDoc * m_pOwnerDoc; public: CSampleTextBuffer (CTProLibDoc * pDoc) {m_pOwnerDoc = pDoc;}; virtual void SetModified (BOOL bModified = TRUE) {m_pOwnerDoc-> SetModifiedFlag (bModified);};}; CSampleTextBuffer m_XTextBuffer;

5, modify the constructor in c ** DOC

#pragma Warning (Disable: 4355) c ** doc :: c ** doc (): m_XTextBuffer (this)

6. Modify the * .rc2 file in the resource directory, add the following code:

#include "editor / editres.rc"

7. To place the data m_xtextbuffer.initnew () in iNewDocument () in the document.

8. Add m_xtextbuffer.freeall () in Delta deleteContents;

9. Add locatetextBuffer () function c ** doc * pdoc = (c ** doc *) getDocument (); return & pdoc-> m_XTextBuffer;

转载请注明原文地址:https://www.9cbs.com/read-6689.html

New Post(0)