home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / samples / sample7 / sample7.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-15  |  11.8 KB  |  393 lines

  1. #include "sample7.h"
  2.  
  3.  
  4. #include <stdlib.h>
  5.  
  6. #define COMMAND_PRINT 200
  7.  
  8.  
  9. /* Sample 7 shows how to draw objects and to print       */
  10. /* For drawing there are two methods avaible                 */
  11. /* 1: Using one graphic device                           */
  12. /*      The most simple way is to use one graphic device   */
  13. /*      in this case you only need to create the objects   */
  14. /*      to draw in this device, every time it has to be    */
  15. /*      redrawn (when the method Draw() of the owning       */
  16. /*      window is called) you call the method Draw() of      */
  17. /*      the device.                                          */
  18. /*   2:   Using two graphic devices                           */
  19. /*      If you have a litle bit more object you should use   */
  20. /*      this method because it works faster. Create one    */
  21. /*      device in memory (the second parameter of the       */
  22. /*      constructor must be TRUE) and create the objects   */
  23. /*      to draw on this device. Only call Draw() if you    */
  24. /*      have inserted/removed/edited one or more objcts.    */
  25. /*      Create a second graphic device associated to the   */
  26. /*      owning window. If the method Draw() of the owning  */
  27. /*      window is called, copy the content of the memory-   */
  28. /*      device to the window-associated device with Copy().*/
  29. /*   This sample show you how to use the second method.      */
  30. MyAppWindow :: MyAppWindow(): XFrameWindow( 100, "Sample7 - Graphic Functions", XFrameWindow::defaultStyle | FRM_TASKLIST )
  31. {
  32.    //create a toolbar
  33.    XToolBar * t = new XToolBar(this, TB_LEFT | TB_RIPABLE, 0, 0, 10);
  34.  
  35.    //create a user defined button
  36.    XRect rectB( 0, 0, 46, 46);
  37.    XUserButton * back = new XUserButton( t, rectB, COMMAND_PRINT, WIN_VISIBLE);
  38.  
  39.    //create a bitmap
  40.    XBitmap bitmap( back);
  41.    bitmap.Load( "printer.bmp");
  42.    XRect re(0, 0, 42, 42);
  43.  
  44.    //set the size of the bitmap to show
  45.    bitmap.SetOutputSize( &re );
  46.  
  47.    // add the bitmap to the button
  48.    back->SetBitmap( &bitmap, 0);
  49.  
  50.    //add the button to the toolbar
  51.    t->AddWindow( back, FALSE, TRUE);
  52.  
  53.    //create a graphic device associated to the window
  54.    graphWindow = new XGraphicDevice( this, FALSE, TRUE, PU_PELS, GPIA_ASSOC);//, PU_PELS);
  55.  
  56.    //create a graphic device which resides in memory
  57.    graphMemory = new XGraphicDevice( this, TRUE, TRUE, PU_PELS, GPIA_ASSOC | GPIT_MICRO); //
  58.  
  59.    //now create some objects to show
  60.    XColor colWhite( COL_WHITE);
  61.    graphMemory->SetBackgroundColor( &colWhite );
  62.  
  63.    XPoint p1(100,100);
  64.    XPoint p2(500, 500);
  65.  
  66.    XLine * line = new XLine( &p1, &p2);
  67.    line->SetLineType( LINETYPE_DASHDOT );
  68.    XColor colRed( COL_RED);
  69.    line->SetColor( &colRed);
  70.    line->SetLineWidth( 2 );
  71.    layer.AddObject(line);
  72.  
  73.    p1.Set(400, 300);
  74.    XCircle * ci = new XCircle( &p1, 100, TRUE, 200, -40, 50);
  75.    XColor colBlue( COL_BLUE);
  76.    ci->SetColor( &colBlue);
  77.    layer.AddObject(ci);
  78.  
  79.    p1.Set(105, 105);
  80.    XArc * a = new XArc( &p1, 100, 0, 70, TRUE, TRUE);
  81.    a->SetColor( &colBlue);
  82.    layer.AddObject(a);
  83.  
  84.    XColor colYellow(COL_YELLOW);
  85.    XArc * b = new XArc( &p1, 100, 70, 100, TRUE, TRUE);
  86.    b->SetColor( &colYellow);
  87.    layer.AddObject(b);
  88.  
  89.    XArc * c = new XArc( &p1, 100, 170, 170, TRUE, TRUE);
  90.    XColor colGreen(COL_GREEN);
  91.    layer.AddObject(c);
  92.  
  93.    c->SetColor( &colGreen);
  94.    XArc * d = new XArc( &p1, 100, 340, 20, TRUE, TRUE);
  95.    d->SetColor( &colRed);
  96.    layer.AddObject(d);
  97.  
  98.    p1.Set(200,200);
  99.    p2.Set(300,100);
  100.    XBox * box = new XBox( &p1, &p2, FALSE);
  101.    XColor colPaleGray(COL_PALEGRAY);
  102.    box->SetColor( &colPaleGray);
  103.    box->SetLineWidth( 40 );
  104.    box->SetLineEnd( LINEEND_FLAT );
  105.    box->SetLineType( LINETYPE_DASHDOT );
  106.    layer.AddObject(box);
  107.  
  108.    p1.Set(200,200);
  109.    XMarker * m1 = new XMarker( &p1);
  110.    layer.AddObject(m1);
  111.    XSize size;
  112.    m1->GetSize( & size );
  113.    p1.Set(200- size.GetWidth()/2, 200+ size.GetHeight()/2);
  114.    m1->Move( &p1);
  115.    p1.Set( 300 + size.GetWidth()/2, 200 + size.GetHeight()/2);
  116.    XMarker * m2 = new XMarker( &p1);
  117.    p1.Set( 200 - size.GetWidth()/2, 100 - size.GetHeight()/2);
  118.    layer.AddObject(m2);
  119.    XMarker * m3 = new XMarker( &p1);
  120.    layer.AddObject(m3);
  121.    p1.Set( 300 + size.GetWidth()/2, 100 - size.GetHeight()/2);
  122.    XMarker * m4 = new XMarker( &p1);
  123.    layer.AddObject(m4);
  124.  
  125.    p1.Set(300,100);
  126.    p2.Set(400,0);
  127.  
  128.    XBox * box2 = new XBox( &p1, &p2, TRUE);
  129.    layer.AddObject(box2);
  130.  
  131.    p1.Set(320, 80);
  132.    p2.Set(380, 20);
  133.    XBox * box3 = new XBox( &p1, &p2, TRUE);
  134.    layer.AddObject(box3);
  135.    box3->SetColor( &colWhite);
  136.  
  137.    XLine * line2 = new XLine( &p1, &p2);
  138.    XColor colBlack(COL_BLACK);
  139.    line2->SetColor( &colBlack);
  140.    line2->SetLineWidth( 2 );
  141.    layer.AddObject(line2);
  142.  
  143.    p1.SetY(20);
  144.    p2.SetY(80);
  145.    XLine * line3 = new XLine( &p1, &p2);
  146.    line3->SetColor( &colBlack);
  147.    line3->SetLineWidth( 2 );
  148.    layer.AddObject(line3);
  149.  
  150.    XFont * font = new XFont( graphMemory, "Tms Rmn Bold Italic", 14 );
  151.    if(font->GetPixelSize() < 4)
  152.    {
  153.       //the font seems not to be installed, choose a default font
  154.       delete font;
  155.       font = new XFont( graphMemory, "Helvetica", 10 );
  156.    }
  157.    XRect rect2( 200, 250, 400, 450);
  158.    XText * text = new XText( font, &rect2, "Text with the font \"Tms Rmn Bold Italic\". If you have not installed the font, youl∩ll see this with a default font.", DT_WORDBREAK);
  159.    text->SetColor( &colRed);
  160.    layer.AddObject(text);
  161.  
  162.    XRect rect3( 100,100,350, 250);
  163.    XFont * font2 = new XFont( graphMemory, "Helv Bold", 12 );
  164.    if(font2->GetPixelSize() < 4)
  165.    {
  166.       //the font seems not to be installed, choose a default font
  167.       delete font2;
  168.       font2 = new XFont( graphMemory, "Helvetica", 12 );
  169.    }
  170.    XText * text2 = new XText( font2, &rect3, "Text with the font \"Helv Bold\". If you have not installed the font, youl∩ll see this with a default font.", DT_WORDBREAK);
  171. //   text2->SetShear( 2, 3);
  172.    layer.AddObject(text2);
  173.  
  174.    //create a bitmap
  175.    XPoint rp(50, 300);
  176.    XBitmap * bmp = new XBitmap( &rp);
  177.  
  178.    //load the bitmap from a file
  179.    bmp->Load( "sample.bmp" );
  180.    layer.AddObject(bmp);
  181.  
  182.    //draw the objects ONLY ONE TIME
  183.    graphMemory->FillBackground();
  184.    layer.Draw(graphMemory);
  185.  
  186.    XRect rect( 150, 150, 500, 500);
  187.    SetSize(&rect);
  188.  
  189.    Activate();
  190. }
  191.  
  192.  
  193. void MyAppWindow :: Draw( void )
  194. {
  195.    //the content of the window has to be drawn
  196.    if(graphWindow && graphMemory)
  197.    {
  198.       //query the size of the rect to copy
  199.       XRect rect;
  200.       GetUpdateRect( &rect);
  201.  
  202.       //copy the content of the memory-device to the window-device
  203.       //without any modifications
  204.       XRect rect2 = rect;
  205.  
  206.       //make sure the update-rect will not be stretched
  207.       rect2.SetWidth(0);
  208.       graphWindow->Copy( graphMemory, &rect, &rect2);
  209.    }
  210. }
  211.  
  212.  
  213. BOOL MyAppWindow :: DoCommand(LONG command)
  214. {
  215.    //the toolbar button has been pressed, get ready to print
  216.    if(command == COMMAND_PRINT)
  217.    {
  218.       XPrinterDevice printer(this);
  219.       XString queue, fileName;
  220.  
  221.       //setup the printer
  222.       if( printer.SetupPrinter("Print", this, &queue, &fileName) == FALSE)
  223.          return TRUE;
  224.  
  225.       if(fileName.GetLength())
  226.       {
  227.          XMessageBox("cannot print into file, aborting");
  228.          return TRUE;
  229.       } /* end if */
  230.       //open a printer-job
  231.       if( printer.OpenPrinterJob( "Test Job") == FALSE)
  232.       {
  233.          XMessageBox( "error, cannot create printer-job" );
  234.          return TRUE;
  235.       } /* end if */
  236.  
  237.       XSize size;
  238.       LONG cx, cy;
  239.  
  240.       //query the size of the used sheet
  241.       printer.GetPageSize( &size );
  242.  
  243.       //create a bitmap to draw
  244.       XPoint rp( 300, 1500);
  245.       XBitmap * bmp = new XBitmap( &rp);
  246.       bmp->Load( "sample.bmp" );
  247.  
  248.       //draw the bitmap bigger
  249.       XSize s;
  250.       bmp->GetDimensions( &s );
  251.       cx = s.GetWidth() * 2;
  252.       cy = s.GetHeight() * 2;
  253.       XRect newRect(0,0,cx,cy);
  254.       bmp->SetOutputSize(&newRect);
  255.  
  256.       //set the bitmap centered
  257.       rp.SetY( size.GetHeight() - cy - 200);
  258.       rp.SetX( size.GetWidth() / 2 - cx /2 );
  259.       bmp->Move( &rp );
  260.       bmp->Draw(&printer);
  261.  
  262.       //create a table
  263.       XPoint p1, p2;
  264.       p1.Set(100, rp.GetY() - 300);
  265.       p2.Set( size.GetWidth() - 100, p1.GetY());
  266.       XLine * baseLine = new XLine( &p1, &p2);
  267.       baseLine->SetLineWidth( LINEWIDTH_THICK );
  268.       baseLine->Draw(&printer);
  269.  
  270.       p1.SetX( 400 );
  271.       p1.SetY( p1.GetY() + 50 );
  272.       p2.SetX( 400 );
  273.       p2.SetY( rp.GetY() - 1300 );
  274.       XLine * line1 = new XLine( &p1, &p2);
  275.       line1->Draw(&printer);
  276.  
  277.       p1.SetX( 800 );
  278.       p2.SetX( 800 );
  279.       XLine * line2 = new XLine( &p1, &p2);
  280.       line2->Draw(&printer);
  281.  
  282.       p1.Set(100, rp.GetY() - 1300);
  283.       p2.Set( size.GetWidth() - 100, rp.GetY() - 1300);
  284.       XLine * lineBase = new XLine( &p1, &p2);
  285.       lineBase->SetLineType( LINETYPE_LONGDASH );
  286.       lineBase->Draw(&printer);
  287.  
  288.       p1.Set( 500, rp.GetY() - 100);
  289.       XFont * fontBakerBook = new XFont( &printer, "BakerBook", 14 );
  290.       if(fontBakerBook->GetPixelSize() < 4)
  291.       {
  292.          delete fontBakerBook;
  293.          fontBakerBook = new XFont( &printer, "Helvetica", 14 );
  294.       }
  295.       XText * titleText = new XText( fontBakerBook, &p1, "Printer sample of the Open Object Library");
  296.       titleText->Draw(&printer);
  297.  
  298.       //Set labels for the table
  299.       XFont * fontLabel = new XFont( &printer, "Helv", 12 );
  300.       p1.Set(100, rp.GetY() - 290);
  301.  
  302.       XText * text1 = new XText( fontLabel, &p1, "Column 1");
  303.       text1->SetShear( 2, 3);
  304.       text1->Draw(&printer);
  305.  
  306.       p1.SetX(420);
  307.       XText * text2 = new XText( fontLabel, &p1, "Column 2");
  308.       text2->SetShear( 2, 3);
  309.       text2->Draw(&printer);
  310.  
  311.       p1.SetX(820);
  312.       XText * text3 = new XText( fontLabel, &p1, "Column 3");
  313.       text3->SetShear( 2, 3);
  314.       text3->Draw(&printer);
  315.  
  316.       //print some rows
  317.       XFont * fontText = new XFont( &printer, "Times New Roman", 10 );
  318.  
  319.       LONG yPos = rp.GetY() - 300 - fontText->GetPixelSize() - 5;
  320.       LONG lineCounter = 1;
  321.       XString text;
  322.       while( yPos > rp.GetY() - 1300)
  323.       {
  324.          p1.Set( 100, yPos );
  325.          text = "Row ";
  326.          text += lineCounter;
  327.          text += " column 1";
  328.          XText line1( fontText, &p1, text);
  329.          line1.Draw(&printer);
  330.  
  331.          p1.SetX( 420);
  332.          text = "Row ";
  333.          text += lineCounter;
  334.          text += " column 2";
  335.          XText line2( fontText, &p1, text);
  336.          line2.Draw(&printer);
  337.  
  338.          p1.SetX( 820 );
  339.          text = "Row ";
  340.          text += lineCounter;
  341.          text += ", this line is a little bit longer";
  342.          XText line3( fontText, &p1, text);
  343.          line3.Draw(&printer);
  344.          yPos-= (fontText->GetPixelSize() - 3);
  345.          lineCounter ++;
  346.       }
  347.  
  348.       //print a business-chart
  349.       p1.Set( size.GetWidth() / 2, 400);
  350.       XArc a(&p1, 300, 0, 70, TRUE, TRUE);
  351.       a.SetPattern(8);
  352.       a.Draw(&printer);
  353.  
  354.       XArc b(&p1, 300, 70, 100, TRUE, TRUE);
  355.       b.SetPattern(9);
  356.  
  357.       XArc c(&p1, 300, 170, 170, TRUE, TRUE);
  358.       c.SetPattern(12);
  359.       c.Draw(&printer);
  360.  
  361.       XArc d(&p1, 300, 340, 20, TRUE, TRUE);
  362.       d.SetPattern(7);
  363.       d.Draw(&printer);
  364.  
  365.       //close the job
  366.       printer.ClosePrinterJob( );
  367.    }
  368.    return TRUE;
  369. }
  370.  
  371.  
  372. BOOL MyAppWindow :: QueryForClose()
  373. {
  374.    XApplication::GetApplication()->Terminate();      //make shure all windows will be closed
  375.                                                    //nessacary if the user had ripped the toolbar
  376.    return TRUE;
  377. }
  378.  
  379.  
  380. void main ( void)
  381. {
  382.    try
  383.    {
  384.       MyAppWindow * window = new MyAppWindow();   //create new framewindow (see above)
  385.       XApplication::GetApplication()->Start();  //let the application work
  386.    }
  387.    catch( XException e)
  388.    {
  389.       e.ShowError();   //shit
  390.       exit(-1);
  391.    }
  392. }
  393.