-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlatBar.cpp
235 lines (206 loc) · 6.94 KB
/
FlatBar.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
////////////////////////////////////////////////////////////////
// CFlatToolBar 1997 Microsoft Systems Journal.
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
#include "StdAfx.h"
#include "FlatBar.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////
// CFlatToolBar--does flat tool bar in MFC.
//
IMPLEMENT_DYNAMIC(CFlatToolBar, CToolBar)
BEGIN_MESSAGE_MAP(CFlatToolBar, CToolBar)
ON_WM_WINDOWPOSCHANGING()
ON_WM_WINDOWPOSCHANGED()
END_MESSAGE_MAP()
////////////////
// Load override modifies the style after loading toolbar.
//
BOOL CFlatToolBar::LoadToolBar(LPCTSTR lpszResourceName)
{
if (!CToolBar::LoadToolBar(lpszResourceName))
return FALSE;
ModifyStyle(0, TBSTYLE_FLAT); // make it flat
return TRUE;
}
//#define ILLUSTRATE_DISPLAY_BUG // remove comment to see the bug
//////////////////
// MFC doesn't handle moving a TBSTYLE_FLAT toolbar correctly.
// The simplest way to fix it is to repaint the old rectangle and
// toolbar itself whenever the toolbar moves.
//
void CFlatToolBar::OnWindowPosChanging(LPWINDOWPOS lpwp)
{
CToolBar::OnWindowPosChanging(lpwp);
#ifndef ILLUSTRATE_DISPLAY_BUG
if (!(lpwp->flags & SWP_NOMOVE)) // if moved:
{
CRect rc; // Fill rectangle with..
GetWindowRect(&rc); // ..my (toolbar) rectangle.
CWnd* pParent = GetParent(); // get parent (dock bar/frame) win..
pParent->ScreenToClient(&rc); // .. and convert to parent coords
// Ask parent window to paint the area beneath my old location.
// Typically, this is just solid grey. The area won't get painted until
// I send WM_NCPAINT after the move, in OnWindowPosChanged below.
//
pParent->InvalidateRect(&rc); // paint old rectangle
}
#endif
}
//////////////////
// Now toolbar has moved: repaint old area
//
void CFlatToolBar::OnWindowPosChanged(LPWINDOWPOS lpwp)
{
CToolBar::OnWindowPosChanged(lpwp);
#ifndef ILLUSTRATE_DISPLAY_BUG
if (!(lpwp->flags & SWP_NOMOVE)) // if moved:
{
// Now paint my non-client area at the new location.
// This is the extra bit of border space surrounding the buttons.
// Without this, you will still have a partial display bug (try it!)
//
SendMessage(WM_NCPAINT);
}
#endif
}
////////////////////////////////////////////////////////////////
// The following stuff is to make the command update UI mechanism
// work properly for flat tool bars. The main idea is to convert
// a "checked" button state into a "pressed" button state. Changed
// lines marked with "PD"
////////////////
// The following class was copied from BARTOOL.CPP in the MFC source.
// All I changed was SetCheck--PD.
//
class CFlatOrCoolBarCmdUI : public CCmdUI // class private to this file !
{
public: // re-implementations only
virtual void Enable(BOOL bOn);
virtual void SetCheck(int nCheck);
virtual void SetText(LPCTSTR lpszText);
};
void CFlatOrCoolBarCmdUI::Enable(BOOL bOn)
{
m_bEnableChanged = TRUE;
CToolBar* pToolBar = (CToolBar*)m_pOther;
ASSERT(pToolBar != NULL);
ASSERT_KINDOF(CToolBar, pToolBar);
ASSERT(m_nIndex < m_nIndexMax);
TBBUTTON btn;
pToolBar->GetToolBarCtrl().GetButton(m_nIndex, &btn);
BYTE byNewStyle= bOn ? btn.fsState | TBSTATE_ENABLED : btn.fsState & ~TBSTATE_ENABLED;
if (byNewStyle != btn.fsState)
{
pToolBar->GetToolBarCtrl().EnableButton(btn.idCommand, bOn);
if (!bOn)
pToolBar->GetToolBarCtrl().Indeterminate(btn.idCommand, false);
}
}
// Take your pick:
//#define MYTBBS_CHECKED TBBS_CHECKED // use "checked" state
//#define MYTBBS_CHECKED TBBS_PRESSED // use pressed state
//#define MYTBBS_CHECKED (TBBS_PRESSED | TBBS_CHECKED)
//////////////////
// This is the only function that has changed: instead of TBBS_CHECKED,
// I use TBBS_PRESSED--PD
//
void CFlatOrCoolBarCmdUI::SetCheck(int nCheck)
{
ASSERT(nCheck >= 0 && nCheck <= 2); // 0=>off, 1=>on, 2=>indeterminate
CToolBar* pToolBar = (CToolBar*)m_pOther;
ASSERT(pToolBar != NULL);
ASSERT_KINDOF(CToolBar, pToolBar);
ASSERT(m_nIndex < m_nIndexMax);
TBBUTTON btn;
pToolBar->GetToolBarCtrl().GetButton(m_nIndex, &btn);
BYTE byNewStyle= btn.fsState;
switch (nCheck)
{
case 0: // off
byNewStyle &= ~(TBSTATE_CHECKED);
break;
case 1: // on
byNewStyle |= TBSTATE_CHECKED;
break;
case 2: // indeterminate
byNewStyle |= TBSTATE_INDETERMINATE;
break;
}
if (byNewStyle != btn.fsState)
pToolBar->GetToolBarCtrl().SetState(btn.idCommand, byNewStyle);
/* if (nCheck != 2)
{
btn.fsState &= ~TBSTATE_INDETERMINATE;
pToolBar->GetToolBarCtrl().SetState(btn.idCommand,
nCheck == 0 ? btn.fsState & ~(TBSTATE_PRESSED | TBSTATE_CHECKED)
: btn.fsState | TBSTATE_PRESSED | TBSTATE_CHECKED);
}
else
{
if ((btn.fsState & TBSTATE_INDETERMINATE) == 0)
{
pToolBar->GetToolBarCtrl().SetState(btn.idCommand,
(btn.fsState & ~(TBSTATE_PRESSED | TBSTATE_CHECKED)) | TBSTATE_INDETERMINATE);
}
} */
/*
UINT nOldStyle = pToolBar->GetButtonStyle(m_nIndex); // PD
UINT nNewStyle = nOldStyle &
~(MYTBBS_CHECKED | TBBS_INDETERMINATE); // PD
if (nCheck == 1)
nNewStyle |= MYTBBS_CHECKED; // PD
else if (nCheck == 2)
nNewStyle |= TBBS_INDETERMINATE;
// Following is to fix display bug for TBBS_CHECKED:
// If new state is unchecked, repaint--but only if style actually changing.
// (Otherwise will end up with flicker)
//
if (nNewStyle != nOldStyle)
{
ASSERT(!(nNewStyle & TBBS_SEPARATOR));
pToolBar->SetButtonStyle(m_nIndex, nNewStyle);
pToolBar->Invalidate();
}
*/
}
void CFlatOrCoolBarCmdUI::SetText(LPCTSTR)
{
// ignore for now, but you should really set the text
}
//////////////////
// This function is mostly copied from CToolBar/BARTOOL.CPP. The only thing
// that's different is I instantiated a CFlatOrCoolBarCmdUI instead of
// CToolCmdUI.
//
void CFlatToolBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
CFlatOrCoolBarCmdUI state; // <<<< This is the only line that's different--PD
state.m_pOther = this;
state.m_nIndexMax = (UINT)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++)
{
// get button state
TBBUTTON button;
VERIFY(DefWindowProc(TB_GETBUTTON, state.m_nIndex, (LPARAM)&button));
// TBSTATE_ENABLED == TBBS_DISABLED so invert it
button.fsState ^= TBSTATE_ENABLED;
state.m_nID = button.idCommand;
// ignore separators
if (!(button.fsStyle & TBSTYLE_SEP))
{
// allow the toolbar itself to have update handlers
if (CWnd::OnCmdMsg(state.m_nID, CN_UPDATE_COMMAND_UI, &state, NULL))
continue;
// allow the owner to process the update
state.DoUpdate(pTarget, bDisableIfNoHndler);
}
}
// update the dialog controls added to the toolbar
UpdateDialogControls(pTarget, bDisableIfNoHndler);
}