2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant 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 GtkRadiant 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 GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <uilib/uilib.h>
27 #include <debugging/debugging.h>
29 #include "generic/callback.h"
31 #include "accelerator.h"
33 #include "container.h"
36 void menu_add_item(ui::Menu menu, ui::MenuItem item)
41 ui::MenuItem menu_separator(ui::Menu menu)
43 auto menu_item = ui::MenuItem::from(gtk_menu_item_new());
45 gtk_widget_set_sensitive(menu_item, FALSE);
50 ui::TearoffMenuItem menu_tearoff(ui::Menu menu)
52 auto menu_item = ui::TearoffMenuItem::from(gtk_tearoff_menu_item_new());
54 // gtk_widget_set_sensitive(menu_item, FALSE); -- controls whether menu is detachable
59 ui::MenuItem new_sub_menu_item_with_mnemonic(const char *mnemonic)
61 auto item = ui::MenuItem(mnemonic, true);
64 auto sub_menu = ui::Menu(ui::New);
65 gtk_menu_item_set_submenu(item, sub_menu);
70 ui::Menu create_sub_menu_with_mnemonic(ui::MenuShell parent, const char *mnemonic)
72 auto item = new_sub_menu_item_with_mnemonic(mnemonic);
74 return ui::Menu::from(gtk_menu_item_get_submenu(item));
77 ui::Menu create_sub_menu_with_mnemonic(ui::MenuBar bar, const char *mnemonic)
79 return create_sub_menu_with_mnemonic(ui::MenuShell::from(bar._handle), mnemonic);
82 ui::Menu create_sub_menu_with_mnemonic(ui::Menu parent, const char *mnemonic)
84 return create_sub_menu_with_mnemonic(ui::MenuShell::from(parent._handle), mnemonic);
87 void activate_closure_callback(ui::Widget widget, gpointer data)
89 (*reinterpret_cast<Callback<void()> *>( data ))();
92 guint menu_item_connect_callback(ui::MenuItem item, const Callback<void()> &callback)
95 return g_signal_connect_swapped(G_OBJECT(item), "activate", G_CALLBACK(callback.getThunk()),
96 callback.getEnvironment());
98 return g_signal_connect_closure( G_OBJECT( item ), "activate", create_cclosure( G_CALLBACK( activate_closure_callback ), callback ), FALSE );
102 guint check_menu_item_connect_callback(ui::CheckMenuItem item, const Callback<void()> &callback)
105 guint handler = g_signal_connect_swapped(G_OBJECT(item), "toggled", G_CALLBACK(callback.getThunk()),
106 callback.getEnvironment());
108 guint handler = g_signal_connect_closure( G_OBJECT( item ), "toggled", create_cclosure( G_CALLBACK( activate_closure_callback ), callback ), TRUE );
110 g_object_set_data(G_OBJECT(item), "handler", gint_to_pointer(handler));
114 ui::MenuItem new_menu_item_with_mnemonic(const char *mnemonic, const Callback<void()> &callback)
116 auto item = ui::MenuItem(mnemonic, true);
118 menu_item_connect_callback(item, callback);
122 ui::MenuItem create_menu_item_with_mnemonic(ui::Menu menu, const char *mnemonic, const Callback<void()> &callback)
124 auto item = new_menu_item_with_mnemonic(mnemonic, callback);
129 ui::CheckMenuItem new_check_menu_item_with_mnemonic(const char *mnemonic, const Callback<void()> &callback)
131 auto item = ui::CheckMenuItem::from(gtk_check_menu_item_new_with_mnemonic(mnemonic));
133 check_menu_item_connect_callback(item, callback);
138 create_check_menu_item_with_mnemonic(ui::Menu menu, const char *mnemonic, const Callback<void()> &callback)
140 auto item = new_check_menu_item_with_mnemonic(mnemonic, callback);
146 new_radio_menu_item_with_mnemonic(GSList **group, const char *mnemonic, const Callback<void()> &callback)
148 auto item = ui::RadioMenuItem::from(gtk_radio_menu_item_new_with_mnemonic(*group, mnemonic));
150 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
152 *group = gtk_radio_menu_item_get_group(item);
154 check_menu_item_connect_callback(item, callback);
158 ui::RadioMenuItem create_radio_menu_item_with_mnemonic(ui::Menu menu, GSList **group, const char *mnemonic,
159 const Callback<void()> &callback)
161 auto item = new_radio_menu_item_with_mnemonic(group, mnemonic, callback);
166 void check_menu_item_set_active_no_signal(ui::CheckMenuItem item, gboolean active)
168 guint handler_id = gpointer_to_int(g_object_get_data(G_OBJECT(item), "handler"));
169 g_signal_handler_block(G_OBJECT(item), handler_id);
170 gtk_check_menu_item_set_active(item, active);
171 g_signal_handler_unblock(G_OBJECT(item), handler_id);
175 void radio_menu_item_set_active_no_signal(ui::RadioMenuItem item, gboolean active)
178 for (GSList *l = gtk_radio_menu_item_get_group(item); l != 0; l = g_slist_next(l)) {
179 g_signal_handler_block(G_OBJECT(l->data), gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
182 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), active);
184 for (GSList *l = gtk_radio_menu_item_get_group(item); l != 0; l = g_slist_next(l)) {
185 g_signal_handler_unblock(G_OBJECT(l->data),
186 gpointer_to_int(g_object_get_data(G_OBJECT(l->data), "handler")));
192 void menu_item_set_accelerator(ui::MenuItem item, GClosure *closure)
194 GtkAccelLabel *accel_label = GTK_ACCEL_LABEL(gtk_bin_get_child(GTK_BIN(item)));
195 gtk_accel_label_set_accel_closure(accel_label, closure);
198 void accelerator_name(const Accelerator &accelerator, GString *gstring)
200 gboolean had_mod = FALSE;
201 if (accelerator.modifiers & GDK_SHIFT_MASK) {
202 g_string_append(gstring, "Shift");
205 if (accelerator.modifiers & GDK_CONTROL_MASK) {
207 g_string_append(gstring, "+");
209 g_string_append(gstring, "Ctrl");
212 if (accelerator.modifiers & GDK_MOD1_MASK) {
214 g_string_append(gstring, "+");
216 g_string_append(gstring, "Alt");
221 g_string_append(gstring, "+");
223 if (accelerator.key < 0x80 || (accelerator.key > 0x80 && accelerator.key <= 0xff)) {
224 switch (accelerator.key) {
226 g_string_append(gstring, "Space");
229 g_string_append(gstring, "Backslash");
232 g_string_append_c(gstring, gchar(toupper(accelerator.key)));
238 tmp = gtk_accelerator_name(accelerator.key, (GdkModifierType) 0);
239 if (tmp[0] != 0 && tmp[1] == 0) {
240 tmp[0] = gchar(toupper(tmp[0]));
242 g_string_append(gstring, tmp);
247 void menu_item_add_accelerator(ui::MenuItem item, Accelerator accelerator)
249 if (accelerator.key != 0 && gtk_accelerator_valid(accelerator.key, accelerator.modifiers)) {
250 GClosure *closure = global_accel_group_find(accelerator);
251 ASSERT_NOTNULL(closure);
252 menu_item_set_accelerator(item, closure);
256 ui::MenuItem create_menu_item_with_mnemonic(ui::Menu menu, const char *mnemonic, const Command &command)
258 auto item = create_menu_item_with_mnemonic(menu, mnemonic, command.m_callback);
259 menu_item_add_accelerator(item, command.m_accelerator);
263 void check_menu_item_set_active_callback(void *it, bool enabled)
265 auto item = ui::CheckMenuItem::from(it);
266 check_menu_item_set_active_no_signal(item, enabled);
269 ui::CheckMenuItem create_check_menu_item_with_mnemonic(ui::Menu menu, const char *mnemonic, const Toggle &toggle)
271 auto item = create_check_menu_item_with_mnemonic(menu, mnemonic, toggle.m_command.m_callback);
272 menu_item_add_accelerator(item, toggle.m_command.m_accelerator);
273 toggle.m_exportCallback(PointerCaller<void, void(bool), check_menu_item_set_active_callback>(item._handle));