home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-01-31 | 5.9 KB | 161 lines | [TEXT/R*ch] |
- // ===========================================================================
- // File: CCmdKeyAttachment.cp
- // Version: 1.0.1 - Jan 31, 1997
- // Author: Mike Shields (mshields@inconnect.com)
- //
- // Copyright ©1996 Mike Shields. All rights reserved.
- // I hereby grant users of CCmdKeyAttachment permission to use it (or any modified
- // version of it) in applications (or any other type of Macintosh software
- // like extensions -- freeware, shareware, commercial, or other) for free,
- // subject to the terms that:
- //
- // (1) This agreement is non-exclusive.
- //
- // (2) I, Mike Shields, retain the copyright to the original source code.
- //
- // These two items are the only required conditions for use. However, I do have
- // an additional request. Note, however, that this is only a request, and
- // that it is not a required condition for use of this code.
- //
- // (1) That I be given credit for CCmdKeyAttachment code in the copyrights or
- // acknowledgements section of your manual or other appropriate documentation.
- //
- //
- // I would like to repeat that this last item is only a request. You are prefectly
- // free to choose not to do any or all of them.
- //
- // This source code is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- // ===========================================================================
- // CCmdKeyAttachment.h <- double-click + Command-D to see class declaration
- //
- // Class which handles drawing and responding to command keys when it is attached
- // to a button
-
- #include "CCmdKeyAttachment.h"
- #include <LStream.h>
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::CCmdKeyAttachment
- //----------------------------------------------------------------------------------------
- CCmdKeyAttachment::CCmdKeyAttachment()
- : mMyControl(nil), mSuperCommander(nil), mCommandKey(0)
- {
- mMessage = 814;
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::CCmdKeyAttachment
- //----------------------------------------------------------------------------------------
- CCmdKeyAttachment::CCmdKeyAttachment(LStream *inStream)
- : LAttachment(inStream), mMyControl(nil), mSuperCommander(nil)
- {
- mMessage = 814;
-
- inStream->ReadData(&mCommandKey, sizeof(char));
- ::LowerText(&mCommandKey, 1);
-
- // We need to do this to since when LAttachhment's constuctor gets called, any calls
- // to an overidden method will only hit the superclass's method and not ours.
- if ( mOwnerHost != nil )
- SetOwnerHostSelf(mOwnerHost);
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::~CCmdKeyAttachment
- //----------------------------------------------------------------------------------------
- CCmdKeyAttachment::~CCmdKeyAttachment()
- {
- // Since we attached ourselves to our control's supercommander, we need
- // to make sure we're not attached to it any more.
- if ( mSuperCommander )
- mSuperCommander->RemoveAttachment(this);
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::SetOwnerHost
- //----------------------------------------------------------------------------------------
- // Overidden so we can do some post processing of the setting.
- void CCmdKeyAttachment::SetOwnerHost(LAttachable *inOwnerHost)
- {
- LAttachment::SetOwnerHost(inOwnerHost);
- SetOwnerHostSelf(inOwnerHost);
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::SetOwnerHostSelf
- //----------------------------------------------------------------------------------------
- // This method starts us repeating and makes sure we're attached to a control
- void CCmdKeyAttachment::SetOwnerHostSelf(LAttachable *inOwnerHost)
- {
- if ( inOwnerHost )
- {
- mMyControl = dynamic_cast<LControl*>(inOwnerHost);
- SignalIf_(mMyControl == nil); // We had better be attached to an LControl
-
- // Find the first LCommander above us...
- for ( LView* aView = mMyControl->GetSuperView(); aView != nil; aView = aView->GetSuperView() )
- {
- if ( (mSuperCommander = dynamic_cast<LCommander*>(aView)) != nil )
- break;
- }
-
- // ...and attach ourselves to it. Oh and start ourselves running.
- if ( mSuperCommander )
- {
- mSuperCommander->AddAttachment(this, nil, false);
- StartRepeating(); // start looking for cmd-key down
- }
- }
- else
- {
- // We're being asked to detach, so we can unhook ourselves and stop repeating.
- if ( mSuperCommander)
- {
- StopRepeating(); // stop looking for cmd-key down
- mSuperCommander->RemoveAttachment(this);
- mSuperCommander = nil;
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::SpendTime
- //----------------------------------------------------------------------------------------
- void CCmdKeyAttachment::SpendTime(const EventRecord& /*inMacEvent*/)
- {
- KeyMap theKeys;
-
- // Check the key map for the state of the command key.
- ::GetKeys(theKeys);
- Boolean cmdDown = ((theKeys[1] & 0x00008000) == 0x00008000);
-
- EnableCommandKey(cmdDown);
- }
-
- //----------------------------------------------------------------------------------------
- // CCmdKeyAttachment::ExecuteSelf
- //----------------------------------------------------------------------------------------
- void CCmdKeyAttachment::ExecuteSelf(MessageT inMessage, void* ioParam)
- {
- SignalIf_(inMessage != msg_KeyPress);
-
- EventRecord *inKeyEvt = (EventRecord*)ioParam;
-
- mExecuteHost = true;
-
- // If the command key is down, the character matches, and the control is active,
- // then go for it.
- if ( ((inKeyEvt->modifiers & cmdKey) != 0) &&
- mCommandKey == (inKeyEvt->message & charCodeMask) &&
- mMyControl->IsEnabled() )
- {
- mMyControl->SimulateHotSpotClick(kControlButtonPart);
- mExecuteHost = false;
- }
- }
-
-
-
-