]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/gtk/menu.cpp |
28fcfbfe | 3 | // Purpose: implementation of wxMenuBar and wxMenu classes for wxGTK |
c801d85f | 4 | // Author: Robert Roebling |
96fd301f | 5 | // Id: $Id$ |
a81258be | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
28fcfbfe VZ |
13 | #if wxUSE_MENUS |
14 | ||
e1bf3ad3 | 15 | #include "wx/menu.h" |
88a7a4e1 WS |
16 | |
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
e4db172a | 19 | #include "wx/log.h" |
d281df50 | 20 | #include "wx/frame.h" |
0bca0373 | 21 | #include "wx/bitmap.h" |
7d02e0d6 | 22 | #include "wx/app.h" |
88a7a4e1 WS |
23 | #endif |
24 | ||
d281df50 | 25 | #include "wx/accel.h" |
ab73fe8d | 26 | #include "wx/stockitem.h" |
9dc44eff PC |
27 | |
28 | #include <gtk/gtk.h> | |
9e691f46 | 29 | #include "wx/gtk/private.h" |
9dc44eff | 30 | #include "wx/gtk/private/gtk2-compat.h" |
b1f17bf0 | 31 | #include "wx/gtk/private/mnemonics.h" |
9e691f46 | 32 | |
9959e2b6 VZ |
33 | // we use normal item but with a special id for the menu title |
34 | static const int wxGTK_TITLE_ID = -3; | |
35 | ||
5e6f2fe9 VZ |
36 | // forward declare it as it's used by wxMenuBar too when using Hildon |
37 | extern "C" | |
38 | { | |
e2147e6d | 39 | static void menuitem_activate(GtkWidget*, wxMenuItem* item); |
5e6f2fe9 | 40 | } |
6c76d1ce | 41 | |
6d971354 | 42 | #if wxUSE_ACCEL |
1deef997 | 43 | static void wxGetGtkAccel(const wxMenuItem*, guint*, GdkModifierType*); |
717a57c2 VZ |
44 | #endif |
45 | ||
cdf003d4 | 46 | static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event) |
e6396ed4 | 47 | { |
e6396ed4 JS |
48 | event.SetEventObject( menu ); |
49 | ||
a01fe3d6 | 50 | wxEvtHandler* handler = menu->GetEventHandler(); |
937013e0 | 51 | if (handler && handler->SafelyProcessEvent(event)) |
e6396ed4 JS |
52 | return; |
53 | ||
6abf7b63 VZ |
54 | wxWindow *win = menu->GetWindow(); |
55 | wxCHECK_RET( win, "event for a menu without associated window?" ); | |
56 | ||
57 | win->HandleWindowEvent( event ); | |
cdf003d4 VZ |
58 | } |
59 | ||
c801d85f KB |
60 | //----------------------------------------------------------------------------- |
61 | // wxMenuBar | |
62 | //----------------------------------------------------------------------------- | |
63 | ||
12a5cf13 PC |
64 | wxMenuBar::~wxMenuBar() |
65 | { | |
4fcad4fb | 66 | if (m_widget && IsAttached()) |
12a5cf13 PC |
67 | { |
68 | // Work around a probable bug in Ubuntu 12.04 which causes a warning if | |
69 | // gtk_widget_destroy() is called on a wxMenuBar attached to a frame | |
70 | GtkWidget* widget = m_widget; | |
e2f268db | 71 | m_focusWidget = |
12a5cf13 | 72 | m_widget = NULL; |
efc645e2 PC |
73 | g_signal_handlers_disconnect_matched(widget, |
74 | GSignalMatchType(G_SIGNAL_MATCH_DATA), 0, 0, NULL, NULL, this); | |
12a5cf13 PC |
75 | g_object_unref(widget); |
76 | } | |
77 | } | |
78 | ||
294ea16d | 79 | void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style) |
3502e687 | 80 | { |
426d19f1 | 81 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
e2f3bc41 VZ |
82 | // Hildon window uses a single menu instead of a menu bar, so wxMenuBar is |
83 | // the same as menu in this case | |
84 | m_widget = | |
85 | m_menubar = gtk_menu_new(); | |
426d19f1 | 86 | #else // !wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
e8375af8 VZ |
87 | if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) || |
88 | !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) | |
4dcaf11a | 89 | { |
223d09f6 | 90 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); |
455fadaa | 91 | return; |
4dcaf11a | 92 | } |
3502e687 | 93 | |
3502e687 RR |
94 | m_menubar = gtk_menu_bar_new(); |
95 | ||
96 | if (style & wxMB_DOCKABLE) | |
97 | { | |
98 | m_widget = gtk_handle_box_new(); | |
10bd1f7d PC |
99 | gtk_container_add(GTK_CONTAINER(m_widget), m_menubar); |
100 | gtk_widget_show(m_menubar); | |
3502e687 RR |
101 | } |
102 | else | |
103 | { | |
10bd1f7d | 104 | m_widget = m_menubar; |
3502e687 RR |
105 | } |
106 | ||
107 | PostCreation(); | |
c4608a8a | 108 | |
496e7ec6 | 109 | GTKApplyWidgetStyle(); |
426d19f1 | 110 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2/!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
294ea16d | 111 | |
12a5cf13 | 112 | g_object_ref_sink(m_widget); |
9ff9d30c | 113 | |
294ea16d VZ |
114 | for (size_t i = 0; i < n; ++i ) |
115 | Append(menus[i], titles[i]); | |
3502e687 RR |
116 | } |
117 | ||
294ea16d | 118 | wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style) |
c801d85f | 119 | { |
294ea16d VZ |
120 | Init(n, menus, titles, style); |
121 | } | |
96fd301f | 122 | |
294ea16d VZ |
123 | wxMenuBar::wxMenuBar(long style) |
124 | { | |
125 | Init(0, NULL, NULL, style); | |
126 | } | |
c4608a8a | 127 | |
294ea16d VZ |
128 | wxMenuBar::wxMenuBar() |
129 | { | |
130 | Init(0, NULL, NULL, 0); | |
6de97a3b | 131 | } |
c801d85f | 132 | |
6abf7b63 VZ |
133 | // recursive helpers for wxMenuBar::Attach() and Detach(): they are called to |
134 | // associate the menus with the frame they belong to or dissociate them from it | |
135 | namespace | |
5bd9e519 | 136 | { |
5bd9e519 | 137 | |
6abf7b63 VZ |
138 | void |
139 | DetachFromFrame(wxMenu* menu, wxFrame* frame) | |
140 | { | |
05b743ba | 141 | // support for native hot keys |
8ef9a526 PC |
142 | if (menu->m_accel) |
143 | { | |
6abf7b63 VZ |
144 | // Note that wxGetTopLevelParent() is really needed because this frame |
145 | // can be an MDI child frame which is a fake frame and not a TLW at all | |
146 | GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget); | |
9dc44eff | 147 | if (g_slist_find(gtk_accel_groups_from_object(G_OBJECT(tlw)), menu->m_accel)) |
8ef9a526 PC |
148 | gtk_window_remove_accel_group(tlw, menu->m_accel); |
149 | } | |
05b743ba | 150 | |
222ed1d6 | 151 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
152 | while (node) |
153 | { | |
1987af7e | 154 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 | 155 | if (menuitem->IsSubMenu()) |
6abf7b63 | 156 | DetachFromFrame(menuitem->GetSubMenu(), frame); |
1987af7e | 157 | node = node->GetNext(); |
5bd9e519 RR |
158 | } |
159 | } | |
160 | ||
6abf7b63 VZ |
161 | void |
162 | AttachToFrame(wxMenu* menu, wxFrame* frame) | |
5bd9e519 | 163 | { |
6d971354 | 164 | // support for native hot keys |
8ef9a526 PC |
165 | if (menu->m_accel) |
166 | { | |
6abf7b63 | 167 | GtkWindow * const tlw = GTK_WINDOW(wxGetTopLevelParent(frame)->m_widget); |
9dc44eff | 168 | if (!g_slist_find(gtk_accel_groups_from_object(G_OBJECT(tlw)), menu->m_accel)) |
8ef9a526 PC |
169 | gtk_window_add_accel_group(tlw, menu->m_accel); |
170 | } | |
5bd9e519 | 171 | |
222ed1d6 | 172 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
5bd9e519 RR |
173 | while (node) |
174 | { | |
1987af7e | 175 | wxMenuItem *menuitem = node->GetData(); |
5bd9e519 | 176 | if (menuitem->IsSubMenu()) |
6abf7b63 | 177 | AttachToFrame(menuitem->GetSubMenu(), frame); |
1987af7e | 178 | node = node->GetNext(); |
5bd9e519 RR |
179 | } |
180 | } | |
181 | ||
6abf7b63 | 182 | } // anonymous namespace |
5bd9e519 | 183 | |
978af864 VZ |
184 | void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir) |
185 | { | |
186 | if ( dir == wxLayout_Default ) | |
187 | { | |
188 | const wxWindow *const frame = GetFrame(); | |
189 | if ( frame ) | |
190 | { | |
191 | // inherit layout from frame. | |
192 | dir = frame->GetLayoutDirection(); | |
193 | } | |
194 | else // use global layout | |
195 | { | |
196 | dir = wxTheApp->GetLayoutDirection(); | |
197 | } | |
198 | } | |
199 | ||
200 | if ( dir == wxLayout_Default ) | |
201 | return; | |
202 | ||
203 | GTKSetLayout(m_menubar, dir); | |
204 | ||
205 | // also set the layout of all menus we already have (new ones will inherit | |
206 | // the current layout) | |
207 | for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
208 | node; | |
209 | node = node->GetNext() ) | |
210 | { | |
211 | wxMenu *const menu = node->GetData(); | |
212 | menu->SetLayoutDirection(dir); | |
213 | } | |
214 | } | |
215 | ||
216 | wxLayoutDirection wxMenuBar::GetLayoutDirection() const | |
217 | { | |
218 | return GTKGetLayout(m_menubar); | |
219 | } | |
220 | ||
221 | void wxMenuBar::Attach(wxFrame *frame) | |
222 | { | |
223 | wxMenuBarBase::Attach(frame); | |
224 | ||
6abf7b63 VZ |
225 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
226 | while (node) | |
227 | { | |
228 | wxMenu *menu = node->GetData(); | |
229 | AttachToFrame( menu, frame ); | |
230 | node = node->GetNext(); | |
231 | } | |
232 | ||
978af864 VZ |
233 | SetLayoutDirection(wxLayout_Default); |
234 | } | |
235 | ||
6abf7b63 | 236 | void wxMenuBar::Detach() |
5bd9e519 | 237 | { |
222ed1d6 | 238 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
5bd9e519 RR |
239 | while (node) |
240 | { | |
1987af7e | 241 | wxMenu *menu = node->GetData(); |
6abf7b63 | 242 | DetachFromFrame( menu, m_menuBarFrame ); |
1987af7e | 243 | node = node->GetNext(); |
5bd9e519 | 244 | } |
6abf7b63 VZ |
245 | |
246 | wxMenuBarBase::Detach(); | |
5bd9e519 RR |
247 | } |
248 | ||
3dfac970 | 249 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) |
c801d85f | 250 | { |
0f29501b PC |
251 | if (wxMenuBarBase::Append(menu, title)) |
252 | { | |
253 | GtkAppend(menu, title); | |
254 | return true; | |
255 | } | |
256 | return false; | |
f03ec224 | 257 | } |
23280650 | 258 | |
0f29501b | 259 | void wxMenuBar::GtkAppend(wxMenu* menu, const wxString& title, int pos) |
f03ec224 | 260 | { |
6c76d1ce | 261 | menu->SetLayoutDirection(GetLayoutDirection()); |
1e133b7d | 262 | |
426d19f1 | 263 | #if wxUSE_LIBHILDON || wxUSE_LIBHILDON2 |
6c76d1ce VZ |
264 | // if the menu has only one item, append it directly to the top level menu |
265 | // instead of inserting a useless submenu | |
266 | if ( menu->GetMenuItemCount() == 1 ) | |
267 | { | |
268 | wxMenuItem * const item = menu->FindItemByPosition(0); | |
23280650 | 269 | |
6c76d1ce VZ |
270 | // remove both mnemonics and accelerator: neither is useful under Maemo |
271 | const wxString str(wxStripMenuCodes(item->GetItemLabel())); | |
96fd301f | 272 | |
6c76d1ce | 273 | if ( item->IsSubMenu() ) |
0f29501b PC |
274 | { |
275 | GtkAppend(item->GetSubMenu(), str, pos); | |
276 | return; | |
277 | } | |
6c76d1ce VZ |
278 | |
279 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); | |
9959e2b6 | 280 | |
6c76d1ce | 281 | g_signal_connect(menu->m_owner, "activate", |
e2147e6d | 282 | G_CALLBACK(menuitem_activate), item); |
6c76d1ce VZ |
283 | item->SetMenuItem(menu->m_owner); |
284 | } | |
285 | else | |
426d19f1 | 286 | #endif // wxUSE_LIBHILDON || wxUSE_LIBHILDON2 /!wxUSE_LIBHILDON && !wxUSE_LIBHILDON2 |
6c76d1ce VZ |
287 | { |
288 | const wxString str(wxConvertMnemonicsToGTK(title)); | |
289 | ||
290 | // This doesn't have much effect right now. | |
291 | menu->SetTitle( str ); | |
292 | ||
293 | // The "m_owner" is the "menu item" | |
294 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); | |
295 | ||
296 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
297 | } | |
298 | ||
299 | gtk_widget_show( menu->m_owner ); | |
9959e2b6 | 300 | |
49826dab RD |
301 | if (pos == -1) |
302 | gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner ); | |
303 | else | |
304 | gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos ); | |
9959e2b6 | 305 | |
6abf7b63 VZ |
306 | if ( m_menuBarFrame ) |
307 | AttachToFrame( menu, m_menuBarFrame ); | |
3dfac970 VZ |
308 | } |
309 | ||
310 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
311 | { | |
0f29501b PC |
312 | if (wxMenuBarBase::Insert(pos, menu, title)) |
313 | { | |
314 | GtkAppend(menu, title, int(pos)); | |
315 | return true; | |
316 | } | |
317 | return false; | |
3dfac970 VZ |
318 | } |
319 | ||
320 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
321 | { | |
f03ec224 VZ |
322 | // remove the old item and insert a new one |
323 | wxMenu *menuOld = Remove(pos); | |
324 | if ( menuOld && !Insert(pos, menu, title) ) | |
325 | { | |
d3b9f782 | 326 | return NULL; |
f03ec224 | 327 | } |
3dfac970 | 328 | |
f03ec224 VZ |
329 | // either Insert() succeeded or Remove() failed and menuOld is NULL |
330 | return menuOld; | |
3dfac970 VZ |
331 | } |
332 | ||
333 | wxMenu *wxMenuBar::Remove(size_t pos) | |
334 | { | |
f03ec224 VZ |
335 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
336 | if ( !menu ) | |
d3b9f782 | 337 | return NULL; |
f03ec224 | 338 | |
6f536f31 | 339 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->m_owner), NULL); |
defc0789 | 340 | gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner); |
c4608a8a | 341 | |
1d62a8b4 | 342 | gtk_widget_destroy( menu->m_owner ); |
defc0789 | 343 | menu->m_owner = NULL; |
c4608a8a | 344 | |
64b78864 VZ |
345 | if ( m_menuBarFrame ) |
346 | DetachFromFrame( menu, m_menuBarFrame ); | |
e4161a2a | 347 | |
1d62a8b4 | 348 | return menu; |
6de97a3b | 349 | } |
96fd301f | 350 | |
716b7364 | 351 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) |
c801d85f | 352 | { |
7bc0ff86 | 353 | if (wxMenuItem::GetLabelText(menu->GetTitle()) == wxMenuItem::GetLabelText(menuString)) |
83624f79 RR |
354 | { |
355 | int res = menu->FindItem( itemString ); | |
c626a8b7 VZ |
356 | if (res != wxNOT_FOUND) |
357 | return res; | |
83624f79 | 358 | } |
c626a8b7 | 359 | |
222ed1d6 | 360 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
83624f79 RR |
361 | while (node) |
362 | { | |
1987af7e | 363 | wxMenuItem *item = node->GetData(); |
83624f79 RR |
364 | if (item->IsSubMenu()) |
365 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
2b1c162e | 366 | |
1987af7e | 367 | node = node->GetNext(); |
83624f79 RR |
368 | } |
369 | ||
c626a8b7 VZ |
370 | return wxNOT_FOUND; |
371 | } | |
372 | ||
c801d85f KB |
373 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const |
374 | { | |
222ed1d6 | 375 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
83624f79 RR |
376 | while (node) |
377 | { | |
1987af7e | 378 | wxMenu *menu = node->GetData(); |
83624f79 | 379 | int res = FindMenuItemRecursive( menu, menuString, itemString); |
1987af7e VZ |
380 | if (res != -1) |
381 | return res; | |
382 | node = node->GetNext(); | |
83624f79 | 383 | } |
1987af7e VZ |
384 | |
385 | return wxNOT_FOUND; | |
6de97a3b | 386 | } |
c801d85f | 387 | |
c626a8b7 | 388 | // Find a wxMenuItem using its id. Recurses down into sub-menus |
96fd301f | 389 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) |
716b7364 | 390 | { |
717a57c2 | 391 | wxMenuItem* result = menu->FindChildItem(id); |
716b7364 | 392 | |
222ed1d6 | 393 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); |
c626a8b7 | 394 | while ( node && result == NULL ) |
83624f79 | 395 | { |
1987af7e | 396 | wxMenuItem *item = node->GetData(); |
83624f79 | 397 | if (item->IsSubMenu()) |
c626a8b7 | 398 | { |
83624f79 | 399 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); |
c626a8b7 | 400 | } |
1987af7e | 401 | node = node->GetNext(); |
83624f79 | 402 | } |
96fd301f | 403 | |
83624f79 | 404 | return result; |
6de97a3b | 405 | } |
716b7364 | 406 | |
3dfac970 | 407 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const |
716b7364 | 408 | { |
83624f79 | 409 | wxMenuItem* result = 0; |
222ed1d6 | 410 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); |
83624f79 RR |
411 | while (node && result == 0) |
412 | { | |
1987af7e | 413 | wxMenu *menu = node->GetData(); |
83624f79 | 414 | result = FindMenuItemByIdRecursive( menu, id ); |
1987af7e | 415 | node = node->GetNext(); |
83624f79 | 416 | } |
c626a8b7 | 417 | |
3dfac970 VZ |
418 | if ( menuForItem ) |
419 | { | |
d3b9f782 | 420 | *menuForItem = result ? result->GetMenu() : NULL; |
3dfac970 | 421 | } |
c626a8b7 | 422 | |
3dfac970 | 423 | return result; |
bbe0af5b RR |
424 | } |
425 | ||
3dfac970 | 426 | void wxMenuBar::EnableTop( size_t pos, bool flag ) |
bbe0af5b | 427 | { |
222ed1d6 | 428 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 429 | |
223d09f6 | 430 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 431 | |
3dfac970 | 432 | wxMenu* menu = node->GetData(); |
c626a8b7 VZ |
433 | |
434 | if (menu->m_owner) | |
435 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
bbe0af5b RR |
436 | } |
437 | ||
e4a23857 VZ |
438 | bool wxMenuBar::IsEnabledTop(size_t pos) const |
439 | { | |
440 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
441 | wxCHECK_MSG( node, false, wxS("invalid index in IsEnabledTop") ); | |
442 | wxMenu* const menu = node->GetData(); | |
443 | wxCHECK_MSG( menu->m_owner, true, wxS("no menu owner?") ); | |
d5027818 | 444 | return gtk_widget_get_sensitive( menu->m_owner ) != 0; |
e4a23857 VZ |
445 | } |
446 | ||
52af3158 | 447 | wxString wxMenuBar::GetMenuLabel( size_t pos ) const |
bbe0af5b | 448 | { |
222ed1d6 | 449 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 450 | |
223d09f6 | 451 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); |
c626a8b7 | 452 | |
3dfac970 | 453 | wxMenu* menu = node->GetData(); |
c626a8b7 | 454 | |
7bc0ff86 | 455 | return menu->GetTitle(); |
bbe0af5b RR |
456 | } |
457 | ||
52af3158 | 458 | void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label ) |
bbe0af5b | 459 | { |
222ed1d6 | 460 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); |
c626a8b7 | 461 | |
223d09f6 | 462 | wxCHECK_RET( node, wxT("menu not found") ); |
c626a8b7 | 463 | |
3dfac970 | 464 | wxMenu* menu = node->GetData(); |
c626a8b7 | 465 | |
b1f17bf0 | 466 | const wxString str(wxConvertMnemonicsToGTK(label)); |
f6bcfd97 BP |
467 | |
468 | menu->SetTitle( str ); | |
469 | ||
470 | if (menu->m_owner) | |
385e8575 | 471 | gtk_label_set_text_with_mnemonic(GTK_LABEL(gtk_bin_get_child(GTK_BIN(menu->m_owner))), wxGTK_CONV(str)); |
bbe0af5b RR |
472 | } |
473 | ||
c801d85f | 474 | //----------------------------------------------------------------------------- |
cf7a7e13 | 475 | // "activate" |
c801d85f KB |
476 | //----------------------------------------------------------------------------- |
477 | ||
865bb325 | 478 | extern "C" { |
e2147e6d | 479 | static void menuitem_activate(GtkWidget*, wxMenuItem* item) |
c801d85f | 480 | { |
e2147e6d | 481 | if (!item->IsEnabled()) |
c626a8b7 | 482 | return; |
96fd301f | 483 | |
e2147e6d PC |
484 | int id = item->GetId(); |
485 | if (id == wxGTK_TITLE_ID) | |
9959e2b6 VZ |
486 | { |
487 | // ignore events from the menu title | |
488 | return; | |
489 | } | |
490 | ||
4e6beae5 RD |
491 | if (item->IsCheckable()) |
492 | { | |
493 | bool isReallyChecked = item->IsChecked(), | |
494 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); | |
495 | ||
496 | // ensure that the internal state is always consistent with what is | |
497 | // shown on the screen | |
498 | item->wxMenuItemBase::Check(isReallyChecked); | |
499 | ||
500 | // we must not report the events for the radio button going up nor the | |
501 | // events resulting from the calls to wxMenuItem::Check() | |
502 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || | |
503 | (isInternallyChecked == isReallyChecked) ) | |
504 | { | |
505 | return; | |
506 | } | |
507 | } | |
508 | ||
e2147e6d | 509 | wxMenu* menu = item->GetMenu(); |
e98c0655 | 510 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); |
cf7a7e13 | 511 | } |
865bb325 | 512 | } |
cf7a7e13 RR |
513 | |
514 | //----------------------------------------------------------------------------- | |
515 | // "select" | |
516 | //----------------------------------------------------------------------------- | |
517 | ||
865bb325 | 518 | extern "C" { |
e2147e6d | 519 | static void menuitem_select(GtkWidget*, wxMenuItem* item) |
cf7a7e13 | 520 | { |
e2147e6d | 521 | if (!item->IsEnabled()) |
c626a8b7 | 522 | return; |
cf7a7e13 | 523 | |
e2147e6d | 524 | wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item->GetId()); |
341d8cb2 | 525 | DoCommonMenuCallbackCode(item->GetMenu(), event); |
6de97a3b | 526 | } |
865bb325 | 527 | } |
c801d85f | 528 | |
cd743a6f RR |
529 | //----------------------------------------------------------------------------- |
530 | // "deselect" | |
531 | //----------------------------------------------------------------------------- | |
532 | ||
865bb325 | 533 | extern "C" { |
e2147e6d | 534 | static void menuitem_deselect(GtkWidget*, wxMenuItem* item) |
cd743a6f | 535 | { |
e2147e6d | 536 | if (!item->IsEnabled()) |
c626a8b7 | 537 | return; |
cd743a6f RR |
538 | |
539 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
341d8cb2 | 540 | DoCommonMenuCallbackCode(item->GetMenu(), event); |
cd743a6f | 541 | } |
865bb325 | 542 | } |
cd743a6f | 543 | |
cf7a7e13 | 544 | //----------------------------------------------------------------------------- |
db1b4961 | 545 | // wxMenuItem |
cf7a7e13 RR |
546 | //----------------------------------------------------------------------------- |
547 | ||
974e8d94 VZ |
548 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, |
549 | int id, | |
550 | const wxString& name, | |
551 | const wxString& help, | |
d65c269b | 552 | wxItemKind kind, |
974e8d94 VZ |
553 | wxMenu *subMenu) |
554 | { | |
d65c269b | 555 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); |
974e8d94 | 556 | } |
96fd301f | 557 | |
974e8d94 VZ |
558 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
559 | int id, | |
560 | const wxString& text, | |
561 | const wxString& help, | |
d65c269b | 562 | wxItemKind kind, |
974e8d94 | 563 | wxMenu *subMenu) |
d65c269b | 564 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) |
2368dcda | 565 | { |
1deef997 | 566 | m_menuItem = NULL; |
2368dcda VZ |
567 | } |
568 | ||
efebabb7 | 569 | #if WXWIN_COMPATIBILITY_2_8 |
2368dcda VZ |
570 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, |
571 | int id, | |
572 | const wxString& text, | |
573 | const wxString& help, | |
574 | bool isCheckable, | |
575 | wxMenu *subMenu) | |
576 | : wxMenuItemBase(parentMenu, id, text, help, | |
577 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
578 | { | |
1deef997 | 579 | m_menuItem = NULL; |
2368dcda | 580 | } |
efebabb7 | 581 | #endif |
2368dcda | 582 | |
d1b15f03 RR |
583 | wxMenuItem::~wxMenuItem() |
584 | { | |
585 | // don't delete menu items, the menus take care of that | |
586 | } | |
587 | ||
52af3158 | 588 | void wxMenuItem::SetItemLabel( const wxString& str ) |
717a57c2 | 589 | { |
6c309653 | 590 | #if wxUSE_ACCEL |
354aa1e3 RR |
591 | if (m_menuItem) |
592 | { | |
1deef997 PC |
593 | // remove old accelerator |
594 | guint accel_key; | |
595 | GdkModifierType accel_mods; | |
596 | wxGetGtkAccel(this, &accel_key, &accel_mods); | |
597 | if (accel_key) | |
ee0a94cf | 598 | { |
1deef997 PC |
599 | gtk_widget_remove_accelerator( |
600 | m_menuItem, m_parentMenu->m_accel, accel_key, accel_mods); | |
ee0a94cf | 601 | } |
98f29783 | 602 | } |
19abd352 | 603 | #endif // wxUSE_ACCEL |
1deef997 PC |
604 | wxMenuItemBase::SetItemLabel(str); |
605 | if (m_menuItem) | |
606 | SetGtkLabel(); | |
354aa1e3 RR |
607 | } |
608 | ||
1deef997 | 609 | void wxMenuItem::SetGtkLabel() |
716b7364 | 610 | { |
1deef997 | 611 | const wxString text = wxConvertMnemonicsToGTK(m_text.BeforeFirst('\t')); |
385e8575 | 612 | GtkLabel* label = GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_menuItem))); |
1deef997 PC |
613 | gtk_label_set_text_with_mnemonic(label, wxGTK_CONV_SYS(text)); |
614 | #if wxUSE_ACCEL | |
615 | guint accel_key; | |
616 | GdkModifierType accel_mods; | |
617 | wxGetGtkAccel(this, &accel_key, &accel_mods); | |
618 | if (accel_key) | |
d7dbc98a | 619 | { |
1deef997 PC |
620 | gtk_widget_add_accelerator( |
621 | m_menuItem, "activate", m_parentMenu->m_accel, | |
622 | accel_key, accel_mods, GTK_ACCEL_VISIBLE); | |
d7dbc98a | 623 | } |
1deef997 | 624 | #endif // wxUSE_ACCEL |
716b7364 RR |
625 | } |
626 | ||
1deef997 | 627 | void wxMenuItem::SetBitmap(const wxBitmap& bitmap) |
717a57c2 | 628 | { |
1deef997 PC |
629 | if (m_kind == wxITEM_NORMAL) |
630 | m_bitmap = bitmap; | |
631 | else | |
4c2ea599 | 632 | { |
1deef997 | 633 | wxFAIL_MSG("only normal menu items can have bitmaps"); |
4c2ea599 | 634 | } |
717a57c2 VZ |
635 | } |
636 | ||
96fd301f | 637 | void wxMenuItem::Check( bool check ) |
716b7364 | 638 | { |
223d09f6 | 639 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 640 | |
974e8d94 VZ |
641 | if (check == m_isChecked) |
642 | return; | |
2d17d68f | 643 | |
974e8d94 | 644 | wxMenuItemBase::Check( check ); |
0472ece7 | 645 | |
24bcaec3 | 646 | switch ( GetKind() ) |
0472ece7 | 647 | { |
24bcaec3 VZ |
648 | case wxITEM_CHECK: |
649 | case wxITEM_RADIO: | |
8394218c | 650 | gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check ); |
24bcaec3 VZ |
651 | break; |
652 | ||
653 | default: | |
9a83f860 | 654 | wxFAIL_MSG( wxT("can't check this item") ); |
0472ece7 | 655 | } |
716b7364 RR |
656 | } |
657 | ||
8bbe427f VZ |
658 | void wxMenuItem::Enable( bool enable ) |
659 | { | |
223d09f6 | 660 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); |
db1b4961 | 661 | |
83624f79 | 662 | gtk_widget_set_sensitive( m_menuItem, enable ); |
974e8d94 | 663 | wxMenuItemBase::Enable( enable ); |
a9c96bcc RR |
664 | } |
665 | ||
96fd301f | 666 | bool wxMenuItem::IsChecked() const |
716b7364 | 667 | { |
670f9935 | 668 | wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") ); |
db1b4961 | 669 | |
670f9935 | 670 | wxCHECK_MSG( IsCheckable(), false, |
974e8d94 | 671 | wxT("can't get state of uncheckable item!") ); |
96fd301f | 672 | |
385e8575 | 673 | return gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(m_menuItem)) != 0; |
716b7364 RR |
674 | } |
675 | ||
db1b4961 | 676 | //----------------------------------------------------------------------------- |
83624f79 | 677 | // wxMenu |
db1b4961 RR |
678 | //----------------------------------------------------------------------------- |
679 | ||
3ed946f2 PC |
680 | extern "C" { |
681 | // "map" from m_menu | |
682 | static void menu_map(GtkWidget*, wxMenu* menu) | |
683 | { | |
684 | wxMenuEvent event(wxEVT_MENU_OPEN, menu->m_popupShown ? -1 : 0, menu); | |
685 | DoCommonMenuCallbackCode(menu, event); | |
686 | } | |
687 | ||
688 | // "hide" from m_menu | |
689 | static void menu_hide(GtkWidget*, wxMenu* menu) | |
690 | { | |
45f0926d VZ |
691 | // When using Ubuntu Unity desktop environment we get "hide" signal even |
692 | // when the window is not shown yet because Unity hides all the menus to | |
693 | // show them only in the global menu bar. Just ignore this even instead of | |
694 | // crashing in DoCommonMenuCallbackCode(). | |
695 | if ( !menu->GetWindow() ) | |
696 | return; | |
697 | ||
3ed946f2 PC |
698 | wxMenuEvent event(wxEVT_MENU_CLOSE, menu->m_popupShown ? -1 : 0, menu); |
699 | menu->m_popupShown = false; | |
700 | DoCommonMenuCallbackCode(menu, event); | |
701 | } | |
702 | } | |
703 | ||
90659535 PC |
704 | // "can_activate_accel" from menu item |
705 | extern "C" { | |
706 | static gboolean can_activate_accel(GtkWidget*, guint, wxMenu* menu) | |
707 | { | |
708 | menu->UpdateUI(); | |
709 | // always allow our "activate" handler to be called | |
710 | return true; | |
711 | } | |
712 | } | |
713 | ||
717a57c2 | 714 | void wxMenu::Init() |
c801d85f | 715 | { |
3ed946f2 PC |
716 | m_popupShown = false; |
717 | ||
034be888 | 718 | m_accel = gtk_accel_group_new(); |
6d971354 | 719 | m_menu = gtk_menu_new(); |
defc0789 VS |
720 | // NB: keep reference to the menu so that it is not destroyed behind |
721 | // our back by GTK+ e.g. when it is removed from menubar: | |
385e8575 | 722 | g_object_ref_sink(m_menu); |
8bbe427f | 723 | |
d3b9f782 | 724 | m_owner = NULL; |
2b2edbed | 725 | |
d9e403cc RR |
726 | // Tearoffs are entries, just like separators. So if we want this |
727 | // menu to be a tear-off one, we just append a tearoff entry | |
728 | // immediately. | |
9959e2b6 | 729 | if ( m_style & wxMENU_TEAROFF ) |
2b2edbed | 730 | { |
9959e2b6 | 731 | GtkWidget *tearoff = gtk_tearoff_menu_item_new(); |
6d971354 | 732 | |
1ab9e06d | 733 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff); |
9959e2b6 | 734 | } |
6d971354 | 735 | |
717a57c2 | 736 | // append the title as the very first entry if we have it |
9959e2b6 | 737 | if ( !m_title.empty() ) |
d1b15f03 | 738 | { |
9959e2b6 | 739 | Append(wxGTK_TITLE_ID, m_title); |
717a57c2 | 740 | AppendSeparator(); |
d1b15f03 | 741 | } |
3ed946f2 PC |
742 | |
743 | // "show" occurs for sub-menus which are not showing, so use "map" instead | |
744 | g_signal_connect(m_menu, "map", G_CALLBACK(menu_map), this); | |
745 | g_signal_connect(m_menu, "hide", G_CALLBACK(menu_hide), this); | |
717a57c2 | 746 | } |
15a2076a | 747 | |
717a57c2 VZ |
748 | wxMenu::~wxMenu() |
749 | { | |
82302ad4 VZ |
750 | // Destroying a menu generates a "hide" signal even if it's not shown |
751 | // currently, so disconnect it to avoid dummy wxEVT_MENU_CLOSE events | |
752 | // generation. | |
753 | g_signal_handlers_disconnect_by_func(m_menu, (gpointer)menu_hide, this); | |
754 | ||
3ed946f2 PC |
755 | // see wxMenu::Init |
756 | g_object_unref(m_menu); | |
52af3158 | 757 | |
3ed946f2 PC |
758 | // if the menu is inserted in another menu at this time, there was |
759 | // one more reference to it: | |
760 | if (m_owner) | |
761 | gtk_widget_destroy(m_menu); | |
f0378929 | 762 | |
3ed946f2 | 763 | g_object_unref(m_accel); |
c2dd8380 GL |
764 | } |
765 | ||
978af864 VZ |
766 | void wxMenu::SetLayoutDirection(const wxLayoutDirection dir) |
767 | { | |
768 | if ( m_owner ) | |
769 | wxWindow::GTKSetLayout(m_owner, dir); | |
770 | //else: will be called later by wxMenuBar again | |
771 | } | |
772 | ||
773 | wxLayoutDirection wxMenu::GetLayoutDirection() const | |
774 | { | |
775 | return wxWindow::GTKGetLayout(m_owner); | |
776 | } | |
777 | ||
96a3acb7 | 778 | wxString wxMenu::GetTitle() const |
7bc0ff86 VZ |
779 | { |
780 | return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle()); | |
781 | } | |
782 | ||
0f29501b | 783 | void wxMenu::GtkAppend(wxMenuItem* mitem, int pos) |
c2dd8380 | 784 | { |
717a57c2 | 785 | GtkWidget *menuItem; |
1deef997 | 786 | switch (mitem->GetKind()) |
717a57c2 | 787 | { |
1deef997 PC |
788 | case wxITEM_SEPARATOR: |
789 | menuItem = gtk_separator_menu_item_new(); | |
790 | break; | |
791 | case wxITEM_CHECK: | |
792 | menuItem = gtk_check_menu_item_new_with_label(""); | |
793 | break; | |
794 | case wxITEM_RADIO: | |
6d971354 | 795 | { |
a0828629 VZ |
796 | // See if we need to create a new radio group for this item or |
797 | // add it to an existing one. | |
798 | wxMenuItem* radioGroupItem = NULL; | |
799 | ||
800 | const size_t numItems = GetMenuItemCount(); | |
0f29501b PC |
801 | const size_t n = pos == -1 ? numItems - 1 : size_t(pos); |
802 | ||
803 | if (n != 0) | |
a0828629 VZ |
804 | { |
805 | wxMenuItem* const itemPrev = FindItemByPosition(n - 1); | |
806 | if ( itemPrev->GetKind() == wxITEM_RADIO ) | |
807 | { | |
808 | // Appending an item after an existing radio item puts | |
809 | // it into the same radio group. | |
810 | radioGroupItem = itemPrev; | |
811 | } | |
812 | } | |
813 | ||
0f29501b | 814 | if (radioGroupItem == NULL && n != numItems - 1) |
a0828629 | 815 | { |
0f29501b | 816 | wxMenuItem* const itemNext = FindItemByPosition(n + 1); |
a0828629 VZ |
817 | if ( itemNext->GetKind() == wxITEM_RADIO ) |
818 | { | |
819 | // Inserting an item before an existing radio item | |
820 | // also puts it into the existing radio group. | |
821 | radioGroupItem = itemNext; | |
822 | } | |
823 | } | |
824 | ||
1deef997 | 825 | GSList* group = NULL; |
a0828629 VZ |
826 | if ( radioGroupItem ) |
827 | { | |
828 | group = gtk_radio_menu_item_get_group( | |
829 | GTK_RADIO_MENU_ITEM(radioGroupItem->GetMenuItem()) | |
830 | ); | |
831 | } | |
832 | ||
1deef997 | 833 | menuItem = gtk_radio_menu_item_new_with_label(group, ""); |
6d971354 | 834 | } |
1deef997 PC |
835 | break; |
836 | default: | |
837 | wxFAIL_MSG("unexpected menu item kind"); | |
838 | // fall through | |
839 | case wxITEM_NORMAL: | |
840 | const wxBitmap& bitmap = mitem->GetBitmap(); | |
841 | const char* stockid; | |
842 | if (bitmap.IsOk()) | |
6d971354 | 843 | { |
4a4a02ac PC |
844 | // always use pixbuf, because pixmap mask does not |
845 | // work with disabled images in some themes | |
846 | GtkWidget* image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf()); | |
1deef997 PC |
847 | menuItem = gtk_image_menu_item_new_with_label(""); |
848 | gtk_widget_show(image); | |
849 | gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuItem), image); | |
6d971354 | 850 | } |
1deef997 PC |
851 | else if ((stockid = wxGetStockGtkID(mitem->GetId())) != NULL) |
852 | // use stock bitmap for this item if available on the assumption | |
853 | // that it never hurts to follow GTK+ conventions more closely | |
854 | menuItem = gtk_image_menu_item_new_from_stock(stockid, NULL); | |
855 | else | |
856 | menuItem = gtk_menu_item_new_with_label(""); | |
857 | break; | |
717a57c2 | 858 | } |
1deef997 | 859 | mitem->SetMenuItem(menuItem); |
9959e2b6 | 860 | |
1deef997 | 861 | gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos); |
e31126cb | 862 | |
6d971354 | 863 | gtk_widget_show( menuItem ); |
23280650 | 864 | |
717a57c2 VZ |
865 | if ( !mitem->IsSeparator() ) |
866 | { | |
1deef997 | 867 | mitem->SetGtkLabel(); |
9fa72bd2 | 868 | g_signal_connect (menuItem, "select", |
e2147e6d | 869 | G_CALLBACK(menuitem_select), mitem); |
9fa72bd2 | 870 | g_signal_connect (menuItem, "deselect", |
e2147e6d | 871 | G_CALLBACK(menuitem_deselect), mitem); |
e31126cb | 872 | |
88d19775 MR |
873 | if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) |
874 | { | |
e31126cb RR |
875 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); |
876 | ||
877 | gtk_widget_show( mitem->GetSubMenu()->m_menu ); | |
88d19775 MR |
878 | } |
879 | else | |
880 | { | |
90659535 PC |
881 | g_signal_connect(menuItem, "can_activate_accel", |
882 | G_CALLBACK(can_activate_accel), this); | |
9fa72bd2 | 883 | g_signal_connect (menuItem, "activate", |
e2147e6d PC |
884 | G_CALLBACK(menuitem_activate), |
885 | mitem); | |
88d19775 | 886 | } |
717a57c2 | 887 | } |
6de97a3b | 888 | } |
c801d85f | 889 | |
9add9367 | 890 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) |
828f655f | 891 | { |
0f29501b PC |
892 | if (wxMenuBase::DoAppend(mitem)) |
893 | { | |
894 | GtkAppend(mitem); | |
895 | return mitem; | |
896 | } | |
897 | return NULL; | |
c33c4050 RR |
898 | } |
899 | ||
9add9367 | 900 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
c33c4050 | 901 | { |
0f29501b PC |
902 | if (wxMenuBase::DoInsert(pos, item)) |
903 | { | |
904 | GtkAppend(item, int(pos)); | |
905 | return item; | |
906 | } | |
907 | return NULL; | |
c33c4050 RR |
908 | } |
909 | ||
717a57c2 | 910 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
c33c4050 | 911 | { |
717a57c2 | 912 | if ( !wxMenuBase::DoRemove(item) ) |
8d749001 VZ |
913 | return NULL; |
914 | ||
915 | GtkWidget * const mitem = item->GetMenuItem(); | |
9dc44eff PC |
916 | #ifdef __WXGTK3__ |
917 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL); | |
918 | #else | |
f089d736 VZ |
919 | if (!gtk_check_version(2,12,0)) |
920 | { | |
921 | // gtk_menu_item_remove_submenu() is deprecated since 2.12, but | |
922 | // gtk_menu_item_set_submenu() can now be used with NULL submenu now so | |
923 | // just do use it. | |
924 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL); | |
925 | } | |
926 | else // GTK+ < 2.12 | |
927 | { | |
928 | // In 2.10 calling gtk_menu_item_set_submenu() with NULL submenu | |
929 | // results in critical GTK+ error messages so use the old function | |
930 | // instead. | |
931 | gtk_menu_item_remove_submenu(GTK_MENU_ITEM(mitem)); | |
932 | } | |
9dc44eff | 933 | #endif |
f089d736 | 934 | |
e828d3fc PC |
935 | gtk_widget_destroy(mitem); |
936 | item->SetMenuItem(NULL); | |
c626a8b7 | 937 | |
717a57c2 | 938 | return item; |
c33c4050 RR |
939 | } |
940 | ||
978af864 VZ |
941 | void wxMenu::Attach(wxMenuBarBase *menubar) |
942 | { | |
943 | wxMenuBase::Attach(menubar); | |
944 | ||
945 | // inherit layout direction from menubar. | |
946 | SetLayoutDirection(menubar->GetLayoutDirection()); | |
947 | } | |
948 | ||
717a57c2 VZ |
949 | // ---------------------------------------------------------------------------- |
950 | // helpers | |
951 | // ---------------------------------------------------------------------------- | |
952 | ||
6d971354 | 953 | #if wxUSE_ACCEL |
a070d8ce | 954 | |
98f29783 | 955 | static wxString GetGtkHotKey( const wxMenuItem& item ) |
c801d85f | 956 | { |
717a57c2 VZ |
957 | wxString hotkey; |
958 | ||
959 | wxAcceleratorEntry *accel = item.GetAccel(); | |
960 | if ( accel ) | |
83624f79 | 961 | { |
717a57c2 VZ |
962 | int flags = accel->GetFlags(); |
963 | if ( flags & wxACCEL_ALT ) | |
964 | hotkey += wxT("<alt>"); | |
965 | if ( flags & wxACCEL_CTRL ) | |
966 | hotkey += wxT("<control>"); | |
967 | if ( flags & wxACCEL_SHIFT ) | |
968 | hotkey += wxT("<shift>"); | |
969 | ||
970 | int code = accel->GetKeyCode(); | |
971 | switch ( code ) | |
c626a8b7 | 972 | { |
717a57c2 VZ |
973 | case WXK_F1: |
974 | case WXK_F2: | |
975 | case WXK_F3: | |
976 | case WXK_F4: | |
977 | case WXK_F5: | |
978 | case WXK_F6: | |
979 | case WXK_F7: | |
980 | case WXK_F8: | |
981 | case WXK_F9: | |
982 | case WXK_F10: | |
983 | case WXK_F11: | |
984 | case WXK_F12: | |
bbcd4085 RR |
985 | case WXK_F13: |
986 | case WXK_F14: | |
987 | case WXK_F15: | |
988 | case WXK_F16: | |
989 | case WXK_F17: | |
990 | case WXK_F18: | |
991 | case WXK_F19: | |
992 | case WXK_F20: | |
993 | case WXK_F21: | |
994 | case WXK_F22: | |
995 | case WXK_F23: | |
996 | case WXK_F24: | |
04cc1e93 | 997 | hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1); |
717a57c2 | 998 | break; |
2368dcda | 999 | |
a070d8ce VZ |
1000 | // TODO: we should use gdk_keyval_name() (a.k.a. |
1001 | // XKeysymToString) here as well as hardcoding the keysym | |
1002 | // names this might be not portable | |
bbcd4085 | 1003 | case WXK_INSERT: |
3ca6a5f0 BP |
1004 | hotkey << wxT("Insert" ); |
1005 | break; | |
1006 | case WXK_DELETE: | |
1007 | hotkey << wxT("Delete" ); | |
1008 | break; | |
2b5f62a0 VZ |
1009 | case WXK_UP: |
1010 | hotkey << wxT("Up" ); | |
1011 | break; | |
1012 | case WXK_DOWN: | |
1013 | hotkey << wxT("Down" ); | |
1014 | break; | |
1015 | case WXK_PAGEUP: | |
f005ea42 | 1016 | hotkey << wxT("Page_Up" ); |
2b5f62a0 VZ |
1017 | break; |
1018 | case WXK_PAGEDOWN: | |
f005ea42 | 1019 | hotkey << wxT("Page_Down" ); |
2b5f62a0 VZ |
1020 | break; |
1021 | case WXK_LEFT: | |
1022 | hotkey << wxT("Left" ); | |
1023 | break; | |
1024 | case WXK_RIGHT: | |
1025 | hotkey << wxT("Right" ); | |
1026 | break; | |
1027 | case WXK_HOME: | |
1028 | hotkey << wxT("Home" ); | |
1029 | break; | |
1030 | case WXK_END: | |
1031 | hotkey << wxT("End" ); | |
1032 | break; | |
1033 | case WXK_RETURN: | |
1034 | hotkey << wxT("Return" ); | |
1035 | break; | |
bbcd4085 RR |
1036 | case WXK_BACK: |
1037 | hotkey << wxT("BackSpace" ); | |
1038 | break; | |
1039 | case WXK_TAB: | |
1040 | hotkey << wxT("Tab" ); | |
1041 | break; | |
1042 | case WXK_ESCAPE: | |
1043 | hotkey << wxT("Esc" ); | |
1044 | break; | |
1045 | case WXK_SPACE: | |
1046 | hotkey << wxT("space" ); | |
1047 | break; | |
1048 | case WXK_MULTIPLY: | |
1049 | hotkey << wxT("Multiply" ); | |
1050 | break; | |
1051 | case WXK_ADD: | |
1052 | hotkey << wxT("Add" ); | |
1053 | break; | |
1054 | case WXK_SEPARATOR: | |
1055 | hotkey << wxT("Separator" ); | |
1056 | break; | |
1057 | case WXK_SUBTRACT: | |
1058 | hotkey << wxT("Subtract" ); | |
1059 | break; | |
1060 | case WXK_DECIMAL: | |
1061 | hotkey << wxT("Decimal" ); | |
1062 | break; | |
1063 | case WXK_DIVIDE: | |
1064 | hotkey << wxT("Divide" ); | |
1065 | break; | |
1066 | case WXK_CANCEL: | |
1067 | hotkey << wxT("Cancel" ); | |
1068 | break; | |
1069 | case WXK_CLEAR: | |
1070 | hotkey << wxT("Clear" ); | |
1071 | break; | |
1072 | case WXK_MENU: | |
1073 | hotkey << wxT("Menu" ); | |
1074 | break; | |
1075 | case WXK_PAUSE: | |
1076 | hotkey << wxT("Pause" ); | |
1077 | break; | |
1078 | case WXK_CAPITAL: | |
1079 | hotkey << wxT("Capital" ); | |
1080 | break; | |
1081 | case WXK_SELECT: | |
1082 | hotkey << wxT("Select" ); | |
1083 | break; | |
1084 | case WXK_PRINT: | |
1085 | hotkey << wxT("Print" ); | |
1086 | break; | |
1087 | case WXK_EXECUTE: | |
1088 | hotkey << wxT("Execute" ); | |
1089 | break; | |
1090 | case WXK_SNAPSHOT: | |
1091 | hotkey << wxT("Snapshot" ); | |
1092 | break; | |
1093 | case WXK_HELP: | |
1094 | hotkey << wxT("Help" ); | |
1095 | break; | |
1096 | case WXK_NUMLOCK: | |
1097 | hotkey << wxT("Num_Lock" ); | |
1098 | break; | |
1099 | case WXK_SCROLL: | |
1100 | hotkey << wxT("Scroll_Lock" ); | |
1101 | break; | |
1102 | case WXK_NUMPAD_INSERT: | |
1103 | hotkey << wxT("KP_Insert" ); | |
1104 | break; | |
1105 | case WXK_NUMPAD_DELETE: | |
1106 | hotkey << wxT("KP_Delete" ); | |
1107 | break; | |
1108 | case WXK_NUMPAD_SPACE: | |
1109 | hotkey << wxT("KP_Space" ); | |
1110 | break; | |
1111 | case WXK_NUMPAD_TAB: | |
1112 | hotkey << wxT("KP_Tab" ); | |
1113 | break; | |
1114 | case WXK_NUMPAD_ENTER: | |
1115 | hotkey << wxT("KP_Enter" ); | |
1116 | break; | |
1117 | case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3: | |
1118 | case WXK_NUMPAD_F4: | |
1119 | hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1); | |
1120 | break; | |
1121 | case WXK_NUMPAD_HOME: | |
1122 | hotkey << wxT("KP_Home" ); | |
1123 | break; | |
1124 | case WXK_NUMPAD_LEFT: | |
1125 | hotkey << wxT("KP_Left" ); | |
1126 | break; | |
1127 | case WXK_NUMPAD_UP: | |
1128 | hotkey << wxT("KP_Up" ); | |
1129 | break; | |
1130 | case WXK_NUMPAD_RIGHT: | |
1131 | hotkey << wxT("KP_Right" ); | |
1132 | break; | |
1133 | case WXK_NUMPAD_DOWN: | |
1134 | hotkey << wxT("KP_Down" ); | |
1135 | break; | |
5bd24f72 | 1136 | case WXK_NUMPAD_PAGEUP: |
f005ea42 | 1137 | hotkey << wxT("KP_Page_Up" ); |
bbcd4085 | 1138 | break; |
5bd24f72 | 1139 | case WXK_NUMPAD_PAGEDOWN: |
f005ea42 | 1140 | hotkey << wxT("KP_Page_Down" ); |
bbcd4085 RR |
1141 | break; |
1142 | case WXK_NUMPAD_END: | |
1143 | hotkey << wxT("KP_End" ); | |
1144 | break; | |
1145 | case WXK_NUMPAD_BEGIN: | |
1146 | hotkey << wxT("KP_Begin" ); | |
1147 | break; | |
1148 | case WXK_NUMPAD_EQUAL: | |
1149 | hotkey << wxT("KP_Equal" ); | |
1150 | break; | |
1151 | case WXK_NUMPAD_MULTIPLY: | |
1152 | hotkey << wxT("KP_Multiply" ); | |
1153 | break; | |
1154 | case WXK_NUMPAD_ADD: | |
1155 | hotkey << wxT("KP_Add" ); | |
1156 | break; | |
1157 | case WXK_NUMPAD_SEPARATOR: | |
1158 | hotkey << wxT("KP_Separator" ); | |
1159 | break; | |
1160 | case WXK_NUMPAD_SUBTRACT: | |
1161 | hotkey << wxT("KP_Subtract" ); | |
1162 | break; | |
1163 | case WXK_NUMPAD_DECIMAL: | |
1164 | hotkey << wxT("KP_Decimal" ); | |
1165 | break; | |
1166 | case WXK_NUMPAD_DIVIDE: | |
1167 | hotkey << wxT("KP_Divide" ); | |
1168 | break; | |
1169 | case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2: | |
1170 | case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5: | |
1171 | case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9: | |
1172 | hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0); | |
1173 | break; | |
1174 | case WXK_WINDOWS_LEFT: | |
1175 | hotkey << wxT("Super_L" ); | |
1176 | break; | |
1177 | case WXK_WINDOWS_RIGHT: | |
1178 | hotkey << wxT("Super_R" ); | |
1179 | break; | |
1180 | case WXK_WINDOWS_MENU: | |
1181 | hotkey << wxT("Menu" ); | |
1182 | break; | |
1183 | case WXK_COMMAND: | |
1184 | hotkey << wxT("Command" ); | |
1185 | break; | |
1186 | /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt | |
88d19775 MR |
1187 | case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4: |
1188 | case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8: | |
1189 | case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12: | |
1190 | case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16: | |
bbcd4085 RR |
1191 | case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20: |
1192 | hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1); | |
1193 | break; | |
1194 | */ | |
90527a50 | 1195 | // if there are any other keys wxAcceleratorEntry::Create() may |
a070d8ce | 1196 | // return, we should process them here |
8bbe427f | 1197 | |
717a57c2 | 1198 | default: |
a070d8ce | 1199 | if ( code < 127 ) |
717a57c2 | 1200 | { |
30083ad8 VZ |
1201 | const wxString |
1202 | name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code)); | |
1203 | if ( !name.empty() ) | |
a070d8ce VZ |
1204 | { |
1205 | hotkey << name; | |
1206 | break; | |
1207 | } | |
717a57c2 | 1208 | } |
c801d85f | 1209 | |
717a57c2 VZ |
1210 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
1211 | } | |
c801d85f | 1212 | |
717a57c2 | 1213 | delete accel; |
631f1bfe | 1214 | } |
717a57c2 VZ |
1215 | |
1216 | return hotkey; | |
631f1bfe | 1217 | } |
a070d8ce | 1218 | |
1deef997 PC |
1219 | static void |
1220 | wxGetGtkAccel(const wxMenuItem* item, guint* accel_key, GdkModifierType* accel_mods) | |
1221 | { | |
1222 | *accel_key = 0; | |
1223 | const wxString string = GetGtkHotKey(*item); | |
1224 | if (!string.empty()) | |
1225 | gtk_accelerator_parse(wxGTK_CONV_SYS(string), accel_key, accel_mods); | |
1226 | else | |
1227 | { | |
1228 | GtkStockItem stock_item; | |
1229 | const char* stockid = wxGetStockGtkID(item->GetId()); | |
1230 | if (stockid && gtk_stock_lookup(stockid, &stock_item)) | |
1231 | { | |
1232 | *accel_key = stock_item.keyval; | |
1233 | *accel_mods = stock_item.modifier; | |
1234 | } | |
1235 | } | |
1236 | } | |
717a57c2 | 1237 | #endif // wxUSE_ACCEL |
43a11e2a | 1238 | |
bcf881ef JS |
1239 | const char *wxGetStockGtkID(wxWindowID id) |
1240 | { | |
1241 | #define STOCKITEM(wx,gtk) \ | |
1242 | case wx: \ | |
1243 | return gtk; | |
1244 | ||
6b1eedc1 VZ |
1245 | #if GTK_CHECK_VERSION(2,8,0) |
1246 | #define STOCKITEM_28(wx,gtk) STOCKITEM(wx,gtk) | |
1247 | #else | |
1248 | #define STOCKITEM_28(wx,gtk) | |
1249 | #endif | |
1250 | ||
bcf881ef JS |
1251 | #if GTK_CHECK_VERSION(2,10,0) |
1252 | #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk) | |
1253 | #else | |
1deef997 | 1254 | #define STOCKITEM_210(wx,gtk) |
bcf881ef JS |
1255 | #endif |
1256 | ||
1257 | ||
1258 | switch (id) | |
1259 | { | |
7dae41b9 | 1260 | STOCKITEM(wxID_ABOUT, GTK_STOCK_ABOUT) |
bcf881ef JS |
1261 | STOCKITEM(wxID_ADD, GTK_STOCK_ADD) |
1262 | STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY) | |
6b1eedc1 | 1263 | STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK) |
bcf881ef | 1264 | STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD) |
6b1eedc1 | 1265 | STOCKITEM(wxID_BOTTOM, GTK_STOCK_GOTO_BOTTOM) |
bcf881ef | 1266 | STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL) |
6b1eedc1 | 1267 | STOCKITEM(wxID_CDROM, GTK_STOCK_CDROM) |
bcf881ef JS |
1268 | STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR) |
1269 | STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE) | |
6b1eedc1 | 1270 | STOCKITEM(wxID_CONVERT, GTK_STOCK_CONVERT) |
bcf881ef JS |
1271 | STOCKITEM(wxID_COPY, GTK_STOCK_COPY) |
1272 | STOCKITEM(wxID_CUT, GTK_STOCK_CUT) | |
1273 | STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE) | |
6b1eedc1 | 1274 | STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN) |
7dae41b9 | 1275 | STOCKITEM(wxID_EDIT, GTK_STOCK_EDIT) |
6b1eedc1 VZ |
1276 | STOCKITEM(wxID_EXECUTE, GTK_STOCK_EXECUTE) |
1277 | STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT) | |
7dae41b9 | 1278 | STOCKITEM(wxID_FILE, GTK_STOCK_FILE) |
6b1eedc1 VZ |
1279 | STOCKITEM(wxID_FIND, GTK_STOCK_FIND) |
1280 | STOCKITEM(wxID_FIRST, GTK_STOCK_GOTO_FIRST) | |
1281 | STOCKITEM(wxID_FLOPPY, GTK_STOCK_FLOPPY) | |
bcf881ef | 1282 | STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD) |
6b1eedc1 | 1283 | STOCKITEM(wxID_HARDDISK, GTK_STOCK_HARDDISK) |
bcf881ef JS |
1284 | STOCKITEM(wxID_HELP, GTK_STOCK_HELP) |
1285 | STOCKITEM(wxID_HOME, GTK_STOCK_HOME) | |
1deef997 | 1286 | STOCKITEM(wxID_INDENT, GTK_STOCK_INDENT) |
bcf881ef | 1287 | STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX) |
6b1eedc1 | 1288 | STOCKITEM_28(wxID_INFO, GTK_STOCK_INFO) |
bcf881ef | 1289 | STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC) |
6b1eedc1 | 1290 | STOCKITEM(wxID_JUMP_TO, GTK_STOCK_JUMP_TO) |
bcf881ef JS |
1291 | STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER) |
1292 | STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL) | |
1293 | STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT) | |
1294 | STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT) | |
6b1eedc1 VZ |
1295 | STOCKITEM(wxID_LAST, GTK_STOCK_GOTO_LAST) |
1296 | STOCKITEM(wxID_NETWORK, GTK_STOCK_NETWORK) | |
bcf881ef JS |
1297 | STOCKITEM(wxID_NEW, GTK_STOCK_NEW) |
1298 | STOCKITEM(wxID_NO, GTK_STOCK_NO) | |
1299 | STOCKITEM(wxID_OK, GTK_STOCK_OK) | |
1300 | STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN) | |
1301 | STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE) | |
1302 | STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES) | |
bcf881ef | 1303 | STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW) |
6b1eedc1 | 1304 | STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT) |
bcf881ef | 1305 | STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES) |
bcf881ef JS |
1306 | STOCKITEM(wxID_REDO, GTK_STOCK_REDO) |
1307 | STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH) | |
1308 | STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE) | |
6b1eedc1 | 1309 | STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE) |
bcf881ef JS |
1310 | STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED) |
1311 | STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE) | |
1312 | STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS) | |
1313 | STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL) | |
6b1eedc1 VZ |
1314 | STOCKITEM(wxID_SELECT_COLOR, GTK_STOCK_SELECT_COLOR) |
1315 | STOCKITEM(wxID_SELECT_FONT, GTK_STOCK_SELECT_FONT) | |
1316 | STOCKITEM(wxID_SORT_ASCENDING, GTK_STOCK_SORT_ASCENDING) | |
1317 | STOCKITEM(wxID_SORT_DESCENDING, GTK_STOCK_SORT_DESCENDING) | |
1318 | STOCKITEM(wxID_SPELL_CHECK, GTK_STOCK_SPELL_CHECK) | |
bcf881ef | 1319 | STOCKITEM(wxID_STOP, GTK_STOCK_STOP) |
6b1eedc1 VZ |
1320 | STOCKITEM(wxID_STRIKETHROUGH, GTK_STOCK_STRIKETHROUGH) |
1321 | STOCKITEM(wxID_TOP, GTK_STOCK_GOTO_TOP) | |
bcf881ef JS |
1322 | STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE) |
1323 | STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE) | |
1324 | STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO) | |
1deef997 | 1325 | STOCKITEM(wxID_UNINDENT, GTK_STOCK_UNINDENT) |
6b1eedc1 | 1326 | STOCKITEM(wxID_UP, GTK_STOCK_GO_UP) |
bcf881ef JS |
1327 | STOCKITEM(wxID_YES, GTK_STOCK_YES) |
1328 | STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100) | |
1329 | STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT) | |
1330 | STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN) | |
1331 | STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT) | |
1332 | ||
1333 | default: | |
bcf881ef JS |
1334 | break; |
1335 | }; | |
1336 | ||
1337 | #undef STOCKITEM | |
1338 | ||
1339 | return NULL; | |
1340 | } | |
1341 | ||
28fcfbfe | 1342 | #endif // wxUSE_MENUS |