Design Pattern Examples
Overview of object-oriented design patterns
MessageWindow Class Reference

Represents a rectangular region that can handle messages directed to that region. More...

#include <HandlerChain_MessageWindow_Class.h>

Inheritance diagram for MessageWindow:
Inheritance graph
Collaboration diagram for MessageWindow:
Collaboration graph

Public Types

using shared_ptr_t = std::shared_ptr< MessageWindow >
 
- Public Types inherited from IMessageHandler
using shared_ptr_t = std::shared_ptr< IMessageHandler >
 Alias to make it easier when using a shared pointer.
 

Public Member Functions

bool _PointInWindow (MessagePosition position)
 Determine if the specified point is in this MessageWindow's region.
 
bool _PointInCloseBox (MessagePosition position)
 Determine if the specified point is in this MessageWindow's "close" region.
 
 MessageWindow (int windowId, std::string title, int x, int y, int width, int height, HandlerChain *handlerChain)
 Constructor.
 
int ID ()
 Returns the ID of the message handler.
 
bool ProcessMessage (Message *message)
 Processes a message.
 
std::string ToString ()
 Convert this handler to a string.
 
- Public Member Functions inherited from IMessageHandler
virtual ~IMessageHandler ()
 Virtual destructor (required for interfaces in C++)
 
virtual int ID ()=0
 ID of the window. This is used to uniquely identify a window in the collection.
 
virtual bool ProcessMessage (Message *message)=0
 Called with a message on each window.
 
virtual std::string ToString ()=0
 Convert the handler to a string.
 

Static Public Member Functions

static MessageWindow::shared_ptr_t CreateWindow (std::string title, int x, int y, int width, int height, HandlerChain *handlerChain)
 Creates an instance of the MessageWindow class with the specified attributes and adds the new instance to the given HandlerChain object.
 
static bool _HandleButtonDownMessage (MessageWindow *window, Message *message)
 Helper method to handle the ButtonDown message.
 
static bool _HandleButtonUpMessage (MessageWindow *window, Message *message)
 Helper method to handle the ButtonUp message.
 
static bool _HandleCloseMessage (MessageWindow *window, Message *message)
 Helper method to handle the Close message.
 

Private Types

using MessageHandler = bool(*)(MessageWindow *window, Message *message)
 Alias for the function that handles the messages.
 

Private Attributes

const int CLOSE_WIDTH = 2
 
const int CLOSE_HEIGHT = 2
 
std::map< MessageType, MessageHandler_messageHandlers
 Maps a message type to a handler method of type MessageHandler.
 
int _windowId
 Unique ID of this window.
 
std::string _title
 Title/Name of this window.
 
WindowRectangle _windowBox
 Position of this window in global coordinates.
 
WindowRectangle _closeBox
 Position of the close window within the window box, although the coordinates are also global coordinates to eliminate the need to convert between window and global coordinates.
 
bool _selected
 Whether this window has been selected (a button click occurred within the window).
 
HandlerChain_handlerChain
 The HandlerChain to which this window belongs (as an IMessageHandler object).
 

Static Private Attributes

static int _nextWindowId = 1
 Used for assigning a unique ID to each created window.
 

Detailed Description

Represents a rectangular region that can handle messages directed to that region.

Note: The IMessageHandler interface is an example of a Facade design pattern, where the complexity of the MessageWindow is exposed only through a few methods to a system that only needs to deal with those few methods.

Definition at line 117 of file HandlerChain_MessageWindow_Class.h.

Member Typedef Documentation

◆ MessageHandler

using MessageHandler = bool (*)(MessageWindow* window, Message* message)
private

Alias for the function that handles the messages.

Definition at line 130 of file HandlerChain_MessageWindow_Class.h.

◆ shared_ptr_t

using shared_ptr_t = std::shared_ptr<MessageWindow>

Definition at line 120 of file HandlerChain_MessageWindow_Class.h.

Constructor & Destructor Documentation

◆ MessageWindow()

MessageWindow ( int  windowId,
std::string  title,
int  x,
int  y,
int  width,
int  height,
HandlerChain handlerChain 
)
inline

Constructor.

Parameters
windowIdUnique ID of the window to use.
titleTitle of the window.
xGlobal x position of the upper left corner of the window's region.
yGlobal y position of the upper left corner of the window's region.
widthWidth of the window's region.
heightHeight of the window's region.
handlerChainA HandlerChain object that will be used for removal of this window when it is closed.

Definition at line 231 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_HandleButtonDownMessage(), MessageWindow::_HandleButtonUpMessage(), MessageWindow::_HandleCloseMessage(), MessageWindow::_messageHandlers, DesignPatternExamples_cpp::ButtonDown, DesignPatternExamples_cpp::ButtonUp, and DesignPatternExamples_cpp::Close.

Member Function Documentation

◆ _HandleButtonDownMessage()

bool _HandleButtonDownMessage ( MessageWindow window,
Message message 
)
static

Helper method to handle the ButtonDown message.

Parameters
windowThe window that is handling the message
messageA Message object describing the ButtonDown message.
Returns
Always returns false even if the message was handled (allows other windows to select/deselect themselves).

Definition at line 31 of file HandlerChain_MessageWindow_Class.cpp.

References MessageWindow::_PointInWindow(), MessageWindow::_selected, MessageWindow::_title, Helpers::formatstring(), and Message::Position.

Referenced by MessageWindow::MessageWindow().

◆ _HandleButtonUpMessage()

bool _HandleButtonUpMessage ( MessageWindow window,
Message message 
)
static

Helper method to handle the ButtonUp message.

Parameters
windowThe window that is handling the message
messageA Message object describing the ButtonUp message.
Returns
Returns true if the message was handled; otherwise, returns false indicating the message was not handled.

Definition at line 62 of file HandlerChain_MessageWindow_Class.cpp.

References MessageWindow::_handlerChain, MessageWindow::_PointInCloseBox(), MessageWindow::_PointInWindow(), MessageWindow::_selected, MessageWindow::_title, DesignPatternExamples_cpp::Close, Helpers::formatstring(), Message::Position, and HandlerChain::SendMessage().

Referenced by MessageWindow::MessageWindow().

◆ _HandleCloseMessage()

bool _HandleCloseMessage ( MessageWindow window,
Message message 
)
static

Helper method to handle the Close message.

Parameters
windowThe window that is handling the message
messageA Message object describing the Close message.
Returns
Returns true if the message was handled; otherwise, returns false indicating the message was not handled.

Definition at line 92 of file HandlerChain_MessageWindow_Class.cpp.

References MessageWindow::_handlerChain, MessageWindow::_selected, MessageWindow::_title, Helpers::formatstring(), and HandlerChain::RemoveHandler().

Referenced by MessageWindow::MessageWindow().

◆ _PointInCloseBox()

bool _PointInCloseBox ( MessagePosition  position)
inline

Determine if the specified point is in this MessageWindow's "close" region.

Parameters
positionThe point to examine.
Returns
Returns true if the point is contained within the MessageWindow's "close" region.

Definition at line 213 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_closeBox, and WindowRectangle::PointInside().

Referenced by MessageWindow::_HandleButtonUpMessage().

◆ _PointInWindow()

bool _PointInWindow ( MessagePosition  position)
inline

Determine if the specified point is in this MessageWindow's region.

Parameters
positionThe global coordinate to examine.
Returns
Returns true if the point is contained within the MessageWindow's region.

Definition at line 200 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_windowBox, and WindowRectangle::PointInside().

Referenced by MessageWindow::_HandleButtonDownMessage(), and MessageWindow::_HandleButtonUpMessage().

◆ CreateWindow()

MessageWindow::shared_ptr_t CreateWindow ( std::string  title,
int  x,
int  y,
int  width,
int  height,
HandlerChain handlerChain 
)
static

Creates an instance of the MessageWindow class with the specified attributes and adds the new instance to the given HandlerChain object.

Parameters
titleTitle of the MessageWindow.
xX position of the upper left corner of the window's region.
yY position of the upper left corner of hte window's region.
widthWidth of the window's region.
heightHeight of the window's region.
handlerChainA HandlerChain object that will be given the window.
Returns
Returns the newly created MessageWindow.

Each MessageWindow instance is assigned a unique ID, which is required by the HandlerChain object.

Definition at line 15 of file HandlerChain_MessageWindow_Class.cpp.

References MessageWindow::_nextWindowId, and HandlerChain::AddHandler().

◆ ID()

int ID ( )
inlinevirtual

Returns the ID of the message handler.

Implements IMessageHandler.

Definition at line 292 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_windowId.

Referenced by MessageWindow::ToString().

◆ ProcessMessage()

bool ProcessMessage ( Message message)
inlinevirtual

Processes a message.

Parameters
messageThe Message object to process.
Returns
Returns true if the message was handled and no further windows should be notified; otherwise return false to allow the message to be passed to subsequent windows.

The message types are mapped to handlers in the MessageWindow constructor and stored in the _messageHandlers dictionary.

Implements IMessageHandler.

Definition at line 309 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_messageHandlers, and Message::MessageType.

◆ ToString()

std::string ToString ( )
inlinevirtual

Convert this handler to a string.

Returns
Returns a string representation of the message handler.

Implements IMessageHandler.

Definition at line 329 of file HandlerChain_MessageWindow_Class.h.

References MessageWindow::_selected, MessageWindow::_title, MessageWindow::_windowBox, Helpers::formatstring(), MessageWindow::ID(), and WindowRectangle::ToString().

Member Data Documentation

◆ _closeBox

WindowRectangle _closeBox
private

Position of the close window within the window box, although the coordinates are also global coordinates to eliminate the need to convert between window and global coordinates.

Definition at line 157 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::_PointInCloseBox().

◆ _handlerChain

HandlerChain* _handlerChain
private

The HandlerChain to which this window belongs (as an IMessageHandler object).

Definition at line 168 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::_HandleButtonUpMessage(), and MessageWindow::_HandleCloseMessage().

◆ _messageHandlers

std::map<MessageType, MessageHandler> _messageHandlers
private

◆ _nextWindowId

int _nextWindowId = 1
staticprivate

Used for assigning a unique ID to each created window.

Definition at line 173 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::CreateWindow().

◆ _selected

bool _selected
private

Whether this window has been selected (a button click occurred within the window).

Definition at line 163 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::_HandleButtonDownMessage(), MessageWindow::_HandleButtonUpMessage(), MessageWindow::_HandleCloseMessage(), and MessageWindow::ToString().

◆ _title

◆ _windowBox

WindowRectangle _windowBox
private

Position of this window in global coordinates.

Definition at line 150 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::_PointInWindow(), and MessageWindow::ToString().

◆ _windowId

int _windowId
private

Unique ID of this window.

Definition at line 140 of file HandlerChain_MessageWindow_Class.h.

Referenced by MessageWindow::ID().

◆ CLOSE_HEIGHT

const int CLOSE_HEIGHT = 2
private

Definition at line 125 of file HandlerChain_MessageWindow_Class.h.

◆ CLOSE_WIDTH

const int CLOSE_WIDTH = 2
private

Definition at line 124 of file HandlerChain_MessageWindow_Class.h.


The documentation for this class was generated from the following files: