Development text editor Study Notes original Tutorial: Design and Implementation of A Win32 text editor by James
Translated by Liu Jianwen (http://blog.csdn.net/keminlau)
Part 1 Introduction
Although the title of the tutorial is to design and implement a text editor, we mainly focus on developing an edit control as the front-end of a text editor ), of course, we also need to write a small complete program to test this custom control, and the function of this complete program will not exceed the built-in notepad of windows.
Custom editing Control
The SDK already provides some editing controls, such as edit and rich-edit controls. However, the editing controls have limited functions, and the development of custom controls aims to implement the desired functions.
No matter what kind of software or software components are developed (like the current custom controls), the first step must be to clarify what kind of functionality the software wants to implement. Here clear and clear features are emphasized, it is best to write it down in text, because the initial design has a significant impact on the implementation of the final software. The custom editing control in this tutorial has the following functions:
- First, there is no size limit on the file, and the performance impact is very small or even not affected when you edit large files in a row;
- Second, fast and smooth (graphic) display. It includes scrolling for text sorting and editing;
- Third, syntax coloring;
- Fourth, support for clipboard and drag-and-drop operations;
- Fifth, a single font;
- Sixth, support for Undo and redo;
- 7. Compatibility with ASCII, Unicode and UTF-8.
Neatpad design and composition
The neatpad consists of four components: Two "visible" and two "invisible ". The main window and the editing control (textview) are visible, while the Text Document Object and text file are invisible ).
- Main Window: the main window is a top-level window consisting of the title bar, menu, and status bar, do not directly operate on text files;
- Editing
Editing control is a separate window (separate
Window), and as a child window of the main window, there is no title bar (title), menu (menu) and status bar (Status)
But there is a scroll bar (scrollbars ). The main function is to display and edit text, including handling keyboard and mouse input and dragging.
- Textdocument: The document object has only one data source and no visible part. Therefore, there is no window attribute (no title, no user input is accepted). The main function is to face the editing control, provide it with the required data (how to organize the data? How to provide ?).
- Text File)
Interfaces between components
The component design reflects the "interfaces" design between components. Textview
Is a line-oriented graphical entity. Its function is to display updated data, and this operation must interact with the textdocument component, from
The textdocument component reads data in the unit of "row.
Another interface is the textdocument interface and the disk file interface. Unlike the One-row method, textdocument either loads the entire disk file into the memory or splits it into manageable data blocks (manageable chunks ). The size of the file to be read can be determined based on the actual situation.
Textview public interface-message sending
Part 2 Text loading and display
The purpose of the second part is to load the text file to the memory and display it to the textview control. This part only provides basic display functions, and textview does not support scrolling, keyboard, and mouse. The code policy in this phase is also very straightforward, and the function implementation is not optimized, with ease of use as the principle.
Text Files and text document objects (textdocument)
- Text FilesExcept for the Convention conventions, it is similar to other binary files. Conventions include defining non-printable characters (such as control characters) and text with separators (such as carriage return line breaks) into multiple lines.
- Text Document Object(Textdocument) is defined as a C ++ class instance.
Class textdocument
{
Public:
Bool Init (char * filename );
Ulong Getline (ulong lineno, char * Buf, size_t Len );
Ulong linecount ();
PRIVATE:
Bool init_linebuffer ();
Char * buffer;
Int length;
};
Textdocument file Loading Function
Class Interface |
Win32 API used |
Auxiliary Functions |
Function |
Textdocument: Init (char * filename) |
Createfile |
Init (handle hfile) |
Open only files, not load |
Textdocument: Init (handle hfile) |
Getfilesize, readfile, closehandle |
Init_linebuffer () |
Load the entire file to the memory |
Init_linebuffer () |
- |
- |
Create row data cache (row array) based on file content) |
Textview Display Requirements for textdocument
First, the textview control must know the number of lines of text, because textview should be set to the scroll bar based on the document length;
Second, textview must be able to know what is a line, because textview draws text in the unit of behavior.
Class Interface |
Used API functions |
Auxiliary Functions |
Function |
Textdocument: init_linebuffer () |
- |
- |
Create row data cache (row array) based on file content) |
Textdocument: Getline (ulong lineno, char * Buf, size_t Len) |
- |
Memcpy |
|