--- /dev/null
+// Win32++ Version 7.3\r
+// Released: 30th November 2011\r
+//\r
+// David Nash\r
+// url: https://sourceforge.net/projects/win32-framework\r
+//\r
+//\r
+// Copyright (c) 2005-2011 David Nash\r
+//\r
+// Permission is hereby granted, free of charge, to\r
+// any person obtaining a copy of this software and\r
+// associated documentation files (the "Software"),\r
+// to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify,\r
+// merge, publish, distribute, sublicense, and/or sell\r
+// copies of the Software, and to permit persons to whom\r
+// the Software is furnished to do so, subject to the\r
+// following conditions:\r
+//\r
+// The above copyright notice and this permission notice\r
+// shall be included in all copies or substantial portions\r
+// of the Software.\r
+//\r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF\r
+// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\r
+// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\r
+// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\r
+// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR\r
+// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
+// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE\r
+// OR OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
+////////////////////////////////////////////////////////\r
+\r
+\r
+#ifndef _WIN32XX_TOOLBAR_H_\r
+#define _WIN32XX_TOOLBAR_H_\r
+\r
+#include "wincore.h"\r
+#include "gdi.h"\r
+#include "rebar.h"\r
+\r
+\r
+namespace Win32xx\r
+{\r
+\r
+ struct ToolBarTheme\r
+ {\r
+ BOOL UseThemes; // TRUE if themes are used\r
+ COLORREF clrHot1; // Colour 1 for hot button\r
+ COLORREF clrHot2; // Colour 2 for hot button\r
+ COLORREF clrPressed1; // Colour 1 for pressed button\r
+ COLORREF clrPressed2; // Colour 2 for pressed button\r
+ COLORREF clrOutline; // Colour for border outline\r
+ };\r
+\r
+\r
+ ////////////////////////////////////\r
+ // Declaration of the CToolBar class\r
+ //\r
+ class CToolBar : public CWnd\r
+ {\r
+ public:\r
+ CToolBar();\r
+ virtual ~CToolBar();\r
+\r
+ // Operations\r
+ virtual int AddBitmap(UINT ToolBarID);\r
+ virtual BOOL AddButton(UINT nID, BOOL bEnabled = TRUE);\r
+ virtual void Destroy();\r
+ virtual BOOL ReplaceBitmap(UINT NewToolBarID);\r
+ virtual BOOL SetBitmap(UINT nID);\r
+ virtual int SetButtons(const std::vector<UINT>& vToolBarData) const;\r
+ virtual BOOL SetButtonText(int idButton, LPCTSTR szText);\r
+ virtual BOOL SetImages(COLORREF crMask, UINT ToolBarID, UINT ToolBarHotID, UINT ToolBarDisabledID);\r
+\r
+ // Wrappers for Win32 API functions\r
+ BOOL AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const;\r
+ int AddString(UINT nStringID) const;\r
+ int AddStrings(LPCTSTR lpszStrings) const;\r
+ void Autosize() const;\r
+ void CheckButton(int idButton, BOOL fCheck) const;\r
+ int CommandToIndex(int idButton) const;\r
+ BOOL DeleteButton(int iButton) const;\r
+ BOOL DisableButton(int idButton) const;\r
+ BOOL EnableButton(int idButton) const;\r
+ BOOL GetButton(int iButton, LPTBBUTTON lpButton) const;\r
+ int GetButtonCount() const;\r
+ DWORD GetButtonSize() const;\r
+ UINT GetButtonState(int idButton) const;\r
+ BYTE GetButtonStyle(int idButton) const;\r
+ CString GetButtonText(int idButton) const;\r
+ int GetCommandID(int iIndex) const;\r
+ HIMAGELIST GetDisabledImageList() const;\r
+ int GetHotItem() const;\r
+ HIMAGELIST GetHotImageList() const;\r
+ HIMAGELIST GetImageList() const;\r
+ CRect GetItemRect(int iIndex) const;\r
+ CSize GetMaxSize() const;\r
+ DWORD GetPadding() const;\r
+ CRect GetRect(int idButton) const;\r
+ int GetRows() const;\r
+ int GetTextRows() const;\r
+ CToolTip* GetToolTips() const;\r
+ BOOL HasText() const;\r
+ BOOL HideButton(int idButton, BOOL fShow) const;\r
+ int HitTest() const;\r
+ BOOL Indeterminate(int idButton, BOOL fIndeterminate) const;\r
+ BOOL InsertButton(int iButton, LPTBBUTTON lpButton) const;\r
+ BOOL IsButtonHidden(int idButton) const;\r
+ BOOL IsButtonHighlighted(int idButton) const;\r
+ BOOL IsButtonIndeterminate(int idButton) const;\r
+ BOOL IsButtonPressed(int idButton) const;\r
+ int MapAccelerator(TCHAR chAccel) const;\r
+ BOOL MarkButton(int idButton) const;\r
+ BOOL MoveButton(UINT uOldPos, UINT uNewPos) const;\r
+ BOOL PressButton(int idButton, BOOL fPress) const;\r
+ void SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const;\r
+ BOOL SetBitmapSize(int cx, int cy) const;\r
+ BOOL SetButtonSize(int cx, int cy) const;\r
+ BOOL SetButtonState(int idButton, UINT State) const;\r
+ BOOL SetButtonStyle(int idButton, BYTE Style) const;\r
+ BOOL SetButtonWidth(int idButton, int nWidth) const;\r
+ BOOL SetCommandID(int iIndex, int idButton) const;\r
+ HIMAGELIST SetDisableImageList(HIMAGELIST himlNewDisabled) const;\r
+ DWORD SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const;\r
+ DWORD SetExtendedStyle(DWORD dwExStyle) const;\r
+ HIMAGELIST SetHotImageList(HIMAGELIST himlNewHot) const;\r
+ int SetHotItem(int iHot) const;\r
+ HIMAGELIST SetImageList(HIMAGELIST himlNew) const;\r
+ BOOL SetIndent(int iIndent) const;\r
+ BOOL SetMaxTextRows(int iMaxRows) const;\r
+ BOOL SetPadding(int cx, int cy) const;\r
+ void SetToolTips(CToolTip* pToolTip) const;\r
+\r
+ // Attributes\r
+ std::vector<UINT>& GetToolBarData() const {return (std::vector <UINT> &)m_vToolBarData;}\r
+ ToolBarTheme& GetToolBarTheme() {return m_Theme;}\r
+ void SetToolBarTheme(ToolBarTheme& Theme);\r
+\r
+ protected:\r
+ // Overridables\r
+ virtual void OnCreate();\r
+ virtual void OnDestroy();\r
+ virtual void OnWindowPosChanging(WPARAM wParam, LPARAM lParam);\r
+ virtual LRESULT OnCustomDraw(NMHDR* pNMHDR);\r
+ virtual LRESULT OnNotifyReflect(WPARAM wParam, LPARAM lParam);\r
+ virtual void PreCreate(CREATESTRUCT &cs);\r
+ virtual void PreRegisterClass(WNDCLASS &wc);\r
+ virtual LRESULT WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam);\r
+\r
+ private:\r
+ CToolBar(const CToolBar&); // Disable copy construction\r
+ CToolBar& operator = (const CToolBar&); // Disable assignment operator\r
+\r
+ std::vector<UINT> m_vToolBarData; // vector of resource IDs for toolbar buttons\r
+ std::map<CString, int> m_StringMap; // a map of strings used in SetButtonText\r
+ UINT m_OldToolBarID; // Bitmap Resource ID, used in AddBitmap/ReplaceBitmap\r
+ ToolBarTheme m_Theme; // The theme structure\r
+ BOOL m_bDrawArrowBkgrnd; // True if a seperate arrow background is to be drawn\r
+\r
+ }; // class CToolBar\r
+\r
+}\r
+\r
+\r
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r
+\r
+\r
+namespace Win32xx\r
+{\r
+\r
+ ////////////////////////////////////\r
+ // Definitions for the CToolBar class\r
+ //\r
+ inline CToolBar::CToolBar() : m_OldToolBarID(0), m_bDrawArrowBkgrnd(FALSE)\r
+ {\r
+ ZeroMemory(&m_Theme, sizeof(ToolBarTheme));\r
+ }\r
+\r
+ inline CToolBar::~CToolBar()\r
+ {\r
+ }\r
+\r
+ inline int CToolBar::AddBitmap(UINT ToolBarID)\r
+ // Adds one or more images to the list of button images available for a toolbar.\r
+\r
+ // Note: AddBitmap supports a maximum colour depth of 8 bits (256 colours)\r
+ // For more colours, use an ImageList instead\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ int iNumButtons = 0;\r
+ std::vector<UINT>::iterator iter;\r
+ for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
+ if ((*iter) != 0) ++iNumButtons;\r
+\r
+ TBADDBITMAP tbab = {0};\r
+ tbab.hInst = GetApp()->GetResourceHandle();\r
+ tbab.nID = ToolBarID;\r
+ int iResult = (int)SendMessage(TB_ADDBITMAP, iNumButtons, (LPARAM)&tbab);\r
+\r
+ if (-1 != iResult)\r
+ m_OldToolBarID = ToolBarID;\r
+\r
+ return iResult;\r
+ }\r
+\r
+ inline BOOL CToolBar::AddButton(UINT nID, BOOL bEnabled /* = TRUE */)\r
+ // Adds Resource IDs to toolbar buttons.\r
+ // A resource ID of 0 is a separator\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ m_vToolBarData.push_back(nID);\r
+\r
+ // TBBUTTON structure for each button in the toolbar\r
+ TBBUTTON tbb = {0};\r
+\r
+ std::vector<UINT>::iterator iter;\r
+ int iImages = 0;\r
+ for(iter = m_vToolBarData.begin(); iter < m_vToolBarData.end(); ++iter)\r
+ if (0 != *iter) iImages++;\r
+\r
+ ZeroMemory(&tbb, sizeof(TBBUTTON));\r
+\r
+ if (0 == nID)\r
+ {\r
+ tbb.fsStyle = TBSTYLE_SEP;\r
+ }\r
+ else\r
+ {\r
+ tbb.dwData = iImages -1;\r
+ tbb.iBitmap = iImages -1;\r
+ tbb.idCommand = nID;\r
+ tbb.fsState = bEnabled? TBSTATE_ENABLED : 0;\r
+ tbb.fsStyle = TBSTYLE_BUTTON;\r
+ }\r
+\r
+ // Add the button to the toolbar\r
+ return (BOOL)SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb);\r
+ }\r
+\r
+ inline BOOL CToolBar::AddButtons(UINT uNumButtons, LPTBBUTTON lpButtons) const\r
+ // Adds one or more buttons to a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ADDBUTTONS, (LPARAM)uNumButtons, (WPARAM)lpButtons);\r
+ }\r
+\r
+ inline int CToolBar::AddString(UINT nStringID) const\r
+ // Adds a new string, passed as a resource ID, to the toolbar's internal list of strings.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_ADDSTRING, (LPARAM)GetApp()->GetResourceHandle(), (WPARAM)nStringID);\r
+ }\r
+\r
+ inline int CToolBar::AddStrings(LPCTSTR lpszStrings) const\r
+ // Adds a new string or strings to the list of strings available for a toolbar control.\r
+ // Strings in the buffer must be separated by a null character. You must ensure that the last string has two null terminators.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_ADDSTRING, 0L, (WPARAM)lpszStrings);\r
+ }\r
+\r
+ inline void CToolBar::Autosize() const\r
+ // Causes a toolbar to be resized.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ SendMessage(TB_AUTOSIZE, 0L, 0L);\r
+ }\r
+\r
+ inline void CToolBar::CheckButton(int idButton, BOOL fCheck) const\r
+ // Checks or unchecks a given button in a toolbar.\r
+ // When a button is checked, it is displayed in the pressed state.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ SendMessage(TB_CHECKBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fCheck, 0));\r
+ }\r
+\r
+ inline int CToolBar::CommandToIndex(int idButton) const\r
+ // Retrieves the zero-based index for the button associated with the specified command identifier\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ // returns -1 on fail\r
+ return (int)SendMessage(TB_COMMANDTOINDEX, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::DeleteButton(int iButton) const\r
+ // Deletes a button from the toolbar.\r
+ // iButton is the Zero-based index of the button to delete.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_DELETEBUTTON, (WPARAM)iButton, 0L);\r
+ }\r
+\r
+ inline void CToolBar::Destroy()\r
+ // Allows CToolBar to be reused after the window is destroyed\r
+ {\r
+ CWnd::Destroy();\r
+ m_StringMap.clear();\r
+ }\r
+\r
+ inline BOOL CToolBar::DisableButton(int idButton) const\r
+ // Disables the specified button in a toolbar\r
+ // An example of idButton would be IDM_FILE_OPEN\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(FALSE, 0));\r
+ }\r
+\r
+ inline BOOL CToolBar::EnableButton(int idButton) const\r
+ // Enables the specified button in a toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ENABLEBUTTON, (WPARAM)idButton, (LPARAM) MAKELONG(TRUE,0 ));\r
+ }\r
+\r
+ inline BOOL CToolBar::GetButton(int iButton, LPTBBUTTON lpButton) const\r
+ // Recieves the TBBUTTON structure information from the specified button\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_GETBUTTON, (LPARAM)iButton, (WPARAM)lpButton);\r
+ }\r
+\r
+ inline int CToolBar::GetButtonCount() const\r
+ // Retrieves a count of the buttons currently in the toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
+ }\r
+\r
+ inline DWORD CToolBar::GetButtonSize() const\r
+ // Retrieves the current width and height of toolbar buttons, in pixels.\r
+ // Returns a DWORD value that contains the width and height values in the low word and high word, respectively.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (DWORD)SendMessage(TB_GETBUTTONSIZE, 0L, 0L);\r
+ }\r
+\r
+ inline UINT CToolBar::GetButtonState(int idButton) const\r
+ // Get the state of an individual button\r
+ // TBSTATE_CHECKED The button has the TBSTYLE_CHECK style and is being clicked.\r
+ // TBSTATE_ELLIPSES The button's text is cut off and an ellipsis is displayed.\r
+ // TBSTATE_ENABLED The button accepts user input. A button that doesn't have this state is grayed.\r
+ // TBSTATE_HIDDEN The button is not visible and cannot receive user input.\r
+ // TBSTATE_INDETERMINATE The button is grayed.\r
+ // TBSTATE_MARKED The button is marked. The interpretation of a marked item is dependent upon the application.\r
+ // TBSTATE_PRESSED The button is being clicked.\r
+ // TBSTATE_WRAP The button is followed by a line break.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (UINT)SendMessage(TB_GETSTATE, (WPARAM) idButton, 0L);\r
+ }\r
+\r
+ inline BYTE CToolBar::GetButtonStyle(int idButton) const\r
+ // Get the the style of the toolbar control. The following button styles are supported:\r
+ // TBSTYLE_BUTTON Standard pushbutton (default)\r
+ // TBSTYLE_SEP Separator\r
+ // TBSTYLE_CHECK Auto check-box button\r
+ // TBSTYLE_GROUP Marks the start of a group of buttons\r
+ // TBSTYLE_CHECKGROUP Marks the start of a group of check-box buttons\r
+ // TBSTYLE_DROPDOWN Creates a drop-down list button\r
+ // TBSTYLE_AUTOSIZE The button's width will be calculated based on the text of the button, not on the size of the image\r
+ // TBSTYLE_NOPREFIX The button text will not have an accelerator prefix associated with it\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ int iIndex = CommandToIndex(idButton);\r
+ TBBUTTON tbb = {0};\r
+ SendMessage(TB_GETBUTTON, iIndex, (LPARAM) &tbb);\r
+\r
+ return tbb.fsStyle;\r
+ }\r
+\r
+ inline CString CToolBar::GetButtonText(int idButton) const\r
+ // Retrieves the display text of a button on a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ int Length = (int)SendMessage(TB_GETBUTTONTEXT, idButton, 0);\r
+ CString str;\r
+ LPTSTR szStr = str.GetBuffer(Length +1);\r
+ SendMessage(TB_GETBUTTONTEXT, (LPARAM)idButton, (WPARAM)szStr);\r
+ str.ReleaseBuffer();\r
+ return str;\r
+ }\r
+\r
+ inline int CToolBar::GetCommandID(int iIndex) const\r
+ // Retrieves information about the specified button in a toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ TBBUTTON tbb = {0};\r
+ SendMessage(TB_GETBUTTON, iIndex, (WPARAM) &tbb);\r
+\r
+ // returns zero if failed\r
+ return tbb.idCommand;\r
+ }\r
+\r
+ inline HIMAGELIST CToolBar::GetDisabledImageList() const\r
+ // Retrieves the image list that a toolbar control uses to display inactive buttons.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
+ }\r
+ \r
+ inline HIMAGELIST CToolBar::GetHotImageList() const\r
+ // Retrieves the image list that a toolbar control uses to display hot buttons.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L); \r
+ }\r
+\r
+ inline int CToolBar::GetHotItem() const\r
+ // Retrieves the index of the hot item in a toolbar, or -1 if no hot item is set.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_GETHOTITEM, 0L, 0L);\r
+ }\r
+\r
+ inline HIMAGELIST CToolBar::GetImageList() const\r
+ // Retrieves the image list that a toolbar control uses to display buttons in their default state.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
+ }\r
+\r
+ inline CRect CToolBar::GetItemRect(int iIndex) const\r
+ // Retrieves the bounding rectangle of a button in a toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ CRect rc;\r
+ int iCount = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
+\r
+ if (iCount >= iIndex)\r
+ SendMessage(TB_GETITEMRECT, (WPARAM)iIndex, (LPARAM)&rc);\r
+\r
+ return rc;\r
+ }\r
+\r
+ inline CSize CToolBar::GetMaxSize() const\r
+ // Retrieves the total size of all of the visible buttons and separators in the toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ CSize sz;\r
+ SendMessage(TB_GETMAXSIZE, 0L, (LPARAM)&sz);\r
+\r
+ // This fixes a Windows bug calculating the size when TBSTYLE_DROPDOWN is used.\r
+ int xMaxSize = 0;\r
+ for (int i= 0 ; i < GetButtonCount(); ++i)\r
+ {\r
+ xMaxSize += GetItemRect(i).Width();\r
+ }\r
+\r
+ sz.cx = xMaxSize;\r
+ return sz;\r
+ }\r
+\r
+ inline DWORD CToolBar::GetPadding() const\r
+ // Returns a DWORD value that contains the horizontal padding in the low word and the vertical padding in the high word, in pixels.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (DWORD)SendMessage(TB_GETPADDING, 0L, 0L);\r
+ }\r
+\r
+ inline CRect CToolBar::GetRect(int idButton) const\r
+ // Retrieves the bounding rectangle for a specified toolbar button.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ CRect rc;\r
+ SendMessage(TB_GETRECT, (WPARAM)idButton, (LPARAM)&rc);\r
+ return rc;\r
+ }\r
+\r
+ inline int CToolBar::GetRows() const\r
+ // Retrieves the number of rows of buttons in a toolbar with the TBSTYLE_WRAPABLE style.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_GETROWS, 0L, 0L);\r
+ }\r
+\r
+ inline int CToolBar::GetTextRows() const\r
+ // Retrieves the maximum number of text rows that can be displayed on a toolbar button.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_GETTEXTROWS, 0L, 0L);\r
+ }\r
+\r
+ inline CToolTip* CToolBar::GetToolTips() const\r
+ // Retrieves the handle to the ToolTip control, if any, associated with the toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (CToolTip*)FromHandle( (HWND)SendMessage(TB_GETTOOLTIPS, 0L, 0L) );\r
+ }\r
+\r
+ inline BOOL CToolBar::HasText() const\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ BOOL bReturn = FALSE;\r
+\r
+ for (int i = 0 ; i < GetButtonCount(); ++i)\r
+ {\r
+ if (SendMessage(TB_GETBUTTONTEXT, GetCommandID(i), 0L) != -1)\r
+ bReturn = TRUE;\r
+ }\r
+\r
+ // return TRUE if any button has text\r
+ return bReturn;\r
+ }\r
+\r
+ inline BOOL CToolBar::HideButton(int idButton, BOOL fShow) const\r
+ //Hides or shows the specified button in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_HIDEBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG (fShow, 0));\r
+ }\r
+\r
+ inline int CToolBar::HitTest() const\r
+ // Determines where a point lies in a toolbar control.\r
+\r
+ // We do our own hit test since TB_HITTEST is a bit buggy,\r
+ // and also doesn't work at all on earliest versions of Win95\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ CPoint pt = GetCursorPos();\r
+ ScreenToClient(pt);\r
+\r
+ int nButtons = (int)SendMessage(TB_BUTTONCOUNT, 0L, 0L);\r
+ int iButton = -1;\r
+\r
+ for (int i = 0 ; i < nButtons; ++i)\r
+ {\r
+ CRect rc = GetItemRect(i);\r
+ if (rc.PtInRect(pt))\r
+ iButton = i;\r
+ }\r
+\r
+ return iButton;\r
+ }\r
+\r
+ inline BOOL CToolBar::Indeterminate(int idButton, BOOL fIndeterminate) const\r
+ //Hides or shows the specified button in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_INDETERMINATE, (WPARAM)idButton, (LPARAM)MAKELONG (fIndeterminate, 0));\r
+ }\r
+\r
+ inline BOOL CToolBar::InsertButton(int iButton, LPTBBUTTON lpButton) const\r
+ // Inserts a button in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_INSERTBUTTON, (WPARAM)iButton, (LPARAM)lpButton);\r
+ }\r
+\r
+ inline BOOL CToolBar::IsButtonHidden(int idButton) const\r
+ // Determines whether the specified button in a toolbar is hidden.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ISBUTTONHIDDEN, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::IsButtonHighlighted(int idButton) const\r
+ // Checks the highlight state of a toolbar button.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ISBUTTONHIGHLIGHTED, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::IsButtonIndeterminate(int idButton) const\r
+ // Determines whether the specified button in a toolbar is indeterminate.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ISBUTTONINDETERMINATE, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::IsButtonPressed(int idButton) const\r
+ // Determines whether the specified button in a toolbar is pressed.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_ISBUTTONPRESSED, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline int CToolBar::MapAccelerator(TCHAR chAccel) const\r
+ // Determines whether the specified button in a toolbar is pressed.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ int uButtonID;\r
+ int idButton;\r
+ if (SendMessage(TB_MAPACCELERATOR, (WPARAM)chAccel, (LPARAM)&uButtonID))\r
+ idButton = uButtonID;\r
+ else\r
+ idButton = -1;\r
+\r
+ return idButton;\r
+ }\r
+\r
+ inline BOOL CToolBar::MarkButton(int idButton) const\r
+ // Sets the highlight state of a given button in a toolbar control.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_MARKBUTTON, (WPARAM)idButton, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::MoveButton(UINT uOldPos, UINT uNewPos) const\r
+ // Moves a button from one index to another.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_MOVEBUTTON, (WPARAM)uOldPos, (LPARAM)uNewPos);\r
+ }\r
+\r
+\r
+ inline void CToolBar::OnCreate()\r
+ {\r
+ // We must send this message before sending the TB_ADDBITMAP or TB_ADDBUTTONS message\r
+ SendMessage(TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0L);\r
+\r
+ // allows buttons to have a separate dropdown arrow\r
+ // Note: TBN_DROPDOWN notification is sent by a toolbar control when the user clicks a dropdown button\r
+ SendMessage(TB_SETEXTENDEDSTYLE, 0L, TBSTYLE_EX_DRAWDDARROWS);\r
+\r
+ // Turn of Double click processing (i.e. treat a double click as two single clicks)\r
+ DWORD dwStyle = (DWORD)GetClassLongPtr(GCL_STYLE);\r
+ dwStyle &= ~CS_DBLCLKS;\r
+ SetClassLongPtr(GCL_STYLE, dwStyle);\r
+\r
+ // Add extra styles for toolbars inside a rebar\r
+ if (lstrcmp(GetParent()->GetClassName(), _T("ReBarWindow32")) == 0)\r
+ {\r
+ DWORD style = (DWORD)GetWindowLongPtr(GWL_STYLE);\r
+ style |= CCS_NODIVIDER | CCS_NORESIZE;\r
+ SetWindowLongPtr(GWL_STYLE, style);\r
+ }\r
+\r
+ SetButtons(m_vToolBarData);\r
+\r
+ // Set rows of text to zero\r
+ SendMessage(TB_SETMAXTEXTROWS, 0L, 0L);\r
+ }\r
+\r
+ inline LRESULT CToolBar::OnCustomDraw(NMHDR* pNMHDR)\r
+ // With CustomDraw we manually control the drawing of each toolbar button\r
+ {\r
+ LPNMTBCUSTOMDRAW lpNMCustomDraw = (LPNMTBCUSTOMDRAW)pNMHDR;\r
+\r
+ switch (lpNMCustomDraw->nmcd.dwDrawStage)\r
+ {\r
+ // Begin paint cycle\r
+ case CDDS_PREPAINT:\r
+ // Send NM_CUSTOMDRAW item draw, and post-paint notification messages.\r
+ return CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT ;\r
+\r
+ // An item is about to be drawn\r
+ case CDDS_ITEMPREPAINT:\r
+ {\r
+ CDC DrawDC(lpNMCustomDraw->nmcd.hdc);\r
+ CRect rcRect = lpNMCustomDraw->nmcd.rc;\r
+ int nState = lpNMCustomDraw->nmcd.uItemState;\r
+ DWORD dwItem = (DWORD)lpNMCustomDraw->nmcd.dwItemSpec;\r
+ DWORD dwTBStyle = (DWORD)SendMessage(TB_GETSTYLE, 0L, 0L);\r
+ int nStyle = GetButtonStyle(dwItem);\r
+\r
+ int nButton = (int)SendMessage(TB_COMMANDTOINDEX, (WPARAM) dwItem, 0L);\r
+ TBBUTTON tbb = {0};\r
+ SendMessage(TB_GETBUTTON, nButton, (LPARAM)&tbb);\r
+ int iImage = (int)tbb.dwData;\r
+\r
+ // Calculate text size\r
+ std::vector<TCHAR> vText(MAX_MENU_STRING, _T('\0'));\r
+ TCHAR* pszText = &vText[0];\r
+ CSize TextSize;\r
+ if (HasText()) // Does any button have text?\r
+ {\r
+ DrawDC.SelectObject(GetFont());\r
+ if (SendMessage(TB_GETBUTTONTEXT, dwItem, (LPARAM)pszText)> 0)\r
+ {\r
+ TextSize = DrawDC.GetTextExtentPoint32(pszText, lstrlen(pszText));\r
+ }\r
+ }\r
+\r
+ // Draw outline rectangle\r
+ if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))\r
+ {\r
+ DrawDC.CreatePen(PS_SOLID, 1, m_Theme.clrOutline);\r
+ DrawDC.MoveTo(rcRect.left, rcRect.top);\r
+ DrawDC.LineTo(rcRect.left, rcRect.bottom-1);\r
+ DrawDC.LineTo(rcRect.right-1, rcRect.bottom-1);\r
+ DrawDC.LineTo(rcRect.right-1, rcRect.top);\r
+ DrawDC.LineTo(rcRect.left, rcRect.top);\r
+ }\r
+\r
+ // Draw filled gradient background\r
+ rcRect.InflateRect(-1, -1);\r
+ if ((nState & (CDIS_SELECTED|CDIS_CHECKED)) || (GetButtonState(dwItem) & TBSTATE_PRESSED))\r
+ {\r
+ DrawDC.GradientFill(m_Theme.clrPressed1, m_Theme.clrPressed2, rcRect, FALSE);\r
+ }\r
+ else if (nState & CDIS_HOT)\r
+ {\r
+ DrawDC.GradientFill(m_Theme.clrHot1, m_Theme.clrHot2, rcRect, FALSE);\r
+ }\r
+\r
+ // Get the appropriate image list depending on the button state\r
+ HIMAGELIST himlToolBar;\r
+ if (nState & CDIS_DISABLED)\r
+ {\r
+ himlToolBar = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
+ }\r
+ else if (nState & (CDIS_HOT | CDIS_SELECTED | CDIS_CHECKED))\r
+ {\r
+ himlToolBar = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
+ if (0 == himlToolBar)\r
+ himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
+ }\r
+ else\r
+ {\r
+ himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
+ }\r
+\r
+ BOOL IsWin95 = (1400 == (GetWinVersion()) || (2400 == GetWinVersion()));\r
+\r
+ // Calculate image position\r
+ int cxImage = 0;\r
+ int cyImage = 0;\r
+ ImageList_GetIconSize(himlToolBar, &cxImage, &cyImage);\r
+\r
+ int yImage = (rcRect.bottom - rcRect.top - cyImage - TextSize.cy +2)/2;\r
+ int xImage = (rcRect.right + rcRect.left - cxImage)/2 + ((nState & (CDIS_SELECTED|CDIS_CHECKED))? 1:0);\r
+ if (dwTBStyle & TBSTYLE_LIST)\r
+ {\r
+ xImage = rcRect.left + (IsXPThemed()?2:4) + ((nState & CDIS_SELECTED)? 1:0);\r
+ yImage = (rcRect.bottom -rcRect.top - cyImage +2)/2 + ((nState & (CDIS_SELECTED|CDIS_CHECKED))? 1:0);\r
+ }\r
+\r
+ // Handle the TBSTYLE_DROPDOWN and BTNS_WHOLEDROPDOWN styles\r
+ if ((nStyle & TBSTYLE_DROPDOWN) || ((nStyle & 0x0080) && (!IsWin95)))\r
+ {\r
+ // Calculate the dropdown arrow position\r
+ int xAPos = (nStyle & TBSTYLE_DROPDOWN)? rcRect.right -6 : (rcRect.right + rcRect.left + cxImage + 4)/2;\r
+ int yAPos = (nStyle & TBSTYLE_DROPDOWN)? (rcRect.bottom - rcRect.top +1)/2 : (cyImage)/2;\r
+ if (dwTBStyle & TBSTYLE_LIST)\r
+ {\r
+ xAPos = (nStyle & TBSTYLE_DROPDOWN)?rcRect.right -6:rcRect.right -5;\r
+ yAPos = (rcRect.bottom - rcRect.top +1)/2 + ((nStyle & TBSTYLE_DROPDOWN)?0:1);\r
+ }\r
+\r
+ xImage -= (nStyle & TBSTYLE_DROPDOWN)?((dwTBStyle & TBSTYLE_LIST)? (IsXPThemed()?-4:0):6):((dwTBStyle & TBSTYLE_LIST)? 0:4);\r
+\r
+ // Draw separate background for dropdown arrow\r
+ if ((m_bDrawArrowBkgrnd) && (nState & CDIS_HOT))\r
+ {\r
+ CRect rcArrowBkgnd = rcRect;\r
+ rcArrowBkgnd.left = rcArrowBkgnd.right - 13;\r
+ DrawDC.GradientFill(m_Theme.clrPressed1, m_Theme.clrPressed2, rcArrowBkgnd, FALSE);\r
+ }\r
+\r
+ m_bDrawArrowBkgrnd = FALSE;\r
+\r
+ // Manually draw the dropdown arrow\r
+ DrawDC.CreatePen(PS_SOLID, 1, RGB(0,0,0));\r
+ for (int i = 2; i >= 0; --i)\r
+ {\r
+ DrawDC.MoveTo(xAPos -i-1, yAPos - i+1);\r
+ DrawDC.LineTo(xAPos +i, yAPos - i+1);\r
+ }\r
+\r
+ // Draw line between icon and dropdown arrow\r
+ if ((nStyle & TBSTYLE_DROPDOWN) && ((nState & CDIS_SELECTED) || nState & CDIS_HOT))\r
+ {\r
+ DrawDC.CreatePen(PS_SOLID, 1, m_Theme.clrOutline);\r
+ DrawDC.MoveTo(rcRect.right - 13, rcRect.top);\r
+ DrawDC.LineTo(rcRect.right - 13, rcRect.bottom);\r
+ }\r
+ }\r
+\r
+ // Draw the button image\r
+ if (xImage > 0)\r
+ {\r
+ ImageList_Draw(himlToolBar, iImage, DrawDC, xImage, yImage, ILD_TRANSPARENT);\r
+ }\r
+\r
+ //Draw Text\r
+ if (lstrlen(pszText) > 0)\r
+ {\r
+ int iWidth = rcRect.right - rcRect.left - ((nStyle & TBSTYLE_DROPDOWN)?13:0);\r
+ CRect rcText(0, 0, MIN(TextSize.cx, iWidth), TextSize.cy);\r
+\r
+ int xOffset = (rcRect.right + rcRect.left - rcText.right + rcText.left - ((nStyle & TBSTYLE_DROPDOWN)? 11 : 1))/2;\r
+ int yOffset = yImage + cyImage +1;\r
+\r
+ if (dwTBStyle & TBSTYLE_LIST)\r
+ {\r
+ xOffset = rcRect.left + cxImage + ((nStyle & TBSTYLE_DROPDOWN)?(IsXPThemed()?10:6): 6) + ((nState & CDIS_SELECTED)? 1:0);\r
+ yOffset = (2+rcRect.bottom - rcRect.top - rcText.bottom + rcText.top)/2 + ((nState & CDIS_SELECTED)? 1:0);\r
+ rcText.right = MIN(rcText.right, rcRect.right - xOffset);\r
+ }\r
+\r
+ OffsetRect(&rcText, xOffset, yOffset);\r
+\r
+ int iMode = DrawDC.SetBkMode(TRANSPARENT);\r
+ DrawDC.SelectObject(GetFont());\r
+\r
+ if (nState & (CDIS_DISABLED))\r
+ {\r
+ // Draw text twice for embossed look\r
+ rcText.OffsetRect(1, 1);\r
+ DrawDC.SetTextColor(RGB(255,255,255));\r
+ DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT);\r
+ rcText.OffsetRect(-1, -1);\r
+ DrawDC.SetTextColor(GetSysColor(COLOR_GRAYTEXT));\r
+ DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT);\r
+ }\r
+ else\r
+ {\r
+ DrawDC.SetTextColor(GetSysColor(COLOR_BTNTEXT));\r
+ DrawDC.DrawText(pszText, lstrlen(pszText), rcText, DT_LEFT | DT_END_ELLIPSIS);\r
+ }\r
+ DrawDC.SetBkMode(iMode);\r
+\r
+ }\r
+ DrawDC.Detach();\r
+ }\r
+ return CDRF_SKIPDEFAULT; // No further drawing\r
+ }\r
+ return 0L;\r
+ }\r
+\r
+ inline void CToolBar::OnDestroy()\r
+ {\r
+ HIMAGELIST himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
+ HIMAGELIST himlToolBarHot = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
+ HIMAGELIST himlToolBarDis = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
+ ImageList_Destroy(himlToolBar);\r
+ ImageList_Destroy(himlToolBarHot);\r
+ ImageList_Destroy(himlToolBarDis);\r
+ }\r
+\r
+ inline LRESULT CToolBar::OnNotifyReflect(WPARAM wParam, LPARAM lParam)\r
+ // Notifications sent to the parent window are reflected back here\r
+ {\r
+ UNREFERENCED_PARAMETER(wParam);\r
+\r
+ switch (((LPNMHDR)lParam)->code)\r
+ {\r
+ case NM_CUSTOMDRAW:\r
+ {\r
+ if ((m_Theme.UseThemes) && (GetComCtlVersion() > 470))\r
+ return OnCustomDraw((LPNMHDR) lParam);\r
+ }\r
+ break;\r
+\r
+ case TBN_DROPDOWN:\r
+ {\r
+ int iItem = ((LPNMTOOLBAR) lParam)->iItem;\r
+\r
+ // a boolean expression\r
+ m_bDrawArrowBkgrnd = (GetButtonStyle(iItem) & TBSTYLE_DROPDOWN);\r
+ }\r
+ break;\r
+ }\r
+ return 0L;\r
+ }\r
+\r
+ inline void CToolBar::OnWindowPosChanging(WPARAM wParam, LPARAM lParam)\r
+ {\r
+ UNREFERENCED_PARAMETER(wParam);\r
+\r
+ // Adjust size for toolbars inside a rebar\r
+ CWnd* pParent = GetParent();\r
+ if (lstrcmp(pParent->GetClassName(), _T("ReBarWindow32")) == 0)\r
+ {\r
+ ReBarTheme* pTheme = (ReBarTheme*)pParent->SendMessage(UWM_GETREBARTHEME, 0, 0);\r
+\r
+ if (pTheme && pTheme->UseThemes && pTheme->ShortBands)\r
+ {\r
+ LPWINDOWPOS pWinPos = (LPWINDOWPOS)lParam;\r
+ pWinPos->cx = GetMaxSize().cx+2;\r
+ }\r
+ }\r
+ }\r
+\r
+ inline void CToolBar::PreCreate(CREATESTRUCT &cs)\r
+ {\r
+ // Sets the CREATESTRUCT parameters prior to window creation\r
+ cs.style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT;\r
+ }\r
+\r
+ inline void CToolBar::PreRegisterClass(WNDCLASS &wc)\r
+ {\r
+ // Set the Window Class\r
+ wc.lpszClassName = TOOLBARCLASSNAME;\r
+ }\r
+\r
+ inline BOOL CToolBar::PressButton(int idButton, BOOL fPress) const\r
+ // Presses or releases the specified button in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_PRESSBUTTON, (WPARAM)idButton, (LPARAM)MAKELONG(fPress, 0));\r
+ }\r
+\r
+ inline BOOL CToolBar::ReplaceBitmap(UINT NewToolBarID)\r
+ // Replaces an existing bitmap with a new bitmap.\r
+\r
+ // Note: ReplaceBitmap supports a maximum colour depth of 8 bits (256 colours)\r
+ // For more colours, use an ImageList instead\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ int iNumButtons = 0;\r
+ std::vector<UINT>::iterator iter;\r
+ for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
+ if ((*iter) != 0) ++iNumButtons;\r
+\r
+ TBREPLACEBITMAP tbrb = {0};\r
+ tbrb.hInstNew = GetApp()->GetResourceHandle();\r
+ tbrb.hInstOld = GetApp()->GetResourceHandle();\r
+ tbrb.nIDNew = NewToolBarID;\r
+ tbrb.nIDOld = m_OldToolBarID;\r
+ tbrb.nButtons = iNumButtons;\r
+\r
+ BOOL bResult = (BOOL)SendMessage(TB_REPLACEBITMAP, iNumButtons, (LPARAM)&tbrb);\r
+ if (bResult)\r
+ m_OldToolBarID = NewToolBarID;\r
+\r
+ return bResult;\r
+ }\r
+\r
+ inline void CToolBar::SaveRestore(BOOL fSave, TBSAVEPARAMS* ptbsp) const\r
+ // Presses or releases the specified button in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ SendMessage(TB_PRESSBUTTON, (WPARAM)fSave, (LPARAM)ptbsp);\r
+ }\r
+\r
+ inline BOOL CToolBar::SetBitmap(UINT nID)\r
+ // Set the button images\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ CBitmap Bitmap(nID);\r
+ assert (Bitmap.GetHandle());\r
+ BITMAP bm = Bitmap.GetBitmapData();\r
+\r
+ int iNumButtons = 0;\r
+ std::vector<UINT>::iterator iter;\r
+ for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
+ {\r
+ if ((*iter) != 0) \r
+ ++iNumButtons;\r
+ }\r
+\r
+ int iImageWidth = bm.bmWidth / iNumButtons;\r
+ int iImageHeight = bm.bmHeight;\r
+\r
+ // Set the bitmap size first\r
+ SetBitmapSize(iImageWidth, iImageHeight);\r
+\r
+ BOOL bResult = FALSE;\r
+ if (m_OldToolBarID)\r
+ bResult = ReplaceBitmap(nID);\r
+ else\r
+ bResult = (BOOL)AddBitmap(nID);\r
+\r
+ return bResult;\r
+ }\r
+\r
+ inline BOOL CToolBar::SetBitmapSize(int cx, int cy) const\r
+ // Sets the size of the bitmapped images to be added to a toolbar.\r
+\r
+ // Needs to be used when the image size is not the default 16 x 15\r
+ // Call this function before using AddBitmap or ReplaceBitmap\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETBITMAPSIZE, 0L, MAKELONG(cx, cy));\r
+ }\r
+\r
+ inline int CToolBar::SetButtons(const std::vector<UINT>& vToolBarData) const\r
+ // Assigns a resource ID to each toolbar button\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ int iImages = 0;\r
+ UINT iNumButtons = (UINT)vToolBarData.size();\r
+\r
+ // Remove any existing buttons\r
+ while (SendMessage(TB_BUTTONCOUNT, 0L, 0L) > 0)\r
+ {\r
+ if(!SendMessage(TB_DELETEBUTTON, 0L, 0L))\r
+ break;\r
+ }\r
+\r
+ if (iNumButtons > 0)\r
+ {\r
+ // TBBUTTON structure for each button in the toolbar\r
+ TBBUTTON tbb = {0};\r
+\r
+ for (UINT j = 0 ; j < iNumButtons; ++j)\r
+ {\r
+ ZeroMemory(&tbb, sizeof(TBBUTTON));\r
+\r
+ if (0 == vToolBarData[j])\r
+ {\r
+ tbb.fsStyle = TBSTYLE_SEP;\r
+ }\r
+ else\r
+ {\r
+ tbb.dwData = iImages;\r
+ tbb.iBitmap = iImages;\r
+ tbb.idCommand = vToolBarData[j];\r
+ tbb.fsState = TBSTATE_ENABLED;\r
+ tbb.fsStyle = TBSTYLE_BUTTON;\r
+ }\r
+\r
+ // Add the button to the toolbar\r
+ if (SendMessage(TB_ADDBUTTONS, 1L, (LPARAM)&tbb))\r
+ iImages++;\r
+ else\r
+ break;\r
+ }\r
+ }\r
+\r
+ return iImages;\r
+ }\r
+\r
+ inline BOOL CToolBar::SetButtonSize(int cx, int cy) const\r
+ // Sets the size of the buttons to be added to a toolbar\r
+ // The size can be set only before adding any buttons to the toolbar\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETBUTTONSIZE, 0L, MAKELONG(cx, cy));\r
+ }\r
+\r
+ inline BOOL CToolBar::SetButtonState(int idButton, UINT State) const\r
+ {\r
+ // Set the state of an individual button\r
+ // TBSTATE_CHECKED The button has the TBSTYLE_CHECK style and is being clicked.\r
+ // TBSTATE_ELLIPSES The button's text is cut off and an ellipsis is displayed.\r
+ // TBSTATE_ENABLED The button accepts user input. A button that doesn't have this state is grayed.\r
+ // TBSTATE_HIDDEN The button is not visible and cannot receive user input.\r
+ // TBSTATE_INDETERMINATE The button is grayed.\r
+ // TBSTATE_MARKED The button is marked. The interpretation of a marked item is dependent upon the application.\r
+ // TBSTATE_PRESSED The button is being clicked.\r
+ // TBSTATE_WRAP The button is followed by a line break.\r
+\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETSTATE, (WPARAM) idButton, (LPARAM)MAKELONG (State, 0));\r
+ }\r
+\r
+ inline BOOL CToolBar::SetButtonStyle(int idButton, BYTE Style) const\r
+ // The the style of the toolbar control. The following button styles are supported:\r
+ // TBSTYLE_BUTTON Standard pushbutton (default)\r
+ // TBSTYLE_SEP Separator\r
+ // TBSTYLE_CHECK Auto check-box button\r
+ // TBSTYLE_GROUP Marks the start of a group of buttons\r
+ // TBSTYLE_CHECKGROUP Marks the start of a group of check-box buttons\r
+ // TBSTYLE_DROPDOWN Creates a drop-down list button\r
+ // TBSTYLE_AUTOSIZE The button's width will be calculated based on the text of the button, not on the size of the image\r
+ // TBSTYLE_NOPREFIX The button text will not have an accelerator prefix associated with it\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ TBBUTTONINFO tbbi = {0};\r
+ tbbi.cbSize = sizeof(TBBUTTONINFO);\r
+ tbbi.dwMask = TBIF_STYLE;\r
+ tbbi.fsStyle = Style;\r
+\r
+ // Note: TB_SETBUTTONINFO requires comctl32.dll version 4.71 or later\r
+ // i.e. Win95 with IE4 / NT with IE4 or later\r
+ return (BOOL)SendMessage(TB_SETBUTTONINFO, idButton, (LPARAM) &tbbi);\r
+ }\r
+\r
+ inline BOOL CToolBar::SetButtonText(int idButton, LPCTSTR szText)\r
+ // This rather convoluted approach to setting toolbar button text supports\r
+ // all versions of Windows, including Win95 with COMCTL32.DLL version 4.0\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ int iIndex = CommandToIndex(idButton);\r
+ assert(-1 != iIndex);\r
+\r
+ BOOL Succeeded = TRUE;\r
+ CString sString = szText;\r
+ std::map<CString, int>::iterator m;\r
+ int iString;\r
+\r
+ // Check to see if the string is already added\r
+ m = m_StringMap.find(sString);\r
+ if (m_StringMap.end() == m)\r
+ {\r
+ if (0 == m_StringMap.size())\r
+ {\r
+ // Place a blank string first in the string table, in case some\r
+ // buttons don't have text\r
+ TCHAR szString[3] = _T(" ");\r
+ szString[2] = _T('\0'); // Double-null terminate\r
+ SendMessage(TB_ADDSTRING, 0L, (LPARAM)szString);\r
+ }\r
+\r
+ // No index for this string exists, so create it now\r
+ TCHAR szBuf[80] = _T("");\r
+ lstrcpyn(szBuf, szText, 79);\r
+ szBuf[lstrlen(szBuf)+1] = _T('\0'); // Double-null terminate\r
+\r
+ iString = (int)SendMessage(TB_ADDSTRING, 0L, (LPARAM)szBuf);\r
+ if (-1 == iString )\r
+ Succeeded = FALSE;\r
+\r
+ // Save the string its index in our map\r
+ m_StringMap.insert(std::make_pair(sString, iString));\r
+ }\r
+ else\r
+ {\r
+ // String found, use the index from our map\r
+ iString = m->second;\r
+ }\r
+\r
+ if (Succeeded)\r
+ {\r
+ TBBUTTON tbb = {0};\r
+ Succeeded = (BOOL)SendMessage(TB_GETBUTTON, iIndex, (LPARAM)&tbb);\r
+\r
+ tbb.iString = iString;\r
+\r
+ // Turn off ToolBar drawing\r
+ SendMessage(WM_SETREDRAW, FALSE, 0L);\r
+\r
+ if (Succeeded)\r
+ Succeeded = (BOOL)SendMessage(TB_DELETEBUTTON, iIndex, 0L);\r
+\r
+ if (Succeeded)\r
+ Succeeded = (BOOL)SendMessage(TB_INSERTBUTTON, iIndex, (LPARAM)&tbb);\r
+\r
+ // Ensure the button now includes some text rows\r
+ if (0 == SendMessage(TB_GETTEXTROWS, 0L, 0L))\r
+ SendMessage(TB_SETMAXTEXTROWS, 1L, 0L);\r
+\r
+ // Turn on ToolBar drawing\r
+ SendMessage(WM_SETREDRAW, TRUE, 0L);\r
+ }\r
+ // Redraw button\r
+ CRect r = GetItemRect(iIndex);\r
+ InvalidateRect(&r, TRUE);\r
+\r
+ return Succeeded;\r
+ }\r
+\r
+ inline BOOL CToolBar::SetButtonWidth(int idButton, int nWidth) const\r
+ // The set button width can adjust the width of the button after it is created.\r
+ // This is useful when replacing a button with a ComboBox or other control.\r
+ // Note: TB_SETBUTTONINFO requires comctl32.dll version 4.71 or later\r
+ // i.e. Win95 with IE4 / NT with IE4 or later\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ TBBUTTONINFO tbbi = {0};\r
+ tbbi.cbSize = sizeof(TBBUTTONINFO);\r
+ tbbi.dwMask = TBIF_SIZE;\r
+ tbbi.cx = (WORD)nWidth;\r
+ BOOL bResult = (BOOL)SendMessage(TB_SETBUTTONINFO, (WPARAM)idButton, (LPARAM)&tbbi);\r
+\r
+ // Send a changed message to the parent (used by the rebar)\r
+ SIZE MaxSize = GetMaxSize();\r
+ GetParent()->SendMessage(UWM_TOOLBAR_RESIZE, (WPARAM)m_hWnd, (LPARAM)&MaxSize);\r
+\r
+ return bResult;\r
+ }\r
+\r
+ inline BOOL CToolBar::SetCommandID(int iIndex, int idButton) const\r
+ // Sets the command identifier of a toolbar button\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETCMDID, iIndex, idButton);\r
+ }\r
+\r
+ inline HIMAGELIST CToolBar::SetDisableImageList(HIMAGELIST himlNewDisabled) const\r
+ // Sets the image list that the toolbar control will use to display disabled buttons.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlNewDisabled);\r
+ }\r
+\r
+ inline DWORD CToolBar::SetDrawTextFlags(DWORD dwMask, DWORD dwDTFlags) const\r
+ // Sets the text drawing flags for the toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (DWORD)SendMessage(TB_SETDRAWTEXTFLAGS, (WPARAM)dwMask, (LPARAM)dwDTFlags);\r
+ }\r
+\r
+ inline DWORD CToolBar::SetExtendedStyle(DWORD dwExStyle) const\r
+ // Sets the text drawing flags for the toolbar.\r
+ // Extended styles include: TBSTYLE_EX_DRAWDDARROWS, TBSTYLE_EX_HIDECLIPPEDBUTTONS, TBSTYLE_EX_DOUBLEBUFFER and TBSTYLE_EX_MIXEDBUTTONS\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (DWORD)SendMessage(TB_SETEXTENDEDSTYLE, 0L, (LPARAM)dwExStyle);\r
+ }\r
+\r
+ inline HIMAGELIST CToolBar::SetHotImageList(HIMAGELIST himlNewHot) const\r
+ // Sets the image list that the toolbar control will use to display hot buttons.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlNewHot);\r
+ }\r
+ \r
+ inline int CToolBar::SetHotItem(int iHot) const\r
+ // Sets the hot item in a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (int)SendMessage(TB_SETHOTITEM, (WPARAM)iHot, 0L);\r
+ }\r
+\r
+ inline HIMAGELIST CToolBar::SetImageList(HIMAGELIST himlNew) const\r
+ // Sets the image list that the toolbar will use to display buttons that are in their default state.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (HIMAGELIST)SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlNew);\r
+ }\r
+\r
+ inline BOOL CToolBar::SetImages(COLORREF crMask, UINT ToolBarID, UINT ToolBarHotID, UINT ToolBarDisabledID)\r
+ // Either sets the imagelist or adds/replaces bitmap depending on ComCtl32.dll version\r
+ // Assumes the width of the button image = bitmap_size / buttons\r
+ // Assumes buttons have been already been added via AdddToolBarButton\r
+ // The colour mask is often grey RGB(192,192,192) or magenta (255,0,255);\r
+ // The color mask is ignored for 32bit bitmap resources\r
+ // The Hot and disiabled bitmap resources can be 0\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+\r
+ // ToolBar ImageLists require Comctl32.dll version 4.7 or later\r
+ if (GetComCtlVersion() < 470)\r
+ {\r
+ // We are using COMCTL32.DLL version 4.0, so we can't use an imagelist.\r
+ // Instead we simply set the bitmap.\r
+ return SetBitmap(ToolBarID);\r
+ }\r
+\r
+ int iNumButtons = 0;\r
+ std::vector<UINT>::iterator iter;\r
+ for (iter = GetToolBarData().begin(); iter < GetToolBarData().end(); ++iter)\r
+ if ((*iter) != 0) ++iNumButtons;\r
+\r
+ if (iNumButtons > 0)\r
+ {\r
+ // Set the button images\r
+ CBitmap Bitmap(ToolBarID);\r
+ assert(Bitmap.GetHandle());\r
+\r
+ BITMAP bm = Bitmap.GetBitmapData();\r
+ int iImageWidth = bm.bmWidth / iNumButtons;\r
+ int iImageHeight = bm.bmHeight;\r
+\r
+ HIMAGELIST himlToolBar = (HIMAGELIST)SendMessage(TB_GETIMAGELIST, 0L, 0L);\r
+ HIMAGELIST himlToolBarHot = (HIMAGELIST)SendMessage(TB_GETHOTIMAGELIST, 0L, 0L);\r
+ HIMAGELIST himlToolBarDis = (HIMAGELIST)SendMessage(TB_GETDISABLEDIMAGELIST, 0L, 0L);\r
+ ImageList_Destroy(himlToolBar);\r
+ ImageList_Destroy(himlToolBarHot);\r
+ ImageList_Destroy(himlToolBarDis);\r
+\r
+ himlToolBar = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
+ assert(himlToolBar);\r
+\r
+ ImageList_AddMasked(himlToolBar, Bitmap, crMask);\r
+ SendMessage(TB_SETIMAGELIST, 0L, (LPARAM)himlToolBar);\r
+\r
+ if (ToolBarHotID)\r
+ {\r
+ CBitmap BitmapHot(ToolBarHotID);\r
+ assert(BitmapHot);\r
+\r
+ himlToolBarHot = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
+ assert(himlToolBarHot);\r
+\r
+ ImageList_AddMasked(himlToolBarHot, BitmapHot, crMask);\r
+ SendMessage(TB_SETHOTIMAGELIST, 0L, (LPARAM)himlToolBarHot);\r
+ }\r
+\r
+ if (ToolBarDisabledID)\r
+ {\r
+ CBitmap BitmapDisabled(ToolBarDisabledID);\r
+ assert(BitmapDisabled);\r
+\r
+ himlToolBarDis = ImageList_Create(iImageWidth, iImageHeight, ILC_COLOR32 | ILC_MASK, iNumButtons, 0);\r
+ assert(himlToolBarDis);\r
+\r
+ ImageList_AddMasked(himlToolBarDis, BitmapDisabled, crMask);\r
+ SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);\r
+ }\r
+ else\r
+ {\r
+ himlToolBarDis = CreateDisabledImageList(himlToolBar);\r
+ SendMessage(TB_SETDISABLEDIMAGELIST, 0L, (LPARAM)himlToolBarDis);\r
+ }\r
+\r
+ // Inform the parent of the change (rebar needs this)\r
+ SIZE MaxSize = GetMaxSize();\r
+ GetParent()->SendMessage(UWM_TOOLBAR_RESIZE, (WPARAM)m_hWnd, (LPARAM)&MaxSize);\r
+ }\r
+\r
+ return TRUE;\r
+ }\r
+\r
+ inline BOOL CToolBar::SetIndent(int iIndent) const\r
+ // Sets the indentation for the first button in a toolbar control.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETINDENT, (WPARAM)iIndent, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::SetMaxTextRows(int iMaxRows) const\r
+ // Sets the maximum number of text rows displayed on a toolbar button.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETMAXTEXTROWS, (WPARAM)iMaxRows, 0L);\r
+ }\r
+\r
+ inline BOOL CToolBar::SetPadding(int cx, int cy) const\r
+ // Sets the padding for a toolbar control.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ return (BOOL)SendMessage(TB_SETPADDING, 0L, (WPARAM)MAKELONG(cx, cy));\r
+ }\r
+\r
+ inline void CToolBar::SetToolBarTheme(ToolBarTheme& Theme)\r
+ {\r
+ m_Theme.UseThemes = Theme.UseThemes;\r
+ m_Theme.clrHot1 = Theme.clrHot1;\r
+ m_Theme.clrHot2 = Theme.clrHot2;\r
+ m_Theme.clrPressed1 = Theme.clrPressed1;\r
+ m_Theme.clrPressed2 = Theme.clrPressed2;\r
+ m_Theme.clrOutline = Theme.clrOutline;\r
+\r
+ if (IsWindow())\r
+ Invalidate();\r
+ }\r
+\r
+ inline void CToolBar::SetToolTips(CToolTip* pToolTip) const\r
+ // Associates a ToolTip control with a toolbar.\r
+ {\r
+ assert(::IsWindow(m_hWnd));\r
+ HWND hToolTip = pToolTip? pToolTip->GetHwnd() : (HWND)0;\r
+ SendMessage(TB_SETTOOLTIPS, (WPARAM)hToolTip, 0L);\r
+ }\r
+\r
+ inline LRESULT CToolBar::WndProcDefault(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
+ {\r
+ switch (uMsg)\r
+ {\r
+ case WM_DESTROY:\r
+ OnDestroy();\r
+ break;\r
+ case UWM_GETTOOLBARTHEME:\r
+ {\r
+ ToolBarTheme& tt = GetToolBarTheme();\r
+ return (LRESULT)&tt;\r
+ }\r
+ case WM_WINDOWPOSCHANGING:\r
+ OnWindowPosChanging(wParam, lParam);\r
+ break;\r
+ }\r
+\r
+ // pass unhandled messages on for default processing\r
+ return CWnd::WndProcDefault(uMsg, wParam, lParam);\r
+ }\r
+\r
+} // namespace Win32xx\r
+\r
+#endif // #ifndef _WIN32XX_TOOLBAR_H_\r