home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / STEP18.PAK / CNTRL18.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.0 KB  |  170 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1994 by Borland International
  3. //   Tutorial application -- cntrl18.cpp
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/edit.h>
  9. #include <strstrea.h>
  10.  
  11. #include <ocf/automacr.h>
  12. #include <iostream.h>
  13. #include <stdlib.h>
  14. #include <conio.h>
  15. #include <math.h>
  16.  
  17. #include "step18.cxx"
  18.  
  19. class TMyApp : public TApplication {
  20.   public:
  21.     TMyApp(): TApplication("Cntrl18") {}
  22.     void InitMainWindow();
  23.     void InitInstance();
  24.     void Output(ostrstream &ostr);
  25.  
  26.     bool DispServer; //True if automation server should be displayed when run
  27.  
  28.   private:
  29.     void TestAutomation(); //Routine to execute step18
  30.     TEdit *Disp; //Client window
  31. };
  32.  
  33. void
  34. TMyApp::InitMainWindow()
  35. {
  36.   Disp=new TEdit(0,0,"",0,0,0,0,0,true);
  37.   SetMainWindow(new TFrameWindow(0,"Step 18 Controller",Disp));
  38. }
  39.  
  40. //
  41. // Outputs ostrstream buf to client window
  42. //
  43. void
  44. TMyApp::Output(ostrstream &ostr)
  45. {
  46.   char *str = ostr.str(); //Get stream buffer as char*
  47.   ostr.seekp(0);  //Reset stream
  48.   Disp->Insert(str);
  49.   Disp->Insert("\r\n");
  50. }
  51.  
  52. void
  53. TMyApp::InitInstance()
  54. {
  55.   TApplication::InitInstance();
  56.   // Our windows are now created and we can output the test results
  57.   TestAutomation();
  58. }
  59.  
  60.  
  61. long Colors[] = {
  62.   RGB(0x00, 0x00, 0x00),
  63.   RGB(0xFF, 0x00, 0x00),
  64.   RGB(0x00, 0xFF, 0x00),
  65.   RGB(0x00, 0x00, 0xFF),
  66.   RGB(0xFF, 0xFF, 0x00),
  67.   RGB(0x00, 0xFF, 0xFF),
  68.   RGB(0xFF, 0x00, 0xFF)
  69. };
  70.  
  71. const int NumColors = sizeof(Colors)/sizeof(Colors[0]);
  72.  
  73. //
  74. // Excercises the server using the classes generated by AUTOGEN.EXE
  75. //
  76. void
  77. TMyApp::TestAutomation()
  78. {
  79.   char buf[256];
  80.   ostrstream ostr(buf, sizeof buf);
  81.  
  82.   try {
  83.     TOleAllocator allocator(0);      // OLE requirement
  84.     TDrawApp app;
  85.     TDrawDoc doc;
  86.  
  87.     app.Bind("DrawPad.Application.18");
  88.  
  89.     ostr << "Application name =" << (const char far*)app.GetName() << ends;
  90.     Output(ostr);
  91.     ostr << "Application path =" << (const char far*)app.GetFullName() << ends;
  92.     Output(ostr);
  93.  
  94.     app.NewDocument(doc);
  95.  
  96.     // if there's one argument on command line, don't display the frame window.
  97.     if (DispServer) {
  98.       app.SetVisible(true);
  99.     }
  100.     else {
  101.       app.SetVisible(false);
  102.     }
  103.     ostr << "Current automated pen size: " << doc.GetPenSize() << ends;
  104.     Output(ostr);
  105.  
  106.     ostr << "Current automated pen color: " << doc.GetPenColor() << ends;
  107.     Output(ostr);
  108.  
  109.     double startX   = 150;
  110.     double startY   = 150;
  111.     double radius   = 0;
  112.     double theta    = 0;
  113.     int colorIndex  = 0;
  114.     short penSize   = 1; //Server expects a short arg in SetPenSize
  115.     double x        = 0;
  116.     double y        = 0;
  117.  
  118.     ostr << "Drawing in automated app ..."<<ends;
  119.     Output(ostr);
  120.  
  121.     doc.AddPoint(startX, startY);
  122.     for (int i=0; radius < 70; i++) {
  123.       if (i % 10 == 0) {
  124.         // use the next pen
  125.         doc.AddLine();
  126.         doc.AddPoint(startX+x, startY-y);
  127.         doc.SetPenColor(Colors[colorIndex++]);
  128.         doc.SetPenSize(penSize++);
  129.         colorIndex %= NumColors;
  130.       }
  131.       radius += 0.2 * theta;
  132.       x = 2 * radius * cos(theta);
  133.       y = 2 * radius * sin(theta);
  134.       doc.AddPoint(startX+x, startY-y);
  135.       theta += 0.2;
  136.     }
  137.     doc.AddLine();
  138.  
  139.     // End of server automation test
  140.     ::MessageBox(0, "End of Program", "CNTRL18.EXE", MB_OK);
  141.     ostr << "Shutting server down..." << ends;
  142.     Output(ostr);
  143.     app.Quit();
  144.  
  145.     ostr << "Server shut down, test complete"<<ends;
  146.     Output(ostr);
  147.  
  148.   }
  149.   catch (TXBase& x) {
  150.     ostr << x.why() << ends;
  151.     Output(ostr);
  152.   }
  153.  
  154. }
  155.  
  156. int
  157. OwlMain(int argc, char* /*argv*/ [])
  158. {
  159.   TMyApp app;
  160.  
  161.   // Pass a dummy arg on command line to make the server invisible
  162.   if (argc == 2)
  163.     app.DispServer=false;
  164.   else
  165.     app.DispServer=true;
  166.  
  167.   return app.Run();
  168. }
  169.  
  170.