/****************************************************************************
 *   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.              *
 ****************************************************************************/

#ifndef MIProgressBarDlgManager_h
#include "MIProgressBarDlgManager.h"
#endif //MIProgressBarDlgManager_h

#import "MIProgressBarDlgCtrller.h"
//#import "MIApplication.h"
//#import "MIMainViewController.h"

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

#ifndef ZzException_h
#include "ZzException.h"
#endif //ZzException_h

id MIProgressBarDlgManager::fsProgressBarDlgCtrller = nil;

MIProgressBarDlgManager::MIProgressBarDlgManager(const char* title, bool disableCancelButton)
{
	fHasBeenDeleted = false;
	fHasBeenDisplayed = false;
	fTitle = [[NSString alloc] initWithCString:title];
	fDoNotDisplay = false;
	fSession = NULL;
	fDisableCancelButton = disableCancelButton;
}

MIProgressBarDlgManager::~MIProgressBarDlgManager()
{
	[fTitle release];
	Delete();
}

void MIProgressBarDlgManager::SetNbSubTasks(SInt32 nbSubTasks)
{
	if (nbSubTasks > 0)
		fLogic.SetNbSubTasks(nbSubTasks);
}

void MIProgressBarDlgManager::Hide()
{
	fDoNotDisplay = true;
}

void MIProgressBarDlgManager::EnterSubTask()
{
	fDoNotDisplay = false;
	if(!fDoNotDisplay && !fHasBeenDisplayed)
		DisplayDlg();
	
	fLogic.EnterSubTask();
}

void MIProgressBarDlgManager::LeaveSubTask()
{
	Update(1.0); // this will only change the actual value if the node is leaf
	fLogic.LeaveSubTask();
}

void MIProgressBarDlgManager::DisplayDlg()
{
	fKeyWindow = [NSApp keyWindow];
	if(!fsProgressBarDlgCtrller)
	{
		fsProgressBarDlgCtrller = [MIProgressBarDlgCtrller alloc];
		[fsProgressBarDlgCtrller initWithWindowNibName:@"MINewProgressBarDlg" owner:fsProgressBarDlgCtrller];
	}
	
	fHasBeenDisplayed = true;
	
	fDlgWindow = [fsProgressBarDlgCtrller window];
	[fsProgressBarDlgCtrller setProgress:0.0];
	[fsProgressBarDlgCtrller setTitle:fTitle];
	if(fDisableCancelButton)
		[fsProgressBarDlgCtrller setCancelButtonEnabled:NO];	
	else
		[fsProgressBarDlgCtrller setCancelButtonEnabled:YES];	
	
	fSession = [NSApp beginModalSessionForWindow:fDlgWindow];
}

void MIProgressBarDlgManager::Update(float progress)
{
	if(!fDoNotDisplay && !fHasBeenDisplayed)
		DisplayDlg();

	fLogic.SetProgress(progress);	
	
	if(fSession)
	{
		[fsProgressBarDlgCtrller setProgress:fLogic.GetWholeProgress()];
		int retCode = [NSApp runModalSession:fSession];
		if(retCode == NSRunContinuesResponse)
		{
		}
		else if(retCode == 0) // cancel was pressed
		{
			VUSERCMD("Cancelled");
			Delete();
			ZZ_THROW(ZzExceptionCancel);
		}
		else
		{
			ZZ_THROW(ZzException); // that's another exception
		}
	}
}

void MIProgressBarDlgManager::Delete()
{
	if (!fHasBeenDeleted && fHasBeenDisplayed)
	{
		[NSApp endModalSession:fSession];
		fSession = nil;
		[fDlgWindow orderOut:nil];
		fHasBeenDeleted = true;
		//Bug in case of a dialog : [[[NSApp mainViewController] window] makeKeyAndOrderFront:nil];
		[((id)fKeyWindow) makeKeyWindow];
	}
}

	