home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Sample Controls / Menus / CMenuControl.cp next >
Encoding:
Text File  |  1996-12-21  |  4.7 KB  |  201 lines  |  [TEXT/CWIE]

  1. #include "ocheaders.h"
  2. #include "BDDISPIDs.h"
  3. #include "CPopupMenuControl.h"
  4. #include "CMenuControl.h"
  5. #include "CPopupMenuTracker.h"
  6. #include "BDUtils.h"
  7. #include "Colors.h"
  8. #include "FnAssert.h"
  9. #include "dispatch.h"
  10. #include <LArray.h>
  11. #include <LArrayIterator.h>
  12. #include "CConnectionPoint.h"
  13. #include "CCPContainer.h"
  14. #include <iterator.h>
  15. #include <ctype.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18.  
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Constants
  22. //
  23.  
  24. const Int32        DefaultIdleTicks    = 60;
  25. const Uint32    DefaultIdleRefCon    = 0;
  26.  
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //
  29. // class statics
  30. //
  31.  
  32. ///////////////////////////////////////////////////////////////////////////////
  33. //
  34. //  CMenuControl::CMenuControl
  35. //
  36.  
  37. CMenuControl::CMenuControl(void)
  38. {
  39.     mCaption = NULL;
  40.     
  41.     SetOriginatorName("Menu");
  42. }
  43.  
  44. ///////////////////////////////////////////////////////////////////////////////
  45. //
  46. //  CMenuControl::~CMenuControl
  47. //
  48.  
  49. CMenuControl::~CMenuControl(void)
  50. {
  51.     if ( mCaption )
  52.     {
  53.         delete [] mCaption;
  54.         mCaption = NULL;
  55.     }
  56. }
  57.  
  58. ///////////////////////////////////////////////////////////////////////////////
  59. //
  60. //  CMenuControl::IInProcObject::Draw
  61. //
  62.  
  63. STDMETHODIMP
  64. CMenuControl::Draw(DrawContext * context)
  65. {
  66.     ASSERT((context != NULL), "NULL draw context!");
  67.  
  68.     if (context->DrawAspect != DVASPECT_CONTENT)
  69.         return ResultFromScode(DV_E_DVASPECT);
  70.         
  71.     SetText(context, mCaption, kDontRedraw);
  72.     Draw3D(context);
  73.     
  74.     return ResultFromScode(S_OK);
  75. }
  76.  
  77. ///////////////////////////////////////////////////////////////////////////////
  78. //
  79. //  CMenuControl::IInProcObject::DoEvent
  80. //
  81.  
  82. STDMETHODIMP
  83. CMenuControl::DoMouse(MouseEventType inMouseET, PlatformEvent* inEvent)
  84. {
  85.     DrawContext    context = {(PortType) 0};
  86.     mContainerSiteP->AcquireContext(mActiveContext->GetContextID(), &context);    
  87.             
  88.     switch (inMouseET) 
  89.     {
  90.         case MouseDown:
  91.         {
  92.             SetTextColor(&context, RGB_RED, kDontRedraw);
  93.             SetState(&context, rstDepressed, kRedraw);
  94.             
  95.             Point menuLocation;
  96.             GetMenuLocation(context, &menuLocation);
  97.             CPopupMenuTracker thePopup(this, mMenuHandle, 0);
  98.             thePopup.TrackMouse(menuLocation);
  99.             thePopup.GetCurrentItem(&mItemSelected);
  100.             SetTextColor(&context, RGB_BLACK, kDontRedraw);
  101.             SetState(&context, rstNominal, kRedraw);
  102.             
  103.             mContainerSiteP->ReleaseContext(&context);
  104.             
  105.             if ( mItemSelected.itemNumber != 0 )
  106.                 FireEvent(IID_IDidMenuEvents, DISPID_CLICK, inEvent);
  107.     
  108.             break;
  109.         }
  110.         
  111.         case MouseEnter:
  112.         {
  113.             SetTextColor(&context, RGB_RED, kDontRedraw);
  114.             SetState(&context, rstGreen, kRedraw);
  115.             mContainerSiteP->ReleaseContext(&context);
  116.             
  117.             break;
  118.         }
  119.         
  120.         case MouseLeave:
  121.         {
  122.             SetTextColor(&context, RGB_BLACK, kDontRedraw);
  123.             SetState(&context, rstNominal, kRedraw);
  124.             mContainerSiteP->ReleaseContext(&context);
  125.             
  126.             break;
  127.         }
  128.         
  129.         default:
  130.             mContainerSiteP->ReleaseContext(&context);
  131.             break;
  132.  
  133.    }
  134.             
  135.  
  136.     return ResultFromScode(S_OK);
  137. }
  138.  
  139. ///////////////////////////////////////////////////////////////////////////////
  140. //
  141. // CMenuControl::IPersistPropertyBag::Load
  142. //
  143.  
  144. STDMETHODIMP
  145. CMenuControl::Load(IPropertyBag* propertyBag, IErrorLog* errorLog)
  146. {
  147.     CPopupMenuControl::Load(propertyBag, errorLog);
  148.     
  149.     char propertyString[Str255BufferLength];
  150.     
  151.     if ( ::LoadPropertyString(propertyBag, "caption", propertyString, Str255StringLength, errorLog) )
  152.     {
  153.         if ( mCaption )
  154.         {
  155.             delete [] mCaption;
  156.             mCaption = NULL;
  157.         }
  158.         
  159.         mCaption = (char *) new char[Str255BufferLength];
  160.         strcpy(mCaption, propertyString);
  161.         
  162.         SetOriginatorName(mCaption);
  163.     }
  164.  
  165.     // If we successfully loaded a mCaption, then set the source name to the
  166.     // same thing.  The source name is used to handle the case where we want to pass
  167.     // the source of a Popup event through to another event client.  If the
  168.     // CPopupMenuControl generates events itself (as when it's subclassed by
  169.     // CMenuControl), then the source is this object itself.
  170.     if ( mCaption )
  171.         SetSourceName(mCaption);
  172.  
  173.     return ResultFromScode(S_OK);
  174. }  
  175.  
  176. ///////////////////////////////////////////////////////////////////////////////
  177. //
  178. //  CMenuControl::IDoMenuEvents::Popup
  179. //
  180.  
  181. STDMETHODIMP
  182. CMenuControl::Popup(IUnknown* /*Source*/, PlatformEvent* /*Event*/)
  183. {
  184.     // We're not expecting anyone to be sending us Popup messages.
  185.       
  186.     return ResultFromScode(E_FAIL);
  187. }
  188.  
  189. ///////////////////////////////////////////////////////////////////////////////
  190. //
  191. // CMenuControl::GetMenuLocation
  192. //
  193. void CMenuControl::GetMenuLocation(const DrawContext & context, Point * menuLocation)
  194. {
  195.     ASSERT(menuLocation != NULL, "NULL parameter!");    
  196.     
  197.     menuLocation->v = context.Location.bottom + 1;
  198.     menuLocation->h = context.Location.left;
  199.     ::LocalToGlobal(menuLocation);
  200. }
  201.