/*  SciGraphica - Scientific graphics and data manipulation
 *  Copyright (C) 2001 Adrian E. Feiguin <feiguin@ifir.edu.ar>
 *
 *  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 __SG_WORKSHEET_H__
#define __SG_WORKSHEET_H__

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <gtk/gtk.h>
#include <gtkextra/gtkextra.h>
#include "sg_widgets_enums.h"

#define GTK_TYPE_SG_WORKSHEET                  (sg_worksheet_get_type ())
#define SG_WORKSHEET(obj)                  (GTK_CHECK_CAST ((obj), GTK_TYPE_SG_WORKSHEET, SGworksheet))
#define SG_WORKSHEET_CLASS(klass)          (GTK_CHECK_CLASS_CAST ((klass), GTK_TYPE_SG_WORKSHEET, SGworksheetClass))
#define GTK_IS_SG_WORKSHEET(obj)               (GTK_CHECK_TYPE ((obj), GTK_TYPE_SG_WORKSHEET))
#define GTK_IS_SG_WORKSHEET_CLASS(klass)       (GTK_CHECK_CLASS_TYPE ((klass), GTK_TYPE_SG_WORKSHEET))

typedef struct _SGworksheet SGworksheet;
typedef struct _SGworksheetClass SGworksheetClass;
typedef struct _SGcolumn SGcolumn;
typedef struct _SGhiddencell SGhiddencell;
typedef union _SGcellvalue SGcellvalue;

union _SGcellvalue {
   glong val_long;
   gdouble val_double;
   gfloat val_float;
   gchar *val_char; /* No need for three different char * types */
};

struct _SGcolumn
{
  SGcolumntype type;
  SGcolumnformat format;
  SGcolumninternal internal;

  gint precision;

  gchar *exp;
};

struct _SGhiddencell
{
  gchar *formula;
  gboolean updated;

  SGcellvalue value;

  SGcolumntype type;
  SGcolumnformat format;
  SGcolumninternal internal;
  gint precision;
};


struct _SGworksheet 
{
  GtkSheet sheet;

  gchar *name, *cell_save;
  gint last_column;

  gint begin, end;

  gboolean is_mapped;
  gboolean is_frozen;

  SGcolumn *column;
  GObject *clipboard;
};

struct _SGworksheetClass
{
  GtkSheetClass parent_class;

  gboolean (*update_exp)	(SGworksheet *worksheet,
				 gint from_row, gint to_row,
				 gint from_col, gint to_col);
  gboolean (*update_cell_format)(SGworksheet *worksheet,
				 gint row, gint col); 
};

GtkType 	sg_worksheet_get_type		(void); 
SGworksheet 	*sg_worksheet_new		(const gchar *name, 
                                                 gint nrows, gint ncols);
gint 		sg_worksheet_rename		(SGworksheet *worksheet,
						 const gchar *name); 
void		sg_worksheet_set_begin		(SGworksheet *worksheet,
						 gint row); 
void		sg_worksheet_set_end		(SGworksheet *worksheet,
						 gint row); 
void		sg_worksheet_set_xrange		(SGworksheet *worksheet,
						 gdouble xmin, gdouble xmax);
void		sg_worksheet_set_yrange		(SGworksheet *worksheet,
						 gdouble ymin, gdouble ymax);
void		sg_worksheet_reset		(SGworksheet *worksheet);
void		sg_worksheet_remove		(SGworksheet *worksheet);
void		sg_worksheet_set_column_name	(SGworksheet *worksheet,
						 gint column,
						 const gchar *text);
void		sg_worksheet_add_columns	(SGworksheet *worksheet,
						 gint n);
void		sg_worksheet_add_rows		(SGworksheet *worksheet,
						 gint n);
void		sg_worksheet_insert_columns	(SGworksheet *worksheet,
						 gint col,
						 gint n);
void		sg_worksheet_insert_rows	(SGworksheet *worksheet,
						 gint row,
						 gint n);
void		sg_worksheet_delete_columns	(SGworksheet *worksheet,
						 gint col,
						 gint n);
void		sg_worksheet_delete_rows	(SGworksheet *worksheet,
						 gint row,
						 gint n);
gint 		sg_worksheet_get_column		(SGworksheet *worksheet,
						 const gchar *col_name);
void 		sg_worksheet_column_set_format	(SGworksheet *worksheet,
                               			 gint column, 
                               			 SGcolumntype type,
                               			 SGcolumnformat format,
                               			 SGcolumninternal internal,
                               			 gint precision);
void 		sg_worksheet_column_set_exp	(SGworksheet *worksheet,
                               			 gint column, 
                               			 const gchar *exp); 
void            sg_worksheet_cell_clear         (SGworksheet *worksheet, 
                                                 gint row, gint col);

void 		sg_worksheet_cell_set		(SGworksheet *worksheet,
						 gint row, gint col, 
                                                 const gchar *text,
                                                 gboolean formula,
                                                 gboolean eval);

const gchar *   sg_worksheet_cell_get_text     (SGworksheet *worksheet,
                                                 gint row, gint col);
void		sg_worksheet_cell_set_text 	(SGworksheet *worksheet, 
                                                 gint row, gint col,
						 const gchar *text);
const gchar *   sg_worksheet_cell_get_formula  (SGworksheet *worksheet,
                                                 gint row, gint col);
double 		sg_worksheet_cell_get_double    (SGworksheet *worksheet,
                                                 gint row, gint col, 
						 gboolean *error);
gint 		sg_worksheet_cell_get_int       (SGworksheet *worksheet,
                                                 gint row, gint col, 
						 gboolean *error);
void		sg_worksheet_set_column_data	(SGworksheet *worksheet,
						 gint col,
						 GtkPlotArray *array);
GtkPlotArray *  sg_worksheet_get_column_data	(SGworksheet *worksheet,
						 gint col,
						 GtkType type);
gboolean	sg_worksheet_cell_update_format	(SGworksheet *worksheet,
						 gint row, gint col);
void 		sg_worksheet_update_column_exp 	(SGworksheet *worksheet,
                                      		 const gchar *exp, gint column,
                                       		 gint from, gint to);
gboolean        sg_worksheet_unupdate_all      	(SGworksheet *worksheet);
gboolean        sg_worksheet_unupdate_exp_range	(SGworksheet *worksheet, 
                                              	 gint row0, gint rowi,
                                              	 gint col0, gint coli);
gboolean        sg_worksheet_update_exp_all  	(SGworksheet *worksheet);
gboolean        sg_worksheet_update_exp_range	(SGworksheet *worksheet, 
                                             	 gint row0, gint rowi,
                                              	 gint col0, gint coli);
void		sg_worksheet_hidden_cell_clear	(SGworksheet *sheet,
						 gint row, gint col);
void    	sg_worksheet_copy               (SGworksheet *worksheet,
					         gboolean clear);
void    	sg_worksheet_paste              (SGworksheet *worksheet);


#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __SG_WORKSHEET_H__ */

