home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UView.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  80.4 KB  |  3,026 lines  |  [TEXT/MPS ]

  1. // UView.cp
  2. // Copyright © 1987-1991 by Apple Computer Inc.    All rights reserved.
  3.  
  4. #ifndef __STDIO__
  5. #include <StdIo.h>
  6. #endif
  7.  
  8. #ifndef __UGEOMETRY__
  9. #include <UGeometry.h>
  10. #endif
  11.  
  12. #ifndef __ULIST__
  13. #include <UList.h>
  14. #endif
  15.  
  16. #ifndef __UEVENT__
  17. #include <UEvent.h>
  18. #endif
  19.  
  20. #ifndef __UCOMMAND__
  21. #include <UCommand.h>
  22. #endif
  23.  
  24. #ifndef __EDITIONS__
  25. #include <Editions.h>
  26. #endif
  27.  
  28. #ifndef __DIALOGS__
  29. #include <Dialogs.h>
  30. #endif
  31.  
  32. #ifndef __UAPPLICATION__
  33. #include <UApplication.h>
  34. #endif
  35.  
  36. #ifndef __UVIEWBEHAVIOR__
  37. #include <UViewBehavior.h>
  38. #endif
  39.  
  40. #ifndef __UDOCUMENT__
  41. #include <UDocument.h>
  42. #endif
  43.  
  44. #ifndef __SCRAP__
  45. #include <Scrap.h>
  46. #endif
  47.  
  48. #ifndef __UCLIPBOARDMGR__
  49. #include <UClipboardMgr.h>
  50. #endif
  51.  
  52. #ifndef __UPRINTHANDLER__
  53. #include <UPrintHandler.h>
  54. #endif
  55.  
  56. #ifndef __UWINDOW__
  57. #include <UWindow.h>
  58. #endif
  59.  
  60. #ifndef __USCROLLER__
  61. #include <UScroller.h>
  62. #endif
  63.  
  64. #ifndef __UFAILURE__
  65. #include <UFailure.h>
  66. #endif
  67.  
  68. #ifndef __UMACAPPUTILITIES__
  69. #include <UMacAppUtilities.h>
  70. #endif
  71.  
  72. #ifndef __UPATCH__
  73. #include <UPatch.h>
  74. #endif
  75.  
  76. #ifndef __UMEMORY__
  77. #include <UMemory.h>
  78. #endif
  79.  
  80. #ifndef __UMACAPPGLOBALS__
  81. #include <UMacAppGlobals.h>
  82. #endif
  83.  
  84. #ifndef __ERRORS__
  85. #include <Errors.h>
  86. #endif
  87.  
  88. #ifndef __TOOLUTILS__
  89. #include <ToolUtils.h>
  90. #endif
  91.  
  92. #ifndef __UVIEW__
  93. #include <UView.h>
  94. #endif
  95.  
  96. // global declarations for globals defined in interface
  97. TView* gFocusedView;
  98. Boolean gDrawingPictScrap;
  99. TView* gDrawingPictScrapView;
  100. Boolean gPrinting;
  101. TPrintHandler* gCurrPrintHandler;
  102. TPrintHandler* gNullPrintHandler;
  103. VPoint gPageOffset;
  104. TPrintHandler* gPrintHandler;
  105. IDType gWResSignature;
  106. Str255 gWResType;
  107.  
  108. const unsigned char kFrame = adnLineTop | adnLineLeft | adnLineBottom | adnLineRight;
  109.  
  110. //--------------------------------------------------------------------------------------------------
  111. #pragma segment MAViewRes
  112.  
  113. CSubViewIterator::CSubViewIterator(TView* itsView,
  114.                                    ArrayIndex itsLowBound,
  115.                                    ArrayIndex itsHighBound,
  116.                                    Boolean itsForward) :
  117.     CObjectIterator(itsView ? itsView->fSubViews : NULL, itsLowBound, itsHighBound, itsForward)
  118. {
  119. }
  120.  
  121. //--------------------------------------------------------------------------------------------------
  122. #pragma segment MAViewRes
  123.  
  124. CSubViewIterator::CSubViewIterator(TView* itsView,
  125.                                    Boolean itsForward) :
  126.     CObjectIterator(itsView ? itsView->fSubViews : NULL, itsForward)
  127. {
  128. }
  129.  
  130. //--------------------------------------------------------------------------------------------------
  131. #pragma segment MAViewRes
  132.  
  133. CSubViewIterator::CSubViewIterator(TView* itsView) :
  134.     CObjectIterator(itsView ? itsView->fSubViews : NULL)
  135. {
  136. }
  137.  
  138. //--------------------------------------------------------------------------------------------------
  139. #pragma segment MAViewRes
  140.  
  141. TView* CSubViewIterator::CurrentSubView(void)
  142. {
  143.     return (TView*)this->CurrentObject();
  144. }
  145.  
  146. //--------------------------------------------------------------------------------------------------
  147. #pragma segment MAViewRes
  148.  
  149. TView* CSubViewIterator::FirstSubView(void)
  150. {
  151.     return (TView*)this->FirstObject();
  152. }
  153.  
  154. //--------------------------------------------------------------------------------------------------
  155. #pragma segment MAViewRes
  156.  
  157. TView* CSubViewIterator::NextSubView(void)
  158. {
  159.     return (TView*)this->NextObject();
  160. }
  161.  
  162. //--------------------------------------------------------------------------------------------------
  163. #pragma segment MAGlobalsRes
  164.  
  165. pascal void VisibleRect(Rect& r)
  166. {
  167. #if qDebug
  168.     UseTempRgn("VisibleRect");
  169. #endif
  170.  
  171.     RectRgn(gTempRgn, r);
  172.     SectRgn(gTempRgn, qd.thePort->visRgn, gTempRgn);
  173.     r = (*gTempRgn)->rgnBBox;
  174. #if qDebug
  175.     DoneWithTempRgn();
  176. #endif
  177.  
  178. }
  179.  
  180. //--------------------------------------------------------------------------------------------------
  181. #pragma segment MAGlobalsRes
  182.  
  183. pascal void DrawableRect(Rect& r)
  184. {
  185.     VisibleRect(r);
  186. #if qDebug
  187.     UseTempRgn("VisibleRect");
  188. #endif
  189.  
  190.     RectRgn(gTempRgn, r);
  191.     SectRgn(gTempRgn, qd.thePort->clipRgn, gTempRgn);
  192.     r = (*gTempRgn)->rgnBBox;
  193. #if qDebug
  194.     DoneWithTempRgn();
  195. #endif
  196.  
  197. }
  198.  
  199. //--------------------------------------------------------------------------------------------------
  200. #pragma segment MAGlobalsRes
  201.  
  202. pascal void PtAndRgn(Point aPoint,
  203.                      RgnHandle aRegion)
  204. {
  205.     //!!! Error checking? 
  206. #if qDebug
  207.     UseTempRgn("PtAndRgn");
  208. #endif
  209.  
  210.     SetRectRgn(gTempRgn, aPoint.h, aPoint.v, aPoint.h + 1, aPoint.v + 1);
  211.     UnionRgn(gTempRgn, aRegion, aRegion);
  212.  
  213. #if qDebug
  214.     DoneWithTempRgn();
  215. #endif
  216.  
  217. }
  218.  
  219. //--------------------------------------------------------------------------------
  220. #pragma segment MAGlobalsRes
  221.  
  222. pascal void VRectAndRgn(const VRect& theVRect,
  223.                         RgnHandle theRgn)
  224. {
  225. #if qDebug
  226.     UseTempRgn("VRectAndRgn");
  227. #endif
  228.  
  229.     RectRgn(gTempRgn, theVRect);
  230.     UnionRgn(gTempRgn, theRgn, theRgn);
  231.  
  232. #if qDebug
  233.     DoneWithTempRgn();
  234. #endif
  235. }
  236.  
  237. //--------------------------------------------------------------------------------------------------
  238. #pragma segment MAViewRes
  239.  
  240. pascal ViewRsrcHandle NewViewRsrc(Ptr& p)
  241. {
  242.     ViewRsrcHandle aHandle = (ViewRsrcHandle)(NewPermHandle(kViewRsrcExpandAmt));
  243.     LockHandleHigh((Handle)aHandle);
  244.  
  245.     (*aHandle)->numViews = 0;
  246.     p = (Ptr) & ((*aHandle)->theViews);
  247.  
  248.     return aHandle;
  249. }
  250.  
  251. //--------------------------------------------------------------------------------------------------
  252. #pragma segment MAOpen
  253.  
  254. pascal void OffsetPtr(Ptr& p,
  255.                       long offset)
  256. {
  257.     p += offset;
  258.     if (((long)p) & 1)
  259.         ++p;
  260. }
  261.  
  262. //--------------------------------------------------------------------------------------------------
  263. #pragma segment MAOpen
  264.  
  265. pascal void OffsetPtrWStr(Ptr& p,
  266.                           long offset)
  267. {
  268.     OffsetPtr(p, offset - 255 + (*(StringPtr)(p + offset - 256)).Length());
  269. }
  270.  
  271. //--------------------------------------------------------------------------------------------------
  272. #pragma segment MAViewRes
  273.  
  274. pascal void DoneViewRsrc(Handle viewRsrc,
  275.                          long lastPtr)
  276. {
  277.     HUnlock(viewRsrc);
  278.     SetPermHandleSize(viewRsrc, StripLong((void*)lastPtr) - StripLong((*viewRsrc)));
  279. }
  280.  
  281. //--------------------------------------------------------------------------------------------------
  282. #pragma segment MAViewRes
  283.  
  284. pascal Ptr ExpandPtr(Handle viewRsrc,
  285.                      Ptr& p,
  286.                      long offset)
  287. {
  288.     Ptr returnValue;
  289.  
  290.     Size rsrcSize = GetHandleSize(viewRsrc);
  291.     long rsrcBase = StripLong((*viewRsrc));
  292.     long currentPtr = StripLong(p);
  293.  
  294.     if (offset & 1)
  295.         ++offset;
  296.     long desiredEnd = currentPtr + offset + sizeof(short);
  297.  
  298.     if (desiredEnd >= rsrcBase + rsrcSize)
  299.     {
  300.         // This appropriation logic might need some re-examination.  If the size of the added
  301.         // template is larger than the minimum amount, then simply the size is added.  If
  302.         // the handle is already near to being full, this won't help for the next allocation.
  303.         // Maybe it should use a hystersis?…
  304.         long oldOffset = currentPtr - rsrcBase;
  305.         HUnlock(viewRsrc);
  306.         SetHandleSize(viewRsrc, rsrcSize + Max(kViewRsrcExpandAmt, offset));
  307.         FailMemError();
  308.         LockHandleHigh(viewRsrc);
  309.         p = (Ptr)((*viewRsrc)) + oldOffset;
  310.     }
  311.  
  312.     returnValue = p;
  313.     OffsetPtr(p, offset);
  314.     return (returnValue);
  315. }
  316.  
  317. //--------------------------------------------------------------------------------------------------
  318. #pragma segment MAViewRes
  319.  
  320. pascal Ptr ExpandPtrWStr(Handle viewRsrc,
  321.                          Ptr& p,
  322.                          long offset,
  323.                          const long len)
  324. {
  325.     return ExpandPtr(viewRsrc, p, offset - 255 + len);
  326. }
  327.  
  328. //--------------------------------------------------------------------------------------------------
  329. #pragma segment MAViewRes
  330.  
  331. pascal Boolean IsPtVisible(Point pt)
  332. {
  333.     return PtInRgn(pt, qd.thePort->visRgn);
  334. }
  335.  
  336. //--------------------------------------------------------------------------------------------------
  337. #pragma segment MAViewRes
  338.  
  339. pascal Boolean IsPtDrawable(Point pt)
  340. {
  341.     return IsPtVisible(pt) && PtInRgn(pt, qd.thePort->clipRgn);
  342. }
  343.  
  344. //--------------------------------------------------------------------------------------------------
  345. #pragma segment MAViewRes
  346.  
  347. pascal Boolean IsRectVisible(const Rect& r)
  348. {
  349.     return RectInRgn(r, qd.thePort->visRgn);
  350. }
  351.  
  352. //--------------------------------------------------------------------------------------------------
  353. #pragma segment MAViewRes
  354.  
  355. pascal Boolean IsRectDrawable(const Rect& r)
  356. {
  357.     return IsRectVisible(r) && RectInRgn(r, qd.thePort->clipRgn);
  358. }
  359.  
  360. //--------------------------------------------------------------------------------------------------
  361. #pragma segment MAViewRes
  362.  
  363. pascal void GetFocus(FocusRec& theFocusRec)
  364. {
  365.     GetViewPortInfo(theFocusRec.itsViewPortInfo);
  366.  
  367.     theFocusRec.focusedView = gFocusedView;
  368.     theFocusRec.printing = gPrinting;
  369.     theFocusRec.drawingPictScrap = gDrawingPictScrap;
  370.     theFocusRec.drawingPictScrapView = gDrawingPictScrapView;
  371. }
  372.  
  373. //--------------------------------------------------------------------------------------------------
  374. #pragma segment MAViewRes
  375.  
  376. pascal void SetFocus(const FocusRec& theFocusRec)
  377. {
  378.     gFocusedView = theFocusRec.focusedView;
  379.     gPrinting = theFocusRec.printing;
  380.     gDrawingPictScrap = theFocusRec.drawingPictScrap;
  381.     gDrawingPictScrapView = theFocusRec.drawingPictScrapView;
  382. }
  383.  
  384. //--------------------------------------------------------------------------------------------------
  385. #pragma segment MAViewRes
  386.  
  387. pascal void GetViewPortInfo(ViewPortInfo& theViewPortInfo)
  388. {
  389.     GetPort(theViewPortInfo.port);
  390.     GetClip(theViewPortInfo.clip);
  391.     theViewPortInfo.org = theViewPortInfo.port->portRect[topLeft];
  392.     theViewPortInfo.isValid = TRUE;
  393. }
  394.  
  395. //--------------------------------------------------------------------------------------------------
  396. #pragma segment MAViewRes
  397.  
  398. pascal void SetViewPortInfo(const ViewPortInfo& theViewPortInfo)
  399. {
  400.     if (qd.thePort != theViewPortInfo.port)        // be nice to accelerator cards 
  401.         SetPort(theViewPortInfo.port);
  402.     if (qd.thePort->portRect[topLeft] != theViewPortInfo.org)
  403.         SetOrigin(theViewPortInfo.org.h, theViewPortInfo.org.v);
  404.     SetClip(theViewPortInfo.clip);
  405. }
  406.  
  407. //--------------------------------------------------------------------------------------------------
  408. #pragma segment MADebug
  409.  
  410. pascal void WriteFocus(void)
  411. {
  412.     WrLblVPt("  fViewToQDOffset", gFocusedView->fViewToQDOffset);
  413.     fprintf(stderr, "\n");
  414.     WrLblRect("     portRect", qd.thePort->portRect);
  415.     fprintf(stderr, "\n");
  416.     WrLblRect("       visRgn", (*(qd.thePort->visRgn))->rgnBBox);
  417.     fprintf(stderr, "\n");
  418.     WrLblRect("      clipRgn", (*(qd.thePort->clipRgn))->rgnBBox);
  419.     fprintf(stderr, "\n");
  420. }
  421.  
  422. //--------------------------------------------------------------------------------------------------
  423. #pragma segment MAOpen
  424.  
  425. pascal void TView::Initialize(void)                // override 
  426. {
  427.     inherited::Initialize();
  428.  
  429.     fCursorID = kNoResource;
  430.     fHelpID = kNoResource;
  431.     fSuperView = NULL;
  432.     fSubViews = NULL;
  433.     fDocument = NULL;
  434.     fLocation = gZeroVPt;
  435.     fSize = gZeroVPt;                            // ??? Should this be non-zero? 
  436.     fSizeDeterminer[hSel] = sizeVariable;
  437.     fSizeDeterminer[vSel] = sizeVariable;
  438.     fHLDesired = hlOff;
  439.     fShown = TRUE;
  440.     fPrintHandler = NULL;
  441.     fTranslation = gZeroVPt;
  442.     fViewToQDOffset = gZeroVPt;
  443.     fQDOrigin = gZeroPt;
  444.     fVisibleExtent = gZeroVRect;
  445.     fViewAdornment = NULL;
  446.     fHandlesCursor = TRUE;
  447.     fLetsSubViewsHandleCursor = TRUE;
  448.     fHandlesHelp = TRUE;
  449.     fLetsSubViewsHandleHelp = TRUE;
  450. }
  451.  
  452. //--------------------------------------------------------------------------------------------------
  453. #pragma segment MAOpen
  454.  
  455. pascal void TView::IView(TDocument* itsDocument,
  456.                          TView* itsSuperView,
  457.                          const VPoint& itsLocation,
  458.                          const VPoint& itsSize,
  459.                          SizeDeterminer itsHSizeDet,
  460.                          SizeDeterminer itsVSizeDet)
  461. {
  462.     FailInfo fi;
  463.  
  464.     this->IEvtHandler(itsSuperView);
  465.  
  466.     // fSuperView = itsSuperView; <= set in AddSubView's call to BeInSuperView (see below...)
  467.     fDocument = itsDocument;
  468.     fLocation = itsLocation;
  469.     fSize = itsSize;
  470.     fSizeDeterminer[hSel] = itsHSizeDet;
  471.     fSizeDeterminer[vSel] = itsVSizeDet;
  472.  
  473.     if (fi.Try())
  474.     {
  475.         if (itsSuperView)
  476.             itsSuperView->AddSubView(this);
  477.         if (itsDocument)
  478.             itsDocument->AddView(this);
  479.         fi.Success();
  480.     }
  481.     else                                        // Recover
  482.         {
  483.             this->Free();
  484.             fi.ReSignal();
  485.         }
  486. }
  487.  
  488. //--------------------------------------------------------------------------------------------------
  489. #pragma segment MAOpen
  490.  
  491. pascal void TView::IRes(TDocument* itsDocument,
  492.                         TView* itsSuperView,
  493.                         Ptr& itsParams)
  494. {
  495.     const SizeDeterminer allSizeDeterminerBits = sizeSuperView | sizeRelSuperView | sizePage | sizeFillPages | sizeVariable | sizeFixed;
  496.  
  497.     ViewTemplate & templateData = *((ViewTemplatePtr)itsParams);
  498.     
  499.     // assign 3bit field from templateData to local variable and mask off unused bits to avoid
  500.     // sign extension.
  501.     SizeDeterminer itsHSizeDet = (SizeDeterminer)(templateData.itsHSizeDet & allSizeDeterminerBits);
  502.     SizeDeterminer itsVSizeDet = (SizeDeterminer)(templateData.itsVSizeDet & allSizeDeterminerBits);
  503.  
  504.     this->IView(itsDocument, itsSuperView, templateData.itsLocation, templateData.itsSize, itsHSizeDet, itsVSizeDet);
  505.  
  506.     this->SetEnable(templateData.isEnabled);
  507.  
  508.     fShown = templateData.isShown;
  509.     fHLDesired = hlOff;                            // ??? This is done in Initialize. Should it
  510.     // be included in the resource?
  511.     fIdentifier = templateData.thisViewID;
  512.     OffsetPtrWStr(itsParams, sizeof(ViewTemplate));
  513. }
  514.  
  515. //--------------------------------------------------------------------------------------------------
  516. #pragma segment MAViewRes
  517.  
  518. pascal TObject* TView::Clone(void)                // override 
  519. {
  520.     TView * aClonedView = (TView *)(inherited::Clone());
  521.  
  522.     aClonedView->fSuperView = NULL;
  523.     aClonedView->fSubViews = NULL;
  524.  
  525.     if (fPrintHandler)
  526.         aClonedView->AttachPrintHandler((TPrintHandler *)fPrintHandler->Clone());
  527.     aClonedView->fNextHandler = NULL;
  528.     aClonedView->fDocument = NULL;
  529.  
  530.     return aClonedView;
  531. }
  532.  
  533. //--------------------------------------------------------------------------------------------------
  534. #pragma segment MAWriteRes
  535.  
  536. pascal void TView::WRes(ViewRsrcHandle theResource,
  537.                         Ptr& itsParams)
  538. {
  539.     ViewTemplatePtr vwPtr = ViewTemplatePtr(ExpandPtrWStr((Handle)theResource, itsParams, sizeof(ViewTemplate), gWResType.Length()));
  540.  
  541.     if (fSuperView)
  542.         vwPtr->itsParentID = fSuperView->fIdentifier;
  543.     else
  544.         vwPtr->itsParentID = kNoIdentifier;
  545.  
  546.     vwPtr->thisViewID = fIdentifier;
  547.     vwPtr->itsLocation = fLocation;
  548.     vwPtr->itsSize = fSize;
  549.     vwPtr->itsVSizeDet = fSizeDeterminer[vSel];
  550.     vwPtr->itsHSizeDet = fSizeDeterminer[hSel];
  551.     vwPtr->isShown = fShown;
  552.     vwPtr->isEnabled = fEnabled;
  553.     vwPtr->itsSignature = gWResSignature;
  554.     // vwPtr->itsType = gWResType; 
  555.     CopyStr255(gWResType, PRStr(vwPtr->u0.itsType));
  556.  
  557.     // Everybody gets this far - bump number of views in resource
  558.     ++(*theResource)->numViews;
  559. }
  560.  
  561. //--------------------------------------------------------------------------------------------------
  562. #pragma segment MAWriteRes
  563.  
  564. pascal void TView::WriteRes(ViewRsrcHandle theResource,
  565.                             Ptr& itsParams)
  566. {
  567.     gWResSignature = 'view';
  568.     gWResType = "TView";
  569.     this->WRes(theResource, itsParams);
  570. }
  571.  
  572. //--------------------------------------------------------------------------------------------------
  573. #pragma segment MAClose
  574.  
  575. pascal void TView::Free(void)
  576. {
  577.     // Because RemoveSubView frees the list when it goes empty we don't need to actually free
  578.     // the list right here.  When each subview gets the free message it removes itself from the
  579.     // superview (this) and the list may get deleted.    Kinda twisty, huh?
  580.  
  581.     CSubViewIterator iter(this);
  582.  
  583.     for (TView * aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
  584.         aView->Free();
  585.  
  586.     this->InvalidateFocus();
  587.     gApplication->InvalidateMouseRegions();        // Must re-calc cursor and help rgn.
  588.  
  589.     if (this == gApplication->GetTarget())        // Fixup the target chain 
  590.         gApplication->SetTarget(gApplication);
  591.  
  592.     if (fSuperView)
  593.     {
  594.         fSuperView->RemoveSubView(this);
  595.         fSuperView = NULL;
  596.     }
  597.  
  598.     fViewAdornment = (TAdornment *)(FreeIfObject(fViewAdornment));
  599.  
  600.     if (fDocument)
  601.     {
  602.         fDocument->DeleteView(this);
  603.         fDocument = NULL;
  604.     }
  605.  
  606.     inherited::Free();                            // will free fPrintHandler
  607. }
  608.  
  609. //--------------------------------------------------------------------------------------------------
  610. #pragma segment MAActivate
  611.  
  612. pascal void TView::Activate(Boolean entering)
  613. {
  614.     HLState newHL;
  615.  
  616.     if (entering)
  617.         newHL = hlOn;
  618.     else
  619.         newHL = hlDim;
  620.  
  621.     if (fViewAdornment && this->IsDrawable())
  622.     {
  623.         VRect visRect;
  624.  
  625.         this->GetDrawableRect(visRect);
  626.         fViewAdornment->DoHighlightSelection(this, visRect, fHLDesired, newHL);
  627.     }
  628.     fHLDesired = newHL;
  629.  
  630.     CSubViewIterator iter(this);
  631.  
  632.     for (TView * aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
  633.         aView->Activate(entering);
  634. }
  635.  
  636. //--------------------------------------------------------------------------------------------------
  637. #pragma segment MANonRes
  638.  
  639. pascal void TView::AddAdorner(TAdorner* anAdorner,
  640.                               AdornPriority itsPriority,
  641.                               Boolean invalidate)
  642. {
  643.     fViewAdornment = this->MakeAdornment();
  644.     fViewAdornment->AddAdorner(anAdorner, itsPriority, invalidate);
  645. }
  646.  
  647. //--------------------------------------------------------------------------------------------------
  648. #pragma segment MANonRes
  649.  
  650. pascal void TView::DeleteAdorner(TAdorner* anAdorner,
  651.                                  Boolean invalidate)
  652. {
  653.     if (fViewAdornment)
  654.     {
  655.         fViewAdornment->DeleteAdorner(anAdorner, invalidate);
  656.  
  657.         // dispose of fViewAdornment if it only contains the default gDrawAdorner 
  658.         if ((fViewAdornment->fAdorners->GetSize() == 1) && (fViewAdornment->AdornerAt(1) == gDrawAdorner))
  659.         {
  660.             fViewAdornment = (TAdornment *)(FreeIfObject(fViewAdornment));
  661.         }
  662.     }
  663. }
  664.  
  665. //--------------------------------------------------------------------------------------------------
  666. #pragma segment MANonRes
  667. pascal void TView::DeleteAdornerByID(IDType itsID,
  668.                                      Boolean invalidate)
  669. {
  670.     if (fViewAdornment)
  671.     {
  672.         fViewAdornment->DeleteAdornerByID(itsID, invalidate);
  673.  
  674.         // dispose of fViewAdornment if it only contains the default gDrawAdorner 
  675.         if ((fViewAdornment->fAdorners->GetSize() == 1) && (fViewAdornment->AdornerAt(1) == gDrawAdorner))
  676.         {
  677.             fViewAdornment = (TAdornment *)(FreeIfObject(fViewAdornment));
  678.         }
  679.     }
  680. }
  681.  
  682. //--------------------------------------------------------------------------------------------------
  683. #pragma segment MAViewRes
  684.  
  685. pascal void TView::Dim(void)
  686. {
  687.     Rect area;
  688.  
  689. #if qDebug
  690.     this->AssumeFocused();
  691. #endif
  692.  
  693.     this->GetQDExtent(area);
  694.     PenPat(qd.gray);
  695.     PenMode(patBic);
  696.     PaintRect(area);
  697. }
  698.  
  699. //--------------------------------------------------------------------------------------------------
  700. #pragma segment MAViewRes
  701.  
  702. pascal void TView::GetAdornExtent(VRect& itsAdornExtent)
  703. {
  704.     this->GetExtent(itsAdornExtent);
  705. }
  706.  
  707. //--------------------------------------------------------------------------------------------------
  708. #pragma segment MAViewRes
  709.  
  710. pascal void TView::Hilite(void)
  711. {
  712.     Rect aRect;
  713.  
  714. #if qDebug
  715.     this->AssumeFocused();
  716. #endif
  717.  
  718.     this->GetQDExtent(aRect);
  719.     InvertRect(aRect);
  720. }
  721.  
  722. //--------------------------------------------------------------------------------------------------
  723. #pragma segment MANonRes
  724.  
  725. pascal TAdornment* TView::MakeAdornment(void)
  726. {
  727.  
  728.     if (fViewAdornment)
  729.         return fViewAdornment;
  730.     else
  731.     {
  732.         TAdornment * anAdornment = new TAdornment;
  733.  
  734.         anAdornment->IAdornment('????', this, Point(1, 1));
  735.         anAdornment->AddAdorner(gDrawAdorner, kViewAdornPriority, FALSE);// default "Draw" adorner 
  736.  
  737.         return anAdornment;
  738.     }
  739. }
  740.  
  741. //--------------------------------------------------------------------------------------------------
  742. #pragma segment MAOpen
  743.  
  744. pascal void TView::AddedASubView(TView* newSubView)
  745. {
  746.     if (fSuperView)
  747.         fSuperView->AddedASubView(newSubView);
  748. }
  749.  
  750. //--------------------------------------------------------------------------------------------------
  751. #pragma segment MAOpen
  752.  
  753. pascal void TView::AddSubView(TView* theSubView)
  754. {
  755.     if (theSubView)
  756.     {
  757.         // Create the subview list if necessary 
  758.         if (!fSubViews)
  759.         {
  760.             fSubViews = NewList();
  761. #if qDebug
  762.             fSubViews->SetEltType("TView");
  763. #endif
  764.  
  765.         }
  766.         if (qDebug && (fSubViews->GetIdentityItemNo(theSubView) != kEmptyIndex))
  767.             ProgramBreak("in TView.AddSubView: Attempting to add the same subview twice.");
  768.  
  769.         fSubViews->InsertLast(theSubView);
  770.         theSubView->BeInSuperView(this);
  771.         this->AddedASubView(theSubView);
  772.     }
  773. }
  774.  
  775. //--------------------------------------------------------------------------------------------------
  776. #pragma segment MAViewRes
  777.  
  778. pascal void TView::AdjustSize(void)
  779. {
  780.     VPoint newSize(fSize);
  781.     this->ComputeSize(newSize);
  782.     if (newSize != fSize)
  783.     {
  784.         this->Resize(newSize, kInvalidate);
  785.         this->DoPagination();
  786.     }
  787.     else
  788.     {
  789.         CSubViewIterator iter(this);
  790.  
  791.         for (TView * aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
  792.             aView->AdjustSize();
  793.     }
  794. }
  795.  
  796. //--------------------------------------------------------------------------------------------------
  797. #pragma segment MADebug
  798.  
  799. pascal void TView::AssumeFocused(void)
  800. {
  801.     if (gAssumeFocused)
  802.         if (!IsFocused())
  803.         {
  804.             Str255 aString;
  805.             MANamePtr aStringPtr;
  806.  
  807.             aStringPtr = MANamePtr(&aString);
  808.             this->GetClassName((*aStringPtr));
  809.             fprintf(stderr, ", ClassName=%s, InspectorName=", (char*)aString);
  810.             this->GetInspectorName(aString);
  811.             fprintf(stderr, "%s\n", (char*)aString);
  812.             if (gFocusedView)
  813.             {
  814.                 fprintf(stderr, "View actually focused is:\n");
  815.                 gFocusedView->GetClassName((*aStringPtr));
  816.                 fprintf(stderr, ", ClassName=%s, InspectorName=", (char*)aString);
  817.                 gFocusedView->GetInspectorName(aString);
  818.                 fprintf(stderr, "%s\n", (char*)aString);
  819.             }
  820.             ProgramBreak("Failed AssumeFocused");
  821.         }
  822. }
  823.  
  824. //--------------------------------------------------------------------------------------------------
  825. #pragma segment MAViewRes
  826.  
  827. pascal void TView::AttachPrintHandler(TPrintHandler* itsPrintHandler)
  828. {
  829.     this->AddAdorner(gPrintAdorner, kLowestAdornPriority, FALSE);// adorner calls DoDrawPrintFeedback 
  830.     fPrintHandler = itsPrintHandler;
  831.     this->AddBehavior(itsPrintHandler);
  832.     this->DoCheckPrinter();
  833. }
  834.  
  835. //--------------------------------------------------------------------------------------------------
  836. #pragma segment MANonRes
  837.  
  838. pascal void TView::BeInPort(GrafPtr itsPort)
  839. {
  840.  
  841.     if ((itsPort) && (fPrintHandler))
  842.         this->DoCheckPrinter();
  843.  
  844.     CSubViewIterator iter(this);
  845.  
  846.     for (TView * aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
  847.         aView->BeInPort(itsPort);
  848. }
  849.  
  850. //--------------------------------------------------------------------------------------------------
  851. #pragma segment MANonRes
  852.  
  853. pascal void TView::BeInScroller(TScroller* itsScroller)
  854. {
  855.     // convert the size to superview location. ??? SRF should we localtosuper the fsize instead?
  856.  
  857.     if (itsScroller)
  858.         itsScroller->SetScrollLimits(fSize + fLocation, kDontRedraw);
  859. }
  860.  
  861. //--------------------------------------------------------------------------------------------------
  862. #pragma segment MAOpen
  863.  
  864. pascal void TView::BeInSuperView(TView* itsSuperview)
  865. {
  866.     TView * oldSuperView = fSuperView;
  867.  
  868.     fSuperView = itsSuperview;
  869.     this->InvalidateFocus();                    // the subview may have been focused 
  870.     this->UpdateCoordinates();
  871.  
  872.     if (!itsSuperview)
  873.     {
  874.         // Removing this view from its superview 
  875.         if (fNextHandler == oldSuperView)        // If next event handler is the former superview
  876.             fNextHandler = NULL;                // take this view out of the event handler chain.
  877.         this->BeInPort(NULL);
  878.     }
  879.     else
  880.     {
  881.         if (!fNextHandler)                        // If necessary, 
  882.             fNextHandler = itsSuperview;        // …hook this view up to an event hander chain.
  883.         this->BeInPort(this->GetGrafPort());
  884.     }
  885. }
  886.  
  887. //--------------------------------------------------------------------------------------------------
  888. #pragma segment MAViewRes
  889. pascal void TView::BeTarget(void)
  890. {
  891.     this->SetTarget(this);
  892. }
  893.  
  894. //--------------------------------------------------------------------------------------------------
  895. #pragma segment MAViewRes
  896.  
  897. pascal void TView::CalcMinSize(VPoint& minSize)
  898. {
  899.     minSize = fSize;
  900. }
  901.  
  902. //--------------------------------------------------------------------------------------------------
  903. #pragma segment MAViewRes
  904.  
  905. // Set clipping to: (<current clipping> INTERSECT r) OFFSET-BY (hDeltaOrg, vDeltaOrg) 
  906. pascal void TView::ClipFurtherTo(const Rect& r,
  907.                                  short hDeltaOrg,
  908.                                  short vDeltaOrg)
  909. {
  910. #if qDebug
  911.     this->AssumeFocused();
  912.     UseTempRgn("ClipFurtherTo");
  913. #endif
  914.  
  915.     RectRgn(gTempRgn, r);
  916.     SectRgn(qd.thePort->clipRgn, gTempRgn, gTempRgn);
  917.     if ((hDeltaOrg) || (vDeltaOrg))
  918.         OffsetRgn(gTempRgn, hDeltaOrg, vDeltaOrg);
  919.     SetClip(gTempRgn);
  920.  
  921. #if qDebug
  922.     DoneWithTempRgn();
  923. #endif
  924.  
  925. }
  926.  
  927. //--------------------------------------------------------------------------------------------------
  928. #pragma segment MAViewRes
  929.  
  930. // Set clipping to: (<current clipping> INTERSECT r) OFFSET-BY (hDeltaOrg, vDeltaOrg) 
  931. pascal void TView::OffsetAndClipFurtherTo(const Rect& r,
  932.                                           short hDeltaOrg,
  933.                                           short vDeltaOrg)
  934. {
  935. #if qDebug
  936.     this->AssumeFocused();
  937.     UseTempRgn("OffsetAndClipFurtherTo");
  938. #endif
  939.  
  940.     // First offset the clip Rgn 
  941.     if ((hDeltaOrg) || (vDeltaOrg))
  942.         OffsetRgn(qd.thePort->clipRgn, hDeltaOrg, vDeltaOrg);
  943.  
  944.     // Now ClipFurtherTo 
  945.     RectRgn(gTempRgn, r);
  946.     SectRgn(qd.thePort->clipRgn, gTempRgn, gTempRgn);
  947.     SetClip(gTempRgn);
  948.  
  949. #if qDebug
  950.     DoneWithTempRgn();
  951. #endif
  952.  
  953. }
  954.  
  955. //--------------------------------------------------------------------------------------------------
  956. #pragma segment MAClose
  957.  
  958. pascal void TView::Close(void)
  959. {
  960.     // ??? Seems like we should notify the world that we're closing instead of groping around for
  961.     // stuff like the last command…
  962.     TCommand * lastCommand = this->GetLastCommand();
  963.  
  964.     if ((lastCommand) && (lastCommand->fView == this))
  965.         this->CommitLastCommand();
  966.  
  967.     CSubViewIterator iter(this);
  968.  
  969.     for (TView * aView = iter.FirstSubView(); iter.More(); aView = iter.NextSubView())
  970.         aView->Close();
  971. }
  972.  
  973. //--------------------------------------------------------------------------------------------------
  974. #pragma segment MAViewRes
  975.  
  976. pascal void TView::ComputeSize(VPoint& newSize)
  977. {
  978.     VCoordinate dimension;
  979.     VHSelect vhs;
  980.     VPoint minSize;
  981.  
  982.     this->CalcMinSize(minSize);
  983.  
  984.     // Adjust view size based on size determiners. 
  985.     for (vhs = vSel; vhs <= hSel; ++vhs)
  986.     {
  987.         dimension = minSize[vhs];                // Set dimension to a default 
  988.         switch (fSizeDeterminer[vhs])
  989.         {
  990.             case sizeVariable:                    // For sizeVariable dimension is already set correctly
  991.                 break;
  992.  
  993.             case sizeFixed:
  994.                 dimension = fSize[vhs];
  995.                 break;
  996.  
  997.             case sizeRelSuperView:
  998.                 dimension = newSize[vhs];        // newSize set in SuperViewChangedSize 
  999.                 break;
  1000.  
  1001.             case sizeSuperView:
  1002.                 if (fSuperView)
  1003.                     dimension = fSuperView->fSize[vhs];
  1004.                 break;
  1005.  
  1006.             case sizePage:
  1007.                 // If there is no printhandler we can defer until one is attached 
  1008.                 if (fPrintHandler)
  1009.                     dimension = fPrintHandler->fViewPerPage[vhs];
  1010.                 break;
  1011.  
  1012.             case sizeFillPages:
  1013.                 // If there is no printhandler we can defer until one is attached 
  1014.                 if (fPrintHandler)
  1015.                     dimension = Min(RoundUp(minSize[vhs], (short)(fPrintHandler->fViewPerPage[vhs])), kMaxCoord);
  1016.                 break;
  1017.         }
  1018.         newSize[vhs] = dimension;
  1019.     }
  1020. }
  1021.  
  1022. //--------------------------------------------------------------------------------------------------
  1023. #pragma segment MAViewRes
  1024.  
  1025. pascal Boolean TView::ContainsClipType(ResType aType)
  1026. {
  1027.     long offset;
  1028.  
  1029.     return (GetScrap(NULL, aType, offset) > 0);
  1030. }
  1031.  
  1032. //--------------------------------------------------------------------------------------------------
  1033. #pragma segment MAViewRes
  1034.  
  1035. pascal Boolean TView::ContainsMouse(const VPoint& theMouse)
  1036. {
  1037.     VRect extent;
  1038.  
  1039.     this->GetExtent(extent);
  1040.     return (this->IsShown() && extent.Contains(theMouse));
  1041. }
  1042.  
  1043. //--------------------------------------------------------------------------------------------------
  1044. #pragma segment MAViewRes
  1045.  
  1046. pascal short TView::CountSubViews(void)
  1047. {
  1048.     if (fSubViews)
  1049.         return fSubViews->GetSize();
  1050.     else
  1051.         return 0;
  1052. }
  1053.  
  1054. //--------------------------------------------------------------------------------------------------
  1055. #pragma segment MAViewRes
  1056.  
  1057. pascal VCoordinate TView::DoBreakFollowing(VHSelect vhs,
  1058.                                            VCoordinate prevBreak,
  1059.                                            Boolean& automatic)
  1060. {
  1061.     return fPrintHandler->BreakFollowing(vhs, prevBreak, automatic);
  1062. }
  1063.  
  1064. //--------------------------------------------------------------------------------------------------
  1065. #pragma segment MANonRes
  1066.  
  1067. pascal void TView::DoCalcPageStrips(VPoint& pageStrips)
  1068. {
  1069.     fPrintHandler->CalcPageStrips(pageStrips);
  1070. }
  1071.  
  1072. //--------------------------------------------------------------------------------------------------
  1073. #pragma segment MANonRes
  1074.  
  1075. pascal void TView::DoCalcViewPerPage(VPoint& viewPerPage)
  1076. {
  1077.     viewPerPage = fSize;                        // in case fPrintHandler is vacuous
  1078.     fPrintHandler->CalcViewPerPage(viewPerPage);
  1079. }
  1080.  
  1081. //--------------------------------------------------------------------------------------------------
  1082. #pragma segment MAViewRes
  1083.  
  1084. pascal void TView::DoCheckPrinter(void)
  1085. {
  1086.     fPrintHandler->CheckPrinter();
  1087. }
  1088.  
  1089. //--------------------------------------------------------------------------------------------------
  1090. #pragma segment MAViewRes
  1091.  
  1092. pascal void TView::DoDrawPrintFeedback(const VRect& area)
  1093. {
  1094.     if (qDebug)
  1095.         this->AssumeFocused();
  1096.  
  1097.     if (fPrintHandler)
  1098.         fPrintHandler->DrawPrintFeedback(area);
  1099. }
  1100.  
  1101. //--------------------------------------------------------------------------------------------------
  1102. #pragma segment MAViewRes
  1103.  
  1104. pascal void TView::DoDrawPageBreak(VHSelect vhs,
  1105.                                    short whichBreak,
  1106.                                    VCoordinate loc,
  1107.                                    Boolean automatic)
  1108. {
  1109.     fPrintHandler->DrawPageBreak(vhs, whichBreak, loc, automatic);
  1110. }
  1111.  
  1112. //--------------------------------------------------------------------------------------------------
  1113. #pragma segment MAViewRes
  1114.  
  1115. pascal Boolean TView::HandlesCursor(void)
  1116. {
  1117.     return (fHandlesCursor && this->IsEnabled()) || this->LetsSubViewsHandleCursor();
  1118. }
  1119.  
  1120. //--------------------------------------------------------------------------------------------------
  1121. #pragma segment MAViewRes
  1122.  
  1123. pascal Boolean TView::LetsSubViewsHandleCursor(void)
  1124. {
  1125.     return (fLetsSubViewsHandleCursor && fSubViews && (!fSubViews->IsEmpty()));
  1126. }
  1127.  
  1128. //--------------------------------------------------------------------------------------------------
  1129. #pragma segment MAViewRes
  1130.  
  1131. pascal Boolean TView::HandlesHelp(void)
  1132. {
  1133.     return fHandlesHelp || this->LetsSubViewsHandleHelp();
  1134. }
  1135.  
  1136. //--------------------------------------------------------------------------------------------------
  1137. #pragma segment MAViewRes
  1138.  
  1139. pascal Boolean TView::LetsSubViewsHandleHelp(void)
  1140. {
  1141.     return (fLetsSubViewsHandleHelp && fSubViews && (!fSubViews->IsEmpty()));
  1142. }
  1143.  
  1144. //--------------------------------------------------------------------------------------------------
  1145. #pragma segment MAViewRes
  1146.  
  1147. pascal void TView::DoHighlightSelection(HLState fromHL,
  1148.                                         HLState toHL)
  1149. {
  1150. }
  1151.  
  1152. //--------------------------------------------------------------------------------------------------
  1153. #pragma segment MASelCommand
  1154.  
  1155. pascal void TView::DoMouseCommand(VPoint& theMouse,
  1156.                                   TToolboxEvent* event,
  1157.                                   Point hysteresis)
  1158. /* It's easy to get simple tracking that feeds back into your view.  Just create a command!
  1159.   {
  1160.   TNoChangesCommand*        aCommand;
  1161.   aCommand = new TNoChangesCommand;
  1162.   aCommand->INoChangesTracker(cNoCommand, fDocument, this, this->GetScroller(FALSE), theMouse);
  1163.   fTracksMouse = TRUE;
  1164.   this->PostCommand(aCommand);
  1165.   }
  1166. */
  1167.  
  1168. {
  1169. }
  1170.  
  1171. //--------------------------------------------------------------------------------------------------
  1172. #pragma segment MANonRes
  1173.  
  1174. pascal void TView::DoPagination(void)
  1175. {
  1176.     if (fPrintHandler)
  1177.         fPrintHandler->RedoPageBreaks();
  1178. }
  1179.  
  1180. //--------------------------------------------------------------------------------------------------
  1181. #pragma segment MANonRes
  1182.  
  1183. pascal void TView::DoPrinterChanged(void)
  1184. {
  1185.     if (fPrintHandler)
  1186.         fPrintHandler->PrinterChanged();
  1187. }
  1188.  
  1189. //--------------------------------------------------------------------------------------------------
  1190. #pragma segment MAPrint
  1191.  
  1192. pascal void TView::DoSetPageOffset(const VPoint& coord)
  1193. {
  1194.     fPrintHandler->SetPageOffset(coord);
  1195. }
  1196.  
  1197. //--------------------------------------------------------------------------------------------------
  1198. #pragma segment MAViewRes
  1199.  
  1200. pascal void TView::DoSetCursor(const VPoint& localPoint,
  1201.                                RgnHandle cursorRgn)
  1202. {
  1203.     CursHandle aCursor;
  1204.  
  1205.     // Our default is to claim the cursor as an arrow for the default cursor region. 
  1206.     if (qDebug)
  1207.         this->AssumeFocused();
  1208.  
  1209.     this->GetDefaultCursorRgn(localPoint, cursorRgn);
  1210.  
  1211.     if (fCursorID == kNoResource)
  1212.         SetCursor(qd.arrow);
  1213.     else
  1214.     {
  1215.         aCursor = GetCursor(fCursorID);
  1216.         FailNILResource((Handle)aCursor);
  1217.         SetCursor(**aCursor);
  1218.     }
  1219.  
  1220.     // If you claim the cursor don't forget to _PROPERLY_ calculate the cursorRgn or
  1221.     // WaitNextEvent might give you a lovely mouse-moved event shower. (Lather up!)
  1222.  
  1223.     // P.S. In debug this is checked for in our original caller: TApplication.TrackMouse.
  1224. }
  1225.  
  1226. //--------------------------------------------------------------------------------------------------
  1227. #pragma segment MAViewRes
  1228.  
  1229. pascal void TView::DoShowBalloon(const VPoint& ,
  1230.                                  RgnHandle helpRgn,
  1231.                                  const HMMessageRecord& helpMessage,
  1232.                                  Point localQDTip,
  1233.                                  const Rect& localQDRect,
  1234.                                  short balloonVariant)
  1235. {
  1236.     if (qNeedsHelpMgr || gConfiguration.hasHelpMgr)
  1237.     {
  1238.         Rect rect = localQDRect;
  1239.  
  1240.         LocalToGlobal(rect[topLeft]);
  1241.         LocalToGlobal(rect[botRight]);
  1242.         LocalToGlobal(localQDTip);
  1243.  
  1244.         OSErr err = (HMShowBalloon(helpMessage, localQDTip, NULL/**rect*/, NULL, 0, balloonVariant, kHMRegularWindow));
  1245.         if (err != hmBalloonAborted)
  1246.             FailOSErr(err);
  1247.  
  1248.     }
  1249.  
  1250.     // Intersect with visible region. Excludes the balloon region. 
  1251.     SectRgn(qd.thePort->visRgn, helpRgn, helpRgn);
  1252.     SectRgn(qd.thePort->clipRgn, helpRgn, helpRgn);
  1253.  
  1254. }
  1255.  
  1256. //--------------------------------------------------------------------------------------------------
  1257. #pragma segment MAViewRes
  1258.  
  1259. pascal void TView::DoShowHelp(const VPoint& localPoint,
  1260.                               RgnHandle helpRgn)
  1261. {
  1262.     HMMessageRecord helpMessage;
  1263.     Point tip;
  1264.     Rect hotRect;
  1265.     short balloonVariant;
  1266.  
  1267.     if ((qNeedsHelpMgr || gConfiguration.hasHelpMgr) && (fHelpID != kNoResource))
  1268.     {
  1269.         this->GetDefaultHelpRgn(localPoint, helpRgn);
  1270.         this->GetHelpParameters(localPoint, helpRgn, helpMessage, tip, hotRect, balloonVariant);
  1271.         this->DoShowBalloon(localPoint, helpRgn, helpMessage, tip, hotRect, balloonVariant);
  1272.     }
  1273.  
  1274. }
  1275.  
  1276. //--------------------------------------------------------------------------------------------------
  1277. #pragma segment MAViewRes
  1278.  
  1279. pascal void TView::ExcludeSubViewRegions(RgnHandle aRegion,
  1280.                                          TestViewType TestSubView,
  1281.                                          void* staticLink)
  1282. {
  1283.     const short kRgnHandleTooBig = 16 * 1024;    // regions > 16K don't work too well, !!!
  1284.     // move this to the interfaces soon...
  1285.     Boolean rgnHandleWasTooBig;
  1286.  
  1287.     if (qDebug)
  1288.         this->AssumeFocused();
  1289. #if qDebug
  1290.     UseTempRgn("ExcludeSubViewRegions");
  1291. #endif
  1292.  
  1293.     rgnHandleWasTooBig = FALSE;
  1294.  
  1295.     CSubViewIterator iter(this);
  1296.  
  1297.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1298.         if (TestSubView(theSubView, staticLink) &&!rgnHandleWasTooBig)
  1299.         {
  1300.             VRect vFrame;
  1301.             Rect frame;
  1302.  
  1303.             theSubView->GetFrame(vFrame);        // Remember, the frame is in OUR
  1304.             // coordinate system
  1305.             this->ViewToQDRect(vFrame, frame);
  1306.             RectRgn(gTempRgn, frame);
  1307.             DiffRgn(aRegion, gTempRgn, aRegion);// Remove the subview from the claimed rgn 
  1308.  
  1309.             if ((GetHandleSize((Handle)aRegion) > kRgnHandleTooBig) || (MemError() != noErr))
  1310.             {
  1311.                 rgnHandleWasTooBig = TRUE;
  1312.                 SetEmptyRgn(aRegion);
  1313.             }
  1314.         }
  1315.  
  1316. #if qDebug
  1317.     DoneWithTempRgn();
  1318. #endif
  1319.  
  1320. }
  1321.  
  1322. //--------------------------------------------------------------------------------------------------
  1323. #pragma segment MAViewRes
  1324.  
  1325. pascal Boolean SubViewHandlesCursor(TView* aSubView,
  1326.                                     void*)
  1327. {
  1328.     return aSubView->HandlesCursor() || aSubView->LetsSubViewsHandleCursor();
  1329. }
  1330.  
  1331.  
  1332. pascal void TView::GetDefaultCursorRgn(const VPoint&/* localPoint */ ,
  1333.                                        RgnHandle cursorRgn)
  1334. {
  1335.     Rect r;
  1336.  
  1337.     /* If we get all the way to this view without any other view having claimed the
  1338.       cursor its because no superview handled it in a "HandleCursor" and no subview handled it
  1339.       in a "DoSetCursor" thus there is a _very_ high probability that this view should set the cursor.
  1340.       Just in case though, we remove the frames.
  1341.       If a containing view wishes to set the cursor for itself && all its subview, the
  1342.       place to do that is in our caller: HandleCursor.  See HandleCursor in the homebrew controls
  1343.       TDialogView subclass in DemoDialogs.
  1344.       This strategy lets MacApp provide the _largest_ possible cursor rgn, as a default.    This way
  1345.       windows, controls, background views, etc can have nice big cursor rgns but the developer can
  1346.       wrest control if they care to. */
  1347.  
  1348.     if (qDebug)
  1349.         this->AssumeFocused();
  1350.  
  1351.     this->GetQDExtent(r);
  1352.     RectRgn(cursorRgn, r);
  1353.  
  1354.     if (this->LetsSubViewsHandleCursor())
  1355.         this->ExcludeSubViewRegions(cursorRgn, SubViewHandlesCursor, this);/* We won't claim subviews'
  1356.                                                   territory */
  1357.  
  1358.     // Intersect with visible region 
  1359.     SectRgn(qd.thePort->visRgn, cursorRgn, cursorRgn);
  1360.     SectRgn(qd.thePort->clipRgn, cursorRgn, cursorRgn);
  1361.  
  1362.     // Add balloon region here? Re-intersect with visRgn? 
  1363.  
  1364.     // Make sure the cursorpoint is _still_ included 
  1365.     //!!! Done in TApplication.TrackCursor. PtAndRgn(this->ViewToQDPt(localPoint),cursorRgn);
  1366.  
  1367.     // If you claim the cursor don't forget to _PROPERLY_ calculate the cursorRgn or
  1368.     // WaitNextEvent might give you a lovely mouse-moved event shower. (Lather up!)
  1369.  
  1370.     // P.S. In debug this is checked for in our original caller: TApplication.TrackCursor.
  1371. }
  1372.  
  1373. //--------------------------------------------------------------------------------------------------
  1374. #pragma segment MAViewRes
  1375.  
  1376. pascal Boolean SubViewHandlesHelp(TView* aSubView,
  1377.                                   void*)
  1378. {
  1379.     return aSubView->HandlesHelp();
  1380. }
  1381.  
  1382.  
  1383. pascal void TView::GetDefaultHelpRgn(const VPoint&/* localPoint */ ,
  1384.                                      RgnHandle helpRgn)
  1385. {
  1386.     Rect r;
  1387.  
  1388.     if (qDebug)
  1389.         this->AssumeFocused();
  1390.  
  1391.     this->GetQDExtent(r);
  1392.     RectRgn(helpRgn, r);
  1393.  
  1394.     if (this->LetsSubViewsHandleHelp())
  1395.         this->ExcludeSubViewRegions(helpRgn, SubViewHandlesHelp, this);// We won't claim subviews' territory 
  1396.  
  1397.     // Make sure the cursorpoint is _still_ included 
  1398.     //!!! Done in TApplication.TrackHelp. PtAndRgn(this->ViewToQDPt(localPoint),helpRgn);
  1399. }
  1400.  
  1401. //--------------------------------------------------------------------------------------------------
  1402. #pragma segment MAViewRes
  1403.  
  1404. pascal void TView::GetLocalOffsetInSuper(VPoint& localOffsetInSuper)
  1405. {
  1406.     localOffsetInSuper = fLocation - fTranslation;
  1407. }
  1408.  
  1409. //--------------------------------------------------------------------------------------------------
  1410. #pragma segment MAViewRes
  1411.  
  1412. pascal void TView::SetLocalOrigin(const VPoint& newOrigin,
  1413.                                   Boolean redraw)
  1414. {
  1415.     fTranslation = -newOrigin;
  1416.     this->InvalidateFocus();
  1417.     this->UpdateCoordinates();
  1418.     if (redraw)
  1419.         this->ForceRedraw();
  1420. }
  1421.  
  1422. //--------------------------------------------------------------------------------------------------
  1423. #pragma segment MAViewRes
  1424.  
  1425. pascal TDesignator* TView::GetUserSelection(void)
  1426. {
  1427.     if (fDocument)
  1428.         return fDocument->GetUserSelection();
  1429.     else
  1430.         return NULL;
  1431. }
  1432.  
  1433. //--------------------------------------------------------------------------------------------------
  1434. #pragma segment MAViewRes
  1435.  
  1436. pascal void TView::SetUserSelection(TDesignator* newSelection)
  1437. {
  1438.     if (fDocument)
  1439.         fDocument->SetUserSelection(newSelection);
  1440. }
  1441.  
  1442. //--------------------------------------------------------------------------------------------------
  1443. #pragma segment MAViewRes
  1444.  
  1445. pascal void TView::UserSelectionChanged(void)
  1446. {
  1447.     if (fDocument)
  1448.         fDocument->UserSelectionChanged();
  1449. }
  1450.  
  1451. //--------------------------------------------------------------------------------------------------
  1452. #pragma segment MAViewRes
  1453.  
  1454. pascal void TView::Draw(const VRect& area)
  1455. {
  1456.     // You should really try to call inherited draw to be ready for the future. 
  1457. }
  1458.  
  1459. //--------------------------------------------------------------------------------------------------
  1460. #pragma segment MAViewRes
  1461.  
  1462. pascal void TView::DrawContents(void)
  1463. {
  1464.     if (!fVisibleExtent.Empty() && this->IsShown() && this->Focus())
  1465.         {
  1466.             // Even if the fVisibleExtent is not empty the Drawable rect might be
  1467.             VRect visRect;    
  1468.     
  1469.             this->GetDrawableRect(visRect);
  1470.             if (!visRect.Empty())
  1471.             {
  1472.                 if (fViewAdornment)
  1473.                     fViewAdornment->DrawContents(this, visRect);
  1474.                 else
  1475.                     this->Draw(visRect);
  1476.     
  1477.                 // Give the beahvior a chance to draw
  1478.                 TViewBehavior * aBehavior = (TViewBehavior *)this->GetFirstBehavior();
  1479.                 if (aBehavior)
  1480.                     aBehavior->Draw(visRect);
  1481.     
  1482.                 CSubViewIterator iter(this);
  1483.     
  1484.                 for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1485.                     theSubView->DrawContents();
  1486.     
  1487.                 // now add the adornments 
  1488.                 if (!(gPrinting || gDrawingPictScrap) && fViewAdornment)
  1489.                     fViewAdornment->DoHighlightSelection(this, visRect, hlOff, fHLDesired);
  1490.             }
  1491.         }
  1492. }
  1493.  
  1494. //--------------------------------------------------------------------------------------------------
  1495. #pragma segment MAViewRes
  1496.  
  1497. pascal void TView::EachSubView(pascal void(* DoToSubView)(TView* theSubView,
  1498.                                                           void* staticLink),
  1499.                                void* staticLink)
  1500. {
  1501.     CSubViewIterator iter(this);
  1502.  
  1503.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1504.         (*DoToSubView)(theSubView,staticLink);
  1505. }
  1506.  
  1507. //--------------------------------------------------------------------------------------------------
  1508. #pragma segment MAViewRes
  1509.  
  1510. pascal TView* TView::FindSubView(IDType itsIdentifier)
  1511. {
  1512.     if (((long)fIdentifier) == ((long)itsIdentifier))
  1513.         return this;
  1514.     else
  1515.     {
  1516.         CSubViewIterator iter(this);
  1517.  
  1518.         for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1519.             if (((long)theSubView->fIdentifier) == ((long)itsIdentifier))
  1520.                 return theSubView;
  1521.             else if (theSubView->fSubViews)
  1522.             {
  1523.                 TView * foundSubView = theSubView->FindSubView(itsIdentifier);
  1524.                 if (foundSubView)
  1525.                     return foundSubView;
  1526.             }
  1527.         return NULL;
  1528.     }
  1529. }
  1530.  
  1531. //--------------------------------------------------------------------------------------------------
  1532. #pragma segment MAViewRes
  1533.  
  1534. pascal TView* TView::FirstSubViewThat(pascal Boolean(* TestSubView)(TView* theSubView,
  1535.                                                                     void* staticLink),
  1536.                                       void* staticLink)
  1537. {
  1538.     if (fSubViews)
  1539.         return ((TView *)fSubViews->FirstThat((TestObjectType)TestSubView, staticLink));
  1540.     else
  1541.         return NULL;
  1542. }
  1543.  
  1544. //--------------------------------------------------------------------------------------------------
  1545. #pragma segment MAViewRes
  1546.  
  1547. pascal Boolean TView::Focus(void)
  1548. {
  1549. #if qDebug
  1550.     GrafPtr currentPort;
  1551. #endif
  1552.  
  1553.     if (this->IsFocused())                        // Already focused 
  1554.     {
  1555. #if qDebug
  1556.         GetPort(currentPort);
  1557.         if (this->GetGrafPort() != currentPort)
  1558.             ProgramBreak("TView.Focus: Port is incorrect");
  1559. #endif
  1560.  
  1561.     }
  1562.     else if (this->FocusOnSuperView())            // do my own focus 
  1563.     {
  1564.         Point relOrigin;
  1565.  
  1566.         SetOrigin(fQDOrigin.h, fQDOrigin.v);    // set the new QD origin 
  1567.         if (fSuperView)
  1568.             relOrigin = fQDOrigin - fSuperView->fQDOrigin;// extract the relorigin 
  1569.         else
  1570.             relOrigin = gZeroPt;
  1571.  
  1572.         gFocusedView = this;                    // Now we have the basic focus 
  1573.  
  1574.         // Add the clipping 
  1575.         Rect visQDRect;
  1576.  
  1577.         this->GetVisibleQDRect(visQDRect);
  1578.         this->OffsetAndClipFurtherTo(visQDRect, relOrigin.h, relOrigin.v);
  1579.     }
  1580.     else                                        // Can't focus 
  1581.         return FALSE;
  1582.     return TRUE;
  1583. }
  1584.  
  1585. //--------------------------------------------------------------------------------------------------
  1586. #pragma segment MAViewRes
  1587.  
  1588. pascal Boolean TView::FocusOnSuperView(void)
  1589. {
  1590.     if (gDrawingPictScrap || gPrinting)
  1591.         return TRUE;
  1592.     else if (fSuperView)
  1593.         return fSuperView->Focus();
  1594.     else
  1595.         return FALSE;
  1596. }
  1597.  
  1598. //--------------------------------------------------------------------------------------------------
  1599. #pragma segment MAViewRes
  1600.  
  1601. pascal void TView::ForceRedraw(void)
  1602. {
  1603.     if (this->IsVisible())
  1604.     {
  1605.         VRect extent;
  1606.  
  1607.         this->GetExtent(extent);
  1608.         this->InvalidateVRect(extent);
  1609.     }
  1610. }
  1611.  
  1612. //--------------------------------------------------------------------------------------------------
  1613. #pragma segment MAClipboard
  1614.  
  1615. pascal void TView::FreeFromClipboard(void)
  1616. {
  1617.     if (fDocument)
  1618.         fDocument->FreeFromClipboard();
  1619.     else
  1620.         this->Free();
  1621. }
  1622.  
  1623. //--------------------------------------------------------------------------------------------------
  1624. #pragma segment MAInspector
  1625.  
  1626. pascal void TView::GetInspectorName(Str255& inspectorName)// override 
  1627. {
  1628.     TView * itsRootView = this->GetRootView();
  1629.  
  1630.     if ((itsRootView) && itsRootView->IsMemberClass(GetClassIDFromName("TWindow")))
  1631.     {
  1632.         ((TWindow *)itsRootView)->GetTitle(inspectorName);
  1633.         inspectorName = "In " + inspectorName;
  1634.     }
  1635. }
  1636.  
  1637. //--------------------------------------------------------------------------------------------------
  1638. #pragma segment MAViewRes
  1639.  
  1640. pascal Boolean TView::IsFocused(void)
  1641. {
  1642.     if (gFocusedView == this)
  1643.         return TRUE;
  1644.     else if ((gDrawingPictScrapView == this) || ((gCurrPrintHandler) && (gCurrPrintHandler->fView == this)))// externally supplied focus 
  1645.     {
  1646.         gFocusedView = this;
  1647.         return TRUE;
  1648.     }
  1649.     else
  1650.         return FALSE;
  1651. }
  1652.  
  1653. //--------------------------------------------------------------------------------------------------
  1654. #pragma segment MAViewRes
  1655.  
  1656. pascal TView* TView::GetDialogView(void)
  1657. {
  1658.     if (fSuperView)
  1659.         return fSuperView->GetDialogView();
  1660.     else
  1661.         return NULL;
  1662. }
  1663.  
  1664. //--------------------------------------------------------------------------------------------------
  1665. #pragma segment MAViewRes
  1666.  
  1667. pascal void TView::GetExtent(VRect& itsExtent)
  1668. {
  1669.     VPoint localOriginInSuper;
  1670.  
  1671.     itsExtent = VRect(gZeroVPt, fSize);
  1672.  
  1673.     // Allow for the origin of the view's coordinate system. 
  1674.     this->GetLocalOffsetInSuper(localOriginInSuper);
  1675.     localOriginInSuper -= fLocation;
  1676.     itsExtent -= localOriginInSuper;
  1677. }
  1678.  
  1679. //--------------------------------------------------------------------------------------------------
  1680. #pragma segment MAViewRes
  1681.  
  1682. pascal void TView::GetFrame(VRect& itsFrame)
  1683. {
  1684.     itsFrame = VRect(fLocation, fLocation + fSize);
  1685. }
  1686.  
  1687. //--------------------------------------------------------------------------------------------------
  1688. #pragma segment MAViewRes
  1689.  
  1690. pascal GrafPtr TView::GetGrafPort(void)
  1691. {
  1692.     if (gPrinting || gDrawingPictScrap)
  1693.         return qd.thePort;                        //thePort assumed to be set by print handler
  1694.     else if (fSuperView)
  1695.         return fSuperView->GetGrafPort();
  1696.     else
  1697.         return NULL;
  1698. }
  1699.  
  1700. //--------------------------------------------------------------------------------------------------
  1701. #pragma segment MANonRes
  1702.  
  1703. pascal short TView::GetPartCode(const VPoint& theMouse)
  1704. {
  1705.     VRect extent;
  1706.  
  1707.     this->GetExtent(extent);
  1708.     if (extent.Contains(theMouse))
  1709.         return inContent;                        // "where" is in the view 
  1710.     else
  1711.         return inDesk;                            // "where" isn't in the view 
  1712. }
  1713.  
  1714. //--------------------------------------------------------------------------------------------------
  1715. #pragma segment MANonRes
  1716.  
  1717. pascal void TView::GetPrintExtent(VRect& printExtent)
  1718. {
  1719.     this->GetExtent(printExtent);
  1720. }
  1721.  
  1722. //--------------------------------------------------------------------------------------------------
  1723. #pragma segment MAViewRes
  1724.  
  1725. pascal void TView::GetQDExtent(Rect& qdExtent)
  1726. {
  1727.     VRect vr;
  1728.  
  1729.     this->GetExtent(vr);
  1730.     this->ViewToQDRect(vr, qdExtent);
  1731. }
  1732.  
  1733. //--------------------------------------------------------------------------------------------------
  1734. #pragma segment MAViewRes
  1735.  
  1736. pascal TScroller* TView::GetScroller(Boolean immediateSuperView)
  1737. {
  1738.     TScroller * aScroller;
  1739.  
  1740.     if (fSuperView)
  1741.     {
  1742.         aScroller = fSuperView->GetScroller(immediateSuperView);
  1743.         if ((aScroller == fSuperView) || (!immediateSuperView))
  1744.             return aScroller;
  1745.     }
  1746.     return NULL;
  1747. }
  1748.  
  1749. //--------------------------------------------------------------------------------------------------
  1750. #pragma segment MAViewRes
  1751.  
  1752. pascal void TView::GetVisibleRect(VRect& visRect)
  1753. {
  1754.     Rect visQDRect;
  1755.  
  1756.     this->GetVisibleQDRect(visQDRect);
  1757.     this->QDToViewRect(visQDRect, visRect);
  1758. }
  1759.  
  1760. //--------------------------------------------------------------------------------------------------
  1761. #pragma segment MAViewRes
  1762.  
  1763. pascal void TView::GetVisibleQDRect(Rect& visQDRect)
  1764. {
  1765.     if (!fVisibleExtent.Empty())
  1766.     {
  1767.         this->ViewToQDRect(fVisibleExtent, visQDRect);
  1768.         VisibleRect(visQDRect);
  1769.     }
  1770.     else
  1771.         visQDRect = gZeroRect;
  1772. }
  1773.  
  1774. //--------------------------------------------------------------------------------------------------
  1775. #pragma segment MAViewRes
  1776.  
  1777. pascal void TView::GetDrawableRect(VRect& drawRect)
  1778. {
  1779.     Rect drawQDRect;
  1780.  
  1781.     this->GetDrawableQDRect(drawQDRect);
  1782.     this->QDToViewRect(drawQDRect, drawRect);
  1783. }
  1784.  
  1785. //--------------------------------------------------------------------------------------------------
  1786. #pragma segment MAViewRes
  1787.  
  1788. pascal void TView::GetDrawableQDRect(Rect& drawQDRect)
  1789. {
  1790.     this->GetVisibleQDRect(drawQDRect);
  1791.     if (!drawQDRect.Empty())
  1792.         DrawableRect(drawQDRect);
  1793. }
  1794.  
  1795. //--------------------------------------------------------------------------------------------------
  1796. #pragma segment MAViewRes
  1797.  
  1798. pascal void TView::GetHelpParameters(const VPoint& localPoint,
  1799.                                      RgnHandle helpRgn,
  1800.                                      HMMessageRecord& helpMessage,
  1801.                                      Point& localQDTip,
  1802.                                      Rect& localQDRect,
  1803.                                      short& balloonVariant)
  1804. {
  1805.     long options;
  1806.     short theProc;
  1807.     short count;
  1808.  
  1809.     //!!! Error checking? 
  1810.     FailOSErr(HMGetIndHelpMsg(kHMRectListResType, fHelpID, 1,// component number within resource 
  1811.                               kHMEnabledItem, options, localQDTip, localQDRect, theProc, balloonVariant, helpMessage, count));
  1812.  
  1813.     // Ignore the hotRect from the resource 
  1814.     if (helpRgn)
  1815.     {
  1816.         localQDRect = (*helpRgn)->rgnBBox;
  1817.         if ((localQDTip.h < 0) || (localQDTip.v < 0))
  1818.         {
  1819.             localQDTip = this->ViewToQDPt(localPoint);
  1820.         }
  1821.     }
  1822. }
  1823.  
  1824. //--------------------------------------------------------------------------------------------------
  1825. #pragma segment MAViewRes
  1826.  
  1827. pascal TView* TView::GetRootView(void)
  1828. {
  1829.     if (fSuperView)
  1830.         return fSuperView->GetRootView();
  1831.     else
  1832.         return this;
  1833. }
  1834.  
  1835. //--------------------------------------------------------------------------------------------------
  1836. #pragma segment MAViewRes
  1837.  
  1838. pascal TWindow* TView::GetWindow(void)
  1839. {
  1840.     TView * rootView = this->GetRootView();
  1841.  
  1842.     if ((rootView) && rootView->IsMemberClass(GetClassIDFromName("TWindow")))
  1843.         return (TWindow *)rootView;
  1844.     else
  1845.         return NULL;
  1846. }
  1847.  
  1848. //--------------------------------------------------------------------------------------------------
  1849. #pragma segment MAClipboard
  1850.  
  1851. pascal long TView::GivePasteData(Handle aDataHandle,
  1852.                                  ResType dataType)
  1853. {
  1854.     long offset;
  1855.     long err;
  1856.     Boolean savedPerm;
  1857.  
  1858.     // Make sure the scrap handle is grown out of permanent memory.    If it
  1859.     // was grown out of temporary memory it would eat away at our code space.
  1860.     savedPerm = PermAllocation(TRUE);
  1861.     err = GetScrap(aDataHandle, dataType, offset);
  1862.     savedPerm = PermAllocation(savedPerm);
  1863.  
  1864. #if qDebugMsg
  1865.     if (err < 0)
  1866.         fprintf(stderr, "TView.GivePasteData got error code # %d from GetScrap for type %s\n", err, (char*) & dataType);
  1867. #endif
  1868.  
  1869.     return err;
  1870. }
  1871.  
  1872. //--------------------------------------------------------------------------------------------------
  1873. #pragma segment MAViewRes
  1874.  
  1875. pascal TView* TView::HandleCursor(const VPoint& theMouse,
  1876.                                   RgnHandle cursorRgn)
  1877. {
  1878.     TView * viewThatHandledCursor = NULL;
  1879.  
  1880.     //Assume if we got here then we already know the mouse is in this view
  1881.     if (this->LetsSubViewsHandleCursor())
  1882.     {
  1883.         CSubViewIterator iter(this, kIterateBackward);
  1884.  
  1885.         for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1886.         {
  1887.             VPoint subViewPt = theMouse;
  1888.  
  1889.             theSubView->SuperToLocal(subViewPt);
  1890.             if ((theSubView->ContainsMouse(subViewPt) && theSubView->HandlesCursor()))
  1891.                 if (viewThatHandledCursor = theSubView->HandleCursor(subViewPt, cursorRgn))
  1892.                     break;
  1893.         }
  1894.     }
  1895.  
  1896.     if (!viewThatHandledCursor && this->Focus())
  1897.     {
  1898.         // First let the Behavior have a crack at handling the cursor
  1899.         Boolean proceed = true;
  1900.  
  1901.         if (this->GetFirstBehavior())
  1902.             proceed =!((TViewBehavior *)this->GetFirstBehavior())->DoSetCursor(theMouse, cursorRgn);
  1903.  
  1904.         if (proceed)
  1905.             this->DoSetCursor(theMouse, cursorRgn);
  1906.  
  1907.         viewThatHandledCursor = this;
  1908.     }
  1909.  
  1910.     return viewThatHandledCursor;
  1911. }
  1912.  
  1913. //--------------------------------------------------------------------------------------------------
  1914. #pragma segment MAViewRes
  1915.  
  1916. pascal TView* TView::HandleHelp(const VPoint& theMouse,
  1917.                                 RgnHandle helpRgn)
  1918. {
  1919.     //Assume if we got here then we already know the mouse is in this view
  1920.  
  1921.     TView * viewThatShowedHelp = NULL;
  1922.  
  1923.     CSubViewIterator iter(this, kIterateBackward);
  1924.  
  1925.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1926.     {
  1927.         VPoint subViewPt = theMouse;
  1928.  
  1929.         theSubView->SuperToLocal(subViewPt);
  1930.  
  1931.         if (theSubView->ContainsMouse(subViewPt) && theSubView->HandlesHelp())
  1932.             if (viewThatShowedHelp = theSubView->HandleHelp(subViewPt, helpRgn))
  1933.                 break;
  1934.     }
  1935.  
  1936.     if (!viewThatShowedHelp && this->HandlesHelp() && this->Focus())
  1937.     {
  1938.         this->DoShowHelp(theMouse, helpRgn);
  1939.         viewThatShowedHelp = this;
  1940.     }
  1941.  
  1942.     return viewThatShowedHelp;
  1943. }
  1944.  
  1945. //--------------------------------------------------------------------------------------------------
  1946. #pragma segment MASelCommand
  1947.  
  1948. pascal Boolean TView::HandleMouseDown(const VPoint& theMouse,
  1949.                                       TToolboxEvent* event,
  1950.                                       Point hysteresis)
  1951. {
  1952.  
  1953.     // Assume if we got here then we already know the mouse is in this view 
  1954.     // If desired, we set the target BEFORE traversing the subviews, because
  1955.     // setting the target may alter the view hierarchy, as in the case of
  1956.     // TEditTexts, which install a "floating" TEView
  1957.  
  1958.     if (this->IsEnabled() && this->WantToBecomeTarget())
  1959.         this->BeTarget();
  1960.  
  1961.     CSubViewIterator iter(this, kIterateBackward);
  1962.  
  1963.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  1964.     {
  1965.         VPoint subViewPt = theMouse;
  1966.  
  1967.         theSubView->SuperToLocal(subViewPt);
  1968.         if (theSubView->ContainsMouse(subViewPt))
  1969.             if (theSubView->HandleMouseDown(subViewPt, event, hysteresis))
  1970.                 return TRUE;
  1971.     }
  1972.  
  1973.     if (this->IsEnabled() && this->Focus())        // see if we can handle it 
  1974.     {
  1975.         Point globalMouse;
  1976.         VPoint tempMouse = theMouse;            // We do this so we can pass a const to DoMouseCommand
  1977.  
  1978.         if (gApplication->IsCursorRgnInvalid())    //  This is important to allowing the cursor
  1979.         // to change when selecting
  1980.         {
  1981.             globalMouse = this->ViewToQDPt(theMouse);
  1982.             LocalToGlobal(globalMouse);
  1983.             gApplication->TrackCursor(globalMouse);
  1984.             this->Focus();
  1985.         }
  1986.  
  1987.         // Change added to support ViewBehaviors
  1988.         Boolean proceed = true;
  1989.  
  1990.         if (this->GetFirstBehavior())
  1991.             proceed =!((TViewBehavior *)this->GetFirstBehavior())->DoMouseCommand(tempMouse, event, hysteresis);
  1992.  
  1993.         if (proceed)
  1994.             this->DoMouseCommand(tempMouse, event, hysteresis);
  1995.  
  1996.         return TRUE;
  1997.     }
  1998.     else
  1999.         return FALSE;
  2000. }
  2001.  
  2002. //--------------------------------------------------------------------------------------------------
  2003. #pragma segment MAViewRes
  2004.  
  2005. pascal Boolean TView::HasPendingUpdate(void)
  2006. {
  2007.     if (fSuperView)
  2008.         return fSuperView->HasPendingUpdate();
  2009.     else
  2010.         return FALSE;
  2011. }
  2012.  
  2013. //--------------------------------------------------------------------------------------------------
  2014. #pragma segment MAViewRes
  2015.  
  2016. pascal void TView::InvalidateVRect(const VRect& badVRect)
  2017. {
  2018.     Rect r;
  2019.  
  2020.     this->ViewToQDRect(badVRect, r);
  2021.     this->InvalidateRect(r);
  2022. }
  2023.  
  2024. //--------------------------------------------------------------------------------------------------
  2025. #pragma segment MAViewRes
  2026.  
  2027. pascal void TView::InvalidateRect(const Rect& badRect)
  2028. {
  2029.     RgnHandle rgn = NewRgn();            // Believe it or not, QD does this on an invalRect call!
  2030.     RectRgn(rgn, badRect);
  2031.     this->InvalidateRgn(rgn);
  2032.     rgn = DisposeIfRgnHandle(rgn);
  2033. }
  2034.  
  2035. //--------------------------------------------------------------------------------------------------
  2036. #pragma segment MAViewRes
  2037.  
  2038. pascal void TView::InvalidateRgn(RgnHandle badRgn)
  2039. {
  2040.     if (this->Focus())
  2041.     {
  2042.         Rect r;
  2043.  
  2044.         this->ViewToQDRect(fVisibleExtent, r);
  2045. #if qDebug
  2046.         UseTempRgn("TView.InvalidateRgn");
  2047. #endif
  2048.  
  2049.         RectRgn(gTempRgn, r);
  2050.         SectRgn(badRgn, gTempRgn, gTempRgn);
  2051.         if (!EmptyRgn(gTempRgn))
  2052.             this->DoInvalidateRgn(gTempRgn);
  2053. #if qDebug
  2054.         DoneWithTempRgn();
  2055. #endif
  2056.  
  2057.     }
  2058. }
  2059.  
  2060. //--------------------------------------------------------------------------------------------------
  2061. #pragma segment MAViewRes
  2062.  
  2063. pascal void TView::DoInvalidateRgn(RgnHandle badRgn)
  2064. {
  2065.     if (fSuperView)
  2066.         fSuperView->DoInvalidateRgn(badRgn);    // see if someone knows how 
  2067. }
  2068.  
  2069. //--------------------------------------------------------------------------------------------------
  2070. #pragma segment MAViewRes
  2071.  
  2072. pascal void TView::InvalidateFocus(void)
  2073. {
  2074.     gFocusedView = NULL;
  2075. }
  2076.  
  2077. //--------------------------------------------------------------------------------------------------
  2078. #pragma segment MAViewRes
  2079.  
  2080. pascal Boolean TView::IsShown(void)
  2081. {
  2082.     // By definition a view can't be shown if its superview isn't shown
  2083.     if (fSuperView)
  2084.         return (fShown && fSuperView->IsShown());
  2085.     else
  2086.         return fShown;
  2087. }
  2088.  
  2089. //--------------------------------------------------------------------------------------------------
  2090. #pragma segment MAViewRes
  2091.  
  2092. pascal Boolean TView::IsActive(void)
  2093. {
  2094.     if (fSuperView)
  2095.         return fSuperView->IsActive();
  2096.     else
  2097.         return FALSE;                            // Can't be "active" without a superview 
  2098. }
  2099.  
  2100. //--------------------------------------------------------------------------------------------------
  2101. #pragma segment MAViewRes
  2102.  
  2103. pascal Boolean TView::IsDrawable(void)
  2104. {
  2105.  
  2106.     if (this->IsShown() && this->Focus())        // leaves view focused if it is drawable 
  2107.     {
  2108.         Rect r;
  2109.  
  2110.         this->GetDrawableQDRect(r);
  2111.         return !r.Empty();
  2112.     }
  2113.     else
  2114.         return FALSE;
  2115. }
  2116.  
  2117. //--------------------------------------------------------------------------------------------------
  2118. #pragma segment MAViewRes
  2119.  
  2120. pascal Boolean TView::IsVisible(void)
  2121. {
  2122.     if (this->IsShown())
  2123.     {
  2124.         Rect r;
  2125.  
  2126.         this->GetVisibleQDRect(r);
  2127.         return !r.Empty();
  2128.     }
  2129.     else
  2130.         return FALSE;
  2131. }
  2132.  
  2133. //--------------------------------------------------------------------------------------------------
  2134. #pragma segment MAViewRes
  2135.  
  2136. pascal TView* TView::LastSubViewThat(pascal Boolean(* TestSubView)(TView* theSubView,
  2137.                                                                    void* staticLink),
  2138.                                      void* staticLink)
  2139. {
  2140.     if (fSubViews)
  2141.         return (TView *)(fSubViews->LastThat((TestObjectType)TestSubView, staticLink));
  2142.     else
  2143.         return NULL;
  2144. }
  2145.  
  2146. //--------------------------------------------------------------------------------------------------
  2147. #pragma segment MAViewRes
  2148.  
  2149. pascal void TView::LocalToSuper(VPoint& thePoint)
  2150. {
  2151.     VPoint localOriginInSuper;
  2152.  
  2153.     this->GetLocalOffsetInSuper(localOriginInSuper);
  2154.     thePoint += localOriginInSuper;
  2155. }
  2156.  
  2157. //--------------------------------------------------------------------------------------------------
  2158. #pragma segment MAViewRes
  2159.  
  2160. pascal void TView::LocalToSuperVRect(VRect& viewRect)
  2161. {
  2162.     this->LocalToSuper(viewRect[topLeft]);
  2163.     this->LocalToSuper(viewRect[botRight]);
  2164. }
  2165.  
  2166. //--------------------------------------------------------------------------------------------------
  2167. #pragma segment MAViewRes
  2168.  
  2169. pascal void TView::LocalToSuperRgn(RgnHandle& theRgn)
  2170. {
  2171.     VPoint localVOriginInSuper;
  2172.  
  2173.     this->GetLocalOffsetInSuper(localVOriginInSuper);
  2174.     Point localOriginInSuper = localVOriginInSuper;// don't want to require focusing 
  2175.     OffsetRgn(theRgn, localOriginInSuper.h, localOriginInSuper.v);
  2176. }
  2177.  
  2178. //--------------------------------------------------------------------------------------------------
  2179. #pragma segment MAViewRes
  2180.  
  2181. pascal void TView::LocalToWindow(VPoint& thePoint)
  2182. {
  2183.     if (fSuperView)
  2184.     {
  2185.         this->LocalToSuper(thePoint);
  2186.         fSuperView->LocalToWindow(thePoint);
  2187.     }
  2188. }
  2189.  
  2190. //--------------------------------------------------------------------------------------------------
  2191. #pragma segment MANonRes
  2192.  
  2193. pascal void TView::Locate(const VPoint& newLoc,
  2194.                           Boolean invalidate)
  2195. {
  2196.     if (newLoc != fLocation)
  2197.     {
  2198.  
  2199.         if (invalidate)
  2200.             this->ForceRedraw();
  2201.         fLocation = newLoc;
  2202.         this->InvalidateFocus();                // Must re-focus because view moved.
  2203.         this->UpdateCoordinates();
  2204.         gApplication->InvalidateMouseRegions();    // Must re-calc cursor and help regions.
  2205.         if (invalidate)
  2206.             this->ForceRedraw();
  2207.  
  2208.         if (fSuperView)
  2209.             fSuperView->SubViewMoved(this);
  2210.  
  2211.         CSubViewIterator iter(this);
  2212.  
  2213.         for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2214.             theSubView->SuperViewMoved(invalidate);
  2215.     }
  2216. }
  2217.  
  2218. //--------------------------------------------------------------------------------------------------
  2219. #pragma segment MANonRes
  2220.  
  2221. pascal void TView::MakeFirstSubView(TView* theSubView)
  2222. {
  2223.     if (fSubViews)                                //there must be at least one subview
  2224.         if (((TView *)fSubViews->First()) != theSubView)
  2225.         {
  2226.             fSubViews->Delete(theSubView);
  2227.             fSubViews->InsertFirst(theSubView);
  2228.             theSubView->ForceRedraw();
  2229.         }
  2230. }
  2231.  
  2232. //--------------------------------------------------------------------------------------------------
  2233. #pragma segment MANonRes
  2234.  
  2235. pascal void TView::MakeLastSubView(TView* theSubView)
  2236. {
  2237.     if (fSubViews)                                //there must be at least one subview
  2238.         if (((TView *)fSubViews->Last()) != theSubView)
  2239.         {
  2240.             fSubViews->Delete(theSubView);
  2241.             fSubViews->InsertLast(theSubView);
  2242.             theSubView->ForceRedraw();
  2243.         }
  2244. }
  2245.  
  2246. //--------------------------------------------------------------------------------------------------
  2247. #pragma segment MAOpen
  2248.  
  2249. pascal void TView::Open(void)
  2250. {
  2251.     CSubViewIterator iter(this);
  2252.  
  2253.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2254.         theSubView->Open();
  2255. }
  2256.  
  2257. //--------------------------------------------------------------------------------------------------
  2258. #pragma segment MANonRes
  2259.  
  2260. pascal void TView::PageInteriorChanged(const VRect& newInterior)
  2261. {
  2262. }
  2263.  
  2264. //--------------------------------------------------------------------------------------------------
  2265. #pragma segment MAViewRes
  2266.  
  2267. pascal void TView::QDToViewPt(Point qdPoint,
  2268.                               VPoint& viewPt)
  2269. {
  2270.     viewPt = VPoint(qdPoint) + fViewToQDOffset;
  2271. }
  2272.  
  2273. //--------------------------------------------------------------------------------------------------
  2274. #pragma segment MAViewRes
  2275.  
  2276. pascal void TView::QDToViewRect(const Rect& qdRect,
  2277.                                 VRect& viewRect)
  2278. {
  2279.     viewRect = qdRect + fViewToQDOffset;
  2280. }
  2281.  
  2282. //--------------------------------------------------------------------------------------------------
  2283. #pragma segment MANonRes
  2284.  
  2285. pascal void TView::RemovedASubView(TView* theSubView)
  2286. {
  2287.     if (fSuperView)
  2288.         fSuperView->RemovedASubView(theSubView);
  2289. }
  2290.  
  2291. //--------------------------------------------------------------------------------------------------
  2292. #pragma segment MANonRes
  2293.  
  2294. pascal void TView::RemoveSubView(TView* theSubView)
  2295. {
  2296.     if (qDebug)
  2297.         FailNonObject(theSubView);
  2298.  
  2299.     if (fSubViews)
  2300.     {
  2301.         fSubViews->Delete(theSubView);
  2302.  
  2303.         // If there aren't any subviews left we free the list 
  2304.         if (fSubViews->IsEmpty())
  2305.             fSubViews = (TList *)FreeIfObject(fSubViews);
  2306.     }
  2307.  
  2308.     this->RemovedASubView(theSubView);
  2309.     theSubView->BeInSuperView(NULL);
  2310. }
  2311.  
  2312. //--------------------------------------------------------------------------------------------------
  2313. #pragma segment MANonRes
  2314.  
  2315. pascal void TView::Resize(const VPoint& newSize,
  2316.                           Boolean invalidate)
  2317. {
  2318.     if (newSize != fSize)
  2319.     {
  2320.         VPoint delta;
  2321.         VRect oldFrame;
  2322.         VRect newFrame;
  2323.         VRect badVRect;
  2324.  
  2325.         if (invalidate)
  2326.             this->GetFrame(oldFrame);
  2327.  
  2328.  
  2329.         delta = newSize - fSize;
  2330.         fSize = newSize;
  2331.  
  2332.         this->InvalidateFocus();                // Must re-focus because size changed
  2333.         this->UpdateCoordinates();
  2334.         gApplication->InvalidateMouseRegions();    // Must re-calc cursor and help regions
  2335.  
  2336.         if (invalidate)
  2337.         {
  2338.             this->GetFrame(newFrame);
  2339.  
  2340.             if (this->FocusOnSuperView())
  2341.             {
  2342.                 if (oldFrame.right != newFrame.right)// must invalidate width difference 
  2343.                 {
  2344.                     badVRect = VRect(Min(oldFrame.top, newFrame.top), Min(oldFrame.right, newFrame.right),
  2345.                                      Max(oldFrame.bottom, newFrame.bottom), Max(oldFrame.right, newFrame.right));
  2346.                     if (fSuperView)
  2347.                         fSuperView->InvalidateVRect(badVRect);
  2348.                 }
  2349.  
  2350.                 if (oldFrame.bottom != newFrame.bottom)// must invalidate height difference 
  2351.                 {
  2352.                     badVRect = VRect(Min(oldFrame.bottom, newFrame.bottom), Min(oldFrame.left, newFrame.left),
  2353.                                      Max(oldFrame.bottom, newFrame.bottom), Max(oldFrame.right, newFrame.right));
  2354.                     if (fSuperView)
  2355.                         fSuperView->InvalidateVRect(badVRect);
  2356.                 }
  2357.             }
  2358.         }
  2359.  
  2360.         if (fSuperView)
  2361.             fSuperView->SubViewChangedSize(this, delta);
  2362.  
  2363.         CSubViewIterator iter(this);
  2364.  
  2365.         for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2366.         {
  2367.             // • Let any adorners no that the view might be changing size
  2368.             if ( theSubView->fViewAdornment )
  2369.                 theSubView->fViewAdornment->ViewChangedSize ( this, delta, invalidate );
  2370.  
  2371.             theSubView->SuperViewChangedSize(delta, invalidate);
  2372.         }
  2373.     }
  2374. }
  2375.  
  2376. //--------------------------------------------------------------------------------------------------
  2377. #pragma segment MAScroll
  2378.  
  2379. pascal void TView::RevealBottom(Boolean redraw)
  2380. {
  2381.     this->RevealRect(VRect(fSize, fSize), gZeroVPt, redraw);
  2382. }
  2383.  
  2384. //--------------------------------------------------------------------------------------------------
  2385. #pragma segment MAScroll
  2386.  
  2387. pascal void TView::RevealRect(const VRect& rectToReveal,
  2388.                               const VPoint& minToSee,
  2389.                               Boolean redraw)
  2390. {
  2391.     if (fSuperView)
  2392.     {
  2393.         VRect localRectToReveal(rectToReveal);
  2394.  
  2395.         this->LocalToSuperVRect(localRectToReveal);
  2396.         fSuperView->RevealRect(localRectToReveal, minToSee, redraw);
  2397.     }
  2398. }
  2399.  
  2400. //--------------------------------------------------------------------------------------------------
  2401. #pragma segment MAScroll
  2402.  
  2403. pascal void TView::RevealTop(Boolean redraw)
  2404. {
  2405.     this->RevealRect(gZeroVRect, gZeroVPt, redraw);
  2406. }
  2407.  
  2408. //--------------------------------------------------------------------------------------------------
  2409. #pragma segment MAViewRes
  2410.  
  2411. pascal void TView::SetTarget(TEvtHandler* newTarget)
  2412. {
  2413.     if (fSuperView)
  2414.         fSuperView->SetTarget(newTarget);
  2415. }
  2416.  
  2417. //--------------------------------------------------------------------------------------------------
  2418. #pragma segment MANonRes
  2419.  
  2420. pascal void TView::Show(Boolean state,
  2421.                         Boolean redraw)
  2422. {
  2423.     if (state != fShown)
  2424.     {
  2425.         if (redraw)
  2426.         {
  2427.             fShown = TRUE;                        // So that ForceRedraw works. 
  2428.             this->ForceRedraw();
  2429.         }
  2430.         this->InvalidateFocus();
  2431.         gApplication->InvalidateMouseRegions();    // Must re-calc cursor and help regions
  2432.         fShown = state;
  2433.     }
  2434. }
  2435.  
  2436. //--------------------------------------------------------------------------------------------------
  2437. #pragma segment MAReadFile
  2438.  
  2439. pascal void TView::ShowReverted(void)
  2440. {
  2441.     this->AdjustSize();
  2442.     this->ForceRedraw();
  2443.  
  2444.     CSubViewIterator iter(this);
  2445.  
  2446.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2447.         theSubView->ShowReverted();
  2448. }
  2449.  
  2450. //--------------------------------------------------------------------------------------------------
  2451. #pragma segment MANonRes
  2452.  
  2453. pascal void TView::SubViewChangedSize(TView* theSubView,
  2454.                                       const VPoint& delta)
  2455. {
  2456. }
  2457.  
  2458. //--------------------------------------------------------------------------------------------------
  2459. #pragma segment MANonRes
  2460.  
  2461. pascal void TView::SubViewMoved(TView* theSubView)
  2462. {
  2463. }
  2464.  
  2465. //--------------------------------------------------------------------------------------------------
  2466. #pragma segment MAViewRes
  2467.  
  2468. pascal void TView::SuperToLocal(VPoint& thePoint)
  2469. {
  2470.     VPoint localOriginInSuper;
  2471.  
  2472.     this->GetLocalOffsetInSuper(localOriginInSuper);
  2473.     thePoint -= localOriginInSuper;
  2474. }
  2475.  
  2476. //--------------------------------------------------------------------------------------------------
  2477. #pragma segment MAViewRes
  2478.  
  2479. pascal void TView::SuperToLocalVRect(VRect& viewRect)
  2480. {
  2481.     this->SuperToLocal(viewRect[topLeft]);
  2482.     this->SuperToLocal(viewRect[botRight]);
  2483. }
  2484.  
  2485. //--------------------------------------------------------------------------------------------------
  2486. #pragma segment MAViewRes
  2487.  
  2488. pascal void TView::SuperToLocalRgn(RgnHandle& theRgn)
  2489. {
  2490.     VPoint localVOriginInSuper;
  2491.  
  2492.     this->GetLocalOffsetInSuper(localVOriginInSuper);
  2493.     Point localOriginInSuper = localVOriginInSuper;// don't want to require focusing 
  2494.     OffsetRgn(theRgn, -localOriginInSuper.h, -localOriginInSuper.v);
  2495. }
  2496.  
  2497. //--------------------------------------------------------------------------------------------------
  2498. #pragma segment MANonRes
  2499.  
  2500. pascal void TView::SuperViewChangedSize(const VPoint& delta,
  2501.                                         Boolean invalidate)
  2502. {
  2503.     VPoint newSize = fSize;
  2504.     Boolean needsResizing = FALSE;
  2505.  
  2506.     for (VHSelect vhs = vSel; vhs <= hSel; ++vhs)
  2507.     {
  2508.         if (fSizeDeterminer[vhs] == sizeSuperView)
  2509.             needsResizing = TRUE;
  2510.         else if (fSizeDeterminer[vhs] == sizeRelSuperView)
  2511.         {
  2512.             newSize[vhs] = newSize[vhs] + delta[vhs];
  2513.             needsResizing = TRUE;
  2514.         }
  2515.     }
  2516.  
  2517.     if (needsResizing)
  2518.     {
  2519.         this->ComputeSize(newSize);
  2520.         this->Resize(newSize, invalidate);
  2521.         this->DoPagination();
  2522.     }
  2523. }
  2524.  
  2525. //--------------------------------------------------------------------------------------------------
  2526. #pragma segment MANonRes
  2527.  
  2528. pascal void TView::SuperViewMoved(Boolean invalidate)
  2529. {
  2530. }
  2531.  
  2532. //--------------------------------------------------------------------------------------------------
  2533. #pragma segment MADoCommand
  2534.  
  2535. pascal Boolean TView::IsDoneTracking(void)
  2536. {
  2537.     // Default to mouse-down tracking 
  2538.     return !StillDown();
  2539. }
  2540.  
  2541. //--------------------------------------------------------------------------------------------------
  2542. #pragma segment MADoCommand
  2543.  
  2544. pascal void TView::TrackConstrain(TrackPhase aTrackPhase,
  2545.                                         const VPoint& anchorPoint,
  2546.                                           const VPoint& previousPoint,
  2547.                                         VPoint& nextPoint,
  2548.                                         Boolean mouseDidMove)
  2549. {
  2550. }
  2551.  
  2552. //--------------------------------------------------------------------------------------------------
  2553. #pragma segment MADoCommand
  2554.  
  2555. pascal void TView::TrackFeedback(TrackPhase /* aTrackPhase */,
  2556.                                         const VPoint& anchorPoint,
  2557.                                         const VPoint& /* previousPoint */,
  2558.                                         const VPoint& nextPoint,
  2559.                                         Boolean mouseDidMove,
  2560.                                         Boolean /* turnItOn */)
  2561.  
  2562. // Use code like this to get a flickering rectangle. 
  2563. {
  2564.     if (mouseDidMove)
  2565.     {
  2566.         Rect r;
  2567.         VRect vr(anchorPoint, nextPoint);
  2568.  
  2569.         this->ViewToQDRect(vr, r);
  2570.         PenPat(qd.gray);
  2571.         FrameRect(r);
  2572.     }
  2573. }
  2574.  
  2575. //--------------------------------------------------------------------------------------------------
  2576. #pragma segment MADoCommand
  2577.  
  2578. pascal void TView::TrackMouse(TrackPhase aTrackPhase,
  2579.                                         VPoint& anchorPoint,
  2580.                                         VPoint& previousPoint,
  2581.                                         VPoint& nextPoint,
  2582.                                         Boolean mouseDidMove)
  2583. {
  2584. }
  2585.  
  2586. //--------------------------------------------------------------------------------------------------
  2587. #pragma segment MAViewRes
  2588.  
  2589. pascal void TView::TraverseSubViewsPreOrder(DoToViewType DoToSubView,
  2590.                                             void* staticLink)
  2591. {
  2592.     CSubViewIterator iter(this);
  2593.  
  2594.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2595.     {
  2596.         DoToSubView(theSubView, staticLink);
  2597.         theSubView->TraverseSubViewsPreOrder(DoToSubView, staticLink);
  2598.     }
  2599. }
  2600.  
  2601. //--------------------------------------------------------------------------------------------------
  2602. #pragma segment MAViewRes
  2603.  
  2604. pascal void TView::TraverseSubViewsPostOrder(DoToViewType DoToSubView,
  2605.                                              void* staticLink)
  2606. {
  2607.     CSubViewIterator iter(this);
  2608.  
  2609.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2610.     {
  2611.         theSubView->TraverseSubViewsPostOrder(DoToSubView, staticLink);
  2612.         DoToSubView(theSubView, staticLink);
  2613.     }
  2614. }
  2615.  
  2616. //--------------------------------------------------------------------------------------------------
  2617. #pragma segment MAViewRes
  2618.  
  2619. pascal void TView::Update(void)
  2620. {
  2621.     if (fSuperView)
  2622.         fSuperView->Update();
  2623.     else
  2624.         this->DrawContents();
  2625. }
  2626.  
  2627. //--------------------------------------------------------------------------------------------------
  2628. #pragma segment MAViewRes
  2629.  
  2630. pascal void TView::ValidateVRect(const VRect& goodVRect)
  2631. {
  2632.     Rect r;
  2633.  
  2634.     this->ViewToQDRect(goodVRect, r);
  2635.     this->ValidateRect(r);
  2636. }
  2637.  
  2638. //--------------------------------------------------------------------------------------------------
  2639. #pragma segment MAViewRes
  2640.  
  2641. pascal void TView::ValidateRect(const Rect& goodRect)
  2642. {
  2643.     RgnHandle rgn = NewRgn();            // Believe it or not, QD does this on an invalRect call!
  2644.  
  2645.     RectRgn(rgn, goodRect);
  2646.     this->ValidateRgn(rgn);
  2647.     rgn = DisposeIfRgnHandle(rgn);
  2648. }
  2649.  
  2650. //--------------------------------------------------------------------------------------------------
  2651. #pragma segment MAViewRes
  2652.  
  2653. pascal void TView::ValidateRgn(RgnHandle goodRgn)
  2654. {
  2655.     Rect r;
  2656.  
  2657.     if (this->Focus())
  2658.     {
  2659.         this->ViewToQDRect(fVisibleExtent, r);
  2660.  
  2661. #if qDebug
  2662.         UseTempRgn("TView.ValidateRgn");
  2663. #endif
  2664.  
  2665.         RectRgn(gTempRgn, r);
  2666.         SectRgn(goodRgn, gTempRgn, gTempRgn);
  2667.         if (!EmptyRgn(gTempRgn))
  2668.             this->DoValidateRgn(gTempRgn);
  2669. #if qDebug
  2670.         DoneWithTempRgn();
  2671. #endif
  2672.  
  2673.     }
  2674. }
  2675.  
  2676. //--------------------------------------------------------------------------------------------------
  2677. #pragma segment MAViewRes
  2678.  
  2679. pascal void TView::DoValidateRgn(RgnHandle goodRgn)
  2680. {
  2681.     if (fSuperView)
  2682.         fSuperView->DoValidateRgn(goodRgn);        // see if someone knows how 
  2683. }
  2684.  
  2685. //--------------------------------------------------------------------------------------------------
  2686. #pragma segment MAViewRes
  2687.  
  2688. pascal void TView::ViewEnable(Boolean state,
  2689.                               Boolean redraw)
  2690. {
  2691.     this->SetEnable(state);
  2692.  
  2693.     if (redraw)
  2694.         this->ForceRedraw();
  2695. }
  2696.  
  2697. //--------------------------------------------------------------------------------------------------
  2698. #pragma segment MAViewRes
  2699.  
  2700. pascal Point TView::ViewToQDPt(const VPoint& viewPt)
  2701. {
  2702.     return viewPt - fViewToQDOffset;
  2703. }
  2704.  
  2705. //--------------------------------------------------------------------------------------------------
  2706. #pragma segment MAViewRes
  2707.  
  2708. pascal void TView::ViewToQDRect(const VRect& viewRect,
  2709.                                 Rect& qdRect)
  2710. {
  2711.     qdRect = viewRect - fViewToQDOffset;
  2712. }
  2713.  
  2714. //--------------------------------------------------------------------------------------------------
  2715. #pragma segment MAViewRes
  2716.  
  2717. pascal void TView::WindowToLocal(VPoint& thePoint)
  2718. {
  2719.     if (fSuperView)
  2720.     {
  2721.         fSuperView->WindowToLocal(thePoint);
  2722.         this->SuperToLocal(thePoint);
  2723.     }
  2724. }
  2725.  
  2726. //--------------------------------------------------------------------------------------------------
  2727. #pragma segment MANonRes
  2728.  
  2729. pascal PicHandle TView::AsPict(void)
  2730. {
  2731.     PicHandle pHndl = NULL;
  2732.     Boolean pictureIsOpen;
  2733.     Rect qdExtent;
  2734.     GrafPort tempPort;
  2735.     CGrafPort tempCPort;
  2736.     FailInfo fi;
  2737.  
  2738.     VOLATILE(pHndl);
  2739.     VOLATILE(pictureIsOpen);
  2740.  
  2741.     this->InvalidateFocus();
  2742.  
  2743.     gDrawingPictScrap = TRUE;
  2744.     gDrawingPictScrapView = this;
  2745.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  2746.     {
  2747.         OpenCPort(&tempCPort);
  2748.         this->BeInPort((GrafPort *) & tempCPort);
  2749.     }
  2750.     else
  2751.     {
  2752.         OpenPort(&tempPort);
  2753.         this->BeInPort(&tempPort);
  2754.     }
  2755.     //??? how do we catch openport failures? 
  2756.  
  2757.     if (fi.Try())
  2758.     {
  2759.         if (this->Focus())
  2760.         {
  2761.             //Open color or black && white, depending on the port
  2762.             this->GetQDExtent(qdExtent);
  2763.             pHndl = OpenPicture(qdExtent);
  2764.             pictureIsOpen = TRUE;                // Failure handler needs to close it 
  2765.             FailNIL(pHndl);
  2766.  
  2767.             ClipRect(qdExtent);
  2768.             this->DrawContents();
  2769.             ClosePicture();
  2770.             pictureIsOpen = FALSE;                // Failure handler doesn't need to close it 
  2771.  
  2772.             //  On the 128K ROMs the picFrame will be empty if drawing the
  2773.             // picture failed.    On the 64K ROM's QuickDraw simply bombs.
  2774.             if ((*pHndl)->picFrame.Empty())
  2775.             {
  2776.                 if (qDebug)
  2777.                     fprintf(stderr, "Picture frame is empty!\n");
  2778.                 FailOSErr(memFullErr);
  2779.             }
  2780.  
  2781.             this->InvalidateFocus();
  2782.         }
  2783.         else if (qDebug)                        // can't focus 
  2784.             ProgramBreak("Can't focus view while writing picture to desk scrap");
  2785.  
  2786.         fi.Success();
  2787.     }
  2788.     else                                        // Recover
  2789.         {
  2790.             if (pHndl)
  2791.             {
  2792.                 if (pictureIsOpen)
  2793.                     ClosePicture();
  2794.                 KillPicture(pHndl);
  2795.                 pHndl = NULL;
  2796.             }
  2797.  
  2798.             if (qNeedsColorQD || gConfiguration.hasColorQD)
  2799.                 CloseCPort(&tempCPort);
  2800.             else
  2801.                 ClosePort(&tempPort);
  2802.             SetPort(gWorkPort);
  2803.             this->BeInPort(NULL);
  2804.             gDrawingPictScrap = FALSE;
  2805.             gDrawingPictScrapView = NULL;
  2806.             fi.ReSignal();
  2807.         }
  2808.     if (qNeedsColorQD || gConfiguration.hasColorQD)
  2809.         CloseCPort(&tempCPort);
  2810.     else
  2811.         ClosePort(&tempPort);
  2812.     SetPort(gWorkPort);
  2813.     gDrawingPictScrap = FALSE;
  2814.     gDrawingPictScrapView = NULL;
  2815.     this->BeInPort(NULL);
  2816.  
  2817.     return pHndl;
  2818. }
  2819.  
  2820. //--------------------------------------------------------------------------------------------------
  2821. #pragma segment MANonRes
  2822.  
  2823. pascal void TView::WriteToDeskScrap(void)
  2824. {
  2825.     PicHandle pHndl = NULL;
  2826.     FailInfo fi;
  2827.  
  2828.     VOLATILE(pHndl);
  2829.  
  2830.     if (fi.Try())
  2831.     {
  2832.         pHndl = this->AsPict();
  2833.         FailNIL(pHndl);
  2834.  
  2835.         FailOSErr(gClipboardMgr->PutDeskScrapData('PICT', (Handle)pHndl));
  2836.  
  2837.         KillPicture(pHndl);                        // dispose of picture created by AsPict 
  2838.         pHndl = NULL;                            // So failure handler doesn't to kill it 
  2839.         fi.Success();
  2840.     }
  2841.     else                                        // Recover
  2842.         {
  2843.             if (pHndl)
  2844.             {
  2845.                 if (qDebug)
  2846.                     ProgramBreak("Failed to put PICT-type scrap");
  2847.                 KillPicture(pHndl);
  2848.             }
  2849.             pHndl = NULL;
  2850.             fi.ReSignal();
  2851.         }
  2852. }
  2853.  
  2854. //--------------------------------------------------------------------------------------------------
  2855. #pragma segment MANonRes
  2856.  
  2857. pascal void TView::GoAwayByUser(const VPoint& theMouse)
  2858. {
  2859. }
  2860.  
  2861. //--------------------------------------------------------------------------------------------------
  2862. #pragma segment MANonRes
  2863.  
  2864. pascal void TView::MoveByUser(const VPoint& theMouse)
  2865. {
  2866. }
  2867.  
  2868. //--------------------------------------------------------------------------------------------------
  2869. #pragma segment MANonRes
  2870.  
  2871. pascal void TView::ResizeByUser(const VPoint& theMouse)
  2872. {
  2873. }
  2874.  
  2875. //--------------------------------------------------------------------------------------------------
  2876. #pragma segment MANonRes
  2877.  
  2878. pascal void TView::SetHLPenState(HLState fromHL,
  2879.                                  HLState toHL)
  2880. {
  2881.     PatPtr pPat;
  2882.     short mode = patXor;                        // every transition except hlOn <-> hlDim uses patXOR
  2883.  
  2884.     if (fromHL + toHL == hlOffOn)
  2885.         pPat = qd.black;
  2886.  
  2887.     else if (fromHL != toHL)
  2888.         pPat = qd.gray;            // ??? make this pattern a parameter ??? 
  2889.  
  2890.     if (fromHL + toHL == hlDimOn)
  2891.         mode = notPatXor;
  2892.  
  2893.     PenMode(mode);
  2894.     PenPat(pPat);
  2895. }
  2896.  
  2897. //--------------------------------------------------------------------------------------------------
  2898. #pragma segment MAViewRes
  2899.  
  2900. pascal void TView::UpdateCoordinates(void)
  2901. {
  2902.  
  2903.     const short kMaxOriginFixup = 1024;            /* The maximum amout by which to _fixup_ (not
  2904.                                                   offset) the QD origin when focusing. This
  2905.                                                   allows patterns and pixpats to be pinned
  2906.                                                   to global space as they were intended. See
  2907.                                                   note below. Larger values than this may be
  2908.                                                   required for _extremely_ complex pixpats
  2909.                                                   (not patterns as they are restricted to 8
  2910.                                                   bit repetition.) The reason that the fixup
  2911.                                                   is restricted is that you don't want to
  2912.                                                   run off the end of QD space and we already
  2913.                                                   have slop allowance of about 2k in
  2914.                                                   ViewCoords.p. (??? should this be settable
  2915.                                                   for the adventurous?) */
  2916.  
  2917.  
  2918.     /*  First calculate and setup the new origins.
  2919.       But first a word about this approach.  (thanks kaar!)
  2920.       Problems with pattern alignment when using 32bit views occur when virtual
  2921.       origin of the port has coordinates that are not multiples of 8. This
  2922.       can occur if, for example, you scroll a view by increments of other than 8,
  2923.       16, or other multiple of 8.
  2924.       The solution is to appropriately set the origin so that patterns are drawn
  2925.       correctly aligned. This can be done by making sure that we set the origin to
  2926.       some value that will cause QuickDraw to perform in exactly the same way as if
  2927.       the origin were really at the value specified by fViewToQDOffset. Since classic
  2928.       QuickDraw patterns repeat after 8 pixels, we can accomplish this task by making
  2929.       sure that fViewToQDOffset is a multiple of 8, and setting the origin to compensate
  2930.       by setting it to fViewToQDOffset % 8.
  2931.       Unfortunately, this will only work for classic QuickDraw patterns that repeat
  2932.       after 8 pixels. With PixPats, this period can be any power of 2. In order to
  2933.       support them, we have to make sure that fViewToQDOffset must be only a multiple of
  2934.       whatever power of 2 is being used. Ideally, therefore, we could like to make
  2935.       the chunkiness of fViewToQDOffset to be as large as possible. On the other hand, we
  2936.       can make it too large, or else the amount by which we offset the origin in
  2937.       TView.Focus may overflow QuickDraw's limits. Probably a value of 1024 or
  2938.       2048 should be sufficient enough to accomodate the largest of PixPats a
  2939.       developer may use, and still be small enough that we shouldn't overflow
  2940.       QuickDraw.
  2941.     */
  2942.     if (fSuperView)
  2943.     {
  2944.         VPoint localOriginInSuper;
  2945.         this->GetLocalOffsetInSuper(localOriginInSuper);
  2946.         VPoint actualViewToQDOffset = fSuperView->fViewToQDOffset - localOriginInSuper;
  2947.  
  2948.         Point deltaOrigin = actualViewToQDOffset;
  2949.         if (fSize > VPoint(kMaxCoord, kMaxCoord))
  2950.             deltaOrigin = Point((short)(deltaOrigin.v % kMaxOriginFixup), (short)(deltaOrigin.h % kMaxOriginFixup));
  2951.         fQDOrigin = fSuperView->fQDOrigin + deltaOrigin;
  2952.         fViewToQDOffset = actualViewToQDOffset - VPoint(deltaOrigin);
  2953.  
  2954.         // Update my visible extent by intersecting my extent with my superview's visible extent
  2955.  
  2956.         // When printing, or drawing the scrap, the grafPort is supplied.
  2957.         if ((gDrawingPictScrapView == this) || ((gCurrPrintHandler != NULL) && (gCurrPrintHandler->fView == this)))
  2958.         {
  2959.             VRect itsExtent;
  2960.             this->GetExtent(itsExtent);
  2961.             fVisibleExtent = itsExtent;
  2962.         }
  2963.         else
  2964.         {
  2965.             VRect itsFrame;
  2966.             this->GetFrame(itsFrame);
  2967.             fVisibleExtent = itsFrame & fSuperView->fVisibleExtent;
  2968.             this->SuperToLocalVRect(fVisibleExtent);
  2969.         }
  2970.     }
  2971.     else
  2972.     {
  2973.         VRect itsExtent;
  2974.  
  2975.         fViewToQDOffset = gZeroVPt;
  2976.         fQDOrigin = gZeroPt;
  2977.         this->GetExtent(itsExtent);
  2978.         fVisibleExtent = itsExtent;
  2979.     }
  2980.  
  2981.     CSubViewIterator iter(this);
  2982.  
  2983.     for (TView * theSubView = iter.FirstSubView(); iter.More(); theSubView = iter.NextSubView())
  2984.         theSubView->UpdateCoordinates();
  2985. }
  2986.  
  2987. //--------------------------------------------------------------------------------------------------
  2988. #pragma segment MANonRes
  2989.  
  2990. pascal void TView::ZoomByUser(const VPoint& theMouse,
  2991.                               short partCode)
  2992. {
  2993. }
  2994.  
  2995. //--------------------------------------------------------------------------------------------------
  2996. #pragma segment MAFields
  2997.  
  2998. pascal void TView::Fields(TObject* obj)            // override 
  2999. {
  3000.     obj->DoToField("TView", (Ptr)NULL, bClass);
  3001.     obj->DoToField("fSuperView", (Ptr) & fSuperView, bObject);
  3002.     obj->DoToField("fSubViews", (Ptr) & fSubViews, bObject);
  3003.     obj->DoToField("fDocument", (Ptr) & fDocument, bObject);
  3004.     obj->DoToField("fLocation", (Ptr) & fLocation, bVPoint);
  3005.     obj->DoToField("fSize", (Ptr) & fSize, bVPoint);
  3006.     obj->DoToField("fTranslation", (Ptr) & fTranslation, bVPoint);
  3007.     obj->DoToField("fSizeDeterminer[hSel]", &fSizeDeterminer[hSel], bSizeDeterminer);
  3008.     obj->DoToField("fSizeDeterminer[vSel]", &fSizeDeterminer[vSel], bSizeDeterminer);
  3009.     obj->DoToField("fHLDesired", (Ptr) & fHLDesired, bHLState);
  3010.     obj->DoToField("fShown", (Ptr) & fShown, bBoolean);
  3011.     obj->DoToField("fPrintHandler", (Ptr) & fPrintHandler, bObject);
  3012.  
  3013.     obj->DoToField("fViewToQDOffset", (Ptr) & fViewToQDOffset, bVPoint);
  3014.     obj->DoToField("fQDOrigin", (Ptr) & fQDOrigin, bPoint);
  3015.     obj->DoToField("fVisibleExtent", (Ptr) & fVisibleExtent, bVRect);
  3016.  
  3017.     obj->DoToField("fHandlesCursor", (Ptr) & fHandlesCursor, bBoolean);
  3018.     obj->DoToField("fSubViewsHandleCursor", (Ptr) & fShown, bBoolean);
  3019.     obj->DoToField("fHandlesHelp", (Ptr) & fShown, bBoolean);
  3020.     obj->DoToField("fSubViewsHandleHelp", (Ptr) & fShown, bBoolean);
  3021.  
  3022.     inherited::Fields(obj);
  3023. }
  3024.  
  3025.  
  3026.