Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
15 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: toolbar.h
  8. //
  9. // Toolbars implementation
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef TOOLBAR_H
  13. #define TOOLBAR_H
  14. #include "tstring.h"
  15. #include "toolbars.h"
  16. /*
  17. * Define/include the stuff we need for WTL::CImageList. We need prototypes
  18. * for IsolationAwareImageList_Read and IsolationAwareImageList_Write here
  19. * because commctrl.h only declares them if __IStream_INTERFACE_DEFINED__
  20. * is defined. __IStream_INTERFACE_DEFINED__ is defined by objidl.h, which
  21. * we can't include before including afx.h because it ends up including
  22. * windows.h, which afx.h expects to include itself. Ugh.
  23. */
  24. HIMAGELIST WINAPI IsolationAwareImageList_Read(LPSTREAM pstm);
  25. BOOL WINAPI IsolationAwareImageList_Write(HIMAGELIST himl,LPSTREAM pstm);
  26. #define _WTL_NO_AUTOMATIC_NAMESPACE
  27. #include "atlapp.h"
  28. #include "atlctrls.h"
  29. #define BUTTON_BITMAP_SIZE 16
  30. // Command Ids for buttons.
  31. // we must start from 1, since 0 is special case by MFC (BUG:451883)
  32. #define MMC_TOOLBUTTON_ID_FIRST 0x0001
  33. // End with 0x5000 as ids from 0x5400 are used for toolbar hot tracking.
  34. // A better soln will be to disable all the toolbar tracking code (in tbtrack.*)
  35. // and use the toolbar tracking provided by the toolbars & rebars implementation.
  36. #define MMC_TOOLBUTTON_ID_LAST 0x5000
  37. // Forward declarations.
  38. class CMMCToolBar;
  39. class CAMCViewToolbars;
  40. //+-------------------------------------------------------------------
  41. //
  42. // Class: CMMCToolbarButton
  43. //
  44. // Purpose: The toolbar button data, the CAMCViewToolbars will
  45. // create this object on request to AddButton/InsertButton
  46. // call and is destroyed when DeleteButton is called
  47. // or the IToolbar is destroyed (snapin destroys its
  48. // toolbar).
  49. // It knows about its toolbar thro CToolbarNotify.
  50. //
  51. // History: 12-01-1999 AnandhaG Created
  52. //
  53. // Note: The fsState refers only to the state set by snapin
  54. // and wont be set hidden if toolbar is hidden.
  55. //
  56. //--------------------------------------------------------------------
  57. class CMMCToolbarButton
  58. {
  59. public:
  60. CMMCToolbarButton(); // Vector of CMMCToolbarButton's requires empty ctor.
  61. CMMCToolbarButton(int nCommandIDFromSnapin, int nUniqueCommandID,
  62. int indexFromSnapin, int iImage,
  63. BYTE fsState, BYTE fsStyle, CToolbarNotify* pToolbarNotify)
  64. : m_nCommandIDFromSnapin(nCommandIDFromSnapin),
  65. m_nUniqueCommandID(nUniqueCommandID),
  66. m_indexFromSnapin(indexFromSnapin),
  67. m_iImage(iImage),
  68. m_fsState(fsState),
  69. m_fsStyle(fsStyle),
  70. m_fAddedToUI(false),
  71. m_pToolbarNotify(pToolbarNotify)
  72. {
  73. }
  74. // Data accessors.
  75. LPCTSTR GetTooltip() {return m_strTooltip.data();}
  76. int GetCommandIDFromSnapin() const {return m_nCommandIDFromSnapin;}
  77. int GetUniqueCommandID() const {return m_nUniqueCommandID;}
  78. int GetIndexFromSnapin() const {return m_indexFromSnapin;}
  79. int GetBitmap() const {return m_iImage;}
  80. BYTE GetStyle() const {return m_fsStyle;}
  81. BYTE GetState() const {return m_fsState;}
  82. CToolbarNotify* GetToolbarNotify() const {return m_pToolbarNotify;}
  83. void SetTooltip(LPCTSTR lpszTiptext)
  84. {
  85. m_strTooltip = lpszTiptext;
  86. }
  87. void SetButtonText(LPCTSTR lpszBtntext)
  88. {
  89. m_strBtnText = lpszBtntext;
  90. }
  91. void SetState(BYTE fsState) {m_fsState = fsState;}
  92. // Keep track if this button is added to the toolbar UI or not.
  93. void SetButtonIsAddedToUI (bool b = true) { m_fAddedToUI = b; }
  94. bool IsButtonIsAddedToUI () const { return m_fAddedToUI;}
  95. private:
  96. int m_nCommandIDFromSnapin;
  97. int m_nUniqueCommandID;
  98. int m_iImage;
  99. int m_indexFromSnapin;
  100. int m_indexUnique;
  101. BYTE m_fsState;
  102. BYTE m_fsStyle;
  103. bool m_fAddedToUI : 1;
  104. CToolbarNotify* m_pToolbarNotify;
  105. tstring m_strTooltip;
  106. tstring m_strBtnText;
  107. };
  108. //+-------------------------------------------------------------------
  109. //
  110. // Class: CAMCViewToolbars
  111. //
  112. // Synopsis: This object maintains data for the toolbars of a CAMCView.
  113. // When its view is active it adds the toolbar buttons to the
  114. // main toolbar UI and handles any of the UI messages.
  115. //
  116. // Desc: This object is created and destroyed by the view. It
  117. // provides following services.
  118. // 1. ability to create/destroy toolbars for this view.
  119. // 2. to manipulate single toolbar. It maintains an array of
  120. // toolbuttons from all snapins including std toolbar.
  121. // 3. to observe the view for activation & de-activation.
  122. // When the view becomes active it adds the buttons & handles
  123. // any button click & tooltip notifications.
  124. // 4. It maintains a single imagelist for all the toolbars for
  125. // this object. To get image index for a tool button it maintains
  126. // a map of CToolbarNotify* (the snapin toolbar) and imagelist
  127. // information like start index & number of images for this CToolbarNotify*
  128. // in that imagelist.
  129. //
  130. // It also provides unique command id for each button (as there is only
  131. // one toolbar UI which needs unique command id for each button from
  132. // different snapin).
  133. //
  134. // History: 12-01-1999 AnandhaG Created
  135. //
  136. //--------------------------------------------------------------------
  137. class CAMCViewToolbars : public CAMCViewToolbarsMgr,
  138. public CMMCToolbarIntf,
  139. public CAMCViewObserver,
  140. public CEventSource<CAMCViewToolbarsObserver>
  141. {
  142. public:
  143. CAMCViewToolbars(CMMCToolBar *pMainToolbar, CAMCView* pAMCViewOwner)
  144. : m_fViewActive(false), m_pMainToolbar(pMainToolbar), m_pAMCViewOwner(pAMCViewOwner), m_bLastActiveView(false)
  145. {
  146. ASSERT(NULL != pMainToolbar);
  147. ASSERT(NULL != pAMCViewOwner);
  148. }
  149. virtual ~CAMCViewToolbars();
  150. public:
  151. // Creation & manipulation of toolbar/toolbars.
  152. virtual SC ScCreateToolBar(CMMCToolbarIntf** ppToolbarIntf);
  153. virtual SC ScDisableToolbars();
  154. // Manipulate given toolbar.
  155. virtual SC ScAddButtons(CToolbarNotify* pNotifyCallbk, int nButtons, LPMMCBUTTON lpButtons);
  156. virtual SC ScAddBitmap (CToolbarNotify* pNotifyCallbk, INT nImages, HBITMAP hbmp, COLORREF crMask);
  157. virtual SC ScInsertButton(CToolbarNotify* pNotifyCallbk, int nIndex, LPMMCBUTTON lpButton);
  158. virtual SC ScDeleteButton(CToolbarNotify* pNotifyCallbk, int nIndex);
  159. virtual SC ScGetButtonState(CToolbarNotify* pNotifyCallbk, int idCommand, BYTE nState, BOOL* pbState);
  160. virtual SC ScSetButtonState(CToolbarNotify* pNotifyCallbk, int idCommand, BYTE nState, BOOL bState);
  161. virtual SC ScAttach(CToolbarNotify* pNotifyCallbk);
  162. virtual SC ScDetach(CToolbarNotify* pNotifyCallbk);
  163. virtual SC ScDelete(CToolbarNotify* pNotifyCallbk);
  164. virtual SC ScShow(CToolbarNotify* pNotifyCallbk, BOOL bShow);
  165. // Observer on view (for activation & de-activation).
  166. virtual SC ScOnActivateView (CAMCView *pAMCView, bool bFirstActiveView);
  167. virtual SC ScOnDeactivateView (CAMCView *pAMCView, bool bLastActiveView);
  168. // Methods used by toolbar UI (to inform button click & to get tooltip).
  169. SC ScButtonClickedNotify(UINT nID);
  170. SC ScGetToolTip(int nCommandID, CString& strTipText);
  171. // Method used by CAMCView to Init.
  172. SC ScInit();
  173. private:
  174. static int GetUniqueCommandID()
  175. {
  176. // Cycle thro, this is not a good design as there may
  177. // be buttons with dup command ids. Alternative is
  178. // to use a set to keep track of available command ids.
  179. if (MMC_TOOLBUTTON_ID_LAST == s_idCommand)
  180. s_idCommand = MMC_TOOLBUTTON_ID_FIRST;
  181. return (s_idCommand++);
  182. }
  183. CMMCToolBar* GetMainToolbar() {return m_pMainToolbar;}
  184. // Helpers
  185. SC ScInsertButtonToToolbar (CMMCToolbarButton* pToolButton);
  186. SC ScInsertButtonToDataStr (CToolbarNotify* pNotifyCallbk, int nIndex,
  187. LPMMCBUTTON lpButton, CMMCToolbarButton **ppToolButton);
  188. SC ScDeleteButtonFromToolbar(CMMCToolbarButton* pToolButton);
  189. SC ScSetButtonStateInToolbar(CMMCToolbarButton* pToolButton, BYTE nState, BOOL bState);
  190. SC ScGetButtonStateInToolbar(CMMCToolbarButton *pToolButton, BYTE nState, BOOL* pbState);
  191. SC ScValidateButton(int nButtons, LPMMCBUTTON lpButtons);
  192. SC ScSetButtonHelper(int nIndex, CMMCToolbarButton* pToolButton);
  193. // Members to search our data structures.
  194. CMMCToolbarButton* GetToolbarButton(int nUniqueCommandID);
  195. CMMCToolbarButton* GetToolbarButton(CToolbarNotify* pNotifyCallbk, int idCommandIDFromSnapin);
  196. CImageList* GetImageList() {return CImageList::FromHandle(m_ImageList);}
  197. int GetImageCount() {return m_ImageList.GetImageCount();}
  198. bool IsToolbarAttached(CToolbarNotify* pNotifyCallbk)
  199. {
  200. return (m_setOfAttachedToolbars.end() != m_setOfAttachedToolbars.find(pNotifyCallbk) );
  201. }
  202. void SetToolbarAttached(CToolbarNotify* pNotifyCallbk, bool bAttach)
  203. {
  204. if (bAttach)
  205. m_setOfAttachedToolbars.insert(pNotifyCallbk);
  206. else
  207. m_setOfAttachedToolbars.erase(pNotifyCallbk);
  208. }
  209. // The toolbar can be hidden using the customize view dialog.
  210. // This actually hides the toolbuttons in the toolbar. But the
  211. // toolbutton is unaware of this hidden information.
  212. // In other words if the toolbar is hidden then its buttons are
  213. // hidden but the fsState in CMMCToolbarButton is not set hidden.
  214. bool IsToolbarHidden(CToolbarNotify* pNotifyCallbk)
  215. {
  216. return (m_setOfHiddenToolbars.end() != m_setOfHiddenToolbars.find(pNotifyCallbk) );
  217. }
  218. void SetToolbarStatusHidden(CToolbarNotify* pNotifyCallbk, bool bHide)
  219. {
  220. if (bHide)
  221. m_setOfHiddenToolbars.insert(pNotifyCallbk);
  222. else
  223. m_setOfHiddenToolbars.erase(pNotifyCallbk);
  224. }
  225. bool IsThereAVisibleButton();
  226. private:
  227. /*
  228. * There is only one imagelist for this object. All the snapin toolbars
  229. * and stdbar will add their bitmaps to this single imagelist.
  230. * So when we add bitmaps for a toolbar we need to know where it starts
  231. * in the imagelist and how many are added.
  232. * So we maintain a data struct between toolbar (CToolbarNotify*) and an
  233. * object (MMCToolbarImages) containing start index & number of images.
  234. *
  235. * A snapin may add bitmaps multiple times for single toolbar. Each bitmap
  236. * is added at different start index.
  237. * So the data struct is a multi-map between toolbar (CToolbarNotify*)
  238. * and MMCToolbarImages.
  239. *
  240. * Assume a snapin adds 3 bitmaps initialy & then 4. Then while adding
  241. * buttons it will specify bitmap index as 5.
  242. *
  243. * The first MMCToolbarImages has cCount = 3, iStartWRTSnapin = 0, thus
  244. * images from 0 (iStartWRTSnapin) to 3 (iStartWRTSnapin + cCount) with respect
  245. * to snapin.
  246. * The second MMCToolbarImages has cCount = 4, iStartWRTSnapin = 3, thus
  247. * images from 3(iStartWRTSnapin) to 7(iStartWRTSnapin + cCount) wrt snapin.
  248. * So MMCToolbarImages has iStartWRTSnapin member in addition.
  249. *
  250. */
  251. typedef struct MMCToolbarImages
  252. {
  253. int iStart; // Start index.
  254. int cCount; // Number of images.
  255. int iStartWRTSnapin; // Start index w.r.t snapin
  256. };
  257. // This is a multi-map so that snapin can call AddBitmap for same toolbar more than once.
  258. typedef std::multimap<CToolbarNotify*, MMCToolbarImages> TBarToBitmapIndex;
  259. // Store toolbars on which attach is called.
  260. typedef std::set<CToolbarNotify*> AttachedToolbars;
  261. // Store toolbars that are hidden.
  262. typedef std::set<CToolbarNotify*> HiddenToolbars;
  263. // All toolbuttons for this view.
  264. typedef std::vector<CMMCToolbarButton> ToolbarButtons;
  265. private:
  266. static int s_idToolbar;
  267. static int s_idCommand;
  268. ToolbarButtons m_vToolbarButtons;
  269. TBarToBitmapIndex m_mapTBarToBitmapIndex;
  270. AttachedToolbars m_setOfAttachedToolbars;
  271. HiddenToolbars m_setOfHiddenToolbars;
  272. /*
  273. * Theming: use WTL::CImageList instead of MFC's CImageList so we can
  274. * insure a theme-correct imagelist will be created.
  275. */
  276. WTL::CImageList m_ImageList;
  277. bool m_fViewActive : 1;
  278. CMMCToolBar* m_pMainToolbar;
  279. CAMCView* m_pAMCViewOwner;
  280. bool m_bLastActiveView;
  281. };
  282. //+-------------------------------------------------------------------
  283. //
  284. // class: CMMCToolBar
  285. //
  286. // Purpose: The toolbar UI that is shown in mainframe. It observes
  287. // each CAMCViewToolbar and stores active CAMCViewToolbar
  288. // so that it can notify that object of button click &
  289. /// tooltip notifications.
  290. //
  291. // History: 10-12-1999 AnandhaG Created
  292. //
  293. //--------------------------------------------------------------------
  294. class CMMCToolBar : public CMMCToolBarCtrlEx,
  295. public CAMCViewToolbarsObserver
  296. {
  297. // Needed to lazy update (not update after adding
  298. // each button, cache all the buttons) of toolbar size.
  299. static const int s_nUpdateToolbarSizeMsg;
  300. public:
  301. CMMCToolBar() : m_pActiveAMCViewToolbars(NULL)
  302. {
  303. }
  304. // CAMCViewToolbarsObserver.
  305. virtual SC ScOnActivateAMCViewToolbars (CAMCViewToolbars *pAMCViewToolbars);
  306. virtual SC ScOnDeactivateAMCViewToolbars ();
  307. // Generated message map functions
  308. protected:
  309. afx_msg void OnButtonClicked(UINT nID);
  310. afx_msg LRESULT OnUpdateToolbarSize(WPARAM wParam, LPARAM lParam);
  311. afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);
  312. afx_msg void OnUpdateAllCmdUI (CCmdUI* pCmdUI)
  313. {
  314. // The idle update looks for this handler else it disables the
  315. // toolbuttons. This method does nothing. The buttons are already
  316. // in right state so dont do anything.
  317. }
  318. DECLARE_MESSAGE_MAP()
  319. public:
  320. // Helpers.
  321. void UpdateSeparators (int idCommand, BOOL fHiding);
  322. void UpdateToolbarSize(bool bAsync);
  323. SC ScInit(CRebarDockWindow* pRebar);
  324. SC ScHideButton(int idCommand, BOOL fHiding);
  325. // Attributes
  326. private:
  327. CAMCViewToolbars* m_pActiveAMCViewToolbars;
  328. };
  329. #endif /* TOOLBAR_H */