home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pwrgu2.zip / POWERGU2.EXE / DM / MENUDRAG / CMDITEM.CPP next >
Text File  |  1995-07-21  |  4KB  |  146 lines

  1. //************************************************************
  2. // Direct Manipulation - Menu Drag Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // All Rights Reserved.
  6. //************************************************************
  7. #define INCL_WINMENUS
  8. #define INCL_WINWINDOWMGR
  9. #define INCL_WININPUT
  10. #include <os2.h>
  11.  
  12. #include <istring.hpp>
  13. #include <idmitem.hpp>
  14. #include <idmprov.hpp>
  15. #include <idmimage.hpp>
  16. #include <isubmenu.hpp>
  17. #include <imnitem.hpp>
  18. #include <ipushbut.hpp>
  19. #include <istattxt.hpp>
  20. #include <itrace.hpp>
  21. #include <irect.hpp>
  22. #include <idmsrch.hpp>
  23. #include "cmditem.hpp"
  24.  
  25.  
  26.  
  27. // Construct IDMItem with type=any, operations=copyable...
  28. CommandItem::CommandItem ( IDMSourceOperation *srcOp )
  29.     : IDMItem( srcOp, IDM::any, IDMItem::linkable ),
  30.       noDismiss(false)
  31.    {
  32.     IFUNCTRACE_DEVELOP()
  33.     // Support only special "menu" rmf...
  34.     this -> addRMF( IDMItem::rmfFrom( IDM::rmLibrary,
  35.                                       IDMItem::rfForThisProcess() ) );
  36.     // Locate source menu item...
  37.     IWindow
  38.      *p = srcOp->sourceWindow();
  39.     ISubmenu
  40.       sub( p->handle() );
  41.     ITRACE_DEVELOP( "submenu->" + IString( (unsigned long)p ) )
  42.     ISubmenu::Cursor
  43.       cursor( sub );
  44.     IPoint
  45.       pos = srcOp->position();
  46.     ITRACE_DEVELOP( "Looking for item for " + pos.asString() )
  47.     for( cursor.setToFirst(); cursor.isValid(); cursor.setToNext() )
  48.       {
  49.       IMenuItem
  50.         item = sub.elementAt( cursor );
  51.       IRectangle itemRect = sub.itemRect( item.id() );
  52.  
  53.       ITRACE_DEVELOP( "testing item->" + IString( item.id() ) )
  54.       ITRACE_DEVELOP( "rectangle->" + itemRect.asString() )
  55.       if ( itemRect.contains( srcOp->position() ) )
  56.         {
  57.         // Set object() pointer to menu id...
  58.         ITRACE_DEVELOP( "Match!" )
  59.         this -> setObject( (void*) item.id() );
  60.         this -> setContents( item.text() );
  61.  
  62.         // Do not allow the menu to be dismissed.
  63.         // Much thanks to Mark Benge for this fix to work
  64.         // around the fact that PM really doesn't support
  65.         // dragging from menu items.
  66.         if (!item.isNoDismiss())
  67.           {
  68.           ITRACE_DEVELOP( "Setting no dismiss")
  69.           item.setNoDismiss();
  70.           sub.setItem( item );
  71.           this->noDismiss = true;
  72.           }
  73.         sub.selectItem( item.id() );
  74.  
  75.         break;
  76.         }
  77.       }
  78.     }
  79.  
  80. Boolean CommandItem::generateSourceItems ( IDMSourceOperation *srcOp )
  81.     {
  82.     IFUNCTRACE_DEVELOP()
  83.     // Source item is object of this class...
  84.     IDMItem::Handle
  85.       item( new CommandItem( srcOp ) );
  86.     // Set image...
  87.     IDMImage
  88.       image( IResourceId( IC_DEFAULT_FRAME_ID ), true );
  89.     item -> setImage( image );
  90.     // Add it to the source operation...
  91.     srcOp -> addItem( item );
  92.     srcOp -> setImageStyle( IDM::allStacked );
  93.     srcOp -> setOperation( IDMOperation::link );
  94.     // Indicate an item is available to drag...
  95.     return true;
  96.     }
  97.  
  98. // Called by the source handler to allow the menu to be dismissed after the  
  99. // target has indicated that the drop  processing has completed.             
  100. Boolean CommandItem::sourceEnd ( IDMSourceEndEvent & )
  101. {
  102.   IFUNCTRACE_DEVELOP()
  103.   IWindow *pWin = sourceOperation()->sourceWindow();
  104.   ISubmenu sub( pWin->handle() );
  105.   IMenuItem item = sub.menuItem( (unsigned long)object() );
  106.  
  107.   if (this->noDismiss)
  108.   {
  109.     ITRACE_DEVELOP( "Resetting no dismiss")
  110.     item.setNoDismiss( false );
  111.     sub.setItem( item );
  112.     this->noDismiss = false;
  113.   }
  114.  
  115.   pWin->sendEvent( MM_SELECTITEM,
  116.                    IEventData((unsigned short)item.id(), true),
  117.                    IEventData(0, true) );
  118.  
  119.   return( true );
  120. }
  121.  
  122. Boolean CommandItem::targetDrop ( IDMTargetDropEvent &event )
  123.     {
  124.     IFUNCTRACE_DEVELOP()
  125.     IPushButton
  126.      *pb = (IPushButton*)( event.window() );
  127.     WinSetWindowUShort( pb->handle(),
  128.                         QWS_ID,
  129.                         (unsigned short)(unsigned long)( this->object() ) );
  130.     pb -> setText( this->contents() );
  131.     pb -> enable();
  132.     return true;
  133.     }
  134.  
  135.  
  136. unsigned long CommandItem::supportedOperationsFor( const IString &selectedRMF ) const
  137.     {
  138.     return IDMItem::linkable;
  139.     }
  140.  
  141. Boolean CmdHandler::command ( ICommandEvent &event )
  142.     {
  143.     text.setText( IString( event.commandId() ) );
  144.     return true;
  145.     }
  146.