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.

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