1 /* Grids/Custom layout
2  *
3  * A GdauiGrid widget where the layout is customized
4  *
5  */
6 
7 #include <libgda-ui/libgda-ui.h>
8 #include <sql-parser/gda-sql-parser.h>
9 #include "demo-common.h"
10 
11 extern GdaConnection *demo_cnc;
12 extern GdaSqlParser *demo_parser;
13 static GtkWidget *window = NULL;
14 
15 GtkWidget *
16 do_grid_data_layout (GtkWidget *do_widget)
17 {
18 	if (!window) {
19                 GdaStatement *stmt;
20 		GtkWidget *vbox;
21 		GtkWidget *label;
22 		GdaDataModel *model;
23 		GtkWidget *grid;
24 		GdauiRawGrid *raw_grid;
25 
26 		window = gtk_dialog_new_with_buttons ("Grid with custom data layout",
27 						      GTK_WINDOW (do_widget),
28 						      0,
29 						      GTK_STOCK_CLOSE,
30 						      GTK_RESPONSE_NONE,
31 						      NULL);
32 
33 		g_signal_connect (window, "response",
34 				  G_CALLBACK (gtk_widget_destroy), NULL);
35 		g_signal_connect (window, "destroy",
36 				  G_CALLBACK (gtk_widget_destroyed), &window);
37 
38 		vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
39 		gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
40 				    vbox, TRUE, TRUE, 0);
41 		gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
42 
43 		label = gtk_label_new ("The following GdauiGrid widget displays information about customers,\n"
44 				       "using a picture of the customer.\n");
45 		gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
46 
47 		/* Create the demo widget: select all the customers and computes the total
48 		 * amount of orders they have spent */
49 		stmt = gda_sql_parser_parse_string (demo_parser,
50 						    "select c.id, c.name, c.country, c.city, c.photo, c.comments, sum (od.quantity * (1 - od.discount/100) * p.price) as total_orders from customers c left join orders o on (c.id=o.customer) left join order_contents od on (od.order_id=o.id) left join products p on (p.ref = od.product_ref) group by c.id order by total_orders desc",
51 						    NULL, NULL);
52 
53                 model = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
54                 g_object_unref (stmt);
55  		grid = gdaui_grid_new (model);
56 		g_object_unref (model);
57 
58 		/* request custom layout:
59 		   <gdaui_grid name="customers">
60 		     <gdaui_entry name="name"/>
61 		     <gdaui_entry name="total_orders" label="Total ordered" plugin="number:NB_DECIMALS=2;CURRENCY=€"/>
62 		     <gdaui_entry name="photo" plugin="picture"/>
63 		   </gdaui_grid>
64 		 */
65 		g_object_get (G_OBJECT (grid), "raw-grid", &raw_grid, NULL);
66 		gchar *filename;
67 		filename = demo_find_file ("custom_layout.xml", NULL);
68 		gdaui_raw_grid_set_layout_from_file (GDAUI_RAW_GRID (raw_grid), filename, "customers");
69 		g_free (filename);
70 
71 		gtk_box_pack_start (GTK_BOX (vbox), grid, TRUE, TRUE, 0);
72 
73 		gtk_widget_set_size_request (window, 500, 500);
74 	}
75 
76 	gboolean visible;
77 	g_object_get (G_OBJECT (window), "visible", &visible, NULL);
78 	if (!visible)
79 		gtk_widget_show_all (window);
80 	else {
81 		gtk_widget_destroy (window);
82 		window = NULL;
83 	}
84 
85 	return window;
86 }
87 
88 
89