-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttoolbar.cpp
409 lines (326 loc) · 11.4 KB
/
ttoolbar.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
TTOOLBAR.CPP
Copyright (c) 1994 Shawn Halpenny
All rights reserved.
Source code module for WOLF's TToolBar class.
*/
#ifndef __WOLF_H
#include <wolf.h>
#endif
#ifndef __MEM_H
#include <mem.h>
#endif
#ifndef __STDLIB_H
#include <stdlib.h>
#endif
extern HINSTANCE hLibInst;
HBITMAP TToolBar::DuplicateBitmap(HBITMAP hbmpSrc)
{
HBITMAP hbmpNew;
HDC hdcSrc, hdcDest;
hdcSrc = CreateCompatibleDC(NULL);
hdcDest = CreateCompatibleDC(hdcSrc);
SelectObject(hdcSrc, hbmpSrc);
hbmpNew = CreateCompatibleBitmap(hdcSrc, nWidth, nHeight);
SelectObject(hdcDest, hbmpNew);
BitBlt(hdcDest, 0, 0, nWidth, nHeight, hdcSrc, 0, 0, SRCCOPY);
DeleteDC(hdcDest);
DeleteDC(hdcSrc);
return hbmpNew;
} //TToolBar::DuplicateBitmap
void TToolBar::DrawBitmap(HDC hdc, HBITMAP hbm, DWORD dwRop)
{
HDC hdcMem;
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hbm);
BitBlt(hdc, 0, 0, nWidth, nHeight, hdcMem, 0, 0, dwRop);
DeleteDC(hdcMem);
} //TToolBar::DrawBitmap
TToolBar::TToolBar(PTWindowObject pParent, LPCSTR lpszWindowTitle, PTToolBarInfo pBInfo, int nNumBtns) :
TWindow(pParent, lpszWindowTitle)
{
int i, i2;
BITMAP bmp;
nNumButtons = nNumBtns;
nMaxWidth = GetSystemMetrics(SM_CXSCREEN) / 2;
pInfo = new TToolBarInfo[nNumButtons];
_fmemcpy(pInfo, pBInfo, sizeof(TToolBarInfo) * nNumButtons);
dwStyle = WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_VISIBLE | WS_SYSMENU;
SetFlags(WB_AUTOCREATE | WB_KBHANDLER, TRUE);
SetFlags(WB_TRANSFER | WB_SYSTEMCLASS, FALSE);
WndClass.hbrBackground = (HBRUSH) COLOR_BTNFACE + 1;
WndClass.lpszClassName = "WOLF::TTOOLBAR";
WndClass.style |= CS_NOCLOSE; //can't use close option
hPatBmp = LoadBitmap(hLibInst, "GRAYINGBMP");
SetCursor(LoadCursor(NULL, IDC_WAIT));
for (i=0; i<nNumButtons; i++)
if ((pInfo+i)->nID) //if not a space
{
i2 = i; //keep track of one non-space bmp so we can get its size
//load all the bitmaps for button and store the handles
if (!(pInfo+i)->hBmp[0]) //normal, no focus
(pInfo+i)->hBmp[0] = LoadBitmap(pApplication->hInstance, MAKEINTRESOURCE((pInfo+i)->nID + 1000));
//although this bmp is only for 3-state, LoadBitmap returns 0 if it can't find it,
//so we can load nonexistent bmps with no problems
if (!(pInfo+i)->hBmp[1]) //pressed, no focus
(pInfo+i)->hBmp[1] = LoadBitmap(pApplication->hInstance, MAKEINTRESOURCE((pInfo+i)->nID + 2000));
if (!(pInfo+i)->hBmp[2]) //pressed, focus
(pInfo+i)->hBmp[2] = LoadBitmap(pApplication->hInstance, MAKEINTRESOURCE((pInfo+i)->nID + 3000));
if (!(pInfo+i)->hBmp[3]) //normal, focus
(pInfo+i)->hBmp[3] = LoadBitmap(pApplication->hInstance, MAKEINTRESOURCE((pInfo+i)->nID + 5000));
ConstructButton((pInfo+i)->nID);
}
GetObject((pInfo+i2)->hBmp[0], sizeof(BITMAP), &bmp);
nWidth = bmp.bmWidth;
nHeight = bmp.bmHeight;
cx = cy = 0; //Tool bar window size will be determined in WM_SIZE
bDone = FALSE; //used in WM_SIZE to prevent infinite recursion
SetCursor(LoadCursor(NULL, IDC_ARROW));
} //TToolBar::TToolBar
TToolBar::~TToolBar()
{
int i, j;
for (i=0; i<nNumButtons; i++)
for (j=0; j<4; j++)
if ((pInfo+i)->hBmp[j]) //valid bmp handle?
DeleteObject((pInfo+i)->hBmp[j]);
DeleteObject(hPatBmp);
delete[] pInfo;
} //TToolBar::~TToolBar
void TToolBar::WMSize(RTMessage Msg)
{
int nNewWndWidth, nx, ny, i, nNewCX, nNewCY;
PTPushButton pb;
if (bDone) //prevent infinite recursion for this object
{
bDone = FALSE;
return;
}
nNewWndWidth = Msg.LP.Lo / nWidth * nWidth;
//reposition the buttons. They must be positioned in the order they exist in pInfo,
//which is not necessarily the order in ChildList (if any removals/additions have occurred)
nx = ny = 0;
for (i=0; i<nNumButtons; i++)
{
if (nx >= nNewWndWidth)
{
nx = 0;
ny += nHeight;
}
if ((pInfo+i)->nID) //isn't a space
{
pb = (PTPushButton) GetChild((pInfo+i)->nID);
SetWindowPos(pb->hWindow, 0, nx, ny, nWidth, nHeight, SWP_NOACTIVATE | SWP_NOZORDER);
}
nx += nWidth;
}
bDone = TRUE;
CalcWindowSize(nNewWndWidth, ny + nHeight, nNewCX, nNewCY);
SetWindowPos(hWindow, 0, 0, 0, nNewCX, nNewCY, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
} //TToolBar::WMSize
void TToolBar::CalcWindowSize(int nNewClientWidth, int nNewClientHeight, int& nNewCX, int& nNewCY)
{
nNewCX = nNewClientWidth + GetSystemMetrics(SM_CXFRAME) * 2;
nNewCY = nNewClientHeight + GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME) * 2 - 1;
} //TToolBar::CalcWindowSize
PTPushButton TToolBar::ConstructButton(int nID)
{
PTPushButton pb;
pb = new TPushButton(this, nID);
pb->SetStyle(BS_OWNERDRAW | WS_VISIBLE | WS_TABSTOP, 0);
pb->SetDimensions(0, 0, 0, 0);
return pb;
} //TToolBar::ConstructButton
void TToolBar::WMDrawItem(RTMessage Msg)
{
LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) Msg.lParam;
int i, nBmpOffset;
//determine the TToolBarInfo that goes with the button's ID
for (i=0; i<nNumButtons && (pInfo+i)->nID != lpdis->CtlID; i++);
if ((pInfo+i)->nType == NORMAL)
switch (lpdis->itemAction)
{
case ODA_DRAWENTIRE:
if ((lpdis->itemState & ODS_DISABLED) != 0)
{
HBRUSH hbr = CreatePatternBrush(hPatBmp);
HDC hdcMem = CreateCompatibleDC(lpdis->hDC);
//duplicate bmp because we don't want to modify original when PatBlt'ing
HBITMAP hNewBmp = DuplicateBitmap((pInfo+i)->hBmp[0]);
SelectObject(hdcMem, hNewBmp);
BitBlt(lpdis->hDC, 0, 0, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY); //prepare dest bmp
SelectObject(hdcMem, hbr);
PatBlt(hdcMem, 0, 0, nWidth, nHeight, 0xFA0089L); //gray the bmp
BitBlt(lpdis->hDC, 0, 0, nWidth, nHeight, hdcMem, 0, 0, 0x900C45L); //combine with dest bmp
DeleteDC(hdcMem);
DeleteObject(hbr);
DeleteObject(hNewBmp);
return; //we're using a different ROP code so don't redraw bitmap
}
else
nBmpOffset = 0;
break;
case ODA_SELECT:
if ((lpdis->itemState & ODS_SELECTED) != 0) //push button in
nBmpOffset = 2;
else //button pops out
nBmpOffset = 3;
break;
case ODA_FOCUS:
if ((lpdis->itemState & ODS_FOCUS) == 0)
nBmpOffset = 0;
else
nBmpOffset = 3;
}
else
if ((pInfo+i)->nType == THREESTATE)
switch (lpdis->itemAction)
{
case ODA_DRAWENTIRE:
if ((lpdis->itemState & ODS_DISABLED) != 0)
{
HBRUSH hbr = CreatePatternBrush(hPatBmp);
HDC hdcMem = CreateCompatibleDC(lpdis->hDC);
//duplicate bmp because we don't want to modify original when PatBlt'ing
HBITMAP hNewBmp = DuplicateBitmap((pInfo+i)->hBmp[0]);
SelectObject(hdcMem, hNewBmp);
BitBlt(lpdis->hDC, 0, 0, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY); //prepare dest bmp
SelectObject(hdcMem, hbr);
PatBlt(hdcMem, 0, 0, nWidth, nHeight, 0xFA0089L); //gray the bmp
BitBlt(lpdis->hDC, 0, 0, nWidth, nHeight, hdcMem, 0, 0, 0x900C45L); //combine with dest bmp
DeleteDC(hdcMem);
DeleteObject(hbr);
DeleteObject(hNewBmp);
return; //we're using a different ROP code so don't redraw bitmap
}
else
nBmpOffset = (pInfo+i)->bDown ? 1 : 0;
break;
case ODA_SELECT:
if ((lpdis->itemState & ODS_SELECTED) != 0)
(pInfo+i)->bDown = !(pInfo+i)->bReserved;
else
{
DWORD dw = GetMessagePos();
POINT pt = MAKEPOINT(dw);
ScreenToClient(lpdis->hwndItem, &pt);
#pragma warn -wstv //turn of Structure passed by value warning
if (!PtInRect(&(lpdis->rcItem), pt))
(pInfo+i)->bDown = (pInfo+i)->bReserved;
#pragma warn .wstv
}
nBmpOffset = (pInfo+i)->bDown ? 2 : 3;
break;
case ODA_FOCUS:
(pInfo+i)->bReserved = (pInfo+i)->bDown;
if ((lpdis->itemState & ODS_FOCUS) == 0)
nBmpOffset = (pInfo+i)->bDown ? 1 : 0;
else
nBmpOffset = (pInfo+i)->bDown ? 2 : 3;
};
DrawBitmap(lpdis->hDC, (pInfo+i)->hBmp[nBmpOffset], SRCCOPY);
} //TToolBar::WMDrawItem
void TToolBar::WMCommand(RTMessage Msg)
{
int i;
TWindow::WMCommand(Msg);
if (Msg.LP.Lo) //msg is from a control (assumably a button)
{
//Determine TToolBarInfo that goes with the button
for (i=0; i<nNumButtons && (pInfo+i)->nID != Msg.wParam; i++);
(pInfo+i)->bReserved = (pInfo+i)->bDown;
}
} //TToolBar::WMCommand
void TToolBar::WMGetMinMaxInfo(RTMessage Msg)
{
MINMAXINFO FAR *lpMinMax = (MINMAXINFO FAR *) Msg.lParam; //always FAR ptrs
lpMinMax->ptMinTrackSize.x = max(nWidth + GetSystemMetrics(SM_CXFRAME) * 2, GetSystemMetrics(SM_CXMINTRACK));
lpMinMax->ptMaxTrackSize.x = nMaxWidth;
} //TToolBar::WMGetMinMaxInfo
void TToolBar::AddButton(PTToolBarInfo pNewBtnInfo, int nNewBtnPos)
{
PTToolBarInfo pNewBarInfo, pCur;
PTPushButton pb;
nNumButtons++;
pCur = pNewBarInfo = new TToolBarInfo[nNumButtons];
//copy info before new button position
_fmemcpy(pCur, pInfo, sizeof(TToolBarInfo) * nNewBtnPos);
pCur += nNewBtnPos;
//copy new button info
_fmemcpy(pCur, pNewBtnInfo, sizeof(TToolBarInfo));
pCur++;
//copy rest of button info
_fmemcpy(pCur, pInfo+nNewBtnPos, sizeof(TToolBarInfo) * (nNumButtons - 1 - nNewBtnPos));
delete[] pInfo; //kill old info
pInfo = pNewBarInfo;
//make WOLF object and interface object for new button
pb = new TPushButton(this, (pInfo+nNewBtnPos)->nID);
pb->SetStyle(BS_OWNERDRAW | WS_VISIBLE | WS_TABSTOP, 0);
pb->SetDimensions(0, 0, 0, 0);
pb->Create();
//force resize and redraw of toolbar
SetWindowPos(hWindow, 0, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
} //TToolBar::AddButton
void TToolBar::RemoveButton(int nPos)
{
PTToolBarInfo pNewBarInfo, pCur;
PTPushButton pb = (PTPushButton) GetChild((pInfo+nPos)->nID);
for (int i=0; i<4; i++)
if ((pInfo+nPos)->hBmp[i]) //valid bmp handle?
DeleteObject((pInfo+nPos)->hBmp[i]);
nNumButtons--;
pCur = pNewBarInfo = new TToolBarInfo[nNumButtons];
//copy info before button position
_fmemcpy(pCur, pInfo, sizeof(TToolBarInfo) * nPos);
pCur += nPos;
//copy rest of button info after button we're deleting
_fmemcpy(pCur, pInfo+nPos+1, sizeof(TToolBarInfo) * (nNumButtons - nPos));
delete[] pInfo; //kill old info
pInfo = pNewBarInfo;
//destroy the interface object
SendMessage(pb->hWindow, WM_CLOSE, 0, 0);
//destroy the WOLF object
delete pb;
//force resize and redraw of toolbar
SetWindowPos(hWindow, 0, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
} //TToolBar::RemoveButton
void TToolBar::SetState(int nID, BOOL bDown)
{
PTToolBarInfo pCur = pInfo + GetPos(nID);
pCur->bDown = bDown;
if (hWindow)
{
InvalidateRect(GetChild(nID)->hWindow, NULL, FALSE);
UpdateWindow(GetChild(nID)->hWindow);
}
} //TToolBar::SetState
int TToolBar::GetPos(int nID)
{
for (int i=0; i<nNumButtons; i++)
if ((pInfo+i)->nID == nID)
return i;
return 32767; //something large enough to probably cause a GP fault,
//which is fine, because if the ID isn't found, we show an error
} //TToolBar::GetPos
void TToolBar::SetType(int nID, ToolBarBtnType Type)
{
PTToolBarInfo pCur = pInfo + GetPos(nID);
pCur->nType = Type;
if (hWindow)
{
InvalidateRect(GetChild(nID)->hWindow, NULL, FALSE);
UpdateWindow(GetChild(nID)->hWindow);
}
} //TToolBar::SetType
void TToolBar::SetBitmap(int nID, int nWhichHandle, HBITMAP hBmp)
{
PTToolBarInfo pCur = pInfo + GetPos(nID);
if (pCur->hBmp[nWhichHandle])
DeleteObject(pCur->hBmp[nWhichHandle]);
pCur->hBmp[nWhichHandle] = hBmp;
if (hWindow)
{
InvalidateRect(GetChild(nID)->hWindow, NULL, FALSE);
UpdateWindow(GetChild(nID)->hWindow);
}
} //TToolBar::SetBitmap