home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / samples / sample7 / sample7.cpp < prev    next >
Text File  |  1997-03-28  |  11KB  |  361 lines

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