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 / ODFDev / Draw / Sources / Ruler.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.3 KB  |  334 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Ruler.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "ODFDraw.hpp"
  11.  
  12. #ifndef CONSTANT_H
  13. #include "Constant.h"
  14. #endif
  15.  
  16. #ifndef RULER_H
  17. #include "Ruler.h"
  18. #endif
  19.  
  20. #ifndef FWFRAME_H
  21. #include "FWFrame.h"
  22. #endif
  23.  
  24. #ifndef FWRECSHP_H
  25. #include "FWRecShp.h"            // FW_CRectShape
  26. #endif
  27.  
  28. #ifndef FWTXTSHP_H
  29. #include "FWTxtShp.h"            // FW_CTextShape
  30. #endif
  31.  
  32. #ifndef FWLINSHP_H
  33. #include "FWLinShp.h"            // FW_CLineShape
  34. #endif
  35.  
  36. #ifndef FWCONTXT_H
  37. #include "FWContxt.h"
  38. #endif
  39.  
  40. #ifndef FWODGEOM_H
  41. #include "FWODGeom.h"
  42. #endif
  43.  
  44. // ----- OpenDoc Includes -----
  45.  
  46. #ifndef SOM_ODFacet_xh
  47. #include <Facet.xh>
  48. #endif
  49.  
  50. #ifndef SOM_ODWindow_xh
  51. #include <Window.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODShape_xh
  55. #include <Shape.xh>
  56. #endif
  57.  
  58. //========================================================================================
  59. // Runtime Informations
  60. //========================================================================================
  61.  
  62. #ifdef FW_BUILD_MAC
  63. #pragma segment odfdraw
  64. #endif
  65.  
  66. FW_DEFINE_AUTO(CRuler)
  67. FW_DEFINE_CLASS_M1(CRuler, FW_CSuperView)
  68.  
  69. const FW_ClassTypeConstant FW_LRuler = FW_TYPE_CONSTANT('r','u','l','r');
  70. FW_REGISTER_ARCHIVABLE_CLASS(FW_LRuler, CRuler, CRuler::Create, FW_CView::Read, CRuler::Destroy, FW_CView::Write)
  71.  
  72. //========================================================================================
  73. // CLASS CRuler
  74. //========================================================================================
  75.  
  76. const FW_Fixed kRulerExtent = FW_IntToFixed(1000);
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // CRuler::CRuler
  80. //----------------------------------------------------------------------------------------
  81.  
  82. CRuler::CRuler(Environment* ev, 
  83.                 FW_CSuperView* container,
  84.                 const FW_CRect& bounds,
  85.                 FW_EScrollingDirection scrollDir,
  86.                 FW_Fixed zoomFactor) :
  87.     FW_CSuperView(ev, 
  88.                     container, 
  89.                     bounds, 
  90.                     0,
  91.                     (scrollDir & FW_kXScrolling) ? 
  92.                                 FW_CPoint(kRulerExtent, bounds.bottom - bounds.top)
  93.                                 : FW_CPoint(bounds.right - bounds.left, kRulerExtent),
  94.                     scrollDir),
  95.     fLineStyle(FW_kFixed0),
  96.     fFont(FW_GetTimesFontName(), FW_kPlain, FW_IntToFixed(10))
  97. {
  98.     FW_ASSERT(scrollDir != FW_kNoScrolling);
  99.     
  100.     ZoomFactorChanged(ev, zoomFactor);
  101.  
  102.     // Bind the ruler to left or top egde of its superview by default
  103.     FW_ViewBinding    rulerBindings = (IsScrollingInY(ev)) ? 
  104.                 FW_kFixedWidth + FW_kLeftBinding + FW_kTopBinding + FW_kBottomBinding :
  105.                 FW_kFixedHeight + FW_kLeftBinding + FW_kTopBinding + FW_kRightBinding;
  106.     SetBindings(ev, rulerBindings);
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // CRuler::CRuler
  111. //----------------------------------------------------------------------------------------
  112.  
  113. CRuler::CRuler(Environment* ev) :
  114.     FW_CSuperView(ev),
  115.     fLineStyle(FW_kFixed0),
  116.     fFont(FW_GetTimesFontName(), FW_kPlain, FW_IntToFixed(10))
  117. {
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // CRuler::~CRuler
  122. //----------------------------------------------------------------------------------------
  123.  
  124. CRuler::~CRuler()
  125. {
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // CRuler::ZoomFactorChanged
  130. //----------------------------------------------------------------------------------------
  131.  
  132. void CRuler::ZoomFactorChanged(Environment* ev, FW_Fixed zoomFactor)
  133. {
  134.     if (IsScrollingInY(ev))
  135.         fFont.SetFontSize(FW_IntToFixed(10) / zoomFactor);    
  136. }
  137.  
  138. //----------------------------------------------------------------------------------------
  139. // CRuler::Draw
  140. //----------------------------------------------------------------------------------------
  141.  
  142. void CRuler::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  143. {
  144.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  145.     
  146.     FW_CRect extent(FW_kZeroPoint, GetExtent(ev));
  147.         
  148.     extent.Intersection(FW_GetShapeBoundingBox(ev, invalidShape));
  149.  
  150.     FW_CRectShape::RenderRect(vc, extent, FW_kFill, FW_kWhiteEraseInk);
  151.  
  152.     if (IsScrollingInX(ev))
  153.         RenderHorizontalRuler(vc, extent);
  154.     else
  155.         RenderVerticalRuler(vc, extent);
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. // CRuler::RenderGradation
  160. //----------------------------------------------------------------------------------------
  161.  
  162. void CRuler::RenderGradation(FW_CViewContext& vc,
  163.                                 short gradation, 
  164.                                 const FW_CPoint& pos)
  165. {
  166.     FW_CString32 string;
  167.     string.ReplaceAllAsSignedDecimalInteger(gradation);
  168.  
  169.     FW_CTextShape::RenderText(vc, string,
  170.                             FW_CPoint(pos.x - FW_kFixedPos1, pos.y - FW_kFixedPos1),
  171.                             fFont,
  172.                             FW_kTextAlignRight | FW_kTextAlignBottom);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. // CRuler::RenderHorizontalRuler
  177. //----------------------------------------------------------------------------------------
  178.  
  179. void CRuler::RenderHorizontalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  180. {    
  181.     FW_CLineShape::RenderLine(vc, 
  182.                                 FW_CPoint(visibleRect.left, kRulerWidth - FW_kFixedPos1), 
  183.                                 FW_CPoint(visibleRect.right, kRulerWidth - FW_kFixedPos1), 
  184.                                 FW_kNormalInk, fLineStyle);
  185.  
  186.     short h = FW_FixedToInt(kRulerWidth);
  187.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  188.     FW_Fixed half = FW_IntToFixed(h / 2);
  189.     FW_Fixed third = FW_IntToFixed(h / 3);
  190.  
  191.     short short72 = 72;
  192.     
  193.     short gradation = FW_FixedToInt(visibleRect.left) / short72;
  194.     FW_Fixed minX = FW_IntToFixed(short72 * gradation);
  195.     FW_Fixed maxX = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.right + FW_IntToFixed(short72)) / short72));
  196.     
  197.     FW_Fixed fixed18 = FW_IntToFixed(18);
  198.     
  199.     short n = 0;
  200.     FW_Fixed height = full;
  201.     if (minX == FW_kFixed0)
  202.     {
  203.         minX = fixed18;
  204.         n = 1;
  205.         height = third;
  206.     }
  207.     
  208.     FW_CPoint start(minX, kRulerWidth);
  209.     FW_CPoint end(minX, kRulerWidth - height);
  210.     for (FW_Fixed x = minX; x <= maxX; x += fixed18)
  211.     {
  212.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  213.         if (n == 0)
  214.             RenderGradation(vc, gradation, start);
  215.         
  216.         n++;
  217.         if (n == 4)
  218.         {
  219.             n = 0;
  220.             gradation++;
  221.             height = full;
  222.         }
  223.         else if (n == 2)
  224.             height = half;
  225.         else if (n == 1 || n == 3)
  226.             height = third;
  227.         
  228.         start.x += fixed18;
  229.         end.x += fixed18;
  230.         end.y = kRulerWidth - height;
  231.     }
  232. }
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // CRuler::RenderVerticalRuler
  236. //----------------------------------------------------------------------------------------
  237.  
  238. void CRuler::RenderVerticalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  239. {
  240.     FW_CLineShape::RenderLine(vc, 
  241.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.top), 
  242.                                 FW_CPoint(kRulerWidth - FW_kFixedPos1, visibleRect.bottom), 
  243.                                 FW_kNormalInk, fLineStyle);
  244.  
  245.     short h = FW_FixedToInt(kRulerWidth);
  246.     FW_Fixed full = FW_IntToFixed(2 * (h / 3));
  247.     FW_Fixed half = FW_IntToFixed(h / 2);
  248.     FW_Fixed third = FW_IntToFixed(h / 3);
  249.  
  250.     short short72 = 72;
  251.     
  252.     short gradation = FW_FixedToInt(visibleRect.top) / short72;
  253.     FW_Fixed minY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.top) / short72));
  254.     FW_Fixed maxY = FW_IntToFixed(short72 * (FW_FixedToInt(visibleRect.bottom + FW_IntToFixed(short72)) / short72));
  255.     
  256.     FW_Fixed fixed18 = FW_IntToFixed(18);
  257.     
  258.     short n = 0;
  259.     FW_Fixed width = full;
  260.     if (minY == FW_kFixed0)
  261.     {
  262.         minY = fixed18;
  263.         n = 1;
  264.         width = third;
  265.     }
  266.         
  267.     FW_CPoint start(kRulerWidth, minY);
  268.     FW_CPoint end(kRulerWidth - width, minY);
  269.     for (FW_Fixed y = minY; y <= maxY; y += fixed18)
  270.     {
  271.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, fLineStyle);
  272.         if (n == 0)
  273.             RenderGradation(vc, gradation, start);
  274.         
  275.         n++;
  276.         if (n == 4)
  277.         {
  278.             n = 0;
  279.             gradation++;
  280.             width = full;
  281.         }
  282.         else if (n == 2)
  283.             width = half;
  284.         else if (n == 1 || n == 3)
  285.             width = third;
  286.         
  287.         start.y += fixed18;
  288.         end.y += fixed18;
  289.         end.x = kRulerWidth - width;
  290.     }
  291. }
  292.  
  293. //----------------------------------------------------------------------------------------
  294. //    CRuler::Create
  295. //----------------------------------------------------------------------------------------
  296.  
  297. void* CRuler::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  298. {
  299. FW_UNUSED(stream);
  300. FW_UNUSED(type);
  301.     FW_SOMEnvironment ev;
  302.     return FW_NEW(CRuler, (ev));
  303. }
  304.  
  305. //----------------------------------------------------------------------------------------
  306. //    CRuler::Destroy
  307. //----------------------------------------------------------------------------------------
  308.  
  309. void CRuler::Destroy(void* object, FW_ClassTypeConstant type)
  310. {
  311. FW_UNUSED(type);
  312.     CRuler* self = (CRuler*) object;
  313.     delete self;
  314. }
  315.  
  316. //----------------------------------------------------------------------------------------
  317. //    CRuler::Flatten
  318. //----------------------------------------------------------------------------------------
  319.  
  320. void CRuler::Flatten(Environment* ev, FW_CWritableStream& archive) const
  321. {
  322.     FW_CSuperView::Flatten(ev, archive);
  323. }
  324.  
  325. //----------------------------------------------------------------------------------------
  326. //    CRuler::InitializeFromStream
  327. //----------------------------------------------------------------------------------------
  328.  
  329. void CRuler::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  330. {
  331.     FW_CSuperView::InitializeFromStream(ev, stream);
  332. }
  333.  
  334.