1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2012, 2016 Free Software Foundation
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.
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.
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/>. */
20 #include "psppire-dialog-action.h"
21 #include "psppire-dialog.h"
24 #include "psppire-data-window.h"
25 #include "builder-wrapper.h"
27 static GObjectClass * parent_class = NULL;
30 __get_name (GAction *act)
32 return G_OBJECT_TYPE_NAME (act);
35 static const GVariantType *
36 __get_state_type (GAction *act)
43 __get_state (GAction *act)
49 static const GVariantType *
50 __get_parameter_type (GAction *act)
52 return PSPPIRE_DIALOG_ACTION (act)->parameter_type;
56 __get_enabled (GAction *act)
61 static void psppire_dialog_action_activate (PsppireDialogAction *act, GVariant *parameter);
64 psppire_dialog_action_activate_null (PsppireDialogAction *act)
66 psppire_dialog_action_activate (act, NULL);
71 __activate (GAction *action, GVariant *parameter)
73 psppire_dialog_action_activate (PSPPIRE_DIALOG_ACTION (action), parameter);
78 action_model_init (GActionInterface *iface)
80 iface->get_name = __get_name;
81 iface->get_state_type = __get_state_type;
82 iface->get_state = __get_state;
83 iface->get_parameter_type = __get_parameter_type;
84 iface->get_enabled = __get_enabled;
85 iface->activate = __activate;
88 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (PsppireDialogAction, \
89 psppire_dialog_action, \
91 G_IMPLEMENT_INTERFACE (G_TYPE_ACTION, \
107 psppire_dialog_action_set_property (GObject *object,
112 PsppireDialogAction *act = PSPPIRE_DIALOG_ACTION (object);
118 GObject *p = g_value_get_object (value);
119 act->toplevel = GTK_WIDGET (p);
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 psppire_dialog_action_get_property (GObject *object,
135 PsppireDialogAction *dialog_action = PSPPIRE_DIALOG_ACTION (object);
140 g_value_take_object (value, dialog_action->toplevel);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 on_destroy_dataset (GObject *w)
153 GHashTable *t = g_object_get_data (w, "thing-table");
154 GSList *dl = g_object_get_data (w, "widget-list");
156 g_slist_free_full (dl, (GDestroyNotify) gtk_widget_destroy);
157 g_hash_table_unref (t);
160 /* Each toplevel widget - that is the data window, which generally has a 1-1 association
161 with a dataset - has an associated GHashTable.
163 This GHashTable is keyed by the address of a PsppireDialogAction, and its values
164 are user determined pointers (typically a GtkBuilder*).
166 This is useful for storing the state of dialogs so they can persist between invocations.
169 psppire_dialog_action_get_hash_table (PsppireDialogAction *act)
171 GHashTable *t = g_object_get_data (G_OBJECT (act->toplevel), "thing-table");
174 t = g_hash_table_new_full (g_direct_hash, g_direct_equal, 0, g_object_unref);
175 g_object_set_data (G_OBJECT (act->toplevel), "thing-table", t);
176 g_object_set_data (G_OBJECT (act->toplevel), "widget-list", NULL);
177 g_signal_connect (act->toplevel, "destroy", G_CALLBACK (on_destroy_dataset), NULL);
184 psppire_dialog_action_get_xml (PsppireDialogAction *da)
186 GHashTable *thing = psppire_dialog_action_get_hash_table (da);
187 GtkBuilder *xml = g_hash_table_lookup (thing, da);
193 psppire_dialog_action_activate (PsppireDialogAction *act, GVariant *parameter)
197 PsppireDialogActionClass *class = PSPPIRE_DIALOG_ACTION_GET_CLASS (act);
199 act->dict = PSPPIRE_DATA_WINDOW(act->toplevel)->dict;
201 GSList *wl = g_object_get_data (G_OBJECT (act->toplevel), "widget-list");
202 wl = g_slist_prepend (wl, act->dialog);
203 g_object_set_data (G_OBJECT (act->toplevel), "widget-list", wl);
205 if (class->initial_activate)
207 GHashTable *thing = psppire_dialog_action_get_hash_table (act);
208 GtkBuilder *xml = g_hash_table_lookup (thing, act);
211 xml = class->initial_activate (act, parameter);
212 g_hash_table_insert (thing, act, xml);
218 GHashTable *thing = psppire_dialog_action_get_hash_table (act);
219 GtkBuilder *xml = g_hash_table_lookup (thing, act);
221 class->activate (act, parameter);
224 gtk_window_set_transient_for (GTK_WINDOW (act->dialog),
225 GTK_WINDOW (act->toplevel));
229 g_object_set (act->source, "model", act->dict, NULL);
230 gtk_widget_grab_focus (act->source);
234 psppire_dialog_reload (PSPPIRE_DIALOG (act->dialog));
236 act->activated = TRUE;
238 response = psppire_dialog_run (PSPPIRE_DIALOG (act->dialog));
240 if (class->generate_syntax)
244 case GTK_RESPONSE_OK:
245 g_free (execute_syntax_string (PSPPIRE_DATA_WINDOW (act->toplevel),
246 class->generate_syntax (act)));
248 case PSPPIRE_RESPONSE_PASTE:
249 g_free (paste_syntax_to_window (class->generate_syntax (act)));
258 psppire_dialog_action_class_init (PsppireDialogActionClass *class)
260 GObjectClass *object_class = G_OBJECT_CLASS (class);
262 parent_class = g_type_class_peek_parent (class);
264 GParamSpec *toplevel_spec =
265 g_param_spec_object ("top-level",
267 "The top level widget to which this dialog action belongs",
269 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
271 object_class->set_property = psppire_dialog_action_set_property;
272 object_class->get_property = psppire_dialog_action_get_property;
274 class->generate_syntax = NULL;
276 g_object_class_install_property (object_class,
280 g_object_class_override_property (object_class, PROP_NAME, "name");
281 g_object_class_override_property (object_class, PROP_ENABLED, "enabled");
282 g_object_class_override_property (object_class, PROP_STATE, "state");
283 g_object_class_override_property (object_class, PROP_STATE_TYPE, "state-type");
284 g_object_class_override_property (object_class, PROP_PARAMETER_TYPE, "parameter-type");
289 psppire_dialog_action_init (PsppireDialogAction *act)
291 act->toplevel = NULL;
293 act->activated = FALSE;
294 act->parameter_type = NULL;
298 psppire_dialog_action_set_valid_predicate (PsppireDialogAction *act,
299 ContentsAreValid dialog_state_valid)
301 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (act->dialog),
302 dialog_state_valid, act);
306 psppire_dialog_action_set_refresh (PsppireDialogAction *pda,
307 PsppireDialogActionRefresh refresh)
309 g_signal_connect_swapped (pda->dialog, "refresh", G_CALLBACK (refresh), pda);