home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-16 | 7.3 KB | 344 lines | [TEXT/CWIE] |
- // =================================================================================
- //
- // CHLinkControl.cpp ©1996 Microsoft Corporation All rights reserved.
- //
- // =================================================================================
-
- #include "ocheaders.h"
- #include "CCFragResource.h"
- #include "CHLinkControl.h"
-
-
- const Int16 OvalSize = 20;
- const Int16 FingerCursorResID = 128;
-
- #pragma mark === CHLinkControl::Construction & Destruction ===
-
- //
- // CHLinkControl::CHLinkControl
- //
-
- CHLinkControl::CHLinkControl(void) : CBaseControl()
- {
- mHLinkP = nil;
- mTargetURL[0] = 0;
- mLinkType = BeginHLinkNavigate;
- mIDP = nil;
-
- // Get the finger cursor
- {
- CCFragResource ResourceSetter; // Sets us to the control's resource file
-
- mFingerCursor = GetCursor(FingerCursorResID);
- if ( mFingerCursor )
- DetachResource((Handle) mFingerCursor);
- }
- // Resource file is reset when ResourceSetter goes out of scope
- }
-
- //
- // CHLinkControl::~CHLinkControl
- //
-
- CHLinkControl::~CHLinkControl(void)
- {
- if ( mHLinkP )
- mHLinkP->Release();
-
- if ( mFingerCursor )
- DisposeHandle((Handle) mFingerCursor);
-
- if ( mIDP )
- delete [] mIDP;
- }
-
- #pragma mark === CHLinkControl::IObjectWithSite ===
-
- //=--------------------------------------------------------------------------=
- // CHLinkControl::IObjectWithSite::SetSite
- //=--------------------------------------------------------------------------=
- // informs the control of it's client site display
- // location within it's container
- //
- STDMETHODIMP
- CHLinkControl::SetSite(IUnknown* inClientSite)
- {
- CBaseControl::SetSite(inClientSite);
-
- // Get the hyperlink interface
- if ( inClientSite )
- {
- if ( mHLinkP )
- mHLinkP->Release();
- mUnkOuterP->QueryInterface(IID_IHLinkBasic, &mHLinkP);
- }
-
- return S_OK;
-
- }
-
-
- #pragma mark === CHLinkControl::IControl ===
-
- //
- // CHLinkControl::IControl::Draw
- //
-
- STDMETHODIMP
- CHLinkControl::Draw( DrawContext* inContext)
- {
- Str255 Name;
- Int16 NameWidth, NameHeight, ButtonWidth, ButtonHeight;
- Rect ButtonRect = inContext->Location;
-
- // Position the Button rect
- InsetRect(&ButtonRect, 5, 5);
-
- // Erase the area
- EraseRoundRect(&ButtonRect, OvalSize, OvalSize);
-
- // Frame the button
- PenSize(2, 2);
- FrameRoundRect(&ButtonRect, OvalSize, OvalSize);
- PenSize(1, 1);
-
- // Center the text
- ::TextFont(applFont);
- ::TextFace(0);
- if ( ButtonEnabled() )
- ::TextMode(srcCopy);
- else
- ::TextMode(grayishTextOr);
- ::TextSize(12);
- GetID(255, (Char8*)Name+1);
- *Name = strlen((Char8*)Name+1);
- NameWidth = ::StringWidth(Name);
- NameHeight = 12;
- ButtonWidth = ButtonRect.right - ButtonRect.left;
- ButtonHeight = ButtonRect.bottom - ButtonRect.top;
- ::MoveTo(ButtonRect.left+ ((ButtonWidth - NameWidth) / 2),
- ButtonRect.top + ((ButtonHeight - NameHeight) / 2 + NameHeight));
-
- // Draw the name
- ::DrawString(Name);
-
- return S_OK;
- }
-
-
- //
- // CHLinkControl::IControl::GetID
- //
-
- STDMETHODIMP
- CHLinkControl::GetID(Int32 inBufferSize, Char8* outID)
- {
- if (mIDP)
- {
- Int16 IDLen = strlen(mIDP);
-
- if (IDLen > --inBufferSize)
- IDLen = inBufferSize;
- ::BlockMove(mIDP, outID, IDLen);
- *(outID + IDLen) = '\0';
- }
- else
- return CBaseControl::GetID(inBufferSize, outID);
-
- return S_OK;
- }
-
-
- //
- // CHLinkControl::IControl::DoMouse
- //
- STDMETHODIMP
- CHLinkControl::DoMouse(MouseEventType inMouseET, PlatformEvent* inEvent)
- {
- #pragma unused (inEvent)
-
- if ( ButtonEnabled() )
- {
- switch (inMouseET)
- {
- case MouseEnter:
- SetCursor(*mFingerCursor);
- break;
-
- case MouseLeave:
- // Hope the app sets it back to its cursor
- break;
-
- case MouseDown:
- {
- DrawContext Context = {BeginPortType};
-
- if ( mContainerSiteP->AcquireContext(mActiveContext->GetContextID(), &Context) == S_OK )
- {
- Rect ButtonRect = Context.Location;
-
- // Position the Button rect
- ::InsetRect(&ButtonRect, 5, 5);
-
- // Do the button behavior
- ::InvertRoundRect(&ButtonRect, OvalSize, OvalSize);
- while (StillDown()) ;
- ::InvertRoundRect(&ButtonRect, OvalSize, OvalSize);
-
- // Release the context
- mContainerSiteP->ReleaseContext(&Context);
- }
-
- break;
- }
-
- case MouseUp:
- switch (mLinkType)
- {
- case NavigateURL:
- mHLinkP->GotoURL(TargetCurrent, (char*)mTargetURL);
- break;
-
- case NavigateBack:
- mHLinkP->GoBack();
- break;
-
- case NavigateForward:
- mHLinkP->GoForward();
- break;
- }
- break;
- }
- }
- else if ( inMouseET == MouseDown )
- ::SysBeep(60);
-
- return S_OK;
- }
-
- //
- // CHLinkControl::ButtonEnabled
- //
- Boolean CHLinkControl::ButtonEnabled(void)
- {
- Boolean Enabled = false;
-
- if ( mHLinkP )
- {
- switch (mLinkType)
- {
- case NavigateURL:
- Enabled = ( strlen((char*) mTargetURL) > 0 );
- break;
-
- case NavigateBack:
- Enabled = ( mHLinkP->CanGoBack() == S_OK );
- break;
-
- case NavigateForward:
- Enabled = ( mHLinkP->CanGoForward() == S_OK );
- break;
- }
- }
-
-
- return Enabled;
-
- }
-
-
- #pragma mark === CHLinkControl::IPersistPropertyBag ===
-
- //
- // CHLinkControl::IPersistPropertyBag::Load
- //
-
- STDMETHODIMP
- CHLinkControl::Load(IPropertyBag* PropertyBag, IErrorLog* ErrorLog)
- {
-
- LoadTextState(PropertyBag, ErrorLog);
-
- return S_OK;
- }
-
- //=--------------------------------------------------------------------------=
- // CHLinkControl::LoadTextState
- //=--------------------------------------------------------------------------=
- // load in our text state for this control.
- //
- // Parameters:
- // IPropertyBag * - [in] property bag to read from
- // IErrorLog * - [in] errorlog object to use with proeprty bag
- //
- // Output:
- // ErrorCode
- //
- STDMETHODIMP CHLinkControl::LoadTextState(IPropertyBag *PropertyBag, IErrorLog *ErrorLog)
- {
- VARIANT v;
- Uint32 length;
-
- // VariantInit(&v);
-
- v.vt = VT_BSTR;
- v.bstrVal = NULL;
-
- // try to load in the property. if we can't get it, then leave
- // things at their default.
- //
- PropertyBag->Read("target", &v, ErrorLog);
- if (v.bstrVal)
- {
- length = *((Uint32*) v.bstrVal) ;
- strcpy((Char8*)mTargetURL, v.bstrVal + sizeof(Uint32));
- CoTaskMemFree(v.bstrVal);
- // VariantInit(&v);
- }
-
- v.vt = VT_BSTR;
- v.bstrVal = NULL;
-
- // try to load in the property. if we can't get it, then leave
- // things at their default.
- //
- PropertyBag->Read("id", &v, ErrorLog);
- if (v.bstrVal)
- {
- length = *((Uint32*) v.bstrVal) ;
- if (mIDP)
- delete [] mIDP;
- mIDP = new char[length + 1];
- strcpy((Char8*) mIDP, v.bstrVal + sizeof(Uint32));
- CoTaskMemFree(v.bstrVal);
- // VariantInit(&v);
- }
-
- v.vt = VT_BSTR;
- v.bstrVal = NULL;
-
- // try to load in the property. if we can't get it, then leave
- // things at their default.
- //
- PropertyBag->Read("linktype", &v, ErrorLog);
- if (v.bstrVal)
- {
- char LinkTypeStr[32];
-
- length = *((Uint32*) v.bstrVal) ;
- strcpy((Char8*) LinkTypeStr, v.bstrVal + sizeof(Uint32));
- if ( strcmp(LinkTypeStr, "goto") == 0 )
- mLinkType = NavigateURL;
- else if ( strcmp(LinkTypeStr, "goback") == 0 )
- mLinkType = NavigateBack;
- else if ( strcmp(LinkTypeStr, "goforward") == 0 )
- mLinkType = NavigateForward;
-
- CoTaskMemFree(v.bstrVal);
- // VariantInit(&v);
- }
-
- return S_OK;
- }
-
-