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

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