]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/menu.cpp | |
3 | // Purpose: implementation of wxMenuBar and wxMenu classes for wxGTK | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_MENUS | |
14 | ||
15 | #include "wx/menu.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/log.h" | |
20 | #include "wx/frame.h" | |
21 | #include "wx/bitmap.h" | |
22 | #include "wx/app.h" | |
23 | #endif | |
24 | ||
25 | #include "wx/accel.h" | |
26 | #include "wx/stockitem.h" | |
27 | #include "wx/gtk/private.h" | |
28 | #include "wx/gtk/private/mnemonics.h" | |
29 | ||
30 | // we use normal item but with a special id for the menu title | |
31 | static const int wxGTK_TITLE_ID = -3; | |
32 | ||
33 | // forward declare it as it's used by wxMenuBar too when using Hildon | |
34 | extern "C" | |
35 | { | |
36 | static void gtk_menu_clicked_callback(GtkWidget *widget, wxMenu *menu); | |
37 | } | |
38 | ||
39 | #if wxUSE_ACCEL | |
40 | static bool wxGetStockGtkAccelerator(const char *id, GdkModifierType *mod, guint *key); | |
41 | static wxString GetGtkHotKey( const wxMenuItem& item ); | |
42 | #endif | |
43 | ||
44 | static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event) | |
45 | { | |
46 | event.SetEventObject( menu ); | |
47 | ||
48 | wxEvtHandler* handler = menu->GetEventHandler(); | |
49 | if (handler && handler->SafelyProcessEvent(event)) | |
50 | return; | |
51 | ||
52 | wxWindow *win = menu->GetInvokingWindow(); | |
53 | if (win) | |
54 | win->HandleWindowEvent( event ); | |
55 | } | |
56 | ||
57 | //----------------------------------------------------------------------------- | |
58 | // wxMenuBar | |
59 | //----------------------------------------------------------------------------- | |
60 | ||
61 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
62 | ||
63 | void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style) | |
64 | { | |
65 | m_invokingWindow = NULL; | |
66 | ||
67 | #if wxUSE_LIBHILDON | |
68 | // Hildon window uses a single menu instead of a menu bar, so wxMenuBar is | |
69 | // the same as menu in this case | |
70 | m_widget = | |
71 | m_menubar = gtk_menu_new(); | |
72 | #else // !wxUSE_LIBHILDON | |
73 | if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) || | |
74 | !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) | |
75 | { | |
76 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); | |
77 | return; | |
78 | } | |
79 | ||
80 | m_menubar = gtk_menu_bar_new(); | |
81 | ||
82 | if (style & wxMB_DOCKABLE) | |
83 | { | |
84 | m_widget = gtk_handle_box_new(); | |
85 | gtk_container_add(GTK_CONTAINER(m_widget), m_menubar); | |
86 | gtk_widget_show(m_menubar); | |
87 | } | |
88 | else | |
89 | { | |
90 | m_widget = m_menubar; | |
91 | } | |
92 | ||
93 | PostCreation(); | |
94 | ||
95 | ApplyWidgetStyle(); | |
96 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON | |
97 | ||
98 | for (size_t i = 0; i < n; ++i ) | |
99 | Append(menus[i], titles[i]); | |
100 | } | |
101 | ||
102 | wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style) | |
103 | { | |
104 | Init(n, menus, titles, style); | |
105 | } | |
106 | ||
107 | wxMenuBar::wxMenuBar(long style) | |
108 | { | |
109 | Init(0, NULL, NULL, style); | |
110 | } | |
111 | ||
112 | wxMenuBar::wxMenuBar() | |
113 | { | |
114 | Init(0, NULL, NULL, 0); | |
115 | } | |
116 | ||
117 | wxMenuBar::~wxMenuBar() | |
118 | { | |
119 | } | |
120 | ||
121 | static void | |
122 | wxMenubarUnsetInvokingWindow(wxMenu* menu, wxWindow* win, GtkWindow* tlw = NULL) | |
123 | { | |
124 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
125 | ||
126 | // support for native hot keys | |
127 | if (menu->m_accel) | |
128 | { | |
129 | if (tlw == NULL) | |
130 | tlw = GTK_WINDOW(wxGetTopLevelParent(win)->m_widget); | |
131 | if (g_slist_find(menu->m_accel->acceleratables, tlw)) | |
132 | gtk_window_remove_accel_group(tlw, menu->m_accel); | |
133 | } | |
134 | ||
135 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
136 | while (node) | |
137 | { | |
138 | wxMenuItem *menuitem = node->GetData(); | |
139 | if (menuitem->IsSubMenu()) | |
140 | wxMenubarUnsetInvokingWindow(menuitem->GetSubMenu(), win, tlw); | |
141 | node = node->GetNext(); | |
142 | } | |
143 | } | |
144 | ||
145 | static void | |
146 | wxMenubarSetInvokingWindow(wxMenu* menu, wxWindow* win, GtkWindow* tlw = NULL) | |
147 | { | |
148 | menu->SetInvokingWindow( win ); | |
149 | ||
150 | // support for native hot keys | |
151 | if (menu->m_accel) | |
152 | { | |
153 | if (tlw == NULL) | |
154 | tlw = GTK_WINDOW(wxGetTopLevelParent(win)->m_widget); | |
155 | if (!g_slist_find(menu->m_accel->acceleratables, tlw)) | |
156 | gtk_window_add_accel_group(tlw, menu->m_accel); | |
157 | } | |
158 | ||
159 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
160 | while (node) | |
161 | { | |
162 | wxMenuItem *menuitem = node->GetData(); | |
163 | if (menuitem->IsSubMenu()) | |
164 | wxMenubarSetInvokingWindow(menuitem->GetSubMenu(), win, tlw); | |
165 | node = node->GetNext(); | |
166 | } | |
167 | } | |
168 | ||
169 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
170 | { | |
171 | m_invokingWindow = win; | |
172 | ||
173 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
174 | while (node) | |
175 | { | |
176 | wxMenu *menu = node->GetData(); | |
177 | wxMenubarSetInvokingWindow( menu, win ); | |
178 | node = node->GetNext(); | |
179 | } | |
180 | } | |
181 | ||
182 | void wxMenuBar::SetLayoutDirection(wxLayoutDirection dir) | |
183 | { | |
184 | if ( dir == wxLayout_Default ) | |
185 | { | |
186 | const wxWindow *const frame = GetFrame(); | |
187 | if ( frame ) | |
188 | { | |
189 | // inherit layout from frame. | |
190 | dir = frame->GetLayoutDirection(); | |
191 | } | |
192 | else // use global layout | |
193 | { | |
194 | dir = wxTheApp->GetLayoutDirection(); | |
195 | } | |
196 | } | |
197 | ||
198 | if ( dir == wxLayout_Default ) | |
199 | return; | |
200 | ||
201 | GTKSetLayout(m_menubar, dir); | |
202 | ||
203 | // also set the layout of all menus we already have (new ones will inherit | |
204 | // the current layout) | |
205 | for ( wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
206 | node; | |
207 | node = node->GetNext() ) | |
208 | { | |
209 | wxMenu *const menu = node->GetData(); | |
210 | menu->SetLayoutDirection(dir); | |
211 | } | |
212 | } | |
213 | ||
214 | wxLayoutDirection wxMenuBar::GetLayoutDirection() const | |
215 | { | |
216 | return GTKGetLayout(m_menubar); | |
217 | } | |
218 | ||
219 | void wxMenuBar::Attach(wxFrame *frame) | |
220 | { | |
221 | wxMenuBarBase::Attach(frame); | |
222 | ||
223 | SetLayoutDirection(wxLayout_Default); | |
224 | } | |
225 | ||
226 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
227 | { | |
228 | m_invokingWindow = (wxWindow*) NULL; | |
229 | ||
230 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
231 | while (node) | |
232 | { | |
233 | wxMenu *menu = node->GetData(); | |
234 | wxMenubarUnsetInvokingWindow( menu, win ); | |
235 | node = node->GetNext(); | |
236 | } | |
237 | } | |
238 | ||
239 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
240 | { | |
241 | if ( !wxMenuBarBase::Append( menu, title ) ) | |
242 | return false; | |
243 | ||
244 | return GtkAppend(menu, title); | |
245 | } | |
246 | ||
247 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) | |
248 | { | |
249 | menu->SetLayoutDirection(GetLayoutDirection()); | |
250 | ||
251 | #if wxUSE_LIBHILDON | |
252 | // if the menu has only one item, append it directly to the top level menu | |
253 | // instead of inserting a useless submenu | |
254 | if ( menu->GetMenuItemCount() == 1 ) | |
255 | { | |
256 | wxMenuItem * const item = menu->FindItemByPosition(0); | |
257 | ||
258 | // remove both mnemonics and accelerator: neither is useful under Maemo | |
259 | const wxString str(wxStripMenuCodes(item->GetItemLabel())); | |
260 | ||
261 | if ( item->IsSubMenu() ) | |
262 | return GtkAppend(item->GetSubMenu(), str, pos); | |
263 | ||
264 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); | |
265 | ||
266 | g_signal_connect(menu->m_owner, "activate", | |
267 | G_CALLBACK(gtk_menu_clicked_callback), menu); | |
268 | item->SetMenuItem(menu->m_owner); | |
269 | } | |
270 | else | |
271 | #endif // wxUSE_LIBHILDON/!wxUSE_LIBHILDON | |
272 | { | |
273 | const wxString str(wxConvertMnemonicsToGTK(title)); | |
274 | ||
275 | // This doesn't have much effect right now. | |
276 | menu->SetTitle( str ); | |
277 | ||
278 | // The "m_owner" is the "menu item" | |
279 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); | |
280 | ||
281 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
282 | } | |
283 | ||
284 | gtk_widget_show( menu->m_owner ); | |
285 | ||
286 | if (pos == -1) | |
287 | gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner ); | |
288 | else | |
289 | gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos ); | |
290 | ||
291 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables | |
292 | // addings menu later on. | |
293 | if (m_invokingWindow) | |
294 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
295 | ||
296 | return true; | |
297 | } | |
298 | ||
299 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
300 | { | |
301 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
302 | return false; | |
303 | ||
304 | // TODO | |
305 | ||
306 | if ( !GtkAppend(menu, title, (int)pos) ) | |
307 | return false; | |
308 | ||
309 | return true; | |
310 | } | |
311 | ||
312 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
313 | { | |
314 | // remove the old item and insert a new one | |
315 | wxMenu *menuOld = Remove(pos); | |
316 | if ( menuOld && !Insert(pos, menu, title) ) | |
317 | { | |
318 | return (wxMenu*) NULL; | |
319 | } | |
320 | ||
321 | // either Insert() succeeded or Remove() failed and menuOld is NULL | |
322 | return menuOld; | |
323 | } | |
324 | ||
325 | wxMenu *wxMenuBar::Remove(size_t pos) | |
326 | { | |
327 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
328 | if ( !menu ) | |
329 | return (wxMenu*) NULL; | |
330 | ||
331 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->m_owner), NULL); | |
332 | gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner); | |
333 | ||
334 | gtk_widget_destroy( menu->m_owner ); | |
335 | menu->m_owner = NULL; | |
336 | ||
337 | if (m_invokingWindow) | |
338 | wxMenubarUnsetInvokingWindow( menu, m_invokingWindow ); | |
339 | ||
340 | return menu; | |
341 | } | |
342 | ||
343 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
344 | { | |
345 | if (wxMenuItem::GetLabelText(wxConvertMnemonicsFromGTK(menu->GetTitle())) == wxMenuItem::GetLabelText(menuString)) | |
346 | { | |
347 | int res = menu->FindItem( itemString ); | |
348 | if (res != wxNOT_FOUND) | |
349 | return res; | |
350 | } | |
351 | ||
352 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
353 | while (node) | |
354 | { | |
355 | wxMenuItem *item = node->GetData(); | |
356 | if (item->IsSubMenu()) | |
357 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
358 | ||
359 | node = node->GetNext(); | |
360 | } | |
361 | ||
362 | return wxNOT_FOUND; | |
363 | } | |
364 | ||
365 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
366 | { | |
367 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
368 | while (node) | |
369 | { | |
370 | wxMenu *menu = node->GetData(); | |
371 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
372 | if (res != -1) | |
373 | return res; | |
374 | node = node->GetNext(); | |
375 | } | |
376 | ||
377 | return wxNOT_FOUND; | |
378 | } | |
379 | ||
380 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
381 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
382 | { | |
383 | wxMenuItem* result = menu->FindChildItem(id); | |
384 | ||
385 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
386 | while ( node && result == NULL ) | |
387 | { | |
388 | wxMenuItem *item = node->GetData(); | |
389 | if (item->IsSubMenu()) | |
390 | { | |
391 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
392 | } | |
393 | node = node->GetNext(); | |
394 | } | |
395 | ||
396 | return result; | |
397 | } | |
398 | ||
399 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const | |
400 | { | |
401 | wxMenuItem* result = 0; | |
402 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
403 | while (node && result == 0) | |
404 | { | |
405 | wxMenu *menu = node->GetData(); | |
406 | result = FindMenuItemByIdRecursive( menu, id ); | |
407 | node = node->GetNext(); | |
408 | } | |
409 | ||
410 | if ( menuForItem ) | |
411 | { | |
412 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; | |
413 | } | |
414 | ||
415 | return result; | |
416 | } | |
417 | ||
418 | void wxMenuBar::EnableTop( size_t pos, bool flag ) | |
419 | { | |
420 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
421 | ||
422 | wxCHECK_RET( node, wxT("menu not found") ); | |
423 | ||
424 | wxMenu* menu = node->GetData(); | |
425 | ||
426 | if (menu->m_owner) | |
427 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
428 | } | |
429 | ||
430 | wxString wxMenuBar::GetMenuLabel( size_t pos ) const | |
431 | { | |
432 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
433 | ||
434 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); | |
435 | ||
436 | wxMenu* menu = node->GetData(); | |
437 | ||
438 | return wxConvertMnemonicsFromGTK(menu->GetTitle()); | |
439 | } | |
440 | ||
441 | void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label ) | |
442 | { | |
443 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
444 | ||
445 | wxCHECK_RET( node, wxT("menu not found") ); | |
446 | ||
447 | wxMenu* menu = node->GetData(); | |
448 | ||
449 | const wxString str(wxConvertMnemonicsToGTK(label)); | |
450 | ||
451 | menu->SetTitle( str ); | |
452 | ||
453 | if (menu->m_owner) | |
454 | gtk_label_set_text_with_mnemonic( GTK_LABEL( GTK_BIN(menu->m_owner)->child), wxGTK_CONV(str) ); | |
455 | } | |
456 | ||
457 | //----------------------------------------------------------------------------- | |
458 | // "activate" | |
459 | //----------------------------------------------------------------------------- | |
460 | ||
461 | extern "C" { | |
462 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) | |
463 | { | |
464 | int id = menu->FindMenuIdByMenuItem(widget); | |
465 | ||
466 | /* should find it for normal (not popup) menu */ | |
467 | wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL), | |
468 | _T("menu item not found in gtk_menu_clicked_callback") ); | |
469 | ||
470 | if (!menu->IsEnabled(id)) | |
471 | return; | |
472 | ||
473 | wxMenuItem* item = menu->FindChildItem( id ); | |
474 | wxCHECK_RET( item, wxT("error in menu item callback") ); | |
475 | ||
476 | if ( item->GetId() == wxGTK_TITLE_ID ) | |
477 | { | |
478 | // ignore events from the menu title | |
479 | return; | |
480 | } | |
481 | ||
482 | if (item->IsCheckable()) | |
483 | { | |
484 | bool isReallyChecked = item->IsChecked(), | |
485 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); | |
486 | ||
487 | // ensure that the internal state is always consistent with what is | |
488 | // shown on the screen | |
489 | item->wxMenuItemBase::Check(isReallyChecked); | |
490 | ||
491 | // we must not report the events for the radio button going up nor the | |
492 | // events resulting from the calls to wxMenuItem::Check() | |
493 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || | |
494 | (isInternallyChecked == isReallyChecked) ) | |
495 | { | |
496 | return; | |
497 | } | |
498 | } | |
499 | ||
500 | ||
501 | // Is this menu on a menubar? (possibly nested) | |
502 | wxFrame* frame = NULL; | |
503 | if(menu->IsAttached()) | |
504 | frame = menu->GetMenuBar()->GetFrame(); | |
505 | ||
506 | // FIXME: why do we have to call wxFrame::GetEventHandler() directly here? | |
507 | // normally wxMenu::SendEvent() should be enough, if it doesn't work | |
508 | // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which | |
509 | // should be fixed instead of working around it here... | |
510 | if (frame) | |
511 | { | |
512 | // If it is attached then let the frame send the event. | |
513 | // Don't call frame->ProcessCommand(id) because it toggles | |
514 | // checkable items and we've already done that above. | |
515 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); | |
516 | commandEvent.SetEventObject(frame); | |
517 | if (item->IsCheckable()) | |
518 | commandEvent.SetInt(item->IsChecked()); | |
519 | ||
520 | frame->HandleWindowEvent(commandEvent); | |
521 | } | |
522 | else | |
523 | { | |
524 | // otherwise let the menu have it | |
525 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | //----------------------------------------------------------------------------- | |
531 | // "select" | |
532 | //----------------------------------------------------------------------------- | |
533 | ||
534 | extern "C" { | |
535 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
536 | { | |
537 | int id = menu->FindMenuIdByMenuItem(widget); | |
538 | ||
539 | wxASSERT( id != -1 ); // should find it! | |
540 | ||
541 | if (!menu->IsEnabled(id)) | |
542 | return; | |
543 | ||
544 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); | |
545 | event.SetEventObject( menu ); | |
546 | ||
547 | wxEvtHandler* handler = menu->GetEventHandler(); | |
548 | if (handler && handler->SafelyProcessEvent(event)) | |
549 | return; | |
550 | ||
551 | wxWindow *win = menu->GetInvokingWindow(); | |
552 | if (win) win->HandleWindowEvent( event ); | |
553 | } | |
554 | } | |
555 | ||
556 | //----------------------------------------------------------------------------- | |
557 | // "deselect" | |
558 | //----------------------------------------------------------------------------- | |
559 | ||
560 | extern "C" { | |
561 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
562 | { | |
563 | int id = menu->FindMenuIdByMenuItem(widget); | |
564 | ||
565 | wxASSERT( id != -1 ); // should find it! | |
566 | ||
567 | if (!menu->IsEnabled(id)) | |
568 | return; | |
569 | ||
570 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
571 | event.SetEventObject( menu ); | |
572 | ||
573 | wxEvtHandler* handler = menu->GetEventHandler(); | |
574 | if (handler && handler->SafelyProcessEvent(event)) | |
575 | return; | |
576 | ||
577 | wxWindow *win = menu->GetInvokingWindow(); | |
578 | if (win) | |
579 | win->HandleWindowEvent( event ); | |
580 | } | |
581 | } | |
582 | ||
583 | //----------------------------------------------------------------------------- | |
584 | // wxMenuItem | |
585 | //----------------------------------------------------------------------------- | |
586 | ||
587 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
588 | ||
589 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
590 | int id, | |
591 | const wxString& name, | |
592 | const wxString& help, | |
593 | wxItemKind kind, | |
594 | wxMenu *subMenu) | |
595 | { | |
596 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); | |
597 | } | |
598 | ||
599 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
600 | int id, | |
601 | const wxString& text, | |
602 | const wxString& help, | |
603 | wxItemKind kind, | |
604 | wxMenu *subMenu) | |
605 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) | |
606 | { | |
607 | Init(text); | |
608 | } | |
609 | ||
610 | #if WXWIN_COMPATIBILITY_2_8 | |
611 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
612 | int id, | |
613 | const wxString& text, | |
614 | const wxString& help, | |
615 | bool isCheckable, | |
616 | wxMenu *subMenu) | |
617 | : wxMenuItemBase(parentMenu, id, text, help, | |
618 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
619 | { | |
620 | Init(text); | |
621 | } | |
622 | #endif | |
623 | ||
624 | void wxMenuItem::Init(const wxString& text) | |
625 | { | |
626 | m_menuItem = (GtkWidget *) NULL; | |
627 | ||
628 | DoSetText(text); | |
629 | } | |
630 | ||
631 | wxMenuItem::~wxMenuItem() | |
632 | { | |
633 | // don't delete menu items, the menus take care of that | |
634 | } | |
635 | ||
636 | // return the menu item text without any menu accels | |
637 | /* static */ | |
638 | ||
639 | wxString wxMenuItemBase::GetLabelText(const wxString& text) | |
640 | { | |
641 | // The argument to this function will now always be in wxWidgets standard label | |
642 | // format, not GTK+ format, so we do what the other ports do. | |
643 | ||
644 | return wxStripMenuCodes(text); | |
645 | ||
646 | #if 0 | |
647 | wxString label; | |
648 | ||
649 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) | |
650 | { | |
651 | if ( *pc == wxT('\t')) | |
652 | break; | |
653 | ||
654 | if ( *pc == wxT('_') ) | |
655 | { | |
656 | // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx" | |
657 | pc++; | |
658 | label += *pc; | |
659 | continue; | |
660 | } | |
661 | ||
662 | if ( *pc == wxT('\\') ) | |
663 | { | |
664 | // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx" | |
665 | pc++; | |
666 | label += *pc; | |
667 | continue; | |
668 | } | |
669 | ||
670 | if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) ) | |
671 | { | |
672 | // wxMSW escapes "&" | |
673 | // "&" is doubled to indicate "&" instead of accelerator | |
674 | continue; | |
675 | } | |
676 | ||
677 | label += *pc; | |
678 | } | |
679 | ||
680 | // wxPrintf( wxT("GetLabelText(): text %s label %s\n"), text.c_str(), label.c_str() ); | |
681 | ||
682 | return label; | |
683 | #endif | |
684 | } | |
685 | ||
686 | wxString wxMenuItem::GetItemLabel() const | |
687 | { | |
688 | wxString label = wxConvertMnemonicsFromGTK(m_text); | |
689 | if (!m_hotKey.IsEmpty()) | |
690 | label << "\t" << m_hotKey; | |
691 | return label; | |
692 | } | |
693 | ||
694 | void wxMenuItem::SetItemLabel( const wxString& str ) | |
695 | { | |
696 | // cache some data which must be used later | |
697 | bool isstock = wxIsStockID(GetId()); | |
698 | const char *stockid = NULL; | |
699 | if (isstock) | |
700 | stockid = wxGetStockGtkID(GetId()); | |
701 | ||
702 | // Some optimization to avoid flicker | |
703 | wxString oldLabel = m_text; | |
704 | oldLabel = wxStripMenuCodes(oldLabel); | |
705 | oldLabel.Replace(wxT("_"), wxT("")); | |
706 | wxString label1 = wxStripMenuCodes(str); | |
707 | #if wxUSE_ACCEL | |
708 | wxString oldhotkey = m_hotKey; // Store the old hotkey in Ctrl-foo format | |
709 | wxCharBuffer oldbuf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); // and as <control>foo | |
710 | #endif // wxUSE_ACCEL | |
711 | ||
712 | DoSetText(str); | |
713 | ||
714 | #if wxUSE_ACCEL | |
715 | if (oldLabel == label1 && | |
716 | oldhotkey == m_hotKey) // Make sure we can change a hotkey even if the label is unaltered | |
717 | return; | |
718 | ||
719 | if (m_menuItem) | |
720 | { | |
721 | // stock menu items can have empty labels: | |
722 | wxString text = m_text; | |
723 | if (text.IsEmpty() && !IsSeparator()) | |
724 | { | |
725 | wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?")); | |
726 | text = wxGetStockLabel(GetId()); | |
727 | ||
728 | // need & => _ conversion | |
729 | text = GTKProcessMenuItemLabel(text, NULL); | |
730 | } | |
731 | ||
732 | GtkLabel* label = GTK_LABEL(GTK_BIN(m_menuItem)->child); | |
733 | gtk_label_set_text_with_mnemonic(label, wxGTK_CONV_SYS(text)); | |
734 | } | |
735 | ||
736 | // remove old accelerator from our parent's accelerator group, if present | |
737 | guint accel_key; | |
738 | GdkModifierType accel_mods; | |
739 | if (oldbuf[(size_t)0] != '\0') | |
740 | { | |
741 | gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); | |
742 | if (accel_key != 0) | |
743 | { | |
744 | gtk_widget_remove_accelerator(m_menuItem, | |
745 | m_parentMenu->m_accel, | |
746 | accel_key, | |
747 | accel_mods ); | |
748 | } | |
749 | } | |
750 | else if (isstock) | |
751 | { | |
752 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock | |
753 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
754 | gtk_widget_remove_accelerator( m_menuItem, | |
755 | m_parentMenu->m_accel, | |
756 | accel_key, | |
757 | accel_mods ); | |
758 | } | |
759 | ||
760 | // add new accelerator to our parent's accelerator group | |
761 | wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*this) ); | |
762 | if (buf[(size_t)0] != '\0') | |
763 | { | |
764 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); | |
765 | if (accel_key != 0) | |
766 | { | |
767 | gtk_widget_add_accelerator( m_menuItem, | |
768 | "activate", | |
769 | m_parentMenu->m_accel, | |
770 | accel_key, | |
771 | accel_mods, | |
772 | GTK_ACCEL_VISIBLE); | |
773 | } | |
774 | } | |
775 | else if (isstock) | |
776 | { | |
777 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock | |
778 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
779 | gtk_widget_remove_accelerator( m_menuItem, | |
780 | m_parentMenu->m_accel, | |
781 | accel_key, | |
782 | accel_mods ); | |
783 | } | |
784 | #endif // wxUSE_ACCEL | |
785 | } | |
786 | ||
787 | // NOTE: this function is different from the similar functions GTKProcessMnemonics() | |
788 | // implemented in control.cpp and from wxMenuItemBase::GetLabelText... | |
789 | // so there's no real code duplication | |
790 | wxString wxMenuItem::GTKProcessMenuItemLabel(const wxString& str, wxString *hotKey) | |
791 | { | |
792 | wxString text; | |
793 | ||
794 | // '\t' is the deliminator indicating a hot key | |
795 | wxString::const_iterator pc = str.begin(); | |
796 | while ( pc != str.end() && *pc != wxT('\t') ) | |
797 | { | |
798 | if (*pc == wxT('&')) | |
799 | { | |
800 | wxString::const_iterator next = pc + 1; | |
801 | if (next != str.end() && *next == wxT('&')) | |
802 | { | |
803 | // "&" is doubled to indicate "&" instead of accelerator | |
804 | ++pc; | |
805 | text << wxT('&'); | |
806 | } | |
807 | else | |
808 | { | |
809 | text << wxT('_'); | |
810 | } | |
811 | } | |
812 | else if ( *pc == wxT('_') ) // escape underscores | |
813 | { | |
814 | text << wxT("__"); | |
815 | } | |
816 | else | |
817 | { | |
818 | text << *pc; | |
819 | } | |
820 | ++pc; | |
821 | } | |
822 | ||
823 | if (hotKey) | |
824 | { | |
825 | hotKey->Empty(); | |
826 | if(*pc == wxT('\t')) | |
827 | { | |
828 | ++pc; | |
829 | hotKey->assign(pc, str.end()); | |
830 | } | |
831 | } | |
832 | ||
833 | return text; | |
834 | } | |
835 | ||
836 | // it's valid for this function to be called even if m_menuItem == NULL | |
837 | void wxMenuItem::DoSetText( const wxString& str ) | |
838 | { | |
839 | m_text.Empty(); | |
840 | m_text = GTKProcessMenuItemLabel(str, &m_hotKey); | |
841 | } | |
842 | ||
843 | #if wxUSE_ACCEL | |
844 | ||
845 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
846 | { | |
847 | if (m_hotKey.empty()) | |
848 | { | |
849 | // nothing | |
850 | return NULL; | |
851 | } | |
852 | ||
853 | // accelerator parsing code looks for them after a TAB, so insert a dummy | |
854 | // one here | |
855 | wxString label; | |
856 | label << wxT('\t') << m_hotKey; | |
857 | ||
858 | return wxAcceleratorEntry::Create(label); | |
859 | } | |
860 | ||
861 | #endif // wxUSE_ACCEL | |
862 | ||
863 | void wxMenuItem::Check( bool check ) | |
864 | { | |
865 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); | |
866 | ||
867 | if (check == m_isChecked) | |
868 | return; | |
869 | ||
870 | wxMenuItemBase::Check( check ); | |
871 | ||
872 | switch ( GetKind() ) | |
873 | { | |
874 | case wxITEM_CHECK: | |
875 | case wxITEM_RADIO: | |
876 | gtk_check_menu_item_set_active( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
877 | break; | |
878 | ||
879 | default: | |
880 | wxFAIL_MSG( _T("can't check this item") ); | |
881 | } | |
882 | } | |
883 | ||
884 | void wxMenuItem::Enable( bool enable ) | |
885 | { | |
886 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); | |
887 | ||
888 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
889 | wxMenuItemBase::Enable( enable ); | |
890 | } | |
891 | ||
892 | bool wxMenuItem::IsChecked() const | |
893 | { | |
894 | wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") ); | |
895 | ||
896 | wxCHECK_MSG( IsCheckable(), false, | |
897 | wxT("can't get state of uncheckable item!") ); | |
898 | ||
899 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
900 | } | |
901 | ||
902 | //----------------------------------------------------------------------------- | |
903 | // wxMenu | |
904 | //----------------------------------------------------------------------------- | |
905 | ||
906 | extern "C" { | |
907 | // "map" from m_menu | |
908 | static void menu_map(GtkWidget*, wxMenu* menu) | |
909 | { | |
910 | wxMenuEvent event(wxEVT_MENU_OPEN, menu->m_popupShown ? -1 : 0, menu); | |
911 | DoCommonMenuCallbackCode(menu, event); | |
912 | } | |
913 | ||
914 | // "hide" from m_menu | |
915 | static void menu_hide(GtkWidget*, wxMenu* menu) | |
916 | { | |
917 | wxMenuEvent event(wxEVT_MENU_CLOSE, menu->m_popupShown ? -1 : 0, menu); | |
918 | menu->m_popupShown = false; | |
919 | DoCommonMenuCallbackCode(menu, event); | |
920 | } | |
921 | } | |
922 | ||
923 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
924 | ||
925 | void wxMenu::Init() | |
926 | { | |
927 | m_popupShown = false; | |
928 | ||
929 | m_accel = gtk_accel_group_new(); | |
930 | m_menu = gtk_menu_new(); | |
931 | // NB: keep reference to the menu so that it is not destroyed behind | |
932 | // our back by GTK+ e.g. when it is removed from menubar: | |
933 | g_object_ref(m_menu); | |
934 | gtk_object_sink(GTK_OBJECT(m_menu)); | |
935 | ||
936 | m_owner = (GtkWidget*) NULL; | |
937 | ||
938 | // Tearoffs are entries, just like separators. So if we want this | |
939 | // menu to be a tear-off one, we just append a tearoff entry | |
940 | // immediately. | |
941 | if ( m_style & wxMENU_TEAROFF ) | |
942 | { | |
943 | GtkWidget *tearoff = gtk_tearoff_menu_item_new(); | |
944 | ||
945 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), tearoff); | |
946 | } | |
947 | ||
948 | m_prevRadio = NULL; | |
949 | ||
950 | // append the title as the very first entry if we have it | |
951 | if ( !m_title.empty() ) | |
952 | { | |
953 | Append(wxGTK_TITLE_ID, m_title); | |
954 | AppendSeparator(); | |
955 | } | |
956 | ||
957 | // "show" occurs for sub-menus which are not showing, so use "map" instead | |
958 | g_signal_connect(m_menu, "map", G_CALLBACK(menu_map), this); | |
959 | g_signal_connect(m_menu, "hide", G_CALLBACK(menu_hide), this); | |
960 | } | |
961 | ||
962 | wxMenu::~wxMenu() | |
963 | { | |
964 | // see wxMenu::Init | |
965 | g_object_unref(m_menu); | |
966 | ||
967 | // if the menu is inserted in another menu at this time, there was | |
968 | // one more reference to it: | |
969 | if (m_owner) | |
970 | gtk_widget_destroy(m_menu); | |
971 | ||
972 | g_object_unref(m_accel); | |
973 | } | |
974 | ||
975 | void wxMenu::SetLayoutDirection(const wxLayoutDirection dir) | |
976 | { | |
977 | if ( m_owner ) | |
978 | wxWindow::GTKSetLayout(m_owner, dir); | |
979 | //else: will be called later by wxMenuBar again | |
980 | } | |
981 | ||
982 | wxLayoutDirection wxMenu::GetLayoutDirection() const | |
983 | { | |
984 | return wxWindow::GTKGetLayout(m_owner); | |
985 | } | |
986 | ||
987 | bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) | |
988 | { | |
989 | GtkWidget *menuItem; | |
990 | ||
991 | // cache some data used later | |
992 | wxString text = mitem->wxMenuItemBase::GetItemLabel(); | |
993 | int id = mitem->GetId(); | |
994 | bool isstock = wxIsStockID(id); | |
995 | const char *stockid = NULL; | |
996 | if (isstock) | |
997 | stockid = wxGetStockGtkID(mitem->GetId()); | |
998 | ||
999 | // stock menu items can have an empty label | |
1000 | if (text.IsEmpty() && !mitem->IsSeparator()) | |
1001 | { | |
1002 | wxASSERT_MSG(isstock, wxT("A non-stock menu item with an empty label?")); | |
1003 | text = wxGetStockLabel(id); | |
1004 | ||
1005 | // need & => _ conversion | |
1006 | text = wxMenuItem::GTKProcessMenuItemLabel(text, NULL); | |
1007 | } | |
1008 | ||
1009 | if ( mitem->IsSeparator() ) | |
1010 | { | |
1011 | menuItem = gtk_separator_menu_item_new(); | |
1012 | m_prevRadio = NULL; | |
1013 | } | |
1014 | else if ( mitem->GetBitmap().Ok() || | |
1015 | (mitem->GetKind() == wxITEM_NORMAL && isstock) ) | |
1016 | { | |
1017 | wxBitmap bitmap(mitem->GetBitmap()); | |
1018 | ||
1019 | menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); | |
1020 | ||
1021 | GtkWidget *image; | |
1022 | if ( !bitmap.Ok() ) | |
1023 | { | |
1024 | // use stock bitmap for this item if available on the assumption | |
1025 | // that it never hurts to follow GTK+ conventions more closely | |
1026 | image = stockid ? gtk_image_new_from_stock(stockid, GTK_ICON_SIZE_MENU) | |
1027 | : NULL; | |
1028 | } | |
1029 | else // we have a custom bitmap | |
1030 | { | |
1031 | wxASSERT_MSG( mitem->GetKind() == wxITEM_NORMAL, | |
1032 | _T("only normal menu items can have bitmaps") ); | |
1033 | ||
1034 | if ( bitmap.HasPixbuf() ) | |
1035 | { | |
1036 | image = gtk_image_new_from_pixbuf(bitmap.GetPixbuf()); | |
1037 | } | |
1038 | else | |
1039 | { | |
1040 | GdkPixmap *gdk_pixmap = bitmap.GetPixmap(); | |
1041 | GdkBitmap *gdk_bitmap = bitmap.GetMask() ? | |
1042 | bitmap.GetMask()->GetBitmap() : | |
1043 | (GdkBitmap*) NULL; | |
1044 | image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap ); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | if ( image ) | |
1049 | { | |
1050 | gtk_widget_show(image); | |
1051 | ||
1052 | gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image ); | |
1053 | } | |
1054 | ||
1055 | m_prevRadio = NULL; | |
1056 | } | |
1057 | else // a normal item | |
1058 | { | |
1059 | // NB: 'text' variable has "_" instead of "&" after mitem->SetItemLabel() | |
1060 | // so don't use it | |
1061 | ||
1062 | switch ( mitem->GetKind() ) | |
1063 | { | |
1064 | case wxITEM_CHECK: | |
1065 | { | |
1066 | menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); | |
1067 | m_prevRadio = NULL; | |
1068 | break; | |
1069 | } | |
1070 | ||
1071 | case wxITEM_RADIO: | |
1072 | { | |
1073 | GSList *group = NULL; | |
1074 | if ( m_prevRadio == NULL ) | |
1075 | { | |
1076 | // start of a new radio group | |
1077 | m_prevRadio = menuItem = | |
1078 | gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) ); | |
1079 | } | |
1080 | else // continue the radio group | |
1081 | { | |
1082 | group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio)); | |
1083 | m_prevRadio = menuItem = | |
1084 | gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV_SYS( text ) ); | |
1085 | } | |
1086 | break; | |
1087 | } | |
1088 | ||
1089 | default: | |
1090 | wxFAIL_MSG( _T("unexpected menu item kind") ); | |
1091 | // fall through | |
1092 | ||
1093 | case wxITEM_NORMAL: | |
1094 | { | |
1095 | menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV_SYS( text ) ); | |
1096 | m_prevRadio = NULL; | |
1097 | break; | |
1098 | } | |
1099 | } | |
1100 | ||
1101 | } | |
1102 | ||
1103 | #if wxUSE_ACCEL | |
1104 | guint accel_key; | |
1105 | GdkModifierType accel_mods; | |
1106 | wxCharBuffer buf = wxGTK_CONV_SYS( GetGtkHotKey(*mitem) ); | |
1107 | ||
1108 | if (buf[(size_t)0] != '\0') | |
1109 | { | |
1110 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); | |
1111 | if (accel_key != 0) | |
1112 | { | |
1113 | gtk_widget_add_accelerator (menuItem, | |
1114 | "activate", | |
1115 | m_accel, | |
1116 | accel_key, | |
1117 | accel_mods, | |
1118 | GTK_ACCEL_VISIBLE); | |
1119 | } | |
1120 | } | |
1121 | else if (isstock) | |
1122 | { | |
1123 | // if the accelerator was taken from a stock ID, just get it back from GTK+ stock | |
1124 | if (wxGetStockGtkAccelerator(stockid, &accel_mods, &accel_key)) | |
1125 | gtk_widget_add_accelerator( menuItem, | |
1126 | "activate", | |
1127 | m_accel, | |
1128 | accel_key, | |
1129 | accel_mods, | |
1130 | GTK_ACCEL_VISIBLE); | |
1131 | } | |
1132 | #endif // wxUSE_ACCEL | |
1133 | ||
1134 | if (pos == -1) | |
1135 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem); | |
1136 | else | |
1137 | gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos); | |
1138 | ||
1139 | gtk_widget_show( menuItem ); | |
1140 | ||
1141 | if ( !mitem->IsSeparator() ) | |
1142 | { | |
1143 | wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); | |
1144 | ||
1145 | g_signal_connect (menuItem, "select", | |
1146 | G_CALLBACK (gtk_menu_hilight_callback), this); | |
1147 | g_signal_connect (menuItem, "deselect", | |
1148 | G_CALLBACK (gtk_menu_nolight_callback), this); | |
1149 | ||
1150 | if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) | |
1151 | { | |
1152 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); | |
1153 | ||
1154 | gtk_widget_show( mitem->GetSubMenu()->m_menu ); | |
1155 | ||
1156 | // if adding a submenu to a menu already existing in the menu bar, we | |
1157 | // must set invoking window to allow processing events from this | |
1158 | // submenu | |
1159 | if ( m_invokingWindow ) | |
1160 | wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); | |
1161 | } | |
1162 | else | |
1163 | { | |
1164 | g_signal_connect (menuItem, "activate", | |
1165 | G_CALLBACK (gtk_menu_clicked_callback), | |
1166 | this); | |
1167 | } | |
1168 | } | |
1169 | ||
1170 | mitem->SetMenuItem(menuItem); | |
1171 | ||
1172 | if (ms_locked) | |
1173 | { | |
1174 | // This doesn't even exist! | |
1175 | // gtk_widget_lock_accelerators(mitem->GetMenuItem()); | |
1176 | } | |
1177 | ||
1178 | return true; | |
1179 | } | |
1180 | ||
1181 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) | |
1182 | { | |
1183 | if (!GtkAppend(mitem)) | |
1184 | return NULL; | |
1185 | ||
1186 | return wxMenuBase::DoAppend(mitem); | |
1187 | } | |
1188 | ||
1189 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
1190 | { | |
1191 | if ( !wxMenuBase::DoInsert(pos, item) ) | |
1192 | return NULL; | |
1193 | ||
1194 | // TODO | |
1195 | if ( !GtkAppend(item, (int)pos) ) | |
1196 | return NULL; | |
1197 | ||
1198 | return item; | |
1199 | } | |
1200 | ||
1201 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
1202 | { | |
1203 | if ( !wxMenuBase::DoRemove(item) ) | |
1204 | return NULL; | |
1205 | ||
1206 | GtkWidget * const mitem = item->GetMenuItem(); | |
1207 | if ( m_prevRadio == mitem ) | |
1208 | { | |
1209 | // deleting an item starts a new radio group (has to as we shouldn't | |
1210 | // keep a deleted pointer anyhow) | |
1211 | m_prevRadio = NULL; | |
1212 | } | |
1213 | ||
1214 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(mitem), NULL); | |
1215 | gtk_widget_destroy(mitem); | |
1216 | item->SetMenuItem(NULL); | |
1217 | ||
1218 | return item; | |
1219 | } | |
1220 | ||
1221 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
1222 | { | |
1223 | wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
1224 | while (node) | |
1225 | { | |
1226 | wxMenuItem *item = node->GetData(); | |
1227 | if (item->GetMenuItem() == menuItem) | |
1228 | return item->GetId(); | |
1229 | node = node->GetNext(); | |
1230 | } | |
1231 | ||
1232 | return wxNOT_FOUND; | |
1233 | } | |
1234 | ||
1235 | void wxMenu::Attach(wxMenuBarBase *menubar) | |
1236 | { | |
1237 | wxMenuBase::Attach(menubar); | |
1238 | ||
1239 | // inherit layout direction from menubar. | |
1240 | SetLayoutDirection(menubar->GetLayoutDirection()); | |
1241 | } | |
1242 | ||
1243 | // ---------------------------------------------------------------------------- | |
1244 | // helpers | |
1245 | // ---------------------------------------------------------------------------- | |
1246 | ||
1247 | #if wxUSE_ACCEL | |
1248 | ||
1249 | static wxString GetGtkHotKey( const wxMenuItem& item ) | |
1250 | { | |
1251 | wxString hotkey; | |
1252 | ||
1253 | wxAcceleratorEntry *accel = item.GetAccel(); | |
1254 | if ( accel ) | |
1255 | { | |
1256 | int flags = accel->GetFlags(); | |
1257 | if ( flags & wxACCEL_ALT ) | |
1258 | hotkey += wxT("<alt>"); | |
1259 | if ( flags & wxACCEL_CTRL ) | |
1260 | hotkey += wxT("<control>"); | |
1261 | if ( flags & wxACCEL_SHIFT ) | |
1262 | hotkey += wxT("<shift>"); | |
1263 | ||
1264 | int code = accel->GetKeyCode(); | |
1265 | switch ( code ) | |
1266 | { | |
1267 | case WXK_F1: | |
1268 | case WXK_F2: | |
1269 | case WXK_F3: | |
1270 | case WXK_F4: | |
1271 | case WXK_F5: | |
1272 | case WXK_F6: | |
1273 | case WXK_F7: | |
1274 | case WXK_F8: | |
1275 | case WXK_F9: | |
1276 | case WXK_F10: | |
1277 | case WXK_F11: | |
1278 | case WXK_F12: | |
1279 | case WXK_F13: | |
1280 | case WXK_F14: | |
1281 | case WXK_F15: | |
1282 | case WXK_F16: | |
1283 | case WXK_F17: | |
1284 | case WXK_F18: | |
1285 | case WXK_F19: | |
1286 | case WXK_F20: | |
1287 | case WXK_F21: | |
1288 | case WXK_F22: | |
1289 | case WXK_F23: | |
1290 | case WXK_F24: | |
1291 | hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1); | |
1292 | break; | |
1293 | ||
1294 | // TODO: we should use gdk_keyval_name() (a.k.a. | |
1295 | // XKeysymToString) here as well as hardcoding the keysym | |
1296 | // names this might be not portable | |
1297 | case WXK_INSERT: | |
1298 | hotkey << wxT("Insert" ); | |
1299 | break; | |
1300 | case WXK_DELETE: | |
1301 | hotkey << wxT("Delete" ); | |
1302 | break; | |
1303 | case WXK_UP: | |
1304 | hotkey << wxT("Up" ); | |
1305 | break; | |
1306 | case WXK_DOWN: | |
1307 | hotkey << wxT("Down" ); | |
1308 | break; | |
1309 | case WXK_PAGEUP: | |
1310 | hotkey << wxT("Page_Up" ); | |
1311 | break; | |
1312 | case WXK_PAGEDOWN: | |
1313 | hotkey << wxT("Page_Down" ); | |
1314 | break; | |
1315 | case WXK_LEFT: | |
1316 | hotkey << wxT("Left" ); | |
1317 | break; | |
1318 | case WXK_RIGHT: | |
1319 | hotkey << wxT("Right" ); | |
1320 | break; | |
1321 | case WXK_HOME: | |
1322 | hotkey << wxT("Home" ); | |
1323 | break; | |
1324 | case WXK_END: | |
1325 | hotkey << wxT("End" ); | |
1326 | break; | |
1327 | case WXK_RETURN: | |
1328 | hotkey << wxT("Return" ); | |
1329 | break; | |
1330 | case WXK_BACK: | |
1331 | hotkey << wxT("BackSpace" ); | |
1332 | break; | |
1333 | case WXK_TAB: | |
1334 | hotkey << wxT("Tab" ); | |
1335 | break; | |
1336 | case WXK_ESCAPE: | |
1337 | hotkey << wxT("Esc" ); | |
1338 | break; | |
1339 | case WXK_SPACE: | |
1340 | hotkey << wxT("space" ); | |
1341 | break; | |
1342 | case WXK_MULTIPLY: | |
1343 | hotkey << wxT("Multiply" ); | |
1344 | break; | |
1345 | case WXK_ADD: | |
1346 | hotkey << wxT("Add" ); | |
1347 | break; | |
1348 | case WXK_SEPARATOR: | |
1349 | hotkey << wxT("Separator" ); | |
1350 | break; | |
1351 | case WXK_SUBTRACT: | |
1352 | hotkey << wxT("Subtract" ); | |
1353 | break; | |
1354 | case WXK_DECIMAL: | |
1355 | hotkey << wxT("Decimal" ); | |
1356 | break; | |
1357 | case WXK_DIVIDE: | |
1358 | hotkey << wxT("Divide" ); | |
1359 | break; | |
1360 | case WXK_CANCEL: | |
1361 | hotkey << wxT("Cancel" ); | |
1362 | break; | |
1363 | case WXK_CLEAR: | |
1364 | hotkey << wxT("Clear" ); | |
1365 | break; | |
1366 | case WXK_MENU: | |
1367 | hotkey << wxT("Menu" ); | |
1368 | break; | |
1369 | case WXK_PAUSE: | |
1370 | hotkey << wxT("Pause" ); | |
1371 | break; | |
1372 | case WXK_CAPITAL: | |
1373 | hotkey << wxT("Capital" ); | |
1374 | break; | |
1375 | case WXK_SELECT: | |
1376 | hotkey << wxT("Select" ); | |
1377 | break; | |
1378 | case WXK_PRINT: | |
1379 | hotkey << wxT("Print" ); | |
1380 | break; | |
1381 | case WXK_EXECUTE: | |
1382 | hotkey << wxT("Execute" ); | |
1383 | break; | |
1384 | case WXK_SNAPSHOT: | |
1385 | hotkey << wxT("Snapshot" ); | |
1386 | break; | |
1387 | case WXK_HELP: | |
1388 | hotkey << wxT("Help" ); | |
1389 | break; | |
1390 | case WXK_NUMLOCK: | |
1391 | hotkey << wxT("Num_Lock" ); | |
1392 | break; | |
1393 | case WXK_SCROLL: | |
1394 | hotkey << wxT("Scroll_Lock" ); | |
1395 | break; | |
1396 | case WXK_NUMPAD_INSERT: | |
1397 | hotkey << wxT("KP_Insert" ); | |
1398 | break; | |
1399 | case WXK_NUMPAD_DELETE: | |
1400 | hotkey << wxT("KP_Delete" ); | |
1401 | break; | |
1402 | case WXK_NUMPAD_SPACE: | |
1403 | hotkey << wxT("KP_Space" ); | |
1404 | break; | |
1405 | case WXK_NUMPAD_TAB: | |
1406 | hotkey << wxT("KP_Tab" ); | |
1407 | break; | |
1408 | case WXK_NUMPAD_ENTER: | |
1409 | hotkey << wxT("KP_Enter" ); | |
1410 | break; | |
1411 | case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3: | |
1412 | case WXK_NUMPAD_F4: | |
1413 | hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1); | |
1414 | break; | |
1415 | case WXK_NUMPAD_HOME: | |
1416 | hotkey << wxT("KP_Home" ); | |
1417 | break; | |
1418 | case WXK_NUMPAD_LEFT: | |
1419 | hotkey << wxT("KP_Left" ); | |
1420 | break; | |
1421 | case WXK_NUMPAD_UP: | |
1422 | hotkey << wxT("KP_Up" ); | |
1423 | break; | |
1424 | case WXK_NUMPAD_RIGHT: | |
1425 | hotkey << wxT("KP_Right" ); | |
1426 | break; | |
1427 | case WXK_NUMPAD_DOWN: | |
1428 | hotkey << wxT("KP_Down" ); | |
1429 | break; | |
1430 | case WXK_NUMPAD_PAGEUP: | |
1431 | hotkey << wxT("KP_Page_Up" ); | |
1432 | break; | |
1433 | case WXK_NUMPAD_PAGEDOWN: | |
1434 | hotkey << wxT("KP_Page_Down" ); | |
1435 | break; | |
1436 | case WXK_NUMPAD_END: | |
1437 | hotkey << wxT("KP_End" ); | |
1438 | break; | |
1439 | case WXK_NUMPAD_BEGIN: | |
1440 | hotkey << wxT("KP_Begin" ); | |
1441 | break; | |
1442 | case WXK_NUMPAD_EQUAL: | |
1443 | hotkey << wxT("KP_Equal" ); | |
1444 | break; | |
1445 | case WXK_NUMPAD_MULTIPLY: | |
1446 | hotkey << wxT("KP_Multiply" ); | |
1447 | break; | |
1448 | case WXK_NUMPAD_ADD: | |
1449 | hotkey << wxT("KP_Add" ); | |
1450 | break; | |
1451 | case WXK_NUMPAD_SEPARATOR: | |
1452 | hotkey << wxT("KP_Separator" ); | |
1453 | break; | |
1454 | case WXK_NUMPAD_SUBTRACT: | |
1455 | hotkey << wxT("KP_Subtract" ); | |
1456 | break; | |
1457 | case WXK_NUMPAD_DECIMAL: | |
1458 | hotkey << wxT("KP_Decimal" ); | |
1459 | break; | |
1460 | case WXK_NUMPAD_DIVIDE: | |
1461 | hotkey << wxT("KP_Divide" ); | |
1462 | break; | |
1463 | case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2: | |
1464 | case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5: | |
1465 | case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9: | |
1466 | hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0); | |
1467 | break; | |
1468 | case WXK_WINDOWS_LEFT: | |
1469 | hotkey << wxT("Super_L" ); | |
1470 | break; | |
1471 | case WXK_WINDOWS_RIGHT: | |
1472 | hotkey << wxT("Super_R" ); | |
1473 | break; | |
1474 | case WXK_WINDOWS_MENU: | |
1475 | hotkey << wxT("Menu" ); | |
1476 | break; | |
1477 | case WXK_COMMAND: | |
1478 | hotkey << wxT("Command" ); | |
1479 | break; | |
1480 | /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt | |
1481 | case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4: | |
1482 | case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8: | |
1483 | case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12: | |
1484 | case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16: | |
1485 | case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20: | |
1486 | hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1); | |
1487 | break; | |
1488 | */ | |
1489 | // if there are any other keys wxAcceleratorEntry::Create() may | |
1490 | // return, we should process them here | |
1491 | ||
1492 | default: | |
1493 | if ( code < 127 ) | |
1494 | { | |
1495 | const wxString | |
1496 | name = wxGTK_CONV_BACK_SYS(gdk_keyval_name((guint)code)); | |
1497 | if ( !name.empty() ) | |
1498 | { | |
1499 | hotkey << name; | |
1500 | break; | |
1501 | } | |
1502 | } | |
1503 | ||
1504 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
1505 | } | |
1506 | ||
1507 | delete accel; | |
1508 | } | |
1509 | ||
1510 | return hotkey; | |
1511 | } | |
1512 | ||
1513 | #endif // wxUSE_ACCEL | |
1514 | ||
1515 | const char *wxGetStockGtkID(wxWindowID id) | |
1516 | { | |
1517 | #define STOCKITEM(wx,gtk) \ | |
1518 | case wx: \ | |
1519 | return gtk; | |
1520 | ||
1521 | #define STOCKITEM_MISSING(wx) \ | |
1522 | case wx: \ | |
1523 | return NULL; | |
1524 | ||
1525 | #if GTK_CHECK_VERSION(2,4,0) | |
1526 | #define STOCKITEM_24(wx,gtk) STOCKITEM(wx,gtk) | |
1527 | #else | |
1528 | #define STOCKITEM_24(wx,gtk) STOCKITEM_MISSING(wx) | |
1529 | #endif | |
1530 | ||
1531 | #if GTK_CHECK_VERSION(2,6,0) | |
1532 | #define STOCKITEM_26(wx,gtk) STOCKITEM(wx,gtk) | |
1533 | #else | |
1534 | #define STOCKITEM_26(wx,gtk) STOCKITEM_MISSING(wx) | |
1535 | #endif | |
1536 | ||
1537 | #if GTK_CHECK_VERSION(2,10,0) | |
1538 | #define STOCKITEM_210(wx,gtk) STOCKITEM(wx,gtk) | |
1539 | #else | |
1540 | #define STOCKITEM_210(wx,gtk) STOCKITEM_MISSING(wx) | |
1541 | #endif | |
1542 | ||
1543 | ||
1544 | switch (id) | |
1545 | { | |
1546 | STOCKITEM_26(wxID_ABOUT, GTK_STOCK_ABOUT) | |
1547 | STOCKITEM(wxID_ADD, GTK_STOCK_ADD) | |
1548 | STOCKITEM(wxID_APPLY, GTK_STOCK_APPLY) | |
1549 | STOCKITEM(wxID_BOLD, GTK_STOCK_BOLD) | |
1550 | STOCKITEM(wxID_CANCEL, GTK_STOCK_CANCEL) | |
1551 | STOCKITEM(wxID_CLEAR, GTK_STOCK_CLEAR) | |
1552 | STOCKITEM(wxID_CLOSE, GTK_STOCK_CLOSE) | |
1553 | STOCKITEM(wxID_COPY, GTK_STOCK_COPY) | |
1554 | STOCKITEM(wxID_CUT, GTK_STOCK_CUT) | |
1555 | STOCKITEM(wxID_DELETE, GTK_STOCK_DELETE) | |
1556 | STOCKITEM_26(wxID_EDIT, GTK_STOCK_EDIT) | |
1557 | STOCKITEM(wxID_FIND, GTK_STOCK_FIND) | |
1558 | STOCKITEM_26(wxID_FILE, GTK_STOCK_FILE) | |
1559 | STOCKITEM(wxID_REPLACE, GTK_STOCK_FIND_AND_REPLACE) | |
1560 | STOCKITEM(wxID_BACKWARD, GTK_STOCK_GO_BACK) | |
1561 | STOCKITEM(wxID_DOWN, GTK_STOCK_GO_DOWN) | |
1562 | STOCKITEM(wxID_FORWARD, GTK_STOCK_GO_FORWARD) | |
1563 | STOCKITEM(wxID_UP, GTK_STOCK_GO_UP) | |
1564 | STOCKITEM(wxID_HELP, GTK_STOCK_HELP) | |
1565 | STOCKITEM(wxID_HOME, GTK_STOCK_HOME) | |
1566 | STOCKITEM_24(wxID_INDENT, GTK_STOCK_INDENT) | |
1567 | STOCKITEM(wxID_INDEX, GTK_STOCK_INDEX) | |
1568 | STOCKITEM(wxID_ITALIC, GTK_STOCK_ITALIC) | |
1569 | STOCKITEM(wxID_JUSTIFY_CENTER, GTK_STOCK_JUSTIFY_CENTER) | |
1570 | STOCKITEM(wxID_JUSTIFY_FILL, GTK_STOCK_JUSTIFY_FILL) | |
1571 | STOCKITEM(wxID_JUSTIFY_LEFT, GTK_STOCK_JUSTIFY_LEFT) | |
1572 | STOCKITEM(wxID_JUSTIFY_RIGHT, GTK_STOCK_JUSTIFY_RIGHT) | |
1573 | STOCKITEM(wxID_NEW, GTK_STOCK_NEW) | |
1574 | STOCKITEM(wxID_NO, GTK_STOCK_NO) | |
1575 | STOCKITEM(wxID_OK, GTK_STOCK_OK) | |
1576 | STOCKITEM(wxID_OPEN, GTK_STOCK_OPEN) | |
1577 | STOCKITEM(wxID_PASTE, GTK_STOCK_PASTE) | |
1578 | STOCKITEM(wxID_PREFERENCES, GTK_STOCK_PREFERENCES) | |
1579 | STOCKITEM(wxID_PRINT, GTK_STOCK_PRINT) | |
1580 | STOCKITEM(wxID_PREVIEW, GTK_STOCK_PRINT_PREVIEW) | |
1581 | STOCKITEM(wxID_PROPERTIES, GTK_STOCK_PROPERTIES) | |
1582 | STOCKITEM(wxID_EXIT, GTK_STOCK_QUIT) | |
1583 | STOCKITEM(wxID_REDO, GTK_STOCK_REDO) | |
1584 | STOCKITEM(wxID_REFRESH, GTK_STOCK_REFRESH) | |
1585 | STOCKITEM(wxID_REMOVE, GTK_STOCK_REMOVE) | |
1586 | STOCKITEM(wxID_REVERT_TO_SAVED, GTK_STOCK_REVERT_TO_SAVED) | |
1587 | STOCKITEM(wxID_SAVE, GTK_STOCK_SAVE) | |
1588 | STOCKITEM(wxID_SAVEAS, GTK_STOCK_SAVE_AS) | |
1589 | STOCKITEM_210(wxID_SELECTALL, GTK_STOCK_SELECT_ALL) | |
1590 | STOCKITEM(wxID_STOP, GTK_STOCK_STOP) | |
1591 | STOCKITEM(wxID_UNDELETE, GTK_STOCK_UNDELETE) | |
1592 | STOCKITEM(wxID_UNDERLINE, GTK_STOCK_UNDERLINE) | |
1593 | STOCKITEM(wxID_UNDO, GTK_STOCK_UNDO) | |
1594 | STOCKITEM_24(wxID_UNINDENT, GTK_STOCK_UNINDENT) | |
1595 | STOCKITEM(wxID_YES, GTK_STOCK_YES) | |
1596 | STOCKITEM(wxID_ZOOM_100, GTK_STOCK_ZOOM_100) | |
1597 | STOCKITEM(wxID_ZOOM_FIT, GTK_STOCK_ZOOM_FIT) | |
1598 | STOCKITEM(wxID_ZOOM_IN, GTK_STOCK_ZOOM_IN) | |
1599 | STOCKITEM(wxID_ZOOM_OUT, GTK_STOCK_ZOOM_OUT) | |
1600 | ||
1601 | default: | |
1602 | wxFAIL_MSG( _T("invalid stock item ID") ); | |
1603 | break; | |
1604 | }; | |
1605 | ||
1606 | #undef STOCKITEM | |
1607 | ||
1608 | return NULL; | |
1609 | } | |
1610 | ||
1611 | #if wxUSE_ACCEL | |
1612 | static | |
1613 | bool wxGetStockGtkAccelerator(const char *id, GdkModifierType *mod, guint *key) | |
1614 | { | |
1615 | if (!id) | |
1616 | return false; | |
1617 | ||
1618 | GtkStockItem stock_item; | |
1619 | if (gtk_stock_lookup (id, &stock_item)) | |
1620 | { | |
1621 | if (key) *key = stock_item.keyval; | |
1622 | if (mod) *mod = stock_item.modifier; | |
1623 | ||
1624 | // some GTK stock items have zero values for the keyval; | |
1625 | // it means that they do not have an accelerator... | |
1626 | if (stock_item.keyval) | |
1627 | return true; | |
1628 | } | |
1629 | ||
1630 | return false; | |
1631 | } | |
1632 | #endif // wxUSE_ACCEL | |
1633 | ||
1634 | #endif // wxUSE_MENUS |