home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 20.2 KB | 714 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: BaseShp.cpp
- // Release Version: $ 1.0d11 $
- //
- // Author: Henri Lamiraux
- //
- // Copyright: © 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "ODFDraw.hpp"
-
- #ifndef BASESHP_H
- #include "BaseShp.h"
- #endif
-
- #ifndef UTILS_H
- #include "Utils.h"
- #endif
-
- #ifndef CONSTANT_H
- #include "Constant.h"
- #endif
-
- #ifndef DRAWPART_H
- #include "DrawPart.h"
- #endif
-
- #ifndef DRAWPART_H
- #include "DrawPart.h"
- #endif
-
- #ifndef DRAWFRM_H
- #include "DrawFrm.h"
- #endif
-
- #ifndef DRAWPRXY_H
- #include "DrawPrxy.h"
- #endif
-
- #ifndef DRAWLINK_H
- #include "DrawLink.h"
- #endif
-
- #ifndef DRAWCLIP_H
- #include "DrawClip.h"
- #endif
-
- // ----- Part Layer -----
-
- #ifndef FWFRMING_H
- #include "FWFrming.h"
- #endif
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWITERS_H
- #include "FWIters.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- #ifndef FWTXTBOX_H
- #include "FWTxtBox.h"
- #endif
-
- #ifndef FWLINSHP_H
- #include "FWLinShp.h"
- #endif
-
- #ifndef FWOVLSHP_H
- #include "FWOvlShp.h"
- #endif
-
- #ifndef FWRRCSHP_H
- #include "FWRRcShp.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- //========================================================================================
- // Runtime Information
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfdrawshapes
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- FW_DEFINE_CLASS_M0(CBaseShape)
-
- FW_REGISTER_ARCHIVABLE_CLASS(LBaseShape, CBaseShape, CBaseShape::Read, CBaseShape::Write)
-
- //========================================================================================
- // globals
- //========================================================================================
-
- FW_CRectShape* CBaseShape::gWorkingHandle = NULL;
- unsigned long CBaseShape::gShapeCount = 0;
-
- // [KVV] Constructing gDefaultStyle and gDefaultInk like this causes
- // problems because of order of initializaiton
- // const FW_PStyle gDefaultStyle = FW_kNormalStyle;
- // const FW_PInk gDefaultInk = FW_kNormalInk;
-
- const FW_EStandardStyles gDefaultStyle = FW_kNormalStyle;
- const FW_EStandardInks gDefaultInk = FW_kNormalInk;
-
- //========================================================================================
- // class CBaseShape
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::CBaseShape
- //----------------------------------------------------------------------------------------
-
- CBaseShape::CBaseShape(short numberOfHandles, unsigned short shapeType, unsigned short renderVerb) :
- fSelected(FALSE),
- fNumberOfHandles(numberOfHandles),
- fShapeType(shapeType),
- fPublishLink(NULL),
- fSubscribeLink(NULL),
- fRenderVerb(renderVerb),
- fFrameInk(gDefaultInk),
- fFillInk(gDefaultInk),
- fFrameStyle(gDefaultStyle),
- fFillStyle(gDefaultStyle),
- fPromised(0)
- {
- InitStatic();
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::CBaseShape
- //----------------------------------------------------------------------------------------
-
- CBaseShape::CBaseShape(FW_CReadableStream& archive) :
- fPublishLink(NULL),
- fSubscribeLink(NULL),
- fSelected(FALSE),
- fPromised(0)
- {
- InitStatic();
-
- archive >> fShapeType;
- archive >> fNumberOfHandles;
-
- archive >> fFrameInk;
- archive >> fFillInk;
- archive >> fFrameStyle;
- archive >> fFillStyle;
-
- archive >> fRenderVerb;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::InitStatic
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::InitStatic()
- {
- if (gShapeCount == 0)
- {
- CBaseShape::gWorkingHandle = FW_NEW(FW_CRectShape, (FW_kZeroRect, FW_kFill));
- CBaseShape::gWorkingHandle->SetInk(FW_kInvertInk);
- }
-
- CBaseShape::gShapeCount++;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::~CBaseShape
- //----------------------------------------------------------------------------------------
-
- CBaseShape::~CBaseShape()
- {
- FW_ASSERT(fPromised == 0);
-
- CBaseShape::gShapeCount--;
- if (CBaseShape::gShapeCount == 0)
- delete CBaseShape::gWorkingHandle;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Removed
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::Removed(Environment* ev)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Deleted
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::Deleted(Environment* ev)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::CheckPromise
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::CheckPromise(Environment* ev, FW_CPart* part)
- {
- if (fPromised != 0)
- part->ResolveAllPromises(ev); // I promised only one so...
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Promised
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::Promised(FW_EStorageKinds storageKind, FW_Boolean state)
- {
- if (state)
- {
- FW_ASSERT((fPromised & storageKind) == 0);
- fPromised |= storageKind;
- }
- else
- {
- FW_ASSERT((fPromised & storageKind) != 0);
- fPromised ^= fPromised & storageKind;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::TrackFeedback
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::TrackFeedback(Environment* ev,
- ODFacet* facet,
- FW_CGraphicContext& gc,
- const FW_CPoint& anchorPoint,
- const FW_CPoint& currentPoint,
- FW_Boolean erase)
- {
- if (!erase)
- SetShapeGeometry(anchorPoint, currentPoint);
-
- RenderShape(ev, facet, gc);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::SelectShape
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::SelectShape(Environment* ev, FW_Boolean state)
- {
- fSelected = state;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ClearCache
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ClearCache()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::RenderHandles
- //----------------------------------------------------------------------------------------
- // if asynchFacet is != NULL it means we are drawing asynchronously in a facet
-
- void CBaseShape::RenderHandles(FW_CGraphicContext& gc)
- {
- for (short i=1; i<=fNumberOfHandles; i++)
- {
- CalcHandle(i, CBaseShape::gWorkingHandle);
- CBaseShape::gWorkingHandle->Render(gc);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::CalcHandle
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::CalcHandle(short whichHandle, FW_CRectShape* handle) const
- {
- FW_CPoint pt;
- GetHandleCenter(whichHandle, pt);
-
- const FW_CFixed fx25 = FW_DoubleToFixed(2.5);
- FW_CRect rect(
- pt.x - fx25, pt.y - fx25, pt.x + fx25, pt.y + fx25);
-
- handle->SetRectangle(rect);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::InSelectionRect
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CBaseShape::InSelectionRect(const FW_CRect& selectRect) const
- {
- FW_CRect bounds;
- GetRectGeometry(bounds);
-
- FW_CRect temp(bounds);
- temp.Intersection(selectRect);
-
- return bounds == temp;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Flatten
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::Flatten(FW_CWritableStream& archive)
- {
- archive << fShapeType;
- archive << fNumberOfHandles;
-
- archive << fFrameInk;
- archive << fFillInk;
- archive << fFrameStyle;
- archive << fFillStyle;
-
- archive << fRenderVerb;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::MovedAfter
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::MovedAfter(Environment* ev, CBaseShape* shape)
- {
- FW_UNUSED(shape);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::MovedBefore
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::MovedBefore(Environment* ev, CBaseShape* shape)
- {
- FW_UNUSED(shape);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::MovedFirst
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::MovedFirst(Environment* ev)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::MovedLast
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::MovedLast(Environment* ev)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::RestoreShape
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::RestoreShape(Environment *ev, CDrawPart* drawPart)
- {
- drawPart->AddShape(ev, this);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::SetFrozen
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CBaseShape::SetFrozen(FW_Boolean state)
- {
- FW_UNUSED(state);
- return FALSE; // Means I don't care
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::IsFrozen
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CBaseShape::IsFrozen() const
- {
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetPenSize
- //----------------------------------------------------------------------------------------
-
- FW_CFixed CBaseShape::GetPenSize() const
- {
- return HasFrameStyle() ? fFrameStyle->GetPenSize() : FW_kFixed0;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetColor
- //----------------------------------------------------------------------------------------
- void CBaseShape::GetColor(FW_Boolean fill, FW_CColor& color)
- {
- if (fill)
- fFillInk->GetForeColor(color);
- else
- fFrameInk->GetForeColor(color);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetPattern
- //----------------------------------------------------------------------------------------
- FW_PPattern CBaseShape::GetPattern(FW_Boolean fill)
- {
- if (fill)
- return fFillStyle->GetPattern();
- else
- return fFrameStyle->GetPattern();
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::UnshareInk
- //----------------------------------------------------------------------------------------
-
- FW_PInk CBaseShape::UnshareInk(const FW_PInk& ink) const
- {
- // Do not use the following syntax. It looks like the life time of temporaries
- // is wrong causing a bad refcounting problem
- // return (ink->GetRefCount() > 1) ? ink->Copy() : ink;
-
- if (ink->GetRefCount() > 1)
- return ink->Copy();
- else
- return ink;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::UnshareStyle
- //----------------------------------------------------------------------------------------
-
- FW_PStyle CBaseShape::UnshareStyle(const FW_PStyle& style) const
- {
- // Do not use the following syntax. It looks like the life time of temporaries
- // is wrong causing a bad refcounting problem
- // return (style->GetRefCount() > 1) ? style->Copy() : style;
-
- if (style->GetRefCount() > 1)
- return style->Copy();
- else
- return style;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangeFrameColor
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangeFrameColor(Environment* ev, CDrawPart* part, const FW_CColor& color)
- {
- if (HasFrameStyle())
- {
- CheckPromise(ev, part);
-
- fFrameInk = UnshareInk(fFrameInk);
- fFrameInk->SetForeColor(color);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangeFramePattern
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangeFramePattern(Environment* ev, CDrawPart* part, const FW_PPattern& pattern)
- {
- if (HasFrameStyle())
- {
- CheckPromise(ev, part);
-
- fFrameStyle = UnshareStyle(fFrameStyle);
- fFrameStyle->SetPattern(pattern);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangeFillColor
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangeFillColor(Environment* ev, CDrawPart* part, const FW_CColor& color)
- {
- if (HasFillStyle())
- {
- CheckPromise(ev, part);
-
- fFillInk = UnshareInk(fFillInk);
- fFillInk->SetForeColor(color);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangeFillPattern
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangeFillPattern(Environment* ev, CDrawPart* part, const FW_PPattern& pattern)
- {
- if (HasFillStyle())
- {
- CheckPromise(ev, part);
-
- fFillStyle = UnshareStyle(fFillStyle);
- fFillStyle->SetPattern(pattern);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangePenSize
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangePenSize(Environment* ev, CDrawPart* part, FW_CFixed newPenSize)
- {
- if (HasFrameStyle())
- {
- CheckPromise(ev, part);
-
- ClearCache();
-
- fFrameStyle = UnshareStyle(fFrameStyle);
- fFrameStyle->SetPenSize(newPenSize);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::SetSubscribeLink
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::SetSubscribeLink(Environment* ev, CDrawSubscribeLink *subscribeLink)
- {
- FW_UNUSED(ev);
-
- fSubscribeLink = subscribeLink;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::SetPublishLink
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::SetPublishLink(Environment* ev, CDrawPublishLink *publishLink)
- {
- FW_UNUSED(ev);
-
- fPublishLink = publishLink;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::AdjustRectForPenSize
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::AdjustRectForPenSize(FW_CRect& rect, FW_CFixed penSize) const
- {
- FW_CFixed half = penSize.Half();
- rect.Inset(-half, -half);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::ChangeRenderVerb
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::ChangeRenderVerb(Environment* ev, CDrawPart* part, unsigned short renderVerb)
- {
- CheckPromise(ev, part);
-
- ClearCache();
- fRenderVerb = renderVerb;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Read
- //----------------------------------------------------------------------------------------
-
- void* CBaseShape::Read(FW_CReadableStream& archive)
- {
- FW_UNUSED(archive);
- FW_DEBUG_MESSAGE("CBaseShape::Read should never be called");
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::Write
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::Write(FW_CWritableStream& archive, const void* objectRep)
- {
- ((CBaseShape*)objectRep)->Flatten(archive);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetUpdateBox
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::GetUpdateBox(Environment* ev, ODShape* updateBox) const
- {
- FW_CRect bounds;
- GetRectGeometry(bounds);
-
- const FW_CFixed fxMargin = FW_IntToFixed(3);
-
- bounds.left -= fxMargin;
- bounds.top -= fxMargin;
- bounds.right += fxMargin;
- bounds.bottom += fxMargin;
-
- ODRect odBounds = bounds;
- updateBox->SetRectangle(ev, &odBounds);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetUpdateBox
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::GetUpdateBox(FW_CRect& updateBox) const
- {
- GetRectGeometry(updateBox);
-
- const FW_CFixed fxMargin = FW_IntToFixed(3);
-
- updateBox.left -= fxMargin;
- updateBox.top -= fxMargin;
- updateBox.right += fxMargin;
- updateBox.bottom += fxMargin;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetDragRect
- //----------------------------------------------------------------------------------------
-
- void CBaseShape::GetDragRect(FW_CRect& dragRect) const
- {
- GetRectGeometry(dragRect);
- AdjustRectForPenSize(dragRect, GetPenSize());
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::GetResizeStyle
- //----------------------------------------------------------------------------------------
-
- FW_PStyle CBaseShape::GetResizeStyle() const
- {
- FW_PStyle resizeStyle(GetPenSize(), FW_kGrayPat);
- return resizeStyle;
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::SubtractToWindowClip
- //----------------------------------------------------------------------------------------
- // ATTENTION: windowClip is in Window coordinates. tempShape is just a working shape. It is
- // allocated by CDrawFrame::ClipEmbeddedFacets so I don't have to allocate one everytime.
-
- void CBaseShape::SubtractToWindowClip(Environment *ev,
- CDrawFacetClipper* facetClipper,
- ODFacet* containingFacet,
- ODShape* windowClip,
- ODShape* tempShape)
- {
- FW_UNUSED(facetClipper);
-
- GetClipRegion(ev, tempShape);
- FW_CAcquiredODTransform aqWindowContentTransform = containingFacet->AcquireWindowContentTransform(ev, NULL);
- tempShape->Transform(ev, aqWindowContentTransform); // in window coordinate
- windowClip->Subtract(ev, tempShape);
- }
-
- //----------------------------------------------------------------------------------------
- // CBaseShape::WhichHandle
- //----------------------------------------------------------------------------------------
-
- short CBaseShape::WhichHandle(FW_CGraphicContext& gc, const FW_CPoint& mouse) const
- {
- for (short i=1; i<=fNumberOfHandles; i++)
- {
- CalcHandle(i, CBaseShape::gWorkingHandle);
- if (CBaseShape::gWorkingHandle->HitTest(gc, mouse, FW_IntToFixed(0)))
- return i;
- }
-
- return 0;
- }
-
-