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

  1. #define DEBUG
  2. #include "sample10.h"
  3. #include "resource.h"
  4. #include <stdlib.h>
  5.  
  6.  
  7.  
  8. class MyDragHandler: public XDragHandler
  9. {
  10.    public:
  11.       MyDragHandler( XWindow * w): XDragHandler( w ) { ;}
  12.       void HandleEvent(XDragEvent*);
  13. };
  14.  
  15.  
  16. /* here the method HandleEvent is overridden, so we can handle drag/drop events*/
  17. void MyDragHandler :: HandleEvent ( XDragEvent * event)
  18. {
  19.    switch( event->GetEventID())
  20.    {
  21.       /* the mousepointer is over the MLE holding one or more objects */
  22.       case DRG_DRAGOVER:
  23.          {
  24.             SHORT i;
  25.             XDragItem item;
  26.             /* ask for every object the type and format */
  27.             for(i=0; i < event->GetDragItemCount(); i ++)
  28.             {
  29.                event->GetDragItem( &item, i);
  30.                if(item.VerifyItemType( DRG_M_FILE ) == FALSE)
  31.                {
  32.                   /* no textfile, we will not accept these objects */
  33.                   event->SetAcceptMode( DRG_NEVERDROP);
  34.                   return;
  35.                }
  36.             }
  37.             event->SetAcceptMode( DRG_DROP);       //ok, can be dropped
  38.             event->SetOperation( DO_COPY);         //we will work with a copy
  39.          }
  40.          return;
  41.       /* one or more object are dropped, because we∩ve tested the format (see above) */
  42.       /* we don∩t have to test it here again, the object will be files */
  43.       case DRG_DROP:
  44.          {
  45.             XDragItem item;
  46.             XString name, container;
  47.             SHORT i;
  48.  
  49.             /* query for every object the information we need */
  50.             for(i=0; i < event->GetDragItemCount(); i++)
  51.             {
  52.                event->GetDragItem( &item, i);
  53.                item.Accept();                        //let the sender know that we accept the object
  54.  
  55.                item.GetName( &name);               //query the filename
  56.                item.GetContainerName( &container); //query the directory
  57.                container+= name;                     //make a complete path
  58.                                                    //get the parent window
  59.                MyAppWindow * appWin = (MyAppWindow*) GetWindow();
  60.                appWin->Load( container );        //load the file
  61.             }
  62.          }
  63.          return;
  64.       }
  65. }
  66.  
  67.  
  68. MyAppWindow :: MyAppWindow(char * filename): XScrollWindow( IDM_MAIN, "Sample10 - Picture viewer", XFrameWindow::defaultStyle | FRM_TASKLIST | FRM_MENU)
  69. {
  70.    init = TRUE;
  71.    loaded = stretch = FALSE;
  72.  
  73.    //create a graphic device associated to the window
  74.    graphWindow = new XGraphicDevice( this, FALSE, TRUE, PU_PELS, GPIA_ASSOC);
  75.  
  76.    //create a graphic device which resides in memory
  77.    graphMemory = new XGraphicDevice( this, TRUE, TRUE, PU_PELS, GPIA_ASSOC | GPIT_MICRO);
  78.    XColor color(COL_PALEGRAY);
  79.    graphMemory->SetBackgroundColor(&color);
  80.    graphWindow->SetBackgroundColor(&color);
  81.    AddVertScroller( );
  82.    AddHorzScroller( );
  83.  
  84.    //create a bitmap
  85.    XPoint rp(0, 0);
  86.    bmp = new XBitmap( &rp);
  87.  
  88.    //draw the objects ONLY ONE TIME
  89.    graphMemory->FillBackground();
  90.  
  91.    XRect rect( 150, 150, 500, 500);
  92.    SetSize(&rect);
  93.  
  94.    MyDragHandler * myDragHandler = new MyDragHandler(this);
  95.  
  96.    Activate();
  97.    if(filename)
  98.       Load(filename);
  99.    init = FALSE;
  100. }
  101.  
  102.  
  103. void MyAppWindow :: CalcBitmap()
  104. {
  105.    XRect rect;
  106.    GetSize(&rect);
  107.    SetYScrollPos( 0 );
  108.    SetXScrollPos( 0 );
  109.    bmp->GetDimensions(&bmpSize);
  110.    int offset = XWindow::GetSysValue(SV_CYHSCROLL) + XWindow::GetSysValue(SV_CYSIZEBORDER) * 2 + XWindow::GetSysValue(SV_CYMENU) + XWindow::GetSysValue(SV_CYTITLEBAR);
  111.    rect.SetY( rect.GetY() + rect.GetHeight() - (bmpSize.GetHeight() + offset));
  112.    rect.SetHeight(bmpSize.GetHeight() + offset);
  113.    rect.SetWidth( bmpSize.GetWidth() + XWindow::GetSysValue(SV_CXSIZEBORDER) * 2 + XWindow::GetSysValue(SV_CXVSCROLL));
  114.    SetSize(&rect);
  115.    GetClientSize(&rect);
  116.  
  117.    if(rect.GetHeight() < bmpSize.GetHeight())
  118.    {
  119.       LONG diff = bmpSize.GetHeight() - rect.GetHeight();
  120.       GetSize(&rect);
  121.       rect.SetHeight( rect.GetHeight() + diff);
  122.       rect.SetY( rect.GetY() - diff);
  123.       SetSize(&rect);
  124.    } /* end if */
  125.    SetVirtualX( bmpSize.GetWidth());
  126.    SetVirtualY( bmpSize.GetHeight());
  127.    graphMemory->FillBackground();
  128.    bmp->Draw(graphMemory);
  129.    loaded = TRUE;
  130.    Invalidate();
  131. }
  132.  
  133.  
  134. void MyAppWindow :: Load( char * filename)
  135. {
  136.    OOL_POINTERHANDLE handle = XWindow::GetPointerHandle();
  137.    XWindow::SetPointer( XWindow::GetSystemPointerHandle(PTR_WAIT));
  138.    try
  139.    {
  140.       XBitmapFilter* p = XBitmap :: GetImportFilterStruct (filename);
  141.       XBitmap :: SetupImportFilter(this, filename, p);
  142.       if (DID_OK == p->command)
  143.           bmp->Load( filename, p);
  144.       else
  145.          return;
  146.       CalcBitmap();
  147.    } /* end try */
  148.    catch( XException e )
  149.    {
  150.       XMessageBox((char*) e.GetErrorMessage(), (char*)"Error", MB_ERROR|MB_MOVEABLE|MB_OK, this);
  151.    } /* end catch */
  152.    XWindow::SetPointer(handle);
  153. }
  154.  
  155.  
  156. void MyAppWindow :: SetBitmapPos()
  157. {
  158.    GetClientSize(&clientRect);
  159.    SetXPageSize( clientRect.GetWidth());
  160.    SetYPageSize( clientRect.GetHeight());
  161.    Invalidate();
  162. }
  163.  
  164.  
  165. void MyAppWindow :: VScroll( LONG y)
  166. {
  167.    if(init == FALSE)
  168.    {
  169.       int pos = GetVertPos() + y;
  170.       if(pos < 0 )
  171.          pos = 0;
  172.       if(pos > bmpSize.GetHeight() - clientRect.GetHeight())
  173.          pos = bmpSize.GetHeight() - clientRect.GetHeight();
  174.       SetYScrollPos( pos );
  175.       Invalidate();
  176.    } /* end if */
  177. }
  178.  
  179.  
  180. void MyAppWindow :: HScroll( LONG x)
  181. {
  182.  
  183.    if(init == FALSE)
  184.    {
  185.       int pos = GetHorzPos() + x;
  186.       if(pos < 0 )
  187.          pos = 0;
  188.       if(pos > bmpSize.GetWidth() - clientRect.GetWidth())
  189.          pos = bmpSize.GetWidth() - clientRect.GetWidth();
  190.       SetXScrollPos( pos );
  191.       Invalidate();
  192.    } /* end if */
  193. }
  194.  
  195.  
  196. void MyAppWindow :: Draw( void )
  197. {
  198.    //the content of the window has to be drawn
  199.    if(graphWindow && graphMemory)
  200.    {
  201.       if(loaded == FALSE)
  202.       {
  203.          graphWindow->FillBackground();
  204.          return;
  205.       } /* end if */
  206.       else if(stretch)
  207.       {
  208.          XRect rect = clientRect;
  209.          rect.SetX(0);
  210.          rect.SetY(0);
  211.          XRect rect2;
  212.          rect2.SetWidth(bmpSize.GetWidth());
  213.          rect2.SetHeight(bmpSize.GetHeight());
  214.          graphWindow->Copy( graphMemory, &rect, &rect2);
  215.       }
  216.       else
  217.       {
  218.          //query the size of the rectangle to draw
  219.          XRect rect;
  220.          GetUpdateRect( &rect);
  221.  
  222.          //copy only the update-rectangle
  223.          XRect rect2;
  224.          rect2.SetWidth(0);
  225.          rect2.SetHeight(0);
  226.  
  227.          if(bmpSize.GetHeight() > clientRect.GetHeight())
  228.          {
  229.             if( rect.GetHeight() + rect.GetY() < clientRect.GetHeight() )
  230.                rect2.SetY( bmpSize.GetHeight() - clientRect.GetHeight() - GetVertPos() + rect.GetY());
  231.             else
  232.                rect2.SetY( bmpSize.GetHeight() - rect.GetHeight() - GetVertPos() );
  233.          } /* end if */
  234.          else
  235.             rect2.SetY(rect.GetY());
  236.  
  237.          if(bmpSize.GetWidth() > clientRect.GetWidth())
  238.             rect2.SetX(GetHorzPos() + rect.GetX());
  239.          else
  240.             rect2.SetX(rect.GetX());
  241.          graphWindow->Copy( graphMemory, &rect, &rect2);
  242.       }
  243.    }
  244. }
  245.  
  246.  
  247. BOOL MyAppWindow :: DoCommand(LONG command)
  248. {
  249.    switch(command)
  250.    {
  251.       case IDM_FILE_EXIT:
  252.          Close();
  253.          break;
  254.       case IDM_FILE_SAVE:
  255.          {
  256.             XGLibFileDialog save(this, "*", "Save bitmap");
  257.             if (USER_OK == save.GetCommand ())
  258.             {
  259.                XString filename;
  260.                save.GetFileName (&filename);
  261.                try
  262.                {
  263.                   XBitmapFilter* p = XBitmap :: GetExportFilterStruct (filename);
  264.                   XBitmap :: SetupExportFilter (this, filename, p);
  265.                   if (DID_OK == p->command)
  266.                      bmp->Save (filename, p);
  267.                }
  268.                catch (XException& e)
  269.                {
  270.                   e.ShowError ();
  271.                }
  272.             }
  273.          }
  274.          break;
  275.       case IDM_FILE_OPEN:
  276.          {
  277.             XGLibFileDialog fileDlg(this, "*", "Open bitmap" );
  278.  
  279.             /* the user selected a file */
  280.             if( fileDlg.GetCommand() == USER_OK)
  281.             {
  282.                XString fileName;
  283.                fileDlg.GetFileName( &fileName, 0 );
  284.                Load(fileName);
  285.             }
  286.          }
  287.          break;
  288.       case IDM_EDIT_COPY:
  289.          {
  290.             if(loaded)
  291.             {
  292.                XClipBoard clipBoard;
  293.                clipBoard.SetBitmap(bmp);
  294.             } /* end if */
  295.          }
  296.          break;
  297.       case IDM_EDIT_PASTE:
  298.          {
  299.             XClipBoard clipBoard;
  300.             if(clipBoard.GetBitmap(bmp))
  301.                CalcBitmap();
  302.          }
  303.          break;
  304.       case IDM_OPTION_STRETCH:
  305.          stretch = stretch ? FALSE : TRUE;
  306.          if(stretch)
  307.          {
  308.             SetVirtualX( 0 );
  309.             SetVirtualY( 0 );
  310.          }
  311.          else
  312.          {
  313.             SetVirtualX( bmpSize.GetWidth());
  314.             SetVirtualY( bmpSize.GetHeight());
  315.          } /* end if */
  316.          Invalidate();
  317.       default :
  318.          break;
  319.    } /* end switch */
  320.    return TRUE;
  321. }
  322.  
  323.  
  324. void MyAppWindow :: InitMenu( XMenu * menu)
  325. {
  326.    if(loaded)
  327.    {
  328.       menu->EnableItem(IDM_EDIT_COPY, TRUE);
  329.       menu->EnableItem(IDM_FILE_SAVE, TRUE);
  330.    } /* end if */
  331.    else
  332.    {
  333.       menu->EnableItem(IDM_EDIT_COPY, FALSE);
  334.       menu->EnableItem(IDM_FILE_SAVE, FALSE);
  335.    }
  336.    XClipBoard clipBoard;
  337.    if(clipBoard.IsFormatAvaible(CF_BITMAP))
  338.       menu->EnableItem(IDM_EDIT_PASTE, TRUE);
  339.    else
  340.       menu->EnableItem(IDM_EDIT_PASTE, FALSE);
  341.    if(stretch)
  342.       menu->CheckItem(IDM_OPTION_STRETCH, TRUE);
  343.    else
  344.       menu->CheckItem(IDM_OPTION_STRETCH, FALSE);
  345. }
  346.  
  347.  
  348. void MyAppWindow :: DoSize( XSize*)
  349. {
  350.    GetClientSize(&clientRect);
  351.    HScroll(0);
  352.    VScroll(0);
  353.    SetBitmapPos();
  354. }
  355.  
  356.  
  357. void main ( int argc, void ** argv)
  358. {
  359.    try
  360.    {
  361.       XMemoryCheck memObj;
  362.       XConsole::CheckConsole(argc, argv);
  363.       MyAppWindow * window = new MyAppWindow(argc > 1 ? (char*) argv[1] : NULL);   //create new framewindow (see above)
  364.       XApplication::GetApplication()->Start();  //let the application work
  365.    }
  366.    catch( XException e)
  367.    {
  368.       e.ShowError();   //shit
  369.    }
  370. }