summaryrefslogtreecommitdiff
path: root/win/Transbar.cpp
blob: 1647cb93250848f560c2dabe11856e41759fdd8f (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
// TransToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "TransBar.h"
#include "BarCmdUI.h"
#include "leocad.h"
#include "piecebar.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTransToolBar

CTransToolBar::CTransToolBar()
{
}

CTransToolBar::~CTransToolBar()
{
}


BEGIN_MESSAGE_MAP(CTransToolBar, CControlBar)
	//{{AFX_MSG_MAP(CTransToolBar)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	ON_UPDATE_COMMAND_UI_RANGE(ID_PIECE_GROUP01, ID_PIECE_GROUP32, OnUpdatePieceGroup)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CTransToolBar message handlers

// Doesn't work if handled by the parent. Why ?
void CTransToolBar::OnUpdatePieceGroup(CCmdUI* pCmdUI)
{
	CPiecesBar* pBar = (CPiecesBar*)GetParent();
	pCmdUI->SetCheck((UINT)(ID_PIECE_GROUP01 + pBar->m_nCurGroup) == pCmdUI->m_nID);
}

/////
//
// CreateTransparentToolbar Function
//     Creates a transparent toolbar control based upon the owner's
//     window handle an incoming style bit field that is assumed
//     to be a combination of TBSTYLE_FLAT and TBSTYLE_LIST flags.
//
// Accepts:
//    HWND: The handle to the parent window.
//    DWORD:  Style values that are included in CreateWindowEx.
//
// Returns:
//    HWND to the newly created toolbar. The owner must resize it.
//
/////
BOOL CTransToolBar::Create(HWND hwndOwner)
{
	TBBUTTON	tbArray[MAX_BUTTONS];
	int			i, iBufferPix;

	// Create the toolbar control.
	if(!CWnd::CreateEx(WS_EX_TOOLWINDOW, TOOLBARCLASSNAME, NULL,
							WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | 
							WS_CLIPSIBLINGS | CCS_NODIVIDER |
							CCS_NOPARENTALIGN | CCS_NORESIZE | m_ToolbarData.dwStyle, 
							0,0,0,0,  // Make it zero, Let owner resize it.
							hwndOwner, (HMENU)m_ToolbarData.idControl,
							NULL)) return FALSE; 
   
	SendMessage(TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
	SendMessage(TB_SETMAXTEXTROWS, 1, 0L);
	SendMessage(TB_SETBITMAPSIZE, 0, (LPARAM)MAKELONG(m_ToolbarData.iButtonCX, m_ToolbarData.iButtonCY));
	SendMessage(TB_SETIMAGELIST, 0, (LPARAM)m_ToolbarData.himl);

	// Loop to fill the array of TBBUTTON structures.
	for(i=0; i < m_ToolbarData.iButtons; i++)
	{
		tbArray[i].iBitmap   = m_ToolbarData.ButtonData[i].iBitmap;
		tbArray[i].idCommand = m_ToolbarData.ButtonData[i].idCommand;
		tbArray[i].fsState   = m_ToolbarData.ButtonData[i].fsState;
		tbArray[i].fsStyle   = m_ToolbarData.ButtonData[i].fsStyle;
		tbArray[i].dwData    = 0;
		tbArray[i].iString   = i;
	}

	// If this is a list style toolbar, add buffer pixels 
	// to make room for button text.
	iBufferPix=LARGEBUTTON_DX;

	// Add the buttons, then set the minimum and maximum button widths.
	SendMessage(TB_ADDBUTTONS, (UINT)m_ToolbarData.iButtons, (LPARAM)tbArray);

	SendMessage(TB_SETBUTTONWIDTH, 0, 
               (LPARAM)MAKELONG(m_ToolbarData.iButtonCX + iBufferPix,
               m_ToolbarData.iButtonCY + iBufferPix));

	return TRUE;

}

void CTransToolBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
{
	CFrameWnd* pMain = (CFrameWnd*)AfxGetMainWnd();

	CFlatOrCoolBarCmdUI state;
	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 buttons state
		TBBUTTON button;
		_GetButton(state.m_nIndex, &button);
		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(pMain, bDisableIfNoHndler);
		}
	}
	// update the dialog controls added to the toolbar
	UpdateDialogControls(pMain, bDisableIfNoHndler);
}

void CTransToolBar::_SetButton(int nIndex, TBBUTTON* pButton)
{
	// get original button state
	TBBUTTON button;
	VERIFY(DefWindowProc(TB_GETBUTTON, nIndex, (LPARAM)&button));

	// prepare for old/new button comparsion
	button.bReserved[0] = 0;
	button.bReserved[1] = 0;
	pButton->fsState ^= TBSTATE_ENABLED;
	pButton->bReserved[0] = 0;
	pButton->bReserved[1] = 0;

	// nothing to do if they are the same
	if (memcmp(pButton, &button, sizeof(TBBUTTON)) != 0)
	{
		// don't redraw everything while setting the button
		DWORD dwStyle = GetStyle();
		ModifyStyle(WS_VISIBLE, 0);
		VERIFY(DefWindowProc(TB_DELETEBUTTON, nIndex, 0));
		VERIFY(DefWindowProc(TB_INSERTBUTTON, nIndex, (LPARAM)pButton));
		ModifyStyle(0, dwStyle & WS_VISIBLE);

		// invalidate appropriate parts
		if (((pButton->fsStyle ^ button.fsStyle) & TBSTYLE_SEP) ||
			((pButton->fsStyle & TBSTYLE_SEP) && pButton->iBitmap != button.iBitmap))
		{
			// changing a separator
			Invalidate(FALSE);
		}
		else
		{
			// invalidate just the button
			CRect rect;
			if (DefWindowProc(TB_GETITEMRECT, nIndex, (LPARAM)&rect))
				InvalidateRect(rect, TRUE);    // erase background
		}
		DefWindowProc(WM_PAINT, (WPARAM)::GetDC(GetSafeHwnd()), (LPARAM)0);
	}
}

void CTransToolBar::_GetButton(int nIndex, TBBUTTON* pButton) const
{
	CTransToolBar* pBar = (CTransToolBar*)this;
	VERIFY(pBar->DefWindowProc(TB_GETBUTTON, nIndex, (LPARAM)pButton));
	pButton->fsState ^= TBSTATE_ENABLED;
}

void CTransToolBar::SetButtonStyle(int nIndex, UINT nStyle)
{
	ASSERT_VALID(this);
	ASSERT(::IsWindow(m_hWnd));

	TBBUTTON button;
	_GetButton(nIndex, &button);
	button.fsStyle = (BYTE)LOWORD(nStyle);
	button.fsState = (BYTE)HIWORD(nStyle);
	_SetButton(nIndex, &button);

	m_bDelayedButtonLayout = TRUE;
}