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.

325 lines
9.6 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. //
  6. //
  7. //
  8. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  9. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  10. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  11. // PURPOSE.
  12. //
  13. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  14. //
  15. //
  16. //
  17. //==============================================================;
  18. #include "tbarExt.h"
  19. #include "globals.h"
  20. #include "resource.h"
  21. #include <commctrl.h> // Needed for button styles...
  22. #include <crtdbg.h>
  23. #include "resource.h"
  24. #include <stdio.h>
  25. #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
  26. // we need to do this to get around MMC.IDL - it explicitly defines
  27. // the clipboard formats as WCHAR types...
  28. #define _T_CCF_DISPLAY_NAME _T("CCF_DISPLAY_NAME")
  29. #define _T_CCF_NODETYPE _T("CCF_NODETYPE")
  30. #define _T_CCF_SNAPIN_CLASSID _T("CCF_SNAPIN_CLASSID")
  31. // These are the clipboard formats that we must supply at a minimum.
  32. // mmc.h actually defined these. We can make up our own to use for
  33. // other reasons. We don't need any others at this time.
  34. UINT CToolBarExtension::s_cfDisplayName = RegisterClipboardFormat(_T_CCF_DISPLAY_NAME);
  35. UINT CToolBarExtension::s_cfNodeType = RegisterClipboardFormat(_T_CCF_NODETYPE);
  36. UINT CToolBarExtension::s_cfSnapInCLSID = RegisterClipboardFormat(_T_CCF_SNAPIN_CLASSID);
  37. CToolBarExtension::CToolBarExtension() : m_cref(0), m_ipControlBar(NULL),
  38. m_ipToolbar(NULL)
  39. {
  40. OBJECT_CREATED
  41. }
  42. CToolBarExtension::~CToolBarExtension()
  43. {
  44. OBJECT_DESTROYED
  45. }
  46. ///////////////////////
  47. // IUnknown implementation
  48. ///////////////////////
  49. STDMETHODIMP CToolBarExtension::QueryInterface(REFIID riid, LPVOID *ppv)
  50. {
  51. if (!ppv)
  52. return E_FAIL;
  53. *ppv = NULL;
  54. if (IsEqualIID(riid, IID_IUnknown))
  55. *ppv = static_cast<IExtendControlbar *>(this);
  56. else if (IsEqualIID(riid, IID_IExtendControlbar))
  57. *ppv = static_cast<IExtendControlbar *>(this);
  58. if (*ppv)
  59. {
  60. reinterpret_cast<IUnknown *>(*ppv)->AddRef();
  61. return S_OK;
  62. }
  63. return E_NOINTERFACE;
  64. }
  65. STDMETHODIMP_(ULONG) CToolBarExtension::AddRef()
  66. {
  67. return InterlockedIncrement((LONG *)&m_cref);
  68. }
  69. STDMETHODIMP_(ULONG) CToolBarExtension::Release()
  70. {
  71. if (InterlockedDecrement((LONG *)&m_cref) == 0)
  72. {
  73. // we need to decrement our object count in the DLL
  74. delete this;
  75. return 0;
  76. }
  77. return m_cref;
  78. }
  79. HRESULT CToolBarExtension::ExtractData( IDataObject* piDataObject,
  80. CLIPFORMAT cfClipFormat,
  81. BYTE* pbData,
  82. DWORD cbData )
  83. {
  84. HRESULT hr = S_OK;
  85. FORMATETC formatetc = {cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  86. STGMEDIUM stgmedium = {TYMED_HGLOBAL, NULL};
  87. stgmedium.hGlobal = ::GlobalAlloc(GPTR, cbData);
  88. do // false loop
  89. {
  90. if (NULL == stgmedium.hGlobal)
  91. {
  92. hr = E_OUTOFMEMORY;
  93. break;
  94. }
  95. hr = piDataObject->GetDataHere( &formatetc, &stgmedium );
  96. if ( FAILED(hr) )
  97. {
  98. break;
  99. }
  100. BYTE* pbNewData = reinterpret_cast<BYTE*>(stgmedium.hGlobal);
  101. if (NULL == pbNewData)
  102. {
  103. hr = E_UNEXPECTED;
  104. break;
  105. }
  106. ::memcpy( pbData, pbNewData, cbData );
  107. } while (FALSE); // false loop
  108. if (NULL != stgmedium.hGlobal)
  109. {
  110. ::GlobalFree(stgmedium.hGlobal);
  111. }
  112. return hr;
  113. } // ExtractData()
  114. HRESULT CToolBarExtension::ExtractString( IDataObject *piDataObject,
  115. CLIPFORMAT cfClipFormat,
  116. WCHAR *pstr,
  117. DWORD cchMaxLength)
  118. {
  119. return ExtractData( piDataObject, cfClipFormat, (PBYTE)pstr, cchMaxLength );
  120. }
  121. HRESULT CToolBarExtension::ExtractSnapInCLSID( IDataObject* piDataObject, CLSID* pclsidSnapin )
  122. {
  123. return ExtractData( piDataObject, s_cfSnapInCLSID, (PBYTE)pclsidSnapin, sizeof(CLSID) );
  124. }
  125. HRESULT CToolBarExtension::ExtractObjectTypeGUID( IDataObject* piDataObject, GUID* pguidObjectType )
  126. {
  127. return ExtractData( piDataObject, s_cfNodeType, (PBYTE)pguidObjectType, sizeof(GUID) );
  128. }
  129. ///////////////////////////////
  130. // Interface IExtendControlBar
  131. ///////////////////////////////
  132. static MMCBUTTON SnapinButtons1[] =
  133. {
  134. { 0, ID_BUTTONSTART, TBSTATE_ENABLED, TBSTYLE_GROUP, L"Extension - Start Vehicle", L"Extension - Start Vehicle" },
  135. { 1, ID_BUTTONPAUSE, TBSTATE_ENABLED, TBSTYLE_GROUP, L"Extension - Pause Vehicle", L"Extension - Pause Vehicle"},
  136. { 2, ID_BUTTONSTOP, TBSTATE_ENABLED, TBSTYLE_GROUP, L"Extension - Stop Vehicle", L"Extension - Stop Vehicle" },
  137. };
  138. HRESULT CToolBarExtension::SetControlbar(
  139. /* [in] */ LPCONTROLBAR pControlbar)
  140. {
  141. HRESULT hr = S_OK;
  142. //
  143. // Clean up
  144. //
  145. // if we've got a cached toolbar, release it
  146. if (m_ipToolbar) {
  147. m_ipToolbar->Release();
  148. m_ipToolbar = NULL;
  149. }
  150. // if we've got a cached control bar, release it
  151. if (m_ipControlBar) {
  152. m_ipControlBar->Release();
  153. m_ipControlBar = NULL;
  154. }
  155. //
  156. // Install new pieces if necessary
  157. //
  158. // if a new one came in, cache and AddRef
  159. if (pControlbar) {
  160. m_ipControlBar = pControlbar;
  161. m_ipControlBar->AddRef();
  162. hr = m_ipControlBar->Create(TOOLBAR, // type of control to be created
  163. dynamic_cast<IExtendControlbar *>(this),
  164. reinterpret_cast<IUnknown **>(&m_ipToolbar));
  165. _ASSERT(SUCCEEDED(hr));
  166. // add the bitmap to the toolbar
  167. HBITMAP hbmp = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDR_TOOLBAR1));
  168. hr = m_ipToolbar->AddBitmap(3, hbmp, 16, 16, RGB(0, 128, 128)); // NOTE, hardcoded value 3
  169. _ASSERT(SUCCEEDED(hr));
  170. // Add the buttons to the toolbar
  171. hr = m_ipToolbar->AddButtons(ARRAYLEN(SnapinButtons1), SnapinButtons1);
  172. _ASSERT(SUCCEEDED(hr));
  173. }
  174. return hr;
  175. }
  176. HRESULT CToolBarExtension::ControlbarNotify(
  177. /* [in] */ MMC_NOTIFY_TYPE event,
  178. /* [in] */ LPARAM arg,
  179. /* [in] */ LPARAM param)
  180. {
  181. _TCHAR pszMsg[255];
  182. BYTE *pbVehicleStatus = NULL;
  183. HRESULT hr = S_OK;
  184. if (event == MMCN_SELECT) {
  185. BOOL bScope = (BOOL) LOWORD(arg);
  186. BOOL bSelect = (BOOL) HIWORD(arg);
  187. if (bSelect) {
  188. // Always make sure the toolbar is attached
  189. hr = m_ipControlBar->Attach(TOOLBAR, m_ipToolbar);
  190. // Set button states
  191. //fake value to set toolbar button states
  192. iStatus = RUNNING;
  193. SetToolbarButtons(iStatus);
  194. } else {
  195. // Always make sure the toolbar is detached
  196. hr = m_ipControlBar->Detach(m_ipToolbar);
  197. }
  198. } else if (event == MMCN_BTN_CLICK) {
  199. //the arg parameter contains the data object from the primary
  200. //snap-in. Use it to get the display name of the currently selected item
  201. WCHAR pszName[255];
  202. HRESULT hr = ExtractString(reinterpret_cast<IDataObject *>(arg), s_cfDisplayName, pszName, sizeof(pszName));
  203. MAKE_TSTRPTR_FROMWIDE(ptrname, pszName);
  204. switch ((int)param)
  205. {
  206. case ID_BUTTONSTART:
  207. iStatus = RUNNING;
  208. break;
  209. case ID_BUTTONPAUSE:
  210. iStatus = PAUSED;
  211. break;
  212. case ID_BUTTONSTOP:
  213. iStatus = STOPPED;
  214. break;
  215. }
  216. _stprintf(pszMsg, _T("%s selected and extension button %s pressed"), ptrname,
  217. (long)param == ID_BUTTONSTART ? _T("1") :
  218. (long)param == ID_BUTTONPAUSE ? _T("2") :
  219. (long)param == ID_BUTTONSTOP ? _T("3") : _T("!!!unknown command!!!"));
  220. ::MessageBox(NULL, pszMsg, _T("Messagebox from Toolbar Extension"), MB_OK|MB_ICONEXCLAMATION);
  221. //Reset toolbar button states
  222. SetToolbarButtons(iStatus);
  223. }
  224. return hr;
  225. }
  226. HRESULT CToolBarExtension::SetToolbarButtons(STATUS iVehicleStatus)
  227. {
  228. HRESULT hr = S_OK;
  229. switch (iVehicleStatus)
  230. {
  231. case RUNNING:
  232. m_ipToolbar->SetButtonState(ID_BUTTONSTART, BUTTONPRESSED, TRUE);
  233. m_ipToolbar->SetButtonState(ID_BUTTONSTART, ENABLED, FALSE);
  234. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, BUTTONPRESSED, FALSE);
  235. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, ENABLED, TRUE);
  236. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, BUTTONPRESSED, FALSE);
  237. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, ENABLED, TRUE);
  238. break;
  239. case PAUSED:
  240. m_ipToolbar->SetButtonState(ID_BUTTONSTART, BUTTONPRESSED, FALSE);
  241. m_ipToolbar->SetButtonState(ID_BUTTONSTART, ENABLED, TRUE);
  242. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, BUTTONPRESSED, TRUE);
  243. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, ENABLED, FALSE);
  244. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, BUTTONPRESSED, FALSE);
  245. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, ENABLED, TRUE);
  246. break;
  247. case STOPPED:
  248. m_ipToolbar->SetButtonState(ID_BUTTONSTART, BUTTONPRESSED, FALSE);
  249. m_ipToolbar->SetButtonState(ID_BUTTONSTART, ENABLED, TRUE);
  250. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, BUTTONPRESSED, FALSE);
  251. m_ipToolbar->SetButtonState(ID_BUTTONPAUSE, ENABLED, TRUE);
  252. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, BUTTONPRESSED, TRUE);
  253. m_ipToolbar->SetButtonState(ID_BUTTONSTOP, ENABLED, FALSE);
  254. break;
  255. }
  256. return hr;
  257. }