News Software Download Buy Now About Us Developers Contact
community


Here's a class you can add to your apps to allow you to quickly and easily add custom controls in the scroll bar area of your document window.
Source Code and Sample App.

    To use:
1. Add SDScrollerAccessoryScrollView.[hm] to your project
2. Open your document nib file in Interface Builder
3. Drag in SDScrollerAccessoryScrollView.h into the Classes window
4. Select the scrollView containing your document view, exp: myGroovyDocumentView
5. Bring up the Inspector
6. Choose "Custom Class"
7. Select "SDScrollerAccessoryScrollView"
8. Add a new instance variable to your NSDocument subclass for each accessory, exp:
        horizontalAccessoryView and verticalAccessoryView
9. Create the buttons and controls in IB, and group them into a box.
        These can just float anywhere in the window - they'll be inserted later.
     Your NSDocument subclass can have methods which these will call
10. set the box to have no title and no border, resize to the size of a scroller width
11. connect the new instance variable to the box
12. In your - windowDidLoad method of your NSDocument subclass, you'd invoke, for example:

- (void)windowDidLoad {
    NSScrollView *enclosingScrollView;

    [super windowDidLoad];
    enclosingScrollView = [myGroovyDocumentView enclosingScrollView];

    [(SDScrollerAccessoryScrollView *)enclosingScrollView
setHorizontalScrollerAccessory:horizontalAccessoryView
onRightSide:YES];

    [(SDScrollerAccessoryScrollView *)enclosingScrollView
setVerticalScrollerAccessory:verticalAccessoryView atTop:YES];

}

//
// SDScrollerAccessoryScrollView.h
//
// (C) Copyright 1989-1999 Andrew C. Stone and Stone Design Corp
// All rights reserved. You can use this code, but please leave credit.
//

#import <AppKit/AppKit.h>

extern NSString *VisibleRectDidChangeNotification;

@interface SDScrollerAccessoryScrollView : NSScrollView
{
    BOOL onRightSide;
    BOOL atTop;
    IBOutlet NSView *horizontalAccessory;
    IBOutlet NSView *verticalAccessory;
}

- (void)setHorizontalScrollerAccessory:(id)sender
onRightSide:(BOOL)rightSide;
- (void)setVerticalScrollerAccessory:(id)sender atTop:(BOOL)top;

- (void)setHorizontalScrollerAccessory:(id)sender;
- (void)setVerticalScrollerAccessory:(id)sender;

// by default, at left, and at top
- (void)setVerticalScrollerAccessoryIsAtTop:(BOOL)atTop;
- (void)setHorizontalAccessoryIsOnRightSide:(BOOL)rightSide;

@end

***************

//
// SDScrollerAccessoryScrollView.m
//
// (C) Copyright 1989-1999 Andrew C. Stone and Stone Design Corp
// All rights reserved. You can use this code, but please leave credit.
//

#import "SDScrollerAccessoryScrollView.h"

NSString *VisibleRectDidChangeNotification = @"VisibleRectChanged";

@implementation SDScrollerAccessoryScrollView

- (void)reflectScrolledClipView:(NSClipView *)aClipView
{
    [super reflectScrolledClipView:aClipView];
// Clients might need to know:
    [[NSNotificationCenter defaultCenter]
postNotificationName:VisibleRectDidChangeNotification object:self];
}

- (void)setHorizontalScrollerAccessory:(id)sender
onRightSide:(BOOL)rightSide
{
    horizontalAccessory = sender;
    onRightSide = rightSide;
}

- (void)setVerticalScrollerAccessory:(id)sender atTop:(BOOL)top
{
    verticalAccessory = sender;
    atTop = top;
}

- (void)setHorizontalScrollerAccessory:(id)sender;
{
    horizontalAccessory = sender;
}

- (void)setVerticalScrollerAccessory:(id)sender;
{
    verticalAccessory = sender;
}


// by default, at left, and at top
- (void)setVerticalScrollerAccessoryIsAtTop:(BOOL)top;
{
    atTop = top;
}

- (void)setHorizontalAccessoryIsOnRightSide:(BOOL)rightSide;
{
    onRightSide = rightSide;
}

- (void)tile;
{
    NSRect scrollFrame = [self frame];
    NSRect frame;
    NSRect r;
    NSScroller *scroller;
    float x, y;

// Let super stick in the accessory views:
    [super tile];


// Grab the existing frame of the scroller & reset that rect according to
// whether it's left-right or top-bottom
    scroller = [self verticalScroller];
    r = [scroller frame];
    if (verticalAccessory) {
        frame = [verticalAccessory frame];
        if (frame.size.width != [[scroller class]scrollerWidth]) {
            frame.size.width = [[scroller class]scrollerWidth];
            [verticalAccessory setFrameSize:frame.size];
        }

        r.size.height -= frame.size.height;

        if ([self isFlipped]) x = r.origin.x;
        else x = scrollFrame.size.width - [[scroller class]scrollerWidth];

        if ([self isFlipped]) {
            if (!atTop) {
                [verticalAccessory setFrameOrigin:NSMakePoint(x,r.origin.y +
                    [[scroller class]scrollerWidth] + 1.)];
            } else {
                [verticalAccessory setFrameOrigin:NSMakePoint(x,r.size.height +
                     [[scroller class]scrollerWidth])];
                [scroller setFrameOrigin:
                    NSMakePoint(x,r.origin.y + frame.size.height)];
            }
        } else {
            if (atTop) {
                [verticalAccessory setFrameOrigin:NSMakePoint(x,r.size.height)];
            } else {
                [verticalAccessory setFrameOrigin:NSMakePoint(x,0.)];
                [scroller
setFrameOrigin:NSMakePoint(x,frame.size.height)];
            }
        }
        [scroller setFrameSize:r.size];
        [verticalAccessory setNeedsDisplay:YES];
    }


    scroller = [self horizontalScroller];
    r = [scroller frame];
    if (horizontalAccessory) {
        frame = [horizontalAccessory frame];

        if (frame.size.height != [[scroller class]scrollerWidth]) {
            frame.size.height = [[scroller class]scrollerWidth];
            [horizontalAccessory setFrameSize:frame.size];
        }

        r.size.width -= frame.size.width;
        if ([self isFlipped])
            y = scrollFrame.size.height - [[scroller class] scrollerWidth];
        else
            y = 0.;

if (onRightSide) {
            [horizontalAccessory setFrameOrigin:NSMakePoint(r.origin.x + r.size.width, 0.)];
} else {
            [horizontalAccessory setFrameOrigin:NSMakePoint(r.origin.x, 0.)];
            [scroller setFrameOrigin:NSMakePoint(r.origin.x +
                    frame.size.width, r.origin.y)];
        }
        [scroller setFrameSize:r.size];
        [horizontalAccessory setNeedsDisplay:YES];

    }
}

@end




Stone Design's Create(tm)
1999-04-20 11:38:28 -0600
©2000 Stone Design top