1 #include "win32/ToolBar.h"
2 
3 using namespace Framework::Win32;
4 
CToolBar()5 CToolBar::CToolBar()
6 {
7 
8 }
9 
CToolBar(HWND hParent,unsigned int nBitmapNumber,HINSTANCE hInstance,unsigned int nBitmapId,unsigned int nButtonWidth,unsigned int nButtonHeight)10 CToolBar::CToolBar(HWND hParent, unsigned int nBitmapNumber, HINSTANCE hInstance, unsigned int nBitmapId, unsigned int nButtonWidth, unsigned int nButtonHeight)
11 {
12 	m_hWnd = CreateToolbarEx(hParent, WS_VISIBLE | TBSTYLE_TOOLTIPS, NULL,
13 		nBitmapNumber, hInstance, nBitmapId, NULL, 0, nButtonWidth, nButtonHeight, nButtonWidth, nButtonHeight, sizeof(TBBUTTON));
14 }
15 
CToolBar(HWND parentWnd,DWORD style)16 CToolBar::CToolBar(HWND parentWnd, DWORD style)
17 {
18 	m_hWnd = CreateWindowEx(NULL, TOOLBARCLASSNAME, NULL, WS_VISIBLE | WS_CHILD | style, 0, 0, 1, 1, parentWnd, NULL, GetModuleHandle(NULL), NULL);
19 	SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
20 }
21 
~CToolBar()22 CToolBar::~CToolBar()
23 {
24 
25 }
26 
operator =(CToolBar && rhs)27 CToolBar& CToolBar::operator =(CToolBar&& rhs)
28 {
29 	Reset();
30 	MoveFrom(std::move(rhs));
31 	return (*this);
32 }
33 
Reset()34 void CToolBar::Reset()
35 {
36 	m_buttonToolTips.clear();
37 	CWindow::Reset();
38 }
39 
MoveFrom(CToolBar && rhs)40 void CToolBar::MoveFrom(CToolBar&& rhs)
41 {
42 	m_buttonToolTips = std::move(rhs.m_buttonToolTips);
43 	CWindow::MoveFrom(std::move(rhs));
44 }
45 
InsertImageButton(unsigned int bitmapId,unsigned int commandId)46 void CToolBar::InsertImageButton(unsigned int bitmapId, unsigned int commandId)
47 {
48 	TBBUTTON button;
49 
50 	memset(&button, 0, sizeof(TBBUTTON));
51 	button.iBitmap		= bitmapId;
52 	button.idCommand	= commandId;
53 	button.fsState		= TBSTATE_ENABLED;
54 	button.fsStyle		= TBSTYLE_BUTTON;
55 
56 	SendMessage(m_hWnd, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&button));
57 }
58 
InsertTextButton(const TCHAR * text,unsigned int commandId)59 void CToolBar::InsertTextButton(const TCHAR* text, unsigned int commandId)
60 {
61 	TBBUTTON button;
62 
63 	memset(&button, 0, sizeof(TBBUTTON));
64 	button.iBitmap		= I_IMAGENONE;
65 	button.idCommand	= commandId;
66 	button.fsState		= TBSTATE_ENABLED;
67 	button.fsStyle		= BTNS_BUTTON | BTNS_AUTOSIZE;
68 	button.iString		= reinterpret_cast<INT_PTR>(text);
69 
70 	SendMessage(m_hWnd, TB_ADDBUTTONS, 1, reinterpret_cast<LPARAM>(&button));
71 }
72 
LoadStandardImageList(unsigned int nIndex)73 void CToolBar::LoadStandardImageList(unsigned int nIndex)
74 {
75 	SendMessage(m_hWnd, TB_LOADIMAGES, nIndex, reinterpret_cast<LPARAM>(HINST_COMMCTRL));
76 }
77 
Resize()78 void CToolBar::Resize()
79 {
80 	SendMessage(m_hWnd, TB_AUTOSIZE, 0, 0);
81 }
82 
GetToolTips()83 HWND CToolBar::GetToolTips()
84 {
85 	return reinterpret_cast<HWND>(SendMessage(m_hWnd, TB_GETTOOLTIPS, NULL, NULL));
86 }
87 
SetButtonToolTipText(unsigned int nId,const TCHAR * sText)88 void CToolBar::SetButtonToolTipText(unsigned int nId, const TCHAR* sText)
89 {
90 	m_buttonToolTips[nId] = sText;
91 }
92 
SetButtonChecked(unsigned int id,bool checked)93 void CToolBar::SetButtonChecked(unsigned int id, bool checked)
94 {
95 	TBBUTTONINFO buttonInfo = {};
96 	buttonInfo.cbSize		= sizeof(TBBUTTONINFO);
97 	buttonInfo.idCommand	= id;
98 	buttonInfo.fsState		= TBSTATE_ENABLED | (checked ? TBSTATE_CHECKED : 0);
99 	buttonInfo.dwMask		= TBIF_STATE;
100 
101 	SendMessage(m_hWnd, TB_SETBUTTONINFO, id, reinterpret_cast<LPARAM>(&buttonInfo));
102 }
103 
ProcessNotify(WPARAM wparam,NMHDR * hdr)104 void CToolBar::ProcessNotify(WPARAM wparam, NMHDR* hdr)
105 {
106 	if(hdr->hwndFrom != GetToolTips()) return;
107 
108 	switch(hdr->code)
109 	{
110 	case TTN_GETDISPINFO:
111 		{
112 			LPTOOLTIPTEXT toolTipText(reinterpret_cast<LPTOOLTIPTEXT>(hdr));
113 
114 			auto toolTipIterator = m_buttonToolTips.find(static_cast<unsigned int>(hdr->idFrom));
115 			if(toolTipIterator == m_buttonToolTips.end()) return;
116 
117 			toolTipText->hinst		= GetModuleHandle(NULL);
118 			toolTipText->lpszText	= const_cast<LPWSTR>((*toolTipIterator).second.c_str());
119 		}
120 		break;
121 	}
122 }
123