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.

810 lines
22 KiB

  1. //**********************************************************************
  2. // File name: app.cpp
  3. //
  4. // Implementation file for the CSimpSvrApp Class
  5. //
  6. // Functions:
  7. //
  8. // See app.h for a list of member functions.
  9. //
  10. // Copyright (c) 1993 Microsoft Corporation. All rights reserved.
  11. //**********************************************************************
  12. #include "pre.h"
  13. #include "obj.h"
  14. #include "app.h"
  15. #include "doc.h"
  16. #include "icf.h"
  17. #include "initguid.h"
  18. #ifdef WIN32
  19. DEFINE_GUID(GUID_SIMPLE, 0xBCF6D4A0, 0xBE8C, 0x1068, 0xB6, 0xD4, 0x00, 0xDD, 0x01, 0x0C, 0x05, 0x09);
  20. #else
  21. DEFINE_GUID(GUID_SIMPLE, 0x9fb878d0, 0x6f88, 0x101b, 0xbc, 0x65, 0x00, 0x00, 0x0b, 0x65, 0xc7, 0xa6);
  22. #endif
  23. //**********************************************************************
  24. //
  25. // CSimpSvrApp::CSimpSvrApp()
  26. //
  27. // Purpose:
  28. //
  29. // Constructor for CSimpSvrApp
  30. //
  31. // Parameters:
  32. //
  33. // None
  34. //
  35. // Return Value:
  36. //
  37. // None
  38. //
  39. // Function Calls:
  40. // Function Location
  41. //
  42. // TestDebugOut Windows API
  43. // SetRectEmpty Windows API
  44. //
  45. // Comments:
  46. //
  47. //
  48. //********************************************************************
  49. CSimpSvrApp::CSimpSvrApp()
  50. {
  51. TestDebugOut("In CSimpSvrApp's Constructor \r\n");
  52. // Set Ref Count
  53. m_nCount = 0;
  54. // clear members
  55. m_hAppWnd = NULL;
  56. m_hInst = NULL;
  57. m_lpDoc = NULL;
  58. // clear flags
  59. m_fInitialized = FALSE;
  60. // used for inplace
  61. SetRectEmpty(&nullRect);
  62. }
  63. //**********************************************************************
  64. //
  65. // CSimpSvrApp::~CSimpSvrApp()
  66. //
  67. // Purpose:
  68. //
  69. // Destructor for CSimpSvrApp Class.
  70. //
  71. // Parameters:
  72. //
  73. // None
  74. //
  75. // Return Value:
  76. //
  77. // None
  78. //
  79. // Function Calls:
  80. // Function Location
  81. //
  82. // TestDebugOut Windows API
  83. // DestroyWindow Windows API
  84. // CSimpSvrApp::IsInitialized APP.H
  85. // OleUninitialize OLE API
  86. //
  87. // Comments:
  88. //
  89. //********************************************************************
  90. CSimpSvrApp::~CSimpSvrApp()
  91. {
  92. TestDebugOut("In CSimpSvrApp's Destructor\r\n");
  93. // need to uninit the library...
  94. if (IsInitialized())
  95. OleUninitialize();
  96. DestroyWindow(m_hAppWnd);
  97. }
  98. //**********************************************************************
  99. //
  100. // CSimpSvrApp::QueryInterface
  101. //
  102. // Purpose:
  103. //
  104. // Used for interface negotiation at the Application level.
  105. //
  106. // Parameters:
  107. //
  108. // REFIID riid - A reference to the interface that is
  109. // being queried.
  110. //
  111. // LPVOID FAR* ppvObj - An out parameter to return a pointer to
  112. // the interface.
  113. //
  114. // Return Value:
  115. //
  116. // S_OK - The interface is supported.
  117. // E_NOINTERFACE - The interface is not supported
  118. //
  119. // Function Calls:
  120. // Function Location
  121. //
  122. // TestDebugOut Windows API
  123. // ResultFromScode OLE API
  124. // IUnknown::AddRef APP.CPP
  125. //
  126. // Comments:
  127. //
  128. //
  129. //********************************************************************
  130. STDMETHODIMP CSimpSvrApp::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  131. {
  132. TestDebugOut("In CSimpSvrApp::QueryInterface\r\n");
  133. SCODE sc = S_OK;
  134. if (riid == IID_IUnknown)
  135. *ppvObj = this;
  136. else
  137. {
  138. *ppvObj = NULL;
  139. sc = E_NOINTERFACE;
  140. }
  141. if (*ppvObj)
  142. ((LPUNKNOWN)*ppvObj)->AddRef();
  143. // asking for something we don't understand at this level.
  144. return ResultFromScode(sc);
  145. }
  146. //**********************************************************************
  147. //
  148. // CSimpSvrApp::AddRef
  149. //
  150. // Purpose:
  151. //
  152. // Adds to the reference count at the Application level.
  153. //
  154. // Parameters:
  155. //
  156. // None
  157. //
  158. // Return Value:
  159. //
  160. // ULONG - The new reference count of the application.
  161. //
  162. // Function Calls:
  163. // Function Location
  164. //
  165. // TestDebugOut Windows API
  166. //
  167. // Comments:
  168. //
  169. // Due to the reference counting model that is used in this
  170. // implementation, this reference count is the sum of the
  171. // reference counts on all interfaces of all objects open
  172. // in the application.
  173. //
  174. //********************************************************************
  175. STDMETHODIMP_(ULONG) CSimpSvrApp::AddRef()
  176. {
  177. TestDebugOut("In CSimpSvrApp::AddRef\r\n");
  178. return ++m_nCount;
  179. }
  180. //**********************************************************************
  181. //
  182. // CSimpSvrApp::Release
  183. //
  184. // Purpose:
  185. //
  186. // Decrements the reference count at this level
  187. //
  188. // Parameters:
  189. //
  190. // None
  191. //
  192. // Return Value:
  193. //
  194. // ULONG - The new reference count of the application.
  195. //
  196. // Function Calls:
  197. // Function Location
  198. //
  199. // TestDebugOut Windows API
  200. //
  201. // Comments:
  202. //
  203. // Due to the reference counting model that is used in this
  204. // implementation, this reference count is the sum of the
  205. // reference counts on all interfaces of all objects open
  206. // in the application.
  207. //
  208. //********************************************************************
  209. STDMETHODIMP_(ULONG) CSimpSvrApp::Release()
  210. {
  211. TestDebugOut("In CSimpSvrApp::Release\r\n");
  212. if (--m_nCount == 0) {
  213. delete this;
  214. return 0;
  215. }
  216. return m_nCount;
  217. }
  218. //**********************************************************************
  219. //
  220. // CSimpSvrApp::fInitApplication
  221. //
  222. // Purpose:
  223. //
  224. // Initializes the application
  225. //
  226. // Parameters:
  227. //
  228. // HANDLE hInstance - Instance handle of the application.
  229. //
  230. // Return Value:
  231. //
  232. // TRUE - Application was successfully initialized.
  233. // FALSE - Application could not be initialized
  234. //
  235. // Function Calls:
  236. // Function Location
  237. //
  238. // LoadIcon Windows API
  239. // LoadCursor Windows API
  240. // GetStockObject Windows API
  241. // RegisterClass Windows API
  242. // RegisterHatchWindowClass OUTLUI.DLL
  243. //
  244. // Comments:
  245. //
  246. //********************************************************************
  247. BOOL CSimpSvrApp::fInitApplication(HANDLE hInstance)
  248. {
  249. WNDCLASS wc;
  250. // Fill in window class structure with parameters that describe the
  251. // main window.
  252. wc.style = NULL; // Class style(s).
  253. wc.lpfnWndProc = MainWndProc; // Function to retrieve messages for
  254. // windows of this class.
  255. wc.cbClsExtra = 0; // No per-class extra data.
  256. wc.cbWndExtra = 0; // No per-window extra data.
  257. wc.hInstance = hInstance; // Application that owns the class.
  258. wc.hIcon = LoadIcon(hInstance, "SimpSvr");
  259. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  260. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  261. wc.lpszMenuName = "SimpSvrMENU"; // Name of menu resource in .RC file.
  262. wc.lpszClassName = "SimpSvrWClass"; // Name used in call to CreateWindow.
  263. if (!RegisterClass(&wc))
  264. return FALSE;
  265. wc.style = CS_VREDRAW | CS_HREDRAW; // Class style(s).
  266. wc.lpfnWndProc = DocWndProc; // Function to retrieve messages for
  267. // windows of this class.
  268. wc.cbClsExtra = 0; // No per-class extra data.
  269. wc.cbWndExtra = 0; // No per-window extra data.
  270. wc.hInstance = hInstance; // Application that owns the class.
  271. wc.hIcon = NULL;
  272. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  273. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  274. wc.lpszMenuName = NULL;
  275. wc.lpszClassName = "DocWClass"; // Name used in call to CreateWindow.
  276. // Register the window class and return success/failure code.
  277. if (!RegisterClass(&wc))
  278. return FALSE;
  279. return (RegisterHatchWindowClass(hInstance));
  280. }
  281. //**********************************************************************
  282. //
  283. // CSimpSvrApp::fInitInstance
  284. //
  285. // Purpose:
  286. //
  287. // Instance initialization.
  288. //
  289. // Parameters:
  290. //
  291. // HANDLE hInstance - App. Instance Handle.
  292. //
  293. // int nCmdShow - Show parameter from WinMain
  294. //
  295. // Return Value:
  296. //
  297. // TRUE - Initialization Successful
  298. // FALSE - Initialization Failed.
  299. //
  300. //
  301. // Function Calls:
  302. // Function Location
  303. //
  304. // CreateWindow Windows API
  305. // InvalidateRect Windows API
  306. // ShowWindow Windows API
  307. // UpdateWindow Windows API
  308. // CoRegisterClassObject OLE API
  309. // OleBuildVersion OLE API
  310. // OleInitialize OLE API
  311. // CSimpSvrDoc::CreateObject DOC.CPP
  312. //
  313. // Comments:
  314. //
  315. // Note that successful Initalization of the OLE libraries
  316. // is remembered so the UnInit is only called if needed.
  317. //
  318. //********************************************************************
  319. BOOL CSimpSvrApp::fInitInstance (HANDLE hInstance, int nCmdShow, CClassFactory FAR * lpClassFactory)
  320. {
  321. m_hInst = hInstance;
  322. DWORD dwVer = OleBuildVersion();
  323. // check to see if we are compatible with this version of the libraries
  324. if (HIWORD(dwVer) != rmm || LOWORD(dwVer) < rup)
  325. TestDebugOut("*** WARNING: Not compatible with current libs ***\r\n");
  326. // initialize the libraries
  327. if (OleInitialize(NULL) == NOERROR)
  328. m_fInitialized = TRUE;
  329. // Create the "application" windows
  330. m_hAppWnd = CreateWindow ("SimpSvrWClass",
  331. "Simple OLE 2.0 Server",
  332. WS_OVERLAPPEDWINDOW,
  333. CW_USEDEFAULT,
  334. CW_USEDEFAULT,
  335. CW_USEDEFAULT,
  336. CW_USEDEFAULT,
  337. NULL,
  338. NULL,
  339. hInstance,
  340. NULL);
  341. if (!m_hAppWnd)
  342. return FALSE;
  343. // if not started by OLE, then show the Window, and create a "fake" object, else
  344. // Register a pointer to IClassFactory so that OLE can instruct us to make an
  345. // object at the appropriate time.
  346. if (!m_fStartByOle)
  347. {
  348. ShowAppWnd(nCmdShow);
  349. m_lpDoc->CreateObject(IID_IOleObject, (LPVOID FAR *)&m_OleObject);
  350. InvalidateRect(m_lpDoc->GethDocWnd(), NULL, TRUE);
  351. }
  352. else
  353. {
  354. lpClassFactory = new CClassFactory(this);
  355. // shouldn't pass an API an object with a zero ref count
  356. lpClassFactory->AddRef();
  357. CoRegisterClassObject(GUID_SIMPLE,(IUnknown FAR *)lpClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &m_dwRegisterClass);
  358. // remove artificial Ref. count
  359. lpClassFactory->Release();
  360. }
  361. m_hMainMenu = GetMenu(m_hAppWnd);
  362. m_hColorMenu = GetSubMenu(m_hMainMenu, 1);
  363. m_hHelpMenu = GetSubMenu(m_hMainMenu, 2);
  364. return m_fInitialized;
  365. }
  366. //**********************************************************************
  367. //
  368. // CSimpSvrApp::lCommandHandler
  369. //
  370. // Purpose:
  371. //
  372. // Handles the processing of WM_COMMAND.
  373. //
  374. // Parameters:
  375. //
  376. // HWND hWnd - Handle to the application Window
  377. //
  378. // UINT message - message (always WM_COMMAND)
  379. //
  380. // WPARAM wParam - Same as passed to the WndProc
  381. //
  382. // LPARAM lParam - Same as passed to the WndProc
  383. //
  384. // Return Value:
  385. //
  386. // NULL
  387. //
  388. // Function Calls:
  389. // Function Location
  390. //
  391. // GetClientRect Windows API
  392. // MessageBox Windows API
  393. // DialogBox Windows API
  394. // MakeProcInstance Windows API
  395. // FreeProcInstance Windows API
  396. // SendMessage Windows API
  397. // DefWindowProc Windows API
  398. // InvalidateRect Windows API
  399. // CSimpSvrDoc::InsertObject DOC.CPP
  400. // CSimpSvrObj::SetColor OBJ.CPP
  401. // CSimpSvrObj::RotateColor OBJ.CPP
  402. //
  403. // Comments:
  404. //
  405. //********************************************************************
  406. long CSimpSvrApp::lCommandHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  407. {
  408. switch (wParam) {
  409. // bring up the About box
  410. case IDM_ABOUT:
  411. {
  412. FARPROC lpProcAbout = MakeProcInstance((FARPROC)About, m_hInst);
  413. DialogBox(m_hInst, // current instance
  414. "AboutBox", // resource to use
  415. m_hAppWnd, // parent handle
  416. lpProcAbout); // About() instance address
  417. FreeProcInstance(lpProcAbout);
  418. break;
  419. }
  420. // exit the application
  421. case IDM_EXIT:
  422. SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  423. break;
  424. case IDM_RED:
  425. m_lpDoc->GetObj()->SetColor (128, 0, 0);
  426. InvalidateRect(m_lpDoc->GethDocWnd(), NULL, TRUE);
  427. break;
  428. case IDM_GREEN:
  429. m_lpDoc->GetObj()->SetColor (0,128, 0);
  430. InvalidateRect(m_lpDoc->GethDocWnd(), NULL, TRUE);
  431. break;
  432. case IDM_BLUE:
  433. m_lpDoc->GetObj()->SetColor (0, 0, 128);
  434. InvalidateRect(m_lpDoc->GethDocWnd(), NULL, TRUE);
  435. break;
  436. case IDM_ROTATE:
  437. m_lpDoc->GetObj()->RotateColor();
  438. InvalidateRect(m_lpDoc->GethDocWnd(), NULL, TRUE);
  439. break;
  440. default:
  441. return (DefWindowProc(hWnd, message, wParam, lParam));
  442. } // end of switch
  443. return NULL;
  444. }
  445. //**********************************************************************
  446. //
  447. // CSimpSvrApp::lSizeHandler
  448. //
  449. // Purpose:
  450. //
  451. // Handles the WM_SIZE message
  452. //
  453. // Parameters:
  454. //
  455. // HWND hWnd - Handle to the application Window
  456. //
  457. // UINT message - message (always WM_SIZE)
  458. //
  459. // WPARAM wParam - Same as passed to the WndProc
  460. //
  461. // LPARAM lParam - Same as passed to the WndProc
  462. //
  463. // Return Value:
  464. //
  465. // LONG - returned from the "document" resizing
  466. //
  467. // Function Calls:
  468. // Function Location
  469. //
  470. // GetClientRect Windows API
  471. // CSimpSvrDoc::lResizeDoc DOC.CPP
  472. //
  473. // Comments:
  474. //
  475. //********************************************************************
  476. long CSimpSvrApp::lSizeHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  477. {
  478. RECT rect;
  479. GetClientRect(m_hAppWnd, &rect);
  480. return m_lpDoc->lResizeDoc(&rect);
  481. }
  482. //**********************************************************************
  483. //
  484. // CSimpSvrApp::lCreateDoc
  485. // d
  486. // Purpose:
  487. //
  488. // Handles the creation of a document.
  489. //
  490. // Parameters:
  491. //
  492. // HWND hWnd - Handle to the application Window
  493. //
  494. // UINT message - message (always WM_CREATE)
  495. //
  496. // WPARAM wParam - Same as passed to the WndProc
  497. //
  498. // LPARAM lParam - Same as passed to the WndProc
  499. //
  500. // Return Value:
  501. //
  502. // NULL
  503. //
  504. // Function Calls:
  505. // Function Location
  506. //
  507. // GetClientRect Windows API
  508. // CSimpSvrDoc::Create DOC.CPP
  509. //
  510. // Comments:
  511. //
  512. //********************************************************************
  513. long CSimpSvrApp::lCreateDoc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  514. {
  515. RECT rect;
  516. GetClientRect(hWnd, &rect);
  517. m_lpDoc = CSimpSvrDoc::Create(this, &rect, hWnd);
  518. return NULL;
  519. }
  520. //**********************************************************************
  521. //
  522. // CSimpSvrApp::PaintApp
  523. //
  524. // Purpose:
  525. //
  526. // Handles the painting of the doc window.
  527. //
  528. //
  529. // Parameters:
  530. //
  531. // HDC hDC - hDC to the Doc Window.
  532. //
  533. // Return Value:
  534. //
  535. // None
  536. //
  537. // Function Calls:
  538. // Function Location
  539. //
  540. // CSimpSvrDoc::PaintDoc DOC.CPP
  541. //
  542. // Comments:
  543. //
  544. //
  545. //********************************************************************
  546. void CSimpSvrApp::PaintApp (HDC hDC)
  547. {
  548. // if we supported multiple documents, we would enumerate
  549. // through each of the open documents and call paint.
  550. if (m_lpDoc)
  551. m_lpDoc->PaintDoc(hDC);
  552. }
  553. //**********************************************************************
  554. //
  555. // CSimpSvrApp::ParseCmdLine
  556. //
  557. // Purpose:
  558. //
  559. // Determines if the app was started by OLE
  560. //
  561. //
  562. // Parameters:
  563. //
  564. // LPSTR lpCmdLine - Pointer to the command line
  565. //
  566. // Return Value:
  567. //
  568. // None
  569. //
  570. // Function Calls:
  571. // Function Location
  572. //
  573. // lstrlen Windows API
  574. // lstrcmp Windows API
  575. //
  576. //
  577. // Comments:
  578. //
  579. // Parses the command line looking for the -Embedding or /Embedding
  580. // flag.
  581. //
  582. //********************************************************************
  583. void CSimpSvrApp::ParseCmdLine(LPSTR lpCmdLine)
  584. {
  585. char szTemp[255];
  586. m_fStartByOle = TRUE;
  587. ::ParseCmdLine (lpCmdLine, &m_fStartByOle, szTemp);
  588. }
  589. //**********************************************************************
  590. //
  591. // CSimpSvrApp::SetStatusText
  592. //
  593. // Purpose:
  594. //
  595. // Blanks out the text in the status bar
  596. //
  597. //
  598. // Parameters:
  599. //
  600. // None
  601. //
  602. // Return Value:
  603. //
  604. // None
  605. //
  606. // Function Calls:
  607. // Function Location
  608. //
  609. // CSimpSvrDoc::SetStatusText DOC.CPP
  610. //
  611. //
  612. // Comments:
  613. //
  614. //
  615. //********************************************************************
  616. void CSimpSvrApp::SetStatusText()
  617. {
  618. m_lpDoc->SetStatusText();
  619. }
  620. //**********************************************************************
  621. //
  622. // CSimpSvrApp::IsInPlaceActive
  623. //
  624. // Purpose:
  625. //
  626. // Safely determines from the app level if currently inplace active.
  627. //
  628. //
  629. // Parameters:
  630. //
  631. // None
  632. //
  633. // Return Value:
  634. //
  635. // TRUE - Inplace active
  636. // FALSE - Not Inplace active
  637. //
  638. // Function Calls:
  639. // Function Location
  640. //
  641. // CSimpSvrDoc::GetObject OBJ.H
  642. // CSimpSvrObj:IsInPlaceActive OBJ.H
  643. //
  644. //
  645. // Comments:
  646. //
  647. //
  648. //********************************************************************
  649. BOOL CSimpSvrApp::IsInPlaceActive()
  650. {
  651. BOOL retval = FALSE;
  652. if (m_lpDoc)
  653. if (m_lpDoc->GetObj())
  654. retval = m_lpDoc->GetObj()->IsInPlaceActive();
  655. return retval;
  656. }
  657. //**********************************************************************
  658. //
  659. // CSimpSvrApp::ShowAppWnd
  660. //
  661. // Purpose:
  662. //
  663. // Shows the Application Window
  664. //
  665. // Parameters:
  666. //
  667. // int nCmdShow - Window State
  668. //
  669. // Return Value:
  670. //
  671. // None
  672. //
  673. // Function Calls:
  674. // Function Location
  675. //
  676. // ShowWindow Windows API
  677. // UpdateWindow Windows API
  678. // CoLockObjectExternal OLE API
  679. //
  680. // Comments:
  681. //
  682. //********************************************************************
  683. void CSimpSvrApp::ShowAppWnd(int nCmdShow)
  684. {
  685. CoLockObjectExternal(this, TRUE, FALSE);
  686. ShowWindow (m_hAppWnd, nCmdShow);
  687. UpdateWindow (m_hAppWnd);
  688. }
  689. //**********************************************************************
  690. //
  691. // CSimpSvrApp::ShowAppWnd
  692. //
  693. // Purpose:
  694. //
  695. // Hides the Application Window
  696. //
  697. // Parameters:
  698. //
  699. // None
  700. //
  701. // Return Value:
  702. //
  703. // None
  704. //
  705. // Function Calls:
  706. // Function Location
  707. //
  708. // ShowWindow Windows API
  709. // CoLockObjectExternal OLE API
  710. //
  711. // Comments:
  712. //
  713. //********************************************************************
  714. void CSimpSvrApp::HideAppWnd()
  715. {
  716. CoLockObjectExternal(this, FALSE, TRUE);
  717. ShowWindow (m_hAppWnd, SW_HIDE);
  718. }