1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* gtkhtml-face-action.c
4 * Copyright (C) 2008 Novell, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gtkhtml-face-action.h"
23 #include "gtkhtml-face-chooser-menu.h"
24 #include "gtkhtml-face-tool-button.h"
26 struct _GtkhtmlFaceActionPrivate
{
28 GtkhtmlFaceChooser
*current_chooser
;
36 static gpointer parent_class
;
39 face_action_proxy_item_activated_cb (GtkhtmlFaceAction
*action
,
40 GtkhtmlFaceChooser
*chooser
)
42 action
->priv
->current_chooser
= chooser
;
44 g_signal_emit_by_name (action
, "item-activated");
48 face_action_set_property (GObject
*object
,
53 switch (property_id
) {
54 case PROP_CURRENT_FACE
:
55 gtkhtml_face_chooser_set_current_face (
56 GTKHTML_FACE_CHOOSER (object
),
57 g_value_get_boxed (value
));
61 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
65 face_action_get_property (GObject
*object
,
70 switch (property_id
) {
71 case PROP_CURRENT_FACE
:
73 value
, gtkhtml_face_chooser_get_current_face (
74 GTKHTML_FACE_CHOOSER (object
)));
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
82 face_action_finalize (GObject
*object
)
84 GtkhtmlFaceActionPrivate
*priv
;
86 priv
= GTKHTML_FACE_ACTION (object
)->priv
;
88 g_list_free (priv
->choosers
);
90 /* Chain up to parent's finalize() method. */
91 G_OBJECT_CLASS (parent_class
)->finalize (object
);
95 face_action_activate (GtkAction
*action
)
97 GtkhtmlFaceActionPrivate
*priv
;
99 priv
= GTKHTML_FACE_ACTION (action
)->priv
;
101 priv
->current_chooser
= NULL
;
105 face_action_create_menu_item (GtkAction
*action
)
110 item
= gtk_image_menu_item_new ();
111 menu
= gtk_action_create_menu (action
);
112 gtk_menu_item_set_submenu (GTK_MENU_ITEM (item
), menu
);
113 gtk_widget_show (menu
);
119 face_action_create_tool_item (GtkAction
*action
)
121 return GTK_WIDGET (gtkhtml_face_tool_button_new ());
125 face_action_connect_proxy (GtkAction
*action
,
128 GtkhtmlFaceActionPrivate
*priv
;
130 priv
= GTKHTML_FACE_ACTION (action
)->priv
;
132 if (!GTKHTML_IS_FACE_CHOOSER (proxy
))
135 if (g_list_find (priv
->choosers
, proxy
) != NULL
)
138 g_signal_connect_swapped (
139 proxy
, "item-activated",
140 G_CALLBACK (face_action_proxy_item_activated_cb
), action
);
143 /* Chain up to parent's connect_proxy() method. */
144 GTK_ACTION_CLASS (parent_class
)->connect_proxy (action
, proxy
);
148 face_action_disconnect_proxy (GtkAction
*action
,
151 GtkhtmlFaceActionPrivate
*priv
;
153 priv
= GTKHTML_FACE_ACTION (action
)->priv
;
155 priv
->choosers
= g_list_remove (priv
->choosers
, proxy
);
157 /* Chain up to parent's disconnect_proxy() method. */
158 GTK_ACTION_CLASS (parent_class
)->disconnect_proxy (action
, proxy
);
162 face_action_create_menu (GtkAction
*action
)
164 GtkhtmlFaceActionPrivate
*priv
;
167 priv
= GTKHTML_FACE_ACTION (action
)->priv
;
169 widget
= gtkhtml_face_chooser_menu_new ();
171 g_signal_connect_swapped (
172 widget
, "item-activated",
173 G_CALLBACK (face_action_proxy_item_activated_cb
), action
);
175 priv
->choosers
= g_list_prepend (priv
->choosers
, widget
);
181 face_action_get_current_face (GtkhtmlFaceChooser
*chooser
)
183 GtkhtmlFaceActionPrivate
*priv
;
184 GtkhtmlFace
*face
= NULL
;
186 priv
= GTKHTML_FACE_ACTION (chooser
)->priv
;
188 if (priv
->current_chooser
!= NULL
)
189 face
= gtkhtml_face_chooser_get_current_face (
190 priv
->current_chooser
);
196 face_action_set_current_face (GtkhtmlFaceChooser
*chooser
,
199 GtkhtmlFaceActionPrivate
*priv
;
202 priv
= GTKHTML_FACE_ACTION (chooser
)->priv
;
204 for (iter
= priv
->choosers
; iter
!= NULL
; iter
= iter
->next
) {
205 GtkhtmlFaceChooser
*proxy_chooser
= iter
->data
;
207 gtkhtml_face_chooser_set_current_face (proxy_chooser
, face
);
212 face_action_class_init (GtkhtmlFaceActionClass
*class)
214 GObjectClass
*object_class
;
215 GtkActionClass
*action_class
;
217 parent_class
= g_type_class_peek_parent (class);
218 g_type_class_add_private (class, sizeof (GtkhtmlFaceAction
));
220 object_class
= G_OBJECT_CLASS (class);
221 object_class
->set_property
= face_action_set_property
;
222 object_class
->get_property
= face_action_get_property
;
223 object_class
->finalize
= face_action_finalize
;
225 action_class
= GTK_ACTION_CLASS (class);
226 action_class
->activate
= face_action_activate
;
227 action_class
->create_menu_item
= face_action_create_menu_item
;
228 action_class
->create_tool_item
= face_action_create_tool_item
;
229 action_class
->connect_proxy
= face_action_connect_proxy
;
230 action_class
->disconnect_proxy
= face_action_disconnect_proxy
;
231 action_class
->create_menu
= face_action_create_menu
;
233 g_object_class_override_property (
234 object_class
, PROP_CURRENT_FACE
, "current-face");
238 face_action_iface_init (GtkhtmlFaceChooserIface
*iface
)
240 iface
->get_current_face
= face_action_get_current_face
;
241 iface
->set_current_face
= face_action_set_current_face
;
245 face_action_init (GtkhtmlFaceAction
*action
)
247 action
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (
248 action
, GTKHTML_TYPE_FACE_ACTION
, GtkhtmlFaceActionPrivate
);
252 gtkhtml_face_action_get_type (void)
254 static GType type
= 0;
256 if (G_UNLIKELY (type
== 0)) {
257 static const GTypeInfo type_info
= {
258 sizeof (GtkhtmlFaceActionClass
),
259 (GBaseInitFunc
) NULL
,
260 (GBaseFinalizeFunc
) NULL
,
261 (GClassInitFunc
) face_action_class_init
,
262 (GClassFinalizeFunc
) NULL
,
263 NULL
, /* class_data */
264 sizeof (GtkhtmlFaceAction
),
266 (GInstanceInitFunc
) face_action_init
,
267 NULL
/* value_table */
270 static const GInterfaceInfo iface_info
= {
271 (GInterfaceInitFunc
) face_action_iface_init
,
272 (GInterfaceFinalizeFunc
) NULL
,
273 NULL
/* interface_data */
276 type
= g_type_register_static (
277 GTK_TYPE_ACTION
, "GtkhtmlFaceAction", &type_info
, 0);
279 g_type_add_interface_static (
280 type
, GTKHTML_TYPE_FACE_CHOOSER
, &iface_info
);
287 gtkhtml_face_action_new (const gchar
*name
,
289 const gchar
*tooltip
,
290 const gchar
*stock_id
)
292 g_return_val_if_fail (name
!= NULL
, NULL
);
294 return g_object_new (
295 GTKHTML_TYPE_FACE_ACTION
, "name", name
, "label", label
,
296 "tooltip", tooltip
, "stock-id", stock_id
, NULL
);