home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / TextEdit / ScalingScrollView.m < prev    next >
Text File  |  1996-08-12  |  6KB  |  161 lines

  1. /*
  2.         ScalingScrollView.m
  3.     Copyright (c) 1995-1996, NeXT Software, Inc.
  4.         All rights reserved.
  5.         Author: Mike Ferris
  6.  
  7.     You may freely copy, distribute and reuse the code in this example.
  8.     NeXT disclaims any warranty of any kind, expressed or implied,
  9.     as to its fitness for any particular use.
  10. */
  11.  
  12. #import "ScalingScrollView.h"
  13.  
  14. #import <Foundation/NSGeometry.h>
  15. #import <AppKit/NSPopUpButton.h>
  16. #import <AppKit/NSScroller.h>
  17. #import <AppKit/NSButtonCell.h>
  18. #import <AppKit/NSFont.h>
  19.  
  20. /* For genstrings:
  21.     NSLocalizedString(@"10%", @"Zoom popup entry")
  22.     NSLocalizedString(@"25%", @"Zoom popup entry")
  23.     NSLocalizedString(@"50%", @"Zoom popup entry")
  24.     NSLocalizedString(@"75%", @"Zoom popup entry")
  25.     NSLocalizedString(@"100%", @"Zoom popup entry")
  26.     NSLocalizedString(@"128%", @"Zoom popup entry")
  27.     NSLocalizedString(@"200%", @"Zoom popup entry")
  28.     NSLocalizedString(@"400%", @"Zoom popup entry")
  29.     NSLocalizedString(@"800%", @"Zoom popup entry")
  30.     NSLocalizedString(@"1600%", @"Zoom popup entry")
  31. */   
  32. static NSString *_NSDefaultScaleMenuLabels[] = {/* @"Set...", */ @"10%", @"25%", @"50%", @"75%", @"100%", @"128%", @"200%", @"400%", @"800%", @"1600%"};
  33. static float _NSDefaultScaleMenuFactors[] = {/* 0.0, */ 0.1, 0.25, 0.5, 0.75, 1.0, 1.28, 2.0, 4.0, 8.0, 16.0};
  34. static unsigned _NSDefaultScaleMenuSelectedItemIndex = 4;
  35. static float _NSButtonPadding = 1.0;
  36. static float _NSScaleMenuFontSize = 10.0;
  37.  
  38. @implementation ScalingScrollView
  39.  
  40. - (id)initWithFrame:(NSRect)rect {
  41.     if (self = [super initWithFrame:rect]) {
  42.         scaleFactor = 1.0;
  43.     }
  44.     return self;
  45. }
  46.  
  47. - (void)_makeScalePopUpButton {
  48.     if (_scalePopUpButton == nil) {
  49.         unsigned cnt, numberOfDefaultItems = (sizeof(_NSDefaultScaleMenuLabels) / sizeof(NSString *));
  50.         NSButtonCell *curItem;
  51.  
  52.         // create it
  53.         _scalePopUpButton = [[NSPopUpButton allocWithZone:[self zone]] initWithFrame:NSMakeRect(0.0, 0.0, 1.0, 1.0) pullsDown:NO];
  54.  
  55.         // fill it
  56.         for (cnt = 0; cnt < numberOfDefaultItems; cnt++) {
  57.             [_scalePopUpButton addItemWithTitle:NSLocalizedString(_NSDefaultScaleMenuLabels[cnt], nil)];
  58.             curItem = [_scalePopUpButton itemAtIndex:cnt];
  59.             if (_NSDefaultScaleMenuFactors[cnt] != 0.0) {
  60.                 [curItem setRepresentedObject:[NSNumber numberWithFloat:_NSDefaultScaleMenuFactors[cnt]]];
  61.             }
  62.         }
  63.         [_scalePopUpButton selectItemAtIndex:_NSDefaultScaleMenuSelectedItemIndex];
  64.  
  65.         // hook it up
  66.         [_scalePopUpButton setTarget:self];
  67.         [_scalePopUpButton setAction:@selector(scalePopUpAction:)];
  68.  
  69.         // set a suitable font
  70.         [_scalePopUpButton setFont:[NSFont systemFontOfSize:_NSScaleMenuFontSize]];
  71.  
  72.         // Make sure the popup is big enough to fit the cells.
  73.         [_scalePopUpButton sizeToFit];
  74.  
  75.     // don't let it become first responder
  76.     [_scalePopUpButton setRefusesFirstResponder:YES];
  77.  
  78.         // put it in the scrollview
  79.         [self addSubview:_scalePopUpButton];
  80.         [_scalePopUpButton release];
  81.     }
  82. }
  83.  
  84. - (void)tile {
  85.     // Let the superclass do most of the work.
  86.     [super tile];
  87.  
  88.     if (![self hasHorizontalScroller]) {
  89.         if (_scalePopUpButton) [_scalePopUpButton removeFromSuperview];
  90.         _scalePopUpButton = nil;
  91.     } else {
  92.     NSScroller *horizScroller;
  93.     NSRect horizScrollerFrame, buttonFrame, incrementLineFrame;
  94.     
  95.         if (!_scalePopUpButton) [self _makeScalePopUpButton];
  96.  
  97.         horizScroller = [self horizontalScroller];
  98.         horizScrollerFrame = [horizScroller frame];
  99.     incrementLineFrame = [horizScroller rectForPart:NSScrollerIncrementLine];
  100.         buttonFrame = [_scalePopUpButton frame];
  101.  
  102.         // Now we'll just adjust the horizontal scroller size and set the button size and location.
  103.         horizScrollerFrame.size.width = horizScrollerFrame.size.width - buttonFrame.size.width - _NSButtonPadding;
  104.         [horizScroller setFrameSize:horizScrollerFrame.size];
  105.  
  106.         buttonFrame.origin.x = NSMaxX(horizScrollerFrame);
  107.     buttonFrame.size.height = incrementLineFrame.size.height;
  108.         buttonFrame.origin.y = horizScrollerFrame.origin.y + incrementLineFrame.origin.y;
  109.         [_scalePopUpButton setFrame:buttonFrame];
  110.     }
  111. }
  112.  
  113. - (void)scalePopUpAction:(id)sender {
  114.     NSNumber *selectedFactorObject = [[sender selectedCell] representedObject];
  115.     
  116.     if (selectedFactorObject == nil) {
  117.         NSLog(@"Scale popup action: setting arbitrary zoom factors is not yet supported.");
  118.         return;
  119.     } else {
  120.         [self setScaleFactor:[selectedFactorObject floatValue]];
  121.     }
  122. }
  123.  
  124. - (float)scaleFactor {
  125.     return scaleFactor;
  126. }
  127.  
  128. - (void)setScaleFactor:(float)newScaleFactor {
  129.     if (scaleFactor != newScaleFactor) {
  130.     NSSize curDocFrameSize, newDocBoundsSize;
  131.     NSView *clipView = [[self documentView] superview];
  132.     
  133.     scaleFactor = newScaleFactor;
  134.     
  135.     // Get the frame.  The frame must stay the same.
  136.     curDocFrameSize = [clipView frame].size;
  137.     
  138.     // The new bounds will be frame divided by scale factor
  139.     newDocBoundsSize.width = curDocFrameSize.width / newScaleFactor;
  140.     newDocBoundsSize.height = curDocFrameSize.height / newScaleFactor;
  141.     
  142.     [clipView setBoundsSize:newDocBoundsSize];
  143.     }
  144. }
  145.  
  146. - (void)setHasHorizontalScroller:(BOOL)flag {
  147.     if (!flag) [self setScaleFactor:1.0];
  148.     [super setHasHorizontalScroller:flag];
  149. }
  150.  
  151. @end
  152.  
  153. /*
  154.  
  155.  12/94 mferris    Created
  156.  3/4/95 aozer    Use in Edit; added ability to turn popup off/on
  157.  5/4/95 aozer    Scale the clipview rather than the docview
  158.  7/20/95 aozer    Made popup entries localizable
  159.  
  160. */
  161.