Пример #1
0
static void
gmdb_sql_execute_cb(GtkWidget *w, GladeXML *xml)
{

	guint len;
	gchar *buf;
	gchar *bound_data[256];
	int i;
	MdbSQLColumn *sqlcol;
	GtkTextBuffer *txtbuffer;
	GtkTextIter start, end;
	GtkWidget *textview, *combobox, *treeview;
	GtkTreeModel *store;
	/*GtkWidget *window;*/
	GType *gtypes;
	GtkTreeIter iter;
	GtkTreeViewColumn *column;
	long row, maxrow;
	/* GdkCursor *watch, *pointer; */

	/*  need to figure out how to clock during the treeview recalc/redraw
	window = glade_xml_get_widget(xml, "sql_window");
	watch = gdk_cursor_new(GDK_WATCH);
	gdk_window_set_cursor(GTK_WIDGET(window)->window, watch);
	gdk_cursor_unref(watch);
	*/

	/* stuff this query on the history */
	textview = glade_xml_get_widget(xml, "sql_textview");
	combobox = glade_xml_get_widget(xml, "sql_combo");
	txtbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
	len = gtk_text_buffer_get_char_count(txtbuffer);
	gtk_text_buffer_get_iter_at_offset (txtbuffer, &start, 0);
	gtk_text_buffer_get_iter_at_offset (txtbuffer, &end, len);
	buf = gtk_text_buffer_get_text(txtbuffer, &start, &end, FALSE);

	/* add to the history */
	gtk_combo_box_prepend_text(GTK_COMBO_BOX(combobox), buf);
	gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);

	/* ok now execute it */
	mdb_sql_run_query(sql, buf);
	if (mdb_sql_has_error(sql)) {
		GtkWidget* dlg = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (w)),
		    GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
		    "%s", mdb_sql_last_error(sql));
		gtk_dialog_run (GTK_DIALOG (dlg));
		gtk_widget_destroy (dlg);
		mdb_sql_reset(sql);
		return;
	}

	treeview = glade_xml_get_widget(xml, "sql_results");

	gtypes = g_malloc(sizeof(GType) * sql->num_columns);
	for (i=0;i<sql->num_columns;i++) 
		gtypes[i]=G_TYPE_STRING;

	store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
	if (store) {
		while ((column = gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0))) {
			gtk_tree_view_remove_column(GTK_TREE_VIEW(treeview), column);
		}
		g_object_unref(store);
	}
	store = (GtkTreeModel*)gtk_list_store_newv(sql->num_columns, gtypes);
	g_free(gtypes);

	gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), store);
	
	GtkCellRenderer *renderer;
	renderer = gtk_cell_renderer_text_new(); 

	for (i=0;i<sql->num_columns;i++) { 
		bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE); 
        	mdb_sql_bind_column(sql, i+1, bound_data[i], NULL);
		sqlcol = g_ptr_array_index(sql->columns,i);
		column = gtk_tree_view_column_new_with_attributes(sqlcol->name, renderer, "text", i, NULL); 
		gtk_tree_view_append_column(GTK_TREE_VIEW (treeview), column); 
	}

	maxrow = gmdb_prefs_get_maxrows();

	row = 0;
	while(mdb_fetch_row(sql->cur_table) &&
			(!maxrow || (row < maxrow))) {
		row++;
		gtk_list_store_append(GTK_LIST_STORE(store), &iter);
		for (i=0;i<sql->num_columns;i++) { 
			gtk_list_store_set(GTK_LIST_STORE(store), 
				&iter, i, (gchar *) bound_data[i], -1);
		}
	}


	/* free the memory used to bind */
	for (i=0;i<sql->num_columns;i++) {
		g_free(bound_data[i]);
	}

	mdb_sql_reset(sql);
	g_free(buf);

	/*
	pointer = gdk_cursor_new(GDK_LEFT_PTR);
	gdk_window_set_cursor(GTK_WIDGET(window)->window, pointer);
	gdk_cursor_unref(pointer);
	*/
	
}
Пример #2
0
/* functions */
GtkWidget *
gmdb_table_data_new(MdbCatalogEntry *entry)
{
MdbTableDef *table;
MdbColumn *col;
GtkWidget *clist;
GtkWidget *scroll;
int i, rownum;
long row, maxrow;
gchar *bound_data[256];
GMdbDataWindow *dataw = NULL;


	/* do we have an active window for this object? if so raise it */
	for (i=0;i<g_list_length(window_list);i++) {
		dataw = g_list_nth_data(window_list, i);
		if (!strcmp(dataw->table_name, entry->object_name)) {
			gdk_window_raise (dataw->window->window);
			return dataw->window;
		}
	}

	dataw = g_malloc(sizeof(GMdbDataWindow));
	strcpy(dataw->table_name, entry->object_name);

	dataw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);	
	gtk_window_set_title(GTK_WINDOW(dataw->window), entry->object_name);
	gtk_widget_set_usize(dataw->window, 300,200);
	gtk_widget_set_uposition(dataw->window, 50,50);
	gtk_widget_show(dataw->window);

    gtk_signal_connect (GTK_OBJECT (dataw->window), "delete_event",
        GTK_SIGNAL_FUNC (gmdb_table_data_close), dataw);


	scroll = gtk_scrolled_window_new(NULL,NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_widget_show (scroll);
	gtk_container_add(GTK_CONTAINER(dataw->window), scroll);

	/* read table */
	table = mdb_read_table(entry);
	mdb_read_columns(table);
	mdb_rewind_table(table);

	clist = gtk_clist_new(table->num_cols);
	gtk_widget_show(clist);
	gtk_container_add(GTK_CONTAINER(scroll),clist);

	for (i=0;i<table->num_cols;i++) {
		/* bind columns */
		bound_data[i] = (char *) g_malloc0(MDB_BIND_SIZE);
		mdb_bind_column(table, i+1, bound_data[i], NULL);

		/* display column titles */
		col=g_ptr_array_index(table->columns,i);
		gtk_clist_set_column_title(GTK_CLIST(clist), i, col->name);
	}
	gtk_clist_column_titles_show(GTK_CLIST(clist));

	maxrow = gmdb_prefs_get_maxrows();

	/* fetch those rows! */
	row = 0;
	while(mdb_fetch_row(table) && 
			(!maxrow || (row < maxrow))) {
		row++;
		rownum = gtk_clist_append(GTK_CLIST(clist), bound_data);
	}

	/* free the memory used to bind */
	for (i=0;i<table->num_cols;i++) {
		g_free(bound_data[i]);
	}

	/* add this one to the window list */
	window_list = g_list_append(window_list, dataw);

	return dataw->window;
}