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.

502 lines
14 KiB

  1. //**********************************************************************
  2. // File name: Simple.cpp
  3. //
  4. // Main source file for the Simple OLE 2.0 object container
  5. //
  6. // Functions:
  7. //
  8. // WinMain - Program entry point
  9. // MainWndProc - Processes messages for the frame window
  10. // About - Processes messages for the about dialog
  11. // DocWndProc - Processes messages for the doc window
  12. //
  13. // Copyright (c) 1992 - 1993 Microsoft Corporation. All rights reserved.
  14. //**********************************************************************
  15. #include "pre.h"
  16. #include "iocs.h"
  17. #include "ias.h"
  18. #include "app.h"
  19. #include "site.h"
  20. #include "doc.h"
  21. #include <stdlib.h>
  22. #include <testmess.h>
  23. #include "tests.h"
  24. // This line is needed for the debug utilities in OLE2UI
  25. extern "C" {
  26. OLEDBGDATA_MAIN(TEXT("SIMPDND"))
  27. }
  28. CSimpleApp FAR * lpCSimpleApp;
  29. BOOL fBeVerbose = FALSE;
  30. extern "C"
  31. void TestDebugOut(LPSTR psz)
  32. {
  33. if (fBeVerbose)
  34. {
  35. OutputDebugStringA(psz);
  36. }
  37. }
  38. BOOL gfUseEmptyEnumerator;
  39. //**********************************************************************
  40. //
  41. // WinMain
  42. //
  43. // Purpose:
  44. //
  45. // Program entry point
  46. //
  47. // Parameters:
  48. //
  49. // HANDLE hInstance - Instance handle for this instance
  50. //
  51. // HANDLE hPrevInstance - Instance handle for the last instance
  52. //
  53. // LPSTR lpCmdLine - Pointer to the command line
  54. //
  55. // int nCmdShow - Window State
  56. //
  57. // Return Value:
  58. //
  59. // msg.wParam or FALSE if failure to initialize
  60. //
  61. // Function Calls:
  62. // Function Location
  63. //
  64. // CSimpleApp::CSimpleApp APP.CPP
  65. // CSimpleApp::fInitApplication APP.CPP
  66. // CSimpleApp::fInitInstance APP.CPP
  67. // CSimpleApp::~CSimpleApp APP.CPP
  68. // CSimpleApp::AddRef APP.CPP
  69. // CSimpleApp::Release APP.CPP
  70. // OleUIInitialize OLE2UI
  71. // OleUIUninitialize OLE2UI
  72. // GetMessage Windows API
  73. // SetMessageQueue Windows API
  74. // MessageBox Windows API
  75. // TranslateMessage Windows API
  76. // DispatchMessage Windows API
  77. //
  78. //
  79. //********************************************************************
  80. int PASCAL WinMain
  81. #ifdef WIN32
  82. (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  83. int nCmdShow)
  84. #else
  85. (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  86. #endif
  87. {
  88. MSG msg;
  89. LPSTR pszTemp;
  90. fBeVerbose = GetProfileInt("OLEUTEST","BeVerbose",0);
  91. if(fBeVerbose == 0)
  92. {
  93. fBeVerbose = GetProfileInt("OLEUTEST","simpdnd",0);
  94. }
  95. // needed for LRPC to work properly...
  96. SetMessageQueue(96);
  97. lpCSimpleApp = new CSimpleApp;
  98. if (!lpCSimpleApp)
  99. {
  100. /* memory allocation problem. We cannot carry on.
  101. */
  102. MessageBox(NULL, TEXT("Out of Memory"), TEXT("SimpDnD"),
  103. MB_SYSTEMMODAL | MB_ICONHAND);
  104. return(FALSE);
  105. }
  106. // we will add one ref count on our App. later when we want to destroy
  107. // the App object we will release this ref count. when the App's ref
  108. // count goes to 0, it will be deleted.
  109. lpCSimpleApp->AddRef();
  110. // process the command line
  111. if( (pszTemp = strstr(lpCmdLine, "-driver")) )
  112. {
  113. //we were launched by the test driver
  114. lpCSimpleApp->m_hDriverWnd = (HWND)strtoul(pszTemp+8, NULL, 10);
  115. }
  116. // check for the -empty flag (use empty enumerator)
  117. if( strstr(lpCmdLine, "-empty") )
  118. {
  119. gfUseEmptyEnumerator = TRUE;
  120. }
  121. // app initialization
  122. if (!hPrevInstance)
  123. if (!lpCSimpleApp->fInitApplication(hInstance))
  124. {
  125. lpCSimpleApp->Release();
  126. return (FALSE);
  127. }
  128. // instance initialization
  129. if (!lpCSimpleApp->fInitInstance(hInstance, nCmdShow))
  130. {
  131. lpCSimpleApp->Release();
  132. return (FALSE);
  133. }
  134. #if 0
  135. /* Initialization required for OLE 2 UI library. This call is
  136. ** needed ONLY if we are using the static link version of the UI
  137. ** library. If we are using the DLL version, we should NOT call
  138. ** this function in our application.
  139. */
  140. if (!OleUIInitialize(hInstance, hPrevInstance, TEXT(SZCLASSICONBOX),
  141. TEXT(SZCLASSRESULTIMAGE)))
  142. {
  143. OleDbgOut(TEXT("Could not initialize OLEUI library\n"));
  144. lpCSimpleApp->Release();
  145. return FALSE;
  146. }
  147. #endif
  148. // message loop
  149. while (GetMessage(&msg, NULL, NULL, NULL))
  150. if (!lpCSimpleApp->HandleAccelerators(&msg))
  151. {
  152. TranslateMessage(&msg); /* Translates virtual key codes */
  153. DispatchMessage(&msg); /* Dispatches message to window */
  154. }
  155. #if 0
  156. // De-initialization for UI libraries. Just like OleUIInitialize, this
  157. // funciton is needed ONLY if we are using the static link version of the
  158. // OLE UI library.
  159. OleUIUninitialize();
  160. #endif
  161. // Release the ref count added on the App above. this will make
  162. // the App's ref count go to 0, and the App object will be deleted.
  163. lpCSimpleApp->Release();
  164. return (msg.wParam); /* Returns the value from PostQuitMessage */
  165. }
  166. //**********************************************************************
  167. //
  168. // MainWndProc
  169. //
  170. // Purpose:
  171. //
  172. // Processes messages for the frame window
  173. //
  174. // Parameters:
  175. //
  176. // HWND hWnd - Window handle for frame window
  177. //
  178. // UINT message - Message value
  179. //
  180. // WPARAM wParam - Message info
  181. //
  182. // LPARAM lParam - Message info
  183. //
  184. // Return Value:
  185. //
  186. // LRESULT
  187. //
  188. // Function Calls:
  189. // Function Location
  190. //
  191. // CSimpleApp::lCommandHandler APP.CPP
  192. // CSimpleApp::DestroyDocs APP.CPP
  193. // CSimpleApp::lCreateDoc APP.CPP
  194. // CSimpleApp::lSizeHandler APP.CPP
  195. // CSimpleDoc::lAddVerbs DOC.CPP
  196. // PostQuitMessage Windows API
  197. // DefWindowProc Windows API
  198. // DestroyWindow Windows API
  199. //
  200. //
  201. //********************************************************************
  202. LRESULT FAR PASCAL EXPORT MainWndProc(HWND hWnd, UINT message,
  203. WPARAM wParam, LPARAM lParam)
  204. {
  205. switch (message)
  206. {
  207. case WM_TEST2:
  208. StartTest2(lpCSimpleApp);
  209. break;
  210. case WM_TEST1:
  211. StartTest1(lpCSimpleApp);
  212. break;
  213. case WM_CLOSE:
  214. DestroyWindow(lpCSimpleApp->m_hAppWnd);
  215. break;
  216. case WM_COMMAND: // message: command from application menu
  217. return lpCSimpleApp->lCommandHandler(hWnd, message, wParam,
  218. lParam);
  219. break;
  220. case WM_CREATE:
  221. #ifdef NOTREADY
  222. RemoveMenu(GetSubMenu(GetMenu(hWnd), 1), 0, MF_BYPOSITION);
  223. #endif // NOTREADY
  224. return lpCSimpleApp->lCreateDoc(hWnd, message, wParam, lParam);
  225. break;
  226. case WM_DESTROY: // message: window being destroyed
  227. lpCSimpleApp->DestroyDocs(); // need to destroy the doc...
  228. PostQuitMessage(0);
  229. break;
  230. case WM_INITMENUPOPUP:
  231. // is this the edit menu?
  232. if ( LOWORD(lParam) == 1)
  233. return lpCSimpleApp->m_lpDoc->lAddVerbs();
  234. break;
  235. case WM_SIZE:
  236. return lpCSimpleApp->lSizeHandler(hWnd, message, wParam, lParam);
  237. default: // Passes it on if unproccessed
  238. return (DefWindowProc(hWnd, message, wParam, lParam));
  239. }
  240. return (NULL);
  241. }
  242. //**********************************************************************
  243. //
  244. // About
  245. //
  246. // Purpose:
  247. //
  248. // Processes dialog box messages
  249. //
  250. // Parameters:
  251. //
  252. // HWND hWnd - Window handle for dialog box
  253. //
  254. // UINT message - Message value
  255. //
  256. // WPARAM wParam - Message info
  257. //
  258. // LPARAM lParam - Message info
  259. //
  260. // Return Value:
  261. //
  262. // Function Calls:
  263. // Function Location
  264. //
  265. // EndDialog Windows API
  266. //
  267. //
  268. //********************************************************************
  269. INT_PTR
  270. #ifdef WIN32
  271. CALLBACK
  272. #else
  273. FAR PASCAL EXPORT
  274. #endif
  275. About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
  276. {
  277. switch (message)
  278. {
  279. case WM_INITDIALOG: /* message: initialize dialog box */
  280. return (TRUE);
  281. case WM_COMMAND: /* message: received a command */
  282. if (wParam == IDOK /* "OK" box selected? */
  283. || wParam == IDCANCEL) /* System menu close command? */
  284. {
  285. EndDialog(hDlg, TRUE); /* Exits the dialog box */
  286. return (TRUE);
  287. }
  288. break;
  289. }
  290. return (FALSE); /* Didn't process a message */
  291. }
  292. //**********************************************************************
  293. //
  294. // DocWndProc
  295. //
  296. // Purpose:
  297. //
  298. // Processes dialog box messages
  299. //
  300. // Parameters:
  301. //
  302. // HWND hWnd - Window handle for doc window
  303. //
  304. // UINT message - Message value
  305. //
  306. // WPARAM wParam - Message info
  307. //
  308. // LPARAM lParam - Message info
  309. //
  310. // Return Value:
  311. //
  312. // long
  313. //
  314. // Function Calls:
  315. // Function Location
  316. //
  317. // CSimpleApp::PaintApp APP.CPP
  318. // BeginPaint Windows API
  319. // EndPaint Windows API
  320. // DefWindowProc Windows API
  321. // IOleObject::DoVerb Object
  322. // CSimpleSite::GetObjRect SITE.CPP
  323. // CSimpleDoc::QueryDrag DOC.CPP
  324. // CSimpleDoc::DoDragDrop DOC.CPP
  325. // SetTimer Windows API
  326. // KillTimer Windows API
  327. // SetCapture Windows API
  328. // ReleaseCapture Windows API
  329. //
  330. //
  331. //********************************************************************
  332. LRESULT FAR PASCAL EXPORT DocWndProc(HWND hWnd, UINT message, WPARAM wParam,
  333. LPARAM lParam)
  334. {
  335. HDC hDC;
  336. PAINTSTRUCT ps;
  337. switch (message)
  338. {
  339. case WM_PAINT:
  340. hDC = BeginPaint(hWnd, &ps);
  341. if (lpCSimpleApp)
  342. lpCSimpleApp->PaintApp (hDC);
  343. EndPaint(hWnd, &ps);
  344. break;
  345. case WM_LBUTTONDBLCLK:
  346. {
  347. POINT pt;
  348. pt.x = (int)(short)LOWORD (lParam );
  349. pt.y = (int)(short)HIWORD (lParam );
  350. if (lpCSimpleApp->m_lpDoc->m_lpSite &&
  351. lpCSimpleApp->m_lpDoc->m_lpSite->m_lpOleObject)
  352. {
  353. RECT rect;
  354. lpCSimpleApp->m_lpDoc->m_lpSite->GetObjRect(&rect);
  355. if ( PtInRect(&rect, pt) )
  356. {
  357. // Execute object's default verb
  358. lpCSimpleApp->m_lpDoc->m_lpSite->m_lpOleObject->DoVerb(
  359. OLEIVERB_PRIMARY, (LPMSG)&message,
  360. &lpCSimpleApp->m_lpDoc->m_lpSite->m_OleClientSite,
  361. -1, hWnd, &rect);
  362. }
  363. }
  364. break;
  365. }
  366. case WM_LBUTTONDOWN:
  367. {
  368. POINT pt;
  369. pt.x = (int)(short)LOWORD (lParam );
  370. pt.y = (int)(short)HIWORD (lParam );
  371. /* OLE2NOTE: check if this is a button down on the region
  372. ** that is a handle to start a drag operation. for us,
  373. ** this this is any where in the window. we
  374. ** do NOT want to start a drag immediately; we want to
  375. ** wait until the mouse moves a certain threshold. or a
  376. ** certain amount of time has elapsed. if
  377. ** LButtonUp comes before the drag is started, then
  378. ** the fPendingDrag state is cleared. we must capture
  379. ** the mouse to ensure the modal state is handled
  380. ** properly.
  381. */
  382. if (lpCSimpleApp->m_lpDoc->QueryDrag(pt) )
  383. {
  384. lpCSimpleApp->m_lpDoc->m_fPendingDrag = TRUE;
  385. lpCSimpleApp->m_lpDoc->m_ptButDown = pt;
  386. SetTimer(hWnd, 1, lpCSimpleApp->m_nDragDelay, NULL);
  387. SetCapture(hWnd);
  388. }
  389. break;
  390. }
  391. case WM_LBUTTONUP:
  392. if (lpCSimpleApp->m_lpDoc->m_fPendingDrag)
  393. {
  394. /* ButtonUP came BEFORE distance/time threshholds were
  395. ** exceeded. clear fPendingDrag state.
  396. */
  397. ReleaseCapture();
  398. KillTimer(hWnd, 1);
  399. lpCSimpleApp->m_lpDoc->m_fPendingDrag = FALSE;
  400. }
  401. break;
  402. case WM_MOUSEMOVE:
  403. {
  404. if (lpCSimpleApp->m_lpDoc->m_fPendingDrag)
  405. {
  406. int x = (int)(short)LOWORD (lParam );
  407. int y = (int)(short)HIWORD (lParam );
  408. POINT pt = lpCSimpleApp->m_lpDoc->m_ptButDown;
  409. int nDragMinDist = lpCSimpleApp->m_nDragMinDist;
  410. if (! ( ((pt.x - nDragMinDist) <= x)
  411. && (x <= (pt.x + nDragMinDist))
  412. && ((pt.y - nDragMinDist) <= y)
  413. && (y <= (pt.y + nDragMinDist)) ) )
  414. {
  415. // mouse moved beyond threshhold to start drag
  416. ReleaseCapture();
  417. KillTimer(hWnd, 1);
  418. lpCSimpleApp->m_lpDoc->m_fPendingDrag = FALSE;
  419. // perform the modal drag/drop operation.
  420. lpCSimpleApp->m_lpDoc->DoDragDrop( );
  421. }
  422. }
  423. break;
  424. }
  425. case WM_TIMER:
  426. {
  427. // drag time delay threshhold exceeded -- start drag
  428. ReleaseCapture();
  429. KillTimer(hWnd, 1);
  430. lpCSimpleApp->m_lpDoc->m_fPendingDrag = FALSE;
  431. // perform the modal drag/drop operation.
  432. lpCSimpleApp->m_lpDoc->DoDragDrop( );
  433. break;
  434. }
  435. default: /* Passes it on if unproccessed */
  436. return (DefWindowProc(hWnd, message, wParam, lParam));
  437. }
  438. return (NULL);
  439. }