//
// Time-stamp: <94/12/30 16:02:59 stephan>
//
// VRulerControl.m
// Project: Rulers
//
// Stephan Wacker
// 93-01-31


#import "VRulerControl.h"
#import "VRulerView.h"
#import "RulerPreferencesControl.h"


@implementation VRulerControl


- (int) vMarksMode
// Answer the current vMarks mode.
{
    return vMarksPosition;
}

- setVMarksMode: (int) position
// Set new vMarks mode.
// PRIVATE
{
    if( vMarksPosition != position )
    {
	vMarksPosition = position;
	[view setCacheInvalid];
    }
    
    return self;
}



- (int) vPosNumMode
// Answer the current positive number mode.
{
    return posNumDirection;
}

- setVPosNumMode: (int) mode
// Set new positive number mode.
// PRIVATE
{
    if( posNumDirection != mode )
    {
	posNumDirection = mode;
	[view setCacheInvalid];
    }
    
    return self;
}



- setColor: (NXColor) col
// Set new color.
{
    [window disableDisplay];

    [super setColor: col];

    // The RulerView will take the new color automatically.
    // The other controls, however, need to have their images changed.

    [self makeColoredImage: &upArrowImage[0]
     shape: "UpArrow" mask: "UpArrowMask"];
    [upArrow setImage: upArrowImage[0]];

    [self makeColoredImage: &upArrowImage[1]
     shape: "UpArrowH" mask: "UpArrowMask"];
    [upArrow setAltImage: upArrowImage[1]];


    [self makeColoredImage: &downArrowImage[0]
     shape: "DownArrow" mask: "DownArrowMask"];
    [downArrow setImage: downArrowImage[0]];

    [self makeColoredImage: &downArrowImage[1]
     shape: "DownArrowH" mask: "DownArrowMask"];
    [downArrow setAltImage: downArrowImage[1]];


    [self makeColoredImage: &battenImage[0]
     shape: "VBatten" mask: "VBattenMask"];
    [leftBatten setImage: battenImage[0]];
    [rightBatten setImage: battenImage[0]];

    [self makeColoredImage: &battenImage[1]
     shape: "VBattenH" mask: "VBattenMask"];
    [leftBatten setAltImage: battenImage[1]];
    [rightBatten setAltImage: battenImage[1]];


    [self makeColoredImage: &controlBoxImage
     shape: "VControlBox" mask: "VControlBoxMask"];
    [controlBoxBackground setImage: controlBoxImage];

    [[window reenableDisplay] display];

    return self;
}



- awakeFromNib
// Initialize yourself after nib file has been loaded.
{
    [self setVMarksMode: [RulerPreferencesControl defaultVMarksMode]];
    [self setVPosNumMode: [RulerPreferencesControl defaultVPosNumMode]];
    
    return [super awakeFromNib];
}



- resizeBottom: sender
// Resize ruler window at bottom edge.
{
    return [self resize: @selector( sizeBottom: )];
}



- resizeTop: sender
// Resize ruler window at top edge.
{
    return [self resize: @selector( sizeTop: )];
}



- slideDown: sender
// Move ruler's origin down.
{
    static NXSize	delta = { 0.0, -1.0 };
    
    [window disableDisplay];
    [view moveOrigin: &delta];			// move ruler marks
    [window reenableDisplay];
    [window display];				// re-display controls
    
    return self;
}



- slideUp: sender
// Move ruler's origin up.
{
    static NXSize	delta = { 0.0, 1.0 };
    
    [window disableDisplay];
    [view moveOrigin: &delta];			// move ruler marks
    [window reenableDisplay];
    [window display];
    
    return self;
}



- takePreferencesFrom: sender
// Get all preferences settings from the sender object.
{
    [self setVMarksMode: [sender vMarksMode]];
    [self setVPosNumMode: [sender vPosNumMode]];
    
    return [super takePreferencesFrom: sender];
}


@end // VRulerControl



@implementation VRulerControl( WindowDelegate )


- windowDidResize: sender
// If the window was resized by our resizeTop: and resizeBottom: methods
// its subviews may be placed incorrectly because of truncation errors.
// The controlBox should be re-centered within its superview.
{
    NXRect	controlBoxFrame;
    NXRect	superviewFrame;
    int		y;
    
    [controlBox getFrame: &controlBoxFrame];
    [[controlBox superview] getFrame: &superviewFrame];
    
    y = (  NX_HEIGHT( &superviewFrame  )
         - NX_HEIGHT( &controlBoxFrame )  ) * 0.5;
    [controlBox moveTo: NX_X( &controlBoxFrame ) : y];
    
    
    return self;
}


// These keystrokes are only recognized by a vertical ruler.
//
- keyDown: (NXEvent *) theEvent
{
    switch( theEvent->data.key.charSet ) {
      
      case NX_ASCIISET:
	
	switch( theEvent->data.key.charCode ) {
	  default:
	    return [super keyDown: theEvent];
	} // switch charCode
	
	break; // case NX_ASCIISET
	
      
      case NX_SYMBOLSET:
	
	switch( theEvent->data.key.charCode ) {
	  case KEY_UP:
	    return [self slideUp: nil];
	  case KEY_DOWN:
	    return [self slideDown: nil];
	  default:
	    return [super keyDown: theEvent];
	} // switch charCode
	
	break; // case NX_SYMBOLSET
	
    } // switch charSet
    
    [window display];
    return self; // event was handled
}


@end // VRulerControl( WindowDelegate )
