1 ////////////////////////////////////////////////////////////////
2 // Copyright 1998 Paul DiLascia
3 // If this code works, it was written by Paul DiLascia.
4 // If not, I don't know who wrote it.
9 #ifndef TB_SETEXTENDEDSTYLE
10 #error FlatBar.h requires a newer version of the SDK than you have!
11 #error Please update your SDK files.
15 #define countof(x) (sizeof(x)/sizeof(x[0]))
18 #define _tsizeof(s) sizeof(s)/sizeof(TCHAR)
24 struct DROPDOWNBUTTON;
26 #define CFlatToolBarBase CFixMFCToolBar
29 // CFlatToolbar is a drop-in replacement for CToolBar that supports
30 // flat-style buttons and gripper handles. Use instead of CToolBar in your
31 // CMainFrame. CFlatTooBar's don'tK actually have to have the flat style,
32 // but they do by default. If you don't want the flat look, you can call
33 // ModifyStyle(TBSTYLE_FLAT, 0);
35 // CFlatToolBar overcomes various MFC drawing bugs that cause it not to work
36 // with flat-style buttons. CFlatToolBar Can also used inside a coolbar
39 // CFlatToolBar has other good stuff too, like an easy way to handle drop-down
40 // buttons--See MBTest for example how to use.
42 class CTRL_EXT_CLASS CFlatToolBar : public CFlatToolBarBase {
45 virtual ~CFlatToolBar();
47 static BOOL bTRACE; // to see TRACE diagnostics
49 // set these before creation:
50 BOOL m_bDrawDisabledButtonsInColor; // draw disabled buttons in color
51 BOOL m_bInCoolBar; // set if flatbar is inside coolbar
53 // You must call one of these to get the flat look; if not, you must
54 // set TBSTYLE_FLAT yourself.
55 BOOL LoadToolBar(LPCTSTR lpszResourceName);
56 BOOL LoadToolBar(UINT nIDResource)
57 { return LoadToolBar(MAKEINTRESOURCE(nIDResource)); }
59 // call to add drop-down buttons
60 BOOL AddDropDownButton(UINT nIDButton, UINT nIDMenu, BOOL bArrow);
62 // Use these to get/set the flat style. By default, LoadToolBar calls
63 // SetFlatStyle(TRUE); if you create some other way, you must call it
65 BOOL SetFlatStyle(BOOL bFlat) {
66 return ModifyStyle(bFlat ? 0 : TBSTYLE_FLAT, bFlat ? TBSTYLE_FLAT : 0);
69 return (GetStyle() & TBSTYLE_FLAT)!=0;
72 // silly function to fake out compiler with const-ness
73 LRESULT SendMessageC(UINT m, WPARAM wp=0, LPARAM lp=0) const
74 { return ((CFixMFCToolBar*)this)->SendMessage(m, wp, lp); }
76 // Wrappers that are not in MFC but should be;
77 // I copied these from CToolBarCtrl
78 BOOL EnableButton(int nID, BOOL bEnable)
79 { return SendMessage(TB_ENABLEBUTTON, nID, MAKELPARAM(bEnable, 0)); }
80 BOOL CheckButton(int nID, BOOL bCheck)
81 { return SendMessage(TB_CHECKBUTTON, nID, MAKELPARAM(bCheck, 0)); }
82 BOOL PressButton(int nID, BOOL bPress)
83 { return SendMessage(TB_PRESSBUTTON, nID, MAKELPARAM(bPress, 0)); }
84 BOOL HideButton(int nID, BOOL bHide)
85 { return SendMessage(TB_HIDEBUTTON, nID, MAKELPARAM(bHide, 0)); }
86 BOOL Indeterminate(int nID, BOOL bIndeterminate)
87 { return SendMessage(TB_INDETERMINATE, nID, MAKELPARAM(bIndeterminate, 0)); }
88 BOOL IsButtonEnabled(int nID) const
89 { return SendMessageC(TB_ISBUTTONENABLED, nID); }
90 BOOL IsButtonChecked(int nID) const
91 { return SendMessageC(TB_ISBUTTONCHECKED, nID); }
92 BOOL IsButtonPressed(int nID) const
93 { return SendMessageC(TB_ISBUTTONPRESSED, nID); }
94 BOOL IsButtonHidden(int nID) const
95 { return SendMessageC(TB_ISBUTTONHIDDEN, nID); }
96 BOOL IsButtonIndeterminate(int nID) const
97 { return SendMessageC(TB_ISBUTTONINDETERMINATE, nID); }
98 BOOL SetState(int nID, UINT nState)
99 { return SendMessage(TB_SETSTATE, nID, MAKELPARAM(nState, 0)); }
100 int GetState(int nID) const
101 { return SendMessageC(TB_GETSTATE, nID); }
102 BOOL AddButtons(int nNumButtons, LPTBBUTTON lpButtons)
103 { return SendMessage(TB_ADDBUTTONS, nNumButtons, (LPARAM)lpButtons); }
104 BOOL InsertButton(int nIndex, LPTBBUTTON lpButton)
105 { return SendMessage(TB_INSERTBUTTON, nIndex, (LPARAM)lpButton); }
106 BOOL DeleteButton(int nIndex)
107 { return SendMessage(TB_DELETEBUTTON, nIndex); }
108 int GetButtonCount() const
109 { return SendMessageC(TB_BUTTONCOUNT); }
110 UINT CommandToIndex(UINT nID) const
111 { return SendMessageC(TB_COMMANDTOINDEX, nID); }
113 { SendMessage(TB_CUSTOMIZE, 0, 0L); }
114 int AddStrings(LPCTSTR lpszStrings)
115 { return SendMessage(TB_ADDSTRING, 0, (LPARAM)lpszStrings); }
116 void SetButtonStructSize(int nSize)
117 { SendMessage(TB_BUTTONSTRUCTSIZE, nSize); }
118 BOOL SetButtonSize(CSize sz)
119 { return SendMessage(TB_SETBUTTONSIZE, 0, MAKELPARAM(sz.cx, sz.cy)); }
120 BOOL SetBitmapSize(CSize sz)
121 { return SendMessage(TB_SETBITMAPSIZE, 0, MAKELPARAM(sz.cx, sz.cy)); }
123 { SendMessage(TB_AUTOSIZE); }
124 CToolTipCtrl* GetToolTips() const
125 { return (CToolTipCtrl*)CWnd::FromHandle((HWND)SendMessageC(TB_GETTOOLTIPS)); }
126 void SetToolTips(CToolTipCtrl* pTip)
127 { SendMessage(TB_SETTOOLTIPS, (WPARAM)pTip->m_hWnd); }
128 // NO!!!--this is not the same as the MFC owner
129 // void SetOwner(CWnd* pWnd)
130 // { SendMessage(TB_SETPARENT, (WPARAM)pWnd->m_hWnd); }
131 void SetRows(int nRows, BOOL bLarger, LPRECT lpRect)
132 { SendMessage(TB_SETROWS, MAKELPARAM(nRows, bLarger), (LPARAM)lpRect); }
134 { return (int) SendMessageC(TB_GETROWS); }
135 BOOL SetCmdID(int nIndex, UINT nID)
136 { return SendMessage(TB_SETCMDID, nIndex, nID); }
137 UINT GetBitmapFlags() const
138 { return (UINT) SendMessageC(TB_GETBITMAPFLAGS); }
140 // Wrappers for some of the newer messages--not complete
141 BOOL SetIndent(int indent)
142 { return SendMessage(TB_SETINDENT, indent); }
143 HIMAGELIST GetImageList() const
144 { return (HIMAGELIST)SendMessageC(TB_GETIMAGELIST); }
145 HIMAGELIST SetImageList(HIMAGELIST hImgList)
146 { return (HIMAGELIST)SendMessage(TB_SETIMAGELIST, 0, (LPARAM)hImgList); }
147 int GetBitmap(UINT nIdButton) const
148 { return SendMessageC(TB_GETBITMAP, nIdButton); }
149 DWORD SetExtendedStyle(DWORD dwStyle)
150 { return SendMessage(TB_SETEXTENDEDSTYLE, 0, dwStyle); }
151 BOOL GetRect(UINT nIdButton, RECT& rc) const
152 { return SendMessageC(TB_GETRECT, nIdButton, (LPARAM)&rc); }
153 DWORD GetToolbarStyle() const
154 { return SendMessageC(TB_GETSTYLE); }
155 void SetToolbarStyle(DWORD dwStyle)
156 { SendMessage(TB_SETSTYLE, 0, dwStyle); }
157 int HitTest(CPoint p) const
158 { return SendMessageC(TB_HITTEST, 0, (LPARAM)&p); }
159 int GetHotItem() const
160 { if (GetSafeHwnd()) return SendMessageC(TB_GETHOTITEM); return 0; }
161 void SetHotItem(int iHot)
162 { if (GetSafeHwnd()) SendMessage(TB_SETHOTITEM, iHot); }
163 BOOL MapAccelerator(TCHAR ch, UINT& nID) const
164 { return SendMessageC(TB_MAPACCELERATOR, (WPARAM)ch, (LPARAM)&nID); }
165 CSize GetPadding() const
166 { return SendMessageC(TB_GETPADDING); }
167 CSize SetPadding(CSize sz)
168 { return SendMessage(TB_SETPADDING, 0, MAKELPARAM(sz.cx,sz.cy)); }
171 CRect m_rcOldPos; // used when toolbar is moved
172 DROPDOWNBUTTON* m_pDropDownButtons; // list of dropdown button/menu pairs
173 BOOL m_bNoEntry; // implementation hack
175 // override to do your own weird drop-down buttons
176 virtual void OnDropDownButton(const NMTOOLBAR& nmtb, UINT nID, CRect rc);
177 DROPDOWNBUTTON* FindDropDownButton(UINT nID);
180 virtual void InvalidateOldPos(const CRect& rcInvalid);
182 afx_msg int OnCreate(LPCREATESTRUCT lpcs);
183 afx_msg void OnTbnDropDown(NMHDR* pNMHDR, LRESULT* pRes);
184 afx_msg void OnWindowPosChanging(LPWINDOWPOS lpWndPos);
185 afx_msg void OnWindowPosChanged(LPWINDOWPOS lpWndPos);
186 afx_msg void OnNcCalcSize(BOOL bCalc, NCCALCSIZE_PARAMS* pncp );
187 afx_msg BOOL OnEraseBkgnd(CDC* pDC);
188 afx_msg BOOL OnNcCreate(LPCREATESTRUCT lpcs);
189 afx_msg void OnPaint();
191 DECLARE_MESSAGE_MAP()
192 DECLARE_DYNAMIC(CFlatToolBar)