/****************************************************************************
 *   Copyright (C) 2004 by Arboretum Systems, Inc.                          *
 *   Technical administrator: Alain Mazy, hav@mazy.be                       *
 *   Lead program designer: Georges Jaroslaw, gjaro@arboretum.com           *
 *                                                                          *
 *   HyperEngine-AV is a video, audio and text editor that can be used to   *
 *   record, import, edit, process and arrange together video, sound and    *
 *   text. These are all achieved in the program's free-form document       *
 *   window, with no limitations to the number of tracks.                   *
 *   The purpose of this program is to provide to users at all levels of    *
 *   expertise a readily accessible tool enabling them to create movies,    *
 *   slide shows or presentations.                                          *
 *                                                                          *
 *   HyperEngine-AV itself is free software. However, plug-ins that can be  *
 *   used as add-ons to HyperEngine-AV may or may not be free software, at  *
 *   the discretion of each plug-in developer and as specified by these     *
 *   plug-ins' respective licenses.                                         *
 *   You can redistribute HyperEngine-AV itself 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.                                             *
 *                                                                          *
 *   HyperEngine-AV 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 General Public License for all applicable details.                 *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to:                             *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/

#import "MIApplication.h"
#import "MIMontagePalette.h"
#import "MIPaletteController.h"

#ifndef ZzVerbose_h
#include "ZzVerbose.h"
#endif //ZzVerbose_h

/******** This is the base class for Palettes ********/

@implementation MIMontagePalette


+ (NSString*)visibleDefaultName {
    return nil;
}

- (NSString*)nibName {
    return nil;
}

+ (BOOL)factoryVisibleDefault {
    return NO;
}

- (int)menuTag {
	return 0;
}

- (NSString*)menuName {
	return @"";
}

+ (void)addDefaultValuesToDictionary:(NSMutableDictionary*)defaultValues {
    [defaultValues setObject:[NSNumber numberWithBool:[self factoryVisibleDefault]] forKey:[self visibleDefaultName]];
}

- (id)init {
    if (( self = [super initWithWindowNibName:[self nibName]] )) {
		fMenuItem = nil;
		fWindowJustLoaded = NO;
		[self registerSelf];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil];
		int menuTag = [self menuTag];
		if ( menuTag ) {
			fMenuItem = [NSApp searchForMenuItemWithTag:menuTag];
			[fMenuItem setTarget:self];
			[fMenuItem setAction:@selector(toggleShowHide:)];
			[fMenuItem setTitle:[NSString stringWithFormat:@"Show %@",[self menuName]]];
		}
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:nil];
    [super dealloc];
}

- (void)registerSelf {
	BOOL visible = [[NSUserDefaults standardUserDefaults] boolForKey:[[self class] visibleDefaultName]];
	[[MIPaletteController sharedPaletteController] registerPaletteController:self visibleAfterLaunch:visible];
}

- (void)windowDidLoad {
    fWindowJustLoaded = YES;
}

- (NSWindow*)window {
	NSWindow *window = [super window];
	if ( fWindowJustLoaded ) {
		fWindowJustLoaded = NO;
		[window setFrameUsingName:[self nibName]];
		//NSLog(@"%@ loading frame : %@", self, NSStringFromRect([window frame]));
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:window];
		[self windowWasLoaded];
	}
	return window;
}

- (void)windowWasLoaded {
}

- (void)updateMenu:(NSString*)visibility {
	if ( fMenuItem ) {
		[fMenuItem setTitle:[NSString stringWithFormat:@"%@ %@",visibility, [self menuName]]];
	}
}

- (IBAction)show:(id)sender {
    [[self window] makeKeyAndOrderFront:self];
	[self updateMenu:@"Hide"];
}

- (IBAction)hide:(id)sender {
    if ( [self isWindowLoaded] ) {
        [[self window] orderOut:self];
    }
	[self updateMenu:@"Show"];
}

- (void)windowWillClose:(NSNotification*)notification {
	[self updateMenu:@"Show"];
}

- (IBAction)toggleShowHide:(id)sender {
    if ( [self isVisible] ) [self hide:sender];
    else [self show:sender];
}

- (BOOL)isVisible {
    if ( [self isWindowLoaded] ) {
        return [[self window] isVisible];
    } else return NO;
}

- (void)applicationWillTerminate:(NSNotification*)theNotification {
	vernl(6,"[MIMontagePalette applicationWillTerminate]");
    //[self release];
}

- (void)saveUserDefaults:(BOOL)shouldBeVisible {
    if ( [self isWindowLoaded] ) {
        [[NSUserDefaults standardUserDefaults] setBool:shouldBeVisible forKey:[[self class] visibleDefaultName]];
        [[self window] saveFrameUsingName:[self nibName]];
		//NSLog(@"%@ saving frame : %@", self, NSStringFromRect([[self window] frame]));
    }	
}

- (BOOL)validateMenuItem:(NSMenuItem *)anItem {
	if ( [[NSDocumentController sharedDocumentController] currentDocument] != NULL ) return YES;
	else return NO;
}



@end