Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_MessageWindow.c File Reference

Declaration of the MessageWindow structure, along with the implementation of the support functions, MessageWindow_Create(), MessageWindow_Destroy(), MessageWindow_ProcessMessage(), and MessageWindow_ToString(), as used in the HandlerChain Pattern. More...

#include <stdlib.h>
#include <stdio.h>
#include "helpers/formatstring.h"
#include "HandlerChain_WindowRectangle.h"
#include "HandlerChain_HandlerFunctions.h"
#include "HandlerChain_MessageWindow.h"
Include dependency graph for HandlerChain_MessageWindow.c:

Go to the source code of this file.

Classes

struct  _Window
 Represents a window in an arbitrary space. It has an ID, title, and position. A close box is within the window's rectangle. The window can be selected by sending a button down message with the position within this window. If a button down message occurs outside this window, it is deselected. More...
 

Typedefs

typedef struct _Window MessageWindow
 Alias for struct _Window, which represents a window in an arbitrary space. It has an ID, title, and position. A close box is within the window's rectangle. The window can be selected by sending a button down message with the position within this window. If a button down message occurs outside this window, it is deselected.
 

Functions

static void _AppendWindowToList (MessageWindow *window)
 Helper function to append the given MessageWindow to the list of MessageWindow objects.
 
static void _RemoveWindowFromList (MessageWindow *window)
 Helper function to remove the given MessageWindow object from the linked list of MessageWindow objects.
 
static MessageWindow_FindWindow (int windowId)
 Helper function to find a MessageWindow given the window's ID.
 
static bool _HandleButtonDownMessage (MessageWindow *window, Message *message)
 Helper function to handle the ButtonDown message.
 
static bool _HandleButtonUpMessage (MessageWindow *window, Message *message)
 Helper function to handle the ButtonUp message.
 
static bool _HandleCloseMessage (MessageWindow *window, Message *message)
 Helper function to handle the Close message.
 
static bool _HandleDestroyMessage (MessageWindow *window, Message *message)
 Helper function to trigger the destruction of the window. The window is destroyed and can no longer receive any messages.
 
int MessageWindow_Create (const char *title, int x, int y, int w, int h)
 Create a MessageWindow object with the given properties and return the ID of the object.
 
void MessageWindow_Destroy (int windowId)
 Destroy the MessageWindow object with the given ID. Removes the window from any message handling as well.
 
bool MessageWindow_ProcessMessage (int windowId, Message *message)
 Pass a Message object to a window for processing.
 
bool MessageWindow_ToString (int windowId, DynamicString *output)
 Convert the specified window to a string representation.
 

Variables

const int CLOSE_WIDTH = 2
 Width of the close region in the window.
 
const int CLOSE_HEIGHT = 2
 Height of the close region in the window.
 
static MessageWindow_windowList = NULL
 List of all created MessageWindow objects, in a double-linked list.
 
static int _nextWindowId = 1
 The next ID to use for a new window.
 

Detailed Description

Declaration of the MessageWindow structure, along with the implementation of the support functions, MessageWindow_Create(), MessageWindow_Destroy(), MessageWindow_ProcessMessage(), and MessageWindow_ToString(), as used in the HandlerChain Pattern.

Definition in file HandlerChain_MessageWindow.c.

Typedef Documentation

◆ MessageWindow

typedef struct _Window MessageWindow

Alias for struct _Window, which represents a window in an arbitrary space. It has an ID, title, and position. A close box is within the window's rectangle. The window can be selected by sending a button down message with the position within this window. If a button down message occurs outside this window, it is deselected.

Definition at line 82 of file HandlerChain_MessageWindow.c.

Function Documentation

◆ _AppendWindowToList()

static void _AppendWindowToList ( MessageWindow window)
static

Helper function to append the given MessageWindow to the list of MessageWindow objects.

Parameters
windowA MessageWindow object to append to the list.

Definition at line 100 of file HandlerChain_MessageWindow.c.

References _windowList, _Window::next, and _Window::prev.

Referenced by MessageWindow_Create().

◆ _FindWindow()

static MessageWindow * _FindWindow ( int  windowId)
static

Helper function to find a MessageWindow given the window's ID.

Parameters
windowIdThe ID of the window to find.
Returns
Returns a pointer to the MessageWindow that matches the ID; otherwise, returns NULL to indicate no window exists by that ID.

Definition at line 153 of file HandlerChain_MessageWindow.c.

References _Window::_windowId, _windowList, and _Window::next.

Referenced by MessageWindow_Destroy(), MessageWindow_ProcessMessage(), and MessageWindow_ToString().

◆ _HandleButtonDownMessage()

static bool _HandleButtonDownMessage ( MessageWindow window,
Message message 
)
static

Helper function to handle the ButtonDown message.

Parameters
windowThe MessageWindow 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 183 of file HandlerChain_MessageWindow.c.

References _Window::_selected, _Window::_title, _Window::_windowBox, Message::Position, and WindowRectangle_PointInside().

Referenced by MessageWindow_ProcessMessage().

◆ _HandleButtonUpMessage()

static bool _HandleButtonUpMessage ( MessageWindow window,
Message message 
)
static

Helper function to handle the ButtonUp message.

Parameters
windowThe MessageWindow 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 221 of file HandlerChain_MessageWindow.c.

References _Window::_closeBox, _Window::_selected, _Window::_title, _Window::_windowBox, _Window::_windowId, Close, HandlerChain_SendMessage(), Message_Initialize(), Message::Position, WindowRectangle_PointInside(), MessagePosition::X, and MessagePosition::Y.

Referenced by MessageWindow_ProcessMessage().

◆ _HandleCloseMessage()

static bool _HandleCloseMessage ( MessageWindow window,
Message message 
)
static

Helper function to handle the Close message.

Parameters
windowThe MessageWindow 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 261 of file HandlerChain_MessageWindow.c.

References _Window::_selected, _Window::_title, _Window::_windowId, Destroy, HandlerChain_SendMessage(), and Message_Initialize().

Referenced by MessageWindow_ProcessMessage().

◆ _HandleDestroyMessage()

static bool _HandleDestroyMessage ( MessageWindow window,
Message message 
)
static

Helper function to trigger the destruction of the window. The window is destroyed and can no longer receive any messages.

The given MessageWindow is destroyed and can no longer be used after this function returns.

Parameters
windowThe MessageWindow that is handling the message.
messageThe Message object describing the Destroy message.
Returns
Returns true if the message was handled; otherwise, returns false indicating the message was not handled.

Definition at line 299 of file HandlerChain_MessageWindow.c.

References _Window::_selected, _Window::_title, _Window::_windowId, HandlerChain_RemoveWindow(), and MessageWindow_Destroy().

Referenced by MessageWindow_ProcessMessage().

◆ _RemoveWindowFromList()

static void _RemoveWindowFromList ( MessageWindow window)
static

Helper function to remove the given MessageWindow object from the linked list of MessageWindow objects.

Parameters
windowThe MessageWindow object to remove from the list.

Definition at line 123 of file HandlerChain_MessageWindow.c.

References _windowList, _Window::next, and _Window::prev.

Referenced by MessageWindow_Destroy().

◆ MessageWindow_Create()

int MessageWindow_Create ( const char *  title,
int  x,
int  y,
int  w,
int  h 
)

Create a MessageWindow object with the given properties and return the ID of the object.

Parameters
titleTitle of the window.
xHorizontal position of the upper left corner of the window.
yVertical position of the upper left corner of the window.
wWidth of the window.
hHeight of the window.
Returns
Returns the ID of the window, if successfully created; otherwise, returns false.

Definition at line 318 of file HandlerChain_MessageWindow.c.

References _AppendWindowToList(), _Window::_closeBox, _nextWindowId, _Window::_title, _Window::_windowBox, _Window::_windowId, CLOSE_HEIGHT, CLOSE_WIDTH, HandlerChain_AddWindow(), WindowRectangle::Right, WindowRectangle::Top, and WindowRectangle_Initialize().

Referenced by _HandlerChain_ConstructWindowChain().

◆ MessageWindow_Destroy()

void MessageWindow_Destroy ( int  windowId)

Destroy the MessageWindow object with the given ID. Removes the window from any message handling as well.

Parameters
windowIdID of the MessageWindow to destroy.

Definition at line 354 of file HandlerChain_MessageWindow.c.

References _FindWindow(), and _RemoveWindowFromList().

Referenced by _HandleDestroyMessage().

◆ MessageWindow_ProcessMessage()

bool MessageWindow_ProcessMessage ( int  windowId,
Message message 
)

Pass a Message object to a window for processing.

Parameters
windowIdThe ID of the window who will get the message.
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.

Definition at line 367 of file HandlerChain_MessageWindow.c.

References _FindWindow(), _HandleButtonDownMessage(), _HandleButtonUpMessage(), _HandleCloseMessage(), _HandleDestroyMessage(), ButtonDown, ButtonUp, Close, Destroy, and Message::MessageType.

Referenced by HandlerChain_SendMessage().

◆ MessageWindow_ToString()

bool MessageWindow_ToString ( int  windowId,
DynamicString output 
)

Convert the specified window to a string representation.

Parameters
windowIdID of the window to convert.
outputA DynamicString that returns the string version of the window. Call DynamicString_Initialize() on this object before passing as an argument.
Returns
Returns true if the message window was converted to a string; otherwise, returns false, indicating an out of memory condition (or a NULL argument).

Definition at line 409 of file HandlerChain_MessageWindow.c.

References _FindWindow(), _Window::_selected, _Window::_title, _Window::_windowBox, _Window::_windowId, DynamicString_Append(), DynamicString_Clear(), formatstring(), DynamicString::string, and WindowRectangle_ToString().

Referenced by HandlerChain_ToString().

Variable Documentation

◆ _nextWindowId

int _nextWindowId = 1
static

The next ID to use for a new window.

Definition at line 93 of file HandlerChain_MessageWindow.c.

Referenced by MessageWindow_Create().

◆ _windowList

MessageWindow* _windowList = NULL
static

List of all created MessageWindow objects, in a double-linked list.

Definition at line 88 of file HandlerChain_MessageWindow.c.

Referenced by _AppendWindowToList(), _FindWindow(), and _RemoveWindowFromList().

◆ CLOSE_HEIGHT

const int CLOSE_HEIGHT = 2

Height of the close region in the window.

Definition at line 21 of file HandlerChain_MessageWindow.c.

Referenced by MessageWindow_Create().

◆ CLOSE_WIDTH

const int CLOSE_WIDTH = 2

Width of the close region in the window.

Definition at line 20 of file HandlerChain_MessageWindow.c.

Referenced by MessageWindow_Create().