X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Fui%2Fgui%2Fpsppire-selector.c;h=7b30106013b21297cd9085a9008856b02de04511;hb=dc29c57e7908b0bdc6ab84f8aa4b7cb8a780026c;hp=9acc2829b22fd575fac9c1579dd08c1b47af02dd;hpb=833944dfd2ecf9dc78f7e9575f4900260df70164;p=pspp
diff --git a/src/ui/gui/psppire-selector.c b/src/ui/gui/psppire-selector.c
index 9acc2829b2..7b30106013 100644
--- a/src/ui/gui/psppire-selector.c
+++ b/src/ui/gui/psppire-selector.c
@@ -1,5 +1,5 @@
/* PSPPIRE - a graphical user interface for PSPP.
- Copyright (C) 2007 Free Software Foundation
+ Copyright (C) 2007, 2009, 2010 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
@@ -12,7 +12,8 @@
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 . */
+ along with this program. If not, see .
+*/
/*
This module provides a widget, PsppireSelector derived from
@@ -57,16 +58,14 @@
#include
-#include
-#include
-#include
+#include "psppire-dictview.h"
+#include "psppire-var-view.h"
+#include "psppire-dict.h"
+#include "psppire-select-dest.h"
-#include "psppire-selector.h"
+#include
-#include
-#include
-#include
-#include
+#include "psppire-selector.h"
static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer);
static void psppire_selector_base_init (PsppireSelectorClass *class);
@@ -112,21 +111,49 @@ psppire_selector_get_type (void)
return psppire_selector_type;
}
+static GObjectClass * parent_class = NULL;
static void
-psppire_selector_finalize (GObject *object)
+psppire_selector_finalize (GObject *obj)
{
+ /* Chain up to the parent class */
+ G_OBJECT_CLASS (parent_class)->finalize (obj);
}
+
+static void
+psppire_selector_dispose (GObject *obj)
+{
+ PsppireSelector *sel = PSPPIRE_SELECTOR (obj);
+
+ if (sel->dispose_has_run)
+ return;
+
+ /* Make sure dispose does not run twice. */
+ sel->dispose_has_run = TRUE;
+
+ g_object_unref (sel->dest);
+ g_object_unref (sel->source);
+
+ /* Chain up to the parent class */
+ G_OBJECT_CLASS (parent_class)->dispose (obj);
+}
+
+
/* Properties */
enum
{
PROP_0,
- PROP_ORIENTATION
+ PROP_ORIENTATION,
+ PROP_PRIMARY,
+ PROP_SOURCE_WIDGET,
+ PROP_DEST_WIDGET
};
-static void on_activate (PsppireSelector *selector, gpointer data);
+static void on_click (GtkButton *b);
+
+static void update_subjects (PsppireSelector *selector);
static void
@@ -143,6 +170,18 @@ psppire_selector_set_property (GObject *object,
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);
+ break;
+ case PROP_DEST_WIDGET:
+ selector->dest = g_value_dup_object (value);
+ update_subjects (selector);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -163,27 +202,57 @@ psppire_selector_get_property (GObject *object,
case PROP_ORIENTATION:
g_value_set_enum (value, selector->orientation);
break;
+ case PROP_SOURCE_WIDGET:
+ g_value_take_object (value, selector->source);
+ break;
+ case PROP_DEST_WIDGET:
+ g_value_take_object (value, selector->dest);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
};
}
-
-
static void
psppire_selector_class_init (PsppireSelectorClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
+ GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
GParamSpec *orientation_spec =
g_param_spec_enum ("orientation",
"Orientation",
"Where the selector is relative to its subjects",
- G_TYPE_PSPPIRE_SELECTOR_ORIENTATION,
+ PSPPIRE_TYPE_SELECTOR_ORIENTATION,
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",
+ "The widget to be used as the source for this selector",
+ GTK_TYPE_WIDGET,
+ G_PARAM_READWRITE);
+
+ GParamSpec *dest_widget_spec =
+ g_param_spec_object ("dest-widget",
+ "Destination Widget",
+ "The widget to be used as the destination for this selector",
+ GTK_TYPE_WIDGET,
+ G_PARAM_READWRITE);
+
+
+ button_class->clicked = on_click;
+
object_class->set_property = psppire_selector_set_property;
object_class->get_property = psppire_selector_get_property;
@@ -191,6 +260,20 @@ psppire_selector_class_init (PsppireSelectorClass *class)
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_object_class_install_property (object_class,
+ PROP_DEST_WIDGET,
+ dest_widget_spec);
+
+ parent_class = g_type_class_peek_parent (class);
+
signals [SELECTED] =
g_signal_new ("selected",
G_TYPE_FROM_CLASS (class),
@@ -210,6 +293,8 @@ psppire_selector_class_init (PsppireSelectorClass *class)
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0);
+
+ class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal);
}
@@ -219,6 +304,7 @@ psppire_selector_base_init (PsppireSelectorClass *class)
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = psppire_selector_finalize;
+ object_class->dispose = psppire_selector_dispose;
class->source_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
}
@@ -230,27 +316,101 @@ psppire_selector_base_finalize(PsppireSelectorClass *class,
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)
+{
+ on_click (GTK_BUTTON (data));
+}
+
+/* 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_widget_set_sensitive (GTK_WIDGET (selector),
+ selector->allow_selection (selector->source, selector->dest));
+ }
+ else if ( GTK_IS_ENTRY (selector->dest) )
+ {
+ gtk_widget_set_sensitive (GTK_WIDGET (selector),
+ 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->filter = NULL;
+
selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
selector->filtered_source = NULL;
- selector->action = gtk_action_new ("select", NULL, NULL, "pspp-stock-select");
-
- gtk_action_connect_proxy (selector->action, GTK_WIDGET (selector));
gtk_container_add (GTK_CONTAINER (selector), selector->arrow);
gtk_widget_show (selector->arrow);
- g_signal_connect_swapped (selector->action, "activate", G_CALLBACK (on_activate), selector);
-
selector->selecting = FALSE;
+
+ 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);
+
}
@@ -313,27 +473,6 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d)
}
}
-/* 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)
@@ -465,7 +604,9 @@ select_selection (PsppireSelector *selector)
selector->select_items (child_iter,
selector->dest,
- childmodel);
+ childmodel,
+ selector->select_user_data
+ );
}
g_list_foreach (selected_rows, (GFunc) gtk_tree_path_free, NULL);
@@ -478,24 +619,14 @@ select_selection (PsppireSelector *selector)
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.
*/
static void
-on_activate (PsppireSelector *selector, gpointer data)
+on_click (GtkButton *b)
{
+ PsppireSelector *selector = PSPPIRE_SELECTOR (b);
+
switch (selector->direction)
{
case PSPPIRE_SELECTOR_SOURCE_TO_DEST:
@@ -508,27 +639,27 @@ on_activate (PsppireSelector *selector, gpointer data)
g_assert_not_reached ();
break;
}
+
+ if (GTK_BUTTON_CLASS (parent_class)->clicked)
+ GTK_BUTTON_CLASS (parent_class)->clicked (b);
+
}
-/* 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
{
@@ -536,29 +667,17 @@ is_item_in_dest (GtkTreeModel *model, GtkTreeIter *iter,
source_iter = *iter;
}
- dest_model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
+ gtk_tree_model_get_value (source_model, &source_iter, DICT_TVM_COL_VAR, &value);
- path = gtk_tree_model_get_path (source_model, &source_iter);
+ result = psppire_select_dest_widget_contains_var (PSPPIRE_SELECT_DEST_WIDGET (selector->dest),
+ &value);
- index = *gtk_tree_path_get_indices (path);
+ g_value_unset (&value);
- gtk_tree_path_free (path);
-
- if ( ! gtk_tree_model_get_iter_first (dest_model, &dest_iter) )
- return FALSE;
+ return result;
+}
- do
- {
- GValue value = {0};
- gtk_tree_model_get_value (dest_model, &dest_iter, 0, &value);
- if ( g_value_get_int (&value) == 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
@@ -593,18 +712,16 @@ static void
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));
@@ -622,20 +739,17 @@ set_tree_view_source (PsppireSelector *selector,
{ /* Append this selector to the list and push the
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);
}
@@ -669,7 +783,17 @@ on_dest_data_delete (GtkTreeModel *tree_model,
}
+static void
+on_dest_model_changed (PsppireSelector *selector)
+{
+ GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest));
+
+ g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
+ selector);
+ g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
+ selector);
+}
/* Set the destination widget to DEST */
static void
@@ -677,21 +801,19 @@ set_tree_view_dest (PsppireSelector *selector,
GtkTreeView *dest)
{
GtkTreeSelection* selection = gtk_tree_view_get_selection (dest);
- GtkTreeModel *model = gtk_tree_view_get_model (dest);
+
gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select),
selector);
- g_signal_connect (model, "row-changed", G_CALLBACK (on_dest_data_change),
- selector);
-
- g_signal_connect (model, "row-deleted", G_CALLBACK (on_dest_data_delete),
- selector);
-
+ on_dest_model_changed (selector);
+ g_signal_connect_swapped (dest, "notify::model",
+ 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.
@@ -703,6 +825,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)
@@ -716,7 +839,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.
@@ -764,63 +886,118 @@ set_entry_dest (PsppireSelector *selector,
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;
+ }
+}
-/* Set SOURCE and DEST for this selector, and
- set SELECT_FUNC and FILTER_FUNC */
-void
-psppire_selector_set_subjects (PsppireSelector *selector,
- GtkWidget *source,
- GtkWidget *dest,
- SelectItemsFunc *select_func,
- FilterItemsFunc *filter_func )
+static void
+update_subjects (PsppireSelector *selector)
{
- g_assert(selector);
+ GtkTreeModel *model = NULL;
- selector->filter = filter_func ;
+ if ( NULL == selector->dest )
+ return;
+
+ set_default_filter (selector);
- selector->source = source;
- selector->dest = dest;
+ if ( NULL == selector->source )
+ return;
- if ( filter_func == NULL)
+ g_signal_connect_swapped (selector->source, "notify::model",
+ G_CALLBACK (update_subjects), selector);
+
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source));
+
+ if ( NULL == model)
+ return;
+
+
+ if ( GTK_IS_TREE_VIEW (selector->source))
+ set_tree_view_source (selector, GTK_TREE_VIEW (selector->source) );
+ else
+ g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (selector->source));
+
+ if ( NULL == selector->dest)
+ ;
+ else if ( GTK_IS_TREE_VIEW (selector->dest))
{
- if (GTK_IS_TREE_VIEW (dest))
- selector->filter = is_item_in_dest;
+ set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest));
}
- if ( GTK_IS_TREE_VIEW (source))
- set_tree_view_source (selector, GTK_TREE_VIEW (source) );
+ else if ( GTK_IS_ENTRY (selector->dest))
+ set_entry_dest (selector, GTK_ENTRY (selector->dest));
+
+ else if (GTK_IS_TEXT_VIEW (selector->dest))
+ {
+ /* Nothing to be done */
+ }
else
- g_error ("Unsupported source widget: %s", G_OBJECT_TYPE_NAME (source));
+ g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (selector->dest));
- g_assert ( GTK_IS_TREE_MODEL_FILTER (selector->filtered_source));
- if ( GTK_IS_TREE_VIEW (dest))
- set_tree_view_dest (selector, GTK_TREE_VIEW (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);
- else if ( GTK_IS_ENTRY (dest))
- set_entry_dest (selector, GTK_ENTRY (dest));
+ SelectItemsFunc *func =
+ g_hash_table_lookup (PSPPIRE_SELECTOR_CLASS (class)->default_selection_funcs, (gpointer) type);
- else if (GTK_IS_TEXT_VIEW (dest))
- {
- /* Nothing to be done */
+ 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);
+}
- else
- g_error ("Unsupported destination widget: %s", G_OBJECT_TYPE_NAME (dest));
+
+
+/* Set FILTER_FUNC for this selector */
+void
+psppire_selector_set_filter_func (PsppireSelector *selector,
+ FilterItemsFunc *filter_func)
+{
+ selector->filter = filter_func ;
+}
+
+
+/* Set SELECT_FUNC for this selector */
+void
+psppire_selector_set_select_func (PsppireSelector *selector,
+ SelectItemsFunc *select_func,
+ gpointer user_data)
+{
+ selector->select_user_data = user_data;
selector->select_items = select_func;
}
void
-psppire_selector_set_allow (PsppireSelector *selector , AllowSelectionFunc *allow)
+psppire_selector_set_allow (PsppireSelector *selector, AllowSelectionFunc *allow)
{
selector->allow_selection = allow;
}
-
GType
psppire_selector_orientation_get_type (void)
{