1 /*
2  * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2010 David King <davidk@openismus.com>
4  * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 #include <libgda/libgda.h>
21 #include <libgda/binreloc/gda-binreloc.h>
22 #include <libgda-ui/libgda-ui.h>
23 #include "canvas/browser-canvas-db-relations.h"
24 #include "dnd.h"
25 
26 static gboolean on_delete_event (GtkWidget *window, GdkEvent *event, gpointer unused_data);
27 static void auto_layout_cb (GtkWidget *button, BrowserCanvas *canvas);
28 static void label_drag_data_received (GtkWidget *label, GdkDragContext *context,
29 				      gint x, gint y, GtkSelectionData *data,
30 				      guint info, guint time);
31 
32 static int
33 scroll_event_cb (G_GNUC_UNUSED GtkWidget *wid, GdkEvent *event, G_GNUC_UNUSED gpointer data)
34 {
35 	gboolean done = TRUE;
36 
37 	g_print ("%d\n", event->type);
38 	switch (event->type) {
39 	case GDK_SCROLL:
40 		if (((GdkEventScroll *) event)->direction == GDK_SCROLL_UP)
41 			g_print ("UP\n");
42 		else if (((GdkEventScroll *) event)->direction == GDK_SCROLL_DOWN)
43 			g_print ("DOWN\n");
44 		done = TRUE;
45 		break;
46 	default:
47 		done = FALSE;
48 		break;
49 	}
50 	return done;
51 }
52 
53 int
54 main (int argc, char *argv[])
55 {
56 	GdaConnection* connection;
57 	GError* error = NULL;
58 
59 	gdaui_init ();
60         gtk_init (&argc, &argv);
61 
62 	/* open connection to the SalesTest data source */
63 	connection = gda_connection_open_from_dsn ("SalesTest", NULL, GDA_CONNECTION_OPTIONS_NONE, &error);
64 	if (!connection) {
65 		fprintf (stderr, "%s\n", error->message);
66 		return -1;
67 	}
68 
69 	/* mate store update */
70 	g_print ("Metastore update...\n");
71 	if (!gda_connection_update_meta_store (connection, NULL, &error))
72 		return -1;
73 
74 	/* GdaMetaStruct */
75 	GdaMetaStruct *mstruct;
76 	mstruct = gda_meta_struct_new (gda_connection_get_meta_store (connection),
77 				       GDA_META_STRUCT_FEATURE_ALL);
78 
79 	/* UI Part */
80 	GtkWidget *window, *grid, *canvas;
81 	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
82 	gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
83 	g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event),
84 			  NULL);
85 
86 	grid = gtk_grid_new ();
87 	gtk_container_set_border_width (GTK_CONTAINER (grid), 15);
88 	gtk_container_add (GTK_CONTAINER (window), grid);
89 
90 	canvas = browser_canvas_db_relations_new (mstruct);
91 	g_object_unref (mstruct);
92 
93 	gtk_grid_attach (GTK_GRID (grid), canvas, 0, 0, 1, 1);
94 
95 	GtkWidget *bbox, *button;
96 	bbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
97 	gtk_grid_attach (GTK_GRID (grid), bbox, 0, 1, 1, 1);
98 	button = gtk_button_new_with_label ("Auto layout");
99 	g_signal_connect (button, "clicked",
100 			  G_CALLBACK (auto_layout_cb), canvas);
101 	gtk_box_pack_start (GTK_BOX (bbox), button, TRUE, TRUE, 0);
102 	g_signal_connect (button, "scroll-event",
103 			  G_CALLBACK (scroll_event_cb), NULL);
104 
105 	GtkWidget *wid;
106 	GtkTargetEntry dbo_table[] = {
107 		{ "text/plain", 0, 0 },
108 		{ "key-value", 0, 1 },
109 		{ "application/x-rootwindow-drop", 0, 2 }
110 	};
111 	wid = gtk_label_new ("\nDROP ZONE\n(hold SHIFT to drag and drop)\n");
112 	gtk_grid_attach (GTK_GRID (grid), wid, 0, 2, 1, 1);
113 	gtk_drag_dest_set (wid,
114 			   GTK_DEST_DEFAULT_ALL,
115 			   dbo_table, G_N_ELEMENTS (dbo_table),
116 			   GDK_ACTION_COPY | GDK_ACTION_MOVE);
117 	g_signal_connect (wid, "drag_data_received",
118 			  G_CALLBACK (label_drag_data_received), NULL);
119 
120 	gtk_window_set_default_size (GTK_WINDOW (window), 600, 450);
121 	gtk_widget_show_all (window);
122 
123 	/* add some tables */
124 	GValue *tname;
125 	g_value_set_string ((tname = gda_value_new (G_TYPE_STRING)), "customers");
126 	browser_canvas_db_relations_add_table (BROWSER_CANVAS_DB_RELATIONS (canvas), NULL, NULL, tname);
127 	g_value_set_string (tname, "orders");
128 	browser_canvas_db_relations_add_table (BROWSER_CANVAS_DB_RELATIONS (canvas), NULL, NULL, tname);
129 	g_value_set_string (tname, "order_contents");
130 	browser_canvas_db_relations_add_table (BROWSER_CANVAS_DB_RELATIONS (canvas), NULL, NULL, tname);
131 	g_value_set_string (tname, "products");
132 	browser_canvas_db_relations_add_table (BROWSER_CANVAS_DB_RELATIONS (canvas), NULL, NULL, tname);
133 	g_value_set_string (tname, "locations");
134 	browser_canvas_db_relations_add_table (BROWSER_CANVAS_DB_RELATIONS (canvas), NULL, NULL, tname);
135 	gda_value_free (tname);
136 
137 	/* Pass control to the GTK+ main event loop. */
138 	gtk_main ();
139 
140 	return 0;
141 }
142 
143 static void
144 auto_layout_cb (G_GNUC_UNUSED GtkWidget *button, BrowserCanvas *canvas)
145 {
146 	browser_canvas_perform_auto_layout (BROWSER_CANVAS (canvas), TRUE, BROWSER_CANVAS_LAYOUT_RADIAL);
147 }
148 
149 static void
150 label_drag_data_received (G_GNUC_UNUSED GtkWidget *label, GdkDragContext *context,
151 			  G_GNUC_UNUSED gint x, G_GNUC_UNUSED gint y, GtkSelectionData *data,
152 			  G_GNUC_UNUSED guint info, guint time)
153 {
154 	if ((gtk_selection_data_get_length (data) >= 0) && (gtk_selection_data_get_format (data) == 8)) {
155 		g_print ("Received \"%s\" in drop zone\n",
156 			 (gchar *) gtk_selection_data_get_data (data));
157 		gtk_drag_finish (context, TRUE, FALSE, time);
158 		return;
159 	}
160 
161 	gtk_drag_finish (context, FALSE, FALSE, time);
162 }
163 
164 static gboolean
165 on_delete_event (G_GNUC_UNUSED GtkWidget *window, G_GNUC_UNUSED GdkEvent *event, G_GNUC_UNUSED gpointer data)
166 {
167 	exit (0);
168 }
169