New menu: Edit|Options
[pspp] / src / ui / gui / options-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2017 Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "options-dialog.h"
20
21 #include "ui/gui/helper.h"
22 #include "ui/gui/psppire-conf.h"
23 #include "ui/gui/builder-wrapper.h"
24 #include "ui/gui/psppire-data-window.h"
25 #include "ui/gui/psppire-dialog.h"
26
27 #include <gtk/gtk.h>
28
29 #include <gettext.h>
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33
34 struct options_dialog
35 {
36   GtkBuilder *xml;
37   GtkWidget *show_labels;
38   GtkWidget *show_names;
39   PsppireConf *conf;
40
41   GtkWidget *sort_names;
42   GtkWidget *sort_labels;
43   GtkWidget *sort_none;
44 };
45
46 GType
47 pspp_options_var_order_get_type (void)
48 {
49   static GType etype = 0;
50   if (G_UNLIKELY(etype == 0)) {
51     static const GEnumValue values[] =
52       {
53         { PSPP_OPTIONS_VAR_ORDER_UNSORTED, "PSPP_OPTIONS_VAR_ORDER_UNSORTED", "unsorted" },
54         { PSPP_OPTIONS_VAR_ORDER_NAME,     "PSPP_OPTIONS_VAR_ORDER_NAME",     "name" },
55         { PSPP_OPTIONS_VAR_ORDER_LABEL,    "PSPP_OPTIONS_VAR_ORDER_LABEL",    "label" },
56         { 0, NULL, NULL }
57       };
58     etype = g_enum_register_static (g_intern_static_string ("PsppOptionsVarOrder"), values);
59   }
60   return etype;
61 }
62
63 /*
64    Pops up the Options dialog box
65  */
66 void
67 options_dialog (PsppireDataWindow *de)
68 {
69   struct options_dialog fd;
70
71   GtkWidget *dialog ;
72
73   gboolean disp_labels = true;
74
75   fd.xml = builder_new ("options.ui");
76
77   dialog = get_widget_assert (fd.xml, "options-dialog");
78
79   fd.show_labels = get_widget_assert (fd.xml, "radiobutton-labels");
80   fd.show_names  = get_widget_assert (fd.xml, "radiobutton-names");
81
82   fd.sort_labels = get_widget_assert (fd.xml, "radiobutton-sort-by-label");
83   fd.sort_names  = get_widget_assert (fd.xml, "radiobutton-sort-by-name");
84   fd.sort_none   = get_widget_assert (fd.xml, "radiobutton-unsorted");
85
86   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
87
88
89   fd.conf = psppire_conf_new ();
90
91   if (psppire_conf_get_boolean (fd.conf,
92                                 "VariableLists", "display-labels", &disp_labels))
93     {
94       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.show_labels),
95                                     disp_labels);
96
97       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.show_names),
98                                     !disp_labels);
99     }
100
101
102   int what = -1;
103   psppire_conf_get_enum (fd.conf, "VariableLists", "sort-order",
104                          PSPP_TYPE_OPTIONS_VAR_ORDER, &what);
105
106   switch (what)
107     {
108     default:
109       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_none), true);
110       break;
111     case PSPP_OPTIONS_VAR_ORDER_NAME:
112       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_names), true);
113       break;
114     case PSPP_OPTIONS_VAR_ORDER_LABEL:
115       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_labels), true);
116       break;
117     }
118
119   const int result = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
120
121   if (result == GTK_RESPONSE_OK)
122     {
123       PsppOptionsVarOrder sort_order = -1;
124       gboolean sl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.show_labels));
125
126       psppire_conf_set_boolean (fd.conf,
127                                 "VariableLists", "display-labels", sl);
128
129       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_labels)))
130         {
131           sort_order = PSPP_OPTIONS_VAR_ORDER_LABEL;
132         }
133       else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_names)))
134         {
135           sort_order = PSPP_OPTIONS_VAR_ORDER_NAME;
136         }
137       else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_none)))
138         {
139           sort_order = PSPP_OPTIONS_VAR_ORDER_UNSORTED;
140         }
141         
142       psppire_conf_set_enum (fd.conf,
143                              "VariableLists", "sort-order",
144                              PSPP_TYPE_OPTIONS_VAR_ORDER,
145                              sort_order);
146     }
147
148   g_object_unref (fd.xml);
149 }