home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / TUTORIAL.PAK / CPPOCF2.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  10KB  |  411 lines

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // CppOcf2.cpp
  4. // ---------------------------------------------------------------------------
  5. #include <ocf/ocfpch.h>
  6. #include <ocf/ocapp.h>
  7. #include <windowsx.h>
  8. #include <ocf/ocreg.h>
  9. #include <ocf/ocdoc.h>
  10. #include <ocf/ocview.h>
  11. #include <ocf/ocremvie.h>
  12. #include <ocf/ocpart.h>
  13. #include "ocfevx.h"
  14. #include "CppOcf2.h"
  15.  
  16. //
  17. //
  18. //
  19. TOcRegistrar* OcRegistrar = 0;
  20. TOcApp*       OcApp       = 0;
  21.  
  22. TOcDocument*  OcDoc       = 0;
  23. TOcRemView*   OcRemView   = 0;
  24.  
  25. bool Inserted = false;
  26. HWND HwndView = 0;
  27. HWND HwndMain = 0;
  28. TRegLink* RegLinkHead = 0;
  29.  
  30. //
  31. // registration information
  32. //
  33. REGISTRATION_FORMAT_BUFFER(100)
  34.  
  35. BEGIN_REGISTRATION(AppReg)
  36.   REGDATA(clsid,    "{BD5E4A81-A4EF-101B-B31B-0694B5E75735}")
  37.   REGDATA(appname,  "Count Server")
  38. END_REGISTRATION
  39.  
  40. BEGIN_REGISTRATION(DocReg)
  41.   REGDATA(description,  "Count Server Document")
  42.   REGDATA(progid,       APPSTRING".Document.2")
  43.   REGDATA(appname,      "Count")
  44. //  REGDATA(debugger,     "tdw")
  45. //  REGDATA(debugprogid,  APPSTRING".Debug.2")
  46. //  REGDATA(debugdesc,    "Count Server (debug) Document")
  47.   REGDATA(menuname,     "CountDocument")
  48.   REGDATA(insertable,   "")
  49.   REGDATA(extension,    "scd")
  50.   REGDATA(docfilter,    "*.scd")
  51.   REGDATA(verb0,        "&Edit")
  52.   REGDATA(verb1,        "&Open")
  53.   REGFORMAT(0, ocrEmbedSource,  ocrContent, ocrIStorage, ocrGet)
  54.   REGFORMAT(1, ocrMetafilePict, ocrContent, ocrMfPict,   ocrGet)
  55.   REGFORMAT(2, ocrBitmap,       ocrContent,  ocrGDI|ocrStaticMed, ocrGet)
  56.   REGFORMAT(3, ocrDib,          ocrContent,  ocrHGlobal|ocrStaticMed, ocrGet)
  57.   REGFORMAT(4, ocrLinkSource,   ocrContent,  ocrIStream, ocrGet)
  58. END_REGISTRATION
  59.  
  60. TRegLink DocLink(DocReg, RegLinkHead);
  61.  
  62. //
  63. //
  64. //
  65. HWND CreateViewWindow(HWND hwndParent)
  66. {
  67.   RECT rect;
  68.  
  69.   GetClientRect(hwndParent, &rect);
  70.   HWND hwnd = CreateWindow(VIEWCLASSNAME, "",
  71.     WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE | WS_BORDER,
  72.     rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
  73.     hwndParent, (HMENU)1, _hInstance, 0);
  74.   return hwnd;
  75. }
  76.  
  77. IUnknown*
  78. ComponentFactory(IUnknown* outer, uint32 options, uint32 id)
  79. {
  80.   IUnknown* ifc = 0;
  81.  
  82.   if (!OcApp) {
  83.     if (options & amShutdown)  // no app to shutdown!
  84.       return 0;
  85.     OcRegistrar->CreateOcApp(options, OcApp);
  86.     if (IsWindow(HwndMain))
  87.       OcApp->SetupWindow(HwndMain);
  88.   }
  89.   else if (options & amShutdown) {
  90.     DestroyWindow(HwndMain);
  91.     return 0;
  92.   }
  93.  
  94.   if (id == 0)
  95.     OcApp->SetOuter(outer);
  96.  
  97.   if (options & amRun) {
  98.     if ((options & amEmbedding) == 0) {
  99.       HwndView = CreateViewWindow(HwndMain);
  100.     }
  101.     MSG msg;
  102.     // Standard Windows message loop
  103.     while (GetMessage(&msg, 0, 0, 0)) {
  104.       TranslateMessage(&msg);
  105.       DispatchMessage(&msg);
  106.     }
  107.   }
  108.  
  109.   if (id) {
  110.     OcDoc     = new TOcDocument(*OcApp);
  111.     HwndView  = CreateViewWindow(HwndMain);
  112.     OcRemView = new TOcRemView(*OcDoc, &DocReg);
  113.     if (IsWindow(HwndView))
  114.       OcRemView->SetupWindow(HwndView);
  115.     ifc = OcRemView->SetOuter(outer);
  116.   }
  117.   return ifc;
  118. }
  119.  
  120. //
  121. // Starting point of all Windows programs
  122. //
  123. int PASCAL
  124. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  125.         char far* lpCmdLine, int nCmdShow)
  126. {
  127.   try {
  128.     TOleAllocator allocator(0); //required for OLE2
  129.     string cmdLine(lpCmdLine);
  130.  
  131.     OcRegistrar = new TOcRegistrar(::AppReg, ComponentFactory, cmdLine,
  132.                                    ::RegLinkHead, hInstance);
  133.  
  134.     // Any previous instance?
  135.     if (!hPrevInstance)
  136.       // Initialize app-specific stuff
  137.       if (!InitApplication(hInstance))
  138.         return 0;
  139.  
  140.     // Instance-specific
  141.     if (OcRegistrar->IsOptionSet(amEmbedding) ||
  142.       OcRegistrar->IsOptionSet(amAnyRegOption))
  143.       nCmdShow = SW_HIDE;
  144.     if (!InitInstance(hInstance, nCmdShow))
  145.       return 0;
  146.  
  147.     if (!OcRegistrar->IsOptionSet(amAnyRegOption))
  148.       OcRegistrar->Run();
  149.     delete OcRegistrar;
  150.   }
  151.   catch (xmsg& x) {
  152.     MessageBox(GetFocus(), x.why().c_str(), "Exception caught", MB_OK);
  153.   }
  154.   return 0;
  155. }
  156.  
  157.  
  158. //
  159. // Initialize application-specific stuff and register the class
  160. //
  161. bool
  162. InitApplication(HINSTANCE hInstance)
  163. {
  164.   WNDCLASS wc;
  165.   WNDCLASS wcView;
  166.  
  167.   wc.style          = 0;
  168.   wc.lpfnWndProc    = (WNDPROC) MainWndProc;
  169.   wc.cbClsExtra     = 0;
  170.   wc.cbWndExtra     = 0;
  171.   wc.hInstance      = hInstance;
  172.   wc.hIcon          = LoadIcon(0, IDI_APPLICATION);
  173.   wc.hCursor        = LoadCursor(0, IDC_ARROW);
  174.   wc.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  175.   wc.lpszMenuName   = 0;
  176.   wc.lpszClassName  = CLASSNAME;
  177.  
  178.   RegisterClass(&wc);
  179.  
  180.   wcView.style          = 0;
  181.   wcView.lpfnWndProc    = (WNDPROC) ViewWndProc;
  182.   wcView.cbClsExtra     = 0;
  183.   wcView.cbWndExtra     = 0;
  184.   wcView.hInstance      = hInstance;
  185.   wcView.hIcon          = LoadIcon(0, IDI_APPLICATION);
  186.   wcView.hCursor        = LoadCursor(0, IDC_ARROW);
  187.   wcView.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  188.   wcView.lpszMenuName   = 0;
  189.   wcView.lpszClassName  = VIEWCLASSNAME;
  190.  
  191.   RegisterClass(&wcView);
  192.  
  193.   return true;
  194. }
  195.  
  196.  
  197. //
  198. // Initialize instance and display the main window
  199. //
  200. bool
  201. InitInstance(HINSTANCE hInstance, int nCmdShow)
  202. {
  203.   HWND hwnd;
  204.  
  205.   hwnd = CreateWindow(CLASSNAME, TITLE,
  206.     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  207.     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  208.     0, 0, hInstance, 0);
  209.  
  210.   if (!hwnd)
  211.     return false;
  212.   ShowWindow(hwnd, nCmdShow);
  213.   UpdateWindow(hwnd);
  214.   return true;
  215. }
  216.  
  217.  
  218. //
  219. // Standard message-handler routine for main window
  220. //
  221. long CALLBACK _export
  222. MainWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  223. {
  224.   switch (message) {
  225.     HANDLE_MSG(hwnd, WM_CREATE,   MainWnd_OnCreate);
  226.     HANDLE_MSG(hwnd, WM_CLOSE,    MainWnd_OnClose);
  227.     HANDLE_MSG(hwnd, WM_DESTROY,  MainWnd_OnDestroy);
  228.     HANDLE_MSG(hwnd, WM_COMMAND,  MainWnd_OnCommand);
  229.     HANDLE_MSG(hwnd, WM_SIZE,     MainWnd_OnSize);
  230.  
  231.     // handle the OCF to application messages
  232.     //
  233.     HANDLE_MSG(hwnd, WM_OCEVENT,  MainWnd_OnOcEvent);
  234.   }
  235.   return DefWindowProc(hwnd, message, wParam, lParam);
  236. }
  237.  
  238. bool
  239. MainWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  240. {
  241.   if (OcApp)
  242.     OcApp->SetupWindow(hwnd);
  243.  
  244.   HwndMain = hwnd;
  245.   return true;
  246. }
  247.  
  248. void
  249. MainWnd_OnClose(HWND hwnd)
  250. {
  251.   if (IsWindow(HwndView))
  252.     DestroyWindow(HwndView);
  253.   DestroyWindow(hwnd);
  254. }
  255.  
  256. void
  257. MainWnd_OnDestroy(HWND /*hwnd*/)
  258. {
  259.   PostQuitMessage(0);
  260. }
  261.  
  262. void
  263. MainWnd_OnCommand(HWND /*hwnd*/, int id, HWND /*hwndCtl*/, uint /*codeNotify*/)
  264. {
  265.   switch (id) {
  266.   }
  267. }
  268.  
  269. void
  270. MainWnd_OnSize(HWND hwnd, UINT /*state*/, int /*cx*/, int /*cy*/)
  271. {
  272.   if (IsWindow(HwndView)) {
  273.     TRect rect;
  274.     GetClientRect(hwnd, &rect);
  275.     MoveWindow(HwndView, rect.left, rect.top, rect.right, rect.bottom, true);
  276.   }
  277. }
  278.  
  279. //
  280. // Subdispatch OC_... messages
  281. //
  282. long
  283. MainWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM /*lParam*/)
  284. {
  285.   switch (wParam) {
  286.     HANDLE_OCF(hwnd, OC_VIEWTITLE,        MainWnd_OnOcViewTitle);
  287.     HANDLE_OCF(hwnd, OC_APPSHUTDOWN,      MainWnd_OnOcAppShutDown);
  288.   }
  289.   return false;
  290. }
  291.  
  292. const char*
  293. MainWnd_OnOcViewTitle(HWND /*hwnd*/)
  294. {
  295.   return APPSTRING;
  296. }
  297.  
  298. bool
  299. MainWnd_OnOcAppShutDown(HWND hwnd)
  300. {
  301.   PostMessage(hwnd, WM_CLOSE, 0, 0);
  302.   return true;
  303. }
  304.  
  305.  
  306. static uint TimerId = 0;
  307.  
  308. bool
  309. ViewWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  310. {
  311.   TimerId = SetTimer(hwnd, 1, 1000, 0);
  312.   return true;
  313. }
  314.  
  315. void
  316. ViewWnd_OnClose(HWND hwnd)
  317. {
  318.   DestroyWindow(hwnd);
  319. }
  320.  
  321. void
  322. ViewWnd_OnDestroy(HWND hwnd)
  323. {
  324.   KillTimer(hwnd, TimerId);
  325.   if (OcDoc) {
  326.     OcDoc->Close();
  327.     delete OcDoc;
  328.     OcDoc = 0;
  329.   }
  330.   if (IsWindow(HwndMain))
  331.     PostMessage(HwndMain, WM_CLOSE, 0, 0);
  332.   HwndView = 0;
  333. }
  334.  
  335. char Buffer[80];
  336. uint Counter = 0;
  337.  
  338. void
  339. ViewWnd_OnPaint(HWND hwnd)
  340. {
  341.   PAINTSTRUCT ps;
  342.   HDC dc = BeginPaint(hwnd, &ps);
  343.  
  344.   RECT rect;
  345.   GetClientRect(hwnd, &rect);
  346.  
  347.   TRect clientRect(rect);
  348.   TRect logicalRect = clientRect;
  349.   DPtoLP(dc, (POINT*)&logicalRect, 2);
  350.  
  351.   wsprintf(Buffer, "%u", Counter);
  352.   TextOut(dc, 0, 0, Buffer, lstrlen(Buffer));
  353.  
  354.   EndPaint(hwnd, &ps);
  355. }
  356.  
  357. void
  358. ViewWnd_OnSize(HWND /*hwnd*/, UINT /*state*/, int /*cx*/, int /*cy*/)
  359. {
  360. }
  361.  
  362. void
  363. ViewWnd_OnTimer(HWND hwnd, UINT /*id*/)
  364. {
  365.   Counter++;
  366.   InvalidateRect(hwnd, 0, true);
  367. }
  368.  
  369.  
  370. //
  371. // Standard message-handler routine for view window
  372. //
  373. long CALLBACK _export
  374. ViewWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  375. {
  376.   switch (message) {
  377.     HANDLE_MSG(hwnd, WM_CREATE,   ViewWnd_OnCreate);
  378.     HANDLE_MSG(hwnd, WM_CLOSE,    ViewWnd_OnClose);
  379.     HANDLE_MSG(hwnd, WM_DESTROY,  ViewWnd_OnDestroy);
  380.     HANDLE_MSG(hwnd, WM_PAINT,    ViewWnd_OnPaint);
  381.     HANDLE_MSG(hwnd, WM_TIMER,    ViewWnd_OnTimer);
  382.  
  383.     // handle the OCF to application messages
  384.     //
  385.     HANDLE_MSG(hwnd, WM_OCEVENT,  ViewWnd_OnOcEvent);
  386.   }
  387.   return DefWindowProc(hwnd, message, wParam, lParam);
  388. }
  389.  
  390.  
  391. //
  392. // Subdispatch OC_... messages
  393. //
  394. long
  395. ViewWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM /*lParam*/)
  396. {
  397.   switch (wParam) {
  398.     HANDLE_OCF(hwnd, OC_VIEWCLOSE, ViewWnd_OnOcViewClose);
  399.   }
  400.   return false;
  401. }
  402.  
  403. bool
  404. ViewWnd_OnOcViewClose(HWND /*hwnd*/)
  405. {
  406.   if (OcDoc)
  407.     OcDoc->Close();
  408.   return true;
  409. }
  410.  
  411.