Leaked source code of windows server 2003
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.

388 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()
  144. : m_fViewActive(false), m_pMainToolbar(NULL), m_pAMCViewOwner(NULL), m_bLastActiveView(false)
  145. {
  146. }
  147. virtual ~CAMCViewToolbars();
  148. // implemented by CRefCountedObject
  149. virtual LONG AddRef() = 0;
  150. virtual LONG Release() = 0;
  151. public:
  152. // Creation & manipulation of toolbar/toolbars.
  153. virtual SC ScCreateToolBar(CMMCToolbarIntf** ppToolbarIntf);
  154. virtual SC ScDisableToolbars();
  155. // Manipulate given toolbar.
  156. virtual SC ScAddButtons(CToolbarNotify* pNotifyCallbk, int nButtons, LPMMCBUTTON lpButtons);
  157. virtual SC ScAddBitmap (CToolbarNotify* pNotifyCallbk, INT nImages, HBITMAP hbmp, COLORREF crMask);
  158. virtual SC ScInsertButton(CToolbarNotify* pNotifyCallbk, int nIndex, LPMMCBUTTON lpButton);
  159. virtual SC ScDeleteButton(CToolbarNotify* pNotifyCallbk, int nIndex);
  160. virtual SC ScGetButtonState(CToolbarNotify* pNotifyCallbk, int idCommand, BYTE nState, BOOL* pbState);
  161. virtual SC ScSetButtonState(CToolbarNotify* pNotifyCallbk, int idCommand, BYTE nState, BOOL bState);
  162. virtual SC ScAttach(CToolbarNotify* pNotifyCallbk);
  163. virtual SC ScDetach(CToolbarNotify* pNotifyCallbk);
  164. virtual SC ScDelete(CToolbarNotify* pNotifyCallbk);
  165. virtual SC ScShow(CToolbarNotify* pNotifyCallbk, BOOL bShow);
  166. // Observer on view (for activation & de-activation).
  167. virtual SC ScOnActivateView (CAMCView *pAMCView, bool bFirstActiveView);
  168. virtual SC ScOnDeactivateView (CAMCView *pAMCView, bool bLastActiveView);
  169. virtual SC ScOnViewDestroyed (CAMCView *pAMCView);
  170. // Methods used by toolbar UI (to inform button click & to get tooltip).
  171. SC ScButtonClickedNotify(UINT nID);
  172. SC ScGetToolTip(int nCommandID, CString& strTipText);
  173. // Method used by CAMCView to Init.
  174. SC ScInit(CMMCToolBar *pMainToolbar, CAMCView* pAMCViewOwner);
  175. private:
  176. static int GetUniqueCommandID()
  177. {
  178. // Cycle thro, this is not a good design as there may
  179. // be buttons with dup command ids. Alternative is
  180. // to use a set to keep track of available command ids.
  181. if (MMC_TOOLBUTTON_ID_LAST == s_idCommand)
  182. s_idCommand = MMC_TOOLBUTTON_ID_FIRST;
  183. return (s_idCommand++);
  184. }
  185. CMMCToolBar* GetMainToolbar() {return m_pMainToolbar;}
  186. // Helpers
  187. SC ScInsertButtonToToolbar (CMMCToolbarButton* pToolButton);
  188. SC ScInsertButtonToDataStr (CToolbarNotify* pNotifyCallbk, int nIndex,
  189. LPMMCBUTTON lpButton, CMMCToolbarButton **ppToolButton);
  190. SC ScDeleteButtonFromToolbar(CMMCToolbarButton* pToolButton);
  191. SC ScSetButtonStateInToolbar(CMMCToolbarButton* pToolButton, BYTE nState, BOOL bState);
  192. SC ScGetButtonStateInToolbar(CMMCToolbarButton *pToolButton, BYTE nState, BOOL* pbState);
  193. SC ScValidateButton(int nButtons, LPMMCBUTTON lpButtons);
  194. SC ScSetButtonHelper(int nIndex, CMMCToolbarButton* pToolButton);
  195. // Members to search our data structures.
  196. CMMCToolbarButton* GetToolbarButton(int nUniqueCommandID);
  197. CMMCToolbarButton* GetToolbarButton(CToolbarNotify* pNotifyCallbk, int idCommandIDFromSnapin);
  198. CImageList* GetImageList() {return CImageList::FromHandle(m_ImageList);}
  199. int GetImageCount() {return m_ImageList.GetImageCount();}
  200. bool IsToolbarAttached(CToolbarNotify* pNotifyCallbk)
  201. {
  202. return (m_setOfAttachedToolbars.end() != m_setOfAttachedToolbars.find(pNotifyCallbk) );
  203. }
  204. void SetToolbarAttached(CToolbarNotify* pNotifyCallbk, bool bAttach)
  205. {
  206. if (bAttach)
  207. m_setOfAttachedToolbars.insert(pNotifyCallbk);
  208. else
  209. m_setOfAttachedToolbars.erase(pNotifyCallbk);
  210. }
  211. // The toolbar can be hidden using the customize view dialog.
  212. // This actually hides the toolbuttons in the toolbar. But the
  213. // toolbutton is unaware of this hidden information.
  214. // In other words if the toolbar is hidden then its buttons are
  215. // hidden but the fsState in CMMCToolbarButton is not set hidden.
  216. bool IsToolbarHidden(CToolbarNotify* pNotifyCallbk)
  217. {
  218. return (m_setOfHiddenToolbars.end() != m_setOfHiddenToolbars.find(pNotifyCallbk) );
  219. }
  220. void SetToolbarStatusHidden(CToolbarNotify* pNotifyCallbk, bool bHide)
  221. {
  222. if (bHide)
  223. m_setOfHiddenToolbars.insert(pNotifyCallbk);
  224. else
  225. m_setOfHiddenToolbars.erase(pNotifyCallbk);
  226. }
  227. bool IsThereAVisibleButton();
  228. private:
  229. /*
  230. * There is only one imagelist for this object. All the snapin toolbars
  231. * and stdbar will add their bitmaps to this single imagelist.
  232. * So when we add bitmaps for a toolbar we need to know where it starts
  233. * in the imagelist and how many are added.
  234. * So we maintain a data struct between toolbar (CToolbarNotify*) and an
  235. * object (MMCToolbarImages) containing start index & number of images.
  236. *
  237. * A snapin may add bitmaps multiple times for single toolbar. Each bitmap
  238. * is added at different start index.
  239. * So the data struct is a multi-map between toolbar (CToolbarNotify*)
  240. * and MMCToolbarImages.
  241. *
  242. * Assume a snapin adds 3 bitmaps initialy & then 4. Then while adding
  243. * buttons it will specify bitmap index as 5.
  244. *
  245. * The first MMCToolbarImages has cCount = 3, iStartWRTSnapin = 0, thus
  246. * images from 0 (iStartWRTSnapin) to 3 (iStartWRTSnapin + cCount) with respect
  247. * to snapin.
  248. * The second MMCToolbarImages has cCount = 4, iStartWRTSnapin = 3, thus
  249. * images from 3(iStartWRTSnapin) to 7(iStartWRTSnapin + cCount) wrt snapin.
  250. * So MMCToolbarImages has iStartWRTSnapin member in addition.
  251. *
  252. */
  253. typedef struct MMCToolbarImages
  254. {
  255. int iStart; // Start index.
  256. int cCount; // Number of images.
  257. int iStartWRTSnapin; // Start index w.r.t snapin
  258. };
  259. // This is a multi-map so that snapin can call AddBitmap for same toolbar more than once.
  260. typedef std::multimap<CToolbarNotify*, MMCToolbarImages> TBarToBitmapIndex;
  261. // Store toolbars on which attach is called.
  262. typedef std::set<CToolbarNotify*> AttachedToolbars;
  263. // Store toolbars that are hidden.
  264. typedef std::set<CToolbarNotify*> HiddenToolbars;
  265. // All toolbuttons for this view.
  266. typedef std::vector<CMMCToolbarButton> ToolbarButtons;
  267. private:
  268. static int s_idToolbar;
  269. static int s_idCommand;
  270. ToolbarButtons m_vToolbarButtons;
  271. TBarToBitmapIndex m_mapTBarToBitmapIndex;
  272. AttachedToolbars m_setOfAttachedToolbars;
  273. HiddenToolbars m_setOfHiddenToolbars;
  274. /*
  275. * Theming: use WTL::CImageList instead of MFC's CImageList so we can
  276. * insure a theme-correct imagelist will be created.
  277. */
  278. WTL::CImageList m_ImageList;
  279. bool m_fViewActive : 1;
  280. CMMCToolBar* m_pMainToolbar;
  281. CAMCView* m_pAMCViewOwner;
  282. bool m_bLastActiveView;
  283. };
  284. //+-------------------------------------------------------------------
  285. //
  286. // class: CMMCToolBar
  287. //
  288. // Purpose: The toolbar UI that is shown in mainframe. It observes
  289. // each CAMCViewToolbar and stores active CAMCViewToolbar
  290. // so that it can notify that object of button click &
  291. /// tooltip notifications.
  292. //
  293. // History: 10-12-1999 AnandhaG Created
  294. //
  295. //--------------------------------------------------------------------
  296. class CMMCToolBar : public CMMCToolBarCtrlEx,
  297. public CAMCViewToolbarsObserver
  298. {
  299. // Needed to lazy update (not update after adding
  300. // each button, cache all the buttons) of toolbar size.
  301. static const int s_nUpdateToolbarSizeMsg;
  302. public:
  303. CMMCToolBar() : m_pActiveAMCViewToolbars(NULL)
  304. {
  305. }
  306. // CAMCViewToolbarsObserver.
  307. virtual SC ScOnActivateAMCViewToolbars (CAMCViewToolbars *pAMCViewToolbars);
  308. virtual SC ScOnDeactivateAMCViewToolbars ();
  309. // Generated message map functions
  310. protected:
  311. afx_msg void OnButtonClicked(UINT nID);
  312. afx_msg LRESULT OnUpdateToolbarSize(WPARAM wParam, LPARAM lParam);
  313. afx_msg BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult);
  314. afx_msg void OnUpdateAllCmdUI (CCmdUI* pCmdUI)
  315. {
  316. // The idle update looks for this handler else it disables the
  317. // toolbuttons. This method does nothing. The buttons are already
  318. // in right state so dont do anything.
  319. }
  320. DECLARE_MESSAGE_MAP()
  321. public:
  322. // Helpers.
  323. void UpdateSeparators (int idCommand, BOOL fHiding);
  324. void UpdateToolbarSize(bool bAsync);
  325. SC ScInit(CRebarDockWindow* pRebar);
  326. SC ScHideButton(int idCommand, BOOL fHiding);
  327. // Attributes
  328. private:
  329. CAMCViewToolbars* m_pActiveAMCViewToolbars;
  330. };
  331. #endif /* TOOLBAR_H */