home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / Developer University / DU Projects / GraphicsBfr / Sources / Frame.cpp < prev    next >
Encoding:
Text File  |  1996-08-22  |  12.7 KB  |  437 lines  |  [TEXT/CWIE]

  1. //    Release Version:    $ ODF 2 $
  2. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  3.  
  4. //=======================================================================
  5. #ifndef FRAME_H
  6. #include "Frame.h"
  7. #endif
  8.  
  9. #ifndef PART_H
  10. #include "Part.h"
  11. #endif
  12.  
  13. // ----- Framework Layer -----
  14. #ifndef FWCONTXT_H
  15. #include "FWContxt.h"            // FW_CViewContext
  16. #endif
  17.  
  18. #ifndef FWEVENT_H
  19. #include "FWEvent.h"            // FW_CMenuEvent, FW_CMouseEvent
  20. #endif
  21.  
  22. #ifndef FWPRHDLR_H
  23. #include "FWPrHdlr.h"            // FW_CPrintHandler
  24. #endif
  25.  
  26. // ----- OS Layer -----
  27. #ifndef FWMENU_H
  28. #include "FWMenu.h"                // FW_CMenuBar, etc.
  29. #endif
  30.  
  31. #ifndef FWCFMRES_H
  32. #include "FWCFMRes.h"            // FW_PSharedLibraryResourceFile
  33. #endif
  34.  
  35. #ifndef FWODGEOM_H
  36. #include "FWODGeom.h"            // FW_NewODShape
  37. #endif
  38.  
  39. #ifndef SLMIXOS_H
  40. #include "SLMixOS.h"            // FW_Beep
  41. #endif
  42.  
  43. // ----- Graphic Includes -----
  44. #ifndef FWRECT_H
  45. #include <FWRect.h>                // FW_CRect
  46. #endif
  47.  
  48. #ifndef FWRECSHP_H
  49. #include "FWRecShp.h"            // FW_CRectShape
  50. #endif
  51.  
  52. #ifndef FWPOLY_H
  53. #include "FWPoly.h"                // FW_PPolygon
  54. #endif
  55.  
  56. #ifndef FWPOLYSH_H
  57. #include "FWPolySh.h"            // FW_CPolygonShape
  58. #endif
  59.  
  60. #ifndef FWOVLSHP_H
  61. #include "FWOvlShp.h"            // FW_COvalShape
  62. #endif
  63.  
  64. #ifndef FWPICSHP_H
  65. #include "FWPicShp.h"            // FW_CPicture, FW_CPictureShape
  66. #endif
  67.  
  68. #ifndef FWTXTSHP_H
  69. #include "FWTxtShp.h"            // FW_CTextShape
  70. #endif
  71.  
  72. #ifndef FWLINSHP_H
  73. #include "FWLinShp.h"            // FW_CLineShape
  74. #endif
  75.  
  76. #ifndef FWRGNSHP_H
  77. #include "FWRgnShp.h"            // FW_CRegionShape
  78. #endif
  79.  
  80. //========================================================================================
  81. #ifdef FW_BUILD_MAC
  82. #pragma segment GraphicsBfr
  83. #endif
  84.  
  85. //========================================================================================
  86. FW_DEFINE_AUTO(CGraphicsBfrFrame)
  87.  
  88. //========================================================================================
  89. CGraphicsBfrFrame::CGraphicsBfrFrame(Environment* ev, ODFrame* odFrame, 
  90.                                     FW_CPresentation* presentation, CGraphicsBfrPart* part)
  91.   : FW_CFrame(ev, odFrame, presentation, part),
  92.       fLargePictureShape(NULL),
  93.       fLineShape(NULL),
  94.       fRegionShape(NULL),
  95.       fPolygonShape(NULL),
  96.       fBitmap(1, 1, 0),
  97.       fBitmapShape(NULL),
  98.       fDisplayChanged(TRUE)
  99. {
  100.     fLargePictureShape = MyCreatePictureShape(ev);
  101.     fLineShape = MyCreateLineShape(ev);
  102.     fRegionShape = MyCreateRegionShape(ev);
  103.     fPolygonShape = MyCreatePolygonShape(ev);
  104.     this->MyUpdateBitmap(ev);
  105.     FW_END_CONSTRUCTOR
  106. }
  107.  
  108. //----------------------------------------------------------------------------------------
  109. CGraphicsBfrFrame::~CGraphicsBfrFrame()
  110. {
  111.     FW_START_DESTRUCTOR
  112.     delete fLargePictureShape;
  113.     delete fLineShape;
  114.     delete fRegionShape;
  115.     delete fPolygonShape;
  116.     delete fBitmapShape;
  117. }
  118.  
  119. //------------------------------------------------------------------------------
  120. FW_CPictureShape*
  121. CGraphicsBfrFrame::MyCreatePictureShape(Environment* ev)
  122. {
  123.     FW_PSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  124.  
  125.     // draw large picture - use natural picture size
  126.     const short kMacPictID = 2001;
  127.     const short kCatPictID = 2002;
  128.     const short kOPFIconPictID = 2003;
  129.     FW_CPicture picture(resFile, kMacPictID);
  130.     picture.GetPictBounds(fLargePictRect);
  131.     return FW_NEW(FW_CPictureShape, (picture, fLargePictRect) );
  132. }
  133.  
  134. //------------------------------------------------------------------------------
  135. FW_CLineShape*
  136. CGraphicsBfrFrame::MyCreateLineShape(Environment* ev)
  137. {
  138.     FW_UNUSED(ev);
  139.     // derived from FW_CAutoDestructObject
  140.     FW_CLineShape* shp = FW_NEW(FW_CLineShape,
  141.                         (FW_CPoint(FW_IntToFixed(200), FW_IntToFixed(70)),
  142.                          FW_CPoint(FW_IntToFixed(300), FW_IntToFixed(70)) ) );
  143.     shp->GetStyle().SetPenSize(FW_IntToFixed(5));
  144.     shp->GetInk().SetForeColor(FW_kRGBRed);
  145.     return shp;
  146. }
  147.  
  148. //------------------------------------------------------------------------------
  149. FW_CRegionShape*
  150. CGraphicsBfrFrame::MyCreateRegionShape(Environment* ev)
  151. {
  152.     FW_CRegionShape* shp = NULL;
  153. #ifdef FW_BUILD_MAC
  154.     // make an arrow QuickDraw region
  155.     RgnHandle rgnHdl = NewRgn();
  156.     OpenRgn();
  157.     MoveTo(00,10);    // start
  158.     LineTo(40,10);    // right
  159.     LineTo(40,00);    // up
  160.     LineTo(60,20);    // to point
  161.     LineTo(40,40);    // from point
  162.     LineTo(40,30);    // up
  163.     LineTo(00,30);    // left
  164.     LineTo(00,10);    // up
  165.     CloseRgn(rgnHdl);
  166.     // make OpenDoc shape
  167.     ODShape* odArrowShape = FW_NewODShape(ev, rgnHdl);
  168.     // make ODF shape
  169.     shp = FW_NEW(FW_CRegionShape, (odArrowShape, FW_kFill) );
  170.     shp->MoveShape(FW_IntToFixed(40), FW_IntToFixed(150));
  171.     shp->GetInk().SetForeColor(FW_kRGBRed);
  172. #endif
  173.     return shp;
  174. }
  175.  
  176. //------------------------------------------------------------------------------
  177. FW_CPolygonShape*
  178. CGraphicsBfrFrame::MyCreatePolygonShape(Environment* ev)
  179. {
  180.     FW_UNUSED(ev);
  181.     FW_CPoint* points = new FW_CPoint[8];
  182.     points[0] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  183.     points[1] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(330));
  184.     points[2] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(320));
  185.     points[3] = FW_CPoint(FW_IntToFixed(290), FW_IntToFixed(340));
  186.     points[4] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(360));
  187.     points[5] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(350));
  188.     points[6] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(350));
  189.     points[7] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  190.     FW_CPolygon poly(8, points);
  191.     delete [] points;
  192.     FW_Boolean kAutoCloseFrame    = true;             
  193.     FW_CPolygonShape* shape = FW_NEW(FW_CPolygonShape, (poly, FW_kFill, kAutoCloseFrame));
  194.     return shape;
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. void 
  199. CGraphicsBfrFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)    // Override
  200. {
  201.     ODCanvas* canvas = odFacet->GetCanvas(ev);
  202.     FW_Boolean dynamicCanvas = canvas->IsDynamic(ev);
  203.     FW_CViewContext vContext(ev, this, odFacet, invalidShape);
  204.     if (dynamicCanvas) {                    // not printing
  205.         this->MyDrawToBitmap(ev, invalidShape);
  206.         fBitmapShape->Render(vContext);
  207.         }
  208.     else
  209.         this->MyDrawAllStuff(ev, vContext);    // for printing    
  210. }
  211.  
  212. //------------------------------------------------------------------------------
  213. void 
  214. CGraphicsBfrFrame::MyDrawBackground(Environment* ev, FW_CGraphicContext& gc)
  215. {
  216.     FW_CRect box = this->GetBounds(ev);    
  217.     FW_CRectShape::RenderRect(gc, box, FW_kFill, FW_kRGBLightGray);
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. void 
  222. CGraphicsBfrFrame::MyDrawAllStuff(Environment* ev, FW_CGraphicContext& gc)
  223. {
  224.     this->MyDrawBackground(ev, gc);
  225.     this->MyDrawPictures(ev, gc);
  226.     this->MyDrawShapes(ev, gc);
  227.     this->MyDrawLine(ev, gc);
  228.     this->MyDrawText(ev, gc);
  229.     this->MyDrawPolygon(ev, gc);
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. void 
  234. CGraphicsBfrFrame::MyDrawToBitmap(Environment *ev, ODShape* invalidShape)
  235. {
  236.     if (fDisplayChanged) {
  237.         FW_CBitmapContext context(ev, fBitmap);
  238.         context.SetClip(invalidShape);
  239.         this->MyDrawAllStuff(ev, context);
  240.         fDisplayChanged = FALSE;
  241.         }
  242. }
  243.  
  244. //----------------------------------------------------------------------------------------
  245. void 
  246. CGraphicsBfrFrame::FrameShapeChanged(Environment* ev)    // override
  247. {
  248.     FW_CFrame::FrameShapeChanged(ev);
  249.     fDisplayChanged = true;
  250.     this->MyUpdateBitmap(ev);
  251. }
  252.  
  253. //----------------------------------------------------------------------------------------
  254. void 
  255. CGraphicsBfrFrame::MyUpdateBitmap(Environment* ev)
  256. {
  257.     fFrameRect = this->GetBounds(ev);        // Get new frame rect
  258.     const short kPixelSize = 0;
  259.     const FW_Boolean kScaleBitmap = true;
  260.     fBitmap.ChangeBitmap(    FW_FixedToInt(fFrameRect.Width()), 
  261.                             FW_FixedToInt(fFrameRect.Height()),
  262.                             kPixelSize,
  263.                             kScaleBitmap);
  264.     if (fBitmapShape)
  265.         delete fBitmapShape;
  266.     fBitmapShape = FW_NEW(FW_CBitmapShape, (fBitmap, fFrameRect));
  267.     FW_CAcquiredODShape invalidShape = ::FW_NewODShape(ev, fFrameRect);
  268.     this->MyDrawToBitmap(ev, invalidShape);
  269. }
  270.  
  271. //------------------------------------------------------------------------------
  272. void 
  273. CGraphicsBfrFrame::MyDrawShapes(Environment* ev, FW_CGraphicContext& gc)
  274. {
  275.     FW_UNUSED(ev);
  276.     FW_CRect         boxRect(FW_IntToFixed(25), FW_IntToFixed (25), FW_IntToFixed(100), FW_IntToFixed(100));
  277.     
  278.     // draw filled black box
  279.     FW_CRectShape     boxShape(boxRect, FW_kFill);
  280.     boxShape.Render(gc);
  281.  
  282.     // draw filled green box
  283.     boxShape.MoveShape(FW_IntToFixed(25), FW_IntToFixed(25));
  284.     boxShape.GetInk().SetForeColor(FW_kRGBGreen);
  285.     boxShape.Render(gc);
  286.     
  287.     // draw framed black box
  288.     boxRect.Offset(FW_IntToFixed(50), FW_IntToFixed(50));
  289.     FW_CRectShape::RenderRect(gc, boxRect, FW_kFrame);
  290.     
  291.     // draw oval inscribed in box; special pattern, transfer mode
  292.     boxRect.Offset(FW_IntToFixed(25), FW_IntToFixed(25));
  293.     const FW_CColor kForeColor(FW_kRGBRed);
  294.     const FW_CColor kBackColor(FW_kRGBBlue);
  295.     FW_CInk ink(kForeColor, kBackColor, FW_kXOr);
  296.     const FW_Fixed kPenSize = FW_IntToFixed(4);
  297.     const FW_CPattern kPattern = FW_kDiagCrossPat;
  298.     FW_CStyle style(kPenSize, kPattern);
  299.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFill, ink, style);
  300.  
  301.     // draw oval frame
  302.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFrame);
  303.     
  304.     // draw arrow
  305.     fRegionShape->Render(gc);
  306. }
  307.  
  308. //------------------------------------------------------------------------------
  309. void 
  310. CGraphicsBfrFrame::MyDrawText(Environment* ev, FW_CGraphicContext& gc)
  311. {
  312.     FW_UNUSED(ev);
  313.     FW_CString32 string("ODF");
  314.     FW_CPoint position( FW_IntToFixed(200), FW_IntToFixed(50) );
  315.  
  316.     // draw 12 point ODF
  317.     FW_CTextShape stringShape(string, position.x, position.y);
  318.     stringShape.Render(gc);
  319.  
  320.     // draw fancy ODF
  321.     const FW_Fixed kFontSize = FW_IntToFixed(48);
  322.     FW_CTextShape::RenderText(gc, 
  323.                             string, 
  324.                             position,
  325.                               FW_CFont(FW_GetPalatinoFontName(), FW_kItalic, kFontSize), 
  326.                               FW_kTextAlignLeft | FW_kTextAlignBottom,
  327.                               FW_CInk(FW_kRGBBlue, FW_kRGBWhite, FW_kOr));
  328. }
  329.  
  330. //------------------------------------------------------------------------------
  331. void 
  332. CGraphicsBfrFrame::MyDrawPictures(Environment* ev, FW_CGraphicContext& gc)
  333. {
  334.     FW_PSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  335.  
  336.     fLargePictureShape->Render(gc);
  337.     
  338.     // draw DevU logo - I choose size
  339.     const short kDevUPictID = 2000;
  340.     const short kCatPictID = 2002;
  341.     const short kOPFIconPictID = 2003;
  342.      FW_CPictureShape::RenderPicture(gc,
  343.                                     FW_CPicture(resFile, kDevUPictID),
  344.                                     FW_CRect(FW_IntToFixed(25), FW_IntToFixed(175), FW_IntToFixed(125), FW_IntToFixed(275))
  345.                                     );
  346. }
  347.  
  348. //------------------------------------------------------------------------------
  349. void 
  350. CGraphicsBfrFrame::MyDrawLine(Environment* ev, FW_CGraphicContext& gc)
  351. {
  352.     FW_UNUSED(ev);
  353.     fLineShape->Render(gc);
  354. }
  355.  
  356. //------------------------------------------------------------------------------
  357. void 
  358. CGraphicsBfrFrame::MyDrawPolygon(Environment* ev, FW_CGraphicContext& gc)
  359. {
  360.     FW_UNUSED(ev);
  361.     fPolygonShape->Render(gc);
  362. }
  363.  
  364. //----------------------------------------------------------------------------------------
  365. FW_Handled 
  366. CGraphicsBfrFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  367. {    
  368.     FW_Handled eventHandled = FALSE;
  369.     FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  370.     this->GetContentView(ev)->FrameToViewContent(ev, where);
  371.     ODFacet* theFacet = theMouseEvent.GetFacet(ev);
  372.     FW_CViewContext context(ev, this, theFacet, NULL);    // invalidShape = NULL
  373.     const FW_Fixed kTolerance = FW_IntToFixed(2);
  374.     if ( fLineShape->HitTest(context, where, kTolerance) ) {            // new argument for d9
  375.         FW_Beep();
  376.         eventHandled = TRUE;
  377.         this->MyInvalidateLine(ev);
  378.         fLineShape->MoveShape(FW_IntToFixed(0), FW_IntToFixed(20));        // move down
  379.         this->MyInvalidateLine(ev);    
  380.         fDisplayChanged = true;    
  381.         }    // if
  382.  
  383.     if ( fRegionShape->HitTest(context, where, kTolerance) ) {
  384.         FW_Beep();
  385.         eventHandled = true;
  386.         this->Invalidate(ev, fRegionShape->GetODShape() );    // won't blink when buffered
  387.         }    // if
  388.  
  389.     if ( fPolygonShape->HitTest(context, where, kTolerance) ) {
  390.         FW_Beep();
  391.         eventHandled = true;
  392.         }    // if
  393.  
  394.     return eventHandled;
  395. }
  396.  
  397. //------------------------------------------------------------------------------
  398. void 
  399. CGraphicsBfrFrame::MyInvalidateLine(Environment* ev)
  400. {
  401.     FW_CPoint start;
  402.     FW_CPoint end;
  403.     fLineShape->GetGeometry(start, end);
  404.     FW_CRect lineRect(start.x, start.y, end.x, end.y);
  405.     lineRect.Sort();
  406.     FW_Fixed halfPen = fLineShape->GetPenSize() / FW_IntToFixed(2);
  407.     lineRect.Inset(-halfPen, -halfPen);        // expand for pen size
  408.     this->Invalidate(ev, lineRect);            // force update
  409. }
  410.  
  411. //------------------------------------------------------------------------------
  412. FW_CPrintHandler* 
  413. CGraphicsBfrFrame::NewPrintHandler(Environment* ev)
  414. {
  415.     FW_CPart* part = GetPart(ev);
  416.     return new FW_CPrintHandler(part, this);
  417. }
  418.  
  419. //------------------------------------------------------------------------------
  420. FW_Boolean 
  421. CGraphicsBfrFrame::IsCurrentlyPrintable(Environment* ev) const
  422. {
  423.     FW_UNUSED(ev);
  424.     return TRUE;
  425. }
  426.  
  427. //------------------------------------------------------------------------------
  428. void 
  429. CGraphicsBfrFrame::GetPrintContentExtent(Environment* ev, FW_CPoint& extent) const
  430. {
  431.     FW_UNUSED(ev);
  432.     extent = fLargePictRect.Size();
  433.     FW_ASSERT(extent.x != FW_kFixed0);
  434.     FW_ASSERT(extent.y != FW_kFixed0);
  435. }
  436.  
  437.