home *** CD-ROM | disk | FTP | other *** search
/ Mac Expert 1995 Winter / Mac Expert - Winter 95.iso / Les fichiers / Communications / Divers / DinkClass ƒ / DinkClass / DScrollWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-31  |  12.5 KB  |  597 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        DScrollWindow.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Written by:    Mark Gross
  7.  
  8.     Copyright:    © 1992 by Applied Technical Software, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <5>    12/31/92    MTG        making the code conditionaly compiled so         that I am
  13.                                     always working with a current         version in either think c
  14.                                     or MPW C++
  15.          <4>    11/14/92    MTG        Bringing the C++ version up to date WRT the ThinkC version.
  16.          <3>      8/9/92    MTG        merging changes form the ThinkC version
  17.          <2>      8/8/92    MTG        cleaning up unused variables
  18.  
  19.     To Do:
  20. */
  21.  
  22. #include "DScrollWindow.h"
  23. #include <DApplication.h>
  24.  
  25.  
  26.  
  27. DScrollWindow::DScrollWindow(void)
  28. {
  29. //null
  30. }
  31.  
  32. DScrollWindow::~DScrollWindow(void)
  33. {
  34. //null
  35. }
  36.  
  37.         
  38. Boolean DScrollWindow::Init(DDocument *doc, Boolean hasColorWindows)
  39. {
  40.     Boolean inheritedSuccess;
  41.     Rect r;
  42.     
  43.     SetRect(&r, 0, 0, 0, 0);
  44.     inheritedSuccess = inherited::Init(doc, hasColorWindows);
  45.     
  46.     fHorizScrollBar = NULL;
  47.     fVertScrollBar = NULL;
  48.     fVMin = 0;
  49.     fVMax = 444;
  50.     
  51.     fHMin = 0;
  52.     fHMax = 444;
  53.     if (inheritedSuccess)
  54.     {
  55.         SetPort(fWindowPtr);
  56.         fHorizScrollBar = NewControl(fWindowPtr,&r, "\p",TRUE,
  57.                                 0, 0, 0, scrollBarProc, 0);
  58.         fVertScrollBar = NewControl(fWindowPtr,&r, "\p",TRUE,
  59.                                 0, 0, 0, scrollBarProc, 0);
  60.         
  61.         SizeScrollBars();
  62.     
  63.         SynchScrollBars();
  64.          InvalRect(&(fWindowPtr->portRect));
  65.      }
  66.     return ( (fHorizScrollBar != NULL)&&(fVertScrollBar != NULL) && inheritedSuccess);
  67. }// end of init function
  68.  
  69.  
  70.  
  71. Boolean DScrollWindow::KillMeNext(void)
  72. {
  73.     Boolean inheritedSuccess = FALSE;
  74.  
  75.     if(fAlive)
  76.     {
  77.         if(inheritedSuccess = inherited::KillMeNext())
  78.         {
  79.             KillControls(fWindowPtr);
  80.         }
  81.     }
  82.     return inheritedSuccess;
  83. }
  84.  
  85.  
  86.  
  87.  
  88. void    DScrollWindow::HandleUpdateEvt(EventRecord *theEvent)
  89. {
  90.     Rect r;
  91.     
  92.     SetPort (fWindowPtr);
  93.     BeginUpdate(fWindowPtr);
  94.         
  95.     FocusOnContent();
  96.     r = (**(fWindowPtr->visRgn)).rgnBBox;
  97.     if( !EmptyRgn(fWindowPtr->visRgn) )
  98.         Draw(&r);
  99.         
  100.     FocusOnWindow();
  101.     DrawControls(fWindowPtr);
  102.     DrawGrowIcon( fWindowPtr);
  103.     EndUpdate(fWindowPtr);
  104.     
  105. }// end of HandleUpdateEvt member function
  106.  
  107. //
  108. // Needed to fixe make the scroll bars behave correctly when a context change occurs
  109. //
  110. void DScrollWindow::HandleOSEvent(EventRecord *theEvent)
  111. {
  112.     if (gApplication->fInBackground)
  113.     {
  114.         FocusOnWindow();
  115.         HideControl(fVertScrollBar);
  116.         HideControl(fHorizScrollBar);
  117.     }
  118.     else
  119.     {
  120.         FocusOnWindow();
  121.         ShowControl(fVertScrollBar);
  122.         ShowControl(fHorizScrollBar);
  123.         DrawControls(fWindowPtr);
  124.         DrawGrowIcon(fWindowPtr);
  125.  
  126.         FocusOnContent();
  127.     }
  128.     inherited::HandleOSEvent(theEvent);
  129.     
  130. }// end of HandleOSEvent member function
  131.         
  132.     
  133. void DScrollWindow::HandleActivateEvt(EventRecord *theEvent)
  134. {
  135.     Boolean    activating;
  136.     
  137.     activating = theEvent->modifiers & activeFlag;
  138.     if(activating)
  139.     {
  140.         FocusOnWindow();
  141.         ShowControl(fVertScrollBar);
  142.         ShowControl(fHorizScrollBar);
  143.         DrawControls(fWindowPtr);
  144.         DrawGrowIcon(fWindowPtr);
  145.  
  146.         FocusOnContent();
  147.         if( !EmptyRgn(fWindowPtr->visRgn) )
  148.             ;// makes comm tool box window go south (erases my window!!!!
  149.             // if Draw( &(fWindowPtr->portRect)); is called
  150.             // besides, DScrollWindow should only take care of its own
  151.     }
  152.     else
  153.     {
  154.         FocusOnWindow();
  155.         HideControl(fVertScrollBar);
  156.         HideControl(fHorizScrollBar);
  157.     }
  158. }// end of HandleActivateEvt member function
  159.     
  160. short    DScrollWindow::GetVertLineScrollAmount(void)
  161. {
  162.     return 16;
  163. }//end of GetVertLineScrollAmount function
  164.  
  165.  
  166. short    DScrollWindow::GetHorizLineScrollAmount(void)
  167. {
  168.     return 16;
  169. }//end of GetHorizLineScrollAmount function
  170.  
  171.     
  172. short    DScrollWindow::GetVertPageScrollAmount(void)
  173. {
  174.     Rect r;
  175.     
  176.     GetContentRect(&r);
  177.     return r.bottom - r.top - kScrollOverlap;
  178. }//end of GetVertPageScrollAmount function
  179.  
  180.     
  181. short    DScrollWindow::GetHorizPageScrollAmount(void)
  182. {
  183.     Rect r;
  184.     
  185.     GetContentRect(&r);
  186.     return r.right - r.left - kScrollOverlap;
  187. }//end of GetHorizPageScrollAmount function
  188.  
  189.  
  190. void    DScrollWindow::DoGrow(EventRecord *theEvent)
  191. {
  192.     FocusOnWindow();
  193.     inherited::DoGrow(theEvent);
  194.     SizeScrollBars();
  195.     SynchScrollBars();
  196. }//end of DoGrow function
  197.  
  198.  
  199.  
  200.  
  201. void    DScrollWindow::DoZoom(short partCode)
  202. {
  203.     FocusOnWindow();
  204.     inherited::DoZoom(partCode);
  205.     SizeScrollBars();
  206.     SynchScrollBars();
  207. }//end of DoZoom function
  208.  
  209.  
  210.  
  211.  
  212. void    DScrollWindow::DoContent(EventRecord *theEvent)
  213. {
  214.     Rect contents;
  215.     
  216.     FocusOnWindow();
  217.     GlobalToLocal(&theEvent->where);
  218.     GetContentRect(&contents);
  219.     if(PtInRect(theEvent->where, &contents))
  220.     {
  221.         FocusOnContent();
  222.         inherited::DoContent(theEvent);
  223.     }
  224.     else
  225.         ScrollClick(theEvent);
  226. }// end of DoContent member function
  227.  
  228.     
  229.  
  230. void    DScrollWindow::SizeScrollBars()
  231. {
  232.     Rect r, r_temp;
  233.     
  234.     FocusOnWindow();
  235.     r = fWindowPtr->portRect;
  236.  
  237.     HideControl(fVertScrollBar);
  238.     SizeControl(fVertScrollBar, kScrollBarWidth,
  239.                 (r.bottom - r.top - kScrollBarPos) + 2);        // the '2' is to make the bar end on the grow box boundry
  240.     MoveControl(fVertScrollBar, r.right - kScrollBarPos, -1);
  241.     ShowControl(fVertScrollBar);
  242.     r_temp = (**fVertScrollBar).contrlRect;
  243.     ValidRect(&r_temp);
  244.     InvalRect(&r_temp);
  245.  
  246.     HideControl(fHorizScrollBar);
  247.     SizeControl(fHorizScrollBar, (r.right - r.left - kScrollBarPos) + 2, 
  248.                                     kScrollBarWidth);
  249.     MoveControl(fHorizScrollBar, -1, r.bottom - r.top - kScrollBarPos);
  250.     ShowControl(fHorizScrollBar);
  251.     r_temp = (**fHorizScrollBar).contrlRect;
  252.     ValidRect(&r_temp);
  253.     InvalRect(&r_temp);
  254.  
  255. }// end of SizeScroll Bars member fuction
  256.  
  257.  
  258.  
  259. // When things happen which could have changed the
  260. // scroll parameters and these routeens is used to get them back
  261. // in the correct form.
  262.  
  263. void    DScrollWindow::ValidateScrollRange(void)
  264. {
  265.     Rect r;
  266.     short dh, dv;
  267.     short newMax;
  268.     RgnHandle oldClip;
  269.     
  270.     GetContentRect(&r);
  271.     dh = 0; dv = 0;
  272.     newMax = (fVMax - fVMin) - (r.bottom - r.top); 
  273.     // see .h file for comment on control ranges vrs visible ranges
  274.         
  275.     if(newMax < 0)
  276.         newMax = 0;
  277.     if(fVOffSet > newMax)
  278.         dv = fVOffSet - newMax;
  279.     SetCtlMax(fVertScrollBar, newMax);
  280.  
  281.     newMax = (fHMax - fHMin) - (r.right - r.left);
  282.     if(newMax < 0)
  283.         newMax = 0;
  284.     if(fHOffSet > newMax)
  285.         dh = fHOffSet - newMax;
  286.     SetCtlMax(fHorizScrollBar, newMax);
  287.     
  288.     if(dh | dv)
  289.     {
  290.         FocusOnContent();
  291.         
  292.         GetContentRect(&r);
  293.         InvalRect(&r);
  294.         
  295.         oldClip = NewRgn();
  296.         GetClip(oldClip);
  297.         SetRect(&r, 0, 0, 0, 0);
  298.         ClipRect(&r);
  299.         ScrollContents(dh,dv);
  300.         
  301.         SetClip(oldClip);
  302.         DisposeRgn(oldClip);
  303.     }
  304. }// end of ValidateScrollRange member function
  305.  
  306.  
  307. void    DScrollWindow::ValidateVertScrollRange(void)
  308. {
  309.     Rect r;
  310.     short dv;
  311.     short newMax;
  312.     RgnHandle oldClip;
  313.     
  314.     GetContentRect(&r);
  315.     dv = 0;
  316.     newMax = (fVMax - fVMin) - (r.bottom - r.top);
  317.     if(newMax < 0)
  318.         newMax = 0;
  319.     if(fVOffSet > newMax)
  320.         dv = fVOffSet - newMax;
  321.     SetCtlMax(fVertScrollBar, newMax);
  322.     
  323.     if(dv)
  324.     {
  325.         FocusOnContent();
  326.         
  327.         GetContentRect(&r);
  328.         InvalRect(&r);
  329.         
  330.         oldClip = NewRgn();
  331.         GetClip(oldClip);
  332.         SetRect(&r, 0, 0, 0, 0);
  333.         ClipRect(&r);
  334.         ScrollContents(0,dv);
  335.         
  336.         SetClip(oldClip);
  337.         DisposeRgn(oldClip);
  338.     }
  339. }// end of ValidateVertScrollRange member function
  340.  
  341.  
  342.  
  343. void    DScrollWindow::SynchScrollBars(void)
  344. {
  345.     ValidateScrollRange();
  346.     FocusOnWindow();
  347.     SetCtlValue(fHorizScrollBar, fHOffSet);
  348.     SetCtlValue(fVertScrollBar, fVOffSet);
  349.     
  350. }// end of SynchScrollBars member function
  351.  
  352.  
  353.  
  354.  
  355. void    DScrollWindow::ScrollClick(EventRecord *theEvent)
  356. {
  357.     ControlHandle whichControl;
  358.     short part;
  359.     
  360.     FocusOnWindow();
  361.     if(part = FindControl(theEvent->where, fWindowPtr, &whichControl) )
  362.     {
  363.         switch(part)
  364.         {
  365.             case inThumb:
  366.                 DoThumbScroll(whichControl, theEvent->where);
  367.                 break;
  368.             case inUpButton:
  369.             case inDownButton:
  370.                 DoButtonScroll(whichControl, theEvent->where);
  371.                 break;
  372.             case inPageUp:
  373.             case inPageDown:
  374.                 DoPageScroll(whichControl,part);
  375.                 break;
  376.         }/*end of switch*/
  377.     }
  378. }// end of ScrollClick member function
  379.  
  380.     
  381. void    DScrollWindow::DoThumbScroll(ControlHandle theControl, Point localPt)
  382. {
  383.     short oldValue, trackResult, newValue, diff;
  384.     
  385.     oldValue = GetCtlValue(theControl);
  386.     trackResult = TrackControl(theControl, localPt, NULL);
  387.     if(trackResult != 0)
  388.     {
  389.         newValue = GetCtlValue(theControl);
  390.         diff = newValue - oldValue;
  391.  
  392.         if(theControl == fHorizScrollBar)
  393.             DoHScroll(diff);
  394.         if(theControl == fVertScrollBar)
  395.             DoVScroll(diff);
  396.     }
  397. }// end of DoThumbScroll member function
  398.  
  399.     
  400.  
  401.  
  402. void    DScrollWindow::DoPageScroll(ControlHandle theControl, short part)
  403. {
  404.     short scrollAmount, currentPart;
  405.     Point thePt;
  406.     
  407.     if(theControl == fVertScrollBar)
  408.     {
  409.         scrollAmount = GetVertPageScrollAmount();
  410.         do
  411.         {
  412.             GetMouse(&thePt);
  413.             currentPart = TestControl(theControl, thePt);
  414.             if(currentPart == part)
  415.             {
  416.                 if(currentPart == inPageUp)
  417.                     DoVScroll(- scrollAmount);
  418.                 if(currentPart == inPageDown)
  419.                     DoVScroll(scrollAmount);
  420.             }
  421.         }while(Button());
  422.     }// end if VeriticalScrollBar
  423.     else if (theControl == fHorizScrollBar)
  424.     {
  425.         scrollAmount = GetHorizPageScrollAmount();
  426.         do
  427.         {
  428.             GetMouse(&thePt);
  429.             currentPart = TestControl(theControl, thePt);
  430.             if(currentPart == part)
  431.             {
  432.                 if(currentPart == inPageUp)
  433.                     DoHScroll(- scrollAmount);
  434.                 if(currentPart == inPageDown)
  435.                     DoHScroll(scrollAmount);
  436.             }
  437.         }while(Button());
  438.     }// end ifHorizontal Scroll Bar
  439. }// end of DoPageScroll function
  440.  
  441.  
  442.  
  443. void    DScrollWindow::DoButtonScroll(ControlHandle theControl, Point localPt)
  444. {
  445.     short result;
  446.     
  447.     result = TrackControl(theControl, localPt, (ProcPtr) ActionProc) ;
  448. }// end of DoButtonScroll member function
  449.  
  450.  
  451.  
  452.     
  453. void    DScrollWindow::ScrollContents(short dh, short dv)
  454. {
  455.     Rect r;
  456.     RgnHandle updateRgn;
  457.     
  458.     GetContentRect(&r);
  459.     updateRgn = NewRgn();
  460.     
  461.     ScrollRect(&r,dh,dv,updateRgn);
  462.     
  463.     fVOffSet -= dv;
  464.     fHOffSet -= dh;
  465.     
  466.     InvalRgn(updateRgn);
  467.     HandleUpdateEvt(NULL);
  468.     DisposeRgn(updateRgn);
  469. }// end of ScrollContents function
  470.  
  471.  
  472.  
  473. void    DScrollWindow::DoHScroll( short change)
  474. {
  475.     RgnHandle oldClip;
  476.     short diff;
  477.     short newValue;
  478.     short oldMin, oldMax;
  479.         // These variables are to allow the use of different
  480.         // ranges in the fHOffSet, fHMax and fHMins than possible
  481.         // in the controls.  It is a result of the fact that the settings
  482.         // of the controls and the settings of the offset, max and min are
  483.         // not equal, becuase the scroll is scrolling the origin suchthat
  484.         // the entier range defined by fHMin and fHMax are accesibl
  485.         // the windows settings.(its best to stay close to that state)
  486.  
  487.         // In particualar its the control's max value wich is not
  488.         // equall to the fHMax/fVMax values,  The mins are equal in 
  489.         // the applications I've created so far but just to be on the 
  490.         // safe side, I'll check the mins too.
  491.  
  492.     oldClip = NewRgn();
  493.     GetClip(oldClip);
  494.     diff = 0;
  495.     newValue = fHOffSet + change;
  496.     
  497.     if(change<0)
  498.     {
  499.         oldMin = GetCtlMin(fHorizScrollBar);
  500.         if(newValue < oldMin)
  501.             newValue = oldMin;
  502.     }
  503.     else
  504.     {
  505.         oldMax = GetCtlMax(fHorizScrollBar);
  506.         if(newValue > oldMax)
  507.             newValue = oldMax;
  508.     }
  509.     diff = fHOffSet - newValue;
  510.     
  511.     FocusOnContent();
  512.     ScrollContents(diff,0);
  513.         // changes the offsets
  514.     
  515.     FocusOnWindow();
  516.     SetCtlValue(fHorizScrollBar, fHOffSet);
  517.         // now bring the control up to date with the offsets
  518.     
  519.     SetClip(oldClip);
  520.     DisposeRgn(oldClip);
  521. }// end of DoHScroll member function
  522.  
  523.  
  524. void    DScrollWindow::DoVScroll(short change)
  525. {
  526.     RgnHandle oldClip;
  527.     short diff;
  528.     short newValue;
  529.     short oldMin, oldMax;
  530.         // this change in theam is to allow the use of different
  531.         // dinamic ranges in the fHOffSet, Max and Mins than possible
  532.         // in the controls.  It also admits to the fact that the settings
  533.         // of the controls and the settings of the offset, max and min are
  534.         // not necessaraly identical (its best to stay close to that state)
  535.  
  536.     
  537.     oldClip = NewRgn();
  538.     GetClip(oldClip);
  539.     diff = 0;
  540.  
  541.     newValue = fVOffSet + change;
  542.     
  543.     if(change<0)
  544.     {
  545.         oldMin = GetCtlMin(fVertScrollBar);
  546.         if(newValue < oldMin)
  547.             newValue = oldMin;
  548.     }
  549.     else
  550.     {
  551.         oldMax = GetCtlMax(fVertScrollBar);
  552.         if(newValue > oldMax)
  553.             newValue = oldMax;
  554.     }
  555.     diff = fVOffSet - newValue;
  556.     
  557.     FocusOnContent();
  558.     ScrollContents(0, diff);
  559.         // changes the offsets
  560.     
  561.     FocusOnWindow();
  562.     SetCtlValue(fVertScrollBar, fVOffSet);
  563.         // now bring the control up to date with the offsets
  564.  
  565.     
  566.     SetClip(oldClip);
  567.     DisposeRgn(oldClip);
  568. }// DoVScroll member function
  569.  
  570.  
  571.     
  572. pascal void DScrollWindow::ActionProc(ControlHandle theControl, short partCode)
  573. {
  574.     short scrollAmount = 0;
  575.     DScrollWindow *theCurrScrollWindow;
  576.     
  577.     theCurrScrollWindow = (DScrollWindow *) GetWRefCon( FrontWindow() );// isn't system7 great?
  578.     
  579.     if(theControl == theCurrScrollWindow->fVertScrollBar )
  580.     {
  581.         scrollAmount = theCurrScrollWindow->GetVertLineScrollAmount();
  582.         if(partCode == inUpButton)
  583.             theCurrScrollWindow->DoVScroll(-scrollAmount);
  584.         if(partCode == inDownButton)
  585.             theCurrScrollWindow->DoVScroll(scrollAmount);
  586.     }
  587.     else if( theControl == theCurrScrollWindow->fHorizScrollBar)
  588.     {
  589.         scrollAmount = theCurrScrollWindow->GetHorizLineScrollAmount();
  590.         if(partCode == inUpButton)
  591.             theCurrScrollWindow->DoHScroll(-scrollAmount);
  592.         if(partCode == inDownButton)
  593.             theCurrScrollWindow->DoHScroll(scrollAmount);
  594.     }
  595.  
  596. }// end of ActionProc proc
  597.