home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscSwapKit / MiscSwapView.m < prev    next >
Encoding:
Text File  |  1994-03-08  |  5.4 KB  |  238 lines

  1. /* MiscSwapView.m
  2.  *
  3.  * This subclass of View is able to swap different views into itself. It's
  4.  * used to implement any kind of multipage windows. (Inspectors, Prefs...)
  5.  *
  6.  * For more interface-info see the header file. More in depth information
  7.  * can be found here in the source-code.
  8.  *
  9.  * Notes: There is one Cateogry for this class includes automatic swapping.
  10.  *
  11.  * Improved by:        Thomas Engel
  12.  * First changes:   24.01.1994 (Copyleft)
  13.  * Last modified:     24.02.1994
  14.  */
  15.  
  16. #import <misckit/misckit.h>
  17.  
  18. @implementation MiscSwapView
  19.  
  20. - init
  21. {
  22.     NXRect theFrame = {{0.0, 0.0}, {0.0, 0.0}};
  23.     return [self initFrame:&theFrame];
  24. }
  25.  
  26. - initFrame:(const NXRect *)frameRect
  27. {
  28.     // Designated initilizer. We will set allow resizing by default and
  29.     // we will draw in Lightgray if we have to swap 'nil' in.
  30.  
  31.     self = [super initFrame:frameRect];
  32.     if( !self ) return self;
  33.  
  34.     backgroundGray = NX_LTGRAY;
  35.     [self setAutoresizeSubviews:YES];
  36.     useBuffering = NO;
  37.     
  38.     // Well the ByObject Category want's some other defaults too.
  39.     
  40.     controllers = [List new];     
  41.      [self setTagComparison:YES];
  42.  
  43.     return self;
  44. }
  45.  
  46. - free
  47. {
  48.     // We do not free the contentsControllers of this list! Well we didin't
  49.     // allocate them..but maybe we should free them...hmmm. I'm not sure yet.
  50.     
  51.     [controllers free];
  52.     return [super free];
  53. }
  54.  
  55. - setDelegate:(id)anObject
  56. {
  57.     delegate = anObject;
  58.     return self;
  59. }
  60.  
  61. - delegate
  62. {
  63.     return delegate;
  64. }
  65.  
  66. - setBackgroundGray:(float)aGray
  67. {
  68.     backgroundGray = aGray;
  69.     return self;
  70. }
  71.  
  72. - (float)backgroundGray
  73. {
  74.     return backgroundGray;
  75. }
  76.  
  77. - setUseBuffering:(BOOL)flag
  78. {
  79.     useBuffering = flag;
  80.     return self;
  81. }
  82.  
  83. - (BOOL)doesUseBuffering
  84. {
  85.     return useBuffering;
  86. }
  87.  
  88. - setContentView:aView
  89. {
  90.      // This is the real method !! 
  91.      // Here we process the finding of the swapping and resizing.
  92.     // If the new view is different from our current contentView we should
  93.     // put the current back where it came from.
  94.     
  95.     NXRect    theFrame;
  96.     id        contentViewsHomeWindow;
  97.     int        gState = 0;
  98.     
  99.     if( aView != contentView )
  100.     {
  101.         if( contentView && contentViewsHomeView )
  102.         {
  103.             [contentView removeFromSuperview];
  104.             [contentViewsHomeView addSubview:contentView];
  105.             [contentView moveTo:contentViewsHomeRect.origin.x
  106.                                :contentViewsHomeRect.origin.y];
  107.             [contentView sizeTo:contentViewsHomeRect.size.width
  108.                                :contentViewsHomeRect.size.height];
  109.         }
  110.     
  111.         // No real subview? So clean up the background. This is done by
  112.         // filling the view bounds with the backgroundGray we did set.
  113.     
  114.         contentView = aView;
  115.         
  116.         if( !contentView )
  117.         {
  118.             contentViewsHomeView = nil;
  119.     
  120.             [self lockFocus];
  121.                 PSsetgray( backgroundGray );
  122.                 NXRectFill(&bounds);
  123.             [self unlockFocus];
  124.         }
  125.  
  126.         // Otherwise we have a new subview so lets remove it form its home-
  127.         // window, resize it and align it to our 0,0 corner.
  128.         // If we have to use buffered composing we will now display our self
  129.         // but we will compose into the screen. This might cause problems with
  130.         // views that have to be resized.
  131.         
  132.         else
  133.         {
  134.             // If we use buffering then lets compose the views offscreen
  135.             // if they have no gState which meant that they haven't been
  136.             // drawn yet.
  137.             // Let's ensure the windows are always retained and non deffered.
  138.             // This make easy-offscreen drawing possible.
  139.             
  140.             contentViewsHomeWindow = [contentView window];
  141.             contentViewsHomeView = [contentView superview];
  142.             [contentView getFrame:&contentViewsHomeRect];
  143.  
  144.             if( useBuffering )
  145.             {
  146.                 [contentViewsHomeWindow setBackingType:NX_RETAINED];
  147.                  
  148.                 // Now lets get the views position inside its home.
  149.                 // We also need a gState to be able to do the fast copying.
  150.                 
  151.                 [contentView getFrame:&theFrame];
  152.                 gState = [contentViewsHomeView gState];
  153.                 if( !gState )
  154.                 {
  155.                     [contentViewsHomeView allocateGState];
  156.                     [contentViewsHomeView display];
  157.                     gState = [contentViewsHomeView gState];                
  158.                 }
  159.                 NXPing();
  160.             }
  161.             
  162.             // The view reorganication is always the same.
  163.             // Resizing is only applyed when we don't use buffering!
  164.             
  165.             [contentView removeFromSuperview];
  166.             [self addSubview:contentView];
  167.             [contentView moveTo:0.0 :0.0];
  168.             
  169.             // The displaying might come with a simple redraw or a quick
  170.             // composing by copying from the offscreen buffer.
  171.             
  172.             if( useBuffering && gState )
  173.             {
  174.                 // We will fill our background first to allow swapping of
  175.                 // views that don't have the same size.
  176.                 // Because they are not resized here to cover the whole
  177.                 // area its a must to clear the whole scene before!
  178.                 
  179.                 [self lockFocus];
  180.                  PSsetgray(backgroundGray);
  181.                  NXRectFill(&bounds);
  182.                 PScomposite( (float)theFrame.origin.x,
  183.                               (float)theFrame.origin.y,
  184.                              (float)theFrame.size.width,
  185.                              (float)theFrame.size.height,
  186.                              (int)gState,
  187.                              (float)0.0,
  188.                              (float)0.0,
  189.                              (int)NX_COPY );
  190.                 [self unlockFocus];
  191.             }
  192.             else
  193.             {
  194.                 [contentView sizeTo:bounds.size.width :bounds.size.height];
  195.                 [self display];
  196.             }
  197.         }
  198.         // Let the windowServer catch up to the changes we made (yawn...)
  199.         // And show our nice new window
  200.         
  201.         NXPing();
  202.         [[self window] flushWindow];
  203.     }
  204.     return self;
  205. }
  206.  
  207. - contentView
  208. {
  209.     return contentView;
  210. }
  211.  
  212. - contentViewsHomeView;
  213. {
  214.     return contentViewsHomeView;
  215. }
  216.  
  217. - write:(NXTypedStream *)stream
  218. {
  219.     return self;
  220. }
  221.  
  222. - read:(NXTypedStream *)stream
  223. {
  224.     return self;
  225. }
  226.  
  227. @end
  228.  
  229. /*
  230.  * History: 24.02.94 Added Greg's quick composing.
  231.  *
  232.  *            24.01.94 Redesign from my old swapView. Added Buffering.
  233.  *
  234.  *            21.01.94 Added the resizing job the Greg's swapView.
  235.  *
  236.  * 
  237.  * Bugs: - Could be. Yes.
  238.  */