[gnome-db] New GnomeDbFieldEditor and GnomeFormEditor



With this mail, I'm sendig my code to inspection of
the team.

This Code try to have simple Widget
(GnomeDbFieldEditor), for Show Data in a right way
depending of the data: String, Date, Bool, Blob, etc.

Now is working with String, Date or other that can be
showed in a GtkEntry Widget; the others are in
development.

With this Widget, I try to create a GnomeDbFormEditor:
the way is to create a GnomeDbForm like where you can
automaticaly show the data using the right widget
depending of the data type, but more... now I try to
this Widget (or Form) can update the data modified by
the user.

This will simplified the code for a simple
applications where you want fast creation of forms.

Now the widget GnomeDbForm, can show the data but not
update; I want to add some functions to costumize the
GnomeDbFieldEditor's arragement.

This a Pre-ALFA version.

In some near future, with the use of GnomeDbFielEditor
widget, you can define a XML file with the dising of
your form and show and update if you want the data!
(nor for the moment).

=====
>>>>>>>>>>>>>>>
Trabajar es la mejor arma para tu superación.

_________________________________________________________
Do You Yahoo!?
La mejor conexión a internet y 2GB en tu buzón de
Correo Yahoo! por $100 al mes. http://net.yahoo.com.mx

=====
>>>>>>>>>>>>>>>
Trabajar es la mejor arma para tu superación.

_________________________________________________________
Do You Yahoo!?
La mejor conexión a internet y 2GB en tu buzón de Correo Yahoo! por $100 al mes. http://net.yahoo.com.mx
/* GNOME DB library
 * Copyright (C) 1999-2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Daniel Espinosa <esodan yahoo com mx>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
#include "gnome-db-field-editor.h"


#define PARENT_TYPE GTK_TYPE_VBOX


struct _GnomeDbFieldEditorPrivate {
	GdaDataModel *model;
	gint row;
	gint col;
	GtkWidget *label;
	GtkWidget *widget;
	gboolean show_label;
	GnomeDbFieldEditorLabelPosition label_position;
};

static void gnome_db_field_editor_class_init   (GnomeDbFieldEditorClass *klass);
static void gnome_db_field_editor_init         (GnomeDbFieldEditor *field_editor, GnomeDbFieldEditorClass *klass);
static void gnome_db_field_editor_set_property (GObject *object,
					guint paramid,
					const GValue *value,
					GParamSpec *pspec);
static void gnome_db_field_editor_get_property (GObject *object,
					guint param_id,
					GValue *value,
					GParamSpec *pspec);
static void gnome_db_field_editor_finalize     (GObject *object);

static void update_value(GnomeDbFieldEditor *field_editor);
static void set_value(GnomeDbFieldEditor *field_editor, GdaValue *value);
static void download_file(GtkButton *button, gpointer data);
static void upload_file(GtkButton *button, gpointer data);
static void create_widgets(GnomeDbFieldEditor *field_editor);

enum {
	MODEL_CHANGED,
	LAST_SIGNAL
};

static gint field_editor_signals[LAST_SIGNAL];
static GObjectClass *parent_class = NULL;

/*
 * Callbacks
 */


/*
 * Private functions
 */

static void
set_value(GnomeDbFieldEditor *field_editor, GdaValue *value)
{
	if(GTK_IS_ENTRY(field_editor->priv->widget))
	{
		gtk_entry_set_text(field_editor->priv->widget, 
							gda_value_stringify(value));
	}
	
	if(GTK_IS_CHECK_BUTTON(field_editor->priv->widget))
	{
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(field_editor->priv->widget),
											gda_value_get_boolean(value));
	}
	
	if(GTK_IS_VBOX(field_editor->priv->widget))
	{
		
	}
}

static void
update_value(GnomeDbFieldEditor *field_editor)
{
	if(GTK_IS_ENTRY(field_editor->priv->widget))
	{
/* 		g_message("Es un Entry -- Val = %s", 
						gda_value_stringify(
								gda_data_model_get_value_at(
										field_editor->priv->model,
										field_editor->priv->col,
										field_editor->priv->row))); */
		
		gchar *txt;

		txt = gnome_db_field_editor_get_value_text(field_editor);
		
		
		gtk_entry_set_text(GTK_ENTRY(field_editor->priv->widget),	txt);
		g_message("Es un Entry -- Val = %s", 
						gnome_db_field_editor_get_value_text(field_editor));
	}
	
	if(GTK_IS_TOGGLE_BUTTON(field_editor->priv->widget))
	{
		g_message("Es un Boolean -- Val = %s", 
					gda_value_stringify(
							gda_data_model_get_value_at(field_editor->priv->model,
														field_editor->priv->col,
														field_editor->priv->row)));
	
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(field_editor->priv->widget),
											gda_value_get_boolean(
												gda_data_model_get_value_at(
													field_editor->priv->model,
													field_editor->priv->col,
													field_editor->priv->row)));
	}
	
	if(GTK_IS_VBOX(field_editor->priv->widget))
	{
		
	}
	
}

static void 
create_widgets(GnomeDbFieldEditor *field_editor) 
{
	GdaValueType type;
	GtkWidget *box;
	
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR(field_editor));
			
	if(GTK_IS_WIDGET(field_editor->priv->widget))
		gtk_widget_destroy(field_editor->priv->widget);
	
	type = gnome_db_field_editor_get_field_editor_type(field_editor);
	
	switch(type) {
		case GDA_VALUE_TYPE_GEOMETRIC_POINT:
			/* Three GtkFieldEditor widgets*/
			break;	
		case GDA_VALUE_TYPE_NULL:
		case GDA_VALUE_TYPE_BIGINT:
		case GDA_VALUE_TYPE_BIGUINT:
		case GDA_VALUE_TYPE_DOUBLE:
		case GDA_VALUE_TYPE_INTEGER:
		case GDA_VALUE_TYPE_MONEY:
		case GDA_VALUE_TYPE_NUMERIC:
		case GDA_VALUE_TYPE_SMALLINT:
		case GDA_VALUE_TYPE_SMALLUINT:
		case GDA_VALUE_TYPE_STRING:
		case GDA_VALUE_TYPE_TIME:
		case GDA_VALUE_TYPE_TIMESTAMP:
		case GDA_VALUE_TYPE_TINYINT:
		case GDA_VALUE_TYPE_TINYUINT:
		case GDA_VALUE_TYPE_DATE: 
			field_editor->priv->widget = gtk_entry_new();
			break;
		
		case GDA_VALUE_TYPE_BINARY:
		case GDA_VALUE_TYPE_BLOB:
			/*Button widget for File Selection to Upload/Download files*/
			{
				GtkButton *button_download, *button_upload;
												
				button_download = gtk_button_new_from_stock("gtk-go-down");
				button_upload = gtk_button_new_from_stock("gtk-go-up");
				
				field_editor->priv->widget = gtk_vbox_new(TRUE, 20);
				
				gtk_box_pack_start_defaults(GTK_BOX(field_editor->priv->widget), button_download);
				gtk_box_pack_start_defaults(GTK_BOX(field_editor->priv->widget), button_upload);
				
				g_signal_connect(G_OBJECT(button_download), "clicked", download_file, field_editor);
				g_signal_connect(G_OBJECT(button_download), "clicked", upload_file, field_editor);
				
			}
			break;
		
		case GDA_VALUE_TYPE_BOOLEAN:
			
			/* Gtk widget */
			field_editor->priv->widget = gtk_toggle_button_new();
			break;
		
		
		case GDA_VALUE_TYPE_GOBJECT:
		case GDA_VALUE_TYPE_LIST:
		case GDA_VALUE_TYPE_SINGLE:
		case GDA_VALUE_TYPE_TYPE:
		case GDA_VALUE_TYPE_UNKNOWN:
			/* With out widget*/
			
			field_editor->priv->widget = gtk_label_new("Can't show Data");
	}
	
	if(!field_editor->priv->show_label)
	{
		if(GTK_IS_WIDGET(field_editor->priv->label))
				gtk_widget_destroy(field_editor->priv->label);
		
		field_editor->priv->label = gtk_label_new(NULL);
		GString *label = g_string_new(NULL);
		g_string_append(label, "<span style='italic' weight='bold'>_");
		g_string_append(label, gnome_db_field_editor_get_column_name(field_editor));
		g_string_append(label, "</span>");
		gtk_label_set_markup_with_mnemonic(field_editor->priv->label, label->str);
		gtk_label_set_mnemonic_widget(field_editor->priv->label, field_editor->priv->widget);
		g_string_free(label, TRUE);
	}
	
	
	switch (field_editor->priv->label_position)
	{
		case GNOME_DB_FIELD_EDITOR_LABEL_BEHIND:
			box = gtk_vbox_new(FALSE,0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->widget, FALSE, FALSE, 0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->label, FALSE, FALSE, 0);
			break;
		case GNOME_DB_FIELD_EDITOR_LABEL_LEFT:
			box = gtk_hbox_new(FALSE,0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->label, FALSE, FALSE, 0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->widget, FALSE, FALSE, 0);
			break;
		case GNOME_DB_FIELD_EDITOR_LABEL_RIGHT:
			box = gtk_hbox_new(FALSE,0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->widget, FALSE, FALSE, 0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->label, FALSE, FALSE, 0);
			break;
		case GNOME_DB_FIELD_EDITOR_LABEL_ON_TOP:
		default :
			box = gtk_vbox_new(FALSE,0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->label, FALSE, FALSE, 0);
			gtk_box_pack_start(GTK_BOX(box), field_editor->priv->widget, FALSE, FALSE, 0);
			break;
	}
	gtk_box_pack_start(GTK_BOX(field_editor), box, FALSE, FALSE, 0);
	
	gtk_widget_show_all(GTK_WIDGET(field_editor));
}

static void
download_file (GtkButton *button, gpointer data) 
{
/*
	GtkFileChooserDialog file_chooser;
	GnomeDbFieldEditor *field_editor = (GnomeDbFieldEditor*) data;
	
	g_return_if_fail(GNOMEDB_IS_FIELD_EDITOR(field_editor));
	
	file_chooser = gtk_file_chooser_dialog_new("Download a Binary Object...", 
												NULL, 
												GTK_FILE_CHOOSER_ACTION_SAVE,
												GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
												GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
	if(gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
	{
		gchar *filename;
		gchar *buffer;
		gint file_len;
		GdaBlob *blob;
		
		blob = gda_value_get_blob(gda_data_model_get_value_at (field_editor->priv->model,
																field_editor->priv->col,
																field_editor->priv->row));
		
		if(!gda_blob_open(blob, GDA_BLOB_MODE_READ))
		{
			buffer = g_try_malloc(
			filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
			g_file_get_contents(filename, &buffer, 
			
			
			g_free(buffer);
		}
		g_free(blob);
		
	}
	*/
}

static void
upload_file (GtkButton *button, gpointer data) 
{
/*	GtkFileChooserDialog file_chooser;
	
	file_chooser = gtk_file_chooser_dialog_new("Upload a Binary Object...", 
												GTK_WIDGET(data), 
												GTK_FILE_CHOOSER_ACTION_SAVE,
												GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
												GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
	if(gtk_dialog_run(GTK_DIALOG(file_chooser)) == GTK_RESPONSE_ACCEPT)
	{
		gchar *filename;
		gchar *buffer;
		gint file_len;
		GdaBlob *blob, *new_blob;
		
		blob = gda_value_get_blob(gda_data_model_get_value_at (field_editor->priv->model,
																field_editor->priv->col,
																field_editor->priv->row));
		
		if(!gda_blob_open(blob, GDA_BLOB_MODE_READ))
		{
			new_blob = (GdaBlob*) g_newa(GdaBlob,1);
			filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser));
			gda_file_load(filename, buffer, file_len);
		}
	}
	*/
}


/*
 * GnomeDbFieldEditor class implementation
 */

static void
gnome_db_field_editor_class_init (GnomeDbFieldEditorClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	object_class->set_property = gnome_db_field_editor_set_property;
	object_class->get_property = gnome_db_field_editor_get_property;
	object_class->finalize = gnome_db_field_editor_finalize;

	/* add class signals */
	
}

static void
gnome_db_field_editor_init (GnomeDbFieldEditor *field_editor, GnomeDbFieldEditorClass *klass)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));

	/* allocate private structure */
	field_editor->priv = g_new0 (GnomeDbFieldEditorPrivate, 1);
	field_editor->priv->model = NULL;
	field_editor->priv->col = 0;
	field_editor->priv->row = 0;
	field_editor->priv->widget = NULL;
	field_editor->priv->show_label = TRUE;
	field_editor->priv->label_position = GNOME_DB_FIELD_EDITOR_LABEL_ON_TOP;
}

static void
gnome_db_field_editor_set_property (GObject *object,
			    guint param_id,
			    const GValue *value,
			    GParamSpec *pspec)
{
	GnomeDbFieldEditor *field_editor = (GnomeDbFieldEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));

	switch (param_id) {
	default :
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
gnome_db_field_editor_get_property (GObject *object,
			    guint param_id,
			    GValue *value,
			    GParamSpec *pspec)
{
	GnomeDbFieldEditor *field_editor = (GnomeDbFieldEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));

	switch (param_id) {
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
gnome_db_field_editor_finalize (GObject *object)
{
	GnomeDbFieldEditor *field_editor = (GnomeDbFieldEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));

	/* free memory */
	if (GDA_IS_DATA_MODEL (field_editor->priv->model)) {
		g_object_unref (G_OBJECT (field_editor->priv->model));
		field_editor->priv->model = NULL;
	}
	
	if (GTK_IS_WIDGET(field_editor->priv->label)) {
		g_object_unref (G_OBJECT (field_editor->priv->label));
		field_editor->priv->label = NULL;
	}
	
	if (GTK_IS_WIDGET(field_editor->priv->widget)) {
		g_object_unref (G_OBJECT (field_editor->priv->widget));
		field_editor->priv->widget = NULL;
	}
	
	/* chain to parent class */
	parent_class->finalize (object);
}

GType
gnome_db_field_editor_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (GnomeDbFieldEditorClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gnome_db_field_editor_class_init,
			NULL,
			NULL,
			sizeof (GnomeDbFieldEditor),
			0,
			(GInstanceInitFunc) gnome_db_field_editor_init
		};
		type = g_type_register_static (PARENT_TYPE, "GnomeDbFieldEditor", &info, 0);
	}
	return type;
}

GdaValueType
gnome_db_field_editor_get_value_vtype (GnomeDbFieldEditor *field_editor)
{
	return gda_value_get_vtype(gnome_db_field_editor_get_value(field_editor));
}

/**
 * gnome_db_field_editor_new
 *
 * Create a new #GnomeDbFieldEditor widget, which is a high-level widget which
 * lets you display display and edit a #GdaValue.
 *
 * Returns: the newly created widget.
 */
GtkWidget *
gnome_db_field_editor_new (void)
{
	GnomeDbFieldEditor *field_editor;

	field_editor = g_object_new (GNOME_DB_TYPE_FIELD_EDITOR, NULL);
	return GTK_WIDGET (field_editor);
}

/**
 * gnome_db_field_editor_new_with_model
 *
 * Create a new #GnomeDbFieldEditor widget, which is a high-level widget which
 * lets you display and edit a #GdaValue in a #GdaDataModel.
 * @model: a GdaDataModel to take data form.
 * @col: The column on the GdaDataModel.
 * @row: The current row on the GdaDataModel.
 * Returns: the newly created widget.
 */
GtkWidget *
gnome_db_field_editor_new_with_model (GdaDataModel *model, gint col, gint row)
{
	GnomeDbFieldEditor *field_editor;

	field_editor = g_object_new (GNOME_DB_TYPE_FIELD_EDITOR, NULL);
	gnome_db_field_editor_set_model(field_editor,model, col, row);
	return GTK_WIDGET (field_editor);
}

/**
 * gnome_db_field_editor_get_model
 */
GdaDataModel*
gnome_db_field_editor_get_model (GnomeDbFieldEditor *field_editor)
{
	
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));

	return field_editor->priv->model;
}

/**
 * gnome_db_field_editor_get_field_editor_type
 */
void
gnome_db_field_editor_set_model (GnomeDbFieldEditor *field_editor, GdaDataModel *model, gint col, gint row)
{
	
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	g_return_if_fail (GDA_IS_DATA_MODEL (model));
	g_return_if_fail (col >= 0 && col <= gda_data_model_get_n_columns(model));
	g_return_if_fail (row >= 0 && row <= gda_data_model_get_n_rows(model));
	
	if(GDA_IS_DATA_MODEL(field_editor->priv->model)) 
		g_object_unref(field_editor->priv->model);
		
	field_editor->priv->model = model;
	g_object_ref(model);
	field_editor->priv->col = col;
	field_editor->priv->row = row;
	g_message("Antes de llamara CREATE_WIDGETS\n");
	create_widgets(field_editor);
	g_message("Antes de llamara UPDATE_VALUE\n");
	update_value(field_editor);
	g_signal_emit (G_OBJECT (field_editor), field_editor_signals[MODEL_CHANGED], 0);
}

/**
 * gnome_db_field_editor_get_field_editor_type
 */
GdaValueType
gnome_db_field_editor_get_field_editor_type (GnomeDbFieldEditor *field_editor)
{
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), -1);
	g_return_val_if_fail (GDA_IS_DATA_MODEL (field_editor->priv->model), -1);
	
	GdaValue *val;
	val	= gnome_db_field_editor_get_value(field_editor);
	
	g_return_val_if_fail (val != NULL, GDA_VALUE_TYPE_UNKNOWN);
	
	g_message("En GetType: Campo: %s   Valor: %s Tipo: %i\n", 
						gnome_db_field_editor_get_column_name(field_editor),
						gda_value_stringify(val),
						gda_value_get_type(val));
	
	return gda_value_get_type(val);
}

/**
 * gnome_db_field_editor_get_value
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * Return the #GdaValue being used by the given widget.
 */
GdaValue *
gnome_db_field_editor_get_value (GnomeDbFieldEditor *field_editor)
{
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), NULL);
	g_return_val_if_fail (GDA_IS_DATA_MODEL(field_editor->priv->model), NULL);
	g_return_val_if_fail (field_editor->priv->col >= 0 && field_editor->priv->col <= gda_data_model_get_n_columns(field_editor->priv->model), -1);
	g_return_val_if_fail (field_editor->priv->row >= 0 && field_editor->priv->row <= gda_data_model_get_n_rows(field_editor->priv->model), -1);
	
	GdaValue *value;
	value = gda_data_model_get_value_at(field_editor->priv->model, 
													field_editor->priv->col, 
													field_editor->priv->row);
	
	return value;
}

/**
 * gnome_db_field_editor_set_value
 * 
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * 
 */
void
gnome_db_field_editor_set_value (GnomeDbFieldEditor *field_editor, GdaValue *value)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	g_return_if_fail (value!= NULL);
	set_value(field_editor, value);
}


/**
 * gnome_db_field_editor_get_text
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * Return the string in the #GdaValue.
 */
const gchar *
gnome_db_field_editor_get_value_text (GnomeDbFieldEditor *field_editor)
{
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), NULL);
	
	GdaValue *val;
	val	= gda_data_model_get_value_at(field_editor->priv->model,
												field_editor->priv->col,
												field_editor->priv->row);
	
	gchar* txt = gda_value_stringify(val);
	

	
	return txt;
}

/**
 * gnome_db_field_editor_get_widget_text
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * Return a gchar* showed by the widget.
 */
const gchar *
gnome_db_field_editor_get_widget_text (GnomeDbFieldEditor *field_editor)
{
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), NULL);
	if(GTK_IS_ENTRY(field_editor->priv->widget))
		return gtk_entry_get_text(GTK_ENTRY(field_editor->priv->widget));
	else
		return NULL;
}

/**
 * gnome_db_field_editor_get_widget_value
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * Return a GdaValue* from the text showed by the widget.
 */
GdaValue*
gnome_db_field_editor_get_widget_value (GnomeDbFieldEditor *field_editor)
{
	GdaValue *val;
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), NULL);
	if(GTK_IS_ENTRY(field_editor->priv->widget))
		
		if(gda_value_set_from_string(val, 
									gtk_entry_get_text(GTK_ENTRY(field_editor->priv->widget)),
									gnome_db_field_editor_get_value_vtype(field_editor)))
			return val;
		else
			/* If the Text is invalid for the type of GdaValue in the Field Editor Return the orignal value */
			return gnome_db_field_editor_get_value(field_editor);
	else
		return NULL;
}


/**
 * gnome_db_field_editor_get_current_row
 * @field_editor: a #GnomeDbFieldEditor widget.
 *
 * Get the row currently being shown by the given field_editor widget.
 *
 * Returns: the current row.
 */
gint
gnome_db_field_editor_get_current_row (GnomeDbFieldEditor *field_editor)
{
	g_return_val_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor), -1);
	return field_editor->priv->row;
}

/**
 * gnome_db_field_editor_get_column
 * @field_editor: a #GnomeDbFieldEditor widget.
 * Returns: the number of column of the field.
 *
 * Sets the current row on the given field_editor widget.
 */
gint
gnome_db_field_editor_get_column (GnomeDbFieldEditor *field_editor)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	return	field_editor->priv->col;
}

/**
 * gnome_db_field_editor_set_column
 * @field_editor: a #GnomeDbFieldEditor widget.
 * @row: the new row to display.
 *
 * Sets the current row on the given field_editor widget.
 */
void
gnome_db_field_editor_set_column (GnomeDbFieldEditor *field_editor, gint col)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	g_return_if_fail (col >= 0 && gda_data_model_get_n_columns(field_editor->priv->model));
	
	field_editor->priv->col = col;
	update_value(field_editor);
}

void
gnome_db_field_editor_show_label(GnomeDbFieldEditor *field_editor, gboolean show)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	field_editor->priv->show_label = show;
	create_widgets(field_editor);
}

gboolean
gnome_db_field_editor_is_label_shown(GnomeDbFieldEditor *field_editor)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	return field_editor->priv->show_label;
}

const char*
gnome_db_field_editor_get_column_name (GnomeDbFieldEditor *field_editor)
{
	return gda_data_model_get_column_title(field_editor->priv->model, field_editor->priv->col);
}

gint
gnome_db_field_editor_get_row (GnomeDbFieldEditor *field_editor)
{
	return field_editor->priv->row;
}
void
gnome_db_field_editor_set_row (GnomeDbFieldEditor *field_editor, gint row)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	g_return_if_fail (row >= 0 && row <= gda_data_model_get_n_rows(field_editor->priv->model));
	
	field_editor->priv->row = row;
	update_value(field_editor);
}

GnomeDbFieldEditorLabelPosition
gnome_db_field_editor_get_label_position(GnomeDbFieldEditor *field_editor)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	return field_editor->priv->label_position;
}
void
gnome_db_field_editor_set_label_position(GnomeDbFieldEditor *field_editor, GnomeDbFieldEditorLabelPosition pos)
{
	g_return_if_fail (GNOME_DB_IS_FIELD_EDITOR (field_editor));
	field_editor->priv->label_position = pos;
	create_widgets(field_editor);
}
/* GNOME DB library
 * Copyright (C) 1999-2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Daniel Espinosa <esodan yahoo com mx>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#if !defined(__gnome_db_field_editor_h__)
#  define __gnome_db_field_editor_h__

#include <libgda-1.2/libgda.h>
#include <gtk/gtk.h>

G_BEGIN_DECLS

#define GNOME_DB_TYPE_FIELD_EDITOR            (gnome_db_form_get_type())
#define GNOME_DB_FIELD_EDITOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_DB_TYPE_FIELD_EDITOR, GnomeDbFieldEditor))
#define GNOME_DB_FIELD_EDITOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GNOME_DB_TYPE_FIELD_EDITOR, GnomeDbFieldEditorClass))
#define GNOME_DB_IS_FIELD_EDITOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GNOME_DB_TYPE_FIELD_EDITOR))
#define GNOME_DB_IS_FIELD_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNOME_DB_TYPE_FIELD_EDITOR))

typedef struct _GnomeDbFieldEditor        GnomeDbFieldEditor;
typedef struct _GnomeDbFieldEditorClass   GnomeDbFieldEditorClass;
typedef struct _GnomeDbFieldEditorPrivate GnomeDbFieldEditorPrivate;

struct _GnomeDbFieldEditor {
	GtkVBox frame;
	GnomeDbFieldEditorPrivate *priv;
};

struct _GnomeDbFieldEditorClass {
	GtkVBoxClass parent_class;

	/* signals */
	void (* model_changed) (GnomeDbFieldEditor *field_editor);
};


typedef enum {
	GNOME_DB_FIELD_EDITOR_LABEL_ON_TOP,
	GNOME_DB_FIELD_EDITOR_LABEL_BEHIND,
	GNOME_DB_FIELD_EDITOR_LABEL_LEFT,
	GNOME_DB_FIELD_EDITOR_LABEL_RIGHT
} GnomeDbFieldEditorLabelPosition;

GType            gnome_db_field_editor_get_type (void);
GdaValueType     gnome_db_field_editor_get_value_vtype (GnomeDbFieldEditor *field_editor);
GtkWidget*       gnome_db_field_editor_new (void);
GtkWidget*       gnome_db_field_editor_new_with_model (GdaDataModel *model, gint col, gint row);
GdaDataModel*    gnome_db_field_editor_get_model(GnomeDbFieldEditor *field_editor);
void			 gnome_db_field_editor_set_model(GnomeDbFieldEditor *field_editor, GdaDataModel *model, gint col, gint row);
GdaValueType     gnome_db_field_editor_get_field_editor_type (GnomeDbFieldEditor *field_editor);
GdaValue*	     gnome_db_field_editor_get_value (GnomeDbFieldEditor *field_editor);
void		     gnome_db_field_editor_set_value (GnomeDbFieldEditor *field_editor, GdaValue *value);
const gchar*     gnome_db_field_editor_get_value_text (GnomeDbFieldEditor *field_editor);
const gchar*     gnome_db_field_editor_get_widget_text (GnomeDbFieldEditor *field_editor);
gint             gnome_db_field_editor_get_row (GnomeDbFieldEditor *field_editor);
void             gnome_db_field_editor_set_row (GnomeDbFieldEditor *field_editor, gint row);
gint             gnome_db_field_editor_get_column (GnomeDbFieldEditor *field_editor);
void             gnome_db_field_editor_set_column (GnomeDbFieldEditor *field_editor, gint col);
const char*		 gnome_db_field_editor_get_column_name (GnomeDbFieldEditor *field_editor);
void             gnome_db_field_editor_show_label(GnomeDbFieldEditor *field_editor, gboolean show);
gboolean         gnome_db_field_editor_is_label_shown(GnomeDbFieldEditor *field_editor);
GnomeDbFieldEditorLabelPosition gnome_db_field_editor_get_label_position(GnomeDbFieldEditor *field_editor);
void             gnome_db_field_editor_set_label_position(GnomeDbFieldEditor *field_editor, GnomeDbFieldEditorLabelPosition pos);

G_END_DECLS

#endif
/* GNOME DB library
 * Copyright (C) 1999-2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Daniel Espinosa <esodan yahoo com mx>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include "gnome_db_form_editor.h"
#include "gnome-db-field-editor.h"

#define PARENT_TYPE GTK_TYPE_VBOX

struct _GnomeDbFormEditorPrivate {
	GdaDataModel *model;
	gint current_row;
	GnomeDbFormEditorType type;
	gboolean columned;
	gboolean show_navigator_buttons;

	/* widgets */
	GtkWidget *form_container;

	GList *navigator_widgets; /* navigator widget */
	GtkWidget *navigator_label;
	GtkWidget *navigator_first;
	GtkWidget *navigator_back;
	GtkWidget *navigator_fwd;
	GtkWidget *navigator_last;
};

enum {
	MODEL_CHANGED,
	ROW_CHANGED,
	LAST_SIGNAL
};

static gint form_signals[LAST_SIGNAL];
static GObjectClass *parent_class = NULL;



static void gnome_db_form_editor_class_init   (GnomeDbFormEditorClass *klass);
static void gnome_db_form_editor_init         (GnomeDbFormEditor *form, GnomeDbFormEditorClass *klass);
static void gnome_db_form_editor_set_property (GObject *object,
					guint paramid,
					const GValue *value,
					GParamSpec *pspec);
static void gnome_db_form_editor_get_property (GObject *object,
					guint param_id,
					GValue *value,
					GParamSpec *pspec);
static void gnome_db_form_editor_finalize     (GObject *object);
static void set_row(GnomeDbFormEditor *form, gint row);
static GtkWidget* create_navigator(GnomeDbFormEditor *model);
static GtkWidget* create_list(GnomeDbFormEditor *model);


/*
 * Callbacks
 */

static void
show_navigator_row (GnomeDbFormEditor *form)
{
	gint total_rows;
	gchar *tmp;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	if (!GDA_IS_DATA_MODEL (form->priv->model)) {
		gtk_label_set_text (GTK_LABEL (form->priv->navigator_label), "No records");
		return;
	}

	if (total_rows <= 0) {
		gtk_widget_set_sensitive (form->priv->navigator_first, FALSE);
		gtk_widget_set_sensitive (form->priv->navigator_back, FALSE);
		gtk_widget_set_sensitive (form->priv->navigator_fwd, FALSE);
		gtk_widget_set_sensitive (form->priv->navigator_last, FALSE);
		gtk_label_set_text (GTK_LABEL (form->priv->navigator_label),"No records");
	}
	else {
		if (form->priv->current_row == 0) {
			gtk_widget_set_sensitive (form->priv->navigator_first, FALSE);
			gtk_widget_set_sensitive (form->priv->navigator_back, FALSE);
		}
		else {
			gtk_widget_set_sensitive (form->priv->navigator_first, TRUE);
			gtk_widget_set_sensitive (form->priv->navigator_back, TRUE);
		}

		if (form->priv->current_row == total_rows - 1) {
			gtk_widget_set_sensitive (form->priv->navigator_fwd, FALSE);
			gtk_widget_set_sensitive (form->priv->navigator_last, FALSE);
		}
		else {
			gtk_widget_set_sensitive (form->priv->navigator_fwd, TRUE);
			gtk_widget_set_sensitive (form->priv->navigator_last, TRUE);
		}

		tmp = g_strdup_printf ("Record %d of %d",
				       form->priv->current_row + 1,
				       total_rows);
		gtk_label_set_text (GTK_LABEL (form->priv->navigator_label), tmp);
		g_free (tmp);
	}
}


static void
navigator_first_cb (GtkButton *button, gpointer user_data)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) user_data;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	form->priv->current_row = 0;
	show_navigator_row (form);
	g_signal_emit (G_OBJECT (form), form_signals[ROW_CHANGED], 0);
}

static void
navigator_back_cb (GtkButton *button, gpointer user_data)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) user_data;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	if (form->priv->current_row > 0) {
		form->priv->current_row--;
		show_navigator_row (form);
	}
	g_signal_emit (G_OBJECT (form), form_signals[ROW_CHANGED], 0);
}

static void
navigator_forward_cb (GtkButton *button, gpointer user_data)
{
	gint max_rows;
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) user_data;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	max_rows = gda_data_model_get_n_rows (form->priv->model) - 1;

	if (form->priv->current_row < max_rows) {
		form->priv->current_row++;
		show_navigator_row (form);
	}
	g_signal_emit (G_OBJECT (form), form_signals[ROW_CHANGED], 0);
}

static void
navigator_last_cb (GtkButton *button, gpointer user_data)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) user_data;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	form->priv->current_row = gda_data_model_get_n_rows (form->priv->model) - 1;
	show_navigator_row (form);
	g_signal_emit (G_OBJECT (form), form_signals[ROW_CHANGED], 0);
}


/*
 * Private functions
 */


static GtkWidget* 
create_navigator(GnomeDbFormEditor *form)
{
	GtkWidget *box;
	GtkWidget *hbox;
	GtkWidget *vbox;
	GtkWidget *widget;
	GtkWidget *nav_box;
	GtkWidget *navigator;
	GtkWidget *sep;
	GdaValue *value;
	gint col, count;
	
	g_return_val_if_fail (GNOME_DB_IS_FORM_EDITOR (form), NULL);
	g_return_val_if_fail (GDA_IS_DATA_MODEL (form->priv->model), NULL);
	
	/*Create GnomeDbFieldEditor widgets*/
	hbox = gtk_hbox_new(FALSE,0);
	
	vbox = gtk_vbox_new(FALSE, 0);
	gtk_box_pack_start_defaults(GTK_BOX(hbox), vbox);
	
	count = 0;
	for(col = 0 ; col < gda_data_model_get_n_columns(form->priv->model) ; col++)
	{		
		count ++;
		if(count>4)
		{
			vbox = gtk_vbox_new(FALSE, 0);
			gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
			gtk_widget_show(vbox);
			count = 0;
		}
		
		widget = gnome_db_field_editor_new_with_model (form->priv->model, col, form->priv->current_row);
		
		g_message("Agregando/Mostrando FieldEditor = %s -- FieldEditorCol = %i -- Col = %i", 
						gnome_db_field_editor_get_column_name(
									GNOME_DB_FIELD_EDITOR(widget)), 
						gnome_db_field_editor_get_column(
									GNOME_DB_FIELD_EDITOR(widget)),
						col);
		g_message("Agregando/Mostrando Valor = %s -- RowActual = %i -- Col = %i", 
						gda_value_stringify(gda_data_model_get_value_at(
												form->priv->model,
												col,
												form->priv->current_row)),		
						form->priv->current_row,
						col);
		gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 10);
		g_object_set_data(G_OBJECT(form), 
							gnome_db_field_editor_get_column_name(
								GNOME_DB_FIELD_EDITOR(widget)),
							widget);
	}
	
	/*Crate Navigator Buttons Bar*/
	navigator = gtk_hbox_new(TRUE, 20);
	if(form->priv->show_navigator_buttons) {
		form->priv->navigator_label = gnome_db_new_label_widget ("No records");
		gtk_misc_set_alignment (GTK_MISC (form->priv->navigator_label), 0.5, 0.5);
		gtk_box_pack_start_defaults(GTK_BOX(navigator),form->priv->navigator_label);
		/* create navigation buttons */
		
		nav_box = gnome_db_new_hbutton_box_widget ();
		gtk_button_box_set_layout (GTK_BUTTON_BOX (nav_box), GTK_BUTTONBOX_DEFAULT_STYLE);
		gtk_box_pack_start_defaults(GTK_BOX(navigator),nav_box);
		
		form->priv->navigator_first = gnome_db_new_button_widget_from_stock (GTK_STOCK_GOTO_FIRST);
		g_signal_connect (G_OBJECT (form->priv->navigator_first), "clicked",
				  G_CALLBACK (navigator_first_cb), form);
		gtk_box_pack_start (GTK_BOX (nav_box), form->priv->navigator_first, FALSE, FALSE, 0);
		
		form->priv->navigator_back = gnome_db_new_button_widget_from_stock (GTK_STOCK_GO_BACK);
		g_signal_connect (G_OBJECT (form->priv->navigator_back), "clicked",
				  G_CALLBACK (navigator_back_cb), form);
		gtk_box_pack_start (GTK_BOX (nav_box), form->priv->navigator_back, FALSE, FALSE, 0);
		
		form->priv->navigator_fwd = gnome_db_new_button_widget_from_stock (GTK_STOCK_GO_FORWARD);
		g_signal_connect (G_OBJECT (form->priv->navigator_fwd), "clicked",
				  G_CALLBACK (navigator_forward_cb), form);
		gtk_box_pack_start (GTK_BOX (nav_box), form->priv->navigator_fwd, FALSE, FALSE, 0);
		
		form->priv->navigator_last = gnome_db_new_button_widget_from_stock (GTK_STOCK_GOTO_LAST);
		g_signal_connect (G_OBJECT (form->priv->navigator_last), "clicked",
				  G_CALLBACK (navigator_last_cb), form);
		gtk_box_pack_start (GTK_BOX (nav_box), form->priv->navigator_last, FALSE, FALSE, 0);
	}
	
	box = gtk_vbox_new(FALSE, 0);
	
	gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(box), navigator, FALSE, FALSE, 0);
	
	gtk_widget_show_all(GTK_WIDGET(box));
	
	show_navigator_row (form);
	
	return box;
}


static GtkWidget *
create_list (GnomeDbFormEditor *form)
{
	g_return_val_if_fail (GNOME_DB_IS_FORM_EDITOR (form), NULL);
	g_return_val_if_fail (GDA_IS_DATA_MODEL (form->priv->model), NULL);

	return gnome_db_new_grid_widget (form->priv->model);
}

static void
setup_widgets_for_model (GnomeDbFormEditor *form)
{
	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));
	/* destroy previous widgets */
	if (GTK_IS_WIDGET(form->priv->form_container))
		gtk_widget_destroy (form->priv->form_container);
	
	/* Delete G_OBJECT's objects saved*/

	/* create the new form depending on the type */
	switch (form->priv->type) {
	case GNOME_DB_FORM_EDITOR_TYPE_NAVIGATOR :
		form->priv->form_container = create_navigator (form);
		break;
	case GNOME_DB_FORM_EDITOR_TYPE_LIST :
		form->priv->form_container = create_list (form);
		break;
	default :
		form->priv->form_container = NULL;
		break;
	}

	if (!form->priv->form_container)
	{
		return;
	}

	gtk_widget_show (form->priv->form_container);
	gtk_box_pack_start (GTK_BOX (form), form->priv->form_container, TRUE, TRUE, 0);
	gtk_widget_show(GTK_WIDGET(form));
}



/*
 * GnomeDbFormEditor class implementation
 */

static void
gnome_db_form_editor_class_init (GnomeDbFormEditorClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);

	object_class->set_property = gnome_db_form_editor_set_property;
	object_class->get_property = gnome_db_form_editor_get_property;
	object_class->finalize = gnome_db_form_editor_finalize;

	/* add class signals */
	form_signals[MODEL_CHANGED] =
		g_signal_new ("model_changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GnomeDbFormEditorClass, model_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
	form_signals[ROW_CHANGED] =
		g_signal_new ("row_changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GnomeDbFormEditorClass, row_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
}

static void
gnome_db_form_editor_init (GnomeDbFormEditor *form, GnomeDbFormEditorClass *klass)
{
	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	/* allocate private structure */
	form->priv = g_new0 (GnomeDbFormEditorPrivate, 1);
	form->priv->model = NULL;
	form->priv->type = GNOME_DB_FORM_EDITOR_TYPE_NAVIGATOR;
	form->priv->form_container = NULL;
	form->priv->navigator_label = NULL;
	form->priv->show_navigator_buttons = TRUE;
	form->priv->columned = TRUE;
	form->priv->current_row = 0;
	form->priv->navigator_label = NULL;
	form->priv->navigator_first = NULL;
	form->priv->navigator_back = NULL;
	form->priv->navigator_fwd = NULL;
	form->priv->navigator_last = NULL;
}

static void
gnome_db_form_editor_set_property (GObject *object,
			    guint param_id,
			    const GValue *value,
			    GParamSpec *pspec)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	switch (param_id) {
	default :
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
gnome_db_form_editor_get_property (GObject *object,
			    guint param_id,
			    GValue *value,
			    GParamSpec *pspec)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	switch (param_id) {
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
gnome_db_form_editor_finalize (GObject *object)
{
	GnomeDbFormEditor *form = (GnomeDbFormEditor *) object;

	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));

	/* free memory */
	if (GDA_IS_DATA_MODEL (form->priv->model)) {
		g_object_unref (G_OBJECT (form->priv->model));
		form->priv->model = NULL;
	}

	if (form->priv->navigator_widgets) {
		g_list_free (form->priv->navigator_widgets);
		form->priv->navigator_widgets = NULL;
	}

	g_free (form->priv);
	form->priv = NULL;

	/* chain to parent class */
	parent_class->finalize (object);
}

GType
gnome_db_form_editor_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (GnomeDbFormEditorClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gnome_db_form_editor_class_init,
			NULL,
			NULL,
			sizeof (GnomeDbFormEditor),
			0,
			(GInstanceInitFunc) gnome_db_form_editor_init
		};
		type = g_type_register_static (PARENT_TYPE, "GnomeDbFormEditor", &info, 0);
	}
	return type;
}

/**
 * gnome_db_form_new
 *
 * Create a new #GnomeDbFormEditor widget, which is a high-level widget which
 * lets you display recordsets in many different ways, such as a list,
 * record-by-record, etc.
 *
 * Returns: the newly created widget.
 */
GtkWidget *
gnome_db_form_editor_new (void)
{
	GnomeDbFormEditor *form;

	form = g_object_new (GNOME_DB_TYPE_FORM_EDITOR, NULL);
	return GTK_WIDGET (form);
}

GtkWidget*
gnome_db_form_editor_new_with_model(GdaDataModel *model, gint row, gboolean columned) 
{
	GnomeDbFormEditor *form;
	
	form = GNOME_DB_FORM_EDITOR(gnome_db_form_editor_new());
		
	form->priv->model = model;
	g_object_ref(model);
	form->priv->current_row = row;
	form->priv->columned = columned;
	setup_widgets_for_model(form);
	return form;
}

GtkWidget*
gnome_db_form_editor_get_field (GnomeDbFormEditor *form, const gchar *name) 
{
	return GTK_WIDGET(g_object_get_data(G_OBJECT(form), name));
}

void
gnome_db_form_editor_update_data(GnomeDbFormEditor *form)
{
	GnomeDbFieldEditor *field;
	GdaRow *row;
	GdaValue *val;
	gint i;
	gboolean changed;
		
	row = gda_row_new(form->priv->model, gda_data_model_get_n_columns(form->priv->model));
	
	for(i = 0; i < gda_data_model_get_n_columns(form->priv->model); i++)
	{
		val = gda_row_get_value(row, i);
		field = (GnomeDbFieldEditor*) 
					g_object_get_data(G_OBJECT(form),
							gda_data_model_get_column_title(form->priv->model, i));
		if(GNOME_DB_IS_FIELD_EDITOR(field))
		{
			GdaValue *v = gnome_db_field_editor_get_widget_value(field);
			GdaValue *v_org = gda_data_model_get_value_at(form->priv->model,
														   i, form->priv->current_row);
			/*Set the original value form data model */
			gda_value_set_from_value(val, v_org);
			
			if(gda_value_compare(v, v_org) != 0)	
			{
				/* If diferent form the stored in the FieldEditor*/
				changed = TRUE;
				gda_value_set_from_value(val, v);
				
			}
			g_message("Original Value: \'%s\' Value to update: \'%s\' Actualizar=%i\n", 
									gda_value_stringify(v_org), gda_value_stringify(v), changed);
		}
		else
			g_message("En UpdateDataForm: No es un FIELD\n");
	}
	
}

void
gnome_db_form_editor_delete_current_row (GnomeDbFormEditor *form)
{
	GdaRow *row = gda_data_model_get_row(form->priv->model, form->priv->current_row);
	
	gda_data_model_remove_row(form->priv->model, row);
}

void
gnome_db_form_editor_set_row(GnomeDbFormEditor *form, gint row)
{
	form->priv->current_row = row;
	set_row(form, row);
}

static void
set_row(GnomeDbFormEditor *form, gint row)
{
	gint col;
	GnomeDbFieldEditor *field_data;
	
	form->priv->current_row = row;
	
	for(col=0; col < gda_data_model_get_n_columns(form->priv->model); col ++)
	{
		field_data = GNOME_DB_FIELD_EDITOR(g_object_get_data(G_OBJECT(form), 
									gda_data_model_get_column_title(
														form->priv->model, col)));
		if(GNOME_DB_IS_FIELD_EDITOR(field_data))
			gnome_db_field_editor_set_row(field_data, row);
		
	}
}

void
gnome_db_form_editor_free(GnomeDbFormEditor *form)
{
	g_free(form);
}

GtkWidget*
gnome_db_form_editor_find_field (GnomeDbFormEditor *form, const gchar *name)
{
	return GTK_WIDGET(g_object_get_data(G_OBJECT(form), name));	
}

/**
 * gnome_db_form_editor_set_model
 * @form: a #GnomeDbForm widget.
 * @model: the data model to associate the widget with.
 */
void
gnome_db_form_set_model (GnomeDbFormEditor *form, GdaDataModel *model)
{
	g_return_if_fail (GNOME_DB_IS_FORM_EDITOR (form));
	g_return_if_fail (GDA_IS_DATA_MODEL (model));

	if (GDA_IS_DATA_MODEL (form->priv->model))
		g_object_unref (G_OBJECT (form->priv->model));

	g_object_ref (G_OBJECT (model));
	form->priv->model = model;
	setup_widgets_for_model (form);
	g_signal_emit (G_OBJECT (form), form_signals[MODEL_CHANGED], 0);
}
/* GNOME DB library
 * Copyright (C) 1999-2002 The GNOME Foundation.
 *
 * AUTHORS:
 *      Daniel Espinosa <esodan yahoo com mx>
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
#ifndef GNOME_DB_FORM_EDITOR_H
#define GNOME_DB_FORM_EDITOR_H

#include <libgda-1.2/libgda.h>
#include <gtk/gtk.h>

G_BEGIN_DECLS

#define GNOME_DB_TYPE_FORM_EDITOR            (gnome_db_form_editor_get_type())
#define GNOME_DB_FORM_EDITOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, GNOME_DB_TYPE_FORM_EDITOR, GnomeDbFormEditor))
#define GNOME_DB_FORM_EDITOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, GNOME_DB_TYPE_FORM_EDITOR, GnomeDbFormEditorClass))
#define GNOME_DB_IS_FORM_EDITOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, GNOME_DB_TYPE_FORM_EDITOR))
#define GNOME_DB_IS_FORM_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNOME_DB_TYPE_FORM_EDITOR))

typedef struct _GnomeDbFormEditor  GnomeDbFormEditor;
typedef struct _GnomeDbFormEditorClass   GnomeDbFormEditorClass;
typedef struct _GnomeDbFormEditorPrivate GnomeDbFormEditorPrivate;

struct _GnomeDbFormEditor {
	GtkVBox frame;
	GnomeDbFormEditorPrivate *priv;
};

struct _GnomeDbFormEditorClass {
	GtkVBoxClass parent_class;

	/* signals */
	void (* model_changed) (GnomeDbFormEditor *form);
	void (* row_changed) (GnomeDbFormEditor *form);
};

typedef enum {
	GNOME_DB_FORM_EDITOR_TYPE_NAVIGATOR,
	GNOME_DB_FORM_EDITOR_TYPE_LIST
} GnomeDbFormEditorType;



GType            gnome_db_form_editor_get_type (void);
GtkWidget       *gnome_db_form_editor_new (void);
GtkWidget       *gnome_db_form_editor_new_with_model(GdaDataModel *model, gint row, gboolean columned);
GnomeDbFormEditorType  gnome_db_form_editor_get_form_type (GnomeDbFormEditor *form);
GtkWidget*       gnome_db_form_editor_find_field (GnomeDbFormEditor *form, const gchar *name);
void             gnome_db_form_editor_update_data(GnomeDbFormEditor *form);
void             gnome_db_form_editor_delete_current_row (GnomeDbFormEditor *form);
void             gnome_db_form_editor_set_form_type (GnomeDbFormEditor *form, GnomeDbFormEditorType type);
GdaDataModel    *gnome_db_form_editor_get_model (GnomeDbFormEditor *form);
void             gnome_db_form_editor_set_model (GnomeDbFormEditor *form, GdaDataModel *model);
void             gnome_db_form_editor_get_current_row(GnomeDbFormEditor *form);
void             gnome_db_form_editor_set_current_row(GnomeDbFormEditor *form, gint row);
void             gnome_db_form_editor_free(GnomeDbFormEditor *form);

G_END_DECLS

#endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]