e3ee25be75fc7f53c021314573e29a6ec42ed537
[pspp-builds.git] / src / ui / gui / psppire-dialog-action.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012  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
18 #include <config.h>
19
20 #include "psppire-dialog-action.h"
21 #include "psppire-dialog.h"
22 #include "executor.h"
23 #include "helper.h"
24 #include "psppire-data-window.h"
25
26 static void psppire_dialog_action_init            (PsppireDialogAction      *act);
27 static void psppire_dialog_action_class_init      (PsppireDialogActionClass *class);
28
29 G_DEFINE_ABSTRACT_TYPE (PsppireDialogAction, psppire_dialog_action, GTK_TYPE_ACTION);
30
31 /* Properties */
32 enum
33 {
34   PROP_0,
35   PROP_MANAGER,
36   PROP_TOPLEVEL,
37 };
38
39 static void
40 psppire_dialog_action_set_property (GObject         *object,
41                                guint            prop_id,
42                                const GValue    *value,
43                                GParamSpec      *pspec)
44 {
45   PsppireDialogAction *act = PSPPIRE_DIALOG_ACTION (object);
46
47   switch (prop_id)
48     {
49     case PROP_MANAGER:
50       {
51
52         GObject *p = g_value_get_object (value);
53         act->uim = GTK_UI_MANAGER (p);
54       }
55       break;
56     default:
57       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
58       break;
59     };
60 }
61
62
63 static void
64 psppire_dialog_action_get_property (GObject    *object,
65                                guint            prop_id,
66                                GValue          *value,
67                                GParamSpec      *pspec)
68 {
69   PsppireDialogAction *dialog_action = PSPPIRE_DIALOG_ACTION (object);
70
71   switch (prop_id)
72     {
73     case PROP_MANAGER:
74       g_value_take_object (value, dialog_action->uim);
75       break;
76   case PROP_TOPLEVEL:
77       g_value_take_object (value, dialog_action->toplevel);
78       break;
79     default:
80       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81       break;
82     };
83 }
84
85
86 static void
87 psppire_dialog_action_activate (PsppireDialogAction *act)
88 {
89   gint response;
90
91   PsppireVarStore *vs;
92   PsppireDialogActionClass *class = PSPPIRE_DIALOG_ACTION_GET_CLASS (act);
93
94   GSList *sl = gtk_ui_manager_get_toplevels (act->uim, GTK_UI_MANAGER_MENUBAR | GTK_UI_MANAGER_TOOLBAR);
95   g_return_if_fail (sl);
96
97   act->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (sl->data));
98   g_slist_free (sl);
99
100   vs = PSPPIRE_DATA_WINDOW(act->toplevel)->var_store;
101   
102   g_object_get (vs, "dictionary", &act->dict, NULL);
103   
104   g_object_set (act->source, "model", act->dict, NULL);
105   
106   gtk_window_set_transient_for (GTK_WINDOW (act->dialog), GTK_WINDOW (act->toplevel));
107
108
109   if (GTK_ACTION_CLASS (psppire_dialog_action_parent_class)->activate)
110     GTK_ACTION_CLASS (psppire_dialog_action_parent_class)->activate ( GTK_ACTION (act));
111
112   response = psppire_dialog_run (PSPPIRE_DIALOG (act->dialog));
113
114   if ( class->generate_syntax )
115     {
116       switch (response)
117         {
118         case GTK_RESPONSE_OK:
119           g_free (execute_syntax_string (PSPPIRE_DATA_WINDOW (act->toplevel),
120                                          class->generate_syntax (act)));
121           break;
122         case PSPPIRE_RESPONSE_PASTE:
123           g_free (paste_syntax_to_window (class->generate_syntax (act)));
124           break;
125         default:
126           break;
127         }
128     }
129 }
130
131 static void
132 psppire_dialog_action_class_init (PsppireDialogActionClass *class)
133 {
134   GObjectClass *object_class = G_OBJECT_CLASS (class);
135
136   GParamSpec *manager_spec =
137     g_param_spec_object ("manager",
138                          "Manager",
139                          "The GtkUIManager which created this object",
140                          GTK_TYPE_UI_MANAGER,
141                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
142
143   GParamSpec *toplevel_spec =
144     g_param_spec_object ("top-level",
145                          "Top Level",
146                          "The top level widget to which this dialog action belongs",
147                          GTK_TYPE_WINDOW,
148                          G_PARAM_READABLE);
149
150   object_class->set_property = psppire_dialog_action_set_property;
151   object_class->get_property = psppire_dialog_action_get_property;
152
153   class->generate_syntax = NULL;
154
155   class->activate = psppire_dialog_action_activate;
156
157   g_object_class_install_property (object_class,
158                                    PROP_MANAGER,
159                                    manager_spec);
160
161   g_object_class_install_property (object_class,
162                                    PROP_TOPLEVEL,
163                                    toplevel_spec);
164 }
165
166
167 static void
168 psppire_dialog_action_init (PsppireDialogAction *act)
169 {
170   act->toplevel = NULL;
171   act->dict = NULL;
172 }
173
174
175 void
176 psppire_dialog_action_set_valid_predicate (PsppireDialogAction *act, 
177                                            ContentsAreValid dialog_state_valid)
178 {
179   psppire_dialog_set_valid_predicate (act->dialog, dialog_state_valid, act);
180 }
181
182 void
183 psppire_dialog_action_set_refresh (PsppireDialogAction *pda, 
184                                    PsppireDialogActionRefresh refresh)
185 {
186   g_signal_connect_swapped (pda->dialog, "refresh", G_CALLBACK (refresh),  pda);
187 }
188