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 / ODF / Framewrk / FWViews / FWStatic.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  11.8 KB  |  384 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWStatic.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWSTATIC_H
  13. #include "FWStatic.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWPOLYSH_H
  21. #include "FWPolySh.h"
  22. #endif
  23.  
  24. #ifndef FWPOLY_H
  25. #include "FWPoly.h"
  26. #endif
  27.  
  28. #ifndef FWCONTXT_H
  29. #include "FWContxt.h"
  30. #endif
  31.  
  32. #ifndef FWTXTSHP_H
  33. #include "FWTxtShp.h"
  34. #endif
  35.  
  36. #ifndef FWRECSHP_H
  37. #include "FWRecShp.h"
  38. #endif
  39.  
  40. //========================================================================================
  41. // File scope definitions
  42. //========================================================================================
  43.  
  44. #ifdef FW_BUILD_MAC
  45. #pragma segment fwgadgts
  46. #endif
  47.  
  48. #define LabelMargin     FW_IntToFixed(4)
  49.     
  50. //========================================================================================
  51. // CLASS FW_CStaticText
  52. //========================================================================================
  53.  
  54. FW_DEFINE_AUTO(FW_CStaticText)
  55. FW_DEFINE_CLASS_M1(FW_CStaticText, FW_CView)
  56.  
  57. // This class is archivable, but we provide the archiving implementation in a separate
  58. // translation unit in order to enable deadstripping of the archiving-related code
  59. // in parts that do not use archiving with this class.
  60.  
  61. //----------------------------------------------------------------------------------------
  62. // FW_CStaticText::FW_CStaticText
  63. //----------------------------------------------------------------------------------------
  64.  
  65. FW_CStaticText::FW_CStaticText (Environment* ev, 
  66.                                 FW_CSuperView* container, 
  67.                                 const FW_CRect& bounds,
  68.                                 ODID viewID,
  69.                                  const FW_CString& text, 
  70.                                  const FW_CFont& font, 
  71.                                  FW_TextBoxOptions options,
  72.                                  const FW_CInk& ink) :
  73.     FW_CView(ev, container, bounds, viewID),
  74.     fTextBoxShape(0),
  75.     fEraseColor(FW_kRGBWhite),
  76.     fEraseOnDraw(false)
  77. {
  78.     Initialize(ev, bounds, text, font, options, ink);
  79.     FW_END_CONSTRUCTOR
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. // FW_CStaticText::FW_CStaticText
  84. //----------------------------------------------------------------------------------------
  85.  
  86. FW_CStaticText::FW_CStaticText (Environment* ev, 
  87.                                 FW_CSuperView* container,
  88.                                 ODID viewID,
  89.                                 const FW_CTextBoxShape& textboxshape) :
  90.     FW_CView(ev, container, FW_kZeroRect, viewID),
  91.     fTextBoxShape(0),
  92.     fEraseColor(FW_kRGBWhite),
  93.     fEraseOnDraw(false)
  94. {
  95.     fTextBoxShape = FW_NEW(FW_CTextBoxShape, (textboxshape));
  96.     
  97.     Disable(ev);    // disable event handler
  98.     
  99.     // set the view bounds with the textbox
  100.     FW_CRect bounds;
  101.     fTextBoxShape->GetTextBox(bounds);
  102.     PrivSetBounds(ev, bounds);                
  103.     FW_END_CONSTRUCTOR
  104. }
  105.                 
  106. //----------------------------------------------------------------------------------------
  107. // FW_CStaticText::FW_CStaticText
  108. //----------------------------------------------------------------------------------------
  109.  
  110. FW_CStaticText::FW_CStaticText (Environment* ev) :
  111.     FW_CView(ev),
  112.     fTextBoxShape(0),
  113.     fEraseColor(FW_kRGBWhite),
  114.     fEraseOnDraw(false)
  115. {
  116.     FW_END_CONSTRUCTOR
  117. }
  118.                 
  119. //----------------------------------------------------------------------------------------
  120. // FW_CStaticText::~FW_CStaticText
  121. //----------------------------------------------------------------------------------------
  122.  
  123. FW_CStaticText::~FW_CStaticText()
  124. {
  125.     FW_START_DESTRUCTOR
  126.     delete fTextBoxShape;
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // FW_CStaticText::Draw
  131. //----------------------------------------------------------------------------------------
  132.  
  133. void FW_CStaticText::Draw (Environment* ev, ODFacet* facet, ODShape* invalidShape)
  134. {
  135. FW_UNUSED(invalidShape);
  136.     FW_CViewContext vc(ev, this, facet); 
  137.     
  138.     if (fEraseOnDraw)
  139.     {
  140.         fEraseOnDraw = false;
  141.         FW_CInk eraseInk(fEraseColor);
  142.         
  143.         FW_CRect invalidRect;
  144.         vc.GetClipRect(invalidRect);
  145.         FW_CRectShape::RenderRect(vc, invalidRect, FW_kFill, eraseInk);
  146.     }
  147.     
  148.     fTextBoxShape->Render(vc);
  149. }
  150.  
  151. //----------------------------------------------------------------------------------------
  152. // FW_CStaticText::SetText
  153. //----------------------------------------------------------------------------------------
  154.  
  155. void FW_CStaticText::SetText(Environment* ev, const FW_CString& text, FW_ERedrawVerb redraw) 
  156. {
  157.     fTextBoxShape->SetString(text);
  158.     
  159.     if (redraw == FW_kInvalidate)
  160.     {
  161.         Invalidate(ev);
  162.         fEraseOnDraw = true;
  163.     }
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. //    FW_CStaticText::Flatten
  168. //----------------------------------------------------------------------------------------
  169.  
  170. void FW_CStaticText::Flatten(Environment* ev, FW_CWritableStream& stream) const
  171. {
  172.     FW_CView::Flatten(ev, stream);
  173.     
  174.     FW_CString text;
  175.     fTextBoxShape->GetString(text);
  176.     
  177.     stream << FW_CFont(FW_kNormalFont);        // [LSD] use default font for now
  178.     stream << text;
  179.     // stream << ink;
  180. }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. //    FW_CStaticText::Initialize
  184. //----------------------------------------------------------------------------------------
  185.  
  186. void FW_CStaticText::Initialize(Environment* ev,
  187.                                 const FW_CRect& bounds,  
  188.                                 const FW_CString& text,
  189.                                 const FW_CFont& font, 
  190.                                 FW_TextBoxOptions options,
  191.                                  const FW_CInk& ink)
  192. {
  193.     // Must reset bounds to (0,0) origin because View drawing is done in view coordinates
  194.     FW_CRect box(FW_kZeroPoint, bounds.Size());
  195.     fTextBoxShape = FW_NEW(FW_CTextBoxShape, (text, box, font, options, ink));
  196.     
  197.     Disable(ev);    // disable event handler
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. //    FW_CStaticText::InitializeFromStream
  202. //----------------------------------------------------------------------------------------
  203.  
  204. void FW_CStaticText::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  205. {
  206.     FW_CView::InitializeFromStream(ev, stream);
  207.  
  208.     FW_CFont font;    
  209.     FW_CString text;
  210.  
  211.     stream >> font;
  212.     stream >> fEraseColor;
  213.     stream >> text;
  214.     
  215.     // FW_CInk    ink;    [LSD] todo
  216.     // stream >> ink;
  217.     FW_CInk ink(FW_kNormalTextInk);
  218.  
  219.     Initialize(ev, GetBounds(ev), text, font, FW_kTextBoxClipToBox, ink);
  220. }
  221.  
  222. //========================================================================================
  223. // CLASS FW_CGroupBox
  224. //========================================================================================
  225.  
  226. FW_DEFINE_AUTO(FW_CGroupBox)
  227. FW_DEFINE_CLASS_M1(FW_CGroupBox, FW_CStaticText)
  228.  
  229. // This class is archivable, but we provide the archiving implementation in a separate
  230. // translation unit in order to enable deadstripping of the archiving-related code
  231. // in parts that do not use archiving with this class.
  232.  
  233. //----------------------------------------------------------------------------------------
  234. // FW_CGroupBox::FW_CGroupBox
  235. //----------------------------------------------------------------------------------------
  236.  
  237. FW_CGroupBox::FW_CGroupBox (Environment* ev, 
  238.                                 FW_CSuperView* container, 
  239.                                 const FW_CRect& bounds,
  240.                                 ODID viewID,
  241.                                  const FW_CString& string, 
  242.                                  const FW_CFont& font, 
  243.                                  FW_TextBoxOptions options,
  244.                                  const FW_CInk& textInk, 
  245.                                  const FW_CInk& frameInk) :
  246.     FW_CStaticText(ev, container, bounds, viewID, string, font, options, textInk),
  247.     fFrameInk(frameInk)
  248. {
  249.     FW_END_CONSTRUCTOR
  250. }
  251.  
  252. //----------------------------------------------------------------------------------------
  253. // FW_CGroupBox::FW_CGroupBox
  254. //----------------------------------------------------------------------------------------
  255.  
  256. FW_CGroupBox::FW_CGroupBox (Environment* ev, 
  257.                             FW_CSuperView* container,
  258.                             ODID viewID,
  259.                             const FW_CTextBoxShape& textbox, 
  260.                             const FW_CInk& frameInk) :
  261.     FW_CStaticText(ev, container, viewID, textbox),
  262.     fFrameInk(frameInk)
  263. {
  264.     FW_END_CONSTRUCTOR
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // FW_CGroupBox::FW_CGroupBox
  269. //----------------------------------------------------------------------------------------
  270.  
  271. FW_CGroupBox::FW_CGroupBox (Environment* ev) :
  272.     FW_CStaticText(ev)
  273. {
  274.     FW_END_CONSTRUCTOR
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. // FW_CGroupBox::~FW_CGroupBox
  279. //----------------------------------------------------------------------------------------
  280.  
  281. FW_CGroupBox::~FW_CGroupBox()
  282. {
  283.     FW_START_DESTRUCTOR
  284. }
  285.  
  286. //----------------------------------------------------------------------------------------
  287. // FW_CGroupBox::Draw
  288. //----------------------------------------------------------------------------------------
  289.  
  290. void FW_CGroupBox::SetLabelRect(Environment* ev, FW_CGraphicContext& gc, FW_CRect& labelRect)
  291. {
  292.     FW_CTextBoxShape* textShape = GetTextBoxShape(ev);
  293.  
  294.     FW_CString255    label;
  295.     textShape->GetString(label);
  296.     
  297.     FW_CPoint textExtent = FW_CTextShape::CalcTextExtent(gc, label,  textShape->GetFont());
  298.     
  299.     labelRect.top = FW_kFixed0;
  300.     labelRect.bottom = textExtent.y;
  301.     
  302.     FW_Fixed    textsize = textExtent.x + LabelMargin;
  303.     FW_Fixed    width = GetSize(ev).x;
  304.     if ((textsize + FW_IntToFixed(2) * LabelMargin) > width )    
  305.         textsize = width - FW_IntToFixed(2) * LabelMargin;    // label is longer than box
  306.     
  307.     switch(textShape->GetOptions() & FW_kPrivTextBoxHorzJusificationMask)
  308.     {
  309.     case FW_kTextBoxJustifyHCenter:
  310.         labelRect.left = (width - textsize) / FW_IntToFixed(2);
  311.         labelRect.right = labelRect.left + textsize;
  312.         break;
  313.     case FW_kTextBoxJustifyRight:
  314.         labelRect.right = width - LabelMargin;
  315.         labelRect.left = labelRect.right - textsize;
  316.         break;
  317.     case FW_kTextBoxJustifyLeft:    
  318.     default:
  319.         labelRect.left = LabelMargin;
  320.         labelRect.right = labelRect.left + textsize;
  321.         break;
  322.     }
  323.     
  324.     // update the label's textbox
  325.     textShape->SetTextBox(labelRect);
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // FW_CGroupBox::Draw
  330. //----------------------------------------------------------------------------------------
  331.  
  332. void FW_CGroupBox::Draw(Environment* ev, ODFacet* facet, ODShape* invalidShape)
  333. {
  334. FW_UNUSED(invalidShape);
  335.     FW_CViewContext gc (ev, this, facet); 
  336.  
  337.     // Must compute the position of the label first
  338.     FW_CRect labelRect;
  339.     SetLabelRect(ev, gc, labelRect);
  340.     
  341.     // Draw box with top side centered on text
  342.     FW_CRect groupRect(FW_kZeroPoint, GetSize(ev));
  343.     groupRect.top += (labelRect.bottom - labelRect.top) / FW_IntToFixed(2);    
  344.  
  345.     // draw bottom and right edge inside the view
  346.     groupRect.bottom -= FW_kFixedPos1;
  347.     groupRect.right -= FW_kFixedPos1;
  348.  
  349.     // Use a polygone instead of drawing the frame to avoid crossing the label
  350.     // (would it work better to clip out the textbox region?)
  351. //    FW_CRectShape groupShape(groupRect, FW_kFrame, fFrameInk);
  352. //    groupShape.Render(gc);
  353.     FW_CPoint points[6] = { FW_CPoint(labelRect.left, groupRect.top),
  354.                             FW_CPoint(groupRect.left, groupRect.top),
  355.                             FW_CPoint(groupRect.left, groupRect.bottom),
  356.                             FW_CPoint(groupRect.right, groupRect.bottom),
  357.                             FW_CPoint(groupRect.right, groupRect.top),
  358.                             FW_CPoint(labelRect.right, groupRect.top) };
  359.     FW_CPolygonShape poly(FW_CPolygon(6, points), FW_kFrame, FALSE, fFrameInk);
  360.     poly.Render(gc);
  361.     
  362.     // Draw label
  363.     GetTextBoxShape(ev)->Render(gc);
  364. }
  365.  
  366. //----------------------------------------------------------------------------------------
  367. //    FW_CGroupBox::Flatten
  368. //----------------------------------------------------------------------------------------
  369.  
  370. void FW_CGroupBox::Flatten(Environment* ev, FW_CWritableStream& stream) const
  371. {
  372.     FW_CStaticText::Flatten(ev, stream);
  373. }
  374.  
  375. //----------------------------------------------------------------------------------------
  376. //    FW_CGroupBox::InitializeFromStream
  377. //----------------------------------------------------------------------------------------
  378.  
  379. void FW_CGroupBox::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  380. {
  381.     FW_CStaticText::InitializeFromStream(ev, stream);
  382. }
  383.  
  384.