home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Appearance SDK 1.0.4 / Appearance Sample Code / Source / UtilityWindow.cp < prev    next >
Encoding:
Text File  |  1999-07-16  |  7.5 KB  |  268 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        UtilityWindow.cp
  3.  
  4.     Contains:    Utility window tester class.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (edv)    Ed Voas
  21.  
  22.     Change History (most recent first):
  23.  
  24.          <1>     9/11/97    edv        First checked in.
  25. */
  26.  
  27. #include <MacWindows.h>
  28. #include "UtilityWindow.h"
  29. #include "Appearance.h"
  30. #include "ColorPenState.h"
  31. #include "Assertions.h"
  32.  
  33. #define width( r )        ( (r).right - (r).left )
  34. #define height( r )        ( (r).bottom - (r).top )
  35.  
  36. UtilityWindow::UtilityWindow( SInt16 windID ) : BaseWindow( windID )
  37. {                
  38.     Rect        rect;
  39.     
  40.         // Note the use of the new defProc constants here.
  41.         // This eliminates the need to go thru the mapping
  42.         // CDEF for scroll bars, which would normally happen
  43.         // after calling RegisterAppearanceClient.
  44.         
  45.     CalcVertScrollBarRect( rect );
  46.     fVertScrollBar = NewControl( fWindow, &rect, "\p", true, 0, 0, 100, kControlScrollBarProc, 0 );
  47.     
  48.     CalcHorizScrollBarRect( rect );
  49.     fHorizScrollBar = NewControl( fWindow, &rect, "\p", true, 0, 0, 100, kControlScrollBarProc, 0 );    
  50. }
  51.  
  52. UtilityWindow::UtilityWindow() : BaseWindow( 131 )
  53. {                
  54.     Rect        rect;
  55.     
  56.         // Note the use of the new defProc constants here.
  57.         // This eliminates the need to go thru the mapping
  58.         // CDEF for scroll bars, which would normally happen
  59.         // after calling RegisterAppearanceClient.
  60.         
  61.     CalcVertScrollBarRect( rect );
  62.     fVertScrollBar = NewControl( fWindow, &rect, "\p", true, 0, 0, 100, kControlScrollBarProc, 0 );
  63.     
  64.     CalcHorizScrollBarRect( rect );
  65.     fHorizScrollBar = NewControl( fWindow, &rect, "\p", true, 0, 0, 100, kControlScrollBarProc, 0 );    
  66. }
  67.  
  68. UtilityWindow::~UtilityWindow()
  69. {
  70.     if ( fVertScrollBar )
  71.         DisposeControl( fVertScrollBar );
  72.     
  73.     if ( fHorizScrollBar )
  74.         DisposeControl( fHorizScrollBar );
  75. }
  76.  
  77. //————————————————————————————————————————————————————————————————————————————————————
  78. //    • Activate
  79. //————————————————————————————————————————————————————————————————————————————————————
  80. //    Activates the contents of the window.
  81. //
  82. void
  83. UtilityWindow::Activate( EventRecord& )
  84. {
  85.         // Here we use the new ActivateControl call. We
  86.         // could have still used HiliteControl, but this
  87.         // is much more straightforward. It also works
  88.         // right with and without embedding, so it's a
  89.         // real good idea to start using it.
  90.         
  91.     ActivateControl( fHorizScrollBar );
  92.     ActivateControl( fVertScrollBar );
  93. }
  94.  
  95. //————————————————————————————————————————————————————————————————————————————————————
  96. //    • Deactivate
  97. //————————————————————————————————————————————————————————————————————————————————————
  98. //    Deactivates the contents of the window.
  99. //
  100. void
  101. UtilityWindow::Deactivate( EventRecord& )
  102. {
  103.         // Here we use the new DeactivateControl call. We
  104.         // could have still used HiliteControl, but this
  105.         // is much more straightforward. It also works
  106.         // right with and without embedding, so it's a
  107.         // real good idea to start using it.
  108.         
  109.     DeactivateControl( fHorizScrollBar );
  110.     DeactivateControl( fVertScrollBar );
  111. }
  112.  
  113. //————————————————————————————————————————————————————————————————————————————————————
  114. //    • Draw
  115. //————————————————————————————————————————————————————————————————————————————————————
  116. //    Draws our window.
  117. //
  118. void
  119. UtilityWindow::Draw()
  120. {
  121.     UpdateControls( fWindow, fWindow->visRgn );
  122. }
  123.  
  124. //————————————————————————————————————————————————————————————————————————————————————
  125. //    • Resize
  126. //————————————————————————————————————————————————————————————————————————————————————
  127. //    Resize our window to the appropriate size specified in width and height. Make sure
  128. //    the scroll bars are repositioned properly.
  129. //
  130. void
  131. UtilityWindow::Resize( SInt16 width, SInt16 height )
  132. {
  133.     Rect        horizRect, vertRect;
  134.     
  135.     InvalidateScrollBars();
  136.     
  137.     BaseWindow::Resize( width, height );
  138.     
  139.     InvalidateScrollBars();
  140.     
  141.     CalcHorizScrollBarRect( horizRect );
  142.     CalcVertScrollBarRect( vertRect );
  143.     
  144.     MoveControl( fHorizScrollBar, horizRect.left, horizRect.top );
  145.     MoveControl( fVertScrollBar, vertRect.left, vertRect.top );
  146.     SizeControl( fHorizScrollBar, width( horizRect ), height( horizRect ) );
  147.     SizeControl( fVertScrollBar, width( vertRect ), height( vertRect ) );
  148. }
  149.  
  150. //————————————————————————————————————————————————————————————————————————————————————
  151. //    • HandleClick
  152. //————————————————————————————————————————————————————————————————————————————————————
  153. //    Simple routine to handle scroll bar tracking, even though they don't do anything.
  154. //
  155. void
  156. UtilityWindow::HandleClick( EventRecord& event )
  157. {
  158.     ControlHandle        control;
  159.     SInt16                part;
  160.     Point                localPt;
  161.     ControlActionUPP    actionProc;
  162.     
  163.     SetPort( fWindow );
  164.     localPt = event.where;
  165.     GlobalToLocal( &localPt );
  166.     
  167.     part = FindControl( localPt, fWindow, &control );
  168.     switch ( part )
  169.     {
  170.         case kControlUpButtonPart:
  171.         case kControlDownButtonPart:
  172.         case kControlPageUpPart:
  173.         case kControlPageDownPart:
  174.             actionProc = NewControlActionProc( ScrollBarAction );
  175.             TrackControl( control, localPt, actionProc );
  176.             DisposeRoutineDescriptor( actionProc );
  177.             break;
  178.         
  179.         case kControlIndicatorPart:
  180.             TrackControl( control, localPt, (ControlActionUPP)-1L );
  181.             break;
  182.     }
  183. }
  184.  
  185. //————————————————————————————————————————————————————————————————————————————————————
  186. //    • InvalidateScrollBars
  187. //————————————————————————————————————————————————————————————————————————————————————
  188. //    Invalidates the scroll bar areas.
  189. //
  190. void
  191. UtilityWindow::InvalidateScrollBars()
  192. {
  193.     Rect        tempRect;
  194.     
  195.     SetPort( fWindow );
  196.     CalcHorizScrollBarRect( tempRect );
  197.     InvalRect( &tempRect );
  198.     EraseRect( &tempRect );
  199.     
  200.     CalcVertScrollBarRect( tempRect );
  201.     InvalRect( &tempRect );
  202.     EraseRect( &tempRect );
  203. }
  204.  
  205. //————————————————————————————————————————————————————————————————————————————————————
  206. //    • CalcHorizScrollBarRect
  207. //————————————————————————————————————————————————————————————————————————————————————
  208. //    Calculates the position where the horizontal scroll bar should be placed.
  209. //
  210. void
  211. UtilityWindow::CalcHorizScrollBarRect( Rect& rect )
  212. {
  213.     rect = fWindow->portRect;
  214.     rect.bottom++;
  215.     rect.left = -1;
  216.     rect.top = rect.bottom - 11;
  217.     rect.right -= 9;
  218. }
  219.  
  220. //————————————————————————————————————————————————————————————————————————————————————
  221. //    • CalcVertScrollBarRect
  222. //————————————————————————————————————————————————————————————————————————————————————
  223. //    Calculates the position where the vertical scroll bar should be placed.
  224. //
  225. void
  226. UtilityWindow::CalcVertScrollBarRect( Rect& rect )
  227. {
  228.     rect = fWindow->portRect;
  229.     rect.right++;
  230.     rect.left = rect.right - 11;
  231.     rect.bottom -= 9;
  232.     rect.top = -1;
  233. }
  234.  
  235. //————————————————————————————————————————————————————————————————————————————————————
  236. //    • ScrollBarAction
  237. //————————————————————————————————————————————————————————————————————————————————————
  238. //    A simple callback to give some feedback when clicking the scroll arrows or page
  239. //    up/down areas.
  240. //
  241. pascal void
  242. UtilityWindow::ScrollBarAction( ControlHandle control, SInt16 part )
  243. {
  244.     switch ( part )
  245.     {
  246.         case kControlUpButtonPart:
  247.             if ( GetControlValue( control) > GetControlMinimum( control ) )
  248.                 SetControlValue( control, GetControlValue( control ) - 1 );
  249.             break;
  250.         
  251.         case kControlDownButtonPart:
  252.             if ( GetControlValue( control) < GetControlMaximum( control ) )
  253.                 SetControlValue( control, GetControlValue( control ) + 1 );
  254.             break;
  255.             
  256.         case kControlPageUpPart:
  257.             if ( GetControlValue( control) > GetControlMinimum( control ) )
  258.                 SetControlValue( control, GetControlValue( control ) - 10 );
  259.             break;
  260.             
  261.         case kControlPageDownPart:
  262.             if ( GetControlValue( control) < GetControlMaximum( control ) )
  263.                 SetControlValue( control, GetControlValue( control ) + 10 );
  264.             break;
  265.     }            
  266. }
  267.  
  268.