home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / samples / sample6 / sample6.cpp < prev    next >
Text File  |  1997-04-07  |  13KB  |  487 lines

  1. #include "sample6.h"
  2.  
  3. #include XColor_i
  4. #include XMessageBox_i
  5. #include XString_i
  6. #include XMenuBar_i
  7. #include XControlEvent_i
  8. #include XValueSet_i
  9. #include XStaticText_i
  10. #include XSlider_i
  11. #include XSpinButton_i
  12. #include XCircularSlider_i
  13. #include XStaticFrame_i
  14. #include XGroupBox_i
  15. #include XListBox_i
  16. #include XComboBox_i
  17. #include XItemDrawEvent_i
  18. #include XItemDrawHandler_i
  19. #include XExtendedMessageBox_i
  20. #include XPushButton_i
  21. #include XNoteBook_i
  22. #include XNoteBookPage_i
  23. #include XIcon_i
  24. #include XException_i
  25.  
  26. #include <stdlib.h>
  27.  
  28. MyApp * app;
  29.  
  30. class MyFrame: public XStaticFrame
  31. {
  32.    public:
  33.    MyFrame( XWindow*w, XRect*r): XStaticFrame(w, r){ ;}
  34.    void Draw( void );
  35. };
  36.  
  37.  
  38. class MenuDrawHandler: public XItemDrawHandler
  39. {
  40.       XBitmap * red, *green, *blue;
  41.    public:
  42.       MenuDrawHandler( XWindow * w);
  43.       BOOL HandleEvent( XItemDrawEvent *);
  44. };
  45.  
  46.  
  47. BOOL MenuDrawHandler :: HandleEvent( XItemDrawEvent * event)
  48. {
  49.    switch( event->GetItemID() )
  50.       {
  51.          case IDM_RED:
  52.             event->DrawItem( red, "Red");
  53.             break;
  54.          case IDM_GREEN:
  55.             event->DrawItem( green, "Green");
  56.             break;
  57.          case IDM_BLUE:
  58.             event->DrawItem( blue, "Blue");
  59.             break;
  60.          default:
  61.             return FALSE;
  62.       }
  63.    return TRUE;
  64. }
  65.  
  66.  
  67. MenuDrawHandler :: MenuDrawHandler( XWindow * w):XItemDrawHandler(w, 80, 25)
  68. {
  69.    red = new XBitmap(w);
  70.    red->Load( "red.bmp" );
  71.    green = new XBitmap(w);
  72.    green->Load( "green.bmp" );
  73.    blue = new XBitmap(w);
  74.    blue->Load( "blue.bmp" );
  75. }
  76.  
  77.  
  78. class ListDrawHandler: public XItemDrawHandler
  79. {
  80.       XBitmap * bmp[3];
  81.    public:
  82.       ListDrawHandler( XWindow * w);
  83.       BOOL HandleEvent( XItemDrawEvent *);
  84. };
  85.  
  86.  
  87. ListDrawHandler :: ListDrawHandler( XWindow * w):XItemDrawHandler(w, 100, 20)
  88. {
  89.    bmp[0] = new XBitmap(w);
  90.    bmp[1] = new XBitmap(w);
  91.    bmp[2] = new XBitmap(w);
  92.    bmp[0]->Load( "red.bmp" );
  93.    bmp[1]->Load( "green.bmp" );
  94.    bmp[2]->Load( "blue.bmp" );
  95. }
  96.  
  97.  
  98. BOOL ListDrawHandler :: HandleEvent( XItemDrawEvent * event)
  99. {
  100.    switch( event->GetItemID())
  101.       {
  102.          case 0:
  103.             {
  104.                XColor col( COL_RED);
  105.                event->SetTextColor( &col);
  106.                event->DrawItem( bmp[0], "Red");
  107.             }
  108.             break;
  109.          case 1:
  110.             {
  111.                XColor col( COL_GREEN);
  112.                event->SetTextColor( &col);
  113.                event->DrawItem( bmp[1], "Green");
  114.             }
  115.             break;
  116.          case 2:
  117.             {
  118.                XColor col( COL_BLUE);
  119.                event->SetTextColor( &col );
  120.                event->DrawItem( bmp[2], "Blue");
  121.             }
  122.             break;
  123.          default:
  124.             return FALSE;
  125.       }
  126.  
  127.    return TRUE;
  128. }
  129.  
  130.  
  131. class MyPage: public XNoteBookPage
  132. {
  133.    public:
  134.       BOOL DoCommand(LONG);
  135.       void DoControl(XControlEvent*);
  136.       MyPage( XNoteBook * b):XNoteBookPage(b){ ;}
  137. };
  138.  
  139.  
  140. //here a window is created out of the resource-file, the last parameter tells the library
  141. //to build the window with the ID given in XResource. Works with resource-DLLs too.
  142. MyAppWindow :: MyAppWindow( XApplication * app, XResource * r ): XFrameWindow( r, "Sample6 - extended controls", defaultDialogStyle | FRM_TITLEBAR | FRM_TASKLIST | FRM_CENTER, NULL, NULL, FALSE, TRUE)
  143. {
  144.    XColor col( COL_PALEGRAY);
  145.    XRect rec(100, 100, 450, 400);
  146.    SetSize(&rec);
  147.    XMenuBar * bar;
  148.  
  149.    try   
  150.    {
  151.       bar = new XMenuBar( this, r);
  152.    }
  153.    catch( XException& e)
  154.    {
  155.       XMessageBox( e.GetErrorMessage());
  156.       exit(-1);
  157.    }
  158.  
  159.    MenuDrawHandler * hdl = new MenuDrawHandler( bar );
  160.  
  161.    SetBackgroundColor( &col);
  162.  
  163.    bar->CheckItem( IDM_RED);
  164.  
  165.     EnableWindowUpdate(FALSE);
  166.  
  167.    XRect rect( 20, 20, 400, 300);
  168.     //enable new notebook-style too
  169.    XNoteBook * notes = new XNoteBook( this, &rect, 0, NB_TABBEDDIALOG|WIN_VISIBLE|NB_SOLIDBIND|NB_BACKPAGESBR|NB_SQUARETABS|NB_TABTEXTCENTER|NB_STATUSTEXTLEFT, "8.Helv");
  170.    notes->SetMajorTabSize( 135, 25);
  171.    notes->SetMajorBackgroundColor( &col);
  172.    notes->SetBackgroundColor( &col);
  173.  
  174.    MyPage * page1 = new MyPage( notes);
  175.    page1->SetText( "XValueSet" );
  176.  
  177.    //construct some controls
  178.  
  179.    //XValueSet
  180.    XRect rect2( 20, 10, 170, 170);
  181.    XValueSet * vSet = new XValueSet( page1, &rect2, ID_VALUESET, VA_TEXT|WIN_VISIBLE|VA_BORDER|VA_ITEMBORDER);
  182.  
  183.    vSet->SetDimensions( 3, 3 );
  184.     XSize size(10, 10);
  185.    vSet->SetItemSpacing( &size );
  186.     size.SetWidth(40);
  187.     size.SetHeight(40);
  188.    vSet->SetItemSize( &size );
  189.  
  190.    XRect rect3( 20, 190, 140, 20);
  191.    XStaticText * text = new XStaticText( page1, "XValueSet", &rect3, WIN_VISIBLE, ID_TEXT);
  192.  
  193.    //fill the first column with text
  194.    vSet->SetItem( 0, 0, "0:0" );
  195.    vSet->SetItem( 1, 0, "1:0" );
  196.    vSet->SetItem( 2, 0, "2:0" );
  197.  
  198.    //fill the second column with icons
  199.    vSet->SetItemAttribute( 0, 1, VA_ICON );
  200.    vSet->SetItemAttribute( 1, 1, VA_ICON );
  201.    vSet->SetItemAttribute( 2, 1, VA_ICON );
  202.  
  203.    XIcon * icon1, * icon2, * icon3;
  204.    icon1 = new XIcon();
  205.    icon1->Load( "paint.ico");
  206.  
  207.    icon2 = new XIcon();
  208.    icon2->Load( "lib.ico");
  209.  
  210.    icon3 = new XIcon();
  211.    icon3->Load( "mond.ico");
  212.  
  213.    vSet->SetItem( 0, 1, icon1);
  214.    vSet->SetItem( 1, 1, icon2);
  215.    vSet->SetItem( 2, 1, icon3);
  216.  
  217.  
  218.    //fill the last column with colors
  219.    vSet->SetItemAttribute( 0, 2, VA_COLOR );
  220.    vSet->SetItemAttribute( 1, 2, VA_COLOR );
  221.    vSet->SetItemAttribute( 2, 2, VA_COLOR );
  222.  
  223.    XColor col1( COL_RED );
  224.    vSet->SetItem(0, 2, &col1);
  225.    XColor col2( COL_GREEN);
  226.    vSet->SetItem(1, 2, &col2);
  227.    XColor col3( COL_BLUE );
  228.    vSet->SetItem(2, 2, &col3);
  229.  
  230.    MyPage * page2 = new MyPage( notes);
  231.    page2->SetTop();
  232.    page2->SetText("XSlider");
  233.  
  234.    //XSlider
  235.    XRect rect4( 10, 90, 200, 70);
  236.    XSlider * slider = new XSlider( page2, &rect4, ID_SLIDER, WIN_VISIBLE | SL_HOMELEFT | SL_LEFT | SL_HORIZONTAL | SL_RIBBONSTRIP | SL_TOP | SL_BUTTONSRIGHT);
  237.    slider->SetScales(11, 10, 20, 5);
  238.    slider->SetTickSize( SMA_SETALLTICKS, 5);
  239.    slider->SetTickSize( 0,10 );
  240.    slider->SetTickSize( 5,10 );
  241.    slider->SetTickSize( 10, 10 );
  242.  
  243.    slider->SetScaleText(0, "0");
  244.    slider->SetScaleText(5, "5");
  245.    slider->SetScaleText(10, "10");
  246.  
  247.    slider->AddDetent( 35 );
  248.    slider->AddDetent( 75 );
  249.  
  250.    slider->SetSliderSize( 10, 30 );
  251.    slider->SetShaftSize( 15 );
  252.    slider->SetSliderPos( 50 );
  253.  
  254.    MyPage * page3 = new MyPage( notes);
  255.    page3->SetTop();
  256.    page3->SetText("XSpinButton");
  257.  
  258.    //XSpinButton
  259.    XRect rect5( 80, 85, 50, 20);
  260.    XSpinButton * spin = new XSpinButton( page3, &rect5, ID_SPIN, WIN_VISIBLE|SP_NUMERIC|SP_MASTER);
  261.  
  262.    spin->SetLimits(0, 100);
  263.    spin->SetValue( 50 );
  264.  
  265.    MyPage * page4 = new MyPage( notes);
  266.    page4->SetTop();
  267.    page4->SetText("XCircularSlider");
  268.  
  269.    //XCircularSlider
  270.    XRect rect6( 50, 40, 110, 110);
  271.    XCircularSlider * circ = new XCircularSlider( page4, &rect6, ID_CIRC, WIN_VISIBLE|CS_CIRCULARVALUE|CS_NOBUTTON);
  272.    circ->SetIncrements( 10, 5);
  273.    circ->SetLimits( 0, 100);
  274.    circ->SetValue( 50 );
  275.  
  276.  
  277.    MyPage * page5 = new MyPage( notes);
  278.    page5->SetTop();
  279.    page5->SetText("XUserButton");
  280.  
  281.    //XUserButton
  282.    XRect rect7( 50, 90, 100, 50);
  283.    btn = new XUserButton( page5, &rect7, ID_BTN, BU_NOPOINTERFOCUS | WIN_VISIBLE, "0");
  284.  
  285.    XBitmap map1( btn );
  286.    map1.Load( "EXIT.BMP" );
  287.    btn->SetBitmap( &map1 );
  288.  
  289.    XBitmap map2( btn );
  290.    map2.Load( "EXIT2.BMP" );
  291.    btn->SetBitmap( &map2, 1);
  292.  
  293.     XPoint p(5, 5);
  294.     btn->GetBitmapPointer( 0 )->Move( &p );
  295.     btn->GetBitmapPointer( 1 )->Move( &p );
  296.  
  297.     p.Set(50,15);
  298.     btn->GetTextPointer()->Move(&p);
  299. //   btn->Invalidate();
  300.  
  301.    MyPage * page6 = new MyPage( notes);
  302.    page6->SetTop();
  303.    page6->SetText("XListBox");
  304.  
  305.    XRect rect8( 60, 50, 100, 60);
  306.    XListBox * list = new XListBox( page6, &rect8, 700, WIN_VISIBLE|LS_OWNERDRAW);
  307.  
  308.    ListDrawHandler * handler = new ListDrawHandler( list );
  309.    list->InsertItem( "Red", 0);
  310.    list->InsertItem( "Green", 1);
  311.    list->InsertItem( "Blue", 2);
  312.    list->InsertItem( "no color", 3);
  313.  
  314.    XRect rect9( 60, 120, 120, 40);
  315.    XStaticText * text6 = new XStaticText( page6, "XListBox\nwith bitmaps", &rect9, WIN_VISIBLE|TX_WORDBREAK);
  316.  
  317.    doorOpen = TRUE;
  318.  
  319.    MyPage * page7 = new MyPage( notes);
  320.    page7->SetTop();
  321.  
  322.     //this page get a bitmap on the tab instead of a text
  323.     XBitmap * pieBmp = new XBitmap( page7 );
  324.     if( pieBmp->Load( "bar.bmp" ) != 0)
  325.         XMessageBox("error loading the bitmap");
  326.     page7->SetBitmap( pieBmp );
  327.  
  328.     //Create a simple chart
  329.    XRect rect10(60, 90, 100, 100);
  330.    pie = new XPie( page7, &rect10, 300, 0, "a XPie" );
  331.  
  332.    pie->SetItemCount( 6 );
  333.  
  334.    pie->SetItemValue( 0, 20);
  335.    pie->SetItemValue( 1, 80);
  336.    pie->SetItemValue( 2, 20);
  337.    pie->SetItemValue( 3, 80);
  338.    pie->SetItemValue( 4, 10);
  339.    pie->SetItemValue( 5, 20);
  340.  
  341.    XRect rect11( 60, 20, 100, 30);
  342.    XPushButton * b = new XPushButton( page7, &rect11, ID_RAND, WIN_VISIBLE, "Randomize!" );
  343.  
  344.    pie->Calculate();
  345.    XColor col4( COL_PALEGRAY);
  346.    pie->SetBackgroundColor( &col4);
  347.  
  348.    //change the color of the pie∩s title
  349.    XText * t = pie->GetTitleObject();
  350.    XColor col5( COL_GREEN );
  351.    t->SetColor( &col5);
  352.  
  353.    pie->Show();
  354.  
  355.    page1->SetTop();
  356.    notes->Show();
  357.     EnableWindowUpdate(TRUE);
  358.  
  359.    //Activate the window
  360.    Activate();
  361. }
  362.  
  363.  
  364. void MyAppWindow :: Draw( void )
  365. {
  366.    FillBackground();
  367. }
  368.  
  369.  
  370. MyAppWindow :: ~MyAppWindow()
  371. {
  372. }
  373.  
  374.  
  375. /* here the commands of the menu and the pushbuttons are posted */
  376. BOOL MyPage :: DoCommand( LONG com)
  377. {
  378.    static LONG r = 0;
  379.  
  380.    switch( com )
  381.       {
  382.          case ID_RAND:
  383.             {
  384.                XPie * pie = (XPie*) GetWindow( 300 );
  385.                SHORT count = (rand() % 10); //randomize the count of items (max 10 in this case)
  386.                if(count < 2 )
  387.                   count = 2;
  388.                pie->SetItemCount( count );  //set the count of items
  389.                for(int i=0; i < count; i++)
  390.                   pie->SetItemValue( i, (rand()+1) % 100); //randomize values
  391.                pie->Calculate();            //recalculate
  392.                pie->Invalidate();           //draw!
  393.             }
  394.             break;
  395.          case ID_BTN:
  396.             {
  397.                XUserButton * btn = (XUserButton *) GetWindow( ID_BTN);
  398.                r++;
  399.                XString text = (LONG) r;
  400.                btn->SetText( text );
  401.                btn->Invalidate();
  402.             }
  403.             break;
  404.       }
  405.    return TRUE;
  406. }
  407.  
  408.  
  409. BOOL MyAppWindow :: QueryForClose( void )
  410. {
  411.    XMessageButton * mb[2];
  412.  
  413.    mb[0] = new XMessageButton( "Terminate", 1);
  414.    mb[1] = new XMessageButton("don∩t Terminate", 4, BS_DEFAULT);
  415.    XIcon icon;
  416.    icon.Load( "exit.ico");
  417.    XExtendedMessageBox msb( "Do you realy want to terminate?", "Sample6", MB_MOVEABLE, 2, (const XMessageButton **) mb, (const XIcon *) &icon, (XWindow*) this);
  418.    if( msb.GetCommand() == 1)
  419.       return TRUE;
  420.    else
  421.       return FALSE;
  422. }
  423.  
  424.  
  425. void MyAppWindow :: DoControl( XControlEvent * event)
  426. {
  427.  
  428. }
  429.  
  430.  
  431. //here the control-events of the window-clients are posted
  432. void MyPage :: DoControl( XControlEvent * event)
  433. {
  434.    switch( event->GetEventID()) //what type of event?
  435.       {
  436.          case WIN_SELECTED:      //window-content selected
  437.             {
  438.                switch( event->GetWindowID()) //which window posted the event?
  439.                   {
  440.                      case ID_VALUESET: //the valueset
  441.                         {
  442.                            XValueSet * set = (XValueSet*) event->GetWindow();  //get a pointer to that window
  443.                            SHORT row, col;
  444.                            set->GetSelection( row, col);
  445.                            XString string = "selected: ";
  446.                            string += "(";
  447.                            string += (int) row;
  448.                            string += ":";
  449.                            string += (int) col;
  450.                            string += ")";
  451.                            XStaticText * text = (XStaticText *) GetWindow( ID_TEXT);
  452.                            text->SetText( string );
  453.                         }
  454.                      default:
  455.                         break;
  456.                   }
  457.             }
  458.       }
  459. }
  460.  
  461.  
  462. MyApp :: MyApp(): XApplication()
  463. {
  464.    //we create an resource
  465.    XResource r( IDM_MENU, GetResourceLibrary());
  466.    app = this;
  467.  
  468.    //create the window
  469.    window = new MyAppWindow( this, &r );
  470. }
  471.  
  472.  
  473. void main ( void)
  474. {
  475.    try
  476.    {
  477.       MyApp * app = new MyApp();  //create a new application
  478.    }
  479.    catch( XException& e)
  480.    {
  481.       XMessageBox( e.GetErrorMessage());
  482.       exit(-1);
  483.    }
  484.  
  485.    app->Start();               //let the application work
  486. }
  487.