//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// main.cc
//
// Vincent LE PRINCE <vincentleprince@users.sourceforge.net>
// Copyright (C) 2000-2001 Vincent LE PRINCE
// This file is part of the TRUEVISION Package

//   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., 675 Mass Ave, Cambridge, MA 02139, USA.  */ 
//*******************************************************************************************
//#include "main.h"
#include "include/interface.h"
#include "include/preferences.h"
#include "include/scene.h"
#include "include/povfe.h"
#include "include/matlib.h"
#include "include/undo.h"
#include "include/dlgutils.h"
#include "config.h"

 
//*****************************************************
// Objets globaux
//*****************************************************
app_objs obj_ref;
Interface *Panel;
Preferences *Pref;
Scene *scene;
PovrayFE *povfe;
MatLib *matlib;
UndoRedoManager *undoman;

//*****************************************************
// Fonction Main
//*****************************************************
int main( int argc, char * argv[], char *envp[] )
{
obj_ref.argc = argc;
obj_ref.argv = argv;
obj_ref.envp = envp;
// Mise à zéro des objets
obj_ref.interf = NULL;
obj_ref.vmanager = NULL;
obj_ref.scene = NULL;

// Internationalisation
#ifdef ENABLE_NLS
bindtextdomain( PACKAGE, PACKAGE_LOCALE_DIR );
textdomain( PACKAGE );
#endif

// Init
g_thread_init(NULL);
gdk_threads_init();
//obj_ref.app = gnome_program_init( PACKAGE, VERSION, LIBGNOMEUI_MODULE, argc, argv, GNOME_PROGRAM_STANDARD_PROPERTIES, NULL );
obj_ref.app = gnome_program_init( PACKAGE, VERSION, LIBGNOMEUI_MODULE, argc, argv,  NULL );	
setlocale( LC_NUMERIC, "C" );
//g_thread_init(NULL);
	
gtk_gl_init( &argc, &argv );

// Splash screen
SplashScreen *splash = new SplashScreen( obj_ref.app );	
	splash->set_status( _("Loading preferences...") );
	Pref = new Preferences( &obj_ref );
	splash->set_status( _("Loading material library...") );
	matlib = new MatLib( &obj_ref );
	splash->set_status( _("Loading interface...") );
	undoman = new UndoRedoManager( &obj_ref );
	povfe = new PovrayFE( &obj_ref );
	obj_ref.povfe = povfe;
	scene = new Scene( &obj_ref );
	Panel = new Interface( &obj_ref );
	undoman->init();
sleep(1);
delete splash;

// Reinit
//gtk_init( &argc, &argv );

// Boucle gtk
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return(-1);
}



//******************************************************
// Fonction app_exit()
//******************************************************
void app_exit() 
{
if ( scene->is_modified() )
	{
	GtkWidget *dialog =  gtk_message_dialog_new ( GTK_WINDOW(Panel->get_gtkwin()), GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_YES_NO, _("Current scene is unsaved, quit anyway ?"));	
	gint button = gtk_dialog_run( GTK_DIALOG(dialog));
	gtk_widget_destroy( dialog );
	if ( button == GTK_RESPONSE_NO ) return;
	}
scene->clear_scene();
Pref->save();
gtk_main_quit();
delete obj_ref.texlist;
exit(-1);
}

void app_fatal_err( const char *message )
{
cout << "\n[TrueVision]: Fatal error -> " << message << endl;
exit(-1);
}

//*******************************************************
// Message boxes
//*******************************************************
GtkWidget *warning_box = NULL;
GdkPixmap *warning_pix = NULL;
GdkPixmap *warning_mask = NULL;

void sign_warning_close( GtkWidget *wid, gpointer data )
{
if ( warning_box == NULL ) return;
gtk_widget_destroy( warning_box );
warning_box = NULL;
}
inline void sign_warning_destroy( GtkWidget *wid, GdkEvent *ev, gpointer data ) { sign_warning_close( wid, data ); }

void app_warning( const char *message )
{
warning_box =  gtk_message_dialog_new ( NULL, GTK_DIALOG_MODAL , GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, message);
gtk_widget_show_all( warning_box );
g_signal_connect (GTK_OBJECT (warning_box), "response", G_CALLBACK (sign_warning_destroy), GTK_OBJECT (warning_box));
}

/*void app_warning( const char *message )
{
if ( warning_box != NULL ) gtk_widget_destroy( warning_box );
GdkColor transparent;
if ( warning_pix == NULL )
	{
	gchar *pixmap = gnome_pixmap_file( "truevision/warning.xpm" );
	warning_pix = gdk_pixmap_create_from_xpm( Panel->get_gdkwin(), &warning_mask, &transparent, pixmap );
	g_free( pixmap );
	}
	
warning_box = gtk_window_new( GTK_WINDOW_POPUP );
gtk_window_set_title( GTK_WINDOW(warning_box), _("Truevision : warning !") );
gtk_window_set_wmclass( GTK_WINDOW(warning_box), PACKAGE, "Warningl" );
gtk_window_set_policy( GTK_WINDOW(warning_box), FALSE, FALSE, TRUE );
gtk_window_set_position( GTK_WINDOW(warning_box),  GTK_WIN_POS_CENTER );
gtk_signal_connect( GTK_OBJECT(warning_box), "delete_event", GTK_SIGNAL_FUNC(sign_warning_close), NULL );

GtkWidget *vbox = gtk_vbox_new( FALSE, 0 );
gtk_container_set_border_width( GTK_CONTAINER(vbox), 7 );
gtk_container_add( GTK_CONTAINER(warning_box), vbox );
GtkWidget *hbox = gtk_hbox_new( FALSE, 5 );
gtk_container_set_border_width( GTK_CONTAINER(hbox), 7 );
gtk_box_pack_start( GTK_BOX(vbox), hbox, TRUE, TRUE, 0 );

GtkWidget *Pix =gtk_pixmap_new( warning_pix, warning_mask );
gtk_box_pack_start( GTK_BOX(hbox), Pix, TRUE, TRUE, 0 );

GtkWidget *lab = gtk_label_new( message );
gtk_box_pack_start( GTK_BOX(hbox), lab, TRUE, TRUE, 0 );

GtkWidget *sep = gtk_hseparator_new();
gtk_box_pack_start( GTK_BOX(vbox), sep, TRUE, TRUE, 8 );

GtkWidget *button_box = gtk_hbutton_box_new();
gtk_button_box_set_layout( GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_SPREAD );
gtk_button_box_set_spacing( GTK_BUTTON_BOX(button_box), 10 );
gtk_box_pack_start( GTK_BOX(vbox), button_box, FALSE, FALSE, 8 );

	GtkWidget *button = gtk_button_new_with_label( _("OK") );
	gtk_container_add( GTK_CONTAINER(button_box), button );
	gtk_signal_connect( GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(sign_warning_close), NULL );

gtk_widget_show_all( warning_box );
}*/

/*void app_warning( const char *message )
{
GtkWidget *dlg = gnome_message_box_new( message, GNOME_MESSAGE_BOX_WARNING, GNOME_STOCK_BUTTON_OK, NULL );
gnome_dialog_run_and_close( GNOME_DIALOG(dlg) );
} */

void app_warning( const char *mess1, const char *mess2 )
{
char *message = new char[ strlen(mess1) + strlen(mess2) + 1 ];
if ( message == NULL ) return;
strcpy( message, mess1 );
strcat( message, mess2 );
app_warning( message );
delete message;
}


//********************************************************
// About Box
//********************************************************
GtkWidget *dlg = NULL;;
GdkPixbuf *buffer = NULL;

void close_about( GtkWidget *widget, gpointer data ) 
{ 
gtk_widget_destroy(dlg); dlg = NULL;
if ( buffer != NULL ) g_object_unref( buffer );
buffer = NULL;
}

void about()
{
if ( GTK_IS_DIALOG(dlg) ) return;
const gchar *authors[] = APP_AUTHORS;
char *fname = tv_get_pixmap( "splash.png" );
buffer = gdk_pixbuf_new_from_file( fname, NULL );
dlg = gnome_about_new( APP_NAME, VERSION, APP_COPY, APP_DESCR, (const gchar**)authors, NULL, NULL, buffer );
g_signal_connect( G_OBJECT(dlg), "close", G_CALLBACK(close_about), NULL );
g_signal_connect( G_OBJECT(dlg), "response", G_CALLBACK(close_about), NULL );
delete fname;
gtk_dialog_run( GTK_DIALOG(dlg) );
}
