//*****************************************************************************************
// Truevision - a 3d modeler for gnome and povray
//
// matlist.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.  */ 
//*******************************************************************************************
#ifndef TV_TEXLIST_H
#define TV_TEXLIST_H
using namespace std;
#include <vector>
#include <iostream>
#include "main.h"
#include "matpov.h"
#include "objparam.h"
#include "povfe.h"

#define MAX_TEX_BUTTONS 6
#define MAX_EDIT_BUTTONS 6
#define TEXLIST_DEF TexList *texlist = (TexList*)(app_ref->texlist);

//**************************************
// Classe
//**************************************
class TexList : app_object {
	#define SENDER ((TexList*)data)
	friend void sign_tex_select( GtkCList *list, gint row, gint col, GdkEvent *ev, gpointer data ) { SENDER->select(row); }
	friend void sign_tex_buttons_clicked( GtkWidget *wid, gpointer data ) { SENDER->tex_buttons_clicked( wid ); }

	friend void sign_ed_dlg_click( GtkDialog *wid, gint button, gpointer data ) { SENDER->clicked_ed_dlg(button); }
	friend void sign_ed_dlg_destroy( GtkWidget *wid, GdkEvent *ev, gpointer data ) { SENDER->clicked_ed_dlg(-1); }		
	friend void sign_ed_tree_select( GtkCTree *tree, GtkCTreeNode *node, gint col, gpointer data ) { SENDER->ed_tree_select(tree, node, col); }		
	friend void sign_ed_dlg_select( GtkWidget *wid, gpointer data ) { SENDER->ed_dlg_select(); }

	friend void sign_edit_button_clicked( GtkWidget *wid, gpointer data ) { SENDER->edit_button_clicked( wid ); }

	friend void sign_mat_del_filebox( GtkWidget *wid, gpointer data ) { gtk_widget_destroy( SENDER->filebox ); }	
	friend void sign_mat_save( GtkWidget *wid, gpointer data ) { SENDER->mat_save(); }	
	friend void sign_mat_load_with_fbox( GtkWidget *wid, gpointer data ) { SENDER->mat_load_with_fbox(); }

	friend void sign_save_dlg_click( GtkWidget *wid, gint button, gpointer data ) { SENDER->clicked_save_dlg(button); }
	friend void sign_save_dlg_destroy( GtkWidget *wid, GdkEvent *ev, gpointer data ) { SENDER->clicked_save_dlg(-1); }
	
	#undef SENDER

	private:
		app_objs *app_ref;
		
		// Liste des materiaux
		vector<Material*> mat_liste;
		char **mat_name_list;
		int mat_name_list_size;
		gint current;

		//  Liste des référenceurs
		vector<ObjParam_texref> ref_liste;
		ObjParam_texref *current_ref;

		// Editor
		GtkWidget *ed_dialog, *edit_box, *save_dialog;
		GtkWidget *edit_butt_wid[MAX_EDIT_BUTTONS];
		GtkTooltips *edit_butt_tt_wid[MAX_EDIT_BUTTONS];

		PovrayFE *preview;
		bool ed_applied;
		int edited;
		GtkCTreeNode *selected_node;
		Material *edited_pointer;
		bool in_change;
		void raise_texeditor();
		TvWidget_option_combo *texed_select;
		void clicked_ed_dlg( gint button );
		void close_ed_dlg();
		void update_texeditor();
		GtkWidget *ed_tree;
		void ed_tree_select( GtkCTree *tree, GtkCTreeNode *node, gint col ) { selected_node = node; if ( edited != -1 ) mat_liste[edited]->mat_tree_select( node ); }
		void ed_dlg_select();
		MaterialItem *copied_item;
		void edit_button_clicked( GtkWidget *src );
		
		// Select list
		GtkWidget *select_widget;
		GtkWidget *select_list;
		GtkWidget *select_buttons[MAX_TEX_BUTTONS];
		GtkWidget *filebox;
		GtkTooltips *select_tooltips[MAX_TEX_BUTTONS];

		bool test_name_exist( char *name );
		void tex_buttons_clicked( GtkWidget *wid );
		void select( gint row ) { current = row; }
		void update_mat_name_list();

		// Sauvegarde
		PovrayFE *save_preview;
		TvWidget_path *file;
		TvWidget_entry *author;
		TvWidget_text *comment;
		TvWidget_bool_activator *use_preview;
		void mat_save();
		void mat_save_dlgbox();
		void close_save_dialog();
		void clicked_save_dlg( gint button );
		void mat_load_with_fbox();
		char *default_path;
		void store_default_path( char *path );
		vector<Material*> * load_list;
		
	public:
		TexList( app_objs *appref );
		~TexList();
		void pref_changed();
		void set_select_widget( GtkWidget *wid );
		int get_size() { return mat_liste.size(); }

		char *create_name( char *name );
		char **get_mat_name_list() { return mat_name_list; }
		int get_mat_name_list_size() { return mat_name_list_size; }
		void set_current_ref( ObjParam_texref *ref ) { current_ref = ref; }
		int get_index_by_pointer( Material *tex );
		Material *get_pointer_by_index( int index ) { return mat_liste[index]; }
		Material *get_pointer_by_name( char *str );
		void rename_list_row( Material *tex, char *name );

		void output_to_povray( ofstream & file );
		Material *get_edited_material() { return edited_pointer; }
		Material *get_selected_material() { return (Material*)gtk_clist_get_row_data( GTK_CLIST(select_list), current ); }
		
		void remove_material( Material *mat );
		void insert_material( Material *mat, int position );
		
		void mat_load_from_file( char *path );
		
		void save( ofstream & file );
		bool load( ifstream & file, char * tag );
		void clear();
		void set_load_list( vector<Material*> * mlist ) { load_list = mlist; }
       void flush_load_list();

       void select_matedit_tree_node( GtkCTreeNode *node ) { selected_node = node; mat_liste[edited]->mat_tree_select( node ); }

};

#endif
