1 // Win32++ Version 7.3
\r
2 // Released: 30th November 2011
\r
6 // url: https://sourceforge.net/projects/win32-framework
\r
9 // Copyright (c) 2005-2011 David Nash
\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
21 // The above copyright notice and this permission notice
\r
22 // shall be included in all copies or substantial portions
\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
35 ////////////////////////////////////////////////////////
\r
38 #ifndef _WIN32XX_TOOLBAR_H_
\r
39 #define _WIN32XX_TOOLBAR_H_
\r
41 #include "wincore.h"
\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
60 ////////////////////////////////////
\r
61 // Declaration of the CToolBar class
\r
63 class CToolBar : public CWnd
\r
67 virtual ~CToolBar();
\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
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
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
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
155 CToolBar(const CToolBar&); // Disable copy construction
\r
156 CToolBar& operator = (const CToolBar&); // Disable assignment operator
\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
164 }; // class CToolBar
\r
169 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\r
175 ////////////////////////////////////
\r
176 // Definitions for the CToolBar class
\r
178 inline CToolBar::CToolBar() : m_OldToolBarID(0), m_bDrawArrowBkgrnd(FALSE)
\r
180 ZeroMemory(&m_Theme, sizeof(ToolBarTheme));
\r
183 inline CToolBar::~CToolBar()
\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
190 // Note: AddBitmap supports a maximum colour depth of 8 bits (256 colours)
\r
191 // For more colours, use an ImageList instead
\r
193 assert(::IsWindow(m_hWnd));
\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
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
206 m_OldToolBarID = ToolBarID;
\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
215 assert(::IsWindow(m_hWnd));
\r
217 m_vToolBarData.push_back(nID);
\r
219 // TBBUTTON structure for each button in the toolbar
\r
220 TBBUTTON tbb = {0};
\r
222 std::vector<UINT>::iterator iter;
\r
224 for(iter = m_vToolBarData.begin(); iter < m_vToolBarData.end(); ++iter)
\r
225 if (0 != *iter) iImages++;
\r
227 ZeroMemory(&tbb, sizeof(TBBUTTON));
\r
231 tbb.fsStyle = TBSTYLE_SEP;
\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
242 // Add the button to the toolbar
\r
243 return (BOOL)SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb);
\r
246 inline BOOL CToolBar::AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const
\r
247 // Adds one or more buttons to a toolbar.
\r
249 assert(::IsWindow(m_hWnd));
\r
250 return (BOOL)SendMessage(TB_ADDBUTTONS, (LPARAM)uNumButtons, (WPARAM)lpButtons);
\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
256 assert(::IsWindow(m_hWnd));
\r
257 return (int)SendMessage(TB_ADDSTRING, (LPARAM)GetApp()->GetResourceHandle(), (WPARAM)nStringID);
\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
264 assert(::IsWindow(m_hWnd));
\r
265 return (int)SendMessage(TB_ADDSTRING, 0L, (WPARAM)lpszStrings);
\r
268 inline void CToolBar::Autosize() const
\r
269 // Causes a toolbar to be resized.
\r
271 assert(::IsWindow(m_hWnd));
\r
272 SendMessage(TB_AUTOSIZE, 0L, 0L);
\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
279 assert(::IsWindow(m_hWnd));
\r
280 SendMessage(TB_CHECKBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fCheck, 0));
\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
286 assert(::IsWindow(m_hWnd));
\r
288 // returns -1 on fail
\r
289 return (int)SendMessage(TB_COMMANDTOINDEX, (WPARAM)idButton, 0L);
\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
296 assert(::IsWindow(m_hWnd));
\r
297 return (int)SendMessage(TB_DELETEBUTTON, (WPARAM)iButton, 0L);
\r
300 inline void CToolBar::Destroy()
\r
301 // Allows CToolBar to be reused after the window is destroyed
\r
304 m_StringMap.clear();
\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
311 assert(::IsWindow(m_hWnd));
\r
312 return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(FALSE, 0));
\r
315 inline BOOL CToolBar::EnableButton(int idButton) const
\r
316 // Enables the specified button in a toolbar
\r
318 assert(::IsWindow(m_hWnd));
\r
319 return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(TRUE,0 ));
\r
322 inline BOOL CToolBar::GetButton(int iButton, LPTBBUTTON lpButton) const
\r
323 // Recieves the TBBUTTON structure information from the specified button
\r
325 assert(::IsWindow(m_hWnd));
\r
326 return (BOOL)SendMessage(TB_GETBUTTON, (LPARAM)iButton, (WPARAM)lpButton);
\r
329 inline int CToolBar::GetButtonCount() const
\r
330 // Retrieves a count of the buttons currently in the toolbar
\r
332 assert(::IsWindow(m_hWnd));
\r
333 return (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);
\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
340 assert(::IsWindow(m_hWnd));
\r
341 return (DWORD)SendMessage(TB_GETBUTTONSIZE, 0L, 0L);
\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
355 assert(::IsWindow(m_hWnd));
\r
356 return (UINT)SendMessage(TB_GETSTATE, (WPARAM) idButton, 0L);
\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
370 assert(::IsWindow(m_hWnd));
\r
372 int iIndex = CommandToIndex(idButton);
\r
373 TBBUTTON tbb = {0};
\r
374 SendMessage(TB_GETBUTTON, iIndex, (LPARAM) &tbb);
\r
376 return tbb.fsStyle;
\r
379 inline CString CToolBar::GetButtonText(int idButton) const
\r
380 // Retrieves the display text of a button on a toolbar.
\r
382 assert(::IsWindow(m_hWnd));
\r
384 int Length = (int)SendMessage(TB_GETBUTTONTEXT, idButton, 0);
\r
386 LPTSTR szStr = str.GetBuffer(Length +1);
\r
387 SendMessage(TB_GETBUTTONTEXT, (LPARAM)idButton, (WPARAM)szStr);
\r
388 str.ReleaseBuffer();
\r
392 inline int CToolBar::GetCommandID(int iIndex) const
\r
393 // Retrieves information about the specified button in a toolbar
\r
395 assert(::IsWindow(m_hWnd));
\r
396 TBBUTTON tbb = {0};
\r
397 SendMessage(TB_GETBUTTON, iIndex, (WPARAM) &tbb);
\r
399 // returns zero if failed
\r
400 return tbb.idCommand;
\r
403 inline HIMAGELIST CToolBar::GetDisabledImageList() const
\r
404 // Retrieves the image list that a toolbar control uses to display inactive buttons.
\r
406 assert(::IsWindow(m_hWnd));
\r
407 return (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);
\r
410 inline HIMAGELIST CToolBar::GetHotImageList() const
\r
411 // Retrieves the image list that a toolbar control uses to display hot buttons.
\r
413 assert(::IsWindow(m_hWnd));
\r
414 return (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);
\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
420 assert(::IsWindow(m_hWnd));
\r
421 return (int)SendMessage(TB_GETHOTITEM, 0L, 0L);
\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
427 assert(::IsWindow(m_hWnd));
\r
428 return (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);
\r
431 inline CRect CToolBar::GetItemRect(int iIndex) const
\r
432 // Retrieves the bounding rectangle of a button in a toolbar
\r
434 assert(::IsWindow(m_hWnd));
\r
436 int iCount = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);
\r
438 if (iCount >= iIndex)
\r
439 SendMessage(TB_GETITEMRECT, (WPARAM)iIndex, (LPARAM)&rc);
\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
447 assert(::IsWindow(m_hWnd));
\r
449 SendMessage(TB_GETMAXSIZE, 0L, (LPARAM)&sz);
\r
451 // This fixes a Windows bug calculating the size when TBSTYLE_DROPDOWN is used.
\r
453 for (int i= 0 ; i < GetButtonCount(); ++i)
\r
455 xMaxSize += GetItemRect(i).Width();
\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
465 assert(::IsWindow(m_hWnd));
\r
466 return (DWORD)SendMessage(TB_GETPADDING, 0L, 0L);
\r
469 inline CRect CToolBar::GetRect(int idButton) const
\r
470 // Retrieves the bounding rectangle for a specified toolbar button.
\r
472 assert(::IsWindow(m_hWnd));
\r
474 SendMessage(TB_GETRECT, (WPARAM)idButton, (LPARAM)&rc);
\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
481 assert(::IsWindow(m_hWnd));
\r
482 return (int)SendMessage(TB_GETROWS, 0L, 0L);
\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
488 assert(::IsWindow(m_hWnd));
\r
489 return (int)SendMessage(TB_GETTEXTROWS, 0L, 0L);
\r
492 inline CToolTip* CToolBar::GetToolTips() const
\r
493 // Retrieves the handle to the ToolTip control, if any, associated with the toolbar.
\r
495 assert(::IsWindow(m_hWnd));
\r
496 return (CToolTip*)FromHandle( (HWND)SendMessage(TB_GETTOOLTIPS, 0L, 0L) );
\r
499 inline BOOL CToolBar::HasText() const
\r
501 assert(::IsWindow(m_hWnd));
\r
502 BOOL bReturn = FALSE;
\r
504 for (int i = 0 ; i < GetButtonCount(); ++i)
\r
506 if (SendMessage(TB_GETBUTTONTEXT, GetCommandID(i), 0L) != -1)
\r
510 // return TRUE if any button has text
\r
514 inline BOOL CToolBar::HideButton(int idButton, BOOL fShow) const
\r
515 //Hides or shows the specified button in a toolbar.
\r
517 assert(::IsWindow(m_hWnd));
\r
518 return (BOOL)SendMessage(TB_HIDEBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG (fShow, 0));
\r
521 inline int CToolBar::HitTest() const
\r
522 // Determines where a point lies in a toolbar control.
\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
527 assert(::IsWindow(m_hWnd));
\r
528 CPoint pt = GetCursorPos();
\r
529 ScreenToClient(pt);
\r
531 int nButtons = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);
\r
534 for (int i = 0 ; i < nButtons; ++i)
\r
536 CRect rc = GetItemRect(i);
\r
537 if (rc.PtInRect(pt))
\r
544 inline BOOL CToolBar::Indeterminate(int idButton, BOOL fIndeterminate) const
\r
545 //Hides or shows the specified button in a toolbar.
\r
547 assert(::IsWindow(m_hWnd));
\r
548 return (BOOL)SendMessage(TB_INDETERMINATE, (WPARAM)idButton, (LPARAM)MAKELONG (fIndeterminate, 0));
\r
551 inline BOOL CToolBar::InsertButton(int iButton, LPTBBUTTON lpButton) const
\r
552 // Inserts a button in a toolbar.
\r
554 assert(::IsWindow(m_hWnd));
\r
555 return (BOOL)SendMessage(TB_INSERTBUTTON, (WPARAM)iButton, (LPARAM)lpButton);
\r
558 inline BOOL CToolBar::IsButtonHidden(int idButton) const
\r
559 // Determines whether the specified button in a toolbar is hidden.
\r
561 assert(::IsWindow(m_hWnd));
\r
562 return (BOOL)SendMessage(TB_ISBUTTONHIDDEN, (WPARAM)idButton, 0L);
\r
565 inline BOOL CToolBar::IsButtonHighlighted(int idButton) const
\r
566 // Checks the highlight state of a toolbar button.
\r
568 assert(::IsWindow(m_hWnd));
\r
569 return (BOOL)SendMessage(TB_ISBUTTONHIGHLIGHTED, (WPARAM)idButton, 0L);
\r
572 inline BOOL CToolBar::IsButtonIndeterminate(int idButton) const
\r
573 // Determines whether the specified button in a toolbar is indeterminate.
\r
575 assert(::IsWindow(m_hWnd));
\r
576 return (BOOL)SendMessage(TB_ISBUTTONINDETERMINATE, (WPARAM)idButton, 0L);
\r
579 inline BOOL CToolBar::IsButtonPressed(int idButton) const
\r
580 // Determines whether the specified button in a toolbar is pressed.
\r
582 assert(::IsWindow(m_hWnd));
\r
583 return (BOOL)SendMessage(TB_ISBUTTONPRESSED, (WPARAM)idButton, 0L);
\r
586 inline int CToolBar::MapAccelerator(TCHAR chAccel) const
\r
587 // Determines whether the specified button in a toolbar is pressed.
\r
589 assert(::IsWindow(m_hWnd));
\r
592 if (SendMessage(TB_MAPACCELERATOR, (WPARAM)chAccel, (LPARAM)&uButtonID))
\r
593 idButton = uButtonID;
\r
600 inline BOOL CToolBar::MarkButton(int idButton) const
\r
601 // Sets the highlight state of a given button in a toolbar control.
\r
603 assert(::IsWindow(m_hWnd));
\r
604 return (BOOL)SendMessage(TB_MARKBUTTON, (WPARAM)idButton, 0L);
\r
607 inline BOOL CToolBar::MoveButton(UINT uOldPos, UINT uNewPos) const
\r
608 // Moves a button from one index to another.
\r
610 assert(::IsWindow(m_hWnd));
\r
611 return (BOOL)SendMessage(TB_MOVEBUTTON, (WPARAM)uOldPos, (LPARAM)uNewPos);
\r
615 inline void CToolBar::OnCreate()
\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
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
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
629 // Add extra styles for toolbars inside a rebar
\r
630 if (lstrcmp(GetParent()->GetClassName(), _T("ReBarWindow32")) == 0)
\r
632 DWORD style = (DWORD)GetWindowLongPtr(GWL_STYLE);
\r
633 style |= CCS_NODIVIDER | CCS_NORESIZE;
\r
634 SetWindowLongPtr(GWL_STYLE, style);
\r
637 SetButtons(m_vToolBarData);
\r
639 // Set rows of text to zero
\r
640 SendMessage(TB_SETMAXTEXTROWS, 0L, 0L);
\r
643 inline LRESULT CToolBar::OnCustomDraw(NMHDR* pNMHDR)
\r
644 // With CustomDraw we manually control the drawing of each toolbar button
\r
646 LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW)pNMHDR;
\r
648 switch (lpNMCustomDraw->nmcd.dwDrawStage)
\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
655 // An item is about to be drawn
\r
656 case CDDS_ITEMPREPAINT:
\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
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
670 // Calculate text size
\r
671 std::vector<TCHAR> vText(MAX_MENU_STRING, _T('\0'));
\r
672 TCHAR* pszText = &vText[0];
\r
674 if (HasText()) // Does any button have text?
\r
676 DrawDC.SelectObject(GetFont());
\r
677 if (SendMessage(TB_GETBUTTONTEXT, dwItem, (LPARAM)pszText)> 0)
\r
679 TextSize = DrawDC.GetTextExtentPoint32(pszText, lstrlen(pszText));
\r
683 // Draw outline rectangle
\r
684 if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))
\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
694 // Draw filled gradient background
\r
695 rcRect.InflateRect(-1, -1);
\r
696 if ((nState & (CDIS_SELECTED|CDIS_CHECKED)) || (GetButtonState(dwItem) & TBSTATE_PRESSED))
\r
698 DrawDC.GradientFill(m_Theme.clrPressed1, m_Theme.clrPressed2, rcRect, FALSE);
\r
700 else if (nState & CDIS_HOT)
\r
702 DrawDC.GradientFill(m_Theme.clrHot1, m_Theme.clrHot2, rcRect, FALSE);
\r
705 // Get the appropriate image list depending on the button state
\r
706 HIMAGELIST himlToolBar;
\r
707 if (nState & CDIS_DISABLED)
\r
709 himlToolBar = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);
\r
711 else if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))
\r
713 himlToolBar = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);
\r
714 if (0 == himlToolBar)
\r
715 himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);
\r
719 himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);
\r
722 BOOL IsWin95 = (1400 == (GetWinVersion()) || (2400 == GetWinVersion()));
\r
724 // Calculate image position
\r
727 ImageList_GetIconSize(himlToolBar, &cxImage, &cyImage);
\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
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
737 // Handle the TBSTYLE_DROPDOWN and BTNS_WHOLEDROPDOWN styles
\r
738 if ((nStyle & TBSTYLE_DROPDOWN) || ((nStyle & 0x0080) && (!IsWin95)))
\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
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
749 xImage -= (nStyle & TBSTYLE_DROPDOWN)?((dwTBStyle & TBSTYLE_LIST)? (IsXPThemed()?-4:0):6):((dwTBStyle & TBSTYLE_LIST)? 0:4);
\r
751 // Draw separate background for dropdown arrow
\r
752 if ((m_bDrawArrowBkgrnd) && (nState & CDIS_HOT))
\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
759 m_bDrawArrowBkgrnd = FALSE;
\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
765 DrawDC.MoveTo(xAPos -i-1, yAPos - i+1);
\r
766 DrawDC.LineTo(xAPos +i, yAPos - i+1);
\r
769 // Draw line between icon and dropdown arrow
\r
770 if ((nStyle & TBSTYLE_DROPDOWN) && ((nState & CDIS_SELECTED) || nState & CDIS_HOT))
\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
778 // Draw the button image
\r
781 ImageList_Draw(himlToolBar, iImage, DrawDC, xImage, yImage, ILD_TRANSPARENT);
\r
785 if (lstrlen(pszText) > 0)
\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
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
793 if (dwTBStyle & TBSTYLE_LIST)
\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
800 OffsetRect(&rcText, xOffset, yOffset);
\r
802 int iMode = DrawDC.SetBkMode(TRANSPARENT);
\r
803 DrawDC.SelectObject(GetFont());
\r
805 if (nState & (CDIS_DISABLED))
\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
817 DrawDC.SetTextColor(GetSysColor(COLOR_BTNTEXT));
\r
818 DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT | DT_END_ELLIPSIS);
\r
820 DrawDC.SetBkMode(iMode);
\r
825 return CDRF_SKIPDEFAULT; // No further drawing
\r
830 inline void CToolBar::OnDestroy()
\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
840 inline LRESULT CToolBar::OnNotifyReflect(WPARAM wParam, LPARAM lParam)
\r
841 // Notifications sent to the parent window are reflected back here
\r
843 UNREFERENCED_PARAMETER(wParam);
\r
845 switch (((LPNMHDR)lParam)->code)
\r
847 case NM_CUSTOMDRAW:
\r
849 if ((m_Theme.UseThemes) && (GetComCtlVersion() > 470))
\r
850 return OnCustomDraw((LPNMHDR) lParam);
\r
856 int iItem = ((LPNMTOOLBAR) lParam)->iItem;
\r
858 // a boolean expression
\r
859 m_bDrawArrowBkgrnd = (GetButtonStyle(iItem) & TBSTYLE_DROPDOWN);
\r
866 inline void CToolBar::OnWindowPosChanging(WPARAM wParam, LPARAM lParam)
\r
868 UNREFERENCED_PARAMETER(wParam);
\r
870 // Adjust size for toolbars inside a rebar
\r
871 CWnd* pParent = GetParent();
\r
872 if (lstrcmp(pParent->GetClassName(), _T("ReBarWindow32")) == 0)
\r
874 ReBarTheme* pTheme = (ReBarTheme*)pParent->SendMessage(UWM_GETREBARTHEME, 0, 0);
\r
876 if (pTheme && pTheme->UseThemes && pTheme->ShortBands)
\r
878 LPWINDOWPOS pWinPos = (LPWINDOWPOS)lParam;
\r
879 pWinPos->cx = GetMaxSize().cx+2;
\r
884 inline void CToolBar::PreCreate(CREATESTRUCT &cs)
\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
890 inline void CToolBar::PreRegisterClass(WNDCLASS &wc)
\r
892 // Set the Window Class
\r
893 wc.lpszClassName = TOOLBARCLASSNAME;
\r
896 inline BOOL CToolBar::PressButton(int idButton, BOOL fPress) const
\r
897 // Presses or releases the specified button in a toolbar.
\r
899 assert(::IsWindow(m_hWnd));
\r
900 return (BOOL)SendMessage(TB_PRESSBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fPress, 0));
\r
903 inline BOOL CToolBar::ReplaceBitmap(UINT NewToolBarID)
\r
904 // Replaces an existing bitmap with a new bitmap.
\r
906 // Note: ReplaceBitmap supports a maximum colour depth of 8 bits (256 colours)
\r
907 // For more colours, use an ImageList instead
\r
909 assert(::IsWindow(m_hWnd));
\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
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
923 BOOL bResult = (BOOL)SendMessage(TB_REPLACEBITMAP, iNumButtons, (LPARAM)&tbrb);
\r
925 m_OldToolBarID = NewToolBarID;
\r
930 inline void CToolBar::SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const
\r
931 // Presses or releases the specified button in a toolbar.
\r
933 assert(::IsWindow(m_hWnd));
\r
934 SendMessage(TB_PRESSBUTTON, (WPARAM)fSave, (LPARAM)ptbsp);
\r
937 inline BOOL CToolBar::SetBitmap(UINT nID)
\r
938 // Set the button images
\r
940 assert(::IsWindow(m_hWnd));
\r
942 CBitmap Bitmap(nID);
\r
943 assert (Bitmap.GetHandle());
\r
944 BITMAP bm = Bitmap.GetBitmapData();
\r
946 int iNumButtons = 0;
\r
947 std::vector<UINT>::iterator iter;
\r
948 for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)
\r
954 int iImageWidth = bm.bmWidth / iNumButtons;
\r
955 int iImageHeight = bm.bmHeight;
\r
957 // Set the bitmap size first
\r
958 SetBitmapSize(iImageWidth, iImageHeight);
\r
960 BOOL bResult = FALSE;
\r
961 if (m_OldToolBarID)
\r
962 bResult = ReplaceBitmap(nID);
\r
964 bResult = (BOOL)AddBitmap(nID);
\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
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
975 assert(::IsWindow(m_hWnd));
\r
976 return (BOOL)SendMessage(TB_SETBITMAPSIZE, 0L, MAKELONG(cx, cy));
\r
979 inline int CToolBar::SetButtons(const std::vector<UINT>& vToolBarData) const
\r
980 // Assigns a resource ID to each toolbar button
\r
982 assert(::IsWindow(m_hWnd));
\r
985 UINT iNumButtons = (UINT)vToolBarData.size();
\r
987 // Remove any existing buttons
\r
988 while (SendMessage(TB_BUTTONCOUNT, 0L, 0L) > 0)
\r
990 if(!SendMessage(TB_DELETEBUTTON, 0L, 0L))
\r
994 if (iNumButtons > 0)
\r
996 // TBBUTTON structure for each button in the toolbar
\r
997 TBBUTTON tbb = {0};
\r
999 for (UINT j = 0 ; j < iNumButtons; ++j)
\r
1001 ZeroMemory(&tbb, sizeof(TBBUTTON));
\r
1003 if (0 == vToolBarData[j])
\r
1005 tbb.fsStyle = TBSTYLE_SEP;
\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
1016 // Add the button to the toolbar
\r
1017 if (SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb))
\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
1031 assert(::IsWindow(m_hWnd));
\r
1032 return (BOOL)SendMessage(TB_SETBUTTONSIZE, 0L, MAKELONG(cx, cy));
\r
1035 inline BOOL CToolBar::SetButtonState(int idButton, UINT State) const
\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
1047 assert(::IsWindow(m_hWnd));
\r
1048 return (BOOL)SendMessage(TB_SETSTATE, (WPARAM) idButton, (LPARAM)MAKELONG (State, 0));
\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
1062 assert(::IsWindow(m_hWnd));
\r
1064 TBBUTTONINFO tbbi = {0};
\r
1065 tbbi.cbSize = sizeof(TBBUTTONINFO);
\r
1066 tbbi.dwMask = TBIF_STYLE;
\r
1067 tbbi.fsStyle = Style;
\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
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
1078 assert(::IsWindow(m_hWnd));
\r
1079 int iIndex = CommandToIndex(idButton);
\r
1080 assert(-1 != iIndex);
\r
1082 BOOL Succeeded = TRUE;
\r
1083 CString sString = szText;
\r
1084 std::map<CString, int>::iterator m;
\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
1091 if (0 == m_StringMap.size())
\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
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
1105 iString = (int)SendMessage(TB_ADDSTRING, 0L, (LPARAM)szBuf);
\r
1106 if (-1 == iString )
\r
1107 Succeeded = FALSE;
\r
1109 // Save the string its index in our map
\r
1110 m_StringMap.insert(std::make_pair(sString, iString));
\r
1114 // String found, use the index from our map
\r
1115 iString = m->second;
\r
1120 TBBUTTON tbb = {0};
\r
1121 Succeeded = (BOOL)SendMessage(TB_GETBUTTON, iIndex, (LPARAM)&tbb);
\r
1123 tbb.iString = iString;
\r
1125 // Turn off ToolBar drawing
\r
1126 SendMessage(WM_SETREDRAW, FALSE, 0L);
\r
1129 Succeeded = (BOOL)SendMessage(TB_DELETEBUTTON, iIndex, 0L);
\r
1132 Succeeded = (BOOL)SendMessage(TB_INSERTBUTTON, iIndex, (LPARAM)&tbb);
\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
1138 // Turn on ToolBar drawing
\r
1139 SendMessage(WM_SETREDRAW, TRUE, 0L);
\r
1142 CRect r = GetItemRect(iIndex);
\r
1143 InvalidateRect(&r, TRUE);
\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
1154 assert(::IsWindow(m_hWnd));
\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
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
1169 inline BOOL CToolBar::SetCommandID(int iIndex, int idButton) const
\r
1170 // Sets the command identifier of a toolbar button
\r
1172 assert(::IsWindow(m_hWnd));
\r
1173 return (BOOL)SendMessage(TB_SETCMDID, iIndex, idButton);
\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
1179 assert(::IsWindow(m_hWnd));
\r
1180 return (HIMAGELIST)SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlNewDisabled);
\r
1183 inline DWORD CToolBar::SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const
\r
1184 // Sets the text drawing flags for the toolbar.
\r
1186 assert(::IsWindow(m_hWnd));
\r
1187 return (DWORD)SendMessage(TB_SETDRAWTEXTFLAGS, (WPARAM)dwMask, (LPARAM)dwDTFlags);
\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
1194 assert(::IsWindow(m_hWnd));
\r
1195 return (DWORD)SendMessage(TB_SETEXTENDEDSTYLE, 0L, (LPARAM)dwExStyle);
\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
1201 assert(::IsWindow(m_hWnd));
\r
1202 return (HIMAGELIST)SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlNewHot);
\r
1205 inline int CToolBar::SetHotItem(int iHot) const
\r
1206 // Sets the hot item in a toolbar.
\r
1208 assert(::IsWindow(m_hWnd));
\r
1209 return (int)SendMessage(TB_SETHOTITEM, (WPARAM)iHot, 0L);
\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
1215 assert(::IsWindow(m_hWnd));
\r
1216 return (HIMAGELIST)SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlNew);
\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
1227 assert(::IsWindow(m_hWnd));
\r
1229 // ToolBar ImageLists require Comctl32.dll version 4.7 or later
\r
1230 if (GetComCtlVersion() < 470)
\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
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
1242 if (iNumButtons > 0)
\r
1244 // Set the button images
\r
1245 CBitmap Bitmap(ToolBarID);
\r
1246 assert(Bitmap.GetHandle());
\r
1248 BITMAP bm = Bitmap.GetBitmapData();
\r
1249 int iImageWidth = bm.bmWidth / iNumButtons;
\r
1250 int iImageHeight = bm.bmHeight;
\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
1259 himlToolBar = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);
\r
1260 assert(himlToolBar);
\r
1262 ImageList_AddMasked(himlToolBar, Bitmap, crMask);
\r
1263 SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlToolBar);
\r
1267 CBitmap BitmapHot(ToolBarHotID);
\r
1268 assert(BitmapHot);
\r
1270 himlToolBarHot = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);
\r
1271 assert(himlToolBarHot);
\r
1273 ImageList_AddMasked(himlToolBarHot, BitmapHot, crMask);
\r
1274 SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlToolBarHot);
\r
1277 if (ToolBarDisabledID)
\r
1279 CBitmap BitmapDisabled(ToolBarDisabledID);
\r
1280 assert(BitmapDisabled);
\r
1282 himlToolBarDis = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);
\r
1283 assert(himlToolBarDis);
\r
1285 ImageList_AddMasked(himlToolBarDis, BitmapDisabled, crMask);
\r
1286 SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);
\r
1290 himlToolBarDis = CreateDisabledImageList(himlToolBar);
\r
1291 SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);
\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
1302 inline BOOL CToolBar::SetIndent(int iIndent) const
\r
1303 // Sets the indentation for the first button in a toolbar control.
\r
1305 assert(::IsWindow(m_hWnd));
\r
1306 return (BOOL)SendMessage(TB_SETINDENT, (WPARAM)iIndent, 0L);
\r
1309 inline BOOL CToolBar::SetMaxTextRows(int iMaxRows) const
\r
1310 // Sets the maximum number of text rows displayed on a toolbar button.
\r
1312 assert(::IsWindow(m_hWnd));
\r
1313 return (BOOL)SendMessage(TB_SETMAXTEXTROWS, (WPARAM)iMaxRows, 0L);
\r
1316 inline BOOL CToolBar::SetPadding(int cx, int cy) const
\r
1317 // Sets the padding for a toolbar control.
\r
1319 assert(::IsWindow(m_hWnd));
\r
1320 return (BOOL)SendMessage(TB_SETPADDING, 0L, (WPARAM)MAKELONG(cx, cy));
\r
1323 inline void CToolBar::SetToolBarTheme(ToolBarTheme& Theme)
\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
1336 inline void CToolBar::SetToolTips(CToolTip* pToolTip) const
\r
1337 // Associates a ToolTip control with a toolbar.
\r
1339 assert(::IsWindow(m_hWnd));
\r
1340 HWND hToolTip = pToolTip? pToolTip->GetHwnd() : (HWND)0;
\r
1341 SendMessage(TB_SETTOOLTIPS, (WPARAM)hToolTip, 0L);
\r
1344 inline LRESULT CToolBar::WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
1351 case UWM_GETTOOLBARTHEME:
\r
1353 ToolBarTheme& tt = GetToolBarTheme();
\r
1354 return (LRESULT)&tt;
\r
1356 case WM_WINDOWPOSCHANGING:
\r
1357 OnWindowPosChanging(wParam, lParam);
\r
1361 // pass unhandled messages on for default processing
\r
1362 return CWnd::WndProcDefault(uMsg, wParam, lParam);
\r
1365 } // namespace Win32xx
\r
1367 #endif // #ifndef _WIN32XX_TOOLBAR_H_
\r