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 / Graphics / Sources / Frame.cpp < prev    next >
Encoding:
Text File  |  1996-08-22  |  11.2 KB  |  383 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 FWPRHDLR_H
  19. #include "FWPrHdlr.h"            // FW_CPrintHandler
  20. #endif
  21.  
  22. #ifndef FWEVENT_H
  23. #include "FWEvent.h"            // FW_CMenuEvent, FW_CMouseEvent
  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 Graphics
  83. #endif
  84.  
  85. //========================================================================================
  86. FW_DEFINE_AUTO(CGraphicsFrame)
  87.  
  88. //========================================================================================
  89. CGraphicsFrame::CGraphicsFrame(Environment* ev, ODFrame* odFrame, 
  90.                                 FW_CPresentation* presentation, CGraphicsPart* part)
  91.   : FW_CFrame(ev, odFrame, presentation, part),
  92.       fLargePictureShape(NULL),
  93.       fLineShape(NULL),
  94.       fRegionShape(NULL),
  95.       fPolygonShape(NULL)
  96. {
  97.     fLargePictureShape = MyCreatePictureShape(ev);
  98.     fLineShape = MyCreateLineShape(ev);
  99.     fRegionShape = MyCreateRegionShape(ev);
  100.     fPolygonShape = MyCreatePolygonShape(ev);
  101.     FW_END_CONSTRUCTOR
  102. }
  103.  
  104. //----------------------------------------------------------------------------------------
  105. CGraphicsFrame::~CGraphicsFrame()
  106. {
  107.     FW_START_DESTRUCTOR
  108.     delete fLargePictureShape;
  109.     delete fLineShape;
  110.     delete fRegionShape;
  111.     delete fPolygonShape;
  112. }
  113.  
  114. //------------------------------------------------------------------------------
  115. FW_CPictureShape*
  116. CGraphicsFrame::MyCreatePictureShape(Environment* ev)
  117. {
  118.     FW_UNUSED(ev);
  119.     FW_PSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  120.  
  121.     // draw large picture - use natural picture size
  122.     const short kMacPictID = 2001;
  123.     const short kCatPictID = 2002;
  124.     const short kODFIconPictID = 2003;
  125.     FW_CPicture picture(resFile, kMacPictID);
  126.     picture.GetPictBounds(fLargePictRect);
  127.     return FW_NEW(FW_CPictureShape, (picture, fLargePictRect) );
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131. FW_CLineShape*
  132. CGraphicsFrame::MyCreateLineShape(Environment* ev)
  133. {
  134.     FW_UNUSED(ev);
  135.     FW_CLineShape* shp = FW_NEW(FW_CLineShape,
  136.                         (FW_CPoint(FW_IntToFixed(200), FW_IntToFixed(70)),
  137.                          FW_CPoint(FW_IntToFixed(300), FW_IntToFixed(70)) ) );
  138.     shp->GetStyle().SetPenSize(FW_IntToFixed(5));
  139.     shp->GetInk().SetForeColor(FW_kRGBRed);
  140.     return shp;
  141. }
  142.  
  143. //------------------------------------------------------------------------------
  144. FW_CRegionShape*
  145. CGraphicsFrame::MyCreateRegionShape(Environment* ev)
  146. {
  147.     const FW_CPoint kSize( FW_IntToFixed(25), FW_IntToFixed(25) );
  148.     const FW_CPoint kOffset( FW_IntToFixed(32), FW_IntToFixed(0) );
  149.     const FW_Fixed kInset = FW_IntToFixed(4);
  150.  
  151.     FW_CRect fwR1(FW_kZeroPoint, kSize);
  152.     FW_CAcquiredODShape odR1 = FW_NewODShape (ev, fwR1);
  153.     
  154.     FW_CRect fwR2(fwR1);
  155.     fwR2.Offset(kOffset);
  156.     fwR2.Inset(kInset);
  157.     ODShape* odR2 = FW_NewODShape (ev, fwR2);
  158.  
  159.     odR2->Union(ev, odR1);
  160.     FW_CRegionShape* shp = FW_NEW(FW_CRegionShape, (odR2, FW_kFill) );
  161.     shp->GetInk().SetForeColor(FW_kRGBPurple);
  162.     shp->MoveShape(FW_IntToFixed(40), FW_IntToFixed(300));    // x, y
  163.     return shp;
  164. }
  165.  
  166. //------------------------------------------------------------------------------
  167. FW_CPolygonShape*
  168. CGraphicsFrame::MyCreatePolygonShape(Environment* ev)
  169. {
  170.     FW_UNUSED(ev);
  171.     FW_CPoint* points = new FW_CPoint[8];
  172.     points[0] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  173.     points[1] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(330));
  174.     points[2] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(320));
  175.     points[3] = FW_CPoint(FW_IntToFixed(290), FW_IntToFixed(340));
  176.     points[4] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(360));
  177.     points[5] = FW_CPoint(FW_IntToFixed(270), FW_IntToFixed(350));
  178.     points[6] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(350));
  179.     points[7] = FW_CPoint(FW_IntToFixed(230), FW_IntToFixed(330));
  180.     FW_CPolygon poly(8, points);
  181.     delete [] points;
  182.     FW_Boolean kAutoCloseFrame    = true;             
  183.     FW_CPolygonShape* shape = FW_NEW(FW_CPolygonShape, (poly, FW_kFill, kAutoCloseFrame));
  184.     return shape;
  185. }
  186.  
  187. //----------------------------------------------------------------------------------------
  188. void 
  189. CGraphicsFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
  190. {        
  191.     FW_CFrame::FacetAdded(ev, facet, facetCount);
  192.     if ( this->IsRoot(ev) ) {
  193.         FW_CPoint interiorSize = fLargePictRect.BotRight();
  194.         this->GetWindow(ev)->SetWindowSize(ev, interiorSize);
  195.     }
  196. }
  197.  
  198. //----------------------------------------------------------------------------------------
  199. void 
  200. CGraphicsFrame::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)    // Override
  201. {
  202.     FW_CViewContext context(ev, this, odFacet, invalidShape);
  203.     this->MyDrawBackground(ev, context);
  204.     this->MyDrawPictures(ev, context);
  205.     this->MyDrawShapes(ev, context);
  206.     this->MyDrawLine(ev, context);
  207.     this->MyDrawText(ev, context);
  208.     this->MyDrawPolygon(ev, context);
  209. }
  210.  
  211. //------------------------------------------------------------------------------
  212. void 
  213. CGraphicsFrame::MyDrawBackground(Environment* ev, FW_CGraphicContext& gc)
  214. {
  215.     FW_CRect box = this->GetBounds(ev);    
  216.     FW_CRectShape::RenderRect(gc, box, FW_kFill, FW_kRGBLightGray);
  217. }
  218.  
  219. //------------------------------------------------------------------------------
  220. void 
  221. CGraphicsFrame::MyDrawShapes(Environment* ev, FW_CGraphicContext& gc)
  222. {
  223.     FW_UNUSED(ev);
  224.     FW_CRect boxRect(FW_IntToFixed(25), FW_IntToFixed (25), 
  225.                         FW_IntToFixed(100), FW_IntToFixed(100));
  226.     
  227.     // draw filled black box
  228.     FW_CRectShape     boxShape(boxRect, FW_kFill);
  229.     boxShape.Render(gc);
  230.  
  231.     // draw filled green box
  232.     boxShape.MoveShape(FW_IntToFixed(25), FW_IntToFixed(25));
  233.     boxShape.GetInk().SetForeColor(FW_kRGBGreen);
  234.     boxShape.Render(gc);
  235.     
  236.     // draw framed black box
  237.     boxRect.Offset(FW_IntToFixed(50), FW_IntToFixed(50));
  238.     FW_CRectShape::RenderRect(gc, boxRect, FW_kFrame);
  239.     
  240.     // draw oval inscribed in box; special pattern, transfer mode
  241.     boxRect.Offset(FW_IntToFixed(25), FW_IntToFixed(25));
  242.     const FW_CColor kForeColor(FW_kRGBRed);
  243.     const FW_CColor kBackColor(FW_kRGBBlue);
  244.     FW_CInk ink(kForeColor, kBackColor, FW_kXOr);
  245.     const FW_Fixed kPenSize = FW_IntToFixed(4);
  246.     const FW_CPattern kPattern = FW_kDiagCrossPat;
  247.     FW_CStyle style(kPenSize, kPattern);
  248.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFill, ink, style);
  249.  
  250.     // draw oval frame
  251.     FW_COvalShape::RenderOval(gc, boxRect, FW_kFrame);
  252.     
  253.     // draw arrow
  254.     fRegionShape->Render(gc);
  255. }
  256.  
  257. //------------------------------------------------------------------------------
  258. void 
  259. CGraphicsFrame::MyDrawText(Environment* ev, FW_CGraphicContext& gc)
  260. {
  261.     FW_UNUSED(ev);
  262.     FW_CString32 string("ODF");
  263.     FW_CPoint position( FW_IntToFixed(200), FW_IntToFixed(50) );
  264.  
  265.     // draw 12 point ODF
  266.     FW_CTextShape stringShape(string, position.x, position.y);
  267.     stringShape.Render(gc);
  268.  
  269.     // draw fancy ODF
  270.     const FW_Fixed kFontSize = FW_IntToFixed(48);
  271.     FW_CTextShape::RenderText(gc, 
  272.                             string, 
  273.                             position,
  274.                               FW_CFont(FW_GetPalatinoFontName(), FW_kItalic, kFontSize), 
  275.                               FW_kTextAlignLeft | FW_kTextAlignBottom,
  276.                               FW_CInk(FW_kRGBBlue, FW_kRGBWhite, FW_kOr));
  277. }
  278.  
  279. //------------------------------------------------------------------------------
  280. void 
  281. CGraphicsFrame::MyDrawPictures(Environment* ev, FW_CGraphicContext& gc)
  282. {
  283.     FW_PSharedLibraryResourceFile resFile(ev);    // resource from our shared library
  284.  
  285.     fLargePictureShape->Render(gc);
  286.     
  287.     // draw DevU logo - I choose size
  288.     const short kODFPictID = 2000;
  289.     const short kCatPictID = 2002;
  290.     const short kODFIconPictID = 2003;
  291.      FW_CPictureShape::RenderPicture(gc,
  292.                                     FW_CPicture(resFile, kODFPictID),
  293.                                     FW_CRect(FW_IntToFixed(25), FW_IntToFixed(175), FW_IntToFixed(125), FW_IntToFixed(275))
  294.                                     );
  295. }
  296.  
  297. //------------------------------------------------------------------------------
  298. void 
  299. CGraphicsFrame::MyDrawLine(Environment* ev, FW_CGraphicContext& gc)
  300. {
  301.     FW_UNUSED(ev);
  302.     fLineShape->Render(gc);
  303. }
  304.  
  305. //------------------------------------------------------------------------------
  306. void 
  307. CGraphicsFrame::MyDrawPolygon(Environment* ev, FW_CGraphicContext& gc)
  308. {
  309.     FW_UNUSED(ev);
  310.     fPolygonShape->Render(gc);
  311. }
  312.  
  313. //----------------------------------------------------------------------------------------
  314. FW_Handled 
  315. CGraphicsFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  316. {    
  317.     FW_Handled eventHandled = false;
  318.     FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  319.     this->GetContentView(ev)->FrameToViewContent(ev, where);
  320.     ODFacet* theFacet = theMouseEvent.GetFacet(ev);
  321.     FW_CViewContext fc(ev, this, theFacet, NULL);                // invalidShape = NULL
  322.     const FW_Fixed kTolerance = FW_IntToFixed(2);
  323.     if ( fLineShape->HitTest(fc, where, kTolerance) ) {    
  324.         FW_Beep();
  325.         eventHandled = true;
  326.         this->MyInvalidateLine(ev);
  327.         fLineShape->MoveShape(FW_IntToFixed(0), FW_IntToFixed(20));        // move down
  328.         this->MyInvalidateLine(ev);        
  329.         }    // if
  330.  
  331.     if ( fRegionShape->HitTest(fc, where, kTolerance) ) {
  332.         FW_Beep();
  333.         eventHandled = true;
  334.         this->Invalidate(ev, fRegionShape->GetODShape() );
  335.         }    // if
  336.  
  337.     if ( fPolygonShape->HitTest(fc, where, kTolerance) ) {
  338.         FW_Beep();
  339.         eventHandled = true;
  340.         }    // if
  341.  
  342.     return eventHandled;
  343. }
  344.  
  345. //------------------------------------------------------------------------------
  346. void 
  347. CGraphicsFrame::MyInvalidateLine(Environment* ev)
  348. {
  349.     FW_CPoint start;
  350.     FW_CPoint end;
  351.     fLineShape->GetGeometry(start, end);
  352.     FW_CRect lineRect(start.x, start.y, end.x, end.y);
  353.     lineRect.Sort();
  354.     FW_Fixed halfPen = fLineShape->GetPenSize() / FW_IntToFixed(2);
  355.     lineRect.Inset(-halfPen, -halfPen);        // expand for pen size
  356.     this->Invalidate(ev, lineRect);            // force update
  357. }
  358.  
  359. //------------------------------------------------------------------------------
  360. FW_CPrintHandler* 
  361. CGraphicsFrame::NewPrintHandler(Environment* ev)
  362. {
  363.     FW_CPart* part = GetPart(ev);
  364.     return new FW_CPrintHandler(part, this);
  365. }
  366.  
  367. //------------------------------------------------------------------------------
  368. FW_Boolean 
  369. CGraphicsFrame::IsCurrentlyPrintable(Environment* ev) const
  370. {
  371.     FW_UNUSED(ev);
  372.     return TRUE;
  373. }
  374.  
  375. //------------------------------------------------------------------------------
  376. void 
  377. CGraphicsFrame::GetPrintContentExtent(Environment *ev, FW_CPoint& extent) const
  378. {
  379.     FW_UNUSED(ev);
  380.     extent = fLargePictRect.Size();
  381.     FW_ASSERT(extent.x != FW_kFixed0);
  382.     FW_ASSERT(extent.y != FW_kFixed0);
  383. }