From 873675c422db29ff7575758411be7af304e3a728 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Wed, 16 Dec 2009 19:49:22 +0100 Subject: [PATCH] New interface PsppireSelectDestWidget. Created a new interface type PsppireSelectDestWidget to serve as an abstract widget which can be the destination for a PsppireSelector. Changed PsppireVarView to implement this interface. --- src/ui/gui/automake.mk | 2 + src/ui/gui/psppire-select-dest.c | 57 ++++++++++++++++++++++++ src/ui/gui/psppire-select-dest.h | 49 +++++++++++++++++++++ src/ui/gui/psppire-selector.c | 75 ++++++++------------------------ src/ui/gui/psppire-var-view.c | 64 +++++++++++++++++---------- src/ui/gui/psppire-var-view.h | 3 -- 6 files changed, 167 insertions(+), 83 deletions(-) create mode 100644 src/ui/gui/psppire-select-dest.c create mode 100644 src/ui/gui/psppire-select-dest.h diff --git a/src/ui/gui/automake.mk b/src/ui/gui/automake.mk index f69cad5b..b7d60a78 100644 --- a/src/ui/gui/automake.mk +++ b/src/ui/gui/automake.mk @@ -170,6 +170,8 @@ src_ui_gui_psppire_SOURCES = \ src/ui/gui/psppire-var-view.c \ src/ui/gui/psppire-var-view.h \ src/ui/gui/psppire-selector.h \ + src/ui/gui/psppire-select-dest.c \ + src/ui/gui/psppire-select-dest.h \ src/ui/gui/psppire-syntax-window.c \ src/ui/gui/psppire-syntax-window.h \ src/ui/gui/psppire-var-ptr.c \ diff --git a/src/ui/gui/psppire-select-dest.c b/src/ui/gui/psppire-select-dest.c new file mode 100644 index 00000000..c46abcee --- /dev/null +++ b/src/ui/gui/psppire-select-dest.c @@ -0,0 +1,57 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2009 Free Software Foundation + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "psppire-select-dest.h" +#include + +GType +psppire_select_dest_widget_get_type (void) +{ + static GType dest_widget_type = 0; + + if (! dest_widget_type) + { + const GTypeInfo dest_widget_info = + { + sizeof (PsppireSelectDestWidgetIface), /* class_size */ + NULL, /* base_init */ + NULL, /* base_finalize */ + NULL, + NULL, /* class_finalize */ + NULL, /* class_data */ + 0, + 0, /* n_preallocs */ + NULL + }; + + dest_widget_type = + g_type_register_static (G_TYPE_INTERFACE, "PsppireSelectDestWidget", + &dest_widget_info, 0); + + g_type_interface_add_prerequisite (dest_widget_type, GTK_TYPE_WIDGET); + } + + return dest_widget_type; +} + + +gboolean +psppire_select_dest_widget_contains_var (PsppireSelectDestWidget *sdm, const GValue *value) +{ + return PSPPIRE_SELECT_DEST_GET_IFACE (sdm)->contains_var (sdm, value); +} diff --git a/src/ui/gui/psppire-select-dest.h b/src/ui/gui/psppire-select-dest.h new file mode 100644 index 00000000..74f01652 --- /dev/null +++ b/src/ui/gui/psppire-select-dest.h @@ -0,0 +1,49 @@ +/* PSPPIRE - a graphical user interface for PSPP. + Copyright (C) 2009 Free Software Foundation + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef __PSPPIRE_SELECT_DEST_H__ +#define __PSPPIRE_SELECT_DEST_H__ + +#include + +GType psppire_select_dest_widget_get_type (void) G_GNUC_CONST; + +#define PSPPIRE_TYPE_SELECT_DEST_WIDGET (psppire_select_dest_widget_get_type ()) +#define PSPPIRE_SELECT_DEST_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PSPPIRE_TYPE_SELECT_DEST_WIDGET, PsppireSelectDestWidget)) +#define PSPPIRE_IS_SELECT_DEST_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PSPPIRE_TYPE_SELECT_DEST_WIDGET)) + + +#define PSPPIRE_SELECT_DEST_GET_IFACE(obj) \ + (G_TYPE_INSTANCE_GET_INTERFACE ((obj), PSPPIRE_TYPE_SELECT_DEST_WIDGET, PsppireSelectDestWidgetIface)) + +typedef struct _PsppireSelectDestWidgetIface PsppireSelectDestWidgetIface; + + +typedef struct _PsppireSelectDestWidget PsppireSelectDestWidget; /* Dummy typedef */ + +struct _PsppireSelectDestWidgetIface +{ + GTypeInterface g_iface; + + /* Return TRUE iff DEST contains V */ + gboolean (*contains_var) (PsppireSelectDestWidget *dest, const GValue *v); +}; + + +gboolean psppire_select_dest_widget_contains_var (PsppireSelectDestWidget *m, const GValue *v); + +#endif diff --git a/src/ui/gui/psppire-selector.c b/src/ui/gui/psppire-selector.c index 2b2b2ed5..83fe2d82 100644 --- a/src/ui/gui/psppire-selector.c +++ b/src/ui/gui/psppire-selector.c @@ -60,8 +60,8 @@ #include "psppire-dictview.h" #include "psppire-var-view.h" - - +#include "psppire-dict.h" +#include "psppire-select-dest.h" #include #include @@ -651,31 +651,20 @@ on_activate (PsppireSelector *selector, gpointer data) } static gboolean -permissive_filter (GtkTreeModel *model, GtkTreeIter *iter, - PsppireSelector *selector) -{ - return FALSE; -} - -/* Default visibility filter for GtkTreeView DEST widget */ -static gboolean -is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter, - PsppireSelector *selector) +is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter, PsppireSelector *selector) { - GtkTreeModel *dest_model; - GtkTreeIter dest_iter; + gboolean result = FALSE; GtkTreeIter source_iter; - gint index; - GtkTreePath *path ; GtkTreeModel *source_model; + GValue value = {0}; - if ( GTK_IS_TREE_MODEL_FILTER (model) ) + if (GTK_IS_TREE_MODEL_FILTER (model)) { source_model = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model)); gtk_tree_model_filter_convert_iter_to_child_iter - ( GTK_TREE_MODEL_FILTER (model), &source_iter, iter ); + (GTK_TREE_MODEL_FILTER (model), &source_iter, iter); } else { @@ -683,40 +672,17 @@ is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter, source_iter = *iter; } - dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest)); - - path = gtk_tree_model_get_path (source_model, &source_iter); - - index = *gtk_tree_path_get_indices (path); - - gtk_tree_path_free (path); - - if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) ) - return FALSE; - - do - { - int x; - GValue value = {0}; - GValue int_value = {0}; - gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value); - - g_value_init (&int_value, G_TYPE_INT); + gtk_tree_model_get_value (source_model, &source_iter, DICT_TVM_COL_VAR, &value); - g_value_transform (&value, &int_value); + result = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (selector->dest), + &value); - x = g_value_get_int (&int_value); + g_value_unset (&value); - g_value_unset (&int_value); - g_value_unset (&value); + return result; +} - if ( x == index ) - return TRUE; - } - while (gtk_tree_model_iter_next (dest_model, &dest_iter)); - return FALSE; -} /* Visibility function for items in the SOURCE widget. Returns TRUE iff *all* the selectors for which SOURCE is associated @@ -751,7 +717,6 @@ static void set_tree_view_source (PsppireSelector *selector, GtkTreeView *source) { - GList *list = NULL; PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE); @@ -790,8 +755,6 @@ set_tree_view_source (PsppireSelector *selector, g_hash_table_replace (class->source_hash, source, list); } } - - } @@ -826,7 +789,7 @@ on_dest_data_delete (GtkTreeModel *tree_model, static void -xxx (PsppireSelector *selector) +on_dest_model_changed (PsppireSelector *selector) { GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest)); @@ -850,12 +813,12 @@ set_tree_view_dest (PsppireSelector *selector, g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select), selector); - + on_dest_model_changed (selector); g_signal_connect_swapped (dest, "notify::model", - G_CALLBACK (xxx), selector); - + G_CALLBACK (on_dest_model_changed), selector); } + /* Callback which causes the filter to be refiltered. Called when the DEST GtkEntry is activated (Enter is pressed), or when it looses focus. @@ -867,6 +830,7 @@ refilter (PsppireSelector *selector) return FALSE; } + /* Callback for when the DEST GtkEntry is selected (clicked) */ static gboolean on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data) @@ -880,7 +844,6 @@ on_entry_dest_select (GtkWidget *widget, GdkEventFocus *event, gpointer data) } - /* Callback for when an item disappears from the source list. By implication, this means that the item has been inserted into the destination. @@ -934,7 +897,7 @@ set_default_filter (PsppireSelector *selector) if ( selector->filter == NULL) { if (GTK_IS_TREE_VIEW (selector->dest)) - selector->filter = permissive_filter; + selector->filter = is_item_in_dest; } } diff --git a/src/ui/gui/psppire-var-view.c b/src/ui/gui/psppire-var-view.c index 2a9ebf2e..f7a017b3 100644 --- a/src/ui/gui/psppire-var-view.c +++ b/src/ui/gui/psppire-var-view.c @@ -20,6 +20,7 @@ #include #include "psppire-var-view.h" #include "psppire-var-ptr.h" +#include "psppire-select-dest.h" #include @@ -32,6 +33,35 @@ static void psppire_var_view_base_init (PsppireVarViewClass *class); static void psppire_var_view_class_init (PsppireVarViewClass *class); static void psppire_var_view_init (PsppireVarView *var_view); +/* Returns TRUE iff VV contains the item V. + V must be an initialised value containing a + PSPPIRE_VAR_PTR_TYPE. +*/ +static gboolean +var_view_contains_var (PsppireSelectDestWidget *sdm, const GValue *v) +{ + gboolean ok; + GtkTreeIter iter; + PsppireVarView *vv = PSPPIRE_VAR_VIEW (sdm); + g_return_val_if_fail (G_VALUE_HOLDS (v, PSPPIRE_VAR_PTR_TYPE), FALSE); + + for (ok = psppire_var_view_get_iter_first (vv, &iter); + ok; + ok = psppire_var_view_get_iter_next (vv, &iter)) + { + const struct variable *var = psppire_var_view_get_variable (vv, 0, &iter); + if (var == g_value_get_boxed (v)) + return TRUE; + } + + return FALSE; +} + +static void +model_init (PsppireSelectDestWidgetIface *iface) +{ + iface->contains_var = var_view_contains_var; +} GType psppire_var_view_get_type (void) @@ -53,9 +83,19 @@ psppire_var_view_get_type (void) (GInstanceInitFunc) psppire_var_view_init, }; + static const GInterfaceInfo var_view_model_info = { + (GInterfaceInitFunc) model_init, /* Fill this in */ + NULL, + NULL + }; + psppire_var_view_type = g_type_register_static (GTK_TYPE_TREE_VIEW, "PsppireVarView", &psppire_var_view_info, 0); + + g_type_add_interface_static (psppire_var_view_type, + PSPPIRE_TYPE_SELECT_DEST_WIDGET, + &var_view_model_info); } return psppire_var_view_type; @@ -290,27 +330,3 @@ psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string) return n_vars; } - -/* Returns TRUE iff VV contains the item V. - V must be an initialised value containing a - PSPPIRE_VAR_PTR_TYPE. -*/ -gboolean -psppire_var_view_contains_var (PsppireVarView *vv, const GValue *v) -{ - gboolean ok; - GtkTreeIter iter; - g_return_val_if_fail (G_VALUE_HOLDS (v, PSPPIRE_VAR_PTR_TYPE), FALSE); - - for (ok = psppire_var_view_get_iter_first (vv, &iter); - ok; - ok = psppire_var_view_get_iter_next (vv, &iter)) - { - const struct variable *var = psppire_var_view_get_variable (vv, 0, &iter); - if (var == g_value_get_boxed (v)) - return TRUE; - } - - return FALSE; -} - diff --git a/src/ui/gui/psppire-var-view.h b/src/ui/gui/psppire-var-view.h index 3509a46b..35dc91d2 100644 --- a/src/ui/gui/psppire-var-view.h +++ b/src/ui/gui/psppire-var-view.h @@ -47,7 +47,6 @@ struct _PsppireVarView GtkListStore *list; gint *nums; - }; struct _PsppireVarViewClass @@ -58,8 +57,6 @@ struct _PsppireVarViewClass GType psppire_var_view_get_type (void); -gboolean psppire_var_view_contains_var (PsppireVarView *vv, const GValue *v); - gint psppire_var_view_append_names (PsppireVarView *vv, gint column, GString *string); gboolean psppire_var_view_get_iter_first (PsppireVarView *vv, GtkTreeIter *iter); -- 2.30.2