044832366e32c60bd7172b41052f9ff0f7e6f887
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / include / toolbar.h
1 // Win32++   Version 7.3\r
2 // Released: 30th November 2011\r
3 //\r
4 //      David Nash\r
5 //      email: [email protected]\r
6 //      url: https://sourceforge.net/projects/win32-framework\r
7 //\r
8 //\r
9 // Copyright (c) 2005-2011  David Nash\r
10 //\r
11 // Permission is hereby granted, free of charge, to\r
12 // any person obtaining a copy of this software and\r
13 // associated documentation files (the "Software"),\r
14 // to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify,\r
16 // merge, publish, distribute, sublicense, and/or sell\r
17 // copies of the Software, and to permit persons to whom\r
18 // the Software is furnished to do so, subject to the\r
19 // following conditions:\r
20 //\r
21 // The above copyright notice and this permission notice\r
22 // shall be included in all copies or substantial portions\r
23 // of the Software.\r
24 //\r
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF\r
26 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\r
27 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\r
28 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\r
29 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR\r
30 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
31 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
32 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE\r
33 // OR OTHER DEALINGS IN THE SOFTWARE.\r
34 //\r
35 ////////////////////////////////////////////////////////\r
36 \r
37 \r
38 #ifndef _WIN32XX_TOOLBAR_H_\r
39 #define _WIN32XX_TOOLBAR_H_\r
40 \r
41 #include "wincore.h"\r
42 #include "gdi.h"\r
43 #include "rebar.h"\r
44 \r
45 \r
46 namespace Win32xx\r
47 {\r
48 \r
49         struct ToolBarTheme\r
50         {\r
51                 BOOL UseThemes;                 // TRUE if themes are used\r
52                 COLORREF clrHot1;               // Colour 1 for hot button\r
53                 COLORREF clrHot2;               // Colour 2 for hot button\r
54                 COLORREF clrPressed1;   // Colour 1 for pressed button\r
55                 COLORREF clrPressed2;   // Colour 2 for pressed button\r
56                 COLORREF clrOutline;    // Colour for border outline\r
57         };\r
58 \r
59 \r
60         ////////////////////////////////////\r
61         // Declaration of the CToolBar class\r
62         //\r
63         class CToolBar : public CWnd\r
64         {\r
65         public:\r
66                 CToolBar();\r
67                 virtual ~CToolBar();\r
68 \r
69                 // Operations\r
70                 virtual int  AddBitmap(UINT ToolBarID);\r
71                 virtual BOOL AddButton(UINT nID, BOOL bEnabled = TRUE);\r
72                 virtual void Destroy();\r
73                 virtual BOOL ReplaceBitmap(UINT NewToolBarID);\r
74                 virtual BOOL SetBitmap(UINT nID);\r
75                 virtual int  SetButtons(const std::vector<UINT>& vToolBarData) const;\r
76                 virtual BOOL SetButtonText(int idButton, LPCTSTR szText);\r
77                 virtual BOOL SetImages(COLORREF crMask, UINT ToolBarID, UINT ToolBarHotID, UINT ToolBarDisabledID);\r
78 \r
79                 // Wrappers for Win32 API functions\r
80                 BOOL  AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const;\r
81                 int   AddString(UINT nStringID) const;\r
82                 int   AddStrings(LPCTSTR lpszStrings) const;\r
83                 void  Autosize() const;\r
84                 void  CheckButton(int idButton, BOOL fCheck) const;\r
85                 int   CommandToIndex(int idButton) const;\r
86                 BOOL  DeleteButton(int iButton) const;\r
87                 BOOL  DisableButton(int idButton) const;\r
88                 BOOL  EnableButton(int idButton) const;\r
89                 BOOL  GetButton(int iButton, LPTBBUTTON lpButton) const;\r
90                 int   GetButtonCount() const;\r
91                 DWORD GetButtonSize() const;\r
92                 UINT  GetButtonState(int idButton) const;\r
93                 BYTE  GetButtonStyle(int idButton) const;\r
94                 CString GetButtonText(int idButton) const;\r
95                 int   GetCommandID(int iIndex) const;\r
96                 HIMAGELIST GetDisabledImageList() const;\r
97                 int   GetHotItem() const;\r
98                 HIMAGELIST GetHotImageList() const;\r
99                 HIMAGELIST GetImageList() const;\r
100                 CRect GetItemRect(int iIndex) const;\r
101                 CSize GetMaxSize() const;\r
102                 DWORD GetPadding() const;\r
103                 CRect GetRect(int idButton) const;\r
104                 int   GetRows() const;\r
105                 int   GetTextRows() const;\r
106                 CToolTip*  GetToolTips() const;\r
107                 BOOL  HasText() const;\r
108                 BOOL  HideButton(int idButton, BOOL fShow) const;\r
109                 int   HitTest() const;\r
110                 BOOL  Indeterminate(int idButton, BOOL fIndeterminate) const;\r
111                 BOOL  InsertButton(int iButton, LPTBBUTTON lpButton) const;\r
112                 BOOL  IsButtonHidden(int idButton) const;\r
113                 BOOL  IsButtonHighlighted(int idButton) const;\r
114                 BOOL  IsButtonIndeterminate(int idButton) const;\r
115                 BOOL  IsButtonPressed(int idButton) const;\r
116                 int   MapAccelerator(TCHAR chAccel) const;\r
117                 BOOL  MarkButton(int idButton) const;\r
118                 BOOL  MoveButton(UINT uOldPos, UINT uNewPos) const;\r
119                 BOOL  PressButton(int idButton, BOOL fPress) const;\r
120                 void  SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const;\r
121                 BOOL  SetBitmapSize(int cx, int cy) const;\r
122                 BOOL  SetButtonSize(int cx, int cy) const;\r
123                 BOOL  SetButtonState(int idButton, UINT State) const;\r
124                 BOOL  SetButtonStyle(int idButton, BYTE Style) const;\r
125                 BOOL  SetButtonWidth(int idButton, int nWidth) const;\r
126                 BOOL  SetCommandID(int iIndex, int idButton) const;\r
127                 HIMAGELIST SetDisableImageList(HIMAGELIST himlNewDisabled) const;\r
128                 DWORD SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const;\r
129                 DWORD SetExtendedStyle(DWORD dwExStyle) const;\r
130                 HIMAGELIST SetHotImageList(HIMAGELIST himlNewHot) const;\r
131                 int   SetHotItem(int iHot) const;\r
132                 HIMAGELIST SetImageList(HIMAGELIST himlNew) const;\r
133                 BOOL  SetIndent(int iIndent) const;\r
134                 BOOL  SetMaxTextRows(int iMaxRows) const;\r
135                 BOOL  SetPadding(int cx, int cy) const;\r
136                 void  SetToolTips(CToolTip* pToolTip) const;\r
137 \r
138                 // Attributes\r
139                 std::vector<UINT>& GetToolBarData() const {return (std::vector <UINT> &)m_vToolBarData;}\r
140                 ToolBarTheme& GetToolBarTheme() {return m_Theme;}\r
141                 void SetToolBarTheme(ToolBarTheme& Theme);\r
142 \r
143         protected:\r
144         // Overridables\r
145                 virtual void OnCreate();\r
146                 virtual void OnDestroy();\r
147                 virtual void OnWindowPosChanging(WPARAM wParam, LPARAM lParam);\r
148                 virtual LRESULT OnCustomDraw(NMHDR* pNMHDR);\r
149                 virtual LRESULT OnNotifyReflect(WPARAM wParam, LPARAM lParam);\r
150                 virtual void PreCreate(CREATESTRUCT &cs);\r
151                 virtual void PreRegisterClass(WNDCLASS &wc);\r
152                 virtual LRESULT WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam);\r
153 \r
154         private:\r
155                 CToolBar(const CToolBar&);                              // Disable copy construction\r
156                 CToolBar& operator = (const CToolBar&); // Disable assignment operator\r
157 \r
158                 std::vector<UINT> m_vToolBarData;       // vector of resource IDs for toolbar buttons\r
159                 std::map<CString, int> m_StringMap;     // a map of strings used in SetButtonText\r
160                 UINT m_OldToolBarID;                            // Bitmap Resource ID, used in AddBitmap/ReplaceBitmap\r
161                 ToolBarTheme m_Theme;                           // The theme structure\r
162                 BOOL m_bDrawArrowBkgrnd;                        // True if a seperate arrow background is to be drawn\r
163 \r
164         };  // class CToolBar\r
165 \r
166 }\r
167 \r
168 \r
169 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r
170 \r
171 \r
172 namespace Win32xx\r
173 {\r
174 \r
175         ////////////////////////////////////\r
176         // Definitions for the CToolBar class\r
177         //\r
178         inline CToolBar::CToolBar() : m_OldToolBarID(0), m_bDrawArrowBkgrnd(FALSE)\r
179         {\r
180                 ZeroMemory(&m_Theme, sizeof(ToolBarTheme));\r
181         }\r
182 \r
183         inline CToolBar::~CToolBar()\r
184         {\r
185         }\r
186 \r
187         inline int CToolBar::AddBitmap(UINT ToolBarID)\r
188         // Adds one or more images to the list of button images available for a toolbar.\r
189 \r
190         // Note: AddBitmap supports a maximum colour depth of 8 bits (256 colours)\r
191         //       For more colours, use an ImageList instead\r
192         {\r
193                 assert(::IsWindow(m_hWnd));\r
194 \r
195                 int iNumButtons = 0;\r
196                 std::vector<UINT>::iterator iter;\r
197                 for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
198                         if ((*iter) != 0) ++iNumButtons;\r
199 \r
200                 TBADDBITMAP tbab = {0};\r
201                 tbab.hInst = GetApp()->GetResourceHandle();\r
202                 tbab.nID   = ToolBarID;\r
203                 int iResult = (int)SendMessage(TB_ADDBITMAP, iNumButtons, (LPARAM)&tbab);\r
204 \r
205                 if (-1 != iResult)\r
206                         m_OldToolBarID = ToolBarID;\r
207 \r
208                 return iResult;\r
209         }\r
210 \r
211         inline BOOL CToolBar::AddButton(UINT nID, BOOL bEnabled /* = TRUE */)\r
212         // Adds Resource IDs to toolbar buttons.\r
213         // A resource ID of 0 is a separator\r
214         {\r
215                 assert(::IsWindow(m_hWnd));\r
216 \r
217                 m_vToolBarData.push_back(nID);\r
218 \r
219                 // TBBUTTON structure for each button in the toolbar\r
220                 TBBUTTON tbb = {0};\r
221 \r
222                 std::vector<UINT>::iterator iter;\r
223                 int iImages = 0;\r
224                 for(iter = m_vToolBarData.begin(); iter < m_vToolBarData.end(); ++iter)\r
225                         if (0 != *iter) iImages++;\r
226 \r
227                 ZeroMemory(&tbb, sizeof(TBBUTTON));\r
228 \r
229                 if (0 == nID)\r
230                 {\r
231                         tbb.fsStyle = TBSTYLE_SEP;\r
232                 }\r
233                 else\r
234                 {\r
235                         tbb.dwData  = iImages -1;\r
236                         tbb.iBitmap = iImages -1;\r
237                         tbb.idCommand = nID;\r
238                         tbb.fsState = bEnabled? TBSTATE_ENABLED : 0;\r
239                         tbb.fsStyle = TBSTYLE_BUTTON;\r
240                 }\r
241 \r
242                 // Add the button to the toolbar\r
243                 return (BOOL)SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb);\r
244         }\r
245 \r
246         inline BOOL CToolBar::AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const\r
247         // Adds one or more buttons to a toolbar.\r
248         {\r
249                 assert(::IsWindow(m_hWnd));\r
250                 return (BOOL)SendMessage(TB_ADDBUTTONS, (LPARAM)uNumButtons, (WPARAM)lpButtons);\r
251         }\r
252 \r
253         inline int CToolBar::AddString(UINT nStringID) const\r
254         // Adds a new string, passed as a resource ID, to the toolbar's internal list of strings.\r
255         {\r
256                 assert(::IsWindow(m_hWnd));\r
257                 return (int)SendMessage(TB_ADDSTRING, (LPARAM)GetApp()->GetResourceHandle(), (WPARAM)nStringID);\r
258         }\r
259 \r
260         inline int CToolBar::AddStrings(LPCTSTR lpszStrings) const\r
261         // Adds a new string or strings to the list of strings available for a toolbar control.\r
262         // Strings in the buffer must be separated by a null character. You must ensure that the last string has two null terminators.\r
263         {\r
264                 assert(::IsWindow(m_hWnd));\r
265                 return (int)SendMessage(TB_ADDSTRING, 0L, (WPARAM)lpszStrings);\r
266         }\r
267 \r
268         inline void CToolBar::Autosize() const\r
269         // Causes a toolbar to be resized.\r
270         {\r
271                 assert(::IsWindow(m_hWnd));\r
272                 SendMessage(TB_AUTOSIZE, 0L, 0L);\r
273         }\r
274 \r
275         inline void CToolBar::CheckButton(int idButton, BOOL fCheck) const\r
276         // Checks or unchecks a given button in a toolbar.\r
277         // When a button is checked, it is displayed in the pressed state.\r
278         {\r
279                 assert(::IsWindow(m_hWnd));\r
280                 SendMessage(TB_CHECKBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fCheck, 0));\r
281         }\r
282 \r
283         inline int CToolBar::CommandToIndex(int idButton) const\r
284         // Retrieves the zero-based index for the button associated with the specified command identifier\r
285         {\r
286                 assert(::IsWindow(m_hWnd));\r
287 \r
288                 // returns -1 on fail\r
289                 return (int)SendMessage(TB_COMMANDTOINDEX, (WPARAM)idButton, 0L);\r
290         }\r
291 \r
292         inline BOOL CToolBar::DeleteButton(int iButton) const\r
293         // Deletes a button from the toolbar.\r
294         // iButton is the Zero-based index of the button to delete.\r
295         {\r
296                 assert(::IsWindow(m_hWnd));\r
297                 return (int)SendMessage(TB_DELETEBUTTON, (WPARAM)iButton, 0L);\r
298         }\r
299 \r
300         inline void CToolBar::Destroy()\r
301         // Allows CToolBar to be reused after the window is destroyed\r
302         {\r
303                 CWnd::Destroy();\r
304                 m_StringMap.clear();\r
305         }\r
306 \r
307         inline BOOL CToolBar::DisableButton(int idButton) const\r
308         // Disables the specified button in a toolbar\r
309         // An example of idButton would be IDM_FILE_OPEN\r
310         {\r
311                 assert(::IsWindow(m_hWnd));\r
312                 return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(FALSE, 0));\r
313         }\r
314 \r
315         inline BOOL CToolBar::EnableButton(int idButton) const\r
316         // Enables the specified button in a toolbar\r
317         {\r
318                 assert(::IsWindow(m_hWnd));\r
319                 return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(TRUE,0 ));\r
320         }\r
321 \r
322         inline BOOL CToolBar::GetButton(int iButton, LPTBBUTTON lpButton) const\r
323         // Recieves the TBBUTTON structure information from the specified button\r
324         {\r
325                 assert(::IsWindow(m_hWnd));\r
326                 return (BOOL)SendMessage(TB_GETBUTTON, (LPARAM)iButton, (WPARAM)lpButton);\r
327         }\r
328 \r
329         inline int CToolBar::GetButtonCount() const\r
330         // Retrieves a count of the buttons currently in the toolbar\r
331         {\r
332                 assert(::IsWindow(m_hWnd));\r
333                 return (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
334         }\r
335 \r
336         inline DWORD CToolBar::GetButtonSize() const\r
337         // Retrieves the current width and height of toolbar buttons, in pixels.\r
338         // Returns a DWORD value that contains the width and height values in the low word and high word, respectively.\r
339         {\r
340                 assert(::IsWindow(m_hWnd));\r
341                 return (DWORD)SendMessage(TB_GETBUTTONSIZE, 0L, 0L);\r
342         }\r
343 \r
344         inline UINT CToolBar::GetButtonState(int idButton) const\r
345         // Get the state of an individual button\r
346         //      TBSTATE_CHECKED         The button has the TBSTYLE_CHECK style and is being clicked.\r
347         //      TBSTATE_ELLIPSES        The button's text is cut off and an ellipsis is displayed.\r
348         //      TBSTATE_ENABLED         The button accepts user input. A button that doesn't have this state is grayed.\r
349         //      TBSTATE_HIDDEN          The button is not visible and cannot receive user input.\r
350         //      TBSTATE_INDETERMINATE   The button is grayed.\r
351         //      TBSTATE_MARKED          The button is marked. The interpretation of a marked item is dependent upon the application.\r
352         //      TBSTATE_PRESSED         The button is being clicked.\r
353         //      TBSTATE_WRAP            The button is followed by a line break.\r
354         {\r
355                 assert(::IsWindow(m_hWnd));\r
356                 return (UINT)SendMessage(TB_GETSTATE, (WPARAM) idButton, 0L);\r
357         }\r
358 \r
359         inline BYTE CToolBar::GetButtonStyle(int idButton) const\r
360         //      Get the the style of the toolbar control. The following button styles are supported:\r
361         //      TBSTYLE_BUTTON          Standard pushbutton (default)\r
362         //      TBSTYLE_SEP                     Separator\r
363         //      TBSTYLE_CHECK           Auto check-box button\r
364         //      TBSTYLE_GROUP           Marks the start of a group of buttons\r
365         //      TBSTYLE_CHECKGROUP      Marks the start of a group of check-box buttons\r
366         //      TBSTYLE_DROPDOWN        Creates a drop-down list button\r
367         //      TBSTYLE_AUTOSIZE        The button's width will be calculated based on the text of the button, not on the size of the image\r
368         //      TBSTYLE_NOPREFIX        The button text will not have an accelerator prefix associated with it\r
369         {\r
370                 assert(::IsWindow(m_hWnd));\r
371 \r
372                 int iIndex = CommandToIndex(idButton);\r
373                 TBBUTTON tbb = {0};\r
374                 SendMessage(TB_GETBUTTON, iIndex, (LPARAM) &tbb);\r
375 \r
376                 return tbb.fsStyle;\r
377         }\r
378 \r
379         inline CString CToolBar::GetButtonText(int idButton) const\r
380         // Retrieves the display text of a button on a toolbar.\r
381         {\r
382                 assert(::IsWindow(m_hWnd));\r
383 \r
384                 int Length = (int)SendMessage(TB_GETBUTTONTEXT, idButton, 0);\r
385                 CString str;\r
386                 LPTSTR szStr = str.GetBuffer(Length +1);\r
387                 SendMessage(TB_GETBUTTONTEXT, (LPARAM)idButton, (WPARAM)szStr);\r
388                 str.ReleaseBuffer();\r
389                 return str;\r
390         }\r
391 \r
392         inline int CToolBar::GetCommandID(int iIndex) const\r
393         // Retrieves information about the specified button in a toolbar\r
394         {\r
395                 assert(::IsWindow(m_hWnd));\r
396                 TBBUTTON tbb = {0};\r
397                 SendMessage(TB_GETBUTTON, iIndex, (WPARAM) &tbb);\r
398 \r
399                 // returns zero if failed\r
400                 return tbb.idCommand;\r
401         }\r
402 \r
403         inline HIMAGELIST CToolBar::GetDisabledImageList() const\r
404         // Retrieves the image list that a toolbar control uses to display inactive buttons.\r
405         {\r
406                 assert(::IsWindow(m_hWnd));\r
407                 return (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
408         }\r
409         \r
410         inline HIMAGELIST CToolBar::GetHotImageList() const\r
411         // Retrieves the image list that a toolbar control uses to display hot buttons.\r
412         {\r
413                 assert(::IsWindow(m_hWnd));\r
414                 return (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);     \r
415         }\r
416 \r
417         inline int CToolBar::GetHotItem() const\r
418         // Retrieves the index of the hot item in a toolbar, or -1 if no hot item is set.\r
419         {\r
420                 assert(::IsWindow(m_hWnd));\r
421                 return (int)SendMessage(TB_GETHOTITEM, 0L, 0L);\r
422         }\r
423 \r
424         inline HIMAGELIST CToolBar::GetImageList() const\r
425         // Retrieves the image list that a toolbar control uses to display buttons in their default state.\r
426         {\r
427                 assert(::IsWindow(m_hWnd));\r
428                 return (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
429         }\r
430 \r
431         inline CRect CToolBar::GetItemRect(int iIndex) const\r
432         // Retrieves the bounding rectangle of a button in a toolbar\r
433         {\r
434                 assert(::IsWindow(m_hWnd));\r
435                 CRect rc;\r
436                 int iCount = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
437 \r
438                 if (iCount >= iIndex)\r
439                         SendMessage(TB_GETITEMRECT, (WPARAM)iIndex, (LPARAM)&rc);\r
440 \r
441                 return rc;\r
442         }\r
443 \r
444         inline CSize CToolBar::GetMaxSize() const\r
445         // Retrieves the total size of all of the visible buttons and separators in the toolbar\r
446         {\r
447                 assert(::IsWindow(m_hWnd));\r
448                 CSize sz;\r
449                 SendMessage(TB_GETMAXSIZE, 0L, (LPARAM)&sz);\r
450 \r
451                 // This fixes a Windows bug calculating the size when TBSTYLE_DROPDOWN is used.\r
452                 int xMaxSize = 0;\r
453                 for (int i= 0 ; i < GetButtonCount(); ++i)\r
454                 {\r
455                         xMaxSize += GetItemRect(i).Width();\r
456                 }\r
457 \r
458                 sz.cx = xMaxSize;\r
459                 return sz;\r
460         }\r
461 \r
462         inline DWORD CToolBar::GetPadding() const\r
463         // Returns a DWORD value that contains the horizontal padding in the low word and the vertical padding in the high word, in pixels.\r
464         {\r
465                 assert(::IsWindow(m_hWnd));\r
466                 return (DWORD)SendMessage(TB_GETPADDING, 0L, 0L);\r
467         }\r
468 \r
469         inline CRect CToolBar::GetRect(int idButton) const\r
470         // Retrieves the bounding rectangle for a specified toolbar button.\r
471         {\r
472                 assert(::IsWindow(m_hWnd));\r
473                 CRect rc;\r
474                 SendMessage(TB_GETRECT, (WPARAM)idButton, (LPARAM)&rc);\r
475                 return rc;\r
476         }\r
477 \r
478         inline int CToolBar::GetRows() const\r
479         // Retrieves the number of rows of buttons in a toolbar with the TBSTYLE_WRAPABLE style.\r
480         {\r
481                 assert(::IsWindow(m_hWnd));\r
482                 return (int)SendMessage(TB_GETROWS, 0L, 0L);\r
483         }\r
484 \r
485         inline int CToolBar::GetTextRows() const\r
486         // Retrieves the maximum number of text rows that can be displayed on a toolbar button.\r
487         {\r
488                 assert(::IsWindow(m_hWnd));\r
489                 return (int)SendMessage(TB_GETTEXTROWS, 0L, 0L);\r
490         }\r
491 \r
492         inline CToolTip* CToolBar::GetToolTips() const\r
493         // Retrieves the handle to the ToolTip control, if any, associated with the toolbar.\r
494         {\r
495                 assert(::IsWindow(m_hWnd));\r
496                 return (CToolTip*)FromHandle( (HWND)SendMessage(TB_GETTOOLTIPS, 0L, 0L) );\r
497         }\r
498 \r
499         inline BOOL CToolBar::HasText() const\r
500         {\r
501                 assert(::IsWindow(m_hWnd));\r
502                 BOOL bReturn = FALSE;\r
503 \r
504                 for (int i = 0 ; i < GetButtonCount(); ++i)\r
505                 {\r
506                         if (SendMessage(TB_GETBUTTONTEXT, GetCommandID(i), 0L) != -1)\r
507                                 bReturn = TRUE;\r
508                 }\r
509 \r
510                 // return TRUE if any button has text\r
511                 return bReturn;\r
512         }\r
513 \r
514         inline BOOL CToolBar::HideButton(int idButton, BOOL fShow) const\r
515         //Hides or shows the specified button in a toolbar.\r
516         {\r
517                 assert(::IsWindow(m_hWnd));\r
518                 return (BOOL)SendMessage(TB_HIDEBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG (fShow, 0));\r
519         }\r
520 \r
521         inline int CToolBar::HitTest() const\r
522         // Determines where a point lies in a toolbar control.\r
523 \r
524         // We do our own hit test since TB_HITTEST is a bit buggy,\r
525         // and also doesn't work at all on earliest versions of Win95\r
526         {\r
527                 assert(::IsWindow(m_hWnd));\r
528                 CPoint pt = GetCursorPos();\r
529                 ScreenToClient(pt);\r
530 \r
531                 int nButtons = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
532                 int iButton = -1;\r
533 \r
534                 for (int i = 0 ; i < nButtons; ++i)\r
535                 {\r
536                         CRect rc = GetItemRect(i);\r
537                         if (rc.PtInRect(pt))\r
538                                 iButton = i;\r
539                 }\r
540 \r
541                 return iButton;\r
542         }\r
543 \r
544         inline BOOL CToolBar::Indeterminate(int idButton, BOOL fIndeterminate) const\r
545         //Hides or shows the specified button in a toolbar.\r
546         {\r
547                 assert(::IsWindow(m_hWnd));\r
548                 return (BOOL)SendMessage(TB_INDETERMINATE, (WPARAM)idButton, (LPARAM)MAKELONG (fIndeterminate, 0));\r
549         }\r
550 \r
551         inline BOOL CToolBar::InsertButton(int iButton, LPTBBUTTON lpButton) const\r
552         // Inserts a button in a toolbar.\r
553         {\r
554                 assert(::IsWindow(m_hWnd));\r
555                 return (BOOL)SendMessage(TB_INSERTBUTTON, (WPARAM)iButton, (LPARAM)lpButton);\r
556         }\r
557 \r
558         inline BOOL CToolBar::IsButtonHidden(int idButton) const\r
559         // Determines whether the specified button in a toolbar is hidden.\r
560         {\r
561                 assert(::IsWindow(m_hWnd));\r
562                 return (BOOL)SendMessage(TB_ISBUTTONHIDDEN, (WPARAM)idButton, 0L);\r
563         }\r
564 \r
565         inline BOOL CToolBar::IsButtonHighlighted(int idButton) const\r
566         // Checks the highlight state of a toolbar button.\r
567         {\r
568                 assert(::IsWindow(m_hWnd));\r
569                 return (BOOL)SendMessage(TB_ISBUTTONHIGHLIGHTED, (WPARAM)idButton, 0L);\r
570         }\r
571 \r
572         inline BOOL CToolBar::IsButtonIndeterminate(int idButton) const\r
573         // Determines whether the specified button in a toolbar is indeterminate.\r
574         {\r
575                 assert(::IsWindow(m_hWnd));\r
576                 return (BOOL)SendMessage(TB_ISBUTTONINDETERMINATE, (WPARAM)idButton, 0L);\r
577         }\r
578 \r
579         inline BOOL CToolBar::IsButtonPressed(int idButton) const\r
580         // Determines whether the specified button in a toolbar is pressed.\r
581         {\r
582                 assert(::IsWindow(m_hWnd));\r
583                 return (BOOL)SendMessage(TB_ISBUTTONPRESSED, (WPARAM)idButton, 0L);\r
584         }\r
585 \r
586         inline int CToolBar::MapAccelerator(TCHAR chAccel) const\r
587         // Determines whether the specified button in a toolbar is pressed.\r
588         {\r
589                 assert(::IsWindow(m_hWnd));\r
590                 int uButtonID;\r
591                 int idButton;\r
592                 if (SendMessage(TB_MAPACCELERATOR, (WPARAM)chAccel, (LPARAM)&uButtonID))\r
593                         idButton = uButtonID;\r
594                 else\r
595                         idButton = -1;\r
596 \r
597                 return idButton;\r
598         }\r
599 \r
600         inline BOOL CToolBar::MarkButton(int idButton) const\r
601         // Sets the highlight state of a given button in a toolbar control.\r
602         {\r
603                 assert(::IsWindow(m_hWnd));\r
604                 return (BOOL)SendMessage(TB_MARKBUTTON, (WPARAM)idButton, 0L);\r
605         }\r
606 \r
607         inline BOOL CToolBar::MoveButton(UINT uOldPos, UINT uNewPos) const\r
608         // Moves a button from one index to another.\r
609         {\r
610                 assert(::IsWindow(m_hWnd));\r
611                 return (BOOL)SendMessage(TB_MOVEBUTTON, (WPARAM)uOldPos, (LPARAM)uNewPos);\r
612         }\r
613 \r
614 \r
615         inline void CToolBar::OnCreate()\r
616         {\r
617                 // We must send this message before sending the TB_ADDBITMAP or TB_ADDBUTTONS message\r
618                 SendMessage(TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0L);\r
619 \r
620                 // allows buttons to have a separate dropdown arrow\r
621                 // Note: TBN_DROPDOWN notification is sent by a toolbar control when the user clicks a dropdown button\r
622                 SendMessage(TB_SETEXTENDEDSTYLE, 0L, TBSTYLE_EX_DRAWDDARROWS);\r
623 \r
624                 // Turn of Double click processing (i.e. treat a double click as two single clicks)\r
625                 DWORD dwStyle = (DWORD)GetClassLongPtr(GCL_STYLE);\r
626                 dwStyle &=      ~CS_DBLCLKS;\r
627                 SetClassLongPtr(GCL_STYLE, dwStyle);\r
628 \r
629                 // Add extra styles for toolbars inside a rebar\r
630                 if (lstrcmp(GetParent()->GetClassName(), _T("ReBarWindow32")) == 0)\r
631                 {\r
632                         DWORD style = (DWORD)GetWindowLongPtr(GWL_STYLE);\r
633                         style |= CCS_NODIVIDER | CCS_NORESIZE;\r
634                         SetWindowLongPtr(GWL_STYLE, style);\r
635                 }\r
636 \r
637                 SetButtons(m_vToolBarData);\r
638 \r
639                 // Set rows of text to zero\r
640                 SendMessage(TB_SETMAXTEXTROWS, 0L, 0L);\r
641         }\r
642 \r
643         inline LRESULT CToolBar::OnCustomDraw(NMHDR* pNMHDR)\r
644         // With CustomDraw we manually control the drawing of each toolbar button\r
645         {\r
646                 LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW)pNMHDR;\r
647 \r
648                 switch (lpNMCustomDraw->nmcd.dwDrawStage)\r
649                 {\r
650                 // Begin paint cycle\r
651                 case CDDS_PREPAINT:\r
652                         // Send NM_CUSTOMDRAW item draw, and post-paint notification messages.\r
653                         return CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT ;\r
654 \r
655                 // An item is about to be drawn\r
656                 case CDDS_ITEMPREPAINT:\r
657                         {\r
658                                 CDC DrawDC(lpNMCustomDraw->nmcd.hdc);\r
659                                 CRect rcRect = lpNMCustomDraw->nmcd.rc;\r
660                                 int nState = lpNMCustomDraw->nmcd.uItemState;\r
661                                 DWORD dwItem = (DWORD)lpNMCustomDraw->nmcd.dwItemSpec;\r
662                                 DWORD dwTBStyle = (DWORD)SendMessage(TB_GETSTYLE, 0L, 0L);\r
663                                 int nStyle = GetButtonStyle(dwItem);\r
664 \r
665                                 int nButton = (int)SendMessage(TB_COMMANDTOINDEX, (WPARAM) dwItem, 0L);\r
666                                 TBBUTTON tbb = {0};\r
667                                 SendMessage(TB_GETBUTTON, nButton, (LPARAM)&tbb);\r
668                                 int iImage = (int)tbb.dwData;\r
669 \r
670                                 // Calculate text size\r
671                                 std::vector<TCHAR> vText(MAX_MENU_STRING, _T('\0'));\r
672                                 TCHAR* pszText = &vText[0];\r
673                                 CSize TextSize;\r
674                                 if (HasText())  // Does any button have text?\r
675                                 {\r
676                                         DrawDC.SelectObject(GetFont());\r
677                                         if (SendMessage(TB_GETBUTTONTEXT, dwItem, (LPARAM)pszText)> 0)\r
678                                         {\r
679                                                 TextSize = DrawDC.GetTextExtentPoint32(pszText, lstrlen(pszText));\r
680                                         }\r
681                                 }\r
682 \r
683                                 // Draw outline rectangle\r
684                                 if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))\r
685                                 {\r
686                                         DrawDC.CreatePen(PS_SOLID, 1, m_Theme.clrOutline);\r
687                                         DrawDC.MoveTo(rcRect.left, rcRect.top);\r
688                                         DrawDC.LineTo(rcRect.left, rcRect.bottom-1);\r
689                                         DrawDC.LineTo(rcRect.right-1, rcRect.bottom-1);\r
690                                         DrawDC.LineTo(rcRect.right-1, rcRect.top);\r
691                                         DrawDC.LineTo(rcRect.left, rcRect.top);\r
692                                 }\r
693 \r
694                                 // Draw filled gradient background\r
695                                 rcRect.InflateRect(-1, -1);\r
696                                 if ((nState & (CDIS_SELECTED|CDIS_CHECKED)) || (GetButtonState(dwItem) & TBSTATE_PRESSED))\r
697                                 {\r
698                                         DrawDC.GradientFill(m_Theme.clrPressed1, m_Theme.clrPressed2, rcRect, FALSE);\r
699                                 }\r
700                                 else if (nState & CDIS_HOT)\r
701                                 {\r
702                                         DrawDC.GradientFill(m_Theme.clrHot1, m_Theme.clrHot2, rcRect, FALSE);\r
703                                 }\r
704 \r
705                                 // Get the appropriate image list depending on the button state\r
706                                 HIMAGELIST himlToolBar;\r
707                                 if (nState & CDIS_DISABLED)\r
708                                 {\r
709                                         himlToolBar = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
710                                 }\r
711                                 else if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))\r
712                                 {\r
713                                         himlToolBar = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
714                                         if (0 == himlToolBar)\r
715                                                 himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
716                                 }\r
717                                 else\r
718                                 {\r
719                                         himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
720                                 }\r
721 \r
722                                 BOOL IsWin95 = (1400 == (GetWinVersion()) || (2400 == GetWinVersion()));\r
723 \r
724                                 // Calculate image position\r
725                                 int cxImage = 0;\r
726                                 int cyImage = 0;\r
727                                 ImageList_GetIconSize(himlToolBar, &cxImage, &cyImage);\r
728 \r
729                                 int yImage = (rcRect.bottom - rcRect.top - cyImage - TextSize.cy +2)/2;\r
730                                 int xImage = (rcRect.right + rcRect.left - cxImage)/2 + ((nState & (CDIS_SELECTED|CDIS_CHECKED))? 1:0);\r
731                                 if (dwTBStyle & TBSTYLE_LIST)\r
732                                 {\r
733                                         xImage = rcRect.left + (IsXPThemed()?2:4) + ((nState & CDIS_SELECTED)? 1:0);\r
734                                         yImage = (rcRect.bottom -rcRect.top - cyImage +2)/2 + ((nState & (CDIS_SELECTED|CDIS_CHECKED))? 1:0);\r
735                                 }\r
736 \r
737                                 // Handle the TBSTYLE_DROPDOWN and BTNS_WHOLEDROPDOWN styles\r
738                                 if ((nStyle & TBSTYLE_DROPDOWN) || ((nStyle & 0x0080) && (!IsWin95)))\r
739                                 {\r
740                                         // Calculate the dropdown arrow position\r
741                                         int xAPos = (nStyle & TBSTYLE_DROPDOWN)? rcRect.right -6 : (rcRect.right + rcRect.left + cxImage + 4)/2;\r
742                                         int yAPos = (nStyle & TBSTYLE_DROPDOWN)? (rcRect.bottom - rcRect.top +1)/2 : (cyImage)/2;\r
743                                         if (dwTBStyle & TBSTYLE_LIST)\r
744                                         {\r
745                                                 xAPos = (nStyle & TBSTYLE_DROPDOWN)?rcRect.right -6:rcRect.right -5;\r
746                                                 yAPos = (rcRect.bottom - rcRect.top +1)/2 + ((nStyle & TBSTYLE_DROPDOWN)?0:1);\r
747                                         }\r
748 \r
749                                         xImage -= (nStyle & TBSTYLE_DROPDOWN)?((dwTBStyle & TBSTYLE_LIST)? (IsXPThemed()?-4:0):6):((dwTBStyle & TBSTYLE_LIST)? 0:4);\r
750 \r
751                                         // Draw separate background for dropdown arrow\r
752                                         if ((m_bDrawArrowBkgrnd) && (nState & CDIS_HOT))\r
753                                         {\r
754                                                 CRect rcArrowBkgnd = rcRect;\r
755                                                 rcArrowBkgnd.left = rcArrowBkgnd.right - 13;\r
756                                                 DrawDC.GradientFill(m_Theme.clrPressed1, m_Theme.clrPressed2, rcArrowBkgnd, FALSE);\r
757                                         }\r
758 \r
759                                         m_bDrawArrowBkgrnd = FALSE;\r
760 \r
761                                         // Manually draw the dropdown arrow\r
762                                         DrawDC.CreatePen(PS_SOLID, 1, RGB(0,0,0));\r
763                                         for (int i = 2; i >= 0; --i)\r
764                                         {\r
765                                                 DrawDC.MoveTo(xAPos -i-1, yAPos - i+1);\r
766                                                 DrawDC.LineTo(xAPos +i,   yAPos - i+1);\r
767                                         }\r
768 \r
769                                         // Draw line between icon and dropdown arrow\r
770                                         if ((nStyle & TBSTYLE_DROPDOWN) && ((nState & CDIS_SELECTED) || nState & CDIS_HOT))\r
771                                         {\r
772                                                 DrawDC.CreatePen(PS_SOLID, 1, m_Theme.clrOutline);\r
773                                                 DrawDC.MoveTo(rcRect.right - 13, rcRect.top);\r
774                                                 DrawDC.LineTo(rcRect.right - 13, rcRect.bottom);\r
775                                         }\r
776                                 }\r
777 \r
778                                 // Draw the button image\r
779                                 if (xImage > 0)\r
780                                 {\r
781                                         ImageList_Draw(himlToolBar, iImage, DrawDC, xImage, yImage, ILD_TRANSPARENT);\r
782                                 }\r
783 \r
784                                 //Draw Text\r
785                                 if (lstrlen(pszText) > 0)\r
786                                 {\r
787                                         int iWidth = rcRect.right - rcRect.left - ((nStyle & TBSTYLE_DROPDOWN)?13:0);\r
788                                         CRect rcText(0, 0, MIN(TextSize.cx, iWidth), TextSize.cy);\r
789 \r
790                                         int xOffset = (rcRect.right + rcRect.left - rcText.right + rcText.left - ((nStyle & TBSTYLE_DROPDOWN)? 11 : 1))/2;\r
791                                         int yOffset = yImage + cyImage +1;\r
792 \r
793                                         if (dwTBStyle & TBSTYLE_LIST)\r
794                                         {\r
795                                                 xOffset = rcRect.left + cxImage + ((nStyle & TBSTYLE_DROPDOWN)?(IsXPThemed()?10:6): 6) + ((nState & CDIS_SELECTED)? 1:0);\r
796                                                 yOffset = (2+rcRect.bottom - rcRect.top - rcText.bottom + rcText.top)/2 + ((nState & CDIS_SELECTED)? 1:0);\r
797                                                 rcText.right = MIN(rcText.right,  rcRect.right - xOffset);\r
798                                         }\r
799 \r
800                                         OffsetRect(&rcText, xOffset, yOffset);\r
801 \r
802                                         int iMode = DrawDC.SetBkMode(TRANSPARENT);\r
803                                         DrawDC.SelectObject(GetFont());\r
804 \r
805                                         if (nState & (CDIS_DISABLED))\r
806                                         {\r
807                                                 // Draw text twice for embossed look\r
808                                                 rcText.OffsetRect(1, 1);\r
809                                                 DrawDC.SetTextColor(RGB(255,255,255));\r
810                                                 DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT);\r
811                                                 rcText.OffsetRect(-1, -1);\r
812                                                 DrawDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));\r
813                                                 DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT);\r
814                                         }\r
815                                         else\r
816                                         {\r
817                                                 DrawDC.SetTextColor(GetSysColor(COLOR_BTNTEXT));\r
818                                                 DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT | DT_END_ELLIPSIS);\r
819                                         }\r
820                                         DrawDC.SetBkMode(iMode);\r
821 \r
822                                 }\r
823                                 DrawDC.Detach();\r
824                         }\r
825                         return CDRF_SKIPDEFAULT;  // No further drawing\r
826                 }\r
827                 return 0L;\r
828         }\r
829 \r
830         inline void CToolBar::OnDestroy()\r
831         {\r
832                 HIMAGELIST himlToolBar    = (HIMAGELIST)SendMessage(TB_GETIMAGELIST,    0L, 0L);\r
833                 HIMAGELIST himlToolBarHot = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
834                 HIMAGELIST himlToolBarDis = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
835                 ImageList_Destroy(himlToolBar);\r
836                 ImageList_Destroy(himlToolBarHot);\r
837                 ImageList_Destroy(himlToolBarDis);\r
838         }\r
839 \r
840         inline LRESULT CToolBar::OnNotifyReflect(WPARAM wParam, LPARAM lParam)\r
841         // Notifications sent to the parent window are reflected back here\r
842         {\r
843                 UNREFERENCED_PARAMETER(wParam);\r
844 \r
845                 switch (((LPNMHDR)lParam)->code)\r
846                 {\r
847                         case NM_CUSTOMDRAW:\r
848                         {\r
849                                 if ((m_Theme.UseThemes) && (GetComCtlVersion() > 470))\r
850                                         return OnCustomDraw((LPNMHDR) lParam);\r
851                         }\r
852                         break;\r
853 \r
854                         case TBN_DROPDOWN:\r
855                         {\r
856                                 int iItem = ((LPNMTOOLBAR) lParam)->iItem;\r
857 \r
858                                 // a boolean expression\r
859                                 m_bDrawArrowBkgrnd = (GetButtonStyle(iItem) & TBSTYLE_DROPDOWN);\r
860                         }\r
861                         break;\r
862                 }\r
863                 return 0L;\r
864         }\r
865 \r
866         inline void CToolBar::OnWindowPosChanging(WPARAM wParam, LPARAM lParam)\r
867         {\r
868                 UNREFERENCED_PARAMETER(wParam);\r
869 \r
870                 // Adjust size for toolbars inside a rebar\r
871                 CWnd* pParent = GetParent();\r
872                 if (lstrcmp(pParent->GetClassName(), _T("ReBarWindow32")) == 0)\r
873                 {\r
874                         ReBarTheme* pTheme = (ReBarTheme*)pParent->SendMessage(UWM_GETREBARTHEME, 0, 0);\r
875 \r
876                         if (pTheme && pTheme->UseThemes && pTheme->ShortBands)\r
877                         {\r
878                                 LPWINDOWPOS pWinPos = (LPWINDOWPOS)lParam;\r
879                                 pWinPos->cx = GetMaxSize().cx+2;\r
880                         }\r
881                 }\r
882         }\r
883 \r
884         inline void CToolBar::PreCreate(CREATESTRUCT &cs)\r
885         {\r
886                 // Sets the CREATESTRUCT parameters prior to window creation\r
887                 cs.style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;\r
888         }\r
889 \r
890         inline void CToolBar::PreRegisterClass(WNDCLASS &wc)\r
891         {\r
892                 // Set the Window Class\r
893                 wc.lpszClassName =  TOOLBARCLASSNAME;\r
894         }\r
895 \r
896         inline BOOL CToolBar::PressButton(int idButton, BOOL fPress) const\r
897         // Presses or releases the specified button in a toolbar.\r
898         {\r
899                 assert(::IsWindow(m_hWnd));\r
900                 return (BOOL)SendMessage(TB_PRESSBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fPress, 0));\r
901         }\r
902 \r
903         inline BOOL CToolBar::ReplaceBitmap(UINT NewToolBarID)\r
904         // Replaces an existing bitmap with a new bitmap.\r
905 \r
906         // Note: ReplaceBitmap supports a maximum colour depth of 8 bits (256 colours)\r
907         //       For more colours, use an ImageList instead\r
908         {\r
909                 assert(::IsWindow(m_hWnd));\r
910 \r
911                 int iNumButtons = 0;\r
912                 std::vector<UINT>::iterator iter;\r
913                 for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
914                         if ((*iter) != 0) ++iNumButtons;\r
915 \r
916                 TBREPLACEBITMAP tbrb = {0};\r
917                 tbrb.hInstNew = GetApp()->GetResourceHandle();\r
918                 tbrb.hInstOld = GetApp()->GetResourceHandle();\r
919                 tbrb.nIDNew = NewToolBarID;\r
920                 tbrb.nIDOld = m_OldToolBarID;\r
921                 tbrb.nButtons  = iNumButtons;\r
922 \r
923                 BOOL bResult = (BOOL)SendMessage(TB_REPLACEBITMAP, iNumButtons, (LPARAM)&tbrb);\r
924                 if (bResult)\r
925                         m_OldToolBarID = NewToolBarID;\r
926 \r
927                 return bResult;\r
928         }\r
929 \r
930         inline void CToolBar::SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const\r
931         // Presses or releases the specified button in a toolbar.\r
932         {\r
933                 assert(::IsWindow(m_hWnd));\r
934                 SendMessage(TB_PRESSBUTTON, (WPARAM)fSave, (LPARAM)ptbsp);\r
935         }\r
936 \r
937         inline BOOL CToolBar::SetBitmap(UINT nID)\r
938         // Set the button images\r
939         {\r
940                 assert(::IsWindow(m_hWnd));\r
941 \r
942                 CBitmap Bitmap(nID);\r
943                 assert (Bitmap.GetHandle());\r
944                 BITMAP bm = Bitmap.GetBitmapData();\r
945 \r
946                 int iNumButtons = 0;\r
947                 std::vector<UINT>::iterator iter;\r
948                 for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
949                 {\r
950                         if ((*iter) != 0) \r
951                                 ++iNumButtons;\r
952                 }\r
953 \r
954                 int iImageWidth  = bm.bmWidth / iNumButtons;\r
955                 int iImageHeight = bm.bmHeight;\r
956 \r
957                 // Set the bitmap size first\r
958                 SetBitmapSize(iImageWidth, iImageHeight);\r
959 \r
960                 BOOL bResult = FALSE;\r
961                 if (m_OldToolBarID)\r
962                         bResult = ReplaceBitmap(nID);\r
963                 else\r
964                         bResult = (BOOL)AddBitmap(nID);\r
965 \r
966                 return bResult;\r
967         }\r
968 \r
969         inline BOOL CToolBar::SetBitmapSize(int cx, int cy) const\r
970         // Sets the size of the bitmapped images to be added to a toolbar.\r
971 \r
972         // Needs to be used when the image size is not the default 16 x 15\r
973         // Call this function before using AddBitmap or ReplaceBitmap\r
974         {\r
975                 assert(::IsWindow(m_hWnd));\r
976                 return (BOOL)SendMessage(TB_SETBITMAPSIZE, 0L, MAKELONG(cx, cy));\r
977         }\r
978 \r
979         inline int CToolBar::SetButtons(const std::vector<UINT>& vToolBarData) const\r
980         // Assigns a resource ID to each toolbar button\r
981         {\r
982                 assert(::IsWindow(m_hWnd));\r
983 \r
984                 int iImages = 0;\r
985                 UINT iNumButtons = (UINT)vToolBarData.size();\r
986 \r
987                 // Remove any existing buttons\r
988                 while (SendMessage(TB_BUTTONCOUNT,  0L, 0L) > 0)\r
989                 {\r
990                         if(!SendMessage(TB_DELETEBUTTON, 0L, 0L))\r
991                                 break;\r
992                 }\r
993 \r
994                 if (iNumButtons > 0)\r
995                 {\r
996                         // TBBUTTON structure for each button in the toolbar\r
997                         TBBUTTON tbb = {0};\r
998 \r
999                         for (UINT j = 0 ; j < iNumButtons; ++j)\r
1000                         {\r
1001                                 ZeroMemory(&tbb, sizeof(TBBUTTON));\r
1002 \r
1003                                 if (0 == vToolBarData[j])\r
1004                                 {\r
1005                                         tbb.fsStyle = TBSTYLE_SEP;\r
1006                                 }\r
1007                                 else\r
1008                                 {\r
1009                                         tbb.dwData  = iImages;\r
1010                                         tbb.iBitmap = iImages;\r
1011                                         tbb.idCommand = vToolBarData[j];\r
1012                                         tbb.fsState = TBSTATE_ENABLED;\r
1013                                         tbb.fsStyle = TBSTYLE_BUTTON;\r
1014                                 }\r
1015 \r
1016                                 // Add the button to the toolbar\r
1017                                 if (SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb))\r
1018                                         iImages++;\r
1019                                 else\r
1020                                         break;\r
1021                         }\r
1022                 }\r
1023 \r
1024                 return iImages;\r
1025         }\r
1026 \r
1027         inline BOOL CToolBar::SetButtonSize(int cx, int cy) const\r
1028         // Sets the size of the buttons to be added to a toolbar\r
1029         // The size can be set only before adding any buttons to the toolbar\r
1030         {\r
1031                 assert(::IsWindow(m_hWnd));\r
1032                 return (BOOL)SendMessage(TB_SETBUTTONSIZE, 0L, MAKELONG(cx, cy));\r
1033         }\r
1034 \r
1035         inline BOOL CToolBar::SetButtonState(int idButton, UINT State) const\r
1036         {\r
1037         // Set the state of an individual button\r
1038         //      TBSTATE_CHECKED         The button has the TBSTYLE_CHECK style and is being clicked.\r
1039         //      TBSTATE_ELLIPSES        The button's text is cut off and an ellipsis is displayed.\r
1040         //      TBSTATE_ENABLED         The button accepts user input. A button that doesn't have this state is grayed.\r
1041         //      TBSTATE_HIDDEN          The button is not visible and cannot receive user input.\r
1042         //      TBSTATE_INDETERMINATE   The button is grayed.\r
1043         //      TBSTATE_MARKED          The button is marked. The interpretation of a marked item is dependent upon the application.\r
1044         //      TBSTATE_PRESSED         The button is being clicked.\r
1045         //      TBSTATE_WRAP            The button is followed by a line break.\r
1046 \r
1047                 assert(::IsWindow(m_hWnd));\r
1048                 return (BOOL)SendMessage(TB_SETSTATE, (WPARAM) idButton, (LPARAM)MAKELONG (State, 0));\r
1049         }\r
1050 \r
1051         inline BOOL CToolBar::SetButtonStyle(int idButton, BYTE Style) const\r
1052         //      The the style of the toolbar control. The following button styles are supported:\r
1053         //      TBSTYLE_BUTTON          Standard pushbutton (default)\r
1054         //      TBSTYLE_SEP                     Separator\r
1055         //      TBSTYLE_CHECK           Auto check-box button\r
1056         //      TBSTYLE_GROUP           Marks the start of a group of buttons\r
1057         //      TBSTYLE_CHECKGROUP      Marks the start of a group of check-box buttons\r
1058         //      TBSTYLE_DROPDOWN        Creates a drop-down list button\r
1059         //      TBSTYLE_AUTOSIZE        The button's width will be calculated based on the text of the button, not on the size of the image\r
1060         //      TBSTYLE_NOPREFIX        The button text will not have an accelerator prefix associated with it\r
1061         {\r
1062                 assert(::IsWindow(m_hWnd));\r
1063 \r
1064                 TBBUTTONINFO tbbi = {0};\r
1065                 tbbi.cbSize = sizeof(TBBUTTONINFO);\r
1066                 tbbi.dwMask = TBIF_STYLE;\r
1067                 tbbi.fsStyle = Style;\r
1068 \r
1069                 // Note:  TB_SETBUTTONINFO requires comctl32.dll version 4.71 or later\r
1070                 //        i.e. Win95 with IE4 / NT with IE4   or later\r
1071                 return (BOOL)SendMessage(TB_SETBUTTONINFO, idButton, (LPARAM) &tbbi);\r
1072         }\r
1073 \r
1074         inline BOOL CToolBar::SetButtonText(int idButton, LPCTSTR szText)\r
1075         // This rather convoluted approach to setting toolbar button text supports\r
1076         // all versions of Windows, including Win95 with COMCTL32.DLL version 4.0\r
1077         {\r
1078                 assert(::IsWindow(m_hWnd));\r
1079                 int iIndex = CommandToIndex(idButton);\r
1080                 assert(-1 != iIndex);\r
1081 \r
1082                 BOOL Succeeded = TRUE;\r
1083                 CString sString = szText;\r
1084                 std::map<CString, int>::iterator m;\r
1085                 int iString;\r
1086 \r
1087                 // Check to see if the string is already added\r
1088                 m = m_StringMap.find(sString);\r
1089                 if (m_StringMap.end() == m)\r
1090                 {\r
1091                         if (0 == m_StringMap.size())\r
1092                         {\r
1093                                 // Place a blank string first in the string table, in case some\r
1094                                 // buttons don't have text\r
1095                                 TCHAR szString[3] = _T(" ");\r
1096                                 szString[2] = _T('\0');         // Double-null terminate\r
1097                                 SendMessage(TB_ADDSTRING, 0L, (LPARAM)szString);\r
1098                         }\r
1099 \r
1100                         // No index for this string exists, so create it now\r
1101                         TCHAR szBuf[80] = _T("");\r
1102                         lstrcpyn(szBuf, szText, 79);\r
1103                         szBuf[lstrlen(szBuf)+1] = _T('\0');             // Double-null terminate\r
1104 \r
1105                         iString = (int)SendMessage(TB_ADDSTRING, 0L, (LPARAM)szBuf);\r
1106                         if (-1 == iString )\r
1107                                 Succeeded = FALSE;\r
1108 \r
1109                         // Save the string its index in our map\r
1110                         m_StringMap.insert(std::make_pair(sString, iString));\r
1111                 }\r
1112                 else\r
1113                 {\r
1114                         // String found, use the index from our map\r
1115                         iString = m->second;\r
1116                 }\r
1117 \r
1118                 if (Succeeded)\r
1119                 {\r
1120                         TBBUTTON tbb = {0};\r
1121                         Succeeded = (BOOL)SendMessage(TB_GETBUTTON, iIndex, (LPARAM)&tbb);\r
1122 \r
1123                         tbb.iString = iString;\r
1124 \r
1125                         // Turn off ToolBar drawing\r
1126                         SendMessage(WM_SETREDRAW, FALSE, 0L);\r
1127 \r
1128                         if (Succeeded)\r
1129                                 Succeeded = (BOOL)SendMessage(TB_DELETEBUTTON, iIndex, 0L);\r
1130 \r
1131                         if (Succeeded)\r
1132                                 Succeeded = (BOOL)SendMessage(TB_INSERTBUTTON, iIndex, (LPARAM)&tbb);\r
1133 \r
1134                         // Ensure the button now includes some text rows\r
1135                         if (0 == SendMessage(TB_GETTEXTROWS, 0L, 0L))\r
1136                                 SendMessage(TB_SETMAXTEXTROWS, 1L, 0L);\r
1137 \r
1138                         // Turn on ToolBar drawing\r
1139                         SendMessage(WM_SETREDRAW, TRUE, 0L);\r
1140                 }\r
1141                 // Redraw button\r
1142                 CRect r = GetItemRect(iIndex);\r
1143                 InvalidateRect(&r, TRUE);\r
1144 \r
1145                 return Succeeded;\r
1146         }\r
1147 \r
1148         inline BOOL CToolBar::SetButtonWidth(int idButton, int nWidth) const\r
1149         // The set button width can adjust the width of the button after it is created.\r
1150         // This is useful when replacing a button with a ComboBox or other control.\r
1151         // Note:  TB_SETBUTTONINFO requires comctl32.dll version 4.71 or later\r
1152         //        i.e. Win95 with IE4 / NT with IE4   or later\r
1153         {\r
1154                 assert(::IsWindow(m_hWnd));\r
1155 \r
1156                 TBBUTTONINFO tbbi = {0};\r
1157                 tbbi.cbSize = sizeof(TBBUTTONINFO);\r
1158                 tbbi.dwMask = TBIF_SIZE;\r
1159                 tbbi.cx = (WORD)nWidth;\r
1160                 BOOL bResult = (BOOL)SendMessage(TB_SETBUTTONINFO, (WPARAM)idButton, (LPARAM)&tbbi);\r
1161 \r
1162                 // Send a changed message to the parent (used by the rebar)\r
1163                 SIZE MaxSize = GetMaxSize();\r
1164                 GetParent()->SendMessage(UWM_TOOLBAR_RESIZE, (WPARAM)m_hWnd, (LPARAM)&MaxSize);\r
1165 \r
1166                 return bResult;\r
1167         }\r
1168 \r
1169         inline BOOL CToolBar::SetCommandID(int iIndex, int idButton) const\r
1170         // Sets the command identifier of a toolbar button\r
1171         {\r
1172                 assert(::IsWindow(m_hWnd));\r
1173                 return (BOOL)SendMessage(TB_SETCMDID, iIndex, idButton);\r
1174         }\r
1175 \r
1176         inline HIMAGELIST CToolBar::SetDisableImageList(HIMAGELIST himlNewDisabled) const\r
1177         // Sets the image list that the toolbar control will use to display disabled buttons.\r
1178         {\r
1179                 assert(::IsWindow(m_hWnd));\r
1180                 return (HIMAGELIST)SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlNewDisabled);\r
1181         }\r
1182 \r
1183         inline DWORD CToolBar::SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const\r
1184         // Sets the text drawing flags for the toolbar.\r
1185         {\r
1186                 assert(::IsWindow(m_hWnd));\r
1187                 return (DWORD)SendMessage(TB_SETDRAWTEXTFLAGS, (WPARAM)dwMask, (LPARAM)dwDTFlags);\r
1188         }\r
1189 \r
1190         inline DWORD CToolBar::SetExtendedStyle(DWORD dwExStyle) const\r
1191         // Sets the text drawing flags for the toolbar.\r
1192         // Extended styles include: TBSTYLE_EX_DRAWDDARROWS, TBSTYLE_EX_HIDECLIPPEDBUTTONS, TBSTYLE_EX_DOUBLEBUFFER and TBSTYLE_EX_MIXEDBUTTONS\r
1193         {\r
1194                 assert(::IsWindow(m_hWnd));\r
1195                 return (DWORD)SendMessage(TB_SETEXTENDEDSTYLE, 0L, (LPARAM)dwExStyle);\r
1196         }\r
1197 \r
1198         inline HIMAGELIST CToolBar::SetHotImageList(HIMAGELIST himlNewHot) const\r
1199         // Sets the image list that the toolbar control will use to display hot buttons.\r
1200         {\r
1201                 assert(::IsWindow(m_hWnd));\r
1202                 return (HIMAGELIST)SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlNewHot);\r
1203         }\r
1204         \r
1205         inline int CToolBar::SetHotItem(int iHot) const\r
1206         // Sets the hot item in a toolbar.\r
1207         {\r
1208                 assert(::IsWindow(m_hWnd));\r
1209                 return (int)SendMessage(TB_SETHOTITEM, (WPARAM)iHot, 0L);\r
1210         }\r
1211 \r
1212         inline HIMAGELIST CToolBar::SetImageList(HIMAGELIST himlNew) const\r
1213         // Sets the image list that the toolbar will use to display buttons that are in their default state.\r
1214         {\r
1215                 assert(::IsWindow(m_hWnd));\r
1216                 return (HIMAGELIST)SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlNew);\r
1217         }\r
1218 \r
1219         inline BOOL CToolBar::SetImages(COLORREF crMask, UINT ToolBarID, UINT ToolBarHotID, UINT ToolBarDisabledID)\r
1220         // Either sets the imagelist or adds/replaces bitmap depending on ComCtl32.dll version\r
1221         // Assumes the width of the button image = bitmap_size / buttons\r
1222         // Assumes buttons have been already been added via AdddToolBarButton\r
1223         // The colour mask is often grey RGB(192,192,192) or magenta (255,0,255);\r
1224         // The color mask is ignored for 32bit bitmap resources\r
1225         // The Hot and disiabled bitmap resources can be 0\r
1226         {\r
1227                 assert(::IsWindow(m_hWnd));\r
1228 \r
1229                 // ToolBar ImageLists require Comctl32.dll version 4.7 or later\r
1230                 if (GetComCtlVersion() < 470)\r
1231                 {\r
1232                         // We are using COMCTL32.DLL version 4.0, so we can't use an imagelist.\r
1233                         // Instead we simply set the bitmap.\r
1234                         return SetBitmap(ToolBarID);\r
1235                 }\r
1236 \r
1237                 int iNumButtons = 0;\r
1238                 std::vector<UINT>::iterator iter;\r
1239                 for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
1240                         if ((*iter) != 0) ++iNumButtons;\r
1241 \r
1242                 if (iNumButtons > 0)\r
1243                 {\r
1244                         // Set the button images\r
1245                         CBitmap Bitmap(ToolBarID);\r
1246                         assert(Bitmap.GetHandle());\r
1247 \r
1248                         BITMAP bm = Bitmap.GetBitmapData();\r
1249                         int iImageWidth  = bm.bmWidth / iNumButtons;\r
1250                         int iImageHeight = bm.bmHeight;\r
1251 \r
1252                         HIMAGELIST himlToolBar    = (HIMAGELIST)SendMessage(TB_GETIMAGELIST,    0L, 0L);\r
1253                         HIMAGELIST himlToolBarHot = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
1254                         HIMAGELIST himlToolBarDis = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
1255                         ImageList_Destroy(himlToolBar);\r
1256                         ImageList_Destroy(himlToolBarHot);\r
1257                         ImageList_Destroy(himlToolBarDis);\r
1258 \r
1259                         himlToolBar = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
1260                         assert(himlToolBar);\r
1261 \r
1262                         ImageList_AddMasked(himlToolBar, Bitmap, crMask);\r
1263                         SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlToolBar);\r
1264 \r
1265                         if (ToolBarHotID)\r
1266                         {\r
1267                                 CBitmap BitmapHot(ToolBarHotID);\r
1268                                 assert(BitmapHot);\r
1269 \r
1270                                 himlToolBarHot = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
1271                                 assert(himlToolBarHot);\r
1272 \r
1273                                 ImageList_AddMasked(himlToolBarHot, BitmapHot, crMask);\r
1274                                 SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlToolBarHot);\r
1275                         }\r
1276 \r
1277                         if (ToolBarDisabledID)\r
1278                         {\r
1279                                 CBitmap BitmapDisabled(ToolBarDisabledID);\r
1280                                 assert(BitmapDisabled);\r
1281 \r
1282                                 himlToolBarDis = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
1283                                 assert(himlToolBarDis);\r
1284 \r
1285                                 ImageList_AddMasked(himlToolBarDis, BitmapDisabled, crMask);\r
1286                                 SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);\r
1287                         }\r
1288                         else\r
1289                         {\r
1290                                 himlToolBarDis = CreateDisabledImageList(himlToolBar);\r
1291                                 SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);\r
1292                         }\r
1293 \r
1294                         // Inform the parent of the change (rebar needs this)\r
1295                         SIZE MaxSize = GetMaxSize();\r
1296                         GetParent()->SendMessage(UWM_TOOLBAR_RESIZE, (WPARAM)m_hWnd, (LPARAM)&MaxSize);\r
1297                 }\r
1298 \r
1299                 return TRUE;\r
1300         }\r
1301 \r
1302         inline BOOL CToolBar::SetIndent(int iIndent) const\r
1303         // Sets the indentation for the first button in a toolbar control.\r
1304         {\r
1305                 assert(::IsWindow(m_hWnd));\r
1306                 return (BOOL)SendMessage(TB_SETINDENT, (WPARAM)iIndent, 0L);\r
1307         }\r
1308 \r
1309         inline BOOL CToolBar::SetMaxTextRows(int iMaxRows) const\r
1310         // Sets the maximum number of text rows displayed on a toolbar button.\r
1311         {\r
1312                 assert(::IsWindow(m_hWnd));\r
1313                 return (BOOL)SendMessage(TB_SETMAXTEXTROWS, (WPARAM)iMaxRows, 0L);\r
1314         }\r
1315 \r
1316         inline BOOL CToolBar::SetPadding(int cx, int cy) const\r
1317         // Sets the padding for a toolbar control.\r
1318         {\r
1319                 assert(::IsWindow(m_hWnd));\r
1320                 return (BOOL)SendMessage(TB_SETPADDING, 0L, (WPARAM)MAKELONG(cx, cy));\r
1321         }\r
1322 \r
1323         inline void CToolBar::SetToolBarTheme(ToolBarTheme& Theme)\r
1324         {\r
1325                 m_Theme.UseThemes   = Theme.UseThemes;\r
1326                 m_Theme.clrHot1     = Theme.clrHot1;\r
1327                 m_Theme.clrHot2     = Theme.clrHot2;\r
1328                 m_Theme.clrPressed1 = Theme.clrPressed1;\r
1329                 m_Theme.clrPressed2 = Theme.clrPressed2;\r
1330                 m_Theme.clrOutline  = Theme.clrOutline;\r
1331 \r
1332                 if (IsWindow())\r
1333                         Invalidate();\r
1334         }\r
1335 \r
1336         inline void CToolBar::SetToolTips(CToolTip* pToolTip) const\r
1337         // Associates a ToolTip control with a toolbar.\r
1338         {\r
1339                 assert(::IsWindow(m_hWnd));\r
1340                 HWND hToolTip = pToolTip? pToolTip->GetHwnd() : (HWND)0;\r
1341                 SendMessage(TB_SETTOOLTIPS, (WPARAM)hToolTip, 0L);\r
1342         }\r
1343 \r
1344         inline LRESULT CToolBar::WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
1345         {\r
1346                 switch (uMsg)\r
1347                 {\r
1348                 case WM_DESTROY:\r
1349                         OnDestroy();\r
1350                         break;\r
1351                 case UWM_GETTOOLBARTHEME:\r
1352                         {\r
1353                                 ToolBarTheme& tt = GetToolBarTheme();\r
1354                                 return (LRESULT)&tt;\r
1355                         }\r
1356                 case WM_WINDOWPOSCHANGING:\r
1357                         OnWindowPosChanging(wParam, lParam);\r
1358                         break;\r
1359                 }\r
1360 \r
1361                 // pass unhandled messages on for default processing\r
1362                 return CWnd::WndProcDefault(uMsg, wParam, lParam);\r
1363         }\r
1364 \r
1365 } // namespace Win32xx\r
1366 \r
1367 #endif // #ifndef _WIN32XX_TOOLBAR_H_\r

UCC git Repository :: git.ucc.asn.au