X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fpsppire-selector.c;h=32e6ce126ca6ce621906cb042001b55c03b2bda8;hb=43f0e5bf6c53ee0ba31b259aab230861baa4eb7e;hp=36cae5c18afe9aed88b4743f9bbdba9c31758fcf;hpb=dcbac2a70dd54c1101cfe758d0282614c3def4a9;p=pspp diff --git a/src/ui/gui/psppire-selector.c b/src/ui/gui/psppire-selector.c index 36cae5c18a..32e6ce126c 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, 2009, 2010, 2012 Free Software Foundation + Copyright (C) 2007, 2009, 2010, 2012, 2015 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 @@ -19,7 +19,7 @@ This module provides a widget, PsppireSelector derived from GtkButton. - It contains a GtkArrow, and is used for selecting objects from a + It contains a GtkImage (to indicate the arrow), and is used for selecting objects from a GtkTreeView and putting them into a destination widget (often another GtkTreeView). Typically this is used in psppire for selecting variables, thus: @@ -59,16 +59,14 @@ #include #include "psppire-dictview.h" -#include "psppire-var-view.h" #include "psppire-dict.h" #include "psppire-select-dest.h" +#include "psppire-means-layer.h" #include #include "psppire-selector.h" -static void psppire_selector_base_finalize (PsppireSelectorClass *, gpointer); -static void psppire_selector_base_init (PsppireSelectorClass *class); static void psppire_selector_class_init (PsppireSelectorClass *class); static void psppire_selector_init (PsppireSelector *selector); @@ -113,8 +111,8 @@ psppire_selector_get_type (void) static const GTypeInfo psppire_selector_info = { sizeof (PsppireSelectorClass), - (GBaseInitFunc) psppire_selector_base_init, - (GBaseFinalizeFunc) psppire_selector_base_finalize, + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, (GClassInitFunc)psppire_selector_class_init, (GClassFinalizeFunc) NULL, NULL, @@ -133,18 +131,43 @@ psppire_selector_get_type (void) static GObjectClass * parent_class = NULL; + + +#define SELECTOR_DEBUGGING 0 + static void -psppire_selector_finalize (GObject *obj) +dump_hash_entry (gpointer key, gpointer value, gpointer obj) +{ + GList *item = NULL; + g_print ("Source %p; ", key); + + for (item = g_list_first (value); + item != NULL; + item = g_list_next (item)) + { + g_print ("%p(%p) ", item->data, item); + } + g_print ("\n"); +} + +/* This function is for debugging only */ +void +psppire_selector_show_map (PsppireSelector *obj) { - /* Chain up to the parent class */ - G_OBJECT_CLASS (parent_class)->finalize (obj); + PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE); + + g_print ("%s %p\n", __FUNCTION__, obj); + g_hash_table_foreach (class->source_hash, dump_hash_entry, obj); } + static void psppire_selector_dispose (GObject *obj) { PsppireSelector *sel = PSPPIRE_SELECTOR (obj); + PsppireSelectorClass *class = g_type_class_peek (PSPPIRE_SELECTOR_TYPE); + GList *list; if (sel->dispose_has_run) return; @@ -152,6 +175,21 @@ psppire_selector_dispose (GObject *obj) /* Make sure dispose does not run twice. */ sel->dispose_has_run = TRUE; + /* Remove ourself from the source map. If we are the only entry for this source + widget, that will result in an empty list for the source. In that case, we + remove the entire entry too. */ + if ((list = g_hash_table_lookup (class->source_hash, sel->source))) + { + GList *newlist = g_list_remove_link (list, sel->source_litem); + g_list_free (sel->source_litem); + if (newlist == NULL) + g_hash_table_remove (class->source_hash, sel->source); + else + g_hash_table_replace (class->source_hash, sel->source, newlist); + + sel->source_litem = NULL; + } + g_object_unref (sel->dest); g_object_unref (sel->source); @@ -175,9 +213,6 @@ static void on_click (GtkButton *b); static void on_realize (GtkWidget *selector); -static void update_subjects (PsppireSelector *selector); - - static void psppire_selector_set_property (GObject *object, guint prop_id, @@ -194,15 +229,15 @@ psppire_selector_set_property (GObject *object, break; case PROP_PRIMARY: selector->primary_requested = TRUE; - update_subjects (selector); + psppire_selector_update_subjects (selector); break; case PROP_SOURCE_WIDGET: selector->source = g_value_dup_object (value); - update_subjects (selector); + psppire_selector_update_subjects (selector); break; case PROP_DEST_WIDGET: selector->dest = g_value_dup_object (value); - update_subjects (selector); + psppire_selector_update_subjects (selector); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); @@ -319,31 +354,13 @@ psppire_selector_class_init (PsppireSelectorClass *class) G_TYPE_NONE, 0); - class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal); -} - - -static void -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); + class->default_selection_funcs = g_hash_table_new (g_direct_hash, g_direct_equal); } - -static void -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, @@ -419,8 +436,7 @@ psppire_selector_init (PsppireSelector *selector) selector->allow_selection = NULL; selector->filter = NULL; - selector->arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE); - + selector->arrow = gtk_image_new_from_icon_name ("pan-start-symbolic", GTK_ICON_SIZE_BUTTON); gtk_container_add (GTK_CONTAINER (selector), selector->arrow); @@ -435,6 +451,8 @@ psppire_selector_init (PsppireSelector *selector) selector->row_activate_id = 0; selector->source_select_id = 0; + + selector->source_litem = NULL; } @@ -457,16 +475,16 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d) switch (selector->orientation) { case PSPPIRE_SELECT_SOURCE_BEFORE_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL); + g_object_set (selector->arrow, "icon-name", "pan-end-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_AFTER_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL); + g_object_set (selector->arrow, "icon-name", "pan-start-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_ABOVE_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL); + g_object_set (selector->arrow, "icon-name", "pan-down-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_BELOW_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL); + g_object_set (selector->arrow, "icon-name", "pan-up-symbolic", NULL); break; default: g_assert_not_reached (); @@ -478,22 +496,21 @@ set_direction (PsppireSelector *selector, enum psppire_selector_dir d) switch (selector->orientation) { case PSPPIRE_SELECT_SOURCE_BEFORE_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_LEFT, NULL); + g_object_set (selector->arrow, "icon-name", "pan-start-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_AFTER_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_RIGHT, NULL); + g_object_set (selector->arrow, "icon-name", "pan-end-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_ABOVE_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_UP, NULL); + g_object_set (selector->arrow, "icon-name", "pan-up-symbolic", NULL); break; case PSPPIRE_SELECT_SOURCE_BELOW_DEST: - g_object_set (selector->arrow, "arrow-type", GTK_ARROW_DOWN, NULL); + g_object_set (selector->arrow, "icon-name", "pan-down-symbolic", NULL); break; default: g_assert_not_reached (); break; }; - } } @@ -508,6 +525,7 @@ on_dest_treeview_select (GtkTreeSelection *treeselection, gpointer data) set_direction (selector, PSPPIRE_SELECTOR_DEST_TO_SOURCE); } + /* Callback for source deselection, when the dest is GtkEntry */ static void de_select_selection_entry (PsppireSelector *selector) @@ -515,23 +533,47 @@ de_select_selection_entry (PsppireSelector *selector) gtk_entry_set_text (GTK_ENTRY (selector->dest), ""); } + +static void de_select_tree_model (GtkTreeSelection *selection, GtkTreeModel *model); + +/* Callback for source deselection, when the dest is PsppireMeansLayer */ +static void +de_select_selection_means_layer (PsppireSelector *selector) +{ + PsppireMeansLayer *ml = PSPPIRE_MEANS_LAYER (selector->dest); + GtkTreeView *tv = GTK_TREE_VIEW (ml->var_view); + GtkTreeSelection *selection = gtk_tree_view_get_selection (tv); + + GtkTreeModel *model = psppire_means_layer_get_model (ml); + + g_return_if_fail (selector->select_items); + + de_select_tree_model (selection, model); +} + /* Callback for source deselection, when the dest is GtkTreeView */ static void de_select_selection_tree_view (PsppireSelector *selector) { - GList *item; - GtkTreeSelection* selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->dest)); GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest)); + g_return_if_fail (selector->select_items); + + de_select_tree_model (selection, model); +} + +static void +de_select_tree_model (GtkTreeSelection *selection, GtkTreeModel *model) +{ + GList *item; + GList *selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL); - g_return_if_fail (selector->select_items); - /* Convert paths to RowRefs */ for (item = g_list_first (selected_rows); item != NULL; @@ -577,7 +619,8 @@ static gboolean refilter (PsppireSelector *selector) { GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source)); - gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model)); + if (GTK_IS_TREE_MODEL_FILTER (model)) + gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (model)); return FALSE; } @@ -593,6 +636,9 @@ de_select_selection (PsppireSelector *selector) else if ( GTK_IS_ENTRY (selector->dest)) de_select_selection_entry (selector); + else if ( PSPPIRE_IS_MEANS_LAYER (selector->dest)) + de_select_selection_means_layer (selector); + else g_assert_not_reached (); @@ -612,11 +658,11 @@ select_selection (PsppireSelector *selector) GtkTreeSelection* selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW (selector->source)); - GList *selected_rows = - gtk_tree_selection_get_selected_rows (selection, NULL); + GList *selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL); - GtkTreeModel *childmodel = - gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source)))); + GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source)); + + GtkTreeModel *childmodel = gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (model)); g_return_if_fail (selector->select_items); @@ -634,14 +680,12 @@ select_selection (PsppireSelector *selector) GtkTreeIter iter; GtkTreePath *path = item->data; - gtk_tree_model_get_iter (GTK_TREE_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))), - &iter, path); + g_return_if_fail (model); - gtk_tree_model_filter_convert_iter_to_child_iter - (GTK_TREE_MODEL_FILTER (gtk_tree_view_get_model (GTK_TREE_VIEW (selector->source))), - &child_iter, - &iter); + gtk_tree_model_get_iter (model, &iter, path); + gtk_tree_model_filter_convert_iter_to_child_iter (GTK_TREE_MODEL_FILTER (model), + &child_iter, &iter); selector->select_items (child_iter, selector->dest, childmodel, @@ -756,8 +800,15 @@ set_tree_view_source (PsppireSelector *selector) if ( ! (list = g_hash_table_lookup (class->source_hash, selector->source))) { + /* Base case: This widget is currently not the source of + any selector. Create a hash entry and make this selector + the first selector in the list */ + list = g_list_append (list, selector); g_hash_table_insert (class->source_hash, selector->source, list); + + /* Save the list item so that it can be removed later */ + selector->source_litem = list; } else { /* Append this selector to the list and push the @@ -766,9 +817,15 @@ set_tree_view_source (PsppireSelector *selector) if ( NULL == g_list_find (list, selector) ) { if ( selector->primary_requested ) - list = g_list_prepend (list, selector); + { + list = g_list_prepend (list, selector); + selector->source_litem = list; + } else - list = g_list_append (list, selector); + { + list = g_list_append (list, selector); + selector->source_litem = g_list_last (list); + } g_hash_table_replace (class->source_hash, selector->source, list); } } @@ -849,19 +906,31 @@ on_dest_data_delete (GtkTreeModel *tree_model, } +static void +remove_selector_handlers (PsppireSelector *selector, GObject *sel) +{ + g_signal_handlers_disconnect_by_data (sel, selector); +} + static void on_dest_model_changed (PsppireSelector *selector) { GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (selector->dest)); - if ( model ) - { - 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); - } + if (model == NULL) + return; + + 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); + + g_signal_connect (selector, "destroy", G_CALLBACK (remove_selector_handlers), model); + + if ( selector->selecting ) return; + + refilter (selector); } /* Set the destination widget to DEST */ @@ -882,7 +951,18 @@ set_tree_view_dest (PsppireSelector *selector, G_CALLBACK (on_dest_model_changed), selector); } +static void +set_layer_dest (PsppireSelector *selector, + PsppireMeansLayer *dest) +{ + GtkTreeSelection* selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dest->var_view)); + gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE); + + + g_signal_connect (selection, "changed", G_CALLBACK (on_dest_treeview_select), + selector); +} /* Callback for when the DEST GtkEntry is selected (clicked) */ @@ -930,8 +1010,8 @@ set_default_filter (PsppireSelector *selector) } -static void -update_subjects (PsppireSelector *selector) +void +psppire_selector_update_subjects (PsppireSelector *selector) { if ( NULL == selector->dest ) return; @@ -959,10 +1039,14 @@ update_subjects (PsppireSelector *selector) { set_tree_view_dest (selector, GTK_TREE_VIEW (selector->dest)); } - else if ( GTK_IS_ENTRY (selector->dest)) - set_entry_dest (selector, GTK_ENTRY (selector->dest)); - + { + set_entry_dest (selector, GTK_ENTRY (selector->dest)); + } + else if (PSPPIRE_IS_MEANS_LAYER (selector->dest)) + { + set_layer_dest (selector, PSPPIRE_MEANS_LAYER (selector->dest)); + } else if (GTK_IS_TEXT_VIEW (selector->dest)) { /* Nothing to be done */ @@ -1032,15 +1116,17 @@ GType psppire_selector_orientation_get_type (void) { static GType etype = 0; - if (etype == 0) { - static const GEnumValue values[] = { - { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" }, - { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" }, - { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" }, - { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" }, - { 0, NULL, NULL } - }; - etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values); - } + if (etype == 0) + { + static const GEnumValue values[] = + { + { PSPPIRE_SELECT_SOURCE_BEFORE_DEST, "PSPPIRE_SELECT_SOURCE_BEFORE_DEST", "source before destination" }, + { PSPPIRE_SELECT_SOURCE_AFTER_DEST, "PSPPIRE_SELECT_SOURCE_AFTER_DEST", "source after destination" }, + { PSPPIRE_SELECT_SOURCE_ABOVE_DEST, "PSPPIRE_SELECT_SOURCE_ABOVE_DEST", "source above destination" }, + { PSPPIRE_SELECT_SOURCE_BELOW_DEST, "PSPPIRE_SELECT_SOURCE_BELOW_DEST", "source below destination" }, + { 0, NULL, NULL } + }; + etype = g_enum_register_static (g_intern_static_string ("PsppireSelectorOrientation"), values); + } return etype; }