//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// material.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 "include/dlgutils.h"
#include "include/material.h"
#include "include/matlist.h"
#include "include/preferences.h"
#include "config.h"

app_objs *MaterialItem::app_ref = NULL;

//**************************************
// Constructeur
//**************************************
Material::Material( app_objs *appref ) : MaterialItem( appref )
{
PREF_DEF
clist = NULL;
current = NULL;
item_type = TV_MITEM_MATERIAL;
author= new TvWidget_entry( N_("Author"), "AUTHOR", NULL, app_ref );
author->set( pref->author->value() );
comment= new TvWidget_text( N_("Comment"), "COMMENT", NULL, app_ref );
}

//**************************************
// Set name
//**************************************
char *def_name = "Material#1";

void Material::set_name( char *nm )
{
TEXLIST_DEF
if ( nm == NULL ) nm = def_name;
if ( strlen(nm) < 1 ) nm = def_name;
char *test = new char[ strlen(nm) + 1 ];
strcpy( test, nm );

char *res = texlist->create_name( test );
name->store();
if ( res != NULL ) 
	{
	name->set(res);
	delete res;
	}
else name->set(test);
delete test;
}

//**************************************
// Add to list
//**************************************
void Material::add_to_list( GtkWidget *list, int position )
{
clist = GTK_CLIST( list );
gchar *titre[] = { name->value() };
int clist_row = 0;
if ( position == -1 ) clist_row = gtk_clist_append( clist, titre );
else clist_row = gtk_clist_insert( clist, position, titre );
gtk_clist_set_row_data( clist, clist_row, (gpointer)this );
}

//**************************************
// Destroy
//**************************************
Material::~Material()
{
delete author;
delete comment;
}

void Material::get_underscore_name( ofstream & file )
{
char *nom = name->value();
//file << nom;
int len = strlen( nom );
for ( int i = 0 ; i < len ; i++ )
	file << (( nom[i] == ' ' || nom[i] == '#' || nom[i] == '-' || nom[i] == '+' || nom[i] == '=' ) ? '_' : nom[i]);
}

//**************************************
// Editor
//**************************************
void Material::get_editor(  GtkWidget *tree, GtkWidget *box )
{
box_tree = GTK_CTREE(tree);
box_editor = box;
current = NULL;
add_to_tree( box_tree, NULL, NULL );
}

void Material::destroy_editor()
{
if ( current != NULL ) current->destroy_widget();
MaterialItem::remove_from_tree();
}

// MATERIAL SELECTED
void Material::mat_tree_select( GtkCTreeNode *node )
{
MaterialItem *sel = (MaterialItem*)(gtk_ctree_node_get_row_data( box_tree, node ));
if ( current != NULL )
	{
	current->flush();
	current->destroy_widget();
	}
current = sel;
current->edit_widget( box_editor );
}


// Name changed
void Material::name_changed()
{
if ( name->in_update ) return;
TEXLIST_DEF
set_name( name->get_current_value() );
gtk_ctree_node_set_text( ctree, ctree_node, 0, name->value() );
texlist->rename_list_row( this, name->value() );
}


// Delete item
void Material::delete_mat_item( GtkCTreeNode *node )
{
//MaterialItem *sel = (MaterialItem*)(gtk_ctree_node_get_row_data( box_tree, node ));
if ( current != NULL ) current->delete_yourself();
}

// Move item
void Material::move_mat_item( GtkCTreeNode *node, int sens )
{
//MaterialItem *sel = (MaterialItem*)(gtk_ctree_node_get_row_data( box_tree, node ));
if ( current != NULL ) current->move_yourself( sens );
}




//**************************************
//
// Material Item
// 
//**************************************
MaterialItem::MaterialItem( app_objs *appref )
{
app_ref = appref;
ctree = NULL;
ctree_node = NULL;
widget = NULL;
edit_cont = NULL;
name = new TvWidget_entry( N_("Name"), "NAME", NULL, app_ref );
expand = new TvWidget_bool( NULL, "EXP", NULL, app_ref, true );
}


MaterialItem::MaterialItem( MaterialItem & ref )
{
app_ref = ref.app_ref;
ctree = NULL;
ctree_node = NULL;
widget = NULL;
edit_cont = NULL;
name = new TvWidget_entry( *ref.name );
expand = new TvWidget_bool( *ref.expand );
item_type = ref.item_type;
}

void MaterialItem::add_to_tree( GtkCTree *tree, GtkCTreeNode *node , GtkCTreeNode *sibling = NULL )
{
ctree = tree;
parent_node = node;
char *titre[] = { name->value() };
ctree_node = gtk_ctree_insert_node( ctree, node, sibling, titre, 10, NULL, NULL, NULL, NULL, FALSE, FALSE );
gtk_ctree_node_set_row_data( ctree, ctree_node, this );
if ( expand->value() == true ) gtk_ctree_expand( ctree, ctree_node );
else gtk_ctree_collapse( ctree, ctree_node );
}

void MaterialItem::save_node_state()
{
if ( ctree == NULL || ctree_node == NULL ) return;
gchar *text;
guint8 spacing;
GdkPixmap *pc, *po;
GdkBitmap *mc, *mo;
gboolean isleaf, isexp;
gtk_ctree_get_node_info( GTK_CTREE(ctree), ctree_node, &text, &spacing, &pc, &mc, &po, &mo, &isleaf, &isexp );
expand->set( isexp );
}

void MaterialItem::edit_widget( GtkWidget *box, char *type )
{
widget = gtk_vbox_new( FALSE, 2 );
gtk_widget_set_usize( widget, 250, 400 );
gtk_box_pack_start( GTK_BOX(box), widget, TRUE, TRUE, 2 );
GtkWidget *frame = gtk_frame_new(NULL);
gtk_box_pack_start( GTK_BOX(widget), frame, FALSE, TRUE, 0 );
GtkWidget *label = gtk_label_new( type );
gtk_container_add( GTK_CONTAINER(frame), label );

GtkWidget *scrolled = gtk_scrolled_window_new( NULL, NULL );
gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
gtk_box_pack_start( GTK_BOX(widget), scrolled, TRUE, TRUE, 4 );
GtkWidget *viewp = gtk_viewport_new( NULL, NULL );
gtk_viewport_set_shadow_type( GTK_VIEWPORT(viewp), /*GTK_SHADOW_ETCHED_IN*/ GTK_SHADOW_NONE );
gtk_container_add( GTK_CONTAINER(scrolled), viewp );
edit_cont = gtk_vbox_new( FALSE, 0 );
gtk_container_set_border_width( GTK_CONTAINER(edit_cont), 1 );
gtk_container_add( GTK_CONTAINER(viewp), edit_cont );

name->get_widget( edit_cont, true );
name->connect_signal_changed( GTK_SIGNAL_FUNC(sign_miname_changed), this );
gtk_widget_show_all( widget );
}

void MaterialItem::set_name( char *nom )
{
name->set( nom );
}


void MaterialItem::name_changed()
{
if ( name->in_update ) return;
name->flush();
gtk_ctree_node_set_text( ctree, ctree_node, 0, name->value() );
}


void MaterialItem::paste_basics( MaterialItem *item )
{
name->copy( item->name );
reset_name_in_tree( name->value() );
}
