Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_MessageWindow_Class.cpp
Go to the documentation of this file.
1
5
6#include "helpers/formatstring.h"
7
9
11{
12 int MessageWindow::_nextWindowId = 1;
13
14
15 MessageWindow::shared_ptr_t MessageWindow::CreateWindow(std::string title,
16 int x, int y, int width, int height, HandlerChain* handlerChain)
17 {
18 MessageWindow::shared_ptr_t window;
19 window = std::make_shared<MessageWindow>(_nextWindowId, title, x, y,
20 width, height, handlerChain);
22
23 if (handlerChain != nullptr)
24 {
25 handlerChain->AddHandler(window);
26 }
27 return window;
28 }
29
30
31 bool MessageWindow::_HandleButtonDownMessage(MessageWindow* window, Message* message)
32 {
33 // Note: we are not saying we handled the message here since
34 // we want other windows to get the button down message as
35 // well so they can select or deselect themselves.
36 bool messageProcessed = false;
37
38 if (window->_PointInWindow(message->Position))
39 {
40 if (!window->_selected)
41 {
42 window->_selected = true;
43 std::cout
44 << Helpers::formatstring(" --> Button Down in \"%s\", window selected", window->_title.c_str())
45 << std::endl;
46 }
47 }
48 else
49 {
50 if (window->_selected)
51 {
52 window->_selected = false;
53 std::cout
54 << Helpers::formatstring(" --> Button Down not in \"%s\", window deselected", window->_title.c_str())
55 << std::endl;
56 }
57 }
58 return messageProcessed;
59 }
60
61
62 bool MessageWindow::_HandleButtonUpMessage(MessageWindow* window, Message* message)
63 {
64 bool messageProcessed = false;
65 if (window->_selected)
66 {
67 if (window->_PointInWindow(message->Position))
68 {
69 // The Button Up is in the same window as Button Down so
70 // we will handle this message and let no other window see
71 // it.
72 messageProcessed = true;
73 if (window->_PointInCloseBox(message->Position))
74 {
75 std::cout
76 << Helpers::formatstring(" --> Button Up in \"%s\" close box, sending Close message", window->_title.c_str())
77 << std::endl;
78 window->_handlerChain->SendMessage(std::make_unique<Message>(MessageType::Close, message->Position).get());
79 }
80 else
81 {
82 std::cout
83 << Helpers::formatstring(" --> Button Up in \"%s\", no further action taken", window->_title.c_str())
84 << std::endl;
85 }
86 }
87 }
88 return messageProcessed;
89 }
90
91
92 bool MessageWindow::_HandleCloseMessage(MessageWindow* window, Message* /*message*/)
93 {
94 bool messageProcessed = false;
95 if (window->_selected)
96 {
97 std::cout
98 << Helpers::formatstring(" --> Close in \"%s\", removing window from handler chain", window->_title.c_str())
99 << std::endl;
100
101 // This window is being closed. We are handling the message
102 // so no other window needs to see it.
103 messageProcessed = true;
104 window->_handlerChain->RemoveHandler(window);
105 window->_selected = false;
106 }
107 else
108 {
109 std::cout
110 << Helpers::formatstring(" --> Close seen in \"%s\" but this window is not selected, ignoring", window->_title.c_str())
111 << std::endl;
112 }
113 return messageProcessed;
114 }
115
116} // end namespace
Declaration of the MessageWindow class and WindowRectangle class used in the HandlerChain Pattern.
Represents a list of handlers that all implement the IMessageHandler interface. This list can be dyna...
void SendMessage(Message *message)
Send a message to each of the handlers in the list, protected by a multi-threading lock.
void AddHandler(IMessageHandler::shared_ptr_t window)
Add an instance of the IMessageHandler interface to end of the list of handlers, protected by a multi...
void RemoveHandler(IMessageHandler::shared_ptr_t window)
Remove an instance of the IMessageHandler interface from the list, protected by a multi-threading loc...
Represents a rectangular region that can handle messages directed to that region.
static int _nextWindowId
Used for assigning a unique ID to each created window.
bool _PointInWindow(MessagePosition position)
Determine if the specified point is in this MessageWindow's region.
HandlerChain * _handlerChain
The HandlerChain to which this window belongs (as an IMessageHandler object).
bool _selected
Whether this window has been selected (a button click occurred within the window).
bool _PointInCloseBox(MessagePosition position)
Determine if the specified point is in this MessageWindow's "close" region.
The namespace containing all Design Pattern Examples implemented in C++.
@ Close
Window is asked to close itself, generally sent by the window itself in response to a button up in a ...
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
Represents a message sent to the windows. A message contains a type and a position.
MessagePosition Position
Position of message when the message was sent. In a real system, this would generally represent the p...