summaryrefslogtreecommitdiffhomepage
path: root/X-Plane-SDK/CHeaders/Wrappers/XPCWidgetAttachments.cpp
blob: d87f105cfc6ac64503bdb8835dd7d31f5e32b52f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "XPCWidgetAttachments.h"
#include "XPStandardWidgets.h"
#include "XPWidgetUtils.h"

static	void	XPCGetOrderedSubWidgets(
							XPWidgetID						inWidget,
							std::vector<XPWidgetID>&		outChildren);					

XPCKeyFilterAttachment::XPCKeyFilterAttachment(
								const char *	inValidKeys,
								const char *	outValidKeys) :
	mInput(inValidKeys),
	mOutput(outValidKeys)
{
}								
								
XPCKeyFilterAttachment::~XPCKeyFilterAttachment()
{
}

int		XPCKeyFilterAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if (inMessage == xpMsg_KeyPress)
	{
		char&	theKey = KEY_CHAR(inParam1);
		std::string::size_type pos = mInput.find(theKey);
		if (pos == std::string::npos)
			return 1;	// Not found; eat the key!
		else {
			theKey = mOutput[pos];
			return 0;
		}	// Let it live.
	}
	return 0;
}								


XPCKeyMessageAttachment::XPCKeyMessageAttachment(
								char			inKey,
								int				inMessage,
								void *			inParam,
								bool			inConsume,
								bool			inVkey,
								XPCListener *	inListener) :
	mKey(inKey), mMsg(inMessage), mParam(inParam), mConsume(inConsume),
	mVkey(inVkey)
{
	if (inListener != NULL)
		this->AddListener(inListener);
}	

XPCKeyMessageAttachment::~XPCKeyMessageAttachment()
{
}
									
int		XPCKeyMessageAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if (inMessage == xpMsg_KeyPress)
	{
		char theKey = mVkey ? KEY_VKEY(inParam1) : KEY_CHAR(inParam1);
		if (theKey != mKey)
			return 0;
		if (!(KEY_FLAGS(inParam1) & xplm_DownFlag))
			return 0;
		
		BroadcastMessage(mMsg, mParam);
		return mConsume ? 1 : 0;
	}
	return 0;
}								

XPCPushButtonMessageAttachment::XPCPushButtonMessageAttachment(
									XPWidgetID		inWidget,
									int				inMessage,
									void *			inParam,
									XPCListener *	inListener) :
	mMsg(inMessage), mParam(inParam), mWidget(inWidget)
{
	if (inListener != NULL)
		this->AddListener(inListener);
}

XPCPushButtonMessageAttachment::~XPCPushButtonMessageAttachment()
{
}

int		XPCPushButtonMessageAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if ((inMessage == xpMsg_PushButtonPressed) && ((XPWidgetID) inParam1 == mWidget))
	{
		BroadcastMessage(mMsg, mParam);
		return 1;
	}

	if ((inMessage == xpMsg_ButtonStateChanged) && ((XPWidgetID) inParam1 == mWidget))
	{
		BroadcastMessage(mMsg, mParam);
		return 1;
	}
	return 0;	
}					

XPCSliderMessageAttachment::XPCSliderMessageAttachment(
									XPWidgetID		inWidget,
									int				inMessage,
									void *			inParam,
									XPCListener *	inListener) :
	mMsg(inMessage), mParam(inParam), mWidget(inWidget)
{
	if (inListener != NULL)
		this->AddListener(inListener);
}

XPCSliderMessageAttachment::~XPCSliderMessageAttachment()
{
}

int		XPCSliderMessageAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if ((inMessage == xpMsg_ScrollBarSliderPositionChanged) && ((XPWidgetID) inParam1 == mWidget))
	{
		BroadcastMessage(mMsg, mParam);
		return 1;
	}

	return 0;	
}									


XPCCloseButtonMessageAttachment::XPCCloseButtonMessageAttachment(
									XPWidgetID		inWidget,
									int				inMessage,
									void *			inParam,
									XPCListener *	inListener) :
	mMsg(inMessage), mParam(inParam), mWidget(inWidget)
{
	if (inListener != NULL)
		this->AddListener(inListener);
}

XPCCloseButtonMessageAttachment::~XPCCloseButtonMessageAttachment()
{
}

int		XPCCloseButtonMessageAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if ((inMessage == xpMessage_CloseButtonPushed) && ((XPWidgetID) inParam1 == mWidget))
	{
		BroadcastMessage(mMsg, mParam);
		return 1;
	}

	return 0;	
}									

XPCTabGroupAttachment::XPCTabGroupAttachment()
{
}

XPCTabGroupAttachment::~XPCTabGroupAttachment()
{
}

int		XPCTabGroupAttachment::HandleWidgetMessage(
								XPCWidget *		inObject,
								XPWidgetMessage	inMessage,
								XPWidgetID		inWidget,
								intptr_t		inParam1,
								intptr_t		inParam2)
{
	if ((inMessage == xpMsg_KeyPress) && (KEY_CHAR(inParam1) == XPLM_KEY_TAB) &&
		((KEY_FLAGS(inParam1) & xplm_UpFlag) == 0))
	{
		bool backwards = (KEY_FLAGS(inParam1) & xplm_ShiftFlag) != 0;
		std::vector<XPWidgetID>	widgets;
		XPCGetOrderedSubWidgets(inWidget, widgets);
		int	n, index = 0;
		XPWidgetID	focusWidget = XPGetWidgetWithFocus();
		std::vector<XPWidgetID>::iterator iter = std::find(widgets.begin(), widgets.end(), focusWidget);
		if (iter != widgets.end())
		{
			index = std::distance(widgets.begin(), iter);
			if (backwards)
				index--;
			else
				index++;
			if (index < 0)
				index = widgets.size() - 1;
			if (index >= widgets.size())
				index = 0;
		}
		
		if (backwards)
		{
			for (n = index; n >= 0; --n)
			{
				if (XPGetWidgetProperty(widgets[n], xpProperty_Enabled, NULL))
				if (XPSetKeyboardFocus(widgets[n]) != NULL)
					return 1;
			}
			for (n = widgets.size() - 1; n > index; --n)
			{
				if (XPGetWidgetProperty(widgets[n], xpProperty_Enabled, NULL))
				if (XPSetKeyboardFocus(widgets[n]) != NULL)
					return 1;
			}				
		} else {
			for (n = index; n < widgets.size(); ++n)
			{
				if (XPGetWidgetProperty(widgets[n], xpProperty_Enabled, NULL))
				if (XPSetKeyboardFocus(widgets[n]) != NULL)
					return 1;
			}
			for (n = 0; n < index; ++n)
			{
				if (XPGetWidgetProperty(widgets[n], xpProperty_Enabled, NULL))
				if (XPSetKeyboardFocus(widgets[n]) != NULL)
					return 1;
			}				
		}
	} 
	return 0;
}								



static	void	XPCGetOrderedSubWidgets(
							XPWidgetID						inWidget,
							std::vector<XPWidgetID>&		outChildren)
{
	outChildren.clear();
	int	count = XPCountChildWidgets(inWidget);
	for (int n = 0; n < count; ++n)
	{
		XPWidgetID	child = XPGetNthChildWidget(inWidget, n);
		outChildren.push_back(child);
		std::vector<XPWidgetID>	grandChildren;
		XPCGetOrderedSubWidgets(child, grandChildren);
		
		outChildren.insert(outChildren.end(), grandChildren.begin(), grandChildren.end());
	}
}