//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// interface.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 <iostream>
#include "config.h"
#include "include/interface.h"
#include "include/preferences.h"
#include "include/scene.h"
#include "include/objectlist.h"
#include "include/povfe.h"
#include "include/tvio.h"
#include "include/undo.h"


//***************************************************
// Constructeur
//***************************************************
Interface::Interface( app_objs *appref )
{
// App gnome
app_ref = appref;
app_ref->interf = (app_object*)this;

win = gnome_app_new( PACKAGE, APP_NAME );
gtk_signal_connect( GTK_OBJECT(win), "delete_event", GTK_SIGNAL_FUNC(app_exit), NULL );
vbox = gtk_hbox_new( FALSE, 1 );
gnome_app_set_contents( GNOME_APP(win), vbox );
gtk_container_set_border_width( GTK_CONTAINER(win), 1 );
gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );

gchar *fname = gnome_program_locate_file( app_ref->app, GNOME_FILE_DOMAIN_PIXMAP, "Truevision/truevision.png", FALSE, NULL );
GdkPixbuf *buffer = gdk_pixbuf_new_from_file( fname, NULL );
gtk_window_set_icon( GTK_WINDOW(win), buffer );
g_free( fname );	
	
accels = gtk_accel_group_new();
gtk_window_add_accel_group( GTK_WINDOW(win), accels );
gtk_widget_realize( win );

SCENE_DEF
scene->set_win_title();

// Préférences
PREF_DEF
if ( pref->save_geo->value() )
	{
	if ( pref->win_xpos->value() < 0 ) pref->win_xpos->set(0);
	if ( pref->win_ypos->value() < 0 ) pref->win_ypos->set(0);	
	gtk_widget_set_uposition( win, pref->win_xpos->value(), pref->win_ypos->value() );
	gtk_window_resize( GTK_WINDOW(win), pref->win_xsize->value(), pref->win_ysize->value() );
	}
	
// Vue 3d
vmanager = new ViewManager( app_ref, vbox );
	
//cout << "\nView manager created !";  cout.flush();	
// Side bar
GtkWidget *hbox = gtk_vbox_new( FALSE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), hbox, FALSE, FALSE, 0 );
	// PropertiesEditor
	panel = new PropPanel( app_ref, hbox );
	
	// App bar
	appbar = gnome_appbar_new( TRUE, TRUE, GNOME_PREFERENCES_NEVER );
	gtk_box_pack_start( GTK_BOX(hbox), appbar, FALSE, TRUE, 0 );

	
// Toolbar
toolbar = new ToolBar( app_ref, win );

// MENUS
// Export
static GnomeUIInfo export_menu[] = {
	{ GNOME_APP_UI_ITEM, N_("To povray..."), NULL, (void*)sign_export_to_povray, app_ref->scene, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN, 0, (GdkModifierType)0, NULL },
	GNOMEUIINFO_END
};

//OBJLIST_DEF
// Menu FILE
static GnomeUIInfo file_menu[] = { 
	GNOMEUIINFO_MENU_NEW_ITEM( N_("New Scene"), NULL, sign_scene_new, app_ref->scene ),
	GNOMEUIINFO_MENU_OPEN_ITEM(  sign_scene_load, app_ref->scene ),	
	GNOMEUIINFO_MENU_SAVE_ITEM(sign_scene_save, app_ref->scene ),
	GNOMEUIINFO_MENU_SAVE_AS_ITEM(sign_scene_save_as, app_ref->scene ),	
	{ GNOME_APP_UI_SUBTREE, N_("Export..."), NULL, export_menu, NULL, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_CONVERT, 0, (GdkModifierType)0, NULL },
	GNOMEUIINFO_SEPARATOR,
	{ GNOME_APP_UI_ITEM, N_("Load object..."), NULL, (void*)sign_objlist_load_object, app_ref->obj3dlist, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN, 0, (GdkModifierType)0, NULL },
	{ GNOME_APP_UI_ITEM, N_("Save object..."), NULL,  (void*)sign_objlist_save_object, app_ref->obj3dlist, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE_AS, 0, (GdkModifierType)0, NULL },
	GNOMEUIINFO_SEPARATOR,
	GNOMEUIINFO_MENU_EXIT_ITEM(app_exit, NULL), 
	GNOMEUIINFO_END
	};
// Menu EDIT
static GnomeUIInfo edit_menu[] = {
	GNOMEUIINFO_MENU_UNDO_ITEM( (void*)sign_undoman_undo, app_ref->undoman ),
	GNOMEUIINFO_MENU_REDO_ITEM( (void*)sign_undoman_redo, app_ref->undoman ),
	//{ GNOME_APP_UI_ITEM, N_("Undo"), NULL, (void*)sign_undoman_undo, app_ref->undoman, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_UNDO, 0, (GdkModifierType)0, NULL },	
	//{ GNOME_APP_UI_ITEM, N_("Redo"), NULL, (void*)sign_undoman_redo, app_ref->undoman, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_REDO, 0, (GdkModifierType)0, NULL },	
	//GNOMEUIINFO_SEPARATOR,
	//{ GNOME_APP_UI_ITEM, N_("Duplicate object"), NULL, about, NULL, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_COPY, 0, (GdkModifierType)0, NULL },
	//{ GNOME_APP_UI_ITEM, N_("Delete object"), NULL, (void*)sign_delete_current, app_ref->obj3dlist, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_CLOSE, 0, (GdkModifierType)0, NULL },
	GNOMEUIINFO_SEPARATOR,
	GNOMEUIINFO_MENU_PREFERENCES_ITEM(pref_dlg, pref ),	
	{ GNOME_APP_UI_ITEM, N_("Render options"), NULL, (void*)sign_options_box, app_ref->povfe, NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_PROP, 0, (GdkModifierType)0, NULL },	
	GNOMEUIINFO_END
	};
// Menu HELP
static GnomeUIInfo help_menu[] = { 
	GNOMEUIINFO_MENU_ABOUT_ITEM(about, NULL ),
	GNOMEUIINFO_END 
	};
// Barre de Menus
static GnomeUIInfo main_menu[] = { 
	GNOMEUIINFO_MENU_FILE_TREE(file_menu),
	GNOMEUIINFO_MENU_EDIT_TREE(edit_menu),	
	GNOMEUIINFO_MENU_HELP_TREE(help_menu),
	GNOMEUIINFO_END 
	};
gnome_app_create_menus( GNOME_APP(win), main_menu );

gtk_widget_show_all( win );
//if ( pref->save_geo->value() ) 
	//gdk_window_resize( win->window, (gint)(pref->win_xsize->value()), (gint)(pref->win_ysize->value()) );
	//cout << "\nSize = " << pref->win_xsize->value() << " x " << pref->win_ysize->value();
//else gdk_window_resize( win->window, 500, 500 );

UNDO_DEF
undoman->set_undo_menu_widgets(  edit_menu[0].widget,  edit_menu[1].widget );
scene->set_save_menu_widget( file_menu[2].widget );
gtk_widget_set_sensitive(  file_menu[2].widget, FALSE );
vmanager->set_pointer_mode( TV_PMODE_TRACKBALL );

}



//*******************************************************
// Préférences
//*******************************************************
void Interface::flush_pref()
{
PREF_DEF
gint x,y;
gdk_window_get_root_origin( win->window, &x, &y );
pref->win_xpos->set( x );
pref->win_ypos->set( y );
pref->win_xsize->set( win->allocation.width );
pref->win_ysize->set( win->allocation.height );
}



//********************************************************
// Save
//********************************************************
void Interface::save( ofstream & file )
{
file << "\nINTERFACE{";
vmanager->save( file );
file << "\n}";
}


void Interface::clear()
{
vmanager->clear();
}


bool Interface::load( ifstream & file, char * ltag )
{
if ( strcmp( ltag, "INTERFACE" ) ) return false;

char * tag = NULL;
do {
	tag = tvio_get_next_tag( file );
	if ( tag == NULL ) break;
	if ( vmanager->load( file, tag ) ) continue;
	tvio_skip_section(file );
	} while ( tag != NULL );
return true;
}
