tagging release
[dasher.git] / trunk / Src / Win32 / Widgets / Toolbar.cpp
blob885677a4a972178df258cf433f73946a9db73d12
1 // Toolbar.cpp
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2002 Iain Murray, Inference Group, Cavendish, Cambridge.
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 // TODO: Prime candidate for ATL
11 #include "WinCommon.h"
13 #include "Toolbar.h"
14 #include "../resource.h"
16 // Track memory leaks on Windows to the line that new'd the memory
17 #ifdef _WIN32
18 #ifdef _DEBUG
19 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
20 #define new DEBUG_NEW
21 #undef THIS_FILE
22 static char THIS_FILE[] = __FILE__;
23 #endif
24 #endif
26 struct SToolbarButton {
27 int iBitmap;
28 int iString;
29 int iStringID;
30 int iCommand;
33 SToolbarButton sButtons[] = {
34 {STD_FILENEW, 0, IDS_FILE_NEW, ID_FILE_NEW},
35 {STD_FILEOPEN, 1, IDS_FILE_OPEN, ID_FILE_OPEN},
36 {STD_FILESAVE, 2, IDS_FILE_SAVE, ID_FILE_SAVE},
37 {-1, -1, 0, 0},
38 {STD_CUT, 3, IDS_EDIT_CUT, ID_EDIT_CUT},
39 {STD_COPY, 4, IDS_EDIT_COPY, ID_EDIT_COPY},
40 {-2, 5, IDS_EDIT_COPY_ALL, ID_EDIT_COPY_ALL},
41 {STD_PASTE, 6, IDS_EDIT_PASTE, ID_EDIT_PASTE}
44 CToolbar::CToolbar(HWND hParent, bool bVisible) {
45 m_hwnd = 0;
46 m_hRebar = 0;
48 m_hParent = hParent;
50 if(bVisible)
51 CreateToolbar();
54 void CToolbar::Resize() {
55 SendMessage(m_hRebar, WM_SIZE, 0, 0);
58 void CToolbar::ShowToolbar(bool bValue) {
59 if(m_hwnd!=0)
60 DestroyToolbar();
61 if(bValue)
62 CreateToolbar();
65 void CToolbar::CreateToolbar() {
66 WinHelper::InitCommonControlLib();
69 m_hRebar = CreateWindowEx(WS_EX_TOOLWINDOW,
70 REBARCLASSNAME,
71 NULL,
72 WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|
73 WS_CLIPCHILDREN|RBS_VARHEIGHT,
74 0,0,0,0,
75 m_hParent, NULL, WinHelper::hInstApp,
76 NULL);
78 REBARINFO rbi;
79 rbi.cbSize = sizeof(REBARINFO);
80 rbi.fMask = 0;
81 rbi.himl = (HIMAGELIST)NULL;
82 SendMessage(m_hRebar, RB_SETBARINFO, 0, (LPARAM)&rbi);
84 REBARBANDINFO rbBand;
85 rbBand.cbSize = sizeof(REBARBANDINFO);
86 rbBand.fMask = RBBIM_STYLE | RBBIM_CHILD | RBBIM_CHILDSIZE;
87 rbBand.fStyle = RBBS_CHILDEDGE | RBBS_FIXEDBMP | RBBS_GRIPPERALWAYS | RBBS_USECHEVRON;
89 // Create Toolbar
90 #ifdef OriginalWin95
91 m_hwnd = CreateWindow(TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, m_hRebar, NULL, WinHelper::hInstApp, NULL);
92 #else
93 //Unless I'm going for ultra-compatibility, display a nice flat-style toolbar
94 m_hwnd = CreateWindow(TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | CCS_NODIVIDER | CCS_NORESIZE | CCS_NOPARENTALIGN, 0, 0, 0, 0, m_hRebar, NULL, WinHelper::hInstApp, NULL);
95 #endif
97 // Allows system to work with any version of common controls library
98 SendMessage(m_hwnd, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
100 // Get Standard toolbar bitmaps.
101 TBADDBITMAP bitmaps;
102 bitmaps.hInst = HINST_COMMCTRL;
103 bitmaps.nID = IDB_STD_LARGE_COLOR;
104 SendMessage(m_hwnd, TB_ADDBITMAP, 0, (LPARAM) & bitmaps);
106 // Get Non-standard Copy-All bitmap
107 bitmaps.hInst = WinHelper::hInstApp;
108 bitmaps.nID = IDB_COPY_ALL_LARGE_COLOR;
109 const int COPY_ALL_INDEX = SendMessage(m_hwnd, TB_ADDBITMAP, 0, (LPARAM) & bitmaps);
111 Tstring AllButtons;
112 Tstring CurButton;
114 int iNumButtons(sizeof(sButtons)/sizeof(SToolbarButton));
116 for(int i(0); i < iNumButtons; ++i) {
117 if(sButtons[i].iBitmap != -1) {
118 WinLocalisation::GetResourceString(sButtons[i].iStringID, &CurButton);
119 AllButtons += CurButton + TEXT('\0');
123 const TCHAR *szButtontext = AllButtons.c_str();
124 SendMessage(m_hwnd, TB_ADDSTRING, 0, (DWORD) szButtontext);
126 // TODO: Should do tooltips
128 TBBUTTON *pButtons(new TBBUTTON[iNumButtons]);
130 for(int i(0); i < iNumButtons; ++i) {
131 if(sButtons[i].iBitmap == -2)
132 pButtons[i].iBitmap = COPY_ALL_INDEX;
133 else
134 pButtons[i].iBitmap = sButtons[i].iBitmap;
136 pButtons[i].idCommand = sButtons[i].iCommand;
137 pButtons[i].fsState = TBSTATE_ENABLED;
139 // TODO: Not sure if this is the best way to handle the separator
140 if(sButtons[i].iBitmap == -1)
141 pButtons[i].fsStyle = TBSTYLE_SEP;
142 else
143 pButtons[i].fsStyle = TBSTYLE_BUTTON;
144 pButtons[i].iString = sButtons[i].iString;
147 SendMessage(m_hwnd, TB_ADDBUTTONS, iNumButtons, (LPARAM)pButtons);
149 delete(pButtons);
151 int dwBtnSize = SendMessage(m_hwnd, TB_GETBUTTONSIZE, 0,0);
153 rbBand.hwndChild = m_hwnd;
154 rbBand.cxMinChild = 0;
155 rbBand.cyMinChild = HIWORD(dwBtnSize);
156 rbBand.cxIdeal = 250;
158 SendMessage(m_hRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);
161 void CToolbar::DestroyToolbar() {
162 DestroyWindow(m_hRebar);
163 m_hRebar = 0;
166 int CToolbar::GetHeight() {
167 RECT sRect;
168 GetWindowRect(m_hRebar, &sRect);
169 return sRect.bottom - sRect.top;