c50048060212a0c9e847dfd88414966a4d98e5e3
[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   GtkWidget *maximize;
46   GtkWidget *alert;
47   GtkWidget *raise;
48 };
49
50 GType
51 pspp_options_var_order_get_type (void)
52 {
53   static GType etype = 0;
54   if (G_UNLIKELY(etype == 0)) {
55     static const GEnumValue values[] =
56       {
57         { PSPP_OPTIONS_VAR_ORDER_UNSORTED, "PSPP_OPTIONS_VAR_ORDER_UNSORTED", "unsorted" },
58         { PSPP_OPTIONS_VAR_ORDER_NAME,     "PSPP_OPTIONS_VAR_ORDER_NAME",     "name" },
59         { PSPP_OPTIONS_VAR_ORDER_LABEL,    "PSPP_OPTIONS_VAR_ORDER_LABEL",    "label" },
60         { 0, NULL, NULL }
61       };
62     etype = g_enum_register_static (g_intern_static_string ("PsppOptionsVarOrder"), values);
63   }
64   return etype;
65 }
66
67 /*
68    Pops up the Options dialog box
69  */
70 void
71 options_dialog (PsppireDataWindow *de)
72 {
73   struct options_dialog fd;
74
75   GtkWidget *dialog ;
76
77   gboolean disp_labels = true;
78
79   fd.xml = builder_new ("options.ui");
80
81   dialog = get_widget_assert (fd.xml, "options-dialog");
82
83   fd.show_labels = get_widget_assert (fd.xml, "radiobutton-labels");
84   fd.show_names  = get_widget_assert (fd.xml, "radiobutton-names");
85
86   fd.sort_labels = get_widget_assert (fd.xml, "radiobutton-sort-by-label");
87   fd.sort_names  = get_widget_assert (fd.xml, "radiobutton-sort-by-name");
88   fd.sort_none   = get_widget_assert (fd.xml, "radiobutton-unsorted");
89
90   fd.maximize = get_widget_assert (fd.xml, "checkbutton-maximize");
91   fd.alert    = get_widget_assert (fd.xml, "checkbutton-alert");
92   fd.raise    = get_widget_assert (fd.xml, "checkbutton-raise");
93
94   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
95
96   fd.conf = psppire_conf_new ();
97
98   if (psppire_conf_get_boolean (fd.conf,
99                                 "VariableLists", "display-labels", &disp_labels))
100     {
101       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.show_labels),
102                                     disp_labels);
103
104       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.show_names),
105                                     !disp_labels);
106     }
107
108
109   int what = -1;
110   psppire_conf_get_enum (fd.conf, "VariableLists", "sort-order",
111                          PSPP_TYPE_OPTIONS_VAR_ORDER, &what);
112
113   switch (what)
114     {
115     default:
116       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_none), true);
117       break;
118     case PSPP_OPTIONS_VAR_ORDER_NAME:
119       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_names), true);
120       break;
121     case PSPP_OPTIONS_VAR_ORDER_LABEL:
122       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.sort_labels), true);
123       break;
124     }
125
126   {
127     gboolean status;
128     if (psppire_conf_get_boolean (fd.conf, "OutputWindowAction", "maximize",
129                                   &status))
130       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.maximize), status);
131   }
132
133   {
134     gboolean status = true;
135     psppire_conf_get_boolean (fd.conf, "OutputWindowAction", "alert", &status);
136     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.alert), status);
137   }
138
139   {
140     gboolean status;
141     if (psppire_conf_get_boolean (fd.conf, "OutputWindowAction", "raise",
142                                   &status))
143       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd.raise), status);
144   }
145
146   const int result = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
147
148   if (result == GTK_RESPONSE_OK)
149     {
150       PsppOptionsVarOrder sort_order = -1;
151       gboolean sl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.show_labels));
152
153       psppire_conf_set_boolean (fd.conf,
154                                 "VariableLists", "display-labels", sl);
155
156       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_labels)))
157         {
158           sort_order = PSPP_OPTIONS_VAR_ORDER_LABEL;
159         }
160       else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_names)))
161         {
162           sort_order = PSPP_OPTIONS_VAR_ORDER_NAME;
163         }
164       else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd.sort_none)))
165         {
166           sort_order = PSPP_OPTIONS_VAR_ORDER_UNSORTED;
167         }
168
169       psppire_conf_set_enum (fd.conf,
170                              "VariableLists", "sort-order",
171                              PSPP_TYPE_OPTIONS_VAR_ORDER,
172                              sort_order);
173
174       psppire_conf_set_boolean (fd.conf, "OutputWindowAction", "maximize",
175                                 gtk_toggle_button_get_active
176                                 (GTK_TOGGLE_BUTTON (fd.maximize)));
177
178       psppire_conf_set_boolean (fd.conf, "OutputWindowAction", "raise",
179                                 gtk_toggle_button_get_active
180                                 (GTK_TOGGLE_BUTTON (fd.raise)));
181
182       psppire_conf_set_boolean (fd.conf, "OutputWindowAction", "alert",
183                                 gtk_toggle_button_get_active
184                                 (GTK_TOGGLE_BUTTON (fd.alert)));
185     }
186
187   g_object_unref (fd.xml);
188 }