2 * geanymenubuttonaction.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* GtkAction subclass to provide a GtkMenuToolButton in a toolbar.
22 * This class is missing the action_create_menu_item() function and so can't be
23 * used for creating menu items. */
29 #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 geany_menu_button_action_set_property(GObject
*object
, guint prop_id
,
80 const GValue
*value
, GParamSpec
*pspec
)
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
);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
98 static GtkWidget
*geany_menu_button_action_create_tool_item(GtkAction
*action
)
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
);
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
,
129 "A special tooltip for the arrow button",
133 signals
[BUTTON_CLICKED
] = g_signal_new("button-clicked",
134 G_TYPE_FROM_CLASS(klass
),
139 g_cclosure_marshal_VOID__VOID
,
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
);
152 priv
->tooltip_arrow
= NULL
;
157 GtkAction
*geany_menu_button_action_new(const gchar
*name
,
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
,
167 "tooltip-arrow", tooltip_arrow
,
168 "stock-id", stock_id
,
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
);
187 static void menu_items_changed_cb(GtkContainer
*container
, GtkWidget
*widget
, GeanyMenubuttonAction
*action
)
189 GeanyMenubuttonActionPrivate
*priv
;
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
);
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
))
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
);
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
);
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
);
243 menu_items_changed_cb(GTK_CONTAINER(menu
), NULL
, action
);