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.

453 lines
13 KiB

  1. //**********************************************************************
  2. // File name: simpsvr.cpp
  3. //
  4. // Main source file for the simple OLE 2.0 server
  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) 1993 Microsoft Corporation. All rights reserved.
  14. //**********************************************************************
  15. #include "pre.h"
  16. #include "obj.h"
  17. #include "app.h"
  18. #include "doc.h"
  19. #include "icf.h"
  20. #include <stdlib.h>
  21. #include <testmess.h>
  22. #define MEASUREITEMWIDTH 40
  23. #define MEASUREITEMHEIGHT 40
  24. // This line is needed for the debug utilities in OLE2UI
  25. extern "C" {
  26. OLEDBGDATA_MAIN(TEXT("SIMPSVR"))
  27. }
  28. BOOL fBeVerbose = FALSE;
  29. extern "C"
  30. void TestDebugOut(LPSTR psz)
  31. {
  32. if (fBeVerbose)
  33. {
  34. OutputDebugStringA(psz);
  35. }
  36. }
  37. CSimpSvrApp FAR * lpCSimpSvrApp;
  38. CClassFactory FAR * lpClassFactory;
  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
  60. //
  61. // Function Calls:
  62. // Function Location
  63. //
  64. // CSimpSvrApp::CSimpSvrApp APP.CPP
  65. // CSimpSvrApp::fInitApplication APP.CPP
  66. // CSimpSvrApp::fInitInstance APP.CPP
  67. // CSimpSvrApp::HandleAccelerators APP.CPP
  68. // CSimpSvrApp::~CSimpSvrApp APP.CPP
  69. // OleUIInitialize OLE2UI
  70. // OleUIUninitialize OLE2UI
  71. // GetMessage Windows API
  72. // TranslateMessage Windows API
  73. // DispatchMessage Windows API
  74. //
  75. //
  76. //********************************************************************
  77. int PASCAL WinMain
  78. #ifdef WIN32
  79. (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  80. #else
  81. (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  82. #endif
  83. {
  84. MSG msg;
  85. fBeVerbose = GetProfileInt("OLEUTEST","BeVerbose",0);
  86. if(fBeVerbose == 0)
  87. {
  88. fBeVerbose = GetProfileInt("OLEUTEST","simpsvr",0);
  89. }
  90. TestDebugOut(TEXT("Starting Simpsvr.....\n"));
  91. // recommended size for OLE apps
  92. SetMessageQueue(96);
  93. lpCSimpSvrApp = new CSimpSvrApp;
  94. if (!lpCSimpSvrApp)
  95. {
  96. /* memory allocation error. We cannot carry on.
  97. */
  98. MessageBox(NULL, TEXT("Out of Memory"), TEXT("SimpSvr"),
  99. MB_SYSTEMMODAL | MB_ICONHAND);
  100. return(FALSE);
  101. }
  102. lpCSimpSvrApp->AddRef(); // need the app ref. count at 1 to hold the
  103. // app alive.
  104. lpCSimpSvrApp->ParseCmdLine(lpCmdLine);
  105. // app initialization
  106. if (!hPrevInstance)
  107. if (!lpCSimpSvrApp->fInitApplication(hInstance))
  108. {
  109. // We need to release CSimpSvrApp before we go away
  110. lpCSimpSvrApp->Release();
  111. return (FALSE);
  112. }
  113. // instance initialization
  114. if (!lpCSimpSvrApp->fInitInstance(hInstance, nCmdShow, lpClassFactory))
  115. {
  116. // We need to release CSimpSvrApp before we go away
  117. lpCSimpSvrApp->Release();
  118. return (FALSE);
  119. }
  120. /* Initialization required for OLE 2 UI library. This call is
  121. ** needed ONLY if we are using the static link version of the UI
  122. ** library. If we are using the DLL version, we should NOT call
  123. ** this function in our application.
  124. ** The 3rd and 4th parameters passed the OleUIInitialize
  125. ** function are strings which are used to name two custom
  126. ** control classes used by the OLE2UI library. These strings
  127. ** must be unique for each application that uses the OLE2UI
  128. ** library. These strings are typically composed by combining
  129. ** the APPNAME with a suffix in order to be unique for a
  130. ** particular application. The special symbols "SZCLASSICONBOX"
  131. ** and "SZCLASSRESULTIMAGE" are used define these strings. These
  132. ** symbols must be defined in a header file that is included
  133. ** both in this file AND the resource file that included the
  134. ** "OLE2UI.RC" resoure file. These symbols should be used in the
  135. ** call to OleUIInitialize and are referenced in the INSOBJ.DLG
  136. ** and PASTESPL.DLG resouce files of the OLE2UI library.
  137. */
  138. if (!OleUIInitialize(hInstance, hPrevInstance, TEXT(SZCLASSICONBOX),
  139. TEXT(SZCLASSRESULTIMAGE)))
  140. {
  141. OleDbgOut(TEXT("Could not initialize OLEUI library\n"));
  142. // We need to release CSimpSvrApp before we go away
  143. lpCSimpSvrApp->Release();
  144. return FALSE;
  145. }
  146. // message loop
  147. while (GetMessage(&msg, NULL, NULL, NULL))
  148. {
  149. // Is this an accelerator for us? -- Remember that this server
  150. // only has one window and only processes one object at a time.
  151. if (TranslateAccelerator(lpCSimpSvrApp->GethAppWnd(),
  152. lpCSimpSvrApp->m_hAccel, &msg))
  153. {
  154. // Yes -- so we can loop for the next message
  155. continue;
  156. }
  157. if (lpCSimpSvrApp->IsInPlaceActive())
  158. {
  159. // Only key messages need to be sent to OleTranslateAccelerator.
  160. // Any other message would result in an extra FAR call to occur
  161. // for that message processing...
  162. if ((msg.message >= WM_KEYFIRST) && (msg.message <= WM_KEYLAST))
  163. {
  164. // OleTranslateAccelerator MUST be called, even though this
  165. // application does not have an accelerator table. This has
  166. // to be done in order for the mneumonics for the top level
  167. // menu items to work properly.
  168. if (OleTranslateAccelerator(
  169. lpCSimpSvrApp->GetDoc()->GetObj()->GetInPlaceFrame(),
  170. lpCSimpSvrApp->GetDoc()->GetObj()->GetFrameInfo(),
  171. &msg) == NOERROR)
  172. {
  173. continue;
  174. }
  175. }
  176. }
  177. TranslateMessage(&msg); /* Translates virtual key codes */
  178. DispatchMessage(&msg); /* Dispatches message to window */
  179. }
  180. // De-initialization for UI libraries. Just like OleUIInitialize, this
  181. // funciton is needed ONLY if we are using the static link version of the
  182. // OLE UI library.
  183. OleUIUninitialize();
  184. return (msg.wParam); /* Returns the value from PostQuitMessage */
  185. }
  186. //**********************************************************************
  187. //
  188. // MainWndProc
  189. //
  190. // Purpose:
  191. //
  192. // Processes messages for the main frame window
  193. //
  194. // Parameters:
  195. //
  196. // HWND hWnd - Window handle for frame window
  197. //
  198. // UINT message - Message value
  199. //
  200. // WPARAM wParam - Message info
  201. //
  202. // LPARAM lParam - Message info
  203. //
  204. // Return Value:
  205. //
  206. // long
  207. //
  208. // Function Calls:
  209. // Function Location
  210. //
  211. // CSimpSvrApp::lCommandHandler APP.CPP
  212. // CSimpSvrApp::DestroyDocs APP.CPP
  213. // CSimpSvrApp::lCreateDoc APP.CPP
  214. // CSimpSvrApp::lSizeHandler APP.CPP
  215. // CGameDoc::lAddVerbs DOC.CPP
  216. // PostQuitMessage Windows API
  217. // DefWindowProc Windows API
  218. //
  219. //
  220. //********************************************************************
  221. LRESULT FAR PASCAL EXPORT MainWndProc(HWND hWnd,UINT message,WPARAM wParam,
  222. LPARAM lParam)
  223. {
  224. switch (message)
  225. {
  226. case WM_CLOSE:
  227. TestDebugOut(TEXT("*** In WM_CLOSE *** \r\n"));
  228. // if there is still a document
  229. if (lpCSimpSvrApp->GetDoc())
  230. // if there is still an object within a document
  231. if (lpCSimpSvrApp->GetDoc()->GetObj())
  232. // this case occurs if there is still
  233. // an outstanding Ref count on the object
  234. // when the app is trying to go away.
  235. // typically this case will occur in
  236. // the "open" editing mode.
  237. //
  238. // Close the document
  239. lpCSimpSvrApp->GetDoc()->Close();
  240. // hide the app window
  241. lpCSimpSvrApp->HideAppWnd();
  242. // if we were started by ole, unregister the class factory,
  243. // otherwise remove the ref count on our dummy OLE object
  244. if (lpCSimpSvrApp->IsStartedByOle())
  245. {
  246. if (CoRevokeClassObject(lpCSimpSvrApp->GetRegisterClass())
  247. != S_OK)
  248. TestDebugOut(TEXT("Fail in CoRevokeClassObject\n"));
  249. }
  250. else
  251. lpCSimpSvrApp->GetOleObject()->Release();
  252. lpCSimpSvrApp->Release(); // This should close the app.
  253. break;
  254. case WM_COMMAND: // message: command from application menu
  255. return lpCSimpSvrApp->lCommandHandler(hWnd, message,
  256. wParam, lParam);
  257. break;
  258. case WM_CREATE:
  259. return lpCSimpSvrApp->lCreateDoc(hWnd, message, wParam, lParam);
  260. break;
  261. case WM_DESTROY: // message: window being destroyed
  262. PostQuitMessage(0);
  263. break;
  264. case WM_MEASUREITEM:
  265. ((LPMEASUREITEMSTRUCT)lParam)->itemWidth = MEASUREITEMWIDTH;
  266. ((LPMEASUREITEMSTRUCT)lParam)->itemHeight = MEASUREITEMHEIGHT;
  267. break;
  268. case WM_DRAWITEM:
  269. lpCSimpSvrApp->HandleDrawItem((LPDRAWITEMSTRUCT) lParam);
  270. break;
  271. case WM_SIZE:
  272. return lpCSimpSvrApp->lSizeHandler(hWnd, message, wParam, lParam);
  273. default: // Passes it on if unproccessed
  274. return (DefWindowProc(hWnd, message, wParam, lParam));
  275. }
  276. return (NULL);
  277. }
  278. //**********************************************************************
  279. //
  280. // About
  281. //
  282. // Purpose:
  283. //
  284. // Processes dialog box messages
  285. //
  286. // Parameters:
  287. //
  288. // HWND hWnd - Window handle for dialog box
  289. //
  290. // UINT message - Message value
  291. //
  292. // WPARAM wParam - Message info
  293. //
  294. // LPARAM lParam - Message info
  295. //
  296. // Return Value:
  297. //
  298. //
  299. // Function Calls:
  300. // Function Location
  301. //
  302. // EndDialog Windows API
  303. //
  304. //
  305. //********************************************************************
  306. INT_PTR
  307. #ifdef WIN32
  308. CALLBACK
  309. #else
  310. FAR PASCAL EXPORT
  311. #endif
  312. About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
  313. {
  314. switch (message)
  315. {
  316. case WM_INITDIALOG: /* message: initialize dialog box */
  317. return (TRUE);
  318. case WM_COMMAND: /* message: received a command */
  319. if (wParam == IDOK /* "OK" box selected? */
  320. || wParam == IDCANCEL) /* System menu close command? */
  321. {
  322. EndDialog(hDlg, TRUE); /* Exits the dialog box */
  323. return (TRUE);
  324. }
  325. break;
  326. }
  327. return (FALSE); /* Didn't process a message */
  328. }
  329. //**********************************************************************
  330. //
  331. // DocWndProc
  332. //
  333. // Purpose:
  334. //
  335. // Processes dialog box messages
  336. //
  337. // Parameters:
  338. //
  339. // HWND hWnd - Window handle for doc window
  340. //
  341. // UINT message - Message value
  342. //
  343. // WPARAM wParam - Message info
  344. //
  345. // LPARAM lParam - Message info
  346. //
  347. // Return Value:
  348. //
  349. // Function Calls:
  350. // Function Location
  351. //
  352. // CSimpSvrApp::PaintApp APP.CPP
  353. // BeginPaint Windows API
  354. // EndPaint Windows API
  355. // DefWindowProc Windows API
  356. // IOleObject::QueryInterface Object
  357. // IOleInPlaceObject::UIDeactivate Object
  358. // IOleObject::DoVerb Object
  359. // IOleInPlaceObject::Release Object
  360. //
  361. //
  362. //********************************************************************
  363. LRESULT FAR PASCAL EXPORT DocWndProc(HWND hWnd,UINT message,WPARAM wParam,
  364. LPARAM lParam)
  365. {
  366. HDC hDC;
  367. PAINTSTRUCT ps;
  368. switch (message)
  369. {
  370. case WM_COMMAND: // message: command from application menu
  371. return lpCSimpSvrApp->lCommandHandler(hWnd, message,
  372. wParam, lParam);
  373. break;
  374. case WM_PAINT:
  375. hDC = BeginPaint(hWnd, &ps);
  376. // tell the app class to paint itself
  377. if (lpCSimpSvrApp)
  378. lpCSimpSvrApp->PaintApp (hDC);
  379. EndPaint(hWnd, &ps);
  380. break;
  381. case WM_MENUSELECT:
  382. lpCSimpSvrApp->SetStatusText();
  383. break;
  384. case WM_MEASUREITEM:
  385. ((LPMEASUREITEMSTRUCT)lParam)->itemWidth = MEASUREITEMWIDTH;
  386. ((LPMEASUREITEMSTRUCT)lParam)->itemHeight = MEASUREITEMHEIGHT;
  387. break;
  388. case WM_DRAWITEM:
  389. lpCSimpSvrApp->HandleDrawItem((LPDRAWITEMSTRUCT) lParam);
  390. break;
  391. default: /* Passes it on if unproccessed */
  392. return (DefWindowProc(hWnd, message, wParam, lParam));
  393. }
  394. return (NULL);
  395. }