/* 
   Project: TestApp3

   Copyright (C) 2007 Free Software Foundation

   Author: Mark Tracy

   Created: 2007-06-02 14:48:38 -0700 by gnustep
   
   Application Controller

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
 
   This application is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include "AppController.h"

@implementation AppController

+ (void)initialize
{
  NSMutableDictionary *defaults = [NSMutableDictionary dictionary];

  /*
   * Register your app's defaults here by adding objects to the
   * dictionary, eg
   *
   * [defaults setObject:anObject forKey:keyForThatObject];
   *
   */
  
  [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
  [[NSUserDefaults standardUserDefaults] synchronize];
}

- (id)init
{
  if ((self = [super init]))
    {
    }
  return self;
}

- (void)dealloc
{
  [textField release];
	[window release];
  [super dealloc];
}

- (void)awakeFromNib
{
  [[NSApp mainMenu] setTitle:@"TestApp3"];
  [textField setStringValue: @"TestApp3"];
  [window orderFront: self];
  [window makeKeyAndOrderFront: self];
  if (window) NSBeep();
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotif
{
}

- (BOOL)applicationShouldTerminate:(id)sender
{
  return YES;
}

- (void)applicationWillTerminate:(NSNotification *)aNotif
{
}

- (BOOL)application:(NSApplication *)application openFile:(NSString *)fileName
{
  return NO;
}

- (void)showPrefPanel:(id)sender
{
}

- (NSWindow *) window { return window; }

- (BOOL) windowShouldClose: (id) sender
{
  if (sender == [self window]) // we clicked the close box so terminate
  {
    [[NSApplication sharedApplication] terminate: sender];
  }
  // we pushed the reset button so just close the window
  return YES;
}

- (void) windowWillClose: (NSNotification *) note
{
}

- (void) resetTextField: (id) sender
{
  [[self window] close];
//	[window release];  // release on close was checked
	window = nil;
  [NSBundle loadNibNamed: @"TestApp3" owner: self];
  [self awakeFromNib];
  NSLog(@"frame %@, bounds %@", NSStringFromRect([textField frame]), NSStringFromRect([textField bounds]));
  {
      NSView *view;

      view = textField;
      while (view != nil)
        {
          NSLog(@"view %@ flipped %d", view, [view isFlipped]);
          NSLog(@"convert rect %@", NSStringFromRect([view convertRect: NSMakeRect(0, 0, 10, 20) toView: nil]));
          view = [view superview];
        }
  }
  
}

- (void) rotateFrame: (id) sender
{
  float oldAngle = [textField frameRotation]; 
	NSRect oldRect;
  [textField setFrameRotation: oldAngle + 10]; 
  
  oldRect = [textField frame]; 
  [textField setFrame: NSOffsetRect(oldRect,10,10)]; 
  [[textField superview] setNeedsDisplay: YES];
  [[textField superview] setNeedsDisplay: YES];

/*
  [textField setFrameRotation: [textField frameRotation] + 15.0];
  NSLog(@"frame %@, bounds %@", NSStringFromRect([textField frame]), NSStringFromRect([textField bounds]));
  NSLog(@"convert rect %@", NSStringFromRect([textField convertRect: NSMakeRect(0, 0, 10, 20) toView: nil]));
  [[self window] display];
*/
}

- (void) rotateBounds: (id) sender
{
  [textField setBoundsRotation: [textField boundsRotation] + 15.0];
  NSLog(@"frame %@, bounds %@", NSStringFromRect([textField frame]), NSStringFromRect([textField bounds]));
  NSLog(@"convert rect %@", NSStringFromRect([textField convertRect: NSMakeRect(0, 0, 10, 20) toView: nil]));
  NSLog(@"convert point(0, 0) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(0, 0) toView: nil]));
  NSLog(@"convert point(1, 0) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(1, 0) toView: nil]));
  NSLog(@"convert point(0, 1) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(0, 1) toView: nil]));
  [[self window] display];
}

- (void) translateFrame: (id) sender
{
  NSRect textFrame = [textField frame];
  textFrame.origin.x += 15.0;
  textFrame.origin.y += 15.0;
  [textField setFrame: textFrame];
  NSLog(@"frame %@, bounds %@", NSStringFromRect([textField frame]), NSStringFromRect([textField bounds]));
  NSLog(@"convert rect %@", NSStringFromRect([textField convertRect: NSMakeRect(0, 0, 10, 20) toView: nil]));
  [[self window] display];
}

- (void) translateBounds: (id) sender
{
  NSRect textBounds = [textField bounds];
  textBounds.origin.x += 5.0;
  textBounds.origin.y += 5.0;
  [textField setBounds: textBounds];
  NSLog(@"frame %@, bounds %@", NSStringFromRect([textField frame]), NSStringFromRect([textField bounds]));
  NSLog(@"convert rect %@", NSStringFromRect([textField convertRect: NSMakeRect(0, 0, 10, 20) toView: nil]));
  NSLog(@"convert point(0, 0) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(0, 0) toView: nil]));
  NSLog(@"convert point(1, 0) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(1, 0) toView: nil]));
  NSLog(@"convert point(0, 1) %@", NSStringFromPoint([textField convertPoint: NSMakePoint(0, 1) toView: nil]));
  [[self window] display];
}

@end
