1 /* Forms/Custom layout
2 *
3 * A GdauiForm widget where the layout is customized
4 */
5
6 #include <libgda-ui/libgda-ui.h>
7 #include <sql-parser/gda-sql-parser.h>
8 #include "demo-common.h"
9
10 extern GdaConnection *demo_cnc;
11 extern GdaSqlParser *demo_parser;
12 static GtkWidget *window = NULL;
13
14 GtkWidget *
do_form_data_layout(GtkWidget * do_widget)15 do_form_data_layout (GtkWidget *do_widget)
16 {
17 if (!window) {
18 GdaStatement *stmt;
19 GtkWidget *vbox;
20 GtkWidget *label;
21 GdaDataModel *model;
22 GtkWidget *form;
23 GdauiRawForm *raw_form;
24
25 window = gtk_dialog_new_with_buttons ("Form with custom data layout",
26 GTK_WINDOW (do_widget),
27 0,
28 GTK_STOCK_CLOSE,
29 GTK_RESPONSE_NONE,
30 NULL);
31
32 g_signal_connect (window, "response",
33 G_CALLBACK (gtk_widget_destroy), NULL);
34 g_signal_connect (window, "destroy",
35 G_CALLBACK (gtk_widget_destroyed), &window);
36
37 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
38 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))),
39 vbox, TRUE, TRUE, 0);
40 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
41
42 label = gtk_label_new ("The following GdauiForm widget displays information about customers,\n"
43 "using a paned container where the right part is used to display\n"
44 "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 model = gda_connection_statement_execute_select (demo_cnc, stmt, NULL, NULL);
53 g_object_unref (stmt);
54 form = gdaui_form_new (model);
55 g_object_unref (model);
56
57 /* hide the ID data entry */
58 g_object_get (G_OBJECT (form), "raw-form", &raw_form, NULL);
59 gdaui_basic_form_entry_set_visible (GDAUI_BASIC_FORM (raw_form),
60 gda_set_get_holder (gdaui_basic_form_get_data_set (GDAUI_BASIC_FORM (raw_form)),
61 "id"), FALSE);
62
63 /* request custom layout:
64 <gdaui_form name="customers" container="hpaned">
65 <gdaui_section title="Summary">
66 <gdaui_column>
67 <gdaui_entry name="id"/>
68 <gdaui_entry name="name" label="Customer name"/>
69 <gdaui_entry name="comments" label="Comments" plugin="text"/>
70 <gdaui_entry name="total_orders" label="Total ordered" plugin="number:NB_DECIMALS=2;CURRENCY=€"/>
71 </gdaui_column>
72 </gdaui_section>
73 <gdaui_section title="Photo">
74 <gdaui_column>
75 <gdaui_entry name="photo" plugin="picture"/>
76 </gdaui_column>
77 </gdaui_section>
78 </gdaui_form>
79 */
80 gchar *filename;
81 filename = demo_find_file ("custom_layout.xml", NULL);
82 gdaui_basic_form_set_layout_from_file (GDAUI_BASIC_FORM (raw_form), filename, "customers");
83 g_free (filename);
84
85 /* we don't need the raw_form's reference anymore */
86 g_object_unref (G_OBJECT (raw_form));
87
88 gtk_box_pack_start (GTK_BOX (vbox), form, TRUE, TRUE, 0);
89 }
90
91 gboolean visible;
92 g_object_get (G_OBJECT (window), "visible", &visible, NULL);
93 if (!visible)
94 gtk_widget_show_all (window);
95 else {
96 gtk_widget_destroy (window);
97 window = NULL;
98 }
99
100 return window;
101 }
102
103
104