home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWViews / Sources / FWStatic.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  11.2 KB  |  356 lines  |  [TEXT/MPS ]

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