2 * geanymenubuttonaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2009-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either vergeany 2 of the License, or
10 * (at your option) any later vergeany.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* GtkAction subclass to provide a GtkMenuToolButton in a toolbar.
23 * This class is missing the action_create_menu_item() function and so can't be
24 * used for creating menu items. */
30 #include "geanymenubuttonaction.h"
33 typedef struct _GeanyMenubuttonActionPrivate GeanyMenubuttonActionPrivate
;
35 #define GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(obj) (GEANY_MENU_BUTTON_ACTION(obj)->priv)
38 struct _GeanyMenubuttonActionPrivate
56 static guint signals
[LAST_SIGNAL
];
59 G_DEFINE_TYPE(GeanyMenubuttonAction
, geany_menu_button_action
, GTK_TYPE_ACTION
)
62 static void geany_menu_button_action_finalize(GObject
*object
)
64 GeanyMenubuttonActionPrivate
*priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(object
);
66 g_object_unref(priv
->menu
);
67 g_free(priv
->tooltip_arrow
);
69 (* G_OBJECT_CLASS(geany_menu_button_action_parent_class
)->finalize
)(object
);
73 static void delegate_button_activated(GtkAction
*action
)
75 g_signal_emit(action
, signals
[BUTTON_CLICKED
], 0);
79 static void set_arrow_tooltip(GtkMenuToolButton
*button
, const gchar
*tooltip
)
81 #if GTK_CHECK_VERSION(2, 12, 0)
82 gtk_menu_tool_button_set_arrow_tooltip_text(button
, tooltip
);
84 static GtkTooltips
*tooltips
= NULL
;
86 if (G_UNLIKELY(tooltips
== NULL
))
87 tooltips
= gtk_tooltips_new();
89 gtk_menu_tool_button_set_arrow_tooltip(button
, tooltips
, tooltip
, NULL
);
94 static void geany_menu_button_action_set_property(GObject
*object
, guint prop_id
,
95 const GValue
*value
, GParamSpec
*pspec
)
99 case PROP_TOOLTIP_ARROW
:
101 GeanyMenubuttonActionPrivate
*priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(object
);
102 g_free(priv
->tooltip_arrow
);
103 priv
->tooltip_arrow
= g_value_dup_string(value
);
107 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
113 static GtkWidget
*geany_menu_button_action_create_tool_item(GtkAction
*action
)
116 GeanyMenubuttonActionPrivate
*priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action
);
118 toolitem
= g_object_new(GTK_TYPE_MENU_TOOL_BUTTON
, NULL
);
119 set_arrow_tooltip(GTK_MENU_TOOL_BUTTON(toolitem
), priv
->tooltip_arrow
);
125 static void geany_menu_button_action_class_init(GeanyMenubuttonActionClass
*klass
)
127 GtkActionClass
*action_class
= GTK_ACTION_CLASS(klass
);
128 GObjectClass
*g_object_class
= G_OBJECT_CLASS(klass
);
130 g_object_class
->finalize
= geany_menu_button_action_finalize
;
131 g_object_class
->set_property
= geany_menu_button_action_set_property
;
133 action_class
->activate
= delegate_button_activated
;
134 action_class
->create_tool_item
= geany_menu_button_action_create_tool_item
;
135 action_class
->toolbar_item_type
= GTK_TYPE_MENU_TOOL_BUTTON
;
137 g_type_class_add_private(klass
, sizeof(GeanyMenubuttonActionPrivate
));
139 g_object_class_install_property(g_object_class
,
144 "A special tooltip for the arrow button",
148 signals
[BUTTON_CLICKED
] = g_signal_new("button-clicked",
149 G_TYPE_FROM_CLASS(klass
),
154 g_cclosure_marshal_VOID__VOID
,
159 static void geany_menu_button_action_init(GeanyMenubuttonAction
*action
)
161 GeanyMenubuttonActionPrivate
*priv
;
163 action
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(action
,
164 GEANY_MENU_BUTTON_ACTION_TYPE
, GeanyMenubuttonActionPrivate
);
167 priv
->tooltip_arrow
= NULL
;
172 GtkAction
*geany_menu_button_action_new(const gchar
*name
,
174 const gchar
*tooltip
,
175 const gchar
*tooltip_arrow
,
176 const gchar
*stock_id
)
178 GtkAction
*action
= g_object_new(GEANY_MENU_BUTTON_ACTION_TYPE
,
182 "tooltip-arrow", tooltip_arrow
,
183 "stock-id", stock_id
,
190 GtkWidget
*geany_menu_button_action_get_menu(GeanyMenubuttonAction
*action
)
192 GeanyMenubuttonActionPrivate
*priv
;
194 g_return_val_if_fail(action
!= NULL
, NULL
);
196 priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action
);
202 static void menu_items_changed_cb(GtkContainer
*container
, GtkWidget
*widget
, GeanyMenubuttonAction
*action
)
204 GeanyMenubuttonActionPrivate
*priv
;
208 g_return_if_fail(action
!= NULL
);
210 priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action
);
211 if (priv
->menu
!= NULL
)
212 enable
= (g_list_length(gtk_container_get_children(GTK_CONTAINER(priv
->menu
))) > 0);
216 foreach_slist(l
, gtk_action_get_proxies(GTK_ACTION(action
)))
218 /* On Windows a GtkImageMenuItem proxy is created for whatever reason. So we filter
219 * by type and act only on GtkMenuToolButton proxies. */
220 /* TODO find why the GtkImageMenuItem proxy is created */
221 if (! GTK_IS_MENU_TOOL_BUTTON(l
->data
))
226 if (gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(l
->data
)) == NULL
)
227 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l
->data
), priv
->menu
);
230 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l
->data
), NULL
);
235 void geany_menu_button_action_set_menu(GeanyMenubuttonAction
*action
, GtkWidget
*menu
)
237 GeanyMenubuttonActionPrivate
*priv
;
239 g_return_if_fail(action
!= NULL
);
241 priv
= GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action
);
243 if (priv
->menu
!= NULL
&& GTK_IS_WIDGET(priv
->menu
))
244 g_signal_handlers_disconnect_by_func(priv
->menu
, menu_items_changed_cb
, action
);
247 g_signal_connect(menu
, "add", G_CALLBACK(menu_items_changed_cb
), action
);
248 g_signal_connect(menu
, "remove", G_CALLBACK(menu_items_changed_cb
), action
);
253 menu_items_changed_cb(GTK_CONTAINER(menu
), NULL
, action
);