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.

710 lines
19 KiB

  1. //**********************************************************************
  2. // File name: app.cpp
  3. //
  4. // Implementation file for the CSimpleApp Class
  5. //
  6. // Functions:
  7. //
  8. // See app.h for a list of member functions.
  9. //
  10. // Copyright (c) 1992 - 1993 Microsoft Corporation. All rights reserved.
  11. //**********************************************************************
  12. #include "pre.h"
  13. #include "iocs.h"
  14. #include "ias.h"
  15. #include "app.h"
  16. #include "site.h"
  17. #include "doc.h"
  18. #include <testmess.h>
  19. //**********************************************************************
  20. //
  21. // CSimpleApp::CSimpleApp()
  22. //
  23. // Purpose:
  24. //
  25. // Constructor for CSimpleApp
  26. //
  27. // Parameters:
  28. //
  29. // None
  30. //
  31. // Return Value:
  32. //
  33. // None
  34. //
  35. // Function Calls:
  36. // Function Location
  37. //
  38. // TestDebugOut Windows API
  39. // SetRectEmpty Windows API
  40. //
  41. // Comments:
  42. //
  43. //********************************************************************
  44. CSimpleApp::CSimpleApp()
  45. {
  46. TestDebugOut("In CSimpleApp's Constructor \r\n");
  47. // Set Ref Count
  48. m_nCount = 0;
  49. // clear members
  50. m_hAppWnd = NULL;
  51. m_hDriverWnd = NULL;
  52. m_hInst = NULL;
  53. m_lpDoc = NULL;
  54. // clear flags
  55. m_fInitialized = FALSE;
  56. }
  57. //**********************************************************************
  58. //
  59. // CSimpleApp::~CSimpleApp()
  60. //
  61. // Purpose:
  62. //
  63. // Destructor for CSimpleApp Class.
  64. //
  65. // Parameters:
  66. //
  67. // None
  68. //
  69. // Return Value:
  70. //
  71. // None
  72. //
  73. // Function Calls:
  74. // Function Location
  75. //
  76. // TestDebugOut Windows API
  77. // OleUninitialize OLE API
  78. //
  79. // Comments:
  80. //
  81. //********************************************************************
  82. CSimpleApp::~CSimpleApp()
  83. {
  84. TestDebugOut("In CSimpleApp's Destructor\r\n");
  85. // need to uninit the library...
  86. if (m_fInitialized)
  87. OleUninitialize();
  88. }
  89. //**********************************************************************
  90. //
  91. // CSimpleApp::DestroyDocs()
  92. //
  93. // Purpose:
  94. //
  95. // Destroys all of the open documents in the application (Only one
  96. // since this is an SDI app, but could easily be modified to
  97. // support MDI).
  98. //
  99. // Parameters:
  100. //
  101. // None
  102. //
  103. // Return Value:
  104. //
  105. // None
  106. //
  107. // Function Calls:
  108. // Function Location
  109. //
  110. // Comments:
  111. //
  112. //********************************************************************
  113. void CSimpleApp::DestroyDocs()
  114. {
  115. m_lpDoc->Close(); // we have only 1 document
  116. }
  117. //**********************************************************************
  118. //
  119. // CSimpleApp::QueryInterface
  120. //
  121. // Purpose:
  122. //
  123. // Used for interface negotiation at the Frame level.
  124. //
  125. // Parameters:
  126. //
  127. // REFIID riid - A reference to the interface that is
  128. // being queried.
  129. //
  130. // LPVOID FAR* ppvObj - An out parameter to return a pointer to
  131. // the interface.
  132. //
  133. // Return Value:
  134. //
  135. // S_OK - The interface is supported.
  136. // S_FALSE - The interface is not supported
  137. //
  138. // Function Calls:
  139. // Function Location
  140. //
  141. // TestDebugOut Windows API
  142. // ResultFromScode OLE API
  143. //
  144. // Comments:
  145. //
  146. //********************************************************************
  147. STDMETHODIMP CSimpleApp::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  148. {
  149. TestDebugOut("In CSimpleApp::QueryInterface\r\n");
  150. *ppvObj = NULL; // must set out pointer parameters to NULL
  151. // Not a supported interface
  152. return ResultFromScode(E_NOINTERFACE);
  153. }
  154. //**********************************************************************
  155. //
  156. // CSimpleApp::AddRef
  157. //
  158. // Purpose:
  159. //
  160. // Adds to the reference count at the Application level.
  161. //
  162. // Parameters:
  163. //
  164. // None
  165. //
  166. // Return Value:
  167. //
  168. // ULONG - The new reference count of the application.
  169. //
  170. // Function Calls:
  171. // Function Location
  172. //
  173. // TestDebugOut Windows API
  174. //
  175. // Comments:
  176. //
  177. // Due to the reference counting model that is used in this
  178. // implementation, this reference count is the sum of the
  179. // reference counts on all interfaces of all objects open
  180. // in the application.
  181. //
  182. //********************************************************************
  183. STDMETHODIMP_(ULONG) CSimpleApp::AddRef()
  184. {
  185. TestDebugOut("In CSimpleApp::AddRef\r\n");
  186. return ++m_nCount;
  187. }
  188. //**********************************************************************
  189. //
  190. // CSimpleApp::Release
  191. //
  192. // Purpose:
  193. //
  194. // Decrements the reference count at this level
  195. //
  196. // Parameters:
  197. //
  198. // None
  199. //
  200. // Return Value:
  201. //
  202. // ULONG - The new reference count of the application.
  203. //
  204. // Function Calls:
  205. // Function Location
  206. //
  207. // TestDebugOut Windows API
  208. //
  209. // Comments:
  210. //
  211. //********************************************************************
  212. STDMETHODIMP_(ULONG) CSimpleApp::Release()
  213. {
  214. TestDebugOut("In CSimpleApp::Release\r\n");
  215. if (--m_nCount == 0) {
  216. delete this;
  217. return 0;
  218. }
  219. return m_nCount;
  220. }
  221. //**********************************************************************
  222. //
  223. // CSimpleApp::fInitApplication
  224. //
  225. // Purpose:
  226. //
  227. // Initializes the application
  228. //
  229. // Parameters:
  230. //
  231. // HANDLE hInstance - Instance handle of the application.
  232. //
  233. // Return Value:
  234. //
  235. // TRUE - Application was successfully initialized.
  236. // FALSE - Application could not be initialized
  237. //
  238. // Function Calls:
  239. // Function Location
  240. //
  241. // LoadIcon Windows API
  242. // LoadCursor Windows API
  243. // GetStockObject Windows API
  244. // RegisterClass Windows API
  245. //
  246. // Comments:
  247. //
  248. //********************************************************************
  249. BOOL CSimpleApp::fInitApplication(HANDLE hInstance)
  250. {
  251. WNDCLASS wc;
  252. // Fill in window class structure with parameters that describe the
  253. // main window.
  254. wc.style = NULL; // Class style(s).
  255. wc.lpfnWndProc = MainWndProc; // Function to retrieve messages for
  256. // windows of this class.
  257. wc.cbClsExtra = 0; // No per-class extra data.
  258. wc.cbWndExtra = 0; // No per-window extra data.
  259. wc.hInstance = hInstance; // Application that owns the class.
  260. wc.hIcon = LoadIcon(hInstance,"SimpDnd");
  261. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  262. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  263. wc.lpszMenuName = "SIMPLEMENU"; // Name of menu resource in .RC file.
  264. wc.lpszClassName = "SimpDndAppWClass"; // Name used in CreateWindow call.
  265. if (!RegisterClass(&wc))
  266. return FALSE;
  267. wc.style = CS_DBLCLKS; // Class style(s). allow DBLCLK's
  268. wc.lpfnWndProc = DocWndProc; // Function to retrieve messages for
  269. // windows of this class.
  270. wc.cbClsExtra = 0; // No per-class extra data.
  271. wc.cbWndExtra = 0; // No per-window extra data.
  272. wc.hInstance = hInstance; // Application that owns the class.
  273. wc.hIcon = NULL;
  274. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  275. wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  276. wc.lpszMenuName = NULL;
  277. wc.lpszClassName = "SimpDndDocWClass"; // Name used in CreateWindow call.
  278. // Register the window class and return success/failure code.
  279. return (RegisterClass(&wc));
  280. }
  281. //**********************************************************************
  282. //
  283. // CSimpleApp::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. // ShowWindow Windows API
  306. // UpdateWindow Windows API
  307. // OleBuildVersion OLE API
  308. // OleInitialize OLE API
  309. //
  310. // Comments:
  311. //
  312. // Note that successful Initalization of the OLE libraries
  313. // is remembered so the UnInit is only called if needed.
  314. //
  315. //********************************************************************
  316. BOOL CSimpleApp::fInitInstance (HANDLE hInstance, int nCmdShow)
  317. {
  318. DWORD dwVer = OleBuildVersion();
  319. LPMALLOC lpMalloc = NULL;
  320. #ifdef NO
  321. // check to see if we are compatible with this version of the libraries
  322. if (HIWORD(dwVer) != rmm || LOWORD(dwVer) < rup) {
  323. #ifdef _DEBUG
  324. TestDebugOut("WARNING: Incompatible OLE library version\r\n");
  325. #else
  326. return FALSE;
  327. #endif
  328. }
  329. #endif
  330. #if defined( _DEBUG )
  331. /* OLE2NOTE: Use a special debug allocator to help track down
  332. ** memory leaks.
  333. */
  334. OleStdCreateDbAlloc(0, &lpMalloc);
  335. #endif
  336. if (OleInitialize(lpMalloc) == NOERROR)
  337. m_fInitialized = TRUE;
  338. #if defined( _DEBUG )
  339. /* OLE2NOTE: release the special debug allocator so that only OLE is
  340. ** holding on to it. later when OleUninitialize is called, then
  341. ** the debug allocator object will be destroyed. when the debug
  342. ** allocator object is destoyed, it will report (to the Output
  343. ** Debug Terminal) whether there are any memory leaks.
  344. */
  345. if (lpMalloc) lpMalloc->Release();
  346. #endif
  347. m_hInst = hInstance;
  348. // Create the "application" windows
  349. m_hAppWnd = CreateWindow ("SimpDndAppWClass",
  350. "Simple OLE 2.0 Drag/Drop Container",
  351. WS_OVERLAPPEDWINDOW,
  352. CW_USEDEFAULT,
  353. CW_USEDEFAULT,
  354. CW_USEDEFAULT,
  355. CW_USEDEFAULT,
  356. NULL,
  357. NULL,
  358. hInstance,
  359. NULL);
  360. if (!m_hAppWnd)
  361. return FALSE;
  362. // if we have been launched by the test driver, tell it our window handle
  363. if( m_hDriverWnd )
  364. {
  365. PostMessage(m_hDriverWnd, WM_TESTREG, (WPARAM)m_hAppWnd, 0);
  366. }
  367. // delay before dragging should start, in milliseconds
  368. m_nDragDelay = GetProfileInt(
  369. "windows",
  370. "DragDelay",
  371. DD_DEFDRAGDELAY
  372. );
  373. // minimum distance (radius) before drag should start, in pixels
  374. m_nDragMinDist = GetProfileInt(
  375. "windows",
  376. "DragMinDist",
  377. DD_DEFDRAGMINDIST
  378. );
  379. // delay before scrolling, in milliseconds
  380. m_nScrollDelay = GetProfileInt(
  381. "windows",
  382. "DragScrollDelay",
  383. DD_DEFSCROLLDELAY
  384. );
  385. // inset-width of the hot zone, in pixels
  386. m_nScrollInset = GetProfileInt(
  387. "windows",
  388. "DragScrollInset",
  389. DD_DEFSCROLLINSET
  390. );
  391. // scroll interval, in milliseconds
  392. m_nScrollInterval = GetProfileInt(
  393. "windows",
  394. "DragScrollInterval",
  395. DD_DEFSCROLLINTERVAL
  396. );
  397. ShowWindow (m_hAppWnd, nCmdShow);
  398. UpdateWindow (m_hAppWnd);
  399. return m_fInitialized;
  400. }
  401. //**********************************************************************
  402. //
  403. // CSimpleApp::lCommandHandler
  404. //
  405. // Purpose:
  406. //
  407. // Handles the processing of WM_COMMAND.
  408. //
  409. // Parameters:
  410. //
  411. // HWND hWnd - Handle to the application Window
  412. //
  413. // UINT message - message (always WM_COMMAND)
  414. //
  415. // WPARAM wParam - Same as passed to the WndProc
  416. //
  417. // LPARAM lParam - Same as passed to the WndProc
  418. //
  419. // Return Value:
  420. //
  421. // NULL
  422. //
  423. // Function Calls:
  424. // Function Location
  425. //
  426. // IOleObject::DoVerb Object
  427. // GetClientRect Windows API
  428. // MessageBox Windows API
  429. // DialogBox Windows API
  430. // MakeProcInstance Windows API
  431. // FreeProcInstance Windows API
  432. // SendMessage Windows API
  433. // DefWindowProc Windows API
  434. // CSimpleDoc::InsertObject DOC.CPP
  435. //
  436. // Comments:
  437. //
  438. //********************************************************************
  439. long CSimpleApp::lCommandHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  440. {
  441. RECT rect;
  442. // see if the command is a verb selections
  443. if (wParam >= IDM_VERB0)
  444. {
  445. // get the rectangle of the object
  446. m_lpDoc->m_lpSite->GetObjRect(&rect);
  447. m_lpDoc->m_lpSite->m_lpOleObject->DoVerb(
  448. wParam - IDM_VERB0, NULL,
  449. &m_lpDoc->m_lpSite->m_OleClientSite, -1,
  450. m_lpDoc->m_hDocWnd, &rect);
  451. }
  452. else
  453. {
  454. switch (wParam) {
  455. // bring up the About box
  456. case IDM_ABOUT:
  457. {
  458. FARPROC lpProcAbout = MakeProcInstance((FARPROC)About, m_hInst);
  459. DialogBox(m_hInst, // current instance
  460. "AboutBox", // resource to use
  461. m_hAppWnd, // parent handle
  462. lpProcAbout); // About() instance address
  463. FreeProcInstance(lpProcAbout);
  464. break;
  465. }
  466. // bring up the InsertObject Dialog
  467. case IDM_INSERTOBJECT:
  468. m_lpDoc->InsertObject();
  469. break;
  470. // Copy the object to the Clipboard
  471. case IDM_COPY:
  472. m_lpDoc->CopyObjectToClip();
  473. break;
  474. // exit the application
  475. case IDM_EXIT:
  476. SendMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  477. break;
  478. case IDM_NEW:
  479. m_lpDoc->Close();
  480. m_lpDoc = NULL;
  481. lCreateDoc(hWnd, 0, 0, 0);
  482. break;
  483. default:
  484. return (DefWindowProc(hWnd, message, wParam, lParam));
  485. } // end of switch
  486. } // end of else
  487. return NULL;
  488. }
  489. //**********************************************************************
  490. //
  491. // CSimpleApp::lSizeHandler
  492. //
  493. // Purpose:
  494. //
  495. // Handles the WM_SIZE message
  496. //
  497. // Parameters:
  498. //
  499. // HWND hWnd - Handle to the application Window
  500. //
  501. // UINT message - message (always WM_SIZE)
  502. //
  503. // WPARAM wParam - Same as passed to the WndProc
  504. //
  505. // LPARAM lParam - Same as passed to the WndProc
  506. //
  507. // Return Value:
  508. //
  509. // LONG - returned from the "document" resizing
  510. //
  511. // Function Calls:
  512. // Function Location
  513. //
  514. // GetClientRect Windows API
  515. // CSimpleDoc::lResizeDoc DOC.CPP
  516. //
  517. // Comments:
  518. //
  519. //********************************************************************
  520. long CSimpleApp::lSizeHandler (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  521. {
  522. RECT rect;
  523. GetClientRect(m_hAppWnd, &rect);
  524. return m_lpDoc->lResizeDoc(&rect);
  525. }
  526. //**********************************************************************
  527. //
  528. // CSimpleApp::lCreateDoc
  529. //
  530. // Purpose:
  531. //
  532. // Handles the creation of a document.
  533. //
  534. // Parameters:
  535. //
  536. // HWND hWnd - Handle to the application Window
  537. //
  538. // UINT message - message (always WM_CREATE)
  539. //
  540. // WPARAM wParam - Same as passed to the WndProc
  541. //
  542. // LPARAM lParam - Same as passed to the WndProc
  543. //
  544. // Return Value:
  545. //
  546. // NULL
  547. //
  548. // Function Calls:
  549. // Function Location
  550. //
  551. // GetClientRect Windows API
  552. // CSimpleDoc::CSimpleDoc DOC.CPP
  553. //
  554. // Comments:
  555. //
  556. //********************************************************************
  557. long CSimpleApp::lCreateDoc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  558. {
  559. RECT rect;
  560. GetClientRect(hWnd, &rect);
  561. m_lpDoc = CSimpleDoc::Create(this, &rect, hWnd);
  562. return NULL;
  563. }
  564. //**********************************************************************
  565. //
  566. // CSimpleApp::HandleAccelerators
  567. //
  568. // Purpose:
  569. //
  570. // To properly handle accelerators in the Message Loop
  571. //
  572. // Parameters:
  573. //
  574. // LPMSG lpMsg - A pointer to the message structure.
  575. //
  576. // Return Value:
  577. //
  578. // TRUE - The accelerator was handled
  579. // FALSE - The accelerator was not handled
  580. //
  581. // Function Calls:
  582. // Function Location
  583. //
  584. // Comments:
  585. //
  586. //********************************************************************
  587. BOOL CSimpleApp::HandleAccelerators(LPMSG lpMsg)
  588. {
  589. BOOL retval = FALSE;
  590. // we do not have any accelerators
  591. return retval;
  592. }
  593. //**********************************************************************
  594. //
  595. // CSimpleApp::PaintApp
  596. //
  597. // Purpose:
  598. //
  599. // Handles the painting of the doc window.
  600. //
  601. //
  602. // Parameters:
  603. //
  604. // HDC hDC - hDC to the Doc Window.
  605. //
  606. // Return Value:
  607. //
  608. // None
  609. //
  610. // Function Calls:
  611. // Function Location
  612. //
  613. // CSimpleDoc::PaintDoc DOC.CPP
  614. //
  615. // Comments:
  616. //
  617. // This is an app level function in case we want to do palette
  618. // management.
  619. //
  620. //********************************************************************
  621. void CSimpleApp::PaintApp (HDC hDC)
  622. {
  623. // at this level, we could enumerate through all of the
  624. // visible objects in the application, so that a palette
  625. // that best fits all of the objects can be built.
  626. // This app is designed to take on the same palette
  627. // functionality that was provided in OLE 1.0, the palette
  628. // of the last object drawn is realized. Since we only
  629. // support one object at a time, it shouldn't be a big
  630. // deal.
  631. // if we supported multiple documents, we would enumerate
  632. // through each of the open documents and call paint.
  633. if (m_lpDoc)
  634. m_lpDoc->PaintDoc(hDC);
  635. }