
/*
**	MiscSwitchViewInspector.m 
**	Copyright (C) 1995  David Slotnick 
*/

// RCS identification information
static char *rcsID = "$Id: MiscSwitchViewInspector.m,v 1.1 1996/08/06 03:37:01 slotnick Exp $";
static void __AvoidCompilerWarning(void) {if(!rcsID)__AvoidCompilerWarning();}

// NeXTSTEP Headers
#import <InterfaceBuilder/InterfaceBuilder.h>

// System Headers

// Third Party Headers

// Other Headers
#import "MiscSwitchView.h"

// Class Headers
#import "MiscSwitchViewInspector.h"

// Private Constants

// Private Macros

// Module variables (static variables)


@implementation MiscSwitchViewInspector

/*"
   The MiscSwitchViewInspector implements the InterfaceBuilder inspector for the MiscSwitchView class.  The methods in this class apply immediately to the inspected MiscSwitchView instance, so the inspector buttons (OK and Revert) aren't displayed.
"*/


//-------------------------------------------------------------------
// 	Accessor methods
//-------------------------------------------------------------------

- (void) setCurrentViewTF:(NSTextField*)currentViewTF
/*" 
    Sets the current view text field to currentViewTF.
"*/
{
    [_currentViewTF autorelease];
    _currentViewTF = [currentViewTF retain];
}

- (NSTextField*) currentViewTF
/*"
   Returns the current view text field.
"*/
{
    return _currentViewTF;
}

- (void) setViewCountTF:(NSTextField*)viewCountTF
/*"
   Sets the view count text field to viewCountTF.
"*/
{
    [_viewCountTF autorelease];
    _viewCountTF = [viewCountTF retain];
}

- (NSTextField*) viewCountTF
/*"
   Returns the view count text field.
"*/
{
    return _viewCountTF;
}


//-------------------------------------------------------------------
// 	Derived accessors
//-------------------------------------------------------------------

- (MiscSwitchView*) switchView
/*"
   Returns the switch view inspected by the receiver.
"*/
{
    return [self object];
}


//-------------------------------------------------------------------
// 	Initialization / deallocation
//-------------------------------------------------------------------

- (id) init
/*"
   Extends the superclass method to load the the inspector nib file.
   Returns self if successful, nil otherwise.
"*/
{
    BOOL error = ([super init] == nil) ? YES : NO;

    if (!error) {

        // Load the inspector nib.
        if (![NSBundle loadNibNamed:@"MiscSwitchViewInspector.nib" owner:self]) {
            NSLog (@"MiscSwitchViewInspector:: Could not load "
                   @"MiscSwitchViewInspector.nib");
            error = YES;
        }

        // Free myself if the initialization fails.
        if (error) {
            [self autorelease];
        }
    }
		
	return error ? nil : self;
}

- (void) dealloc
/*"
   Standard deallocator.
"*/
{
    [_viewCountTF release];
    [_currentViewTF release];
}


//-------------------------------------------------------------------
// 	Adding and deleting subviews
//-------------------------------------------------------------------

- (void) addView:(id)sender
/*"
   Adds an empty view to the end of the receiver's subview list.
"*/
{
    [[self switchView] addView];
	[self revert:sender];
}

- (void) insertView:(id)sender
/*"
   Inserts an empty view at the index specified in the current view text field.
"*/
{
    MiscSwitchView*		switchView = [self switchView];
    BOOL		   		error = NO;	

    // Check internal state.
    if (!error && switchView == nil) {
        error = YES;
    }

    // Insert a new view.
    if (!error) {
        int newIndex = [[self currentViewTF] intValue];

        // Only insert a view if the requested index is valid.
        if ([switchView isIndexValid:newIndex]) {
            [switchView insertViewAtIndex:newIndex];
            [switchView displayViewAtIndex:newIndex];
        }
        
        // Otherwise, beep to indicate an invalid index.
        else {
            NSBeep ();
        }
        
        // Revert the inspector.
        [self revert:sender];
   }
}

- (void) deleteView:(id)sender
/*"
   Deletes the view at the index specified in the current view text field.
"*/
{
    MiscSwitchView*		switchView = [self switchView];
    BOOL				error = NO;

    // Check internal state.
    if (!error && switchView == nil) {
        error = YES;
    }

    // Delete the specified view.
    if (!error) {
        int indexToDelete = [[self currentViewTF] intValue];

        // Only delete the view if the requested index is valid.
        if ([switchView isIndexValid:indexToDelete]) {
            [switchView deleteViewAtIndex:indexToDelete];
            [switchView displayViewAtIndex:
                MIN (indexToDelete, [switchView lastIndex])];
        }

        // Otherwise, beep to indicate an invalid index.
        else {
            NSBeep ();
        }

        // Revert the inspector.
        [self revert:sender];
    }
}

- (void) setCurrentView:(id)sender
/*"
    Displays the view at the index specified in the current view text field.
"*/
{
    MiscSwitchView*		switchView = [self switchView];
    BOOL				error = NO;

    // Check internal state.
    if (!error && switchView == nil) {
        error = YES;
    }

    if (!error) {
        int indexToDisplay = [[self currentViewTF] intValue];

        // Only display the view if the requested index is valid.
        if ([switchView isIndexValid:indexToDisplay]) {
            [switchView displayViewAtIndex:indexToDisplay];
        }

        // Otherwise, beep to indicate an invalid index.
        else {
            NSBeep ();
        }

        // Revert the inspector.
        [self revert:sender];
    }
}

- (void) nextView:(id)sender
/*"
    Displays the view after the currently visible view, or the first view if the last view is currently visible.
"*/
{
    MiscSwitchView*		switchView = [self switchView];
    BOOL				error = NO;

    // Check internal state.
    if (!error && switchView == nil) {
        error = YES;
    }

    // Display the view after the currently visible view.
    if (!error) {
        int 	indexOfVisibileView = [switchView indexOfVisibleView];
        int 	indexToDisplay = [switchView indexAfter:indexOfVisibileView];

        // Only display the view if the requested index is valid.
        if ([switchView isIndexValid:indexToDisplay]) {
            [switchView displayViewAtIndex:indexToDisplay];
        }

        // Otherwise, beep to indicate an invalid index.
        else {
            NSBeep ();
        }

        // Revert the inspector.
        [self revert:sender];
    }
}

- (void) previousView:(id)sender
/*"
   Displays the view before the currently visible view, or the last view if the first view is currently visible.
"*/
{
    MiscSwitchView*		switchView = [self switchView];
    BOOL				error = NO;

    // Check internal state.
    if (!error && switchView == nil) {
        error = YES;
    }

    // Display the view before the currently visible view.
    if (!error) {
        int 	indexOfVisibileView = [switchView indexOfVisibleView];
        int 	indexToDisplay = [switchView indexBefore:indexOfVisibileView];

        // Only display the view if the requested index is valid.
        if ([switchView isIndexValid:indexToDisplay]) {
            [switchView displayViewAtIndex:indexToDisplay];
        }

        // Otherwise, beep to indicate an invalid index.
        else {
            NSBeep ();
        }

        // Revert the inspector.
        [self revert:sender];
    }
}


//-------------------------------------------------------------------
// 	IBInspector methods
//-------------------------------------------------------------------

- (void) revert:(id)sender
/*"
   Updates the inspector with the attributes of the currently inspected switch view.
"*/
{
    NSTextField*		viewCountTF = [self viewCountTF];
    NSTextField*		currentViewTF = [self currentViewTF];
    MiscSwitchView*		switchView = [self switchView];

    // Update the view count TF.
    [viewCountTF setIntValue:[switchView viewCount]];
	[viewCountTF display];

    // Update the current view TF.
    [currentViewTF setIntValue:[switchView indexOfVisibleView]];
	[currentViewTF display];

// Added to display editors after subview changes.  This seems
// to have broken in OpenStep/Mach PR2 [dls 4/30/96].
    [[(id <IB>)[NSApplication sharedApplication] activeDocument]
        drawObject:switchView];
// End added [dls 4/30/96].

    // Invoke superclass functionality.
    [super revert:sender];
}

- (BOOL) wantsButtons
/*"
   The inspector controls take effect immediately, so the OK and Revert buttons aren't needed.
"*/   
{
    return NO;
}

@end
