X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fui%2Fgui%2Fvar-type-dialog.c;h=7f1d4aeccdc6e187332045965438a54f537f2170;hb=5601f2c5999f82827af7e52e8745de9e277ae5fa;hp=cd6f690230f371dfb24ba832d46cbc1dd282ac06;hpb=471c983e61d5924356b1428ee87d467536caf395;p=pspp diff --git a/src/ui/gui/var-type-dialog.c b/src/ui/gui/var-type-dialog.c index cd6f690230..7f1d4aeccd 100644 --- a/src/ui/gui/var-type-dialog.c +++ b/src/ui/gui/var-type-dialog.c @@ -1,5 +1,5 @@ /* PSPPIRE - a graphical user interface for PSPP. - Copyright (C) 2005, 2006, 2010, 2011, 2012 Free Software Foundation + Copyright (C) 2005, 2006, 2010, 2011, 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 @@ -29,6 +29,7 @@ #include "data/variable.h" #include "libpspp/message.h" #include "ui/gui/builder-wrapper.h" +#include "ui/gui/psppire-format.h" #include "ui/gui/var-type-dialog.h" static const struct fmt_spec date_format[] = @@ -79,6 +80,12 @@ static const int cc_format[] = FMT_CCE, }; +static GObject *psppire_var_type_dialog_constructor (GType type, guint, + GObjectConstructParam *); +static void psppire_var_type_dialog_set_state (PsppireVarTypeDialog *); + +static void psppire_var_type_dialog_set_format (PsppireVarTypeDialog *dialog, + const struct fmt_spec *format); static int find_format (const struct fmt_spec *target, const struct fmt_spec formats[], int n_formats); @@ -86,23 +93,144 @@ static int find_format_type (int target, const int types[], int n_types); static void select_treeview_at_index (GtkTreeView *, int index); -static void update_width_decimals (const struct var_type_dialog *); -static void refresh_active_button (struct var_type_dialog *); +static void update_width_decimals (const PsppireVarTypeDialog *); +static void refresh_active_button (PsppireVarTypeDialog *); static void on_active_button_change (GtkToggleButton *, - struct var_type_dialog *); + PsppireVarTypeDialog *); +static void on_width_changed (GtkEntry *, PsppireVarTypeDialog *); +static void on_decimals_changed (GtkEntry *, PsppireVarTypeDialog *); + +G_DEFINE_TYPE (PsppireVarTypeDialog, + psppire_var_type_dialog, + PSPPIRE_TYPE_DIALOG); + +enum + { + PROP_0, + PROP_FORMAT + }; + +static void +psppire_var_type_dialog_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + PsppireVarTypeDialog *obj = PSPPIRE_VAR_TYPE_DIALOG (object); + + switch (prop_id) + { + case PROP_FORMAT: + psppire_var_type_dialog_set_format (obj, g_value_get_boxed (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +psppire_var_type_dialog_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + PsppireVarTypeDialog *obj = PSPPIRE_VAR_TYPE_DIALOG (object); + + switch (prop_id) + { + case PROP_FORMAT: + g_value_set_boxed (value, &obj->fmt_l); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +psppire_var_type_dialog_set_format (PsppireVarTypeDialog *dialog, + const struct fmt_spec *format) +{ + dialog->base_format = *format; + psppire_var_type_dialog_set_state (dialog); +} + +static const struct fmt_spec * +psppire_var_type_dialog_get_format (const PsppireVarTypeDialog *dialog) +{ + return &dialog->fmt_l; +} + +static void +psppire_var_type_dialog_init (PsppireVarTypeDialog *obj) +{ + /* We do all of our work on widgets in the constructor function, because that + runs after the construction properties have been set. Otherwise + PsppireDialog's "orientation" property hasn't been set and therefore we + have no box to populate. */ + obj->base_format = F_8_0; + obj->fmt_l = F_8_0; +} + +static void +psppire_var_type_dialog_class_init (PsppireVarTypeDialogClass *class) +{ + GObjectClass *gobject_class; + gobject_class = G_OBJECT_CLASS (class); + + gobject_class->constructor = psppire_var_type_dialog_constructor; + gobject_class->set_property = psppire_var_type_dialog_set_property; + gobject_class->get_property = psppire_var_type_dialog_get_property; + + g_object_class_install_property ( + gobject_class, PROP_FORMAT, + g_param_spec_boxed ("format", + "Format", + "The format being edited.", + PSPPIRE_TYPE_FORMAT, + G_PARAM_READABLE | G_PARAM_WRITABLE)); +} + +PsppireVarTypeDialog * +psppire_var_type_dialog_new (const struct fmt_spec *format) +{ + return PSPPIRE_VAR_TYPE_DIALOG ( + g_object_new (PSPPIRE_TYPE_VAR_TYPE_DIALOG, + "format", format, + NULL)); +} + +void +psppire_var_type_dialog_run (GtkWindow *parent_window, + struct fmt_spec *format) +{ + PsppireVarTypeDialog *dialog; + + dialog = psppire_var_type_dialog_new (format); + gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window); + gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); + gtk_widget_show (GTK_WIDGET (dialog)); + + if (psppire_dialog_run (PSPPIRE_DIALOG (dialog)) == GTK_RESPONSE_OK) + *format = *psppire_var_type_dialog_get_format (dialog); + + gtk_widget_destroy (GTK_WIDGET (dialog)); +} + /* callback for when any of the radio buttons are toggled */ static void on_toggle (GtkToggleButton *togglebutton, gpointer dialog_) { - struct var_type_dialog *dialog = dialog_; + PsppireVarTypeDialog *dialog = dialog_; if ( gtk_toggle_button_get_active (togglebutton) == TRUE) refresh_active_button (dialog); } static void -refresh_active_button (struct var_type_dialog *dialog) +refresh_active_button (PsppireVarTypeDialog *dialog) { int i; @@ -124,10 +252,30 @@ refresh_active_button (struct var_type_dialog *dialog) g_return_if_reached (); } +static void +update_adj_ranges (PsppireVarTypeDialog *dialog) +{ + enum fmt_type type = dialog->fmt_l.type; + const enum fmt_use use = FMT_FOR_OUTPUT; + int min_w = fmt_min_width (type, use); + int max_w = fmt_max_width (type, use); + int max_d = fmt_max_decimals (type, max_w, use); + + g_object_set (dialog->adj_width, + "lower", (double) min_w, + "upper", (double) max_w, + NULL); + + g_object_set (dialog->adj_decimals, + "lower", 0.0, + "upper", (double) max_d, + NULL); +} + /* callback for when any of the radio buttons are toggled */ static void on_active_button_change (GtkToggleButton *togglebutton, - struct var_type_dialog *dialog) + PsppireVarTypeDialog *dialog) { enum widgets { W_WIDTH = 1 << 0, @@ -181,7 +329,7 @@ on_active_button_change (GtkToggleButton *togglebutton, gtk_widget_set_visible (dialog->dollar_window, (widgets & W_DOLLAR_FORMATS) != 0); - dialog->fmt_l = *var_get_print_format (dialog->pv); + dialog->fmt_l = dialog->base_format; switch (dialog->active_button) { @@ -221,15 +369,10 @@ on_active_button_change (GtkToggleButton *togglebutton, } fmt_fix_output (&dialog->fmt_l); + update_adj_ranges (dialog); update_width_decimals (dialog); } - - -static gint on_var_type_ok_clicked (GtkWidget *w, gpointer data); -static gint hide_dialog (GtkWidget *w, gpointer data); - - static void add_to_group (GtkWidget *w, gpointer data) { @@ -240,18 +383,26 @@ add_to_group (GtkWidget *w, gpointer data) /* Set the local width and decimals entry boxes to reflec the local format */ static void -update_width_decimals (const struct var_type_dialog *dialog) +update_width_decimals (const PsppireVarTypeDialog *dialog) { - gchar *text; - g_assert (dialog); + gtk_adjustment_set_value (dialog->adj_width, dialog->fmt_l.w); + gtk_adjustment_set_value (dialog->adj_decimals, dialog->fmt_l.d); +} - text = g_strdup_printf ("%d", dialog->fmt_l.w); - gtk_entry_set_text (GTK_ENTRY (dialog->entry_width), text); - g_free (text); +static void +on_width_changed (GtkEntry *entry, PsppireVarTypeDialog *dialog) +{ + int w = atoi (gtk_entry_get_text (GTK_ENTRY (dialog->entry_width))); + fmt_change_width (&dialog->fmt_l, w, FMT_FOR_OUTPUT); + update_width_decimals (dialog); +} - text = g_strdup_printf ("%d", dialog->fmt_l.d); - gtk_entry_set_text (GTK_ENTRY (dialog->entry_decimals), text); - g_free (text); +static void +on_decimals_changed (GtkEntry *entry, PsppireVarTypeDialog *dialog) +{ + int d = atoi (gtk_entry_get_text (GTK_ENTRY (dialog->entry_decimals))); + fmt_change_decimals (&dialog->fmt_l, d, FMT_FOR_OUTPUT); + update_width_decimals (dialog); } /* Callback for when the custom treeview row is changed. @@ -261,7 +412,7 @@ preview_custom (GtkWidget *w, gpointer data) { const gchar *text ; - struct var_type_dialog *dialog = data; + PsppireVarTypeDialog *dialog = data; if ( dialog->active_button != BUTTON_CUSTOM ) return; @@ -296,72 +447,97 @@ preview_custom (GtkWidget *w, gpointer data) msg_enable (); } -/* Callback for when a treeview row is changed. - It sets the fmt_l_spec to reflect the selected format */ -static void -set_format_from_treeview (GtkTreeView *treeview, gpointer data) +static gint +get_index_from_treeview (GtkTreeView *treeview) { - struct var_type_dialog *dialog = data; - GtkTreeIter iter ; - GValue the_value = {0}; - - GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview); + GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview); + GtkTreeModel *model; + GtkTreePath *path; + GtkTreeIter iter; + gint index; - GtkTreeModel * model = gtk_tree_view_get_model (treeview); + if (selection == NULL) + return -1; - gtk_tree_selection_get_selected (sel, &model, &iter); + gtk_tree_selection_get_selected (selection, &model, &iter); + path = gtk_tree_model_get_path (model, &iter); + if (!path || gtk_tree_path_get_depth (path) < 1) + index = 0; + else + index = gtk_tree_path_get_indices (path)[0]; + gtk_tree_path_free (path); - gtk_tree_model_get_value (model, &iter, 1, &the_value); + return index; +} - dialog->fmt_l = *(struct fmt_spec *) g_value_get_pointer (&the_value); +/* Callback for when a date treeview row is changed. + It sets the fmt_l_spec to reflect the selected format */ +static void +set_date_format_from_treeview (GtkTreeView *treeview, + PsppireVarTypeDialog *dialog) +{ + gint idx = get_index_from_treeview (treeview); + if (idx < 0) + return; - g_value_unset (&the_value); + dialog->fmt_l = date_format[idx]; } +/* Callback for when a dollar treeview row is changed. + It sets the fmt_l_spec to reflect the selected format */ +static void +set_dollar_format_from_treeview (GtkTreeView *treeview, + PsppireVarTypeDialog *dialog) +{ + gint idx = get_index_from_treeview (treeview); + if (idx < 0) + return; + + dialog->fmt_l = dollar_format[idx]; +} /* Callback for when a treeview row is changed. It sets the type of the fmt_l to reflect the selected type */ static void -set_format_type_from_treeview (GtkTreeView *treeview, gpointer data) +set_custom_format_from_treeview (GtkTreeView *treeview, + PsppireVarTypeDialog *dialog) { - static struct fmt_spec custom_format = {0,0,0}; - struct var_type_dialog *dialog = data; - GtkTreeIter iter ; - GValue the_value = {0}; - - GtkTreeSelection* sel = gtk_tree_view_get_selection (treeview); - - GtkTreeModel * model = gtk_tree_view_get_model (treeview); - - gtk_tree_selection_get_selected (sel, &model, &iter); - - gtk_tree_model_get_value (model, &iter, 1, &the_value); - - dialog->fmt_l = custom_format; - dialog->fmt_l.type = g_value_get_int (&the_value); + gint idx = get_index_from_treeview (treeview); + if (idx < 0) + return; - g_value_unset (&the_value); + dialog->fmt_l.type = cc_format[idx]; + update_adj_ranges (dialog); + fmt_fix_output (&dialog->fmt_l); + update_width_decimals (dialog); } - /* Create the structure */ -struct var_type_dialog * -var_type_dialog_create (GtkWindow *toplevel) +static GObject * +psppire_var_type_dialog_constructor (GType type, + guint n_properties, + GObjectConstructParam *properties) { + PsppireVarTypeDialog *dialog; + GtkContainer *content_area; + GtkBuilder *xml; + GObject *obj; gint i; - struct var_type_dialog *dialog = g_malloc (sizeof (struct var_type_dialog)); - GtkBuilder *xml = builder_new ("var-type-dialog.ui"); + obj = G_OBJECT_CLASS (psppire_var_type_dialog_parent_class)->constructor ( + type, n_properties, properties); + dialog = PSPPIRE_VAR_TYPE_DIALOG (obj); - dialog->window = get_widget_assert (xml,"var_type_dialog"); - dialog->active_button = -1; + xml = builder_new ("var-type-dialog.ui"); + content_area = GTK_CONTAINER (PSPPIRE_DIALOG (dialog)); + gtk_container_add (GTK_CONTAINER (content_area), + get_widget_assert (xml, "var-type-dialog")); - g_signal_connect (dialog->window, "delete-event", - G_CALLBACK (gtk_widget_hide_on_delete), NULL); + dialog->active_button = -1; - gtk_window_set_transient_for (GTK_WINDOW (dialog->window), - toplevel); + g_signal_connect (dialog, "delete-event", + G_CALLBACK (gtk_widget_hide_on_delete), NULL); dialog->radioButton[BUTTON_NUMERIC] = get_widget_assert (xml,"radiobutton1"); @@ -385,13 +561,16 @@ var_type_dialog_create (GtkWindow *toplevel) dialog->width_decimals = get_widget_assert (xml, "width_decimals"); dialog->label_decimals = get_widget_assert (xml, "decimals_label"); dialog->entry_decimals = get_widget_assert (xml, "decimals_entry"); + dialog->adj_decimals = gtk_spin_button_get_adjustment ( + GTK_SPIN_BUTTON (dialog->entry_decimals)); dialog->label_psample = get_widget_assert (xml, "psample_label"); dialog->label_nsample = get_widget_assert (xml, "nsample_label"); dialog->entry_width = get_widget_assert (xml,"width_entry"); - + dialog->adj_width = gtk_spin_button_get_adjustment ( + GTK_SPIN_BUTTON (dialog->entry_width)); dialog->custom_currency_hbox = get_widget_assert (xml, "custom_currency_hbox"); @@ -403,8 +582,6 @@ var_type_dialog_create (GtkWindow *toplevel) GTK_TREE_VIEW (get_widget_assert (xml, "custom_treeview")); - dialog->ok = get_widget_assert (xml,"var_type_ok"); - { GtkTreeIter iter; @@ -443,7 +620,7 @@ var_type_dialog_create (GtkWindow *toplevel) column); - list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_POINTER); + list_store = gtk_list_store_new (1, G_TYPE_STRING); for ( i = 0 ; i < sizeof (date_format) / sizeof (date_format[0]) ; ++i ) { @@ -451,7 +628,6 @@ var_type_dialog_create (GtkWindow *toplevel) gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, 0, fmt_date_template (f->type, f->w), - 1, f, -1); } @@ -461,7 +637,7 @@ var_type_dialog_create (GtkWindow *toplevel) g_object_unref (list_store); g_signal_connect (dialog->date_format_treeview, "cursor-changed", - G_CALLBACK (set_format_from_treeview), dialog); + G_CALLBACK (set_date_format_from_treeview), dialog); /* populate the dollar treeview */ @@ -478,8 +654,7 @@ var_type_dialog_create (GtkWindow *toplevel) column); - list_store = gtk_list_store_new (2, G_TYPE_STRING, - G_TYPE_POINTER); + list_store = gtk_list_store_new (1, G_TYPE_STRING); for ( i = 0 ; i < sizeof (dollar_format)/sizeof (dollar_format[0]) ; ++i ) { @@ -487,7 +662,6 @@ var_type_dialog_create (GtkWindow *toplevel) gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, 0, template, - 1, &dollar_format[i], -1); free (template); } @@ -499,7 +673,7 @@ var_type_dialog_create (GtkWindow *toplevel) g_signal_connect (dialog->dollar_treeview, "cursor-changed", - G_CALLBACK (set_format_from_treeview), dialog); + G_CALLBACK (set_dollar_format_from_treeview), dialog); g_signal_connect_swapped (dialog->dollar_treeview, "cursor-changed", @@ -520,7 +694,7 @@ var_type_dialog_create (GtkWindow *toplevel) column); - list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT); + list_store = gtk_list_store_new (1, G_TYPE_STRING); for ( i = 0 ; i < 5 ; ++i ) { @@ -528,7 +702,6 @@ var_type_dialog_create (GtkWindow *toplevel) gtk_list_store_append (list_store, &iter); gtk_list_store_set (list_store, &iter, 0, fmt_name (cc_fmts[i]), - 1, cc_format[i], -1); } @@ -540,7 +713,7 @@ var_type_dialog_create (GtkWindow *toplevel) g_signal_connect (dialog->custom_treeview, "cursor-changed", - G_CALLBACK (set_format_type_from_treeview), dialog); + G_CALLBACK (set_custom_format_from_treeview), dialog); g_signal_connect (dialog->custom_treeview, @@ -548,6 +721,11 @@ var_type_dialog_create (GtkWindow *toplevel) G_CALLBACK (preview_custom), dialog); + g_signal_connect (dialog->entry_width, "changed", + G_CALLBACK (on_width_changed), dialog); + g_signal_connect (dialog->entry_decimals, "changed", + G_CALLBACK (on_decimals_changed), dialog); + g_signal_connect (dialog->entry_width, "changed", G_CALLBACK (preview_custom), dialog); @@ -557,27 +735,19 @@ var_type_dialog_create (GtkWindow *toplevel) "changed", G_CALLBACK (preview_custom), dialog); - - /* Connect to the OK button */ - g_signal_connect (dialog->ok, "clicked", G_CALLBACK (on_var_type_ok_clicked), - dialog); - - - /* And the cancel button */ - g_signal_connect (get_widget_assert (xml, "var_type_cancel") , "clicked", - G_CALLBACK (hide_dialog), - dialog); } g_object_unref (xml); - return dialog; + psppire_var_type_dialog_set_state (dialog); + + return obj; } /* Set a particular button to be active */ void -var_type_dialog_set_active_button (struct var_type_dialog *dialog, gint b) +var_type_dialog_set_active_button (PsppireVarTypeDialog *dialog, gint b) { gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->radioButton[b]), TRUE); @@ -622,15 +792,14 @@ find_format_type (int target, const int types[], int n_types) /* Set up the state of the dialog box to match the variable VAR */ static void -var_type_dialog_set_state (struct var_type_dialog *dialog) +psppire_var_type_dialog_set_state (PsppireVarTypeDialog *dialog) { int button; - g_assert (dialog); - g_assert (dialog->pv); + g_return_if_fail (dialog != NULL); /* Populate the radio button states */ - switch (var_get_print_format (dialog->pv)->type) + switch (dialog->base_format.type) { default: case FMT_F: @@ -677,109 +846,3 @@ var_type_dialog_set_state (struct var_type_dialog *dialog) on_active_button_change (GTK_TOGGLE_BUTTON (dialog->radioButton[button]), dialog); } - - -/* Popup the dialog box */ -void -var_type_dialog_show (struct var_type_dialog *dialog) -{ - var_type_dialog_set_state (dialog); - - gtk_widget_show (dialog->window); -} - -/* Fills F with an output format specification with type TYPE, width - W, and D decimals. Iff it's a valid format, then return true. -*/ -static bool -make_output_format_try (struct fmt_spec *f, int type, int w, int d) -{ - f->type = type; - f->w = w; - f->d = d; - return fmt_check_output (f); -} - - - - -/* Callbacks for the Variable Type Dialog Box */ - -/* Callback for when the var type dialog is closed using the OK button. - It sets the appropriate variable accordingly. */ -static gint -on_var_type_ok_clicked (GtkWidget *w, gpointer data) -{ - struct var_type_dialog *dialog = data; - - g_assert (dialog); - g_assert (dialog->pv); - - { - gint width = atoi (gtk_entry_get_text - (GTK_ENTRY (dialog->entry_width))); - - gint decimals = atoi (gtk_entry_get_text - (GTK_ENTRY (dialog->entry_decimals))); - - gint new_width = 0; - bool result = false; - struct fmt_spec spec; - switch (dialog->active_button) - { - case BUTTON_STRING: - new_width = width; - result = make_output_format_try (&spec, FMT_A, width, 0); - break; - case BUTTON_NUMERIC: - result = make_output_format_try (&spec, FMT_F, width, decimals); - break; - case BUTTON_COMMA: - result = make_output_format_try (&spec, FMT_COMMA, width, decimals); - break; - case BUTTON_DOT: - result = make_output_format_try (&spec, FMT_DOT, width, decimals); - break; - case BUTTON_SCIENTIFIC: - result = make_output_format_try (&spec, FMT_E, width, decimals); - break; - case BUTTON_DATE: - case BUTTON_CUSTOM: - if (! fmt_check_output (&dialog->fmt_l)) - g_critical ("Invalid variable format"); - else - result = memcpy (&spec, &dialog->fmt_l, sizeof (struct fmt_spec)); - break; - case BUTTON_DOLLAR: - result = make_output_format_try (&spec, FMT_DOLLAR, width, decimals); - break; - default: - g_critical ("Unknown variable type: %d", dialog->active_button) ; - result = false; - break; - } - - if ( result == true ) - { - var_set_width (dialog->pv, new_width); - var_set_both_formats (dialog->pv, &spec); - } - - } - gtk_widget_hide (dialog->window); - - return FALSE; -} - - - -static gint -hide_dialog (GtkWidget *w, gpointer data) -{ - struct var_type_dialog *dialog = data; - - gtk_widget_hide (dialog->window); - - return FALSE; -} -