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

  1. // ---------------------------------------------------------------------------
  2. // Copyright (C) 1994 Borland International
  3. // CppOcf1.cpp
  4. // ---------------------------------------------------------------------------
  5. #include <ocf/ocfpch.h>
  6. #include <ocf/ocapp.h>
  7. #include <windowsx.h>
  8. #include <ocf/ocdoc.h>
  9. #include <ocf/ocview.h>
  10. #include <ocf/ocpart.h>
  11. #include "ocfevx.h"
  12. #include "CppOcf1.h"
  13.  
  14. //
  15. // OCF variables that are needed per window
  16. //
  17. TOcRegistrar* OcRegistrar = 0;
  18. TOcApp*       OcApp       = 0;
  19. TOcDocument*  OcDoc       = 0;
  20. TOcView*      OcView      = 0;
  21.  
  22. bool Inserted = false;
  23. HWND HwndView = 0;
  24. TRegLink* RegLinkHead = 0;  // initialize reglink list
  25.  
  26. //
  27. // registration information
  28. //
  29. REGISTRATION_FORMAT_BUFFER(100)
  30.  
  31. BEGIN_REGISTRATION(AppReg)
  32.   REGDATA(clsid,    "{8646DB80-94E5-101B-B01F-00608CC04F66}")
  33. END_REGISTRATION
  34.  
  35. //
  36. // Starting point of all Windows programs
  37. //
  38. int PASCAL
  39. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  40.         char far* lpCmdLine, int nCmdShow)
  41. {
  42.   try {
  43.     TOleAllocator allocator(0);  // Required for OLE2
  44.     MSG msg;
  45.  
  46.     // Initialize OCF objects
  47.     //
  48.     OcRegistrar = new TOcRegistrar(::AppReg, 0, string(lpCmdLine),
  49.                                    ::RegLinkHead, hInstance);
  50.     OcRegistrar->CreateOcApp(OcRegistrar->GetOptions(), OcApp);
  51.  
  52.     // Any previous instance?
  53.     if (!hPrevInstance)
  54.       // Initialize app-specific stuff
  55.       if (!InitApplication(hInstance))
  56.         return 0;
  57.  
  58.     // Instance-specific
  59.     if (!InitInstance(hInstance, nCmdShow))
  60.       return 0;
  61.  
  62.     // Standard Windows message loop
  63.     while (GetMessage(&msg, 0, 0, 0)) {
  64.       TranslateMessage(&msg);
  65.       DispatchMessage(&msg);
  66.     }
  67.  
  68.   }
  69.   catch (TXBase& xbase) {
  70.     MessageBox(GetFocus(), xbase.why().c_str(), "Exception caught", MB_OK);
  71.   }
  72.  
  73.   // free the OCF object
  74.   //
  75.   delete OcRegistrar;
  76.   return 0;
  77. }
  78.  
  79.  
  80. //
  81. // Initialize application-specific stuff and register the class
  82. //
  83. bool
  84. InitApplication(HINSTANCE hInstance)
  85. {
  86.   WNDCLASS wc;
  87.   WNDCLASS wcView;
  88.  
  89.   wc.style          = 0;
  90.   wc.lpfnWndProc    = (WNDPROC) MainWndProc;
  91.   wc.cbClsExtra     = 0;
  92.   wc.cbWndExtra     = 0;
  93.   wc.hInstance      = hInstance;
  94.   wc.hIcon          = LoadIcon(0, IDI_APPLICATION);
  95.   wc.hCursor        = LoadCursor(0, IDC_ARROW);
  96.   wc.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  97.   wc.lpszMenuName   = MENUNAME;
  98.   wc.lpszClassName  = CLASSNAME;
  99.  
  100.   RegisterClass(&wc);
  101.  
  102.   wcView.style          = 0;
  103.   wcView.lpfnWndProc    = (WNDPROC) ViewWndProc;
  104.   wcView.cbClsExtra     = 0;
  105.   wcView.cbWndExtra     = 0;
  106.   wcView.hInstance      = hInstance;
  107.   wcView.hIcon          = LoadIcon(0, IDI_APPLICATION);
  108.   wcView.hCursor        = LoadCursor(0, IDC_ARROW);
  109.   wcView.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  110.   wcView.lpszMenuName   = 0;
  111.   wcView.lpszClassName  = VIEWCLASSNAME;
  112.  
  113.   RegisterClass(&wcView);
  114.  
  115.   return true;
  116. }
  117.  
  118. //
  119. // Initialize instance and display the main window
  120. //
  121. bool
  122. InitInstance(HINSTANCE hInstance, int nCmdShow)
  123. {
  124.   HWND hwnd;
  125.  
  126.   hwnd = CreateWindow(CLASSNAME, TITLE,
  127.     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
  128.     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  129.     0, 0, hInstance, 0);
  130.  
  131.   if (!hwnd)
  132.     return false;
  133.   ShowWindow(hwnd, nCmdShow);
  134.   UpdateWindow(hwnd);
  135.   return true;
  136. }
  137.  
  138.  
  139. //
  140. // Standard message-handler routine for main window
  141. //
  142. long CALLBACK _export
  143. MainWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  144. {
  145.   switch (message) {
  146.     HANDLE_MSG(hwnd, WM_CREATE,   MainWnd_OnCreate);
  147.     HANDLE_MSG(hwnd, WM_CLOSE,    MainWnd_OnClose);
  148.     HANDLE_MSG(hwnd, WM_DESTROY,  MainWnd_OnDestroy);
  149.     HANDLE_MSG(hwnd, WM_COMMAND,  MainWnd_OnCommand);
  150.     HANDLE_MSG(hwnd, WM_SIZE,     MainWnd_OnSize);
  151.  
  152.     // handle the OCF to application messages
  153.     //
  154.     HANDLE_MSG(hwnd, WM_OCEVENT,  MainWnd_OnOcEvent);
  155.   }
  156.   return DefWindowProc(hwnd, message, wParam, lParam);
  157. }
  158.  
  159. bool
  160. MainWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  161. {
  162.   if (OcApp)
  163.     OcApp->SetupWindow(hwnd);
  164.  
  165.   HwndView = CreateWindow(VIEWCLASSNAME, "",
  166.     WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE | WS_BORDER,
  167.     10, 10, 300, 300,
  168.     hwnd, (HMENU)1, _hInstance, 0);
  169.  
  170.   return true;
  171. }
  172.  
  173. void
  174. MainWnd_OnClose(HWND hwnd)
  175. {
  176.   if (IsWindow(HwndView))
  177.     DestroyWindow(HwndView);
  178.   DestroyWindow(hwnd);
  179. }
  180.  
  181. void
  182. MainWnd_OnDestroy(HWND /*hwnd*/)
  183. {
  184.  
  185.   PostQuitMessage(0);
  186. }
  187.  
  188. void
  189. MainWnd_OnCommand(HWND hwnd, int id, HWND /*hwndCtl*/, uint /*codeNotify*/)
  190. {
  191.   switch (id) {
  192.     case CM_INSERTOBJECT: {
  193.       try {
  194.         TOcInitInfo initInfo(OcView);
  195.  
  196.         if (OcApp->Browse(initInfo)) {
  197.           TRect rect(30, 30, 100, 100);
  198.           new TOcPart(*OcDoc, initInfo, rect);
  199.           Inserted = true;
  200.         }
  201.       }
  202.       catch (TXBase& xbase) {
  203.         MessageBox(GetFocus(), xbase.why().c_str(), "Exception caught", MB_OK);
  204.       }
  205.       break;
  206.     }
  207.     case CM_EXIT: {
  208.       PostMessage(hwnd, WM_CLOSE, 0, 0);
  209.       break;
  210.     }
  211.   }
  212. }
  213.  
  214. void
  215. MainWnd_OnSize(HWND hwnd, UINT /*state*/, int /*cx*/, int /*cy*/)
  216. {
  217.   if (IsWindow(HwndView)) {
  218.     TRect rect;
  219.     GetClientRect(hwnd, &rect);
  220.     MoveWindow(HwndView, rect.left, rect.top, rect.right, rect.bottom, true);
  221.   }
  222. }
  223.  
  224. bool
  225. MainWnd_OnOcAppFrameRect(HWND hwnd, TRect far* rect)
  226. {
  227.   GetClientRect(hwnd, rect);
  228.   return true;
  229. }
  230.  
  231. //
  232. // Subdispatch OC_... messages
  233. //
  234. long
  235. MainWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM lParam)
  236. {
  237.   switch (wParam) {
  238.     HANDLE_OCF(hwnd, OC_APPFRAMERECT, MainWnd_OnOcAppFrameRect);
  239.   }
  240.   return false;
  241. }
  242.  
  243. //
  244. // view window
  245. //
  246. bool
  247. ViewWnd_OnCreate(HWND hwnd, CREATESTRUCT FAR* /*lpCreateStruct*/)
  248. {
  249.   OcDoc  = new TOcDocument(*OcApp);
  250.   OcView = new TOcView(*OcDoc);
  251.  
  252.   if (OcView)
  253.     OcView->SetupWindow(hwnd);
  254.  
  255.   return true;
  256. }
  257.  
  258. void
  259. ViewWnd_OnClose(HWND hwnd)
  260. {
  261.   PostMessage(GetParent(hwnd), WM_CLOSE, 0, 0);
  262.   DestroyWindow(hwnd);
  263. }
  264.  
  265. void
  266. ViewWnd_OnDestroy(HWND /*hwnd*/)
  267. {
  268.   for (TOcPartCollectionIter i(OcDoc->GetParts()); i; i++) {
  269.     TOcPart& p = *i.Current();
  270.     p.Activate(false);
  271.   }
  272.  
  273.   if (OcView)
  274.     OcView->ReleaseObject();
  275.  
  276.   OcDoc->Close();
  277.  
  278.   if (OcApp)
  279.     OcApp->Release();
  280.  
  281.   delete OcDoc;
  282. }
  283.  
  284. void
  285. ViewWnd_OnPaint(HWND hwnd)
  286. {
  287.   PAINTSTRUCT ps;
  288.   HDC dc = BeginPaint(hwnd, &ps);
  289.  
  290.   RECT rect;
  291.   GetClientRect(hwnd, &rect);
  292.  
  293.   TRect clientRect(rect);
  294.   TRect logicalRect = clientRect;
  295.   DPtoLP(dc, (POINT*)&logicalRect, 2);
  296.  
  297.   for (TOcPartCollectionIter i(OcDoc->GetParts()); i; i++) {
  298.     TOcPart& p = *i.Current();
  299.     if (p.IsVisible(logicalRect)) {
  300.       TRect r = p.GetRect();
  301.       p.Draw(dc, r, clientRect, asDefault);
  302.     }
  303.   }
  304.  
  305.   EndPaint(hwnd, &ps);
  306. }
  307.  
  308. void
  309. ViewWnd_OnSize(HWND /*hwnd*/, UINT /*state*/, int /*cx*/, int /*cy*/)
  310. {
  311.   if (OcView) {
  312.     OcView->EvResize();
  313.   }
  314. }
  315.  
  316. //
  317. // Standard message-handler routine for view window
  318. //
  319. long CALLBACK _export
  320. ViewWndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
  321. {
  322.   switch (message) {
  323.     HANDLE_MSG(hwnd, WM_CREATE,   ViewWnd_OnCreate);
  324.     HANDLE_MSG(hwnd, WM_CLOSE,    ViewWnd_OnClose);
  325.     HANDLE_MSG(hwnd, WM_DESTROY,  ViewWnd_OnDestroy);
  326.     HANDLE_MSG(hwnd, WM_PAINT,    ViewWnd_OnPaint);
  327.  
  328.     // handle the OCF to application messages
  329.     //
  330.     HANDLE_MSG(hwnd, WM_OCEVENT,  ViewWnd_OnOcEvent);
  331.   }
  332.   return DefWindowProc(hwnd, message, wParam, lParam);
  333. }
  334.  
  335.  
  336. const char*
  337. ViewWnd_OnOcViewTitle(HWND /*hwnd*/)
  338. {
  339.   return APPSTRING;
  340. }
  341.  
  342.  
  343. bool
  344. ViewWnd_OnOcViewClose(HWND /*hwnd*/)
  345. {
  346.   if (OcDoc)
  347.     OcDoc->Close();
  348.   return true;
  349. }
  350.  
  351. //
  352. // Subdispatch OC_... messages
  353. //
  354. long
  355. ViewWnd_OnOcEvent(HWND hwnd, WPARAM wParam, LPARAM /*lParam*/)
  356. {
  357.   switch (wParam) {
  358.     HANDLE_OCF(hwnd, OC_VIEWCLOSE,  ViewWnd_OnOcViewClose);
  359.     HANDLE_OCF(hwnd, OC_VIEWTITLE,  ViewWnd_OnOcViewTitle);
  360.   }
  361.   return false;
  362. }
  363.  
  364.  
  365.