Re: [gnome-db] Path that implements gnome_db_combo_set_model
- From: Gonzalo Paniagua Javier <gonzalo gnome-db org>
- To: Gnome-db list <gnome-db-list gnome org>
- Subject: Re: [gnome-db] Path that implements gnome_db_combo_set_model
- Date: Fri, 28 Nov 2003 19:20:08 +0100
El vie, 28-11-2003 a las 16:41, Rodrigo Moya escribió:
> On Fri, 2003-11-28 at 16:37, Gonzalo Paniagua Javier wrote:
> > gnome_db_combo_set_model is empty. Here's a patch to implement it.
> >
> > Ok to commit?
>
> yes, please, commit to both HEAD and the 1.0 branch, since that should
> go there also.
After thinking for a while, I think that my previous patch does not do
what we intend. Ie, it does not bind the model to the combo. It just
copies what the model has when it's called.
I came up with a patch to really bind it, so that changes to the model
are inmediatly reflected in the combo. I hooked the "changed" signal in
the model and tried a simple test that adds a row to the model after
setting the combo model. It did nothing.
Shouldn't we be emitting the "changed" signal in:
gda_data_model_append_row
gda_data_model_remove_row
gda_data_model_update_row
gda_data_model_end_edit (not sure about this)
?
Apart from "changed" we should also emit "row_inserted", "row_removed"
and "row_updated"...
Am I right?
If so, here are the patches. I swear I have the ChangeLog entries around
;-).
-Gonzalo
--
Gonzalo Paniagua Javier <gonzalo gnome-db org>
http://www.gnome-db.org/~gonzalo/
Index: libgnomedb/gnome-db-combo.c
===================================================================
RCS file: /cvs/gnome/libgnomedb/libgnomedb/gnome-db-combo.c,v
retrieving revision 1.3
diff -u -r1.3 gnome-db-combo.c
--- libgnomedb/gnome-db-combo.c 28 Nov 2003 15:55:10 -0000 1.3
+++ libgnomedb/gnome-db-combo.c 28 Nov 2003 18:15:22 -0000
@@ -26,6 +26,7 @@
struct _GnomeDbComboPrivate {
GdaDataModel *model;
+ gint col;
};
static void gnome_db_combo_class_init (GnomeDbComboClass *klass);
@@ -40,6 +41,8 @@
GValue *value,
GParamSpec *pspec);
static void gnome_db_combo_finalize (GObject *object);
+static void model_changed_cb (GdaDataModel *model,
+ gpointer user_data);
enum {
PROP_0,
@@ -132,6 +135,7 @@
/* free memory */
if (GDA_IS_DATA_MODEL (combo->priv->model)) {
+ g_signal_handlers_disconnect_by_func (G_OBJECT (combo->priv->model), model_changed_cb, combo);
g_object_unref (G_OBJECT (combo->priv->model));
combo->priv->model = NULL;
}
@@ -181,6 +185,36 @@
return GTK_WIDGET (combo);
}
+static void
+model_changed_cb (GdaDataModel *model, gpointer user_data)
+{
+ GnomeDbCombo *combo = GNOME_DB_COMBO (user_data);
+ gint rows, i, cols;
+ const GdaValue *value;
+ GList *list = NULL;
+
+ g_return_if_fail (GDA_IS_DATA_MODEL (model));
+ g_return_if_fail (GNOME_DB_IS_COMBO (combo));
+
+ cols = gda_data_model_get_n_columns (model);
+ rows = gda_data_model_get_n_rows (model);
+ if (rows == 0 || cols < combo->priv->col) {
+ list = g_list_append (list, "");
+ gtk_combo_set_popdown_strings (GTK_COMBO (combo), list);
+ g_list_free (list);
+ return;
+ }
+
+ for (i = 0; i < rows; i++) {
+ value = gda_data_model_get_value_at (model, combo->priv->col, i);
+ list = g_list_append (list, gda_value_stringify (value));
+ }
+
+ gtk_combo_set_popdown_strings (GTK_COMBO (combo), list);
+ g_list_foreach (list, (GFunc) g_free, NULL);
+ g_list_free (list);
+}
+
/**
* gnome_db_combo_set_model
* @combo: a #GnomeDbCombo widget.
@@ -189,30 +223,34 @@
*
* Associate a #GdaDataModel with the given combo widget. Doing so makes the
* combo widget refresh its list of values and display the values contained
- * in the model, in the given position.
+ * in the model, in the given position. A NULL @model will make the combo empty
+ * and disassociate the previous model, if any.
*/
void
gnome_db_combo_set_model (GnomeDbCombo *combo, GdaDataModel *model, gint col)
{
GList *list = NULL;
- gint rows, i;
- const GdaValue *value;
g_return_if_fail (GNOME_DB_IS_COMBO (combo));
- g_return_if_fail (GDA_IS_DATA_MODEL (model));
- g_return_if_fail (col >= 0 && col < gda_data_model_get_n_columns (model));
-
- rows = gda_data_model_get_n_rows (model);
- if (rows == 0)
- return;
+ g_return_if_fail (model == NULL || GDA_IS_DATA_MODEL (model));
- for (i = 0; i < rows; i++) {
- value = gda_data_model_get_value_at (model, col, i);
- list = g_list_append (list, gda_value_stringify (value));
+ if (GDA_IS_DATA_MODEL (combo->priv->model)) {
+ g_signal_handlers_disconnect_by_func (G_OBJECT (combo->priv->model), model_changed_cb, combo);
+ g_object_unref (G_OBJECT (combo->priv->model));
}
- gtk_combo_set_popdown_strings (GTK_COMBO (combo), list);
- g_list_foreach (list, (GFunc) g_free, NULL);
- g_list_free (list);
+ combo->priv->model = model;
+ if (model != NULL) {
+ g_object_ref (G_OBJECT (model));
+ g_signal_connect (G_OBJECT (combo->priv->model), "changed",
+ G_CALLBACK (model_changed_cb), combo);
+
+ combo->priv->col = col;
+ model_changed_cb (model, combo);
+ } else {
+ list = g_list_append (list, "");
+ gtk_combo_set_popdown_strings (GTK_COMBO (combo), list);
+ g_list_free (list);
+ }
}
Index: libgda/gda-data-model.c
===================================================================
RCS file: /cvs/gnome/libgda/libgda/gda-data-model.c,v
retrieving revision 1.40
diff -u -r1.40 gda-data-model.c
--- libgda/gda-data-model.c 16 Nov 2003 01:28:36 -0000 1.40
+++ libgda/gda-data-model.c 28 Nov 2003 18:17:59 -0000
@@ -561,15 +561,20 @@
*
* Appends a row to the given data model.
*
- * Returns: the unique ID of the added row.
+ * Returns: the added row.
*/
const GdaRow *
gda_data_model_append_row (GdaDataModel *model, const GList *values)
{
+ const GdaRow *row;
+
g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
g_return_val_if_fail (CLASS (model)->append_row != NULL, NULL);
- return CLASS (model)->append_row (model, values);
+ row = CLASS (model)->append_row (model, values);
+ gda_data_model_row_inserted (model, gda_row_get_number ((GdaRow *) row));
+ gda_data_model_changed (model);
+ return row;
}
/**
@@ -585,11 +590,18 @@
gboolean
gda_data_model_remove_row (GdaDataModel *model, const GdaRow *row)
{
+ gboolean result;
+
g_return_val_if_fail (GDA_IS_DATA_MODEL (model), FALSE);
g_return_val_if_fail (row != NULL, FALSE);
g_return_val_if_fail (CLASS (model)->remove_row != NULL, FALSE);
- return CLASS (model)->remove_row (model, row);
+ result = CLASS (model)->remove_row (model, row);
+ if (result) {
+ gda_data_model_row_removed (model, gda_row_get_number ((GdaRow *) row));
+ gda_data_model_changed (model);
+ }
+ return result;
}
/**
@@ -605,11 +617,18 @@
gboolean
gda_data_model_update_row (GdaDataModel *model, const GdaRow *row)
{
+ gboolean result;
+
g_return_val_if_fail (GDA_IS_DATA_MODEL (model), FALSE);
g_return_val_if_fail (row != NULL, FALSE);
g_return_val_if_fail (CLASS (model)->update_row != NULL, FALSE);
- return CLASS (model)->update_row (model, row);
+ result = CLASS (model)->update_row (model, row);
+ if (result) {
+ gda_data_model_row_updated (model, gda_row_get_number ((GdaRow *) row));
+ gda_data_model_changed (model);
+ }
+ return result;
}
/**
@@ -638,7 +657,7 @@
{
gint rows;
gint r;
- GdaRow *row;
+ const GdaRow *row;
gboolean more;
g_return_if_fail (GDA_IS_DATA_MODEL (model));
@@ -649,7 +668,7 @@
for (r = 0; more && r < rows ; r++) {
row = gda_data_model_get_row (model, r);
/* call the callback function */
- more = func (model, row, user_data);
+ more = func (model, (GdaRow *) row, user_data);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]