/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */

/* AbiSource Application Framework
 * Copyright (C) 1998 AbiSource, Inc.
 * Copyright (c) 2003 Hubert Figuiere
 * Copyright (c) 2005 Francis James Franklin
 * 
 * This program 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 program 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 more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "ut_assert.h"
#include "ut_debugmsg.h"
#include "ut_string.h"

#include "xap_App.h"
#include "xap_CocoaDialog_Utilities.h"
#include "xap_CocoaDlg_Language.h"

/* *************************************************************** */

XAP_Dialog * XAP_CocoaDialog_Language::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
{
	XAP_CocoaDialog_Language * p = new XAP_CocoaDialog_Language(pFactory, dlgid);
	return p;
}

XAP_CocoaDialog_Language::XAP_CocoaDialog_Language(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
	XAP_Dialog_Language(pDlgFactory, dlgid),
	m_dlg(nil)
{
	// 
}

XAP_CocoaDialog_Language::~XAP_CocoaDialog_Language(void)
{
	// 
}

void XAP_CocoaDialog_Language::runModal(XAP_Frame * pFrame)
{
	m_dlg = [[XAP_CocoaDialog_Language_Controller alloc] initFromNib];

	[m_dlg setXAPOwner:this];

	[NSApp runModalForWindow:[m_dlg window]];

	int index = [m_dlg indexOfSelectedLanguage];

	if (index >= 0)
		{
			m_answer = XAP_Dialog_Language::a_OK;

			setMakeDocumentDefault([m_dlg applyToDocument] == YES);

			_setLanguage(m_ppLanguages[index]);

			m_bChangedLanguage = true;
		}
	else
		{
			m_answer = XAP_Dialog_Language::a_CANCEL;
		}

	[m_dlg close];
	[m_dlg release];

	m_dlg = nil;
}

/* **************************************************************************************** */

@implementation XAP_CocoaDialog_Language_Controller

- (id)initFromNib
{
	if (self = [super initWithWindowNibName:@"xap_CocoaDlg_Language"])
		{
			_xap = 0;

			m_Selection = 0;
			m_SelectionIndex = -1;

			m_bApplyToDocument = NO;

			m_Languages = [[NSMutableArray alloc] initWithCapacity:100];
			if (!m_Languages)
				{
					[self release];
					self = nil;
				}
		}
	return self;
}

- (void)dealloc
{
	if (m_Languages)
		{
			[m_Languages release];
			m_Languages = 0;
		}
	if (m_Selection)
		{
			[m_Selection release];
			m_Selection = 0;
		}
	[super dealloc];
}

- (void)setXAPOwner:(XAP_Dialog *)owner
{
	if (m_Selection)
		{
			[m_Selection release];
			m_Selection = 0;
		}
	m_SelectionIndex = -1;

	_xap = static_cast<XAP_CocoaDialog_Language *>(owner);
}

- (void)discardXAP
{
	if (m_Selection)
		{
			[m_Selection release];
			m_Selection = 0;
		}
	m_SelectionIndex = -1;

	_xap = 0;
}

- (void)windowDidLoad
{
	const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();

	LocalizeControl([self window],     pSS, XAP_STRING_ID_DLG_ULANG_LangTitle);

	LocalizeControl(oCancel,           pSS, XAP_STRING_ID_DLG_Cancel);
	LocalizeControl(oOK,               pSS, XAP_STRING_ID_DLG_OK);

	LocalizeControl(oDocumentDefault,  pSS, XAP_STRING_ID_DLG_ULANG_DefaultLangChkbox);

	if (_xap)
		{
			UT_UTF8String defaultLanguageString;
			_xap->getDocDefaultLangDescription(defaultLanguageString);
			[oDocumentCurrent setStringValue:[NSString stringWithUTF8String:(defaultLanguageString.utf8_str())]];

			const char * current_language = _xap->getCurrentLanguage();

			int current_index = -1;

			UT_uint32 count = _xap->getLanguageCount();

			for (UT_uint32 i = 0; i < count; i++)
				{
					const char * language = _xap->getNthLanguage(i);

					[m_Languages addObject:[NSString stringWithUTF8String:language]];

					if (current_language)
						if (UT_stricmp(current_language, language) == 0)
							{
								current_index = (int) i;
							}
				}
			[oLanguageTable reloadData];

			if (current_index >= 0)
				{
					[oLanguageTable selectRow:current_index byExtendingSelection:NO];
				}
		}
}

- (NSString *)selectedLanguage
{
	return m_Selection;
}

- (int)indexOfSelectedLanguage
{
	return m_SelectionIndex;
}

- (BOOL)applyToDocument
{
	return m_bApplyToDocument;
}

- (IBAction)aCancel:(id)sender
{
	if (m_Selection)
		{
			[m_Selection release];
			m_Selection = 0;
		}
	m_SelectionIndex = -1;

	[NSApp stopModal];
}

- (IBAction)aOK:(id)sender
{
	m_bApplyToDocument = ([oDocumentDefault state] == NSOnState) ? YES : NO;

	[NSApp stopModal];
}

- (IBAction)aLanguageTable:(id)sender
{
	if (m_Selection)
		{
			[m_Selection release];
			m_Selection = 0;
		}
	m_SelectionIndex = [oLanguageTable selectedRow];

	if (m_SelectionIndex >= 0)
		{
			if ((unsigned) m_SelectionIndex < [m_Languages count])
				{
					m_Selection = (NSString *) [m_Languages objectAtIndex:((unsigned) m_SelectionIndex)];
					[m_Selection retain];
				}
			else // huh?
				{
					m_SelectionIndex = -1;
				}
		}
}

/* NSTableViewDataSource methods
 */
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
	return (int) [m_Languages count];
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	return [m_Languages objectAtIndex:((unsigned) rowIndex)];
}

/* NSTableView delegate methods
 */
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
	// 
}

- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex
{
	return YES;
}

@end
