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.

442 lines
12 KiB

  1. // RatCtl.cpp : Implementation of the CRatCtrl OLE control class.
  2. #include "stdafx.h"
  3. #include <iadmw.h>
  4. #include "cnfgprts.h"
  5. #include "RatCtl.h"
  6. #include "RatPpg.h"
  7. #include "parserat.h"
  8. #include "RatData.h"
  9. #include "RatGenPg.h"
  10. #include "RatSrvPg.h"
  11. #include "wrapmb.h"
  12. #include <isvctrl.h>
  13. #include <winsock2.h>
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. IMPLEMENT_DYNCREATE(CRatCtrl, COleControl)
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Message map
  22. BEGIN_MESSAGE_MAP(CRatCtrl, COleControl)
  23. //{{AFX_MSG_MAP(CRatCtrl)
  24. //}}AFX_MSG_MAP
  25. ON_MESSAGE(OCM_COMMAND, OnOcmCommand)
  26. ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
  27. END_MESSAGE_MAP()
  28. /////////////////////////////////////////////////////////////////////////////
  29. // Dispatch map
  30. BEGIN_DISPATCH_MAP(CRatCtrl, COleControl)
  31. //{{AFX_DISPATCH_MAP(CRatCtrl)
  32. DISP_FUNCTION(CRatCtrl, "SetAdminTarget", SetAdminTarget, VT_EMPTY, VTS_BSTR VTS_BSTR)
  33. DISP_STOCKFUNC_DOCLICK()
  34. DISP_STOCKPROP_BORDERSTYLE()
  35. DISP_STOCKPROP_ENABLED()
  36. DISP_STOCKPROP_FONT()
  37. DISP_STOCKPROP_CAPTION()
  38. //}}AFX_DISPATCH_MAP
  39. END_DISPATCH_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // Event map
  42. BEGIN_EVENT_MAP(CRatCtrl, COleControl)
  43. //{{AFX_EVENT_MAP(CRatCtrl)
  44. EVENT_STOCK_CLICK()
  45. EVENT_STOCK_KEYUP()
  46. //}}AFX_EVENT_MAP
  47. END_EVENT_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // Property pages
  50. BEGIN_PROPPAGEIDS(CRatCtrl, 2)
  51. PROPPAGEID(CRatPropPage::guid)
  52. PROPPAGEID(CLSID_CFontPropPage)
  53. END_PROPPAGEIDS(CRatCtrl)
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Initialize class factory and guid
  56. IMPLEMENT_OLECREATE_EX(CRatCtrl, "CNFGPRTS.RatCtrl.1",
  57. 0xba634607, 0xb771, 0x11d0, 0x92, 0x96, 0, 0xc0, 0x4f, 0xb6, 0x67, 0x8b)
  58. /////////////////////////////////////////////////////////////////////////////
  59. // Type library ID and version
  60. IMPLEMENT_OLETYPELIB(CRatCtrl, _tlid, _wVerMajor, _wVerMinor)
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Interface IDs
  63. const IID BASED_CODE IID_DRat =
  64. { 0xba634605, 0xb771, 0x11d0, { 0x92, 0x96, 0, 0xc0, 0x4f, 0xb6, 0x67, 0x8b } };
  65. const IID BASED_CODE IID_DRatEvents =
  66. { 0xba634606, 0xb771, 0x11d0, { 0x92, 0x96, 0, 0xc0, 0x4f, 0xb6, 0x67, 0x8b } };
  67. /////////////////////////////////////////////////////////////////////////////
  68. // Control type information
  69. static const DWORD BASED_CODE _dwRatOleMisc =
  70. OLEMISC_ACTIVATEWHENVISIBLE |
  71. OLEMISC_SETCLIENTSITEFIRST |
  72. OLEMISC_INSIDEOUT |
  73. OLEMISC_CANTLINKINSIDE |
  74. OLEMISC_ACTSLIKEBUTTON |
  75. OLEMISC_RECOMPOSEONRESIZE;
  76. IMPLEMENT_OLECTLTYPE(CRatCtrl, IDS_RAT, _dwRatOleMisc)
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CRatCtrl::CRatCtrlFactory::UpdateRegistry -
  79. // Adds or removes system registry entries for CRatCtrl
  80. BOOL CRatCtrl::CRatCtrlFactory::UpdateRegistry(BOOL bRegister)
  81. {
  82. // TODO: Verify that your control follows apartment-model threading rules.
  83. // Refer to MFC TechNote 64 for more information.
  84. // If your control does not conform to the apartment-model rules, then
  85. // you must modify the code below, changing the 6th parameter from
  86. // afxRegApartmentThreading to 0.
  87. if (bRegister)
  88. return AfxOleRegisterControlClass(
  89. AfxGetInstanceHandle(),
  90. m_clsid,
  91. m_lpszProgID,
  92. IDS_RAT,
  93. IDB_RAT,
  94. afxRegApartmentThreading,
  95. _dwRatOleMisc,
  96. _tlid,
  97. _wVerMajor,
  98. _wVerMinor);
  99. else
  100. return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  101. }
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CRatCtrl::CRatCtrl - Constructor
  104. CRatCtrl::CRatCtrl():
  105. m_fUpdateFont( FALSE ),
  106. m_hAccel( NULL ),
  107. m_cAccel( 0 )
  108. {
  109. InitializeIIDs(&IID_DRat, &IID_DRatEvents);
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CRatCtrl::~CRatCtrl - Destructor
  113. CRatCtrl::~CRatCtrl()
  114. {
  115. if ( m_hAccel )
  116. DestroyAcceleratorTable( m_hAccel );
  117. m_hAccel = NULL;
  118. }
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CRatCtrl::OnDraw - Drawing function
  121. void CRatCtrl::OnDraw(
  122. CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  123. {
  124. DoSuperclassPaint(pdc, rcBounds);
  125. }
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CRatCtrl::DoPropExchange - Persistence support
  128. void CRatCtrl::DoPropExchange(CPropExchange* pPX)
  129. {
  130. ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  131. COleControl::DoPropExchange(pPX);
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CRatCtrl::OnResetState - Reset control to default state
  135. void CRatCtrl::OnResetState()
  136. {
  137. COleControl::OnResetState(); // Resets defaults found in DoPropExchange
  138. }
  139. /////////////////////////////////////////////////////////////////////////////
  140. // CRatCtrl::PreCreateWindow - Modify parameters for CreateWindowEx
  141. BOOL CRatCtrl::PreCreateWindow(CREATESTRUCT& cs)
  142. {
  143. if ( cs.style & WS_CLIPSIBLINGS )
  144. cs.style ^= WS_CLIPSIBLINGS;
  145. cs.lpszClass = _T("BUTTON");
  146. return COleControl::PreCreateWindow(cs);
  147. }
  148. /////////////////////////////////////////////////////////////////////////////
  149. // CRatCtrl::IsSubclassedControl - This is a subclassed control
  150. BOOL CRatCtrl::IsSubclassedControl()
  151. {
  152. return TRUE;
  153. }
  154. /////////////////////////////////////////////////////////////////////////////
  155. // CRatCtrl::OnOcmCommand - Handle command messages
  156. LRESULT CRatCtrl::OnOcmCommand(WPARAM wParam, LPARAM lParam)
  157. {
  158. #ifdef _WIN32
  159. WORD wNotifyCode = HIWORD(wParam);
  160. #else
  161. WORD wNotifyCode = HIWORD(lParam);
  162. #endif
  163. return 0;
  164. }
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CRatCtrl message handlers
  167. //---------------------------------------------------------------------------
  168. void CRatCtrl::OnClick(USHORT iButton)
  169. {
  170. WSADATA wsaData;
  171. // in case there are any errors, prepare the error string
  172. CString sz;
  173. // set the name of the application correctly
  174. sz.LoadString( IDS_RAT_ERR_TITLE );
  175. // free the existing name, and copy in the new one
  176. free((void*)AfxGetApp()->m_pszAppName);
  177. AfxGetApp()->m_pszAppName = _tcsdup(sz);
  178. // start up WSA services
  179. if ( WSAStartup(MAKEWORD(1, 1), &wsaData) )
  180. return;
  181. //DebugBreak();
  182. CWaitCursor wait;
  183. // initialize the metabase wrappings - pass in the name of the target machine
  184. // if one has been specified
  185. IMSAdminBase* pMB;
  186. if ( !FInitMetabaseWrapperEx( (LPTSTR)(LPCTSTR)m_szMachine, &pMB ) )
  187. {
  188. MessageBeep(0);
  189. WSACleanup();
  190. return;
  191. }
  192. // if there is no set metabase path - give it a test path
  193. if ( m_szMetaObject.IsEmpty() )
  194. m_szMetaObject = _T("/lm/w3svc/1/Root");
  195. // we have to be able to initialize the ratings data object
  196. CRatingsData dataRatings(pMB);
  197. if ( !dataRatings.FInit(m_szMachine, m_szMetaObject) )
  198. {
  199. AfxMessageBox( IDS_RAT_READFILE_ERROR );
  200. FCloseMetabaseWrapperEx(&pMB);
  201. WSACleanup();
  202. return;
  203. }
  204. // pointers to the pages (construction may throw, so we need to be careful)
  205. CRatServicePage pageService;
  206. CRatGenPage pageSetRatings;
  207. // declare the property sheet
  208. CPropertySheet propsheet( IDS_RAT_SHEETTITLE );
  209. // prepare the pages
  210. pageService.m_pRatData = &dataRatings;
  211. pageSetRatings.m_pRatData = &dataRatings;
  212. // add the pages to the sheet
  213. propsheet.AddPage( &pageService );
  214. propsheet.AddPage( &pageSetRatings );
  215. // turn on help
  216. propsheet.m_psh.dwFlags |= PSH_HASHELP;
  217. pageService.m_psp.dwFlags |= PSP_HASHELP;
  218. pageSetRatings.m_psp.dwFlags |= PSP_HASHELP;
  219. // Things could (potentially maybe) throw here, so better protect it.
  220. try
  221. {
  222. // run the propdsheet dialog
  223. // let the host container know that we are putting up a modal dialog
  224. PreModalDialog();
  225. // run the dialog
  226. if ( propsheet.DoModal() == IDOK )
  227. {
  228. // generate the label and save it into the metabase
  229. // dataRatings.SaveTheLable();
  230. }
  231. // let the host container know we are done with the modality
  232. PostModalDialog();
  233. }
  234. catch ( CException e )
  235. {
  236. }
  237. // close the metabase wrappings
  238. FCloseMetabaseWrapperEx(&pMB);
  239. WSACleanup();
  240. // don't fire anything off
  241. COleControl::OnClick(iButton);
  242. }
  243. //---------------------------------------------------------------------------
  244. void CRatCtrl::OnFontChanged()
  245. {
  246. m_fUpdateFont = TRUE;
  247. COleControl::OnFontChanged();
  248. }
  249. //---------------------------------------------------------------------------
  250. void CRatCtrl::SetAdminTarget(LPCTSTR szMachineName, LPCTSTR szMetaTarget)
  251. {
  252. m_szMachine = szMachineName;
  253. m_szMetaObject = szMetaTarget;
  254. }
  255. //---------------------------------------------------------------------------
  256. // an important method where we tell the container how to deal with us.
  257. // pControlInfo is passed in by the container, although we are responsible
  258. // for maintining the hAccel structure
  259. void CRatCtrl::OnGetControlInfo(LPCONTROLINFO pControlInfo)
  260. {
  261. // do a rudimentary check to see if we understand pControlInfo
  262. if ( !pControlInfo || pControlInfo->cb < sizeof(CONTROLINFO) )
  263. return;
  264. // set the accelerator handle into place
  265. pControlInfo->hAccel = m_hAccel;
  266. pControlInfo->cAccel = m_cAccel;
  267. // when we have focus, we do want the enter key
  268. pControlInfo->dwFlags = CTRLINFO_EATS_RETURN;
  269. }
  270. //---------------------------------------------------------------------------
  271. // when the caption text has changed, we need to rebuild the accelerator handle
  272. void CRatCtrl::OnTextChanged()
  273. {
  274. ACCEL accel;
  275. int iAccel;
  276. // get the new text
  277. CString sz = InternalGetText();
  278. sz.MakeLower();
  279. // if the handle has already been allocated, free it
  280. if ( m_hAccel )
  281. {
  282. DestroyAcceleratorTable( m_hAccel );
  283. m_hAccel = NULL;
  284. m_cAccel = 0;
  285. }
  286. // if there is a & character, then declare the accelerator
  287. iAccel = sz.Find(_T('&'));
  288. if ( iAccel >= 0 )
  289. {
  290. // fill in the accererator record
  291. accel.fVirt = FALT;
  292. accel.key = sz.GetAt(iAccel + 1);
  293. accel.cmd = (USHORT)GetDlgCtrlID();
  294. m_hAccel = CreateAcceleratorTable( &accel, 1 );
  295. if ( m_hAccel )
  296. m_cAccel = 1;
  297. // make sure the new accelerator table gets loaded
  298. ControlInfoChanged();
  299. }
  300. // finish with the default handling.
  301. COleControl::OnTextChanged();
  302. }
  303. //---------------------------------------------------------------------------
  304. void CRatCtrl::OnMnemonic(LPMSG pMsg)
  305. {
  306. OnClick((USHORT)GetDlgCtrlID());
  307. COleControl::OnMnemonic(pMsg);
  308. }
  309. //---------------------------------------------------------------------------
  310. void CRatCtrl::OnAmbientPropertyChange(DISPID dispid)
  311. {
  312. BOOL flag;
  313. UINT style;
  314. // do the right thing depending on the dispid
  315. switch ( dispid )
  316. {
  317. case DISPID_AMBIENT_DISPLAYASDEFAULT:
  318. if ( GetAmbientProperty( DISPID_AMBIENT_DISPLAYASDEFAULT, VT_BOOL, &flag ) )
  319. {
  320. style = GetWindowLong(
  321. GetSafeHwnd(), // handle of window
  322. GWL_STYLE // offset of value to retrieve
  323. );
  324. if ( flag )
  325. style |= BS_DEFPUSHBUTTON;
  326. else
  327. style ^= BS_DEFPUSHBUTTON;
  328. SetWindowLong(
  329. GetSafeHwnd(), // handle of window
  330. GWL_STYLE, // offset of value to retrieve
  331. style
  332. );
  333. Invalidate(TRUE);
  334. }
  335. break;
  336. };
  337. COleControl::OnAmbientPropertyChange(dispid);
  338. }
  339. //---------------------------------------------------------------------------
  340. // the ole control container object specifically filters out the space
  341. // key so we do not get it as a OnMnemonic call. Thus we need to look
  342. // for it ourselves
  343. void CRatCtrl::OnKeyUpEvent(USHORT nChar, USHORT nShiftState)
  344. {
  345. if ( nChar == _T(' ') )
  346. {
  347. OnClick((USHORT)GetDlgCtrlID());
  348. }
  349. COleControl::OnKeyUpEvent(nChar, nShiftState);
  350. }