Design Pattern Examples
Overview of object-oriented design patterns
HandlerChain_Message.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <stdio.h>
9
10#include "helpers/formatstring.h"
11
13
15// MessagePosition_ToString()
18{
19 bool success = false;
20
21 if (position != NULL && output != NULL)
22 {
23 char* buffer = formatstring("x=%2d,y=%2d", position->X, position->Y);
24 if (buffer != NULL)
25 {
26 success = DynamicString_Append(output, buffer);
27 if (!success)
28 {
29 printf(" Error! out of memory condition appending message position as string in MessagePosition_ToString()!\n");
30 }
31 free(buffer);
32 }
33 else
34 {
35 printf(" Error! Out of memory formatting position in MessagePosition_ToString()!\n");
36 }
37 }
38
39 return success;
40}
41
42
43//=============================================================================
44//=============================================================================
45//=============================================================================
46
47
49// Message_Initialize()
51void Message_Initialize(Message* message, MessageType type, int x, int y)
52{
53 if (message != NULL)
54 {
55 message->MessageType = type;
56 message->Position.X = x;
57 message->Position.Y = y;
58 }
59}
60
62// Message_ToString()
64bool Message_ToString(Message* message, DynamicString* output)
65{
66 bool success = false;
67
68 if (message != NULL && output != NULL)
69 {
70 const char* messageTypeAsString = NULL;
71 switch (message->MessageType)
72 {
73 case Close:
74 messageTypeAsString = "Close";
75 break;
76
77 case ButtonDown:
78 messageTypeAsString = "ButtonDown";
79 break;
80
81 case ButtonUp:
82 messageTypeAsString = "ButtonUp";
83 break;
84
85 case Destroy:
86 // Handled elsewhere
87 break;
88
89 default:
90 messageTypeAsString = "Unknown message type";
91 break;
92 }
93 DynamicString positionOutput = { 0 };
94 DynamicString_Initialize(&positionOutput);
95 success = MessagePosition_ToString(&message->Position, &positionOutput);
96
97 if (success)
98 {
99 char *buffer = formatstring("%s at (%s)", messageTypeAsString, positionOutput.string);
100 if (buffer != NULL)
101 {
102 success = DynamicString_Append(output, buffer);
103 if (!success)
104 {
105 printf(" Error! Out of memory condition in Message_ToString() while appending string to output!\n");
106 }
107 free(buffer);
108 }
109 else
110 {
111 printf(" Error! out of memory formatting message in Message_ToString()!\n");
112 }
113 }
114 DynamicString_Clear(&positionOutput);
115 }
116
117 return success;
118}
bool Message_ToString(Message *message, DynamicString *output)
Convert a Message object to a string representation.
void Message_Initialize(Message *message, MessageType type, int x, int y)
Initialize a Message structure.
bool MessagePosition_ToString(MessagePosition *position, DynamicString *output)
Convert a MessagePosition object to a string representation.
Declaration of the Message and MessagePosition structures, along with the functions,...
MessageType
Type of message handled by MessageWindow.
@ ButtonUp
Take an action on the currently selected window.
@ ButtonDown
Selects a window based on position.
@ Destroy
Window is being told to destroy itself. This is sent in response to seeing the Close message.
@ Close
Window is asked to close itself, generally sent by the window itself in response to a button up in a ...
void DynamicString_Clear(DynamicString *string)
Clear a DynamicString object, releasing any allocated memory. Resets to an empty string.
Definition: dynamicstring.c:27
void DynamicString_Initialize(DynamicString *string)
Initialize a DynamicString object to an empty string.
Definition: dynamicstring.c:15
bool DynamicString_Append(DynamicString *string, const char *s)
Append the specified string to the DynamicString object.
Definition: dynamicstring.c:39
char * formatstring(const char *format,...)
Use the given string and arguments to return a buffer containing the formatted string....
Definition: formatstring.c:15
Represents a string that can be grown dynamically.
Definition: dynamicstring.h:16
char * string
The string that can grow.
Definition: dynamicstring.h:17
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...
MessageType MessageType
Value from the MessageType enumeration indicating the type of this message.
Position of the message in global coordinates (same scope of coordinates as windows)....