/* Grids/Using the picture plugin
 *
 * A GnomeDbGrid widget where the 'picture' plugin is used to display an image
 * internally stored as a BLOB
 */

#include <libgnomedb/libgnomedb.h>

extern GdaDict *demo_dict;
static GtkWidget *window = NULL;

GtkWidget *
do_grid_pict (GtkWidget *do_widget)
{  
	if (!window) {
                GdaQuery *query;
		GtkWidget *vbox;
		GtkWidget *label;
		GdaDataModel *model;
		GtkWidget *grid;
		GnomeDbRawGrid *raw_grid;
		GdaParameterList *data_set;
		GdaParameter *param;
		
		window = gtk_dialog_new_with_buttons ("Grid with the 'picture' plugin",
						      GTK_WINDOW (do_widget),
						      0,
						      GTK_STOCK_CLOSE,
						      GTK_RESPONSE_NONE,
						      NULL);
		
		g_signal_connect (window, "response",
				  G_CALLBACK (gtk_widget_destroy), NULL);
		g_signal_connect (window, "destroy",
				  G_CALLBACK (gtk_widget_destroyed), &window);
		
		vbox = gtk_vbox_new (FALSE, 5);
		gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
		
		label = gtk_label_new ("The following GnomeDbGrid widget displays data from the 'pictures' table.\n\n"
				       "The pictures are stored as BLOB inside the database and\n"
				       "are displayed using the 'picture' plugin (right click to \n"
				       "open a menu, or double click to load an image).");
		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
		
		/* Create the demo widget */
                query = gda_query_new_from_sql (demo_dict, "SELECT id, pict FROM pictures", NULL);
		model = gda_data_model_query_new (query);
		g_object_unref (query);

		gda_data_model_query_set_modification_query (GDA_DATA_MODEL_QUERY (model),
							     "UPDATE pictures set "
							     "pict=##/*name:'+1' type:'GdaBinary'*/ "
							     "WHERE id=##/*name:'-0' type:gint*/", NULL);
		gda_data_model_query_set_modification_query (GDA_DATA_MODEL_QUERY (model),
							     "DELETE FROM pictures WHERE id=##/*name:'-0' type:gint*/", NULL);
		gda_data_model_query_set_modification_query (GDA_DATA_MODEL_QUERY (model),
							     "INSERT INTO pictures (pict) "
							     "VALUES (##/*name:'+1' type:'GdaBinary'*/)", NULL);

		grid = gnome_db_grid_new (model);
		g_object_unref (model);

		/* specify that we want to use the 'picture' plugin */
		g_object_get (G_OBJECT (grid), "raw_grid", &raw_grid, NULL);
		data_set = GDA_PARAMETER_LIST (gnome_db_data_widget_get_current_data (GNOME_DB_DATA_WIDGET (raw_grid)));
		param = gda_parameter_list_find_param (data_set, "pict");
		g_object_set (G_OBJECT (param), "entry_plugin", "picture", NULL);

		gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, TRUE, 0);
	}

	if (!GTK_WIDGET_VISIBLE (window))
		gtk_widget_show_all (window);
	else
		gtk_widget_destroy (window);

	return window;
}


