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 <http://www.gnu.org/licenses/>. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
/*
This module provides a widget, PsppireSelector derived from
#include <config.h>
+#include "psppire-dictview.h"
+#include "psppire-var-view.h"
+#include "psppire-dict.h"
+#include "psppire-select-dest.h"
+
#include <gtk/gtksignal.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtkentry.h>
{
PROP_0,
PROP_ORIENTATION,
+ PROP_PRIMARY,
PROP_SOURCE_WIDGET,
PROP_DEST_WIDGET
};
selector->orientation = g_value_get_enum (value);
set_direction (selector, selector->direction);
break;
+ case PROP_PRIMARY:
+ selector->primary_requested = TRUE;
+ update_subjects (selector);
+ break;
case PROP_SOURCE_WIDGET:
selector->source = g_value_dup_object (value);
update_subjects (selector);
};
}
-
-
static void
psppire_selector_class_init (PsppireSelectorClass *class)
{
PSPPIRE_SELECT_SOURCE_BEFORE_DEST /* default value */,
G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
+
+ /* Meaningfull only if more than one selector shares this selectors source */
+ GParamSpec *primary_spec =
+ g_param_spec_boolean ("primary",
+ "Primary",
+ "Whether this selector should be the primary selector for the source",
+ FALSE,
+ G_PARAM_READWRITE);
+
GParamSpec *source_widget_spec =
g_param_spec_object ("source-widget",
"Source Widget",
PROP_ORIENTATION,
orientation_spec);
+ g_object_class_install_property (object_class,
+ PROP_PRIMARY,
+ primary_spec);
+
g_object_class_install_property (object_class,
PROP_SOURCE_WIDGET,
source_widget_spec);
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
+
+ class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal);
}
gpointer class_data)
{
g_hash_table_destroy (class->source_hash);
+ g_hash_table_destroy (class->default_selection_funcs);
}
+/* Callback for when the source treeview is activated (double clicked) */
+static void
+on_row_activate (GtkTreeView *tree_view,
+ GtkTreePath *path,
+ GtkTreeViewColumn *column,
+ gpointer data)
+{
+ PsppireSelector *selector = data;
+
+ gtk_action_activate (selector->action);
+}
+
+/* Callback for when the source selection changes */
+static void
+on_source_select (GtkTreeSelection *treeselection, gpointer data)
+{
+ PsppireSelector *selector = data;
+
+ set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
+
+ if ( selector->allow_selection )
+ {
+ gtk_action_set_sensitive (selector->action,
+ selector->allow_selection (selector->source, selector->dest));
+ }
+ else if ( GTK_IS_ENTRY (selector->dest) )
+ {
+ gtk_action_set_sensitive (selector->action,
+ gtk_tree_selection_count_selected_rows
+ (treeselection) <= 1 );
+ }
+}
+
+
+static void
+on_realize (PsppireSelector *selector)
+{
+ PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+ GtkTreeSelection* selection ;
+
+ GList *list = g_hash_table_lookup (class->source_hash, selector->source);
+
+ if ( NULL == list)
+ return;
+
+ if ( g_list_first (list)->data == selector)
+ {
+ if ( selector->row_activate_id )
+ g_signal_handler_disconnect (selector->source, selector->row_activate_id);
+
+ selector->row_activate_id =
+ g_signal_connect (selector->source, "row-activated", G_CALLBACK (on_row_activate), selector);
+ }
+
+ selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (selector->source));
+
+ if ( selector->source_select_id )
+ g_signal_handler_disconnect (selection, selector->source_select_id);
+
+ selector->source_select_id =
+ g_signal_connect (selection, "changed", G_CALLBACK (on_source_select), selector);
+}
static void
psppire_selector_init (PsppireSelector *selector)
{
+ selector->primary_requested = FALSE;
selector->select_user_data = NULL;
selector->select_items = NULL;
selector->allow_selection = NULL;
selector->source = NULL;
selector->dest = NULL;
selector->dispose_has_run = FALSE;
+
+
+ selector->row_activate_id = 0;
+ selector->source_select_id = 0;
+
+ g_signal_connect (selector, "realize",
+ G_CALLBACK (on_realize), NULL);
+
}
}
}
-/* Callback for when the source selection changes */
-static void
-on_source_select (GtkTreeSelection *treeselection, gpointer data)
-{
- PsppireSelector *selector = data;
-
- set_direction (selector, PSPPIRE_SELECTOR_SOURCE_TO_DEST);
-
- if ( selector->allow_selection )
- {
- gtk_action_set_sensitive (selector->action,
- selector->allow_selection (selector->source, selector->dest));
- }
- else if ( GTK_IS_ENTRY (selector->dest) )
- {
- gtk_action_set_sensitive (selector->action,
- gtk_tree_selection_count_selected_rows
- (treeselection) <= 1 );
- }
-}
-
/* Callback for when the destination treeview selection changes */
static void
on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data)
selector->selecting = FALSE;
}
-/* Callback for when the source treeview is activated (double clicked) */
-static void
-on_row_activate (GtkTreeView *tree_view,
- GtkTreePath *path,
- GtkTreeViewColumn *column,
- gpointer data)
-{
- PsppireSelector *selector = data;
-
- gtk_action_activate (selector->action);
-}
-
/* Callback for when the selector button is clicked,
or other event which causes the selector's action to occur.
*/
}
}
-/* 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
{
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_model_get_value (source_model, &source_iter, DICT_TVM_COL_VAR, &value);
- gtk_tree_path_free (path);
+ result = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (selector->dest),
+ &value);
- if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
- return FALSE;
+ g_value_unset (&value);
- 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);
-
- g_value_transform (&value, &int_value);
-
- x = g_value_get_int (&int_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
set_tree_view_source (PsppireSelector *selector,
GtkTreeView *source)
{
- GtkTreeSelection* selection ;
GList *list = NULL;
PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE);
+
+ GtkTreeModel *model = gtk_tree_view_get_model (source);
if ( ! (list = g_hash_table_lookup (class->source_hash, source)))
{
selector->filtered_source =
- GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new
- (gtk_tree_view_get_model (source), NULL));
-
- gtk_tree_view_set_model (source, NULL);
+ GTK_TREE_MODEL_FILTER (gtk_tree_model_filter_new (model, NULL));
gtk_tree_view_set_model (source,
GTK_TREE_MODEL (selector->filtered_source));
{ /* Append this selector to the list and push the <source,list>
pair onto the hash table */
- selector->filtered_source = GTK_TREE_MODEL_FILTER (
- gtk_tree_view_get_model (source));
+ selector->filtered_source = GTK_TREE_MODEL_FILTER (model);
- list = g_list_append (list, selector);
- g_hash_table_replace (class->source_hash, source, list);
+ if ( NULL == g_list_find (list, selector) )
+ {
+ if ( selector->primary_requested )
+ list = g_list_prepend (list, selector);
+ else
+ list = g_list_append (list, selector);
+ g_hash_table_replace (class->source_hash, source, list);
+ }
}
-
- selection = gtk_tree_view_get_selection (source);
-
- g_signal_connect (source, "row-activated", G_CALLBACK (on_row_activate),
- selector);
-
- g_signal_connect (selection, "changed", G_CALLBACK (on_source_select),
- selector);
}
static void
-xxx (PsppireSelector *selector)
+on_dest_model_changed (PsppireSelector *selector)
{
GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
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.
return FALSE;
}
+
/* Callback for when the DEST GtkEntry is selected (clicked) */
static gboolean
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.
G_CALLBACK (on_row_inserted), selector);
}
+static void
+set_default_filter (PsppireSelector *selector)
+{
+ if ( selector->filter == NULL)
+ {
+ if (GTK_IS_TREE_VIEW (selector->dest))
+ selector->filter = is_item_in_dest;
+ }
+}
+
static void
update_subjects (PsppireSelector *selector)
{
if ( NULL == selector->dest )
return;
- if ( selector->filter == NULL)
- {
- if (GTK_IS_TREE_VIEW (selector->dest))
- selector->filter = is_item_in_dest;
- }
+ set_default_filter (selector);
if ( NULL == selector->source )
return;
else
g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
+
+ /* FIXME: Remove this dependency */
+ if ( PSPPIRE_IS_DICT_VIEW (selector->source) )
+ {
+ GObjectClass *class = G_OBJECT_GET_CLASS (selector);
+ GType type = G_OBJECT_TYPE (selector->dest);
+
+ SelectItemsFunc *func =
+ g_hash_table_lookup (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type);
+
+ if ( func )
+ psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
+ func, NULL);
+ }
+}
+
+
+void
+psppire_selector_set_default_selection_func (GType type, SelectItemsFunc *func)
+{
+ GObjectClass *class = g_type_class_ref (PSPPIRE_SELECTOR_TYPE);
+
+ g_hash_table_insert (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type, func);
+
+ g_type_class_unref (class);
}
+
+
/* Set FILTER_FUNC for this selector */
void
psppire_selector_set_filter_func (PsppireSelector *selector,
FilterItemsFunc *filter_func)
{
selector->filter = filter_func ;
-
- if ( selector->filter == NULL)
- {
- if (GTK_IS_TREE_VIEW (selector->dest))
- selector->filter = is_item_in_dest;
- }
}
}
-
GType
psppire_selector_orientation_get_type (void)
{