home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Views / PedViewScroller.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-24  |  6.1 KB  |  310 lines

  1. /*    ==================
  2.  *    PedViewScroller.cc
  3.  *    ==================
  4.  */
  5.  
  6. #include "PedestalDebugging.h"
  7.  
  8. #include "PedViewScroller.hh"
  9. #include "PedPaneSubView.hh"
  10. #include "PedViewScroll.hh"
  11. #include "PedScrollbar.hh"
  12.  
  13.  
  14. PedViewScroller::PedViewScroller(PedPaneSubView &inSuperPane)
  15. : PedViewSub(inSuperPane), mScrollV(NULL), mScrollH(NULL)
  16. {
  17.     //mScrollV = new PedScrollbar(*this, kPedScrollVertical);
  18.     //mScrollH = new PedScrollbar(*this, kPedScrollHorizontal);
  19. }
  20.  
  21. PedViewScroller::~PedViewScroller()
  22. {
  23.     if (mScrollV) mScrollV->release();
  24.     if (mScrollH) mScrollH->release();
  25. }
  26.  
  27. void
  28. PedViewScroller::Dispose()
  29. {
  30.     if (mScrollV) {
  31.         mScrollV->Dispose();
  32.         mScrollV->release();
  33.         mScrollV = NULL;
  34.     }
  35.     if (mScrollH) {
  36.         mScrollH->Dispose();
  37.         mScrollH->release();
  38.         mScrollH = NULL;
  39.     }
  40. }
  41.  
  42.  
  43. void
  44. PedViewScroller::NotifyScrolledTo(short inH, short inV)
  45. {
  46.     if (mScrollH) {
  47.         mScrollH->SetValue(inH);
  48.     }
  49.     if (mScrollV) {
  50.         mScrollV->SetValue(inV);
  51.     }
  52. }
  53.  
  54. enum {kPedMsgNone, kPedMsgScroll, kPedMsgNotifyScrolledTo};
  55.  
  56. long
  57. PedViewScroller::Message(long inMsgCode, void *inData)
  58. {
  59.     switch (inMsgCode) {
  60.         case kPedMsgScroll:
  61.             // We need a block here
  62.             if (1) {
  63.                 SPedScrollInfo info = *(SPedScrollInfo *)inData;
  64.                 Scroll(info.pt.h, info.pt.v, info.update);
  65.             }
  66.             break;
  67.         case kPedMsgNotifyScrolledTo:
  68.             // We need a block here
  69.             if (1) {
  70.                 Point pt = *(Point *)inData;
  71.                 NotifyScrolledTo(pt.h, pt.v);
  72.             }
  73.             break;
  74.         case kPedMsgNone:
  75.             break;
  76.         default:
  77.             break;
  78.     }
  79.     return 0;
  80. }
  81.  
  82.  
  83. void
  84. PedViewScroller::SetScrollbarPresence(short inAxis, bool inPresent)
  85. {
  86.     if (inAxis & kPedScrollVertical) {
  87.         if (inPresent) {
  88.             mScrollV = new PedScrollbar(*this, kPedScrollVertical);
  89.         } else {
  90.             delete mScrollV;
  91.             mScrollV = NULL;
  92.         }
  93.     }
  94.     if (inAxis & kPedScrollHorizontal) {
  95.         if (inPresent) {
  96.             mScrollH = new PedScrollbar(*this, kPedScrollHorizontal);
  97.         } else {
  98.             delete mScrollH;
  99.             mScrollH = NULL;
  100.         }
  101.     }
  102.     Resize(mFrame.right, mFrame.bottom);
  103. }
  104.  
  105. void
  106. PedViewScroller::GetFrame(Rect &outFrame)
  107. {
  108.     PedViewSub::GetFrame(outFrame);
  109.     //outFrame.right -= 15;
  110.     //outFrame.bottom -= 15;
  111. }
  112.  
  113. void
  114. PedViewScroller::Open()
  115. {
  116.     if (mScrollV) {
  117.         mScrollV->Open();
  118.     }
  119.     if (mScrollH) {
  120.         mScrollH->Open();
  121.     }
  122.     if (MyPane()) {
  123.         MyPane()->Open();
  124.     }
  125. }
  126.  
  127. void
  128. PedViewScroller::Close()
  129. {
  130.     if (mScrollV) {
  131.         mScrollV->Close();
  132.     }
  133.     if (mScrollH) {
  134.         mScrollH->Close();
  135.     }
  136.     if (MyPane()) {
  137.         MyPane()->Close();
  138.     }
  139. }
  140.  
  141. void
  142. PedViewScroller::Activate()
  143. {
  144.     if (mScrollV) {
  145.         mScrollV->Activate();
  146.     }
  147.     if (mScrollH) {
  148.         mScrollH->Activate();
  149.     }
  150.     if (MyPane()) {
  151.         MyPane()->Activate();
  152.     }
  153. }
  154.  
  155. void
  156. PedViewScroller::Deactivate()
  157. {
  158.     if (mScrollV) {
  159.         mScrollV->Deactivate();
  160.     }
  161.     if (mScrollH) {
  162.         mScrollH->Deactivate();
  163.     }
  164.     if (MyPane()) {
  165.         MyPane()->Deactivate();
  166.     }
  167. }
  168.  
  169. void
  170. PedViewScroller::Resize(short inWidth, short inHeight)
  171. {
  172.     PedView::Resize(inWidth, inHeight);
  173.     mFrame.right = inWidth;
  174.     mFrame.bottom = inHeight;
  175.     
  176.     Point offset;
  177.     GetWindowToLocalOffset(offset);
  178.     
  179.     Rect bounds;
  180.     bool box = true;
  181.     
  182.     if (mScrollV) {
  183.         bounds.top = mFrame.top + offset.v - 1;
  184.         bounds.bottom = mFrame.bottom + offset.v - (box || mScrollH ? 14 : -1);
  185.         bounds.left = mFrame.right + offset.h - 15;
  186.         bounds.right = mFrame.right + offset.h + 1;
  187.         mScrollV->SetBounds(bounds);
  188.         short contentHeight = mFrame.bottom - mFrame.top - (mScrollH ? 15 : 0);
  189.         mScrollV->SetJumpUnit(contentHeight);
  190.     }
  191.     if (mScrollH) {
  192.         bounds.left = mFrame.left + offset.h - 1;
  193.         bounds.right = mFrame.right + offset.h - (box || mScrollV ? 14 : -1);
  194.         bounds.top = mFrame.bottom + offset.v - 15;
  195.         bounds.bottom = mFrame.bottom + offset.v + 1;
  196.         mScrollH->SetBounds(bounds);
  197.         short contentWidth = mFrame.right - mFrame.left - (mScrollV ? 15 : 0);
  198.         mScrollH->SetJumpUnit(contentWidth);
  199.     }
  200.     if (MyPane()) {
  201.         MyPane()->Resize(inWidth  - (mScrollV ? 15 : 0), 
  202.                       inHeight - (mScrollH ? 15 : 0));
  203.     }
  204.     Calibrate();
  205. }
  206.  
  207.  
  208. void
  209. PedViewScroller::Calibrate()
  210. {
  211.     short hMax, vMax;
  212.     
  213.     PedPane *pane = Pane();
  214.     if (pane) {
  215.         Point scrollPos;
  216.         GetScrollPos(scrollPos);
  217.         Rect aperture, bounds;
  218.         GetAperture(aperture);
  219.         pane->GetBounds(bounds);
  220.         short 
  221.             contentHeight, contentWidth, 
  222.             viewHeight,    viewWidth, 
  223.             scrollHeight,  scrollWidth;
  224.         // Scroll height is either the content height, or the view height plus
  225.         // the scroll position, whichever is greater.  (Think of it as the height 
  226.         // of an actual 'scroll' that we're 'scrolling' through.
  227.         contentWidth  = bounds.right  - bounds.left;
  228.         contentHeight = bounds.bottom - bounds.top;
  229.         viewWidth  = aperture.right  - aperture.left;
  230.         viewHeight = aperture.bottom - aperture.top;
  231.         scrollWidth  = viewWidth  + scrollPos.h;
  232.         scrollHeight = viewHeight + scrollPos.v;
  233.         if (scrollWidth  < contentWidth ) scrollWidth  = contentWidth;
  234.         if (scrollHeight < contentHeight) scrollHeight = contentHeight;
  235.         hMax = scrollWidth  - viewWidth;
  236.         vMax = scrollHeight - viewHeight;
  237.         //if (hMax < 0) hMax = 0;
  238.         //if (vMax < 0) vMax = 0;
  239.     } else {
  240.         hMax = vMax = 0;
  241.     }
  242.     if (mScrollV) {
  243.         mScrollV->SetMaximum(vMax);
  244.     }
  245.     if (mScrollH) {
  246.         mScrollH->SetMaximum(hMax);
  247.     }
  248. }
  249.  
  250.  
  251. void
  252. PedViewScroller::DrawContent()
  253. {
  254.     // set up drawing environment
  255.     // for each pane, Draw()
  256.     // restore drawing environment
  257.     Rect bounds;
  258.     if (mScrollV) {
  259.         mScrollV->GetBounds(bounds);
  260.         ::ClipRect(&bounds);
  261.         mScrollV->DrawContent();
  262.     }
  263.     if (mScrollH) {
  264.         mScrollH->GetBounds(bounds);
  265.         ::ClipRect(&bounds);
  266.         mScrollH->DrawContent();
  267.     }
  268.     if (MyPane()) {
  269.         MyPane()->GetBounds(bounds);
  270.         ::ClipRect(&bounds);
  271.         MyPane()->DrawContent();
  272.     }
  273.     ::ClipRect(&qd.thePort->portRect);
  274. }
  275.  
  276. void
  277. PedViewScroller::DispatchNullEvent(EventRecord &inEvent)
  278. {
  279.     if (MyPane()) {
  280.         MyPane()->DispatchNullEvent(inEvent);
  281.     }
  282. }
  283.  
  284. void
  285. PedViewScroller::DispatchClickEvent(EventRecord &inEvent)
  286. {
  287.     Point pt, offset;
  288.     pt = inEvent.where;
  289.     ::GlobalToLocal(&pt);
  290.     GetWindowToLocalOffset(offset);
  291.     ::AddPt(offset, &pt);
  292.     if (MyPane() && MyPane()->PointInAperture(pt)) {
  293.         MyPane()->DispatchClickEvent(inEvent);
  294.     } else if (mScrollV && mScrollV->PointInBounds(pt)) {
  295.         mScrollV->DispatchClickEvent(inEvent);
  296.     } else if (mScrollH && mScrollH->PointInBounds(pt)) {
  297.         mScrollH->DispatchClickEvent(inEvent);
  298.     } else {
  299.         // !?
  300.     }
  301. }
  302.  
  303. void
  304. PedViewScroller::DispatchKey(EventRecord &inEvent)
  305. {
  306.     if (MyPane()) {
  307.         MyPane()->DispatchKey(inEvent);
  308.     }
  309. }
  310.