[libgda] New UI extension example



commit 752bcb5876840bceab3a9143609a1c2086e21975
Author: Vivien Malerba <malerba gnome-db org>
Date:   Tue Jan 19 21:47:13 2010 +0100

    New UI extension example

 Makefile.am                         |    6 ++-
 samples/README                      |    3 +
 samples/SimpleExample/example.c     |    1 +
 samples/SimpleUIForm/README         |   20 +++++
 samples/SimpleUIForm/ScreenShot.png |  Bin 0 -> 20545 bytes
 samples/SimpleUIForm/example.c      |  133 +++++++++++++++++++++++++++++++++++
 6 files changed, 162 insertions(+), 1 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index c0c5888..a220366 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -88,7 +88,11 @@ example_files = \
 	samples/AsyncExec/example.c \
 	samples/WritableSelect/README \
 	samples/WritableSelect/example.c \
-	samples/WritableSelect/Makefile
+	samples/WritableSelect/Makefile \
+	samples/SimpleUIForm/README \
+	samples/SimpleUIForm/example.c \
+	samples/SimpleUIForm/Makefile \
+	samples/SimpleUIForm/ScreenShot.png
 
 EXTRA_DIST = \
 	COPYING \
diff --git a/samples/README b/samples/README
index 55982f8..3ab22d4 100644
--- a/samples/README
+++ b/samples/README
@@ -23,4 +23,7 @@ Currently the contents are:
 * in WritableSelect/: example to show how to modify a GdaDataModel returned after
   the execution of a SELECT statement
 
+And using the UI extension:
+* in SimpleUIForm/: a very simple form
+
 Good luck and happy hacking!
diff --git a/samples/SimpleExample/example.c b/samples/SimpleExample/example.c
index 09cf5fc..ded1406 100644
--- a/samples/SimpleExample/example.c
+++ b/samples/SimpleExample/example.c
@@ -31,6 +31,7 @@ main (int argc, char *argv[])
 	display_products_contents (cnc);
 
         gda_connection_close (cnc);
+	g_object_unref (cnc);
 
         return 0;
 }
diff --git a/samples/SimpleUIForm/README b/samples/SimpleUIForm/README
new file mode 100644
index 0000000..93028d8
--- /dev/null
+++ b/samples/SimpleUIForm/README
@@ -0,0 +1,20 @@
+Libgda simple example
+=====================
+
+Description:
+------------
+
+The example in this directory illustrate how to create a simple form using Libgdaui.
+
+It displays a form containing the customers of Libgda's SalesTest database, and for
+each customer, displays its ID, its name and the associated sales representative, using
+the name of the representative in a combo box rather than the representative's own ID.
+
+Compiling and running:
+----------------------
+
+To compile (make sure Libgdaui is installed prior to this):
+> make
+
+and to run:
+> ./example
diff --git a/samples/SimpleUIForm/ScreenShot.png b/samples/SimpleUIForm/ScreenShot.png
new file mode 100644
index 0000000..937b429
Binary files /dev/null and b/samples/SimpleUIForm/ScreenShot.png differ
diff --git a/samples/SimpleUIForm/example.c b/samples/SimpleUIForm/example.c
new file mode 100644
index 0000000..2bd1e7c
--- /dev/null
+++ b/samples/SimpleUIForm/example.c
@@ -0,0 +1,133 @@
+#include <libgda-ui/libgda-ui.h>
+#include <sql-parser/gda-sql-parser.h>
+
+static GdaConnection *open_connection (void);
+static GdaDataModel *get_customers (GdaConnection *cnc);
+static GdaDataModel *get_salesrep (GdaConnection *cnc);
+
+static void destroy (GtkWidget *widget, gpointer data)
+{
+	gtk_main_quit ();
+}
+
+int
+main (int argc, char *argv[])
+{
+	gtk_init (&argc, &argv);
+	gdaui_init ();
+
+	/* open connection */
+        GdaConnection *cnc;
+	cnc = open_connection ();
+
+	/* create data models */
+	GdaDataModel *customers, *salesrep;
+	customers = get_customers (cnc);
+	salesrep = get_salesrep (cnc);
+	
+	/* create UI */
+	GtkWidget *window, *vbox, *button, *form;
+	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+	g_signal_connect_swapped (window, "destroy",
+				  G_CALLBACK (destroy),
+				  window);
+	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
+
+	vbox = gtk_vbox_new (FALSE, 5);
+	gtk_container_add (GTK_CONTAINER (window), vbox);
+
+	/* main form to list customers */
+	form = gdaui_form_new (customers);
+	gtk_box_pack_start (GTK_BOX (vbox), form, TRUE, TRUE, 0);
+
+	GdaDataModelIter *iter;
+	GdaHolder *holder;
+	iter = gdaui_data_selector_get_data_set (GDAUI_DATA_SELECTOR (form));
+	holder = gda_data_model_iter_get_holder_for_field (iter, 2);
+	gda_holder_set_source_model (holder, salesrep, 0, NULL);
+
+	/* button to quit */
+	button = gtk_button_new_with_label ("Quit");
+	gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
+	g_signal_connect_swapped (button, "clicked",
+				  G_CALLBACK (gtk_widget_destroy),
+				  window);
+
+	gtk_widget_show_all (window);
+	gtk_main ();
+
+        gda_connection_close (cnc);
+	g_object_unref (cnc);
+
+        return 0;
+}
+
+/*
+ * Open a connection to the example.db file
+ */
+static GdaConnection *
+open_connection ()
+{
+        GdaConnection *cnc;
+        GError *error = NULL;
+	GdaSqlParser *parser;
+
+	/* open connection */
+        cnc = gda_connection_open_from_dsn ("SalesTest", NULL,
+					    GDA_CONNECTION_OPTIONS_NONE,
+					    &error);
+        if (!cnc) {
+                g_print ("Could not open connection to SalesTest DSN: %s\n",
+                         error && error->message ? error->message : "No detail");
+                exit (1);
+        }
+
+	/* create an SQL parser */
+	parser = gda_connection_create_parser (cnc);
+	if (!parser) /* @cnc doe snot provide its own parser => use default one */
+		parser = gda_sql_parser_new ();
+	/* attach the parser object to the connection */
+	g_object_set_data_full (G_OBJECT (cnc), "parser", parser, g_object_unref);
+
+        return cnc;
+}
+
+static GdaDataModel *
+get_customers (GdaConnection *cnc)
+{
+	GdaDataModel *data_model;
+	GdaSqlParser *parser;
+	GdaStatement *stmt;
+	gchar *sql = "SELECT id, name, default_served_by FROM customers ORDER BY name";
+	GError *error = NULL;
+
+	parser = g_object_get_data (G_OBJECT (cnc), "parser");
+	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
+	data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
+	g_object_unref (stmt);
+        if (!data_model) 
+                g_error ("Could not get the contents of the 'products' table: %s\n",
+                         error && error->message ? error->message : "No detail");
+	gda_data_model_dump (data_model, stdout);
+	return data_model;
+}
+
+static GdaDataModel *
+get_salesrep (GdaConnection *cnc)
+{
+	GdaDataModel *data_model;
+	GdaSqlParser *parser;
+	GdaStatement *stmt;
+	gchar *sql = "SELECT id, name FROM salesrep ORDER BY name";
+	GError *error = NULL;
+
+	parser = g_object_get_data (G_OBJECT (cnc), "parser");
+	stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
+	data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
+	g_object_unref (stmt);
+        if (!data_model) 
+                g_error ("Could not get the contents of the 'products' table: %s\n",
+                         error && error->message ? error->message : "No detail");
+	gda_data_model_dump (data_model, stdout);
+	return data_model;
+}



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