home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / samples / sample2 / sample2.cpp < prev    next >
C/C++ Source or Header  |  1997-03-18  |  12KB  |  436 lines

  1. #include "sample2.h"
  2. #include "dialog.h"
  3.  
  4. #include XColor_i
  5. #include XMessageBox_i
  6. #include XException_i
  7. #include XDragHandler_i
  8. #include XSlider_i
  9. #include XString_i
  10. #include XBubbleHelp_i
  11. #include XWebView_i
  12.  
  13. #include <stdlib.h>
  14.  
  15. MyApp * app;
  16.  
  17. class MyAboutWin: public XFrameWindow
  18. {
  19.   public:
  20.     MyAboutWin( XResource *);
  21.     BOOL DoCommand( LONG);
  22. };
  23.  
  24.  
  25. BOOL MyAboutWin :: DoCommand( LONG com)
  26. {
  27.    if( com == DID_PUSH_OK)  //ok-button pressed, close window
  28.       delete this;//Close();
  29.    return TRUE;
  30. }
  31.  
  32.  
  33. MyAboutWin :: MyAboutWin ( XResource * r): XFrameWindow ( r, "About", XFrameWindow::defaultDialogStyle | FRM_CENTER, NULL, NULL, TRUE)
  34. {
  35. }
  36.  
  37.  
  38. BOOL MyApp :: QueryForQuit( void )
  39. {
  40.   XMessageBox mBox( "Do you realy want to quit?", "To the User", MB_YESNO | MB_ICONQUESTION|MB_MOVEABLE, window);
  41.  
  42.   if( mBox.GetCommand() == MBID_YES)
  43.      return TRUE;
  44.   else
  45.      return FALSE;
  46. }
  47.  
  48.  
  49. BOOL MyAppWindow :: QueryForClose( void )
  50. {
  51.   XMessageBox mBox( "Ready to go?", "To the User", MB_YESNO | MB_ICONQUESTION|MB_MOVEABLE, this);
  52.  
  53.   if( mBox.GetCommand() == MBID_YES)
  54.      {
  55.         app->Terminate();//close all windows
  56.         return TRUE;
  57.      }
  58.   else
  59.      return FALSE;
  60. }
  61.  
  62.  
  63. class MyBubble: public XBubbleHelp
  64. {
  65.    public:
  66.       MyBubble( XResource * r, XWindow*w): XBubbleHelp(r, w) { ;}
  67.       BOOL SetMsgText( ULONG windowID );
  68. };
  69.  
  70.  
  71. BOOL MyBubble :: SetMsgText( ULONG windowID )
  72. {
  73.    switch( windowID)
  74.       {
  75.          case IDM_BACK:
  76.             SetText( "Move backward");
  77.             break;
  78.          case IDM_HOME:
  79.             SetText("Move to the homepage");
  80.             break;
  81.          case IDM_FORWARD:
  82.             SetText("Move Foreward");
  83.             break;
  84.          case IDM_ANIMATION:
  85.             SetText("Start/Stop loading the document");
  86.             break;
  87.          case 100:
  88.             SetText( "Enter an adress or select a loaded document");
  89.             break;
  90.          default:
  91.             return FALSE;
  92.       }
  93.    return TRUE;
  94. }
  95.  
  96.  
  97. class MyWebView: public XWebView
  98. {
  99.       MyAppWindow * win;
  100.       BOOL loadError;
  101.    public:
  102.       MyWebView(XWindow * w):XWebView(w, NULL,500) { win = (MyAppWindow*) w; }
  103.       void ButtonPressed( SHORT num, SHORT x, SHORT y) { SetFocus(); }
  104.       virtual void HandleError( ULONG, char* );
  105.       LONG Load( const char * url, const BOOL reload = FALSE);
  106.       void LoadResult( LONG res);
  107.       void LoadProgress( LONG status );
  108. };
  109.  
  110.  
  111. void MyWebView :: LoadProgress( LONG status)
  112. {
  113.    switch(status)
  114.       {
  115.          case WEB_CONNECT:
  116.             win->text2->SetText( "connecting.." );
  117.             break;
  118.          case WEB_WAIT:
  119.             win->text2->SetText( "waiting for reply.." );
  120.             break;
  121.          case WEB_LOADDATA:
  122.             win->text2->SetText( "loading data.." );
  123.             break;
  124.          case WEB_RENDER:
  125.             win->text2->SetText( "building document.." );
  126.             break;
  127.       }
  128. }
  129.  
  130.  
  131. void MyWebView :: HandleError( ULONG errorCode, char * errorMsg)
  132. {
  133.    loadError = TRUE;
  134.    XString msg = "Error: ";
  135.    if(errorMsg)
  136.       msg += errorMsg;
  137.    win->text2->SetText( (char*) msg);
  138.    XProcess::Beep( 400, 300);
  139. }
  140.  
  141.  
  142. void MyWebView :: LoadResult( LONG res)
  143. {
  144.    win->animation->Animate(FALSE);
  145.  
  146.    if( res == WEB_SUCCESS && loadError == FALSE)
  147.       {
  148.          XString buffer1, buffer2;
  149.          win->GetText( &buffer1 );
  150.          ULONG f;
  151.          if( buffer1.Find(f, ":"))
  152.             buffer1 = buffer1.Left(f);
  153.          GetDocTitle( &buffer2 );
  154.          buffer1 = buffer1 + ": " + buffer2;
  155.          win->SetText( buffer1 );
  156.  
  157.          win->text2->SetText( "Document done");
  158.       }
  159.    if( res == WEB_ERROR)
  160.       {
  161.          XProcess::Beep(400, 500);
  162.          win->text2->SetText( "Error ocured!");
  163.       }
  164.    if( res == WEB_CANCELLED)     
  165.       win->text2->SetText( "Load interrupted");
  166. }
  167.  
  168.  
  169. LONG MyWebView :: Load( const char * url, const BOOL reload)
  170. {                  
  171.    loadError = FALSE;
  172.    if( strstr(url, "#") == NULL)
  173.       {
  174.          win->animation->Animate(TRUE, FALSE);
  175.          XString msg = "Loading: ";
  176.          msg += url;
  177.          win->text2->SetText( msg);
  178.       }
  179.    if( win->http->SearchString( url ) == LS_NONE)
  180.      {
  181.          win->loading = TRUE;
  182.          win->http->InsertItem( url, 0);
  183.          win->http->SelectItem( 0);
  184.          win->loading = FALSE;
  185.      }
  186.  
  187.    XWebView::Load(url, reload);
  188.  
  189.    return 0;
  190. }
  191.  
  192.  
  193. class MyToolBar: public XToolBar
  194. {
  195.    public:
  196.       MyToolBar( XFrameWindow*w, SHORT id):XToolBar(w, TB_TOP|TB_RIPABLE, id) { ;}
  197.       void Cut( void );
  198. };
  199.  
  200.  
  201. void MyToolBar :: Cut( void )
  202. {
  203.    XToolBar::Cut();
  204.    if( GetWindowID() == 800)
  205.       GetCutWindow()->SetText( "ToolBar 1" );
  206.    else
  207.       GetCutWindow()->SetText( "ToolBar 2" );
  208. }
  209.  
  210.  
  211. MyAppWindow :: MyAppWindow( XApplication * app, XResource r ): XFrameWindow( &r, "Sample2 - It∩s not Netscape", XFrameWindow::defaultStyle | FRM_MINBUTTON | FRM_TASKLIST | FRM_ICON )
  212. {
  213.    XColor col( COL_WHITE);
  214.    SetBackgroundColor( &col );
  215.  
  216.    //create a helpinstance
  217.    help = new XHelpInstance( "sample2.hlp", app, "Sample2 - Help");
  218.    //associate the help with this function
  219.    help->AssociateWindow( this );
  220.  
  221.    //we want a menubar, it is defined in the resources of the exe-file with the id IDM_MAIN
  222.    XResource rs( IDM_MAIN, app->GetResourceLibrary());
  223.    menu = new XMenuBar( this, &rs);
  224.  
  225.    //we create a toolbar for the buttons at the top...
  226.    toolBar1 = new MyToolBar( this, 800);
  227.  
  228.    //now create pushbuttons with bitmaps, the bitmaps must be in the resources of the exe
  229.    XRect rec( 0, 0, 30, 30);
  230.    back = new XPushButton( toolBar1, &rec, (USHORT) IDM_BACK, BU_BITMAP);
  231.    forward = new XPushButton( toolBar1, &rec, (USHORT) IDM_FORWARD, BU_BITMAP);
  232.    home = new XPushButton( toolBar1, &rec, (USHORT) IDM_HOME, BU_BITMAP);
  233.  
  234.    //add the pushbuttons to the toolbar, the toolbar will use the size of the first button
  235.    toolBar1->AddWindow( back , FALSE, TRUE);
  236.    toolBar1->AddWindow( forward );
  237.    toolBar1->AddWindow( home );
  238.  
  239.    /*create another toolbar*/
  240.    toolBar2 = new MyToolBar( this, 900);
  241.    toolBar2->SetHeight( 50 );
  242.  
  243.    XRect r1( 0, 0, 80, 20);
  244.    text = new XStaticText( toolBar2, "Location:", &r1, TX_RIGHT);
  245.    XColor black( COL_BLACK);
  246.    text->SetForegroundColor( &black);
  247.  
  248.    toolBar2->AddWindow( text, FALSE, FALSE, NULL, 0, -23);
  249.  
  250.    /*create a combo-box, it get the id 100, so its associated with the helpinstance*/
  251.    XRect r2( 0, 0, 490, 200);
  252.    http = new XComboBox(toolBar2, &r2, 100);
  253.  
  254.    toolBar2->AddWindow( http, TRUE, FALSE, NULL, -10, - 20);
  255.  
  256.    /* now a graphic-button will be created. the button displays a simple animation. */
  257.    /* to do so, the buttons needs a list of bitmap-IDs (from bitmaps in the resources)*/
  258.    /* which build the animation. in this case we have 6 bitmaps */
  259.    SHORT list[6] = {STAR1, STAR2, STAR3, STAR4, STAR5, STAR6};
  260.  
  261.    /* give in the constructor the styles for playing animation. additionaly the button */
  262.    /* has two states (if you press the button, it will not come up automaticaly */
  263.    /* the id IDM_ANIMATION is also in the help-file so help will be displayed automaticaly */
  264.    XRect r0(0,0,0,0);
  265.    animation = new XGraphicButton( toolBar2, &r0, IDM_ANIMATION, WIN_VISIBLE | GB_ANIMATION, "", 6, list);
  266.  
  267.    /* set the speed of the animation */
  268.    animation->SetAnimationRate( 50 );
  269.  
  270.    /* add the button to the toolbar */
  271.    toolBar2->AddWindow( animation, FALSE, TRUE, NULL, 10);
  272.  
  273.    /*..and a toolbar at the bottom*/
  274.    toolBar3 = new XToolBar( this, TB_BOTTOM);
  275.    toolBar3->SetHeight( 30);
  276.  
  277.    XRect r3(0,0,40,40);
  278.    key = new XStaticBitmap( toolBar3, &r3, IDM_KEY);
  279.    toolBar3->AddWindow( key, FALSE, FALSE, NULL, 10, -3);
  280.  
  281.    XRect r4( 0,0, 520, 20);
  282.    border = new XStaticFrame( toolBar3, &r4);
  283.    toolBar3->AddWindow( border, TRUE, FALSE, 0, -4);
  284.  
  285.    XRect r5( 0, 0, 500, 15);
  286.    text2 = new XStaticText( toolBar3, "", &r5, TX_LEFT | TX_VCENTER);
  287.    text2->SetForegroundColor( &black );
  288.    text2->SetFont( "8.Helv");
  289.  
  290.    toolBar3->AddWindow( text2, FALSE, FALSE, NULL, -505, -3);
  291.  
  292.    //setup the menu
  293.    menu->CheckItem( IDM_TOOLBAR );
  294.    menu->CheckItem( IDM_URL );
  295.    menu->CheckItem( IDM_STATUSLINE );
  296.  
  297.    XRect rect( 100, 100, 670, 500);
  298.    SetSize( &rect);
  299.  
  300.    Activate();
  301.  
  302.    XResource resource2(0, r.GetResourceLibrary());
  303.    MyBubble * b1 = new MyBubble( &resource2, toolBar1);
  304.    MyBubble * b2 = new MyBubble( &resource2, toolBar2);
  305.  
  306.    loading = FALSE;
  307.  
  308.    webView = new MyWebView( this );
  309.    SetClient( webView );
  310.    http->InsertItem( "sample2.htm", 0);
  311.    http->SelectItem(0);
  312.  
  313.    webView->SetFocus();
  314. }
  315.  
  316.  
  317. void MyAppWindow :: DoControl( XControlEvent * e)
  318. {
  319.    if( (e->GetEventID() == WIN_ENTER || e->GetEventID() == WIN_SELECTED) && e->GetWindowID() == 100 && loading == FALSE)
  320.       {
  321.          if( (http->GetSelection() == LS_NONE && e->GetEventID() == WIN_SELECTED) || loading == TRUE)
  322.             return;
  323.          XString url;
  324.          SHORT res;
  325.          if( e->GetEventID() == WIN_ENTER )
  326.             http->GetText( &url );
  327.          else
  328.             {
  329.                res=http->GetSelection();
  330.                http->GetItemText(res, &url);
  331.                http->SetText(url);
  332.             }
  333.  
  334.          if(url.GetLength() == 0)
  335.             return;
  336.  
  337.          res = http->SearchString( url );
  338.          if( res == LS_NONE )
  339.             http->InsertItem( url, 0);
  340.          webView->Load( url );
  341.       }
  342. }
  343.  
  344.  
  345. BOOL MyAppWindow :: DoCommand( LONG com)
  346. {
  347.   switch(com)
  348.      {
  349.         case IDM_BACK:
  350.            {
  351.               SHORT res = http->GetSelection();
  352.               http->SelectItem( 1 + res );
  353.            }
  354.            break;
  355.         case IDM_ANIMATION:
  356.            if( animation->IsSelected() == TRUE)
  357.               webView->StopLoading();
  358.            break;
  359.         case IDM_ABOUT:  //menuitem <about> selected, show the window
  360.            {
  361.               XResource res( DIALOG_ABOUT, app->GetResourceLibrary());
  362.               MyAboutWin * win = new MyAboutWin( &res);
  363.               win->ShowModal( this );
  364.            }
  365.            break;
  366.         case IDM_TOOLBAR:
  367.            {
  368.               if( toolBar1->IsVisible())
  369.                  {
  370.                     menu->CheckItem( IDM_TOOLBAR, FALSE);
  371.                     toolBar1->Show(FALSE);
  372.                  }
  373.               else
  374.                  {
  375.                     menu->CheckItem( IDM_TOOLBAR, TRUE);
  376.                     toolBar1->Show();
  377.                  }
  378.            }
  379.            break;
  380.         case IDM_STATUSLINE:
  381.            {
  382.               if( toolBar3->IsVisible())
  383.                  {
  384.                     menu->CheckItem( IDM_STATUSLINE, FALSE);
  385.                     toolBar3->Show(FALSE);
  386.                  }
  387.               else
  388.                  {
  389.                     menu->CheckItem( IDM_STATUSLINE, TRUE);
  390.                     toolBar3->Show();
  391.                  }
  392.            }
  393.            break;
  394.         case IDM_URL:
  395.            {
  396.               if( toolBar2->IsVisible())
  397.                  {
  398.                     menu->CheckItem( IDM_URL, FALSE);
  399.                     toolBar2->Show(FALSE);
  400.                  }
  401.               else
  402.                  {
  403.                     menu->CheckItem( IDM_URL, TRUE);
  404.                     toolBar2->Show();
  405.                  }
  406.            }
  407.            break;
  408.         case IDM_HELP_HELP:
  409.            help->ShowHelpForHelp();
  410.            break;
  411.         case IDM_HELP_INDEX:
  412.            help->ShowHelpIndex();
  413.            break;
  414.      }
  415.    return TRUE;
  416. }
  417.  
  418.  
  419. void main ( void)
  420. {
  421.    try
  422.    {
  423.       app = new MyApp();  //create a new application
  424.  
  425.       XResource r( IDM_ICON, app->GetResourceLibrary());
  426.  
  427.       app->window = new MyAppWindow( app, r );   //create new framewindow (see above)
  428.       app->Start();               //let the application work
  429.    }
  430.    catch( XException e)
  431.    {
  432.       XMessageBox( e.GetErrorMessage());
  433.       exit(-1);
  434.    }
  435. }
  436.