1 /*
2  * glade-gtk-action-group.c - GladeWidgetAdaptor for GtkActionGroup
3  *
4  * Copyright (C) 2013 Tristan Van Berkom
5  *
6  * Authors:
7  *      Tristan Van Berkom <tristan.van.berkom@gmail.com>
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #include <config.h>
25 #include <glib/gi18n-lib.h>
26 #include <gladeui/glade.h>
27 
28 #include "glade-gtk.h"
29 #include "glade-accels.h"
30 
31 gboolean
glade_gtk_action_group_add_verify(GladeWidgetAdaptor * adaptor,GtkWidget * container,GtkWidget * child,gboolean user_feedback)32 glade_gtk_action_group_add_verify (GladeWidgetAdaptor *adaptor,
33 				   GtkWidget          *container,
34 				   GtkWidget          *child,
35 				   gboolean            user_feedback)
36 {
37 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
38   if (!GTK_IS_ACTION (child))
39 G_GNUC_END_IGNORE_DEPRECATIONS
40     {
41       if (user_feedback)
42 	{
43 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
44 	  GladeWidgetAdaptor *action_adaptor =
45 	    glade_widget_adaptor_get_by_type (GTK_TYPE_ACTION);
46 G_GNUC_END_IGNORE_DEPRECATIONS
47 	  glade_util_ui_message (glade_app_get_window (),
48 				 GLADE_UI_INFO, NULL,
49 				 ONLY_THIS_GOES_IN_THAT_MSG,
50 				 glade_widget_adaptor_get_title (action_adaptor),
51 				 glade_widget_adaptor_get_title (adaptor));
52 	}
53 
54       return FALSE;
55     }
56 
57   return TRUE;
58 }
59 
60 void
glade_gtk_action_group_add_child(GladeWidgetAdaptor * adaptor,GObject * container,GObject * child)61 glade_gtk_action_group_add_child (GladeWidgetAdaptor * adaptor,
62                                   GObject * container, GObject * child)
63 {
64 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
65     if (GTK_IS_ACTION (child))
66 G_GNUC_END_IGNORE_DEPRECATIONS
67     {
68       /* Dont really add/remove actions (because name conflicts inside groups)
69        */
70       GladeWidget *ggroup = glade_widget_get_from_gobject (container);
71       GladeWidget *gaction = glade_widget_get_from_gobject (child);
72       GList *actions = g_object_get_data (G_OBJECT (ggroup), "glade-actions");
73 
74       actions = g_list_copy (actions);
75       actions = g_list_append (actions, child);
76 
77       g_object_set_data_full (G_OBJECT (ggroup), "glade-actions", actions,
78                               (GDestroyNotify) g_list_free);
79 
80       glade_widget_property_set_sensitive (gaction, "accelerator", TRUE, NULL);
81       glade_widget_set_action_sensitive (gaction, "launch_editor", TRUE);
82     }
83 }
84 
85 void
glade_gtk_action_group_remove_child(GladeWidgetAdaptor * adaptor,GObject * container,GObject * child)86 glade_gtk_action_group_remove_child (GladeWidgetAdaptor * adaptor,
87                                      GObject * container, GObject * child)
88 {
89 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
90   if (GTK_IS_ACTION (child))
91 G_GNUC_END_IGNORE_DEPRECATIONS
92     {
93       /* Dont really add/remove actions (because name conflicts inside groups)
94        */
95       GladeWidget *ggroup = glade_widget_get_from_gobject (container);
96       GladeWidget *gaction = glade_widget_get_from_gobject (child);
97       GList *actions = g_object_get_data (G_OBJECT (ggroup), "glade-actions");
98 
99       actions = g_list_copy (actions);
100       actions = g_list_remove (actions, child);
101 
102       g_object_set_data_full (G_OBJECT (ggroup), "glade-actions", actions,
103                               (GDestroyNotify) g_list_free);
104 
105       glade_widget_property_set_sensitive (gaction, "accelerator", FALSE,
106 					   ACTION_ACCEL_INSENSITIVE_MSG);
107       glade_widget_set_action_sensitive (gaction, "launch_editor", FALSE);
108     }
109 }
110 
111 void
glade_gtk_action_group_replace_child(GladeWidgetAdaptor * adaptor,GObject * container,GObject * current,GObject * new_action)112 glade_gtk_action_group_replace_child (GladeWidgetAdaptor * adaptor,
113                                       GObject * container,
114                                       GObject * current, GObject * new_action)
115 {
116   glade_gtk_action_group_remove_child (adaptor, container, current);
117   glade_gtk_action_group_add_child (adaptor, container, new_action);
118 }
119 
120 GList *
glade_gtk_action_group_get_children(GladeWidgetAdaptor * adaptor,GObject * container)121 glade_gtk_action_group_get_children (GladeWidgetAdaptor * adaptor,
122                                      GObject * container)
123 {
124   GladeWidget *ggroup = glade_widget_get_from_gobject (container);
125   GList *actions = g_object_get_data (G_OBJECT (ggroup), "glade-actions");
126 
127   return g_list_copy (actions);
128 }
129 
130 
131 void
glade_gtk_action_group_read_child(GladeWidgetAdaptor * adaptor,GladeWidget * widget,GladeXmlNode * node)132 glade_gtk_action_group_read_child (GladeWidgetAdaptor * adaptor,
133                                    GladeWidget * widget, GladeXmlNode * node)
134 {
135   GladeXmlNode *widget_node;
136   GladeWidget *child_widget;
137 
138   if (!glade_xml_node_verify (node, GLADE_XML_TAG_CHILD))
139     return;
140 
141   if ((widget_node =
142        glade_xml_search_child (node, GLADE_XML_TAG_WIDGET)) != NULL)
143     {
144       if ((child_widget = glade_widget_read (glade_widget_get_project (widget),
145                                              widget, widget_node,
146                                              NULL)) != NULL)
147         {
148           glade_widget_add_child (widget, child_widget, FALSE);
149 
150           /* Read in accelerator */
151           glade_gtk_read_accels (child_widget, node, FALSE);
152         }
153     }
154 }
155 
156 
157 void
glade_gtk_action_group_write_child(GladeWidgetAdaptor * adaptor,GladeWidget * widget,GladeXmlContext * context,GladeXmlNode * node)158 glade_gtk_action_group_write_child (GladeWidgetAdaptor * adaptor,
159                                     GladeWidget * widget,
160                                     GladeXmlContext * context,
161                                     GladeXmlNode * node)
162 {
163   GladeXmlNode *child_node;
164 
165   child_node = glade_xml_node_new (context, GLADE_XML_TAG_CHILD);
166   glade_xml_node_append_child (node, child_node);
167 
168   /* Write out the widget */
169   glade_widget_write (widget, context, child_node);
170 
171   /* Write accelerator here  */
172   glade_gtk_write_accels (widget, context, child_node, FALSE);
173 }
174 
175 static void
glade_gtk_action_child_selected(GladeBaseEditor * editor,GladeWidget * gchild,gpointer data)176 glade_gtk_action_child_selected (GladeBaseEditor *editor,
177 				 GladeWidget *gchild,
178 				 gpointer data)
179 {
180   glade_base_editor_add_label (editor, _("Action"));
181 
182   glade_base_editor_add_default_properties (editor, gchild);
183 
184   glade_base_editor_add_label (editor, _("Properties"));
185   glade_base_editor_add_editable (editor, gchild, GLADE_PAGE_GENERAL);
186 }
187 
188 static gboolean
glade_gtk_action_move_child(GladeBaseEditor * editor,GladeWidget * gparent,GladeWidget * gchild,gpointer data)189 glade_gtk_action_move_child (GladeBaseEditor *editor,
190 			     GladeWidget *gparent,
191 			     GladeWidget *gchild,
192 			     gpointer data)
193 {
194   return FALSE;
195 }
196 
197 static void
glade_gtk_action_launch_editor(GObject * action)198 glade_gtk_action_launch_editor (GObject  *action)
199 {
200   GladeWidget        *widget  = glade_widget_get_from_gobject (action);
201   GladeBaseEditor    *editor;
202   GtkWidget          *window;
203 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
204   GType type_action = GTK_TYPE_ACTION;
205   GType type_toggle_action = GTK_TYPE_TOGGLE_ACTION;
206   GType type_radio_action  = GTK_TYPE_RADIO_ACTION;
207   GType type_recent_action = GTK_TYPE_RECENT_ACTION;
208 G_GNUC_END_IGNORE_DEPRECATIONS
209 
210   /* Make sure we get the group here */
211   widget = glade_widget_get_toplevel (widget);
212 
213   /* Editor */
214   editor = glade_base_editor_new (glade_widget_get_object (widget), NULL,
215 				  _("Action"), type_action,
216 				  _("Toggle"), type_toggle_action,
217 				  _("Radio"),  type_radio_action,
218 				  _("Recent"), type_recent_action,
219 				  NULL);
220 
221   g_signal_connect (editor, "child-selected", G_CALLBACK (glade_gtk_action_child_selected), NULL);
222   g_signal_connect (editor, "move-child", G_CALLBACK (glade_gtk_action_move_child), NULL);
223 
224   gtk_widget_show (GTK_WIDGET (editor));
225 
226   window = glade_base_editor_pack_new_window (editor, _("Action Group Editor"), NULL);
227   gtk_widget_show (window);
228 }
229 
230 void
glade_gtk_action_action_activate(GladeWidgetAdaptor * adaptor,GObject * object,const gchar * action_path)231 glade_gtk_action_action_activate (GladeWidgetAdaptor *adaptor,
232 				  GObject *object,
233 				  const gchar *action_path)
234 {
235   if (strcmp (action_path, "launch_editor") == 0)
236     {
237       glade_gtk_action_launch_editor (object);
238     }
239 }
240