Reader small image

You're reading from  C++ Windows Programming

Product typeBook
Published inSep 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786464224
Edition1st Edition
Languages
Right arrow
Author (1)
Stefan Björnander
Stefan Björnander
author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander

Right arrow

Chapter 6. Building a Word Processor

In this chapter, we build a word processor that is capable of handling text on character level: that is, a single character that has its own font, color, size, and style. We also introduce caret handling, printing and print previewing, and file dropping, as well as clipboard handling with ASCII and Unicode text, which means that we can cut and paste between this application and, for instance, a text editor.

Auxiliary classes


A document in this application is made up of pages, paragraphs, lines, and characters. Let me try to explain how it all hangs together:

  • First of all, the document is made up of a list of characters. Each character has its own font and pointers to the paragraph and line it belongs to. The character information is stored in objects of the CharInfo class. The charList field in the WordDocument class is a list of CharInfo objects.

  • The characters are divided into paragraphs. A paragraph does not hold its own character list. Instead, it holds the indexes in the character list of its first and last characters. The paragraphList field in WordDocument is a list of Paragraph objects. The last character of each paragraph is always a newline.

  • Each paragraph is divided into a list of lines. The Paragraph class below holds a list of Line objects. A line holds the indexes of its first and last characters relative to the beginning of the paragraph.

  • Finally, the document is also divided into...

The MainWindow class


The MainWindow class is nearly identical to the versions of the previous chapters. It sets the application name to Word and returns the address of a WordDocument instance:

#include "..\\SmallWindows\\SmallWindows.h" 
#include "CharInfo.h" 
#include "LineInfo.h" 
#include "Paragraph.h" 
#include "WordDocument.h" 
 
void MainWindow(vector<String> /* argumentList */, 
                WindowShow windowShow) { 
  Application::ApplicationName() = TEXT("Word"); 
  Application::MainWindowPtr() = new WordDocument(windowShow); 
} 

The WordDocument class


The WordDocument class is the main class of the application. It extends the StandardDocument class and takes advantage of its document-based functionality.

WordDocument.h

class WordDocument : public StandardDocument { 
  public: 
    WordDocument(WindowShow windowShow); 

The InitDocument class is called by the constructor, the ClearDocument, and Delete classes.

    void InitDocument(); 

The OnKeyboardMode method is called every time the user presses the Insert key. The UpdateCaret method sets the caret to a vertical bar in insert mode and a block in overwrite mode. When the user marks one or several characters, the caret is cleared.

    void OnKeyboardMode(KeyboardMode keyboardMode); 
    void UpdateCaret(); 

When the user presses, moves, and releases the mouse, we need to find the index of the character located at the mouse position. The MousePointToIndex method finds the paragraph, and the MousePointToParagraphIndex method finds the character...

Summary


In this chapter, you started to develop a word processor capable of handling individual characters. The word processor supports the following:

  • Individual font and style of each character

  • Left, center, right, and justified alignment of each paragraph

  • Paragraphs that are distributed over the pages

  • Scrolling and zooming

  • Touchscreen

  • Cut, copy, and paste with ASCII or Unicode text, as well as application-specific generic information

In Chapter 7, Keyboard Input and Character Calculation, we will continue with the keyboard input and character calculation.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Windows Programming
Published in: Sep 2016Publisher: PacktISBN-13: 9781786464224
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander