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 / FWSclBar.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  13.1 KB  |  448 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWSclBar.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 FWSCLBAR_H
  13. #include "FWSclBar.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWPART_H
  21. #include "FWPart.h"
  22. #endif
  23.  
  24. #ifndef FWSCLNOT_H
  25. #include "FWSclNot.h"
  26. #endif
  27.  
  28. #ifndef FWNOTDEF_H
  29. #include "FWNotDef.h"
  30. #endif
  31.  
  32. #ifndef FWCONTXT_H
  33. #include "FWContxt.h"
  34. #endif
  35.  
  36. // ----- OS Layer -----
  37.  
  38. #ifndef FWEVENT_H
  39. #include "FWEvent.h"
  40. #endif
  41.  
  42. #ifndef FWFONT_H
  43. #include "FWFont.h"
  44. #endif
  45.  
  46. #ifndef FWODGEOM_H
  47. #include "FWODGeom.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef SOM_ODSession_xh
  53. #include <ODSessn.xh>
  54. #endif
  55.  
  56. #ifndef SOM_ODFacet_xh
  57. #include <Facet.xh>
  58. #endif
  59.  
  60. #ifndef SOM_ODTransform_xh
  61. #include <Trnsform.xh>
  62. #endif
  63.  
  64. // ----- Platform Includes -----
  65.  
  66. #if defined(FW_BUILD_MAC) && !defined(__CONTROLS__)
  67. #include <Controls.h>
  68. #endif
  69.  
  70. #ifdef FW_BUILD_WIN
  71. #include <Windows.h>
  72. #endif
  73.  
  74. //========================================================================================
  75. // File scope definitions
  76. //========================================================================================
  77.  
  78. #ifdef FW_BUILD_MAC
  79. #pragma segment fwgadgts
  80. #endif
  81.  
  82. //========================================================================================
  83. // CLASS FW_CScrollBar
  84. //========================================================================================
  85.  
  86. FW_DEFINE_AUTO(FW_CScrollBar)
  87. FW_DEFINE_CLASS_M1(FW_CScrollBar, FW_CNativeControl)
  88.  
  89. // This class is archivable, but we provide the archiving implementation in a separate
  90. // translation unit in order to enable deadstripping of the archiving-related code
  91. // in parts that do not use archiving with this class.
  92.  
  93. //----------------------------------------------------------------------------------------
  94. // FW_CScrollBar::FW_CScrollBar
  95. //----------------------------------------------------------------------------------------
  96.  
  97. FW_CScrollBar::FW_CScrollBar(Environment* ev, 
  98.                              FW_CSuperView* container, 
  99.                              ODID viewId,
  100.                              const FW_CRect& bounds) :
  101.     FW_CNativeControl(ev, container, viewId, bounds),
  102.     fMinorScrollUnits(FW_kFixedPos1),
  103.     fMajorScrollUnits(FW_kFixedPos1)
  104. {
  105.     Initialize(ev, GetValue(ev), FW_CScrollBar::kDefaultScrollMin, FW_CScrollBar::kDefaultScrollMax);
  106.  
  107.     // Bind the scroll-bar to right or bottom egde of its superview by default
  108.     FW_ViewBinding    sbBindings = (bounds.Height() > bounds.Width()) ? 
  109.                 FW_kFixedWidth + FW_kRightBinding + FW_kTopBinding + FW_kBottomBinding :
  110.                 FW_kFixedHeight + FW_kRightBinding + FW_kLeftBinding + FW_kBottomBinding;
  111.     SetBindings(ev, sbBindings);
  112. }
  113.  
  114. //----------------------------------------------------------------------------------------
  115. // FW_CScrollBar::FW_CScrollBar
  116. //----------------------------------------------------------------------------------------
  117.  
  118. FW_CScrollBar::FW_CScrollBar(Environment* ev) :
  119.     FW_CNativeControl(ev),
  120.     fMinorScrollUnits(FW_kFixedPos1),
  121.     fMajorScrollUnits(FW_kFixedPos1)
  122. {
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. // FW_CScrollBar::~FW_CScrollBar
  127. //----------------------------------------------------------------------------------------
  128.  
  129. FW_CScrollBar::~FW_CScrollBar()
  130. {
  131. }
  132.  
  133. //----------------------------------------------------------------------------------------
  134. // FW_CScrollBar::Initialize
  135. //----------------------------------------------------------------------------------------
  136.  
  137. void FW_CScrollBar::Initialize(Environment *ev, FW_ControlValue value, FW_ControlValue min,
  138.                             FW_ControlValue max)
  139. {
  140.     FW_CRect bounds = GetBounds(ev);
  141.     FW_ViewBinding sbBindings = GetBindings(ev);
  142.     
  143.     // Ensure that width of vert. SB and height or horiz. SB are constant
  144.     if (bounds.Height() > bounds.Width())
  145.     {
  146.         sbBindings |= FW_kFixedWidth;
  147.     } 
  148.     else
  149.     {
  150.         sbBindings |= FW_kFixedHeight;
  151.     }
  152.     SetBindings(ev, sbBindings);
  153.  
  154.     // Create the control helper
  155. #ifdef FW_BUILD_MAC
  156.     fControlHelper = new FW_CPrivMacScrollBarControlHelper(ev, this, value, min, max, scrollBarProc);
  157. #endif
  158.  
  159. #ifdef FW_BUILD_WIN
  160.     // TO UPDATE
  161.     fControlHelper  = new FW_CPrivWinControlHelper
  162.                                 (ev, FW_TYPEID_FROM_TYPE(FW_CPrivWinScrollBarHelper), this);
  163. #endif
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // FW_CScrollBar::PrivSetValue
  168. //----------------------------------------------------------------------------------------
  169.  
  170. FW_Boolean FW_CScrollBar::PrivSetValue(Environment *ev, FW_ControlValue value, ODFacet* facet)
  171. {
  172.     // Update the native control.  Return false if value was not changed 
  173.     if (FW_CNativeControl::PrivSetValue(ev, value, facet))
  174.     {
  175.         // Send a control message to its receivers
  176.         if (GetMessage(ev) != FW_kScrollMsg && GetMessage(ev) != FW_kNullMsg)
  177.             Notify(ev, FW_CControlNotification(this));
  178.  
  179.         return true;
  180.     }
  181.     else
  182.         return false;
  183. }
  184.  
  185. //----------------------------------------------------------------------------------------
  186. // FW_CScrollBar::ControlClicked
  187. //----------------------------------------------------------------------------------------
  188.  
  189. void FW_CScrollBar::ControlClicked(Environment *ev, FW_ControlValue delta, ODFacet* facet)
  190. {
  191.     // Update the control first
  192.     if (PrivSetValue(ev, GetValue(ev) + delta, facet))
  193.     {        
  194.         // Propagate the scrolling notification and ask to scroll
  195.         FW_Fixed fxDelta = FW_IntToFixed(delta);
  196.         ScrollPositionChanged(ev, fxDelta, true);
  197.     }
  198. }
  199.  
  200. //----------------------------------------------------------------------------------------
  201. // FW_CScrollBar::ScrollPositionChanged
  202. //----------------------------------------------------------------------------------------
  203.  
  204. void FW_CScrollBar::ScrollPositionChanged(Environment *ev, FW_Fixed delta, FW_Boolean shouldScroll)
  205. {
  206.     // ----- Figure out which direction we're scrolling
  207.     FW_CRect bounds = GetBounds(ev);
  208.     FW_XYSelector direction = (bounds.Height() > bounds.Width()) ? FW_kVertical : FW_kHorizontal;
  209.  
  210.     // ----- Notify receivers of the changes in scroll position
  211.     FW_CScrollNotification notification(this, direction, delta, shouldScroll);
  212.     Notify(ev, notification);
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // FW_CScrollBar::GetDefaultScrollBarSize
  217. //----------------------------------------------------------------------------------------
  218.  
  219. FW_CPoint FW_CScrollBar::GetDefaultScrollBarSize()
  220. {
  221. #ifdef FW_BUILD_MAC
  222.     return FW_CPoint(FW_IntToFixed(15), FW_IntToFixed(15));
  223. #endif
  224. #ifdef FW_BUILD_WIN
  225.     return FW_CPoint(FW_IntToFixed(::GetSystemMetrics(SM_CXHSCROLL)),
  226.                      FW_IntToFixed(::GetSystemMetrics(SM_CXVSCROLL)));
  227. #endif
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // FW_CScrollBar::DoActivateEvent
  232. //----------------------------------------------------------------------------------------
  233.  
  234. void FW_CScrollBar::DoActivateEvent(Environment *ev, const FW_CActivateEvent& theActivateEvent)
  235. {
  236. #ifdef FW_BUILD_MAC
  237.     ODFacet* facet = theActivateEvent.GetFacet(ev);
  238.     FW_CViewContext vc(ev, this, facet);
  239.  
  240.     FW_CPlatformPoint qdPoint    = vc.LogicalToDevice(FW_kZeroPoint);
  241.     FW_CPlatformPoint qdSize    = GetSize(ev).AsPlatformPoint();
  242.  
  243.     if (theActivateEvent.IsActivating(ev))
  244.     {    
  245.         // Draw scrollbar immediately
  246.         fControlHelper->Show(qdPoint, qdSize);
  247.     }
  248.     else
  249.     {
  250.         // Use invalidation to avoid flashing
  251.         fControlHelper->Hide();
  252.         Invalidate(ev);
  253.     }
  254. #endif
  255.  
  256. #ifdef FW_BUILD_WIN
  257.     // [TODO]
  258. #endif
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // FW_CScrollBar::SetMinorScrollUnits
  263. //----------------------------------------------------------------------------------------
  264.  
  265. void FW_CScrollBar::SetMinorScrollUnits(Environment *ev, FW_Fixed units)
  266. {
  267. FW_UNUSED(ev);
  268.     fMinorScrollUnits = units;
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. // FW_CScrollBar::SetMajorScrollUnits
  273. //----------------------------------------------------------------------------------------
  274.  
  275. void FW_CScrollBar::SetMajorScrollUnits(Environment *ev, FW_Fixed units)
  276. {
  277. FW_UNUSED(ev);
  278.     fMajorScrollUnits = units;
  279. }
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // FW_CScrollBar::SetScrollMax
  283. //----------------------------------------------------------------------------------------
  284.  
  285. void FW_CScrollBar::SetScrollMax(Environment *ev, FW_Fixed units)
  286. {
  287. FW_UNUSED(ev);
  288. #ifdef FW_BUILD_MAC
  289.     fControlHelper->SetMax(FW_FixedToInt(units));
  290. #endif
  291.  
  292. #ifdef FW_BUILD_WIN
  293.     // [TODO]
  294.     int min, max;
  295.     fScrollBarHelper->GetScrollRange(min, max);
  296.     fScrollBarHelper->SetScrollRange(min, FW_FixedToInt(units));
  297. #endif
  298. }
  299.  
  300. //----------------------------------------------------------------------------------------
  301. // FW_CScrollBar::SetScrollMin
  302. //----------------------------------------------------------------------------------------
  303.  
  304. void FW_CScrollBar::SetScrollMin(Environment *ev, FW_Fixed units)
  305. {
  306. FW_UNUSED(ev);
  307. #ifdef FW_BUILD_MAC
  308.     fControlHelper->SetMin(FW_FixedToInt(units));
  309. #endif
  310.  
  311. #ifdef FW_BUILD_WIN
  312.     // [TODO]
  313.     int min, max;
  314.     fScrollBarHelper->GetScrollRange(min, max);
  315.     fScrollBarHelper->SetScrollRange(FW_FixedToInt(units), max);
  316. #endif
  317. }
  318.  
  319. //----------------------------------------------------------------------------------------
  320. // FW_CScrollBar::SetScrollPos
  321. //----------------------------------------------------------------------------------------
  322.  
  323. void FW_CScrollBar::SetScrollPos(Environment *ev, FW_Fixed units)
  324. {
  325.     SetValue(ev, FW_FixedToInt(units));    
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // FW_CScrollBar::SetScrollPos
  330. //----------------------------------------------------------------------------------------
  331.  
  332. void FW_CScrollBar::SetScrollPos(Environment *ev, FW_Fixed range, FW_Fixed pos)
  333. {
  334.     FW_Fixed units = FW_kFixed0;
  335.     if (range != FW_kFixed0)
  336.         units = FW_WideMultiply(GetScrollMax(ev) - GetScrollMin(ev), pos) / range + GetScrollMin(ev);
  337.  
  338.     SetValue(ev, FW_FixedToInt(units));    
  339. }
  340.  
  341. //----------------------------------------------------------------------------------------
  342. //    FW_CScrollBar::Flatten
  343. //----------------------------------------------------------------------------------------
  344.  
  345. void FW_CScrollBar::Flatten(Environment* ev, FW_CWritableStream& stream) const
  346. {
  347.     FW_CNativeControl::Flatten(ev, stream);
  348.  
  349.     short min = fControlHelper->GetMin();
  350.     short max = fControlHelper->GetMax();
  351.     
  352.     stream << min;
  353.     stream << max;
  354.     stream << fMinorScrollUnits;
  355.     stream << fMajorScrollUnits;
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CScrollBar::InitializeFromStream
  360. //----------------------------------------------------------------------------------------
  361.  
  362. void FW_CScrollBar::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  363. {
  364.     FW_CNativeControl::InitializeFromStream(ev, stream);
  365.     
  366.     short min, max;
  367.     FW_Fixed major, minor;
  368.  
  369.     stream >> min >> max;
  370.     stream >> minor >> major;
  371.  
  372.     SetMinorScrollUnits(ev, minor);
  373.     SetMajorScrollUnits(ev, major);
  374.     
  375.     // [LSD] check for valid 16 bits value for scroll-bar
  376.     FW_ControlValue value = GetValue(ev);
  377.     FW_ASSERT(value <= FW_MAXINT16 && value >= FW_MININT16);
  378.  
  379.     Initialize(ev, value, min, max);
  380. }
  381.  
  382. //----------------------------------------------------------------------------------------
  383. // FW_CScrollBar::GetMinorScrollUnits
  384. //----------------------------------------------------------------------------------------
  385.  
  386. FW_Fixed FW_CScrollBar::GetMinorScrollUnits(Environment*) const
  387. {
  388.     return fMinorScrollUnits;
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. // FW_CScrollBar::GetMajorScrollUnits
  393. //----------------------------------------------------------------------------------------
  394.  
  395. FW_Fixed FW_CScrollBar::GetMajorScrollUnits(Environment*) const
  396. {
  397.     return fMajorScrollUnits;
  398. }
  399.  
  400. //----------------------------------------------------------------------------------------
  401. // FW_CScrollBar::GetScrollMax
  402. //----------------------------------------------------------------------------------------
  403.  
  404. FW_Fixed FW_CScrollBar::GetScrollMax(Environment*) const
  405. {
  406. #ifdef FW_BUILD_MAC
  407.     return FW_IntToFixed(fControlHelper->GetMax());
  408. #endif
  409.  
  410. #ifdef FW_BUILD_WIN
  411.     int min, max;
  412.     fScrollBarHelper->GetScrollRange(min, max);
  413.     return FW_IntToFixed(max);
  414. #endif
  415. }
  416.  
  417. //----------------------------------------------------------------------------------------
  418. // FW_CScrollBar::GetScrollMin
  419. //----------------------------------------------------------------------------------------
  420.  
  421. FW_Fixed FW_CScrollBar::GetScrollMin(Environment*) const
  422. {
  423. #ifdef FW_BUILD_MAC
  424.     return FW_IntToFixed(fControlHelper->GetMin());
  425. #endif
  426.  
  427. #ifdef FW_BUILD_WIN
  428.     int min, max;
  429.     fScrollBarHelper->GetScrollRange(min, max);
  430.     return FW_IntToFixed(min);
  431. #endif
  432. }
  433.  
  434. //----------------------------------------------------------------------------------------
  435. // FW_CScrollBar::GetScrollPos
  436. //----------------------------------------------------------------------------------------
  437.  
  438. FW_Fixed FW_CScrollBar::GetScrollPos(Environment*) const
  439. {
  440. #ifdef FW_BUILD_MAC
  441.     return FW_IntToFixed(fControlHelper->GetValue());
  442. #endif
  443.  
  444. #ifdef FW_BUILD_WIN
  445.     return FW_IntToFixed(fScrollBarHelper->GetScrollPos());
  446. #endif
  447. }
  448.