1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as
6  * published by the Free Software Foundation; either version 2.1 of
7  * the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Authors:
19  *   Matthias Clasen <mclasen@redhat.com>
20  */
21 
22 #include <config.h>
23 #include <gladeui/glade.h>
24 #include <glib/gi18n-lib.h>
25 #include <gdk/gdkkeysyms.h>
26 
27 #include "glade-action-bar-editor.h"
28 
29 static void glade_action_bar_editor_editable_init (GladeEditableIface * iface);
30 static void glade_action_bar_editor_grab_focus (GtkWidget * widget);
31 static void use_center_child_toggled (GtkWidget *widget, GladeActionBarEditor * box_editor);
32 
33 
34 struct _GladeActionBarEditorPrivate
35 {
36   GtkWidget *embed;
37   GtkWidget *use_center_child;
38 };
39 
40 static GladeEditableIface *parent_editable_iface;
41 
42 G_DEFINE_TYPE_WITH_CODE (GladeActionBarEditor, glade_action_bar_editor, GLADE_TYPE_EDITOR_SKELETON,
43                          G_ADD_PRIVATE (GladeActionBarEditor)
44                          G_IMPLEMENT_INTERFACE (GLADE_TYPE_EDITABLE,
45                                                 glade_action_bar_editor_editable_init));
46 
47 static void
glade_action_bar_editor_class_init(GladeActionBarEditorClass * klass)48 glade_action_bar_editor_class_init (GladeActionBarEditorClass * klass)
49 {
50   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
51 
52   widget_class->grab_focus = glade_action_bar_editor_grab_focus;
53 
54   gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/gladegtk/glade-action-bar-editor.ui");
55   gtk_widget_class_bind_template_child_private (widget_class, GladeActionBarEditor, embed);
56   gtk_widget_class_bind_template_child_private (widget_class, GladeActionBarEditor, use_center_child);
57 
58   gtk_widget_class_bind_template_callback (widget_class, use_center_child_toggled);
59 }
60 
61 static void
glade_action_bar_editor_init(GladeActionBarEditor * self)62 glade_action_bar_editor_init (GladeActionBarEditor * self)
63 {
64   self->priv = glade_action_bar_editor_get_instance_private (self);
65   gtk_widget_init_template (GTK_WIDGET (self));
66 }
67 
68 static void
glade_action_bar_editor_grab_focus(GtkWidget * widget)69 glade_action_bar_editor_grab_focus (GtkWidget * widget)
70 {
71   GladeActionBarEditor *box_editor = GLADE_ACTION_BAR_EDITOR (widget);
72   gtk_widget_grab_focus (box_editor->priv->embed);
73 }
74 
75 static void
glade_action_bar_editor_load(GladeEditable * editable,GladeWidget * gwidget)76 glade_action_bar_editor_load (GladeEditable *editable,
77                               GladeWidget   *gwidget)
78 {
79   GladeActionBarEditor *box_editor = GLADE_ACTION_BAR_EDITOR (editable);
80   GladeActionBarEditorPrivate *priv = box_editor->priv;
81 
82   /* Chain up to default implementation */
83   parent_editable_iface->load (editable, gwidget);
84 
85   if (gwidget)
86     {
87       gboolean use_center_child;
88       glade_widget_property_get (gwidget, "use-center-child", &use_center_child);
89       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->use_center_child), use_center_child);
90     }
91 }
92 
93 static void
glade_action_bar_editor_editable_init(GladeEditableIface * iface)94 glade_action_bar_editor_editable_init (GladeEditableIface * iface)
95 {
96   parent_editable_iface = g_type_interface_peek_parent (iface);
97   iface->load = glade_action_bar_editor_load;
98 }
99 
100 static void
use_center_child_toggled(GtkWidget * widget,GladeActionBarEditor * box_editor)101 use_center_child_toggled (GtkWidget            * widget,
102                           GladeActionBarEditor * box_editor)
103 {
104   GladeActionBarEditorPrivate *priv = box_editor->priv;
105   GladeWidget   *gwidget = glade_editable_loaded_widget (GLADE_EDITABLE (box_editor));
106   GladeWidget   *gcenter = NULL;
107   GtkWidget     *box;
108   GtkWidget     *center;
109   GladeProperty *property;
110   gboolean       use_center_child;
111 
112   if (glade_editable_loading (GLADE_EDITABLE (box_editor)) || !gwidget)
113     return;
114 
115   /* Get new desired property state */
116   use_center_child = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->use_center_child));
117 
118   /* Get any existing center child */
119   box = (GtkWidget *)glade_widget_get_object (gwidget);
120   center = gtk_action_bar_get_center_widget (GTK_ACTION_BAR (box));
121 
122   if (center && !GLADE_IS_PLACEHOLDER (center))
123     gcenter = glade_widget_get_from_gobject (center);
124 
125   glade_editable_block (GLADE_EDITABLE (box_editor));
126 
127   if (use_center_child)
128     glade_command_push_group (_("Setting %s to use a center child"),
129                               glade_widget_get_name (gwidget));
130   else
131     glade_command_push_group (_("Setting %s to not use a center child"),
132                               glade_widget_get_name (gwidget));
133 
134   /* If a project widget exists when were disabling center child, it needs
135    * to be removed first as a part of the issuing GladeCommand group
136    */
137   if (gcenter)
138     {
139       GList list;
140       list.prev = list.next = NULL;
141       list.data = gcenter;
142       glade_command_delete (&list);
143     }
144 
145   property = glade_widget_get_property (gwidget, "use-center-child");
146   glade_command_set_property (property, use_center_child);
147 
148   glade_command_pop_group ();
149 
150   glade_editable_unblock (GLADE_EDITABLE (box_editor));
151 
152   /* reload buttons and sensitivity and stuff... */
153   glade_editable_load (GLADE_EDITABLE (box_editor), gwidget);
154 }
155 
156 GtkWidget *
glade_action_bar_editor_new(void)157 glade_action_bar_editor_new (void)
158 {
159   return g_object_new (GLADE_TYPE_ACTION_BAR_EDITOR, NULL);
160 }
161