-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonTest.CPP
207 lines (189 loc) · 8.67 KB
/
ButtonTest.CPP
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
// Test program for the shaded button class
#define INCL_WIN
#define INCL_GPI
#include <os2.h>
#include "Buttons.H"
#include "ButtonTest.H"
typedef union {
ULONG Index;
struct {
UCHAR blue,green,red;
UCHAR dummy;
} rgb;
} CLR;
MRESULT EXPENTRY ClientWndProc(HWND, ULONG, MPARAM, MPARAM);
MRESULT EXPENTRY AboutDlgProc(HWND, ULONG, MPARAM, MPARAM);
int main() {
static UCHAR szClientClass[] = "ButtonTest";
static ULONG flFrameFlags= FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER | FCF_MINMAX | FCF_SHELLPOSITION | FCF_TASKLIST;
HAB hab;
HMQ hmq;
HWND hWndFrame, hWndClient;
QMSG qmsg;
hab = WinInitialize(0);
hmq = WinCreateMsgQueue(hab, 0);
RegisterShadedButtonClass();
WinRegisterClass(hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0);
hWndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flFrameFlags, szClientClass, NULL, 0L, 0, 0, &hWndClient);
while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
WinDispatchMsg(hab, &qmsg);
WinDestroyWindow(hWndFrame);
WinDestroyMsgQueue(hmq);
WinTerminate(hab);
return 0;
}
MRESULT EXPENTRY ClientWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
static HWND hwndFrame, hwndButton[10];
static INT cxClient, cyClient, cyChar;
static LONG ButtonWidth, ButtonHeight;
static CLR buttonColour;
CHAR szBuffer[32];
FONTMETRICS fm;
HAB hab;
HPS hps;
INT id;
RECTL rcl;
switch(msg) {
case WM_CREATE:
hab = WinQueryAnchorBlock(hwnd);
hwndFrame = WinQueryWindow(hwnd, QW_PARENT);
hps = WinGetPS(hwnd);
GpiQueryFontMetrics(hps, sizeof(fm),&fm);
cyChar = fm.lMaxBaselineExt;
WinReleasePS(hps);
buttonColour.Index = 0x00CCCCCC;
// Buttons should be big enough to hold the pointer, with a 4 pixel border
ButtonWidth = WinQuerySysValue(HWND_DESKTOP, SV_CXPOINTER) + 4;
ButtonHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYPOINTER) + 4;
hwndButton[0] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "100", WS_VISIBLE | GBT_NORIGHTLINE | GBT_GRAPHIC, 0, 0, ButtonWidth, ButtonHeight, hwnd, HWND_BOTTOM, 0, NULL, NULL);
hwndButton[1] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "101", WS_VISIBLE | GBT_NOLEFTLINE | GBT_GRAPHIC, 0, 0, ButtonWidth, ButtonHeight, hwnd, HWND_BOTTOM, 1, NULL, NULL);
// This is the status bar
hwndButton[2] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "Status Bar", WS_VISIBLE | GBT_NOPUSH, 0, 0, 0, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 2, NULL, NULL);
sprintf(szBuffer, "Red: %d Green: %d Blue: %d", buttonColour.rgb.red, buttonColour.rgb.green, buttonColour.rgb.blue);
WinSetWindowText(hwndButton[2], szBuffer);
// Buttons to change the colour of the about button
hwndButton[3] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 3, NULL, NULL);
hwndButton[4] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 4, NULL, NULL);
hwndButton[5] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 5, NULL, NULL);
hwndButton[6] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 6, NULL, NULL);
hwndButton[7] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 7, NULL, NULL);
hwndButton[8] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 8, NULL, NULL);
// info about this program
hwndButton[9] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "About", WS_VISIBLE | GBT_RIGHTROUND | GBT_LEFTROUND, 0, 0, 2 * ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 9, NULL, NULL);
// change the colour of the buttons
WinSendMsg(hwndButton[3], GBM_SETCOLOR, MPFROMLONG(0x00FFCCCC), NULL);
WinSendMsg(hwndButton[4], GBM_SETCOLOR, MPFROMLONG(0x00FFCCCC), NULL);
WinSendMsg(hwndButton[5], GBM_SETCOLOR, MPFROMLONG(0x00CCFFCC), NULL);
WinSendMsg(hwndButton[6], GBM_SETCOLOR, MPFROMLONG(0x00CCFFCC), NULL);
WinSendMsg(hwndButton[7], GBM_SETCOLOR, MPFROMLONG(0x00CCCCFF), NULL);
WinSendMsg(hwndButton[8], GBM_SETCOLOR, MPFROMLONG(0x00CCCCFF), NULL);
WinSendMsg(hwndButton[9], GBM_SETCOLOR, MPFROMLONG(buttonColour.Index), NULL);
return 0;
case WM_SIZE:
hps = WinGetPS(hwnd);
GpiQueryFontMetrics(hps, sizeof(fm),&fm);
cyChar = fm.lMaxBaselineExt;
WinReleasePS(hps);
cxClient = SHORT1FROMMP(mp2);
cyClient = SHORT2FROMMP(mp2);
// Center the buttons
for(id = 0; id < 2; id++)
WinSetWindowPos(hwndButton[id], NULLHANDLE, (cxClient >> 1) + ButtonWidth * (id - 1), (cyClient - ButtonHeight) >> 1, 0, 0, SWP_MOVE);
for(id = 3; id < 9; id++)
WinSetWindowPos(hwndButton[id], NULLHANDLE, (cxClient >> 1) + ButtonWidth * (-(id % 2)), ((cyClient + ButtonHeight) >> 1) + (3 - (id-1) / 2) * cyChar * 5 / 4, 0, 0, SWP_MOVE);
WinSetWindowPos(hwndButton[9], NULLHANDLE, (cxClient >> 1) - ButtonWidth, ((cyClient - ButtonHeight) >> 1) - cyChar * 15 / 8, 0, 0, SWP_MOVE);
// make the status bar span the width of the window
WinSetWindowPos(hwndButton[2], NULLHANDLE, 0, 0, cxClient, cyChar * 5 / 4, SWP_SIZE);
// Enable "Smaller" button if window is large enough for the buttons
if (!WinIsWindowEnabled(hwndButton[0]) && ((9 * cxClient / 10) >= (2 * ButtonWidth)) && ((9 * cyClient / 10) >= (ButtonHeight + cyChar * 15 / 2)))
WinEnableWindow(hwndButton[0], TRUE);
// Disable "Smaller" button if window is too small for the buttons
if (WinIsWindowEnabled(hwndButton[0]) && (((9 * cxClient / 10) < (2 * ButtonWidth)) || ((9 * cyClient / 10) < (ButtonHeight + cyChar * 15 / 2))))
WinEnableWindow(hwndButton[0], FALSE);
return 0;
case WM_COMMAND:
WinQueryWindowRect(hwnd, &rcl);
WinMapWindowPoints(hwnd, HWND_DESKTOP, (PPOINTL) &rcl, 2);
switch(COMMANDMSG(&msg)->cmd) {
case 0: // "Smaller"
rcl.xLeft += cxClient / 20;
rcl.xRight -= cxClient / 20;
rcl.yBottom += cyClient / 20;
rcl.yTop -= cyClient / 20;
break;
case 1: // "Larger"
rcl.xLeft -= cxClient / 20;
rcl.xRight += cxClient / 20;
rcl.yBottom -= cyClient / 20;
rcl.yTop += cyClient / 20;
break;
case 3: // change red
buttonColour.rgb.red--;
if (buttonColour.rgb.red == 0)
WinEnableWindow(hwndButton[3], FALSE);
if (!WinIsWindowEnabled(hwndButton[4]))
WinEnableWindow(hwndButton[4], TRUE);
break;
case 4:
buttonColour.rgb.red++;
if (buttonColour.rgb.red == 255)
WinEnableWindow(hwndButton[4], FALSE);
if (!WinIsWindowEnabled(hwndButton[3]))
WinEnableWindow(hwndButton[3], TRUE);
break;
case 5: // change green
buttonColour.rgb.green--;
if (buttonColour.rgb.green == 0)
WinEnableWindow(hwndButton[5], FALSE);
if (!WinIsWindowEnabled(hwndButton[6]))
WinEnableWindow(hwndButton[6], TRUE);
break;
case 6:
buttonColour.rgb.green++;
if (buttonColour.rgb.green == 255)
WinEnableWindow(hwndButton[6], FALSE);
if (!WinIsWindowEnabled(hwndButton[5]))
WinEnableWindow(hwndButton[5], TRUE);
break;
case 7:// change blue
buttonColour.rgb.blue--;
if (buttonColour.rgb.blue == 0)
WinEnableWindow(hwndButton[7], FALSE);
if (!WinIsWindowEnabled(hwndButton[8]))
WinEnableWindow(hwndButton[8], TRUE);
break;
case 8:
buttonColour.rgb.blue++;
if (buttonColour.rgb.blue == 255)
WinEnableWindow(hwndButton[8], FALSE);
if (!WinIsWindowEnabled(hwndButton[7]))
WinEnableWindow(hwndButton[7], TRUE);
break;
case 9:
WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc, NULLHANDLE, IDD_ABOUT, NULL);
return 0;
}
sprintf(szBuffer, "Red: %d Green: %d Blue: %d", buttonColour.rgb.red, buttonColour.rgb.green, buttonColour.rgb.blue);
WinSetWindowText(hwndButton[2], szBuffer);
WinCalcFrameRect(hwndFrame, &rcl, FALSE);
WinSetWindowPos(hwndFrame, NULLHANDLE, rcl.xLeft, rcl.yBottom, rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom, SWP_MOVE | SWP_SIZE);
WinPostMsg(hwndButton[9], GBM_SETCOLOR, MPFROMLONG(buttonColour.Index), NULL);
return 0;
case WM_ERASEBACKGROUND:
WinFillRect((HPS)LONGFROMMP(mp1), PVOIDFROMMP(mp2), CLR_PALEGRAY);
return 0;
}
return WinDefWindowProc(hwnd, msg, mp1, mp2);
}
MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
switch(msg) {
case WM_COMMAND:
switch(COMMANDMSG(&msg)->cmd) {
case DID_OK:
WinDismissDlg(hwnd, TRUE);
return 0;
}
break;
}
return WinDefDlgProc(hwnd, msg, mp1, mp2);
}