Saturday 30 July 2011

Doc-View Tutorial from Scratch : Part I




Document/View Architecture Tutorial

If you are working on VC++ then it is difficult to resist using MFC in your applications. MFC classes are a nice wrapper around Win32 API and easy to use. When we start VC++ application using MFC, we find ourselves in the lap of MFC Framework. Why should not be? Using wizards is so fun. Creating MDI, SDI, or Dialog based applications are so simple. Just select one option. Build and run the application. By default, we got menus support, printing support, document supports with multiple views. Who cares what the hell is going on under the hood? But when we want to add some functionality that MFC Framework doesn't support, then what ? Aaooo .... MFC framework sucks. Isn't it ? Yes it does if we don't know what's going on under the hood. But if we know then it makes things easier and interesting to play with it.

Every application's purpose is to perform operations on some kind of data and represent it in a desired form so user can understand it. MFC Framework helps developers to manage and represent data in a desired form. We can write same application without MFC Framework, but Framework makes it simpler. The Document/View is a part of MFC Framework. The Document/View architecture is implemented by using MFC classes created when we create an application using the AppWizard.

MFC Framework


The Microsoft Foundation Class Library (MFC) framework is an implementation of some classes and several Visual C++ tools. These classes encapsulate a Win32 API and other concepts such as documents, views, OLE, DAO, ODBC and the application itself. The MFC is an "application framework" for programming in Microsoft Windows that provides much of the code necessary for managing windows, menus, and dialog boxes; performing basic input/output; storing collections of data objects; and so on. All you need to do is add your application-specific code into this framework then it's easy to extend or override the basic functionality of MFC framework. MFC shortens development time; makes code more portable; provides tremendous support without reducing programming freedom and flexibility; and gives easy access to "hard to program" user-interface elements and technologies, like Active technology, OLE, database, network, and Internet programming.
What is Document/View Architecture?
The Document/view architecture is a part of MFC framework that provides a logical separation between application's data and representation of data. A document is an object that acts as a container of application's data and a view is a window object, usually associated with the client area of screen window, through which user interacts with the data contained in the document. So eventually, what's is document/view architecture?Let's say " A set of MFC classes that manages application's data in a structured way and render that data so user can interact with that data". One document object can have it's multiple views and each view is different rendering of same data. For example, we can represend a numerical data in either a tabular form, or a chart form.
MFC's CDocument and CView classes inherit the functionality of document object and it's view. A document represents the unit of data that the user typically opens with the Open command on the File menu and saves with the Save command on the File menu. A view is attached to a document and acts as an intermediary between the document and the user: the view renders an image of the document on the screen and interprets user input as operations upon the document. The view also renders the image for both printing and printpreview.
Advantages of Document/View Architecture
The document/view architecture assists developers by simplifying the process of rendering data on the screen
in a way that enables users to interact with the data. Think.... if we design this all functionality from scratch
through the overloading view's drawing function, handling mouse and keyboard interaction with the view, and
handling menu, toolbar commands. Wouldn't it be pain and time consuming? And ofcourse, separation of data
from it's views increases the modularity of an application.

  • Multiple views - The main advantage is that a single document can have multiple views. Architecture
    provides correct synchronization of multiple views attached to same document. It reflects updated data
    to all of document's views instantly.
  • Serialization support - CObject helps in saving data to disk and loading back to memory from disk.
  • Intellegent commnad routing - Implementing command routing for menus, toolbars, status bars won't
    be easy using Win32 API.
  • Support for printing and print preview by same logic to render the data on a view, on a print preview
    window, or on a printer.
Document/View Architecture Components
There are five components in document/view architecture.
  • The document
  • The view
  • The view's frame window
  • The document template
  • The document template resource ID
Role of the document
  • It conceptually contains the data.- That means a document object doesn't need to store the actual
    data in it's own data structure. Instead, a document object stores and manages the entry point of the
    relevant data. For example a recordset handle. Actual data is stored in a database but recordset handle is
    stored in document's data structure.
  • Manipulation of data.
  • Serialization - With the help of CDocument's Serialize() overridable function, a document can store and
    retrieve data and it's own state to a persistant medium such as disk. The MFC framework responds
    automatically to File menu's Open, Save, and Save As commands by calling the document's Serialize()member function.An ID_FILE_OPEN command, calls a handler function in the application object. During
    this process, the user sees and responds to the File Open dialog box and the framework obtains the
    filename the user chooses. The framework creates a CArchive object set up for loading data into the
    document and passes the archive to Serialize. The framework has already opened the file. The code in
    your document's Serialize member function reads the data in through the archive, reconstructing data
    objects as needed.
  • Synchronization - A document maintains a list of all of it's veiws that are opened on it. To update all
    views it calls UpdateAllViews function, which calls each view's OnUpdate() that causes each view to
    refresh itself and display the current data from the document object.
  • Handle most of commands and messages - Your document class may also handle certain commands
    generated by menu items, toolbar buttons, or accelerator keys. By default, CDocument handles the Save
    and Save As commands on the File menu, using serialization. Other commands that affect the data may
    also be handled by member functions of your document. Documents can have message maps, but unlike
    views, documents cannot handle standard Windows messages รข€” only WM_COMMAND messages, or
    "commands."
Role of the view
  • As an input device, the view can handle user inputs, such as keyboard, mouse or input via drag-and-drop,
    as well as commands from menus, toolbars, or scroll bars. A view receives commands forwarded by its
    frame window. If the view does not handle a given command, it forwards the command to its associated
    document. Like all command targets, a view handles messages via a message map.
  • As an output device, a view extracts the data from the document and presents the data in an appropriate
    way. A view is responsible for displaying and modifying the document's data but not for storing it. The
    document provides the view with the necessary details about its data.
Role of the view's frame window
  • Provides support of splitter window which helps to separate more than one views hosted by frame window.
  • Host views and other control bars such as toolbars, status bars, and dialog bars.
Role of the document template
  • Document template dynamically creates instances of the document, view, and frame window classes.
    MFC's CDocTemplate class inherits the functionality of document template. Usually InitInstance function
    of an MFC application creates one or more document templates. A document template defines the
    relationships among three types of classes:
  • A document class derived from CDocument.
  • A view class, which displays data from the document class listed above. This class is derived from on of
    CView, CScrollView, CFormView, or CEditView classes.
  • A frame window class, which contains the view. For a single document interface (SDI) application, you
    derive this class from CFrameWnd. For a multiple document interface (MDI) application, you derive this
    class from CMDIChildWnd.
An application has one document template for each type of document that it supports. For example, if
application supports both spreadsheets and text documents, the application has two document template
objects. Each document template is responsible for creating and managing all the documents of its type.
The document template stores pointers to the CRuntimeClass objects for the document, view, and frame
window classes. These CRuntimeClass objects are specified when constructing a document template.
The document template contains the ID of the resources used with the document type (such as menu, icon, or
accelerator table resources). The document template also has strings containing additional information about its
document type. These include the name of the document type (for example, "Worksheet") and the file
extension (for example, ".xls"). Optionally, it can contain other strings used by the application's user interface,
the Windows File Manager, and Object Linking and Embedding (OLE) support.
Because CDocTemplate is an abstract class, you cannot use the class directly. A typical application uses one
of the two CDocTemplate-derived classes provided by the Microsoft Foundation Class Library:
CSingleDocTemplate, which implements SDI, and CMultiDocTemplate, which implements MDI. See those
classes for more information on using document templates.
Here is InitInstance of an SDI application :
BOOL CSDIAppApp::InitInstance()
{
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CSDIAppDoc), RUNTIME_CLASS
CMainFrame), // main SDI frame window
RUNTIME_CLASS(CSDIAppView));
AddDocTemplate(pDocTemplate);
}

Here is InitInstance of an SDI application :
BOOL CMDIAppApp::InitInstance()
{
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate( IDR_MDIAPPTYPE, RUNTIME_CLASS(CMDIAppDoc), RUNTIME_CLASS
CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CMDIAppView));
AddDocTemplate(pDocTemplate);
}
Role of the document template resource ID
The document template resource ID associates three specific resources with a given document template object:
  • An icon Resource.
  • A menu resource. When the user activates a particular frame window,MFC replaces the main application
    menu with the menu resource associated with the document template resource ID of the active frame
    window object.
  • A string resource. This string is concatnation of seven substrings separated by "\n". These are frame
    window's title ( windowTitle), default name assigned to new document (docName), descriptive name for
    document of this type ( fileNewName), descriptive name for document of this type and associated default
    file type (filterName), default file name extension (filterExt), name registered in the registry for this type
    of document ( regFileTypeId), and human readable name for this file type appears in windows explorer
    (regFileTypeName). For example
    nMDIApp\nMDIApp\n\n\nMDIApp.Document\nMDIApp Document , here windowTitle = "", docName =
    "MDIApp", fileNewName = "MDIApp", filterName = "", filterExt = "", regFileTypeID = MDIApp.Document, and
    regFileTypeName = MDIApp Document.

0 comments:

Post a Comment

Popular Posts

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls