//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// material.h
//
// 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.  */ 
//*******************************************************************************************
#ifndef TV_TEXTURE_H
#define TV_TEXTURE_H
using namespace std;
#include "main.h"
#include <iostream>
#include <fstream>
#include "tvwidgets.h"

//**********************************************************
// Material item types
//**********************************************************
enum matit_type {
	TV_MITEM_MATERIAL = 0,
	TV_MITEM_TEXTURE,
	TV_MITEM_INTERIOR,
	TV_MITEM_FINISH,
	TV_MITEM_PIGMENT,
	TV_MITEM_NORMAL,
	TV_MITEM_MEDIA,
	TV_MITEM_DENSITY,
};
typedef matit_type MaterialItemType;


//****************************************************
// Classe Item de Texture
//****************************************************
class MaterialItem {
	#define SENDER ((MaterialItem*)data)
	friend void sign_miname_changed( GtkWidget *wid, gpointer data ) { SENDER->name_changed(); }
	#undef SENDER

	protected:
		static app_objs *app_ref;
		MaterialItemType item_type;
		GtkCTreeNode *ctree_node, *parent_node;
		GtkCTree *ctree;
		GtkWidget *widget;
		GtkWidget *edit_cont;
		TvWidget_entry *name;
		TvWidget_bool *expand;

	public:
		MaterialItem( app_objs *appref );
		MaterialItem( MaterialItem & ref );
		virtual MaterialItem *duplicate_yourself() { MaterialItem *res = new MaterialItem( *this ); return res; }
		
		char *get_name() { return name->value(); }
		MaterialItemType get_type() { return item_type; }
		char *get_previous_name() { return name->get_previous_value(); }
		virtual void set_name( char *nom );
		virtual void name_changed();
		
		virtual void add_to_tree( GtkCTree *tree, GtkCTreeNode *node, GtkCTreeNode *sibling  );
		void reset_name_in_tree( char *iname ) { gtk_ctree_node_set_text( ctree, ctree_node, 0, iname ); }
		virtual void remove_from_tree() { if ( ctree_node != NULL ) gtk_ctree_remove_node( ctree, ctree_node ); ctree_node = NULL; }
		void clear_tree_node() { ctree_node = NULL; }

		virtual void delete_yourself() {}
		virtual void move_yourself( int sens ) {}
		
		virtual void edit_widget( GtkWidget *box, char *type );
		virtual void edit_widget( GtkWidget *box ) {}
		virtual void destroy_widget() { if ( widget != NULL ) gtk_widget_destroy(widget); widget = NULL; edit_cont = NULL; }
		virtual void flush() { name->flush(); }
		virtual bool is_used() { return true; }
		virtual void output_to_povray( ofstream & file ) {}
		GtkCTreeNode *get_ctree_node() { return ctree_node; }
		virtual void save_node_state();

		virtual void save( ofstream &file ) {}
		virtual bool load( ifstream &file ) { return false; }
		virtual bool load( ifstream & file, char *tag ) { return false; }
		
		virtual bool paste( MaterialItem *item ) { return false; }
		void paste_basics( MaterialItem *item );
};



//****************************************************
// Material types
//****************************************************
enum mattypes {
	TV_MAT_POV,
	};
typedef mattypes MaterialType;


//****************************************************
// Classe Texture de base
//****************************************************
class Material : public MaterialItem {
	#define SENDER ((Material*)data)
	friend void sign_name_focusout( GtkWidget *wid, GdkEvent *ev, gpointer data ) { SENDER->name_edition_finished(); }
	#undef SENDER

	protected:
		MaterialType type;
		GtkCList *clist;

		GtkCTree *box_tree;
		GtkWidget *box_editor;
		MaterialItem *current;
		void set_name( char *nm );
		void name_changed();
		void name_edition_finished() { if ( ! name->in_update ) name->update_widget();  }
		
	public:
		TvWidget_entry *author;
		TvWidget_text *comment;
	
	public:
		Material( app_objs *appref );
		virtual ~Material();
		char *get_name() { return name->value(); }
		MaterialType get_type() { return type; }
		virtual void add_to_list( GtkWidget *list , int position = -1 );
		virtual void get_editor( GtkWidget *tree, GtkWidget *box );
		void mat_tree_select( GtkCTreeNode *node );
		void delete_mat_item( GtkCTreeNode *node );
		void move_mat_item( GtkCTreeNode *node, int sens );
		MaterialItem *get_selected_item() { return current; }
		
		virtual void edit_widget( GtkWidget *box ) { MaterialItem::edit_widget( box, _("Material") ); name->connect_signal2( GTK_SIGNAL_FUNC(sign_name_focusout), this );  }
		virtual void destroy_widget() { MaterialItem::destroy_widget(); name->clear_widget();  }

		virtual void destroy_editor();
		virtual void flush_material() { if ( current != NULL ) current->flush(); }
		virtual void flush() {}
		virtual void output_to_povray( ofstream & file ) {}
		void get_underscore_name( ofstream & file );

		virtual void set_gl_material() {}
};

#endif
