Merge pull request #232 from stefan-it/enhancement/lang-de-corrections
[geany-mirror.git] / src / geanymenubuttonaction.c
blob4f4dfbaeb9ed63469bf270910ea7a6ae29c828df
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. */
27 #include "geany.h"
28 #include "support.h"
29 #include "utils.h"
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
40 GtkWidget *menu;
42 gchar *tooltip_arrow;
45 enum
47 PROP_0,
48 PROP_TOOLTIP_ARROW
51 enum
53 BUTTON_CLICKED,
54 LAST_SIGNAL
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 geany_menu_button_action_set_property(GObject *object, guint prop_id,
80 const GValue *value, GParamSpec *pspec)
82 switch (prop_id)
84 case PROP_TOOLTIP_ARROW:
86 GeanyMenubuttonActionPrivate *priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(object);
87 g_free(priv->tooltip_arrow);
88 priv->tooltip_arrow = g_value_dup_string(value);
89 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
93 break;
98 static GtkWidget *geany_menu_button_action_create_tool_item(GtkAction *action)
100 GtkWidget *toolitem;
101 GeanyMenubuttonActionPrivate *priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
103 toolitem = g_object_new(GTK_TYPE_MENU_TOOL_BUTTON, NULL);
104 gtk_menu_tool_button_set_arrow_tooltip_text(GTK_MENU_TOOL_BUTTON(toolitem), priv->tooltip_arrow);
106 return toolitem;
110 static void geany_menu_button_action_class_init(GeanyMenubuttonActionClass *klass)
112 GtkActionClass *action_class = GTK_ACTION_CLASS(klass);
113 GObjectClass *g_object_class = G_OBJECT_CLASS(klass);
115 g_object_class->finalize = geany_menu_button_action_finalize;
116 g_object_class->set_property = geany_menu_button_action_set_property;
118 action_class->activate = delegate_button_activated;
119 action_class->create_tool_item = geany_menu_button_action_create_tool_item;
120 action_class->toolbar_item_type = GTK_TYPE_MENU_TOOL_BUTTON;
122 g_type_class_add_private(klass, sizeof(GeanyMenubuttonActionPrivate));
124 g_object_class_install_property(g_object_class,
125 PROP_TOOLTIP_ARROW,
126 g_param_spec_string(
127 "tooltip-arrow",
128 "Arrow tooltip",
129 "A special tooltip for the arrow button",
131 G_PARAM_WRITABLE));
133 signals[BUTTON_CLICKED] = g_signal_new("button-clicked",
134 G_TYPE_FROM_CLASS(klass),
135 (GSignalFlags) 0,
138 NULL,
139 g_cclosure_marshal_VOID__VOID,
140 G_TYPE_NONE, 0);
144 static void geany_menu_button_action_init(GeanyMenubuttonAction *action)
146 GeanyMenubuttonActionPrivate *priv;
148 action->priv = G_TYPE_INSTANCE_GET_PRIVATE(action,
149 GEANY_MENU_BUTTON_ACTION_TYPE, GeanyMenubuttonActionPrivate);
151 priv = action->priv;
152 priv->tooltip_arrow = NULL;
153 priv->menu = NULL;
157 GtkAction *geany_menu_button_action_new(const gchar *name,
158 const gchar *label,
159 const gchar *tooltip,
160 const gchar *tooltip_arrow,
161 const gchar *stock_id)
163 GtkAction *action = g_object_new(GEANY_MENU_BUTTON_ACTION_TYPE,
164 "name", name,
165 "label", label,
166 "tooltip", tooltip,
167 "tooltip-arrow", tooltip_arrow,
168 "stock-id", stock_id,
169 NULL);
171 return action;
175 GtkWidget *geany_menu_button_action_get_menu(GeanyMenubuttonAction *action)
177 GeanyMenubuttonActionPrivate *priv;
179 g_return_val_if_fail(action != NULL, NULL);
181 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
183 return priv->menu;
187 static void menu_items_changed_cb(GtkContainer *container, GtkWidget *widget, GeanyMenubuttonAction *action)
189 GeanyMenubuttonActionPrivate *priv;
190 gboolean enable;
191 GSList *l;
193 g_return_if_fail(action != NULL);
195 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
196 if (priv->menu != NULL)
198 GList *children = gtk_container_get_children(GTK_CONTAINER(priv->menu));
200 enable = (g_list_length(children) > 0);
201 g_list_free(children);
203 else
204 enable = FALSE;
206 foreach_slist(l, gtk_action_get_proxies(GTK_ACTION(action)))
208 /* On Windows a GtkImageMenuItem proxy is created for whatever reason. So we filter
209 * by type and act only on GtkMenuToolButton proxies. */
210 /* TODO find why the GtkImageMenuItem proxy is created */
211 if (! GTK_IS_MENU_TOOL_BUTTON(l->data))
212 continue;
214 if (enable)
216 if (gtk_menu_tool_button_get_menu(GTK_MENU_TOOL_BUTTON(l->data)) == NULL)
217 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l->data), priv->menu);
219 else
220 gtk_menu_tool_button_set_menu(GTK_MENU_TOOL_BUTTON(l->data), NULL);
225 void geany_menu_button_action_set_menu(GeanyMenubuttonAction *action, GtkWidget *menu)
227 GeanyMenubuttonActionPrivate *priv;
229 g_return_if_fail(action != NULL);
231 priv = GEANY_MENU_BUTTON_ACTION_GET_PRIVATE(action);
233 if (priv->menu != NULL && GTK_IS_WIDGET(priv->menu))
234 g_signal_handlers_disconnect_by_func(priv->menu, menu_items_changed_cb, action);
235 if (menu != NULL)
237 g_signal_connect(menu, "add", G_CALLBACK(menu_items_changed_cb), action);
238 g_signal_connect(menu, "remove", G_CALLBACK(menu_items_changed_cb), action);
241 priv->menu = menu;
243 menu_items_changed_cb(GTK_CONTAINER(menu), NULL, action);