Bump API version for new plugin entry points (oops)
[geany-mirror.git] / src / geanymenubuttonaction.c
blob29c09e836e7284c9ab2556ddd8ae7dadad4eeb4b
1 /*
2 * geanymenubuttonaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2009-2012 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 version 2 of the License, or
10 * (at your option) any later version.
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 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. */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "geanymenubuttonaction.h"
32 #include "utils.h"
34 typedef struct _GeanyMenubuttonActionPrivate GeanyMenubuttonActionPrivate;
36 #define GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(obj) (GEANY_MENU_BUTTON_ACTION(obj)->priv)
39 struct _GeanyMenubuttonActionPrivate
41 GtkWidget *menu;
43 gchar *tooltip_arrow;
46 enum
48 PROP_0,
49 PROP_TOOLTIP_ARROW
52 enum
54 BUTTON_CLICKED,
55 LAST_SIGNAL
57 static guint signals[LAST_SIGNAL];
60 G_DEFINE_TYPE(GeanyMenubuttonAction, geany_menu_button_action, GTK_TYPE_ACTION)
63 static void geany_menu_button_action_finalize(GObject *object)
65 GeanyMenubuttonActionPrivate *priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(object);
67 g_object_unref(priv->menu);
68 g_free(priv->tooltip_arrow);
70 (* G_OBJECT_CLASS(geany_menu_button_action_parent_class)->finalize)(object);
74 static void delegate_button_activated(GtkAction *action)
76 g_signal_emit(action, signals[BUTTON_CLICKED], 0);
80 static void geany_menu_button_action_set_property(GObject *object, guint prop_id,
81 const GValue *value, GParamSpec *pspec)
83 switch (prop_id)
85 case PROP_TOOLTIP_ARROW:
87 GeanyMenubuttonActionPrivate *priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(object);
88 g_free(priv->tooltip_arrow);
89 priv->tooltip_arrow = g_value_dup_string(value);
90 break;
92 default:
93 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
94 break;
99 static GtkWidget *geany_menu_button_action_create_tool_item(GtkAction *action)
101 GtkWidget *toolitem;
102 GeanyMenubuttonActionPrivate *priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
104 toolitem = g_object_new(GTK_TYPE_MENU_TOOL_BUTTON, NULL);
105 gtk_menu_tool_button_set_arrow_tooltip_text(GTK_MENU_TOOL_BUTTON(toolitem), priv->tooltip_arrow);
107 return toolitem;
111 static void geany_menu_button_action_class_init(GeanyMenubuttonActionClass *klass)
113 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
114 GObjectClass *g_object_class = G_OBJECT_CLASS(klass);
116 g_object_class->finalize = geany_menu_button_action_finalize;
117 g_object_class->set_property = geany_menu_button_action_set_property;
119 action_class->activate = delegate_button_activated;
120 action_class->create_tool_item = geany_menu_button_action_create_tool_item;
121 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
123 g_type_class_add_private(klass, sizeof(GeanyMenubuttonActionPrivate));
125 g_object_class_install_property(g_object_class,
126 PROP_TOOLTIP_ARROW,
127 g_param_spec_string(
128 "tooltip-arrow",
129 "Arrow tooltip",
130 "A special tooltip for the arrow button",
132 G_PARAM_WRITABLE));
134 signals[BUTTON_CLICKED] = g_signal_new("button-clicked",
135 G_TYPE_FROM_CLASS(klass),
136 (GSignalFlags) 0,
139 NULL,
140 g_cclosure_marshal_VOID__VOID,
141 G_TYPE_NONE, 0);
145 static void geany_menu_button_action_init(GeanyMenubuttonAction *action)
147 GeanyMenubuttonActionPrivate *priv;
149 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
150 GEANY_MENU_BUTTON_ACTION_TYPE, GeanyMenubuttonActionPrivate);
152 priv = action->priv;
153 priv->tooltip_arrow = NULL;
154 priv->menu = NULL;
158 GtkAction *geany_menu_button_action_new(const gchar *name,
159 const gchar *label,
160 const gchar *tooltip,
161 const gchar *tooltip_arrow,
162 const gchar *stock_id)
164 GtkAction *action = g_object_new(GEANY_MENU_BUTTON_ACTION_TYPE,
165 "name", name,
166 "label", label,
167 "tooltip", tooltip,
168 "tooltip-arrow", tooltip_arrow,
169 "stock-id", stock_id,
170 NULL);
172 return action;
176 GtkWidget *geany_menu_button_action_get_menu(GeanyMenubuttonAction *action)
178 GeanyMenubuttonActionPrivate *priv;
180 g_return_val_if_fail(action != NULL, NULL);
182 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
184 return priv->menu;
188 static void menu_items_changed_cb(GtkContainer *container, GtkWidget *widget, GeanyMenubuttonAction *action)
190 GeanyMenubuttonActionPrivate *priv;
191 gboolean enable;
192 GSList *l;
194 g_return_if_fail(action != NULL);
196 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
197 if (priv->menu != NULL)
199 GList *children = gtk_container_get_children(GTK_CONTAINER(priv->menu));
201 enable = (g_list_length(children) > 0);
202 g_list_free(children);
204 else
205 enable = FALSE;
207 foreach_slist(l, gtk_action_get_proxies(GTK_ACTION(action)))
209 /* On Windows a GtkImageMenuItem proxy is created for whatever reason. So we filter
210 * by type and act only on GtkMenuToolButton proxies. */
211 /* TODO find why the GtkImageMenuItem proxy is created */
212 if (! GTK_IS_MENU_TOOL_BUTTON(l->data))
213 continue;
215 if (enable)
217 if (gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(l->data)) == NULL)
218 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l->data), priv->menu);
220 else
221 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l->data), NULL);
226 void geany_menu_button_action_set_menu(GeanyMenubuttonAction *action, GtkWidget *menu)
228 GeanyMenubuttonActionPrivate *priv;
230 g_return_if_fail(action != NULL);
232 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
234 if (priv->menu != NULL && GTK_IS_WIDGET(priv->menu))
235 g_signal_handlers_disconnect_by_func(priv->menu, menu_items_changed_cb, action);
236 if (menu != NULL)
238 g_signal_connect(menu, "add", G_CALLBACK(menu_items_changed_cb), action);
239 g_signal_connect(menu, "remove", G_CALLBACK(menu_items_changed_cb), action);
242 priv->menu = menu;
244 menu_items_changed_cb(GTK_CONTAINER(menu), NULL, action);