home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / CommandKeyAttachments / CCmdKeyAttachment.cp < prev    next >
Encoding:
Text File  |  1997-01-31  |  5.9 KB  |  161 lines  |  [TEXT/R*ch]

  1. // ===========================================================================
  2. //    File:                        CCmdKeyAttachment.cp
  3. // Version:                    1.0.1 - Jan 31, 1997
  4. //    Author:                    Mike Shields (mshields@inconnect.com)
  5. //                            
  6. //    Copyright ©1996 Mike Shields. All rights reserved.
  7. //    I hereby grant users of CCmdKeyAttachment permission to use it (or any modified 
  8. //    version of it) in applications (or any other type of Macintosh software 
  9. //    like extensions -- freeware, shareware, commercial, or other) for free, 
  10. //    subject to the terms that:
  11. //
  12. //        (1)  This agreement is non-exclusive.
  13. //
  14. //        (2)  I, Mike Shields, retain the copyright to the original source code.
  15. //
  16. //    These two items are the only required conditions for use. However, I do have 
  17. //    an additional request. Note, however, that this is only a request, and 
  18. //    that it is not a required condition for use of this code.
  19. //
  20. //        (1) That I be given credit for CCmdKeyAttachment code in the copyrights or 
  21. //            acknowledgements section of your manual or other appropriate documentation.
  22. //
  23. //
  24. //    I would like to repeat that this last item is only a request. You are prefectly 
  25. //    free to choose not to do any or all of them.
  26. //    
  27. //        This source code is distributed in the hope that it will be useful,
  28. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. // ===========================================================================
  31. //    CCmdKeyAttachment.h        <- double-click + Command-D to see class declaration
  32. //
  33. //    Class which handles drawing and responding to command keys when it is attached 
  34. // to a button
  35.  
  36. #include "CCmdKeyAttachment.h"
  37. #include <LStream.h>
  38.  
  39. //----------------------------------------------------------------------------------------
  40. // CCmdKeyAttachment::CCmdKeyAttachment
  41. //----------------------------------------------------------------------------------------
  42. CCmdKeyAttachment::CCmdKeyAttachment()
  43.     : mMyControl(nil), mSuperCommander(nil), mCommandKey(0)
  44. {
  45.     mMessage = 814;
  46. }
  47.  
  48. //----------------------------------------------------------------------------------------
  49. // CCmdKeyAttachment::CCmdKeyAttachment
  50. //----------------------------------------------------------------------------------------
  51. CCmdKeyAttachment::CCmdKeyAttachment(LStream *inStream)
  52.     : LAttachment(inStream), mMyControl(nil), mSuperCommander(nil)
  53. {
  54.     mMessage = 814;
  55.  
  56.     inStream->ReadData(&mCommandKey, sizeof(char));
  57.     ::LowerText(&mCommandKey, 1);
  58.     
  59.     // We need to do this to since when LAttachhment's constuctor gets called, any calls
  60.     // to an overidden method will only hit the superclass's method and not ours.
  61.     if ( mOwnerHost != nil ) 
  62.         SetOwnerHostSelf(mOwnerHost);
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // CCmdKeyAttachment::~CCmdKeyAttachment
  67. //----------------------------------------------------------------------------------------
  68. CCmdKeyAttachment::~CCmdKeyAttachment()
  69. {
  70.     // Since we attached ourselves to our control's supercommander, we need
  71.     // to make sure we're not attached to it any more.
  72.     if ( mSuperCommander )
  73.         mSuperCommander->RemoveAttachment(this);
  74. }
  75.     
  76. //----------------------------------------------------------------------------------------
  77. // CCmdKeyAttachment::SetOwnerHost
  78. //----------------------------------------------------------------------------------------
  79. // Overidden so we can do some post processing of the setting.
  80. void CCmdKeyAttachment::SetOwnerHost(LAttachable *inOwnerHost)
  81. {
  82.     LAttachment::SetOwnerHost(inOwnerHost);
  83.     SetOwnerHostSelf(inOwnerHost);
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // CCmdKeyAttachment::SetOwnerHostSelf
  88. //----------------------------------------------------------------------------------------
  89. // This method starts us repeating and makes sure we're attached to a control
  90. void CCmdKeyAttachment::SetOwnerHostSelf(LAttachable *inOwnerHost)
  91. {
  92.     if ( inOwnerHost )
  93.     {
  94.         mMyControl = dynamic_cast<LControl*>(inOwnerHost);
  95.         SignalIf_(mMyControl == nil);        // We had better be attached to an LControl
  96.         
  97.         // Find the first LCommander above us...
  98.         for ( LView* aView = mMyControl->GetSuperView(); aView != nil; aView = aView->GetSuperView() )
  99.         {
  100.             if ( (mSuperCommander = dynamic_cast<LCommander*>(aView)) != nil )
  101.                 break;
  102.         }
  103.         
  104.         // ...and attach ourselves to it. Oh and start ourselves running.
  105.         if ( mSuperCommander )
  106.         {
  107.             mSuperCommander->AddAttachment(this, nil, false);
  108.             StartRepeating();    // start looking for cmd-key down
  109.         }
  110.     }
  111.     else
  112.     {
  113.         // We're being asked to detach, so we can unhook ourselves and stop repeating.
  114.         if ( mSuperCommander) 
  115.         {
  116.             StopRepeating();    // stop looking for cmd-key down
  117.             mSuperCommander->RemoveAttachment(this);
  118.             mSuperCommander = nil;
  119.         }
  120.     }
  121. }
  122.     
  123. //----------------------------------------------------------------------------------------
  124. // CCmdKeyAttachment::SpendTime
  125. //----------------------------------------------------------------------------------------
  126. void CCmdKeyAttachment::SpendTime(const EventRecord& /*inMacEvent*/)
  127. {
  128.     KeyMap        theKeys;
  129.  
  130.     // Check the key map for the state of the command key.
  131.     ::GetKeys(theKeys);    
  132.     Boolean cmdDown = ((theKeys[1] & 0x00008000) == 0x00008000);
  133.  
  134.     EnableCommandKey(cmdDown);
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // CCmdKeyAttachment::ExecuteSelf
  139. //----------------------------------------------------------------------------------------
  140. void CCmdKeyAttachment::ExecuteSelf(MessageT inMessage, void* ioParam)
  141. {
  142.     SignalIf_(inMessage != msg_KeyPress);
  143.  
  144.     EventRecord        *inKeyEvt = (EventRecord*)ioParam;
  145.     
  146.     mExecuteHost = true;
  147.     
  148.     // If the command key is down, the character matches, and the control is active, 
  149.     // then go for it.
  150.     if ( ((inKeyEvt->modifiers & cmdKey) != 0) && 
  151.             mCommandKey == (inKeyEvt->message & charCodeMask) &&
  152.             mMyControl->IsEnabled() ) 
  153.     {
  154.         mMyControl->SimulateHotSpotClick(kControlButtonPart);
  155.         mExecuteHost = false;
  156.     }
  157. }
  158.  
  159.  
  160.  
  161.