home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / step15.pak / STEP15.CPP next >
C/C++ Source or Header  |  1997-07-23  |  5KB  |  172 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1994 by Borland International
  3. //   Tutorial application -- step15.cpp
  4. //   Embed/Linking Server example
  5. //----------------------------------------------------------------------------
  6. #include <owl/owlpch.h>
  7. #include <owl/applicat.h>
  8. #include <owl/dialog.h>
  9. #include <owl/controlb.h>
  10. #include <owl/buttonga.h>
  11. #include <owl/statusba.h>
  12. #include <owl/docmanag.h>
  13. #include <owl/olemdifr.h>
  14. #include <owl/oleview.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "step15.rc"
  18.  
  19. DEFINE_APP_DICTIONARY(AppDictionary);
  20. static TPointer<TOcRegistrar> Registrar;
  21.  
  22. // Registration
  23. //
  24. REGISTRATION_FORMAT_BUFFER(100)
  25.  
  26. BEGIN_REGISTRATION(AppReg)
  27.   REGDATA(clsid,      "{5E4BD320-8ABC-101B-A23B-CE4E85D07ED2}")
  28.   REGDATA(appname,    "DrawPad Server")
  29. END_REGISTRATION
  30.  
  31. //
  32. // TDrawApp
  33. //
  34. class _USERCLASS TDrawApp : public TApplication, public TOcModule {
  35.   public:
  36.     TDrawApp() :
  37.       TApplication(::AppReg["appname"], ::Module, &::AppDictionary) {}
  38.  
  39.   protected:
  40.     TMDIClient*      Client;
  41.  
  42.     // Override methods of TApplication
  43.     void InitInstance();
  44.     void InitMainWindow();
  45.  
  46.     // Event handlers
  47.     void EvNewView(TView& view);
  48.     void EvCloseView(TView& view);
  49.     void EvDropFiles(TDropInfo dropInfo);
  50.     void CmAbout();
  51.  
  52.   DECLARE_RESPONSE_TABLE(TDrawApp);
  53. };
  54.  
  55. DEFINE_RESPONSE_TABLE1(TDrawApp, TApplication)
  56.   EV_OWLVIEW(dnCreate, EvNewView),
  57.   EV_OWLVIEW(dnClose,  EvCloseView),
  58.   EV_WM_DROPFILES,
  59.   EV_COMMAND(CM_ABOUT, CmAbout),
  60. END_RESPONSE_TABLE;
  61.  
  62. void
  63. TDrawApp::InitMainWindow()
  64. {
  65.   TOleMDIFrame* frame;
  66.   frame = new TOleMDIFrame(GetName(), 0, *(Client = new TMDIClient(this)), true, this);
  67.  
  68.   // Construct a status bar
  69.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed);
  70.  
  71.   // Construct a control bar
  72.   TControlBar* cb = new TControlBar(frame);
  73.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW, TButtonGadget::Command));
  74.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN, TButtonGadget::Command));
  75.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE, TButtonGadget::Command));
  76.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS, TButtonGadget::Command));
  77.   cb->Insert(*new TSeparatorGadget);
  78.   cb->Insert(*new TButtonGadget(CM_PENSIZE, CM_PENSIZE, TButtonGadget::Command));
  79.   cb->Insert(*new TButtonGadget(CM_PENCOLOR, CM_PENCOLOR, TButtonGadget::Command));
  80.   cb->Insert(*new TSeparatorGadget);
  81.   cb->Insert(*new TButtonGadget(CM_ABOUT, CM_ABOUT, TButtonGadget::Command));
  82.   cb->SetHintMode(TGadgetWindow::EnterHints);
  83.  
  84.   cb->Attr.Id = IDW_TOOLBAR;
  85.  
  86.   // Insert the status bar and control bar into the frame
  87.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  88.   frame->Insert(*cb, TDecoratedFrame::Top);
  89.  
  90.   // Set the main window and its menu
  91.   SetMainWindow(frame);
  92.   GetMainWindow()->SetMenuDescr(TMenuDescr(IDM_MDICMNDS));
  93.  
  94.   // Install the document manager
  95.   SetDocManager(new TDocManager(dmMDI, this));
  96. }
  97.  
  98. void
  99. TDrawApp::InitInstance()
  100. {
  101.   TApplication::InitInstance();
  102.   GetMainWindow()->DragAcceptFiles(true);
  103. }
  104.  
  105. void
  106. TDrawApp::EvDropFiles(TDropInfo dropInfo)
  107. {
  108.   int fileCount = dropInfo.DragQueryFileCount();
  109.   for (int index = 0; index < fileCount; index++) {
  110.     int fileLength = dropInfo.DragQueryFileNameLen(index)+1;
  111.     char* filePath = new char [fileLength];
  112.     dropInfo.DragQueryFile(index, filePath, fileLength);
  113.     TDocTemplate* tpl = GetDocManager()->MatchTemplate(filePath);
  114.     GetDocManager()->CreateDoc(tpl, filePath);
  115.     delete filePath;
  116.   }
  117.   dropInfo.DragFinish();
  118. }
  119.  
  120. void
  121. TDrawApp::EvNewView(TView& view)
  122. {
  123.   TOleView* ov = TYPESAFE_DOWNCAST(&view, TOleView);
  124.   if (view.GetDocument().IsEmbedded() && ov && !ov->IsOpenEditing()) {
  125.     TWindow* vw = view.GetWindow();
  126.     vw->SetParent(TYPESAFE_DOWNCAST(GetMainWindow(), TOleFrame)->GetRemViewBucket());
  127.     vw->Create();
  128.   }
  129.   else {
  130.     TMDIChild* child = new TMDIChild(*Client, 0);
  131.     if (view.GetViewMenu())
  132.       child->SetMenuDescr(*view.GetViewMenu());
  133.     child->Create();
  134.     child->SetClientWindow(view.GetWindow());
  135.   }
  136.  
  137.   if (!ov || !ov->GetOcRemView())
  138.     OcApp->SetOption(amEmbedding, false);
  139. }
  140.  
  141. void
  142. TDrawApp::EvCloseView(TView& /*view*/)
  143. {
  144.   // nothing needs to be done here for MDI
  145. }
  146.  
  147. void
  148. TDrawApp::CmAbout()
  149. {
  150.   TDialog(&TWindow(::GetFocus(), this), IDD_ABOUT).Execute();
  151. }
  152.  
  153. int
  154. OwlMain(int /*argc*/, char* /*argv*/ [])
  155. {
  156.   try {
  157.     Registrar = new TOcRegistrar(AppReg, TOleDocViewFactory<TDrawApp>(),
  158.                                  TApplication::GetCmdLine(), ::DocTemplateStaticHead);
  159.     if (Registrar->IsOptionSet(amAnyRegOption))
  160.       return 0;
  161.  
  162.     // If this is an exe server normal run, run the app now. Otherwise, wait
  163.     // until our factory gets a call (don't call us, we'll call you)
  164.     //
  165.     return Registrar->Run();
  166.   }
  167.   catch (xmsg& x) {
  168.     ::MessageBox(0, x.why().c_str(), "Exception", MB_OK);
  169.   }
  170.   return -1;
  171. }
  172.