Application Framework > The Bitmap Editor Application > Step 3: Modifying and Saving a Bitmap > Defining the DrawRectangleCommand
 
Defining the DrawRectangleCommand
The DrawRectangleCommand class is a subclass of IlvDvCommand. It is declared in drawcmd.h and defined in drawcmd.cpp. The purpose of the DrawRectangleCommand class is to modify the document by drawing a filled rectangle in the document bitmap at the specified location, using the specified color. Thus, from this command, you need to access:
*The document
*The rectangle that will be modified
*The palette used to draw into the bitmap
The constructor appears as follows:
DrawRectangleCommand::DrawRectangleCommand(BitmapDocument* document,
const IlvPoint& point,
IlvDim size,
IlvPalette* palette,
const char* name)
Note: The point and size arguments are used to compute the rectangle that will be modified by the command.
The DrawRectangleCommand::doIt method is called when the command is executed (See the IlvDvDocument::doCommand method). Before modifying the document, the rectangle that will be modified to make the command undoable must be saved:
void
DrawRectangleCommand::doIt()
{
// Save the initial bitmap
_bitmap->drawBitmap(_document->getPalette(),
_document->getBitmap(),
_rect,
IlvPoint(0,0));
// Then draw in the document's bitmap
_document->getBitmap()->fillRectangle(_palette, _rect);
// Finally, refresh all the views connected to the document
_document->refreshViews(_rect);
}
The refreshViews method will be described later in this step. Its purpose is to refresh all the views connected to the document being modified.
The undo method simply restores the bitmap saved in the doIt method and then refreshes the views.
void
DrawRectangleCommand::undo()
{
// Restore the bitmap saved in _bitmap into the document's bitmap
_document->getBitmap()->drawBitmap(_document->getPalette(),
_bitmap,
IlvRect(0,
0,
_bitmap->width(),
_bitmap->height()),
IlvPoint(_rect.x(), _rect.y()));
// Then, refresh all the views connected to the document
_document->refreshViews(_rect);
}

Version 6.0
Copyright © 2015, Rogue Wave Software, Inc. All Rights Reserved.