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


#import "HRulerControl.h"
#import "HRulerView.h"
#import "RulerPreferencesControl.h"


@implementation HRulerControl


- (int) hMarksMode
// Answer the current hMarks mode.
{
    return hMarksPosition;
}

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



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

- setHPosNumMode: (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: &leftArrowImage[0]
     shape: "LeftArrow" mask: "LeftArrowMask"];
    [leftArrow setImage: leftArrowImage[0]];

    [self makeColoredImage: &leftArrowImage[1]
     shape: "LeftArrowH" mask: "LeftArrowMask"];
    [leftArrow setAltImage: leftArrowImage[1]];


    [self makeColoredImage: &rightArrowImage[0]
     shape: "RightArrow" mask: "RightArrowMask"];
    [rightArrow setImage: rightArrowImage[0]];

    [self makeColoredImage: &rightArrowImage[1]
     shape: "RightArrowH" mask: "RightArrowMask"];
    [rightArrow setAltImage: rightArrowImage[1]];


    [self makeColoredImage: &battenImage[0]
     shape: "HBatten" mask: "HBattenMask"];
    [topBatten setImage: battenImage[0]];
    [bottomBatten setImage: battenImage[0]];

    [self makeColoredImage: &battenImage[1]
     shape: "HBattenH" mask: "HBattenMask"];
    [topBatten setAltImage: battenImage[1]];
    [bottomBatten setAltImage: battenImage[1]];


    [self makeColoredImage: &controlBoxImage
     shape: "HControlBox" mask: "HControlBoxMask"];
    [controlBoxBackground setImage: controlBoxImage];

    [[window reenableDisplay] display];

    return self;
}



- awakeFromNib
// Initialize yourself after nib file has been loaded.
{
    [self setHMarksMode: [RulerPreferencesControl defaultHMarksMode]];
    [self setHPosNumMode: [RulerPreferencesControl defaultHPosNumMode]];
    
    return [super awakeFromNib];
}



- resizeLeft: sender
// Resize ruler window at left edge.
{
    return [self resize: @selector( sizeLeft: )];
}



- resizeRight: sender
// Resize ruler window at right edge.
{
    return [self resize: @selector( sizeRight: )];
}



- slideLeft: sender
// Move ruler's origin to the left.
{
    static NXSize	delta = { -1.0, 0.0 };

    [window disableDisplay];
    [view moveOrigin: &delta];			// move ruler marks
    [window reenableDisplay];
    [window display];				// re-display controls
    
    return self;
}



- slideRight: sender
// Move ruler's origin to the right.
{
    static NXSize	delta = { 1.0, 0.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 setHMarksMode: [sender hMarksMode]];
    [self setHPosNumMode: [sender hPosNumMode]];
    
    return [super takePreferencesFrom: sender];
}


@end // HRulerControl



@implementation HRulerControl( WindowDelegate )


- windowDidResize: sender
// If the window was resized by our resizeLeft: and resizeRight: 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		x;
    
    [controlBox getFrame: &controlBoxFrame];
    [[controlBox superview] getFrame: &superviewFrame];
    
    x = (  NX_WIDTH( &superviewFrame  )
         - NX_WIDTH( &controlBoxFrame )  ) * 0.5;
    [controlBox moveTo: x : NX_Y( &controlBoxFrame )];
    
    
    return self;
}


// These keystrokes are only recognized by a horizontal 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_LEFT:
	    return [self slideLeft: nil];
	  case KEY_RIGHT:
	    return [self slideRight: nil];
	  default:
	    return [super keyDown: theEvent];
	} // switch charCode
	
	break; // case NX_SYMBOLSET
	
    } // switch charSet
    
    [window display];
    return self; // event was handled
}


@end // HRulerControl( WindowDelegate )
