]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: | |
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 | #include "wx/menu.h" | |
14 | #include "wx/log.h" | |
15 | #include "wx/intl.h" | |
16 | #include "wx/app.h" | |
17 | #include "wx/bitmap.h" | |
18 | ||
19 | #if wxUSE_ACCEL | |
20 | #include "wx/accel.h" | |
21 | #endif // wxUSE_ACCEL | |
22 | ||
23 | #include "wx/gtk/private.h" | |
24 | ||
25 | #include <gdk/gdkkeysyms.h> | |
26 | ||
27 | // FIXME: is this right? somehow I don't think so (VZ) | |
28 | #ifdef __WXGTK20__ | |
29 | #include <glib-object.h> | |
30 | ||
31 | #define gtk_accel_group_attach(g, o) gtk_window_add_accel_group((o), (g)) | |
32 | #define gtk_accel_group_detach(g, o) gtk_window_remove_accel_group((o), (g)) | |
33 | #define gtk_menu_ensure_uline_accel_group(m) gtk_menu_get_accel_group(m) | |
34 | ||
35 | #define ACCEL_OBJECT GtkWindow | |
36 | #define ACCEL_OBJECTS(a) (a)->acceleratables | |
37 | #define ACCEL_OBJ_CAST(obj) ((GtkWindow*) obj) | |
38 | #else // GTK+ 1.x | |
39 | #define ACCEL_OBJECT GtkObject | |
40 | #define ACCEL_OBJECTS(a) (a)->attach_objects | |
41 | #define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj) | |
42 | #endif | |
43 | ||
44 | // we use normal item but with a special id for the menu title | |
45 | static const int wxGTK_TITLE_ID = -3; | |
46 | ||
47 | //----------------------------------------------------------------------------- | |
48 | // idle system | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | extern void wxapp_install_idle_handler(); | |
52 | extern bool g_isIdle; | |
53 | ||
54 | #if wxUSE_ACCEL | |
55 | static wxString GetGtkHotKey( const wxMenuItem& item ); | |
56 | #endif | |
57 | ||
58 | //----------------------------------------------------------------------------- | |
59 | // idle system | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
62 | static wxString wxReplaceUnderscore( const wxString& title ) | |
63 | { | |
64 | const wxChar *pc; | |
65 | ||
66 | // GTK 1.2 wants to have "_" instead of "&" for accelerators | |
67 | wxString str; | |
68 | pc = title; | |
69 | while (*pc != wxT('\0')) | |
70 | { | |
71 | if ((*pc == wxT('&')) && (*(pc+1) == wxT('&'))) | |
72 | { | |
73 | // "&" is doubled to indicate "&" instead of accelerator | |
74 | ++pc; | |
75 | str << wxT('&'); | |
76 | } | |
77 | else if (*pc == wxT('&')) | |
78 | { | |
79 | str << wxT('_'); | |
80 | } | |
81 | else | |
82 | { | |
83 | if ( *pc == wxT('_') ) | |
84 | { | |
85 | // underscores must be doubled to prevent them from being | |
86 | // interpreted as accelerator character prefix by GTK | |
87 | str << *pc; | |
88 | } | |
89 | ||
90 | str << *pc; | |
91 | } | |
92 | ++pc; | |
93 | } | |
94 | ||
95 | // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() ); | |
96 | ||
97 | return str; | |
98 | } | |
99 | ||
100 | //----------------------------------------------------------------------------- | |
101 | // activate message from GTK | |
102 | //----------------------------------------------------------------------------- | |
103 | ||
104 | extern "C" { | |
105 | static void gtk_menu_open_callback( GtkWidget *widget, wxMenu *menu ) | |
106 | { | |
107 | if (g_isIdle) wxapp_install_idle_handler(); | |
108 | ||
109 | wxMenuEvent event( wxEVT_MENU_OPEN, -1, menu ); | |
110 | event.SetEventObject( menu ); | |
111 | ||
112 | wxEvtHandler* handler = menu->GetEventHandler(); | |
113 | if (handler && handler->ProcessEvent(event)) | |
114 | return; | |
115 | ||
116 | wxWindow *win = menu->GetInvokingWindow(); | |
117 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
118 | } | |
119 | } | |
120 | ||
121 | //----------------------------------------------------------------------------- | |
122 | // wxMenuBar | |
123 | //----------------------------------------------------------------------------- | |
124 | ||
125 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
126 | ||
127 | void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style) | |
128 | { | |
129 | // the parent window is known after wxFrame::SetMenu() | |
130 | m_needParent = FALSE; | |
131 | m_style = style; | |
132 | m_invokingWindow = (wxWindow*) NULL; | |
133 | ||
134 | if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) || | |
135 | !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") )) | |
136 | { | |
137 | wxFAIL_MSG( wxT("wxMenuBar creation failed") ); | |
138 | return; | |
139 | } | |
140 | ||
141 | m_menubar = gtk_menu_bar_new(); | |
142 | #ifndef __WXGTK20__ | |
143 | m_accel = gtk_accel_group_new(); | |
144 | #endif | |
145 | ||
146 | if (style & wxMB_DOCKABLE) | |
147 | { | |
148 | m_widget = gtk_handle_box_new(); | |
149 | gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) ); | |
150 | gtk_widget_show( GTK_WIDGET(m_menubar) ); | |
151 | } | |
152 | else | |
153 | { | |
154 | m_widget = GTK_WIDGET(m_menubar); | |
155 | } | |
156 | ||
157 | PostCreation(); | |
158 | ||
159 | ApplyWidgetStyle(); | |
160 | ||
161 | for (size_t i = 0; i < n; ++i ) | |
162 | Append(menus[i], titles[i]); | |
163 | } | |
164 | ||
165 | wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style) | |
166 | { | |
167 | Init(n, menus, titles, style); | |
168 | } | |
169 | ||
170 | wxMenuBar::wxMenuBar(long style) | |
171 | { | |
172 | Init(0, NULL, NULL, style); | |
173 | } | |
174 | ||
175 | wxMenuBar::wxMenuBar() | |
176 | { | |
177 | Init(0, NULL, NULL, 0); | |
178 | } | |
179 | ||
180 | wxMenuBar::~wxMenuBar() | |
181 | { | |
182 | } | |
183 | ||
184 | static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
185 | { | |
186 | menu->SetInvokingWindow( (wxWindow*) NULL ); | |
187 | ||
188 | wxWindow *top_frame = win; | |
189 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
190 | top_frame = top_frame->GetParent(); | |
191 | ||
192 | #ifndef __WXGTK20__ | |
193 | // support for native hot keys | |
194 | gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); | |
195 | #endif | |
196 | ||
197 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
198 | while (node) | |
199 | { | |
200 | wxMenuItem *menuitem = node->GetData(); | |
201 | if (menuitem->IsSubMenu()) | |
202 | wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win ); | |
203 | node = node->GetNext(); | |
204 | } | |
205 | } | |
206 | ||
207 | static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win ) | |
208 | { | |
209 | menu->SetInvokingWindow( win ); | |
210 | ||
211 | wxWindow *top_frame = win; | |
212 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
213 | top_frame = top_frame->GetParent(); | |
214 | ||
215 | // support for native hot keys | |
216 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); | |
217 | if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) ) | |
218 | gtk_accel_group_attach( menu->m_accel, obj ); | |
219 | ||
220 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
221 | while (node) | |
222 | { | |
223 | wxMenuItem *menuitem = node->GetData(); | |
224 | if (menuitem->IsSubMenu()) | |
225 | wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win ); | |
226 | node = node->GetNext(); | |
227 | } | |
228 | } | |
229 | ||
230 | void wxMenuBar::SetInvokingWindow( wxWindow *win ) | |
231 | { | |
232 | m_invokingWindow = win; | |
233 | wxWindow *top_frame = win; | |
234 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
235 | top_frame = top_frame->GetParent(); | |
236 | ||
237 | #ifndef __WXGTK20__ | |
238 | // support for native key accelerators indicated by underscroes | |
239 | ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget); | |
240 | if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) ) | |
241 | gtk_accel_group_attach( m_accel, obj ); | |
242 | #endif | |
243 | ||
244 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
245 | while (node) | |
246 | { | |
247 | wxMenu *menu = node->GetData(); | |
248 | wxMenubarSetInvokingWindow( menu, win ); | |
249 | node = node->GetNext(); | |
250 | } | |
251 | } | |
252 | ||
253 | void wxMenuBar::UnsetInvokingWindow( wxWindow *win ) | |
254 | { | |
255 | m_invokingWindow = (wxWindow*) NULL; | |
256 | wxWindow *top_frame = win; | |
257 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) | |
258 | top_frame = top_frame->GetParent(); | |
259 | ||
260 | #ifndef __WXGTK20__ | |
261 | // support for native key accelerators indicated by underscroes | |
262 | gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) ); | |
263 | #endif | |
264 | ||
265 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
266 | while (node) | |
267 | { | |
268 | wxMenu *menu = node->GetData(); | |
269 | wxMenubarUnsetInvokingWindow( menu, win ); | |
270 | node = node->GetNext(); | |
271 | } | |
272 | } | |
273 | ||
274 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
275 | { | |
276 | if ( !wxMenuBarBase::Append( menu, title ) ) | |
277 | return FALSE; | |
278 | ||
279 | return GtkAppend(menu, title); | |
280 | } | |
281 | ||
282 | bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos) | |
283 | { | |
284 | wxString str( wxReplaceUnderscore( title ) ); | |
285 | ||
286 | // This doesn't have much effect right now. | |
287 | menu->SetTitle( str ); | |
288 | ||
289 | // The "m_owner" is the "menu item" | |
290 | #ifdef __WXGTK20__ | |
291 | menu->m_owner = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( str ) ); | |
292 | #else | |
293 | menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) ); | |
294 | GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); | |
295 | // set new text | |
296 | gtk_label_set_text( label, wxGTK_CONV( str ) ); | |
297 | // reparse key accel | |
298 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) ); | |
299 | if (accel_key != GDK_VoidSymbol) | |
300 | { | |
301 | gtk_widget_add_accelerator (menu->m_owner, | |
302 | "activate_item", | |
303 | m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)), | |
304 | accel_key, | |
305 | GDK_MOD1_MASK, | |
306 | GTK_ACCEL_LOCKED); | |
307 | } | |
308 | #endif | |
309 | ||
310 | gtk_widget_show( menu->m_owner ); | |
311 | ||
312 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu ); | |
313 | ||
314 | if (pos == -1) | |
315 | gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner ); | |
316 | else | |
317 | gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos ); | |
318 | ||
319 | gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate", | |
320 | GTK_SIGNAL_FUNC(gtk_menu_open_callback), | |
321 | (gpointer)menu ); | |
322 | ||
323 | // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables | |
324 | // addings menu later on. | |
325 | if (m_invokingWindow) | |
326 | { | |
327 | wxMenubarSetInvokingWindow( menu, m_invokingWindow ); | |
328 | ||
329 | // OPTIMISE ME: we should probably cache this, or pass it | |
330 | // directly, but for now this is a minimal | |
331 | // change to validate the new dynamic sizing. | |
332 | // see (and refactor :) similar code in Remove | |
333 | // below. | |
334 | ||
335 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); | |
336 | ||
337 | if( frame ) | |
338 | frame->UpdateMenuBarSize(); | |
339 | } | |
340 | ||
341 | return TRUE; | |
342 | } | |
343 | ||
344 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
345 | { | |
346 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
347 | return FALSE; | |
348 | ||
349 | // TODO | |
350 | ||
351 | if ( !GtkAppend(menu, title, (int)pos) ) | |
352 | return FALSE; | |
353 | ||
354 | return TRUE; | |
355 | } | |
356 | ||
357 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
358 | { | |
359 | // remove the old item and insert a new one | |
360 | wxMenu *menuOld = Remove(pos); | |
361 | if ( menuOld && !Insert(pos, menu, title) ) | |
362 | { | |
363 | return (wxMenu*) NULL; | |
364 | } | |
365 | ||
366 | // either Insert() succeeded or Remove() failed and menuOld is NULL | |
367 | return menuOld; | |
368 | } | |
369 | ||
370 | wxMenu *wxMenuBar::Remove(size_t pos) | |
371 | { | |
372 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
373 | if ( !menu ) | |
374 | return (wxMenu*) NULL; | |
375 | ||
376 | gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) ); | |
377 | gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner); | |
378 | ||
379 | gtk_widget_destroy( menu->m_owner ); | |
380 | menu->m_owner = NULL; | |
381 | ||
382 | if (m_invokingWindow) | |
383 | { | |
384 | // OPTIMISE ME: see comment in GtkAppend | |
385 | wxFrame *frame = wxDynamicCast( m_invokingWindow, wxFrame ); | |
386 | ||
387 | if( frame ) | |
388 | frame->UpdateMenuBarSize(); | |
389 | } | |
390 | ||
391 | return menu; | |
392 | } | |
393 | ||
394 | static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString ) | |
395 | { | |
396 | if (wxMenuItem::GetLabelFromText(menu->GetTitle()) == wxMenuItem::GetLabelFromText(menuString)) | |
397 | { | |
398 | int res = menu->FindItem( itemString ); | |
399 | if (res != wxNOT_FOUND) | |
400 | return res; | |
401 | } | |
402 | ||
403 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
404 | while (node) | |
405 | { | |
406 | wxMenuItem *item = node->GetData(); | |
407 | if (item->IsSubMenu()) | |
408 | return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString); | |
409 | ||
410 | node = node->GetNext(); | |
411 | } | |
412 | ||
413 | return wxNOT_FOUND; | |
414 | } | |
415 | ||
416 | int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const | |
417 | { | |
418 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
419 | while (node) | |
420 | { | |
421 | wxMenu *menu = node->GetData(); | |
422 | int res = FindMenuItemRecursive( menu, menuString, itemString); | |
423 | if (res != -1) | |
424 | return res; | |
425 | node = node->GetNext(); | |
426 | } | |
427 | ||
428 | return wxNOT_FOUND; | |
429 | } | |
430 | ||
431 | // Find a wxMenuItem using its id. Recurses down into sub-menus | |
432 | static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id) | |
433 | { | |
434 | wxMenuItem* result = menu->FindChildItem(id); | |
435 | ||
436 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst(); | |
437 | while ( node && result == NULL ) | |
438 | { | |
439 | wxMenuItem *item = node->GetData(); | |
440 | if (item->IsSubMenu()) | |
441 | { | |
442 | result = FindMenuItemByIdRecursive( item->GetSubMenu(), id ); | |
443 | } | |
444 | node = node->GetNext(); | |
445 | } | |
446 | ||
447 | return result; | |
448 | } | |
449 | ||
450 | wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const | |
451 | { | |
452 | wxMenuItem* result = 0; | |
453 | wxMenuList::compatibility_iterator node = m_menus.GetFirst(); | |
454 | while (node && result == 0) | |
455 | { | |
456 | wxMenu *menu = node->GetData(); | |
457 | result = FindMenuItemByIdRecursive( menu, id ); | |
458 | node = node->GetNext(); | |
459 | } | |
460 | ||
461 | if ( menuForItem ) | |
462 | { | |
463 | *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL; | |
464 | } | |
465 | ||
466 | return result; | |
467 | } | |
468 | ||
469 | void wxMenuBar::EnableTop( size_t pos, bool flag ) | |
470 | { | |
471 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
472 | ||
473 | wxCHECK_RET( node, wxT("menu not found") ); | |
474 | ||
475 | wxMenu* menu = node->GetData(); | |
476 | ||
477 | if (menu->m_owner) | |
478 | gtk_widget_set_sensitive( menu->m_owner, flag ); | |
479 | } | |
480 | ||
481 | wxString wxMenuBar::GetLabelTop( size_t pos ) const | |
482 | { | |
483 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
484 | ||
485 | wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") ); | |
486 | ||
487 | wxMenu* menu = node->GetData(); | |
488 | ||
489 | wxString label; | |
490 | wxString text( menu->GetTitle() ); | |
491 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) | |
492 | { | |
493 | if ( *pc == wxT('_') ) | |
494 | { | |
495 | // '_' is the escape character for GTK+ | |
496 | continue; | |
497 | } | |
498 | ||
499 | // don't remove ampersands '&' since if we have them in the menu title | |
500 | // it means that they were doubled to indicate "&" instead of accelerator | |
501 | ||
502 | label += *pc; | |
503 | } | |
504 | ||
505 | return label; | |
506 | } | |
507 | ||
508 | void wxMenuBar::SetLabelTop( size_t pos, const wxString& label ) | |
509 | { | |
510 | wxMenuList::compatibility_iterator node = m_menus.Item( pos ); | |
511 | ||
512 | wxCHECK_RET( node, wxT("menu not found") ); | |
513 | ||
514 | wxMenu* menu = node->GetData(); | |
515 | ||
516 | const wxString str( wxReplaceUnderscore( label ) ); | |
517 | ||
518 | menu->SetTitle( str ); | |
519 | ||
520 | if (menu->m_owner) | |
521 | { | |
522 | GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child ); | |
523 | ||
524 | /* set new text */ | |
525 | gtk_label_set( glabel, wxGTK_CONV( str ) ); | |
526 | ||
527 | /* reparse key accel */ | |
528 | (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) ); | |
529 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) ); | |
530 | } | |
531 | ||
532 | } | |
533 | ||
534 | //----------------------------------------------------------------------------- | |
535 | // "activate" | |
536 | //----------------------------------------------------------------------------- | |
537 | ||
538 | extern "C" { | |
539 | static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu ) | |
540 | { | |
541 | if (g_isIdle) | |
542 | wxapp_install_idle_handler(); | |
543 | ||
544 | int id = menu->FindMenuIdByMenuItem(widget); | |
545 | ||
546 | /* should find it for normal (not popup) menu */ | |
547 | wxASSERT_MSG( (id != -1) || (menu->GetInvokingWindow() != NULL), | |
548 | _T("menu item not found in gtk_menu_clicked_callback") ); | |
549 | ||
550 | if (!menu->IsEnabled(id)) | |
551 | return; | |
552 | ||
553 | wxMenuItem* item = menu->FindChildItem( id ); | |
554 | wxCHECK_RET( item, wxT("error in menu item callback") ); | |
555 | ||
556 | if ( item->GetId() == wxGTK_TITLE_ID ) | |
557 | { | |
558 | // ignore events from the menu title | |
559 | return; | |
560 | } | |
561 | ||
562 | if (item->IsCheckable()) | |
563 | { | |
564 | bool isReallyChecked = item->IsChecked(), | |
565 | isInternallyChecked = item->wxMenuItemBase::IsChecked(); | |
566 | ||
567 | // ensure that the internal state is always consistent with what is | |
568 | // shown on the screen | |
569 | item->wxMenuItemBase::Check(isReallyChecked); | |
570 | ||
571 | // we must not report the events for the radio button going up nor the | |
572 | // events resulting from the calls to wxMenuItem::Check() | |
573 | if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) || | |
574 | (isInternallyChecked == isReallyChecked) ) | |
575 | { | |
576 | return; | |
577 | } | |
578 | } | |
579 | ||
580 | ||
581 | // Is this menu on a menubar? (possibly nested) | |
582 | wxFrame* frame = NULL; | |
583 | if(menu->IsAttached()) | |
584 | frame = menu->GetMenuBar()->GetFrame(); | |
585 | ||
586 | // FIXME: why do we have to call wxFrame::GetEventHandler() directly here? | |
587 | // normally wxMenu::SendEvent() should be enough, if it doesn't work | |
588 | // in wxGTK then we have a bug in wxMenu::GetInvokingWindow() which | |
589 | // should be fixed instead of working around it here... | |
590 | if (frame) | |
591 | { | |
592 | // If it is attached then let the frame send the event. | |
593 | // Don't call frame->ProcessCommand(id) because it toggles | |
594 | // checkable items and we've already done that above. | |
595 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); | |
596 | commandEvent.SetEventObject(frame); | |
597 | if (item->IsCheckable()) | |
598 | commandEvent.SetInt(item->IsChecked()); | |
599 | commandEvent.SetEventObject(menu); | |
600 | ||
601 | frame->GetEventHandler()->ProcessEvent(commandEvent); | |
602 | } | |
603 | else | |
604 | { | |
605 | // otherwise let the menu have it | |
606 | menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1); | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
611 | //----------------------------------------------------------------------------- | |
612 | // "select" | |
613 | //----------------------------------------------------------------------------- | |
614 | ||
615 | extern "C" { | |
616 | static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu ) | |
617 | { | |
618 | if (g_isIdle) wxapp_install_idle_handler(); | |
619 | ||
620 | int id = menu->FindMenuIdByMenuItem(widget); | |
621 | ||
622 | wxASSERT( id != -1 ); // should find it! | |
623 | ||
624 | if (!menu->IsEnabled(id)) | |
625 | return; | |
626 | ||
627 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id ); | |
628 | event.SetEventObject( menu ); | |
629 | ||
630 | wxEvtHandler* handler = menu->GetEventHandler(); | |
631 | if (handler && handler->ProcessEvent(event)) | |
632 | return; | |
633 | ||
634 | wxWindow *win = menu->GetInvokingWindow(); | |
635 | if (win) win->GetEventHandler()->ProcessEvent( event ); | |
636 | } | |
637 | } | |
638 | ||
639 | //----------------------------------------------------------------------------- | |
640 | // "deselect" | |
641 | //----------------------------------------------------------------------------- | |
642 | ||
643 | extern "C" { | |
644 | static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu ) | |
645 | { | |
646 | if (g_isIdle) wxapp_install_idle_handler(); | |
647 | ||
648 | int id = menu->FindMenuIdByMenuItem(widget); | |
649 | ||
650 | wxASSERT( id != -1 ); // should find it! | |
651 | ||
652 | if (!menu->IsEnabled(id)) | |
653 | return; | |
654 | ||
655 | wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 ); | |
656 | event.SetEventObject( menu ); | |
657 | ||
658 | wxEvtHandler* handler = menu->GetEventHandler(); | |
659 | if (handler && handler->ProcessEvent(event)) | |
660 | return; | |
661 | ||
662 | wxWindow *win = menu->GetInvokingWindow(); | |
663 | if (win) | |
664 | win->GetEventHandler()->ProcessEvent( event ); | |
665 | } | |
666 | } | |
667 | ||
668 | //----------------------------------------------------------------------------- | |
669 | // wxMenuItem | |
670 | //----------------------------------------------------------------------------- | |
671 | ||
672 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
673 | ||
674 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
675 | int id, | |
676 | const wxString& name, | |
677 | const wxString& help, | |
678 | wxItemKind kind, | |
679 | wxMenu *subMenu) | |
680 | { | |
681 | return new wxMenuItem(parentMenu, id, name, help, kind, subMenu); | |
682 | } | |
683 | ||
684 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
685 | int id, | |
686 | const wxString& text, | |
687 | const wxString& help, | |
688 | wxItemKind kind, | |
689 | wxMenu *subMenu) | |
690 | : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu) | |
691 | { | |
692 | Init(text); | |
693 | } | |
694 | ||
695 | wxMenuItem::wxMenuItem(wxMenu *parentMenu, | |
696 | int id, | |
697 | const wxString& text, | |
698 | const wxString& help, | |
699 | bool isCheckable, | |
700 | wxMenu *subMenu) | |
701 | : wxMenuItemBase(parentMenu, id, text, help, | |
702 | isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu) | |
703 | { | |
704 | Init(text); | |
705 | } | |
706 | ||
707 | void wxMenuItem::Init(const wxString& text) | |
708 | { | |
709 | m_labelWidget = (GtkWidget *) NULL; | |
710 | m_menuItem = (GtkWidget *) NULL; | |
711 | ||
712 | DoSetText(text); | |
713 | } | |
714 | ||
715 | wxMenuItem::~wxMenuItem() | |
716 | { | |
717 | // don't delete menu items, the menus take care of that | |
718 | } | |
719 | ||
720 | // return the menu item text without any menu accels | |
721 | /* static */ | |
722 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
723 | { | |
724 | wxString label; | |
725 | ||
726 | for ( const wxChar *pc = text.c_str(); *pc; pc++ ) | |
727 | { | |
728 | if ( *pc == wxT('\t')) | |
729 | break; | |
730 | ||
731 | if ( *pc == wxT('_') ) | |
732 | { | |
733 | // GTK 1.2 escapes "xxx_xxx" to "xxx__xxx" | |
734 | pc++; | |
735 | label += *pc; | |
736 | continue; | |
737 | } | |
738 | ||
739 | #ifdef __WXGTK20__ | |
740 | if ( *pc == wxT('\\') ) | |
741 | { | |
742 | // GTK 2.0 escapes "xxx/xxx" to "xxx\/xxx" | |
743 | pc++; | |
744 | label += *pc; | |
745 | continue; | |
746 | } | |
747 | #endif | |
748 | ||
749 | if ( (*pc == wxT('&')) && (*(pc+1) != wxT('&')) ) | |
750 | { | |
751 | // wxMSW escapes "&" | |
752 | // "&" is doubled to indicate "&" instead of accelerator | |
753 | continue; | |
754 | } | |
755 | ||
756 | label += *pc; | |
757 | } | |
758 | ||
759 | // wxPrintf( wxT("GetLabelFromText(): text %s label %s\n"), text.c_str(), label.c_str() ); | |
760 | ||
761 | return label; | |
762 | } | |
763 | ||
764 | void wxMenuItem::SetText( const wxString& str ) | |
765 | { | |
766 | // Some optimization to avoid flicker | |
767 | wxString oldLabel = m_text; | |
768 | oldLabel = wxStripMenuCodes(oldLabel); | |
769 | oldLabel.Replace(wxT("_"), wxT("")); | |
770 | wxString label1 = wxStripMenuCodes(str); | |
771 | wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format | |
772 | wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo | |
773 | ||
774 | DoSetText(str); | |
775 | ||
776 | if (oldLabel == label1 && | |
777 | oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered | |
778 | return; | |
779 | ||
780 | if (m_menuItem) | |
781 | { | |
782 | GtkLabel *label; | |
783 | if (m_labelWidget) | |
784 | label = (GtkLabel*) m_labelWidget; | |
785 | else | |
786 | label = GTK_LABEL( GTK_BIN(m_menuItem)->child ); | |
787 | ||
788 | #ifdef __WXGTK20__ | |
789 | gtk_label_set_text_with_mnemonic( GTK_LABEL(label), wxGTK_CONV(m_text) ); | |
790 | #else | |
791 | // set new text | |
792 | gtk_label_set( label, wxGTK_CONV( m_text ) ); | |
793 | ||
794 | // reparse key accel | |
795 | (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) ); | |
796 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) ); | |
797 | #endif | |
798 | } | |
799 | ||
800 | guint accel_key; | |
801 | GdkModifierType accel_mods; | |
802 | gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods); | |
803 | if (accel_key != 0) | |
804 | { | |
805 | gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem), | |
806 | m_parentMenu->m_accel, | |
807 | accel_key, | |
808 | accel_mods ); | |
809 | } | |
810 | ||
811 | wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) ); | |
812 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); | |
813 | if (accel_key != 0) | |
814 | { | |
815 | gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem), | |
816 | "activate", | |
817 | m_parentMenu->m_accel, | |
818 | accel_key, | |
819 | accel_mods, | |
820 | GTK_ACCEL_VISIBLE); | |
821 | } | |
822 | } | |
823 | ||
824 | // it's valid for this function to be called even if m_menuItem == NULL | |
825 | void wxMenuItem::DoSetText( const wxString& str ) | |
826 | { | |
827 | // '\t' is the deliminator indicating a hot key | |
828 | m_text.Empty(); | |
829 | const wxChar *pc = str; | |
830 | while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) ) | |
831 | { | |
832 | if ((*pc == wxT('&')) && (*(pc+1) == wxT('&'))) | |
833 | { | |
834 | // "&" is doubled to indicate "&" instead of accelerator | |
835 | ++pc; | |
836 | m_text << wxT('&'); | |
837 | } | |
838 | else if (*pc == wxT('&')) | |
839 | { | |
840 | m_text << wxT('_'); | |
841 | } | |
842 | else if ( *pc == wxT('_') ) // escape underscores | |
843 | { | |
844 | m_text << wxT("__"); | |
845 | } | |
846 | else | |
847 | { | |
848 | m_text << *pc; | |
849 | } | |
850 | ++pc; | |
851 | } | |
852 | ||
853 | m_hotKey = wxT(""); | |
854 | ||
855 | if(*pc == wxT('\t')) | |
856 | { | |
857 | pc++; | |
858 | m_hotKey = pc; | |
859 | } | |
860 | ||
861 | // wxPrintf( wxT("DoSetText(): str %s m_text %s hotkey %s\n"), str.c_str(), m_text.c_str(), m_hotKey.c_str() ); | |
862 | } | |
863 | ||
864 | #if wxUSE_ACCEL | |
865 | ||
866 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
867 | { | |
868 | if ( !GetHotKey() ) | |
869 | { | |
870 | // nothing | |
871 | return (wxAcceleratorEntry *)NULL; | |
872 | } | |
873 | ||
874 | // as wxGetAccelFromString() looks for TAB, insert a dummy one here | |
875 | wxString label; | |
876 | label << wxT('\t') << GetHotKey(); | |
877 | ||
878 | return wxGetAccelFromString(label); | |
879 | } | |
880 | ||
881 | #endif // wxUSE_ACCEL | |
882 | ||
883 | void wxMenuItem::Check( bool check ) | |
884 | { | |
885 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); | |
886 | ||
887 | if (check == m_isChecked) | |
888 | return; | |
889 | ||
890 | wxMenuItemBase::Check( check ); | |
891 | ||
892 | switch ( GetKind() ) | |
893 | { | |
894 | case wxITEM_CHECK: | |
895 | case wxITEM_RADIO: | |
896 | gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check ); | |
897 | break; | |
898 | ||
899 | default: | |
900 | wxFAIL_MSG( _T("can't check this item") ); | |
901 | } | |
902 | } | |
903 | ||
904 | void wxMenuItem::Enable( bool enable ) | |
905 | { | |
906 | wxCHECK_RET( m_menuItem, wxT("invalid menu item") ); | |
907 | ||
908 | gtk_widget_set_sensitive( m_menuItem, enable ); | |
909 | wxMenuItemBase::Enable( enable ); | |
910 | } | |
911 | ||
912 | bool wxMenuItem::IsChecked() const | |
913 | { | |
914 | wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") ); | |
915 | ||
916 | wxCHECK_MSG( IsCheckable(), FALSE, | |
917 | wxT("can't get state of uncheckable item!") ); | |
918 | ||
919 | return ((GtkCheckMenuItem*)m_menuItem)->active != 0; | |
920 | } | |
921 | ||
922 | //----------------------------------------------------------------------------- | |
923 | // wxMenu | |
924 | //----------------------------------------------------------------------------- | |
925 | ||
926 | IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) | |
927 | ||
928 | void wxMenu::Init() | |
929 | { | |
930 | m_accel = gtk_accel_group_new(); | |
931 | m_menu = gtk_menu_new(); | |
932 | // NB: keep reference to the menu so that it is not destroyed behind | |
933 | // our back by GTK+ e.g. when it is removed from menubar: | |
934 | gtk_widget_ref(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_append(GTK_MENU(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 | ||
958 | wxMenu::~wxMenu() | |
959 | { | |
960 | WX_CLEAR_LIST(wxMenuItemList, m_items); | |
961 | ||
962 | if ( GTK_IS_WIDGET( m_menu )) | |
963 | { | |
964 | // see wxMenu::Init | |
965 | gtk_widget_unref( m_menu ); | |
966 | // if the menu is inserted in another menu at this time, there was | |
967 | // one more reference to it: | |
968 | if ( m_owner ) | |
969 | gtk_widget_destroy( m_menu ); | |
970 | } | |
971 | } | |
972 | ||
973 | bool wxMenu::GtkAppend(wxMenuItem *mitem, int pos) | |
974 | { | |
975 | GtkWidget *menuItem; | |
976 | ||
977 | wxString text; | |
978 | #ifndef __WXGTK20__ | |
979 | GtkLabel* label; | |
980 | #endif | |
981 | ||
982 | if ( mitem->IsSeparator() ) | |
983 | { | |
984 | #ifdef __WXGTK20__ | |
985 | menuItem = gtk_separator_menu_item_new(); | |
986 | #else | |
987 | // TODO | |
988 | menuItem = gtk_menu_item_new(); | |
989 | #endif | |
990 | } | |
991 | else if (mitem->GetBitmap().Ok()) | |
992 | { | |
993 | text = mitem->GetText(); | |
994 | const wxBitmap *bitmap = &mitem->GetBitmap(); | |
995 | ||
996 | #ifdef __WXGTK20__ | |
997 | menuItem = gtk_image_menu_item_new_with_mnemonic( wxGTK_CONV( text ) ); | |
998 | ||
999 | GtkWidget *image; | |
1000 | if (bitmap->HasPixbuf()) | |
1001 | { | |
1002 | image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf()); | |
1003 | } | |
1004 | else | |
1005 | { | |
1006 | GdkPixmap *gdk_pixmap = bitmap->GetPixmap(); | |
1007 | GdkBitmap *gdk_bitmap = bitmap->GetMask() ? | |
1008 | bitmap->GetMask()->GetBitmap() : | |
1009 | (GdkBitmap*) NULL; | |
1010 | image = gtk_image_new_from_pixmap( gdk_pixmap, gdk_bitmap ); | |
1011 | } | |
1012 | ||
1013 | gtk_widget_show(image); | |
1014 | ||
1015 | gtk_image_menu_item_set_image( GTK_IMAGE_MENU_ITEM(menuItem), image ); | |
1016 | ||
1017 | #else | |
1018 | // TODO | |
1019 | wxUnusedVar(bitmap); | |
1020 | menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) ); | |
1021 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); | |
1022 | #endif | |
1023 | ||
1024 | m_prevRadio = NULL; | |
1025 | } | |
1026 | else // a normal item | |
1027 | { | |
1028 | // text has "_" instead of "&" after mitem->SetText() so don't use it | |
1029 | text = mitem->GetText() ; | |
1030 | ||
1031 | switch ( mitem->GetKind() ) | |
1032 | { | |
1033 | case wxITEM_CHECK: | |
1034 | { | |
1035 | #ifdef __WXGTK20__ | |
1036 | menuItem = gtk_check_menu_item_new_with_mnemonic( wxGTK_CONV( text ) ); | |
1037 | #else | |
1038 | menuItem = gtk_check_menu_item_new_with_label( wxGTK_CONV( text ) ); | |
1039 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); | |
1040 | // set new text | |
1041 | gtk_label_set_text( label, wxGTK_CONV( text ) ); | |
1042 | #endif | |
1043 | m_prevRadio = NULL; | |
1044 | break; | |
1045 | } | |
1046 | ||
1047 | case wxITEM_RADIO: | |
1048 | { | |
1049 | GSList *group = NULL; | |
1050 | if ( m_prevRadio == NULL ) | |
1051 | { | |
1052 | // start of a new radio group | |
1053 | #ifdef __WXGTK20__ | |
1054 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) ); | |
1055 | #else | |
1056 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) ); | |
1057 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); | |
1058 | // set new text | |
1059 | gtk_label_set_text( label, wxGTK_CONV( text ) ); | |
1060 | #endif | |
1061 | } | |
1062 | else // continue the radio group | |
1063 | { | |
1064 | #ifdef __WXGTK20__ | |
1065 | group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (m_prevRadio)); | |
1066 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_mnemonic( group, wxGTK_CONV( text ) ); | |
1067 | #else | |
1068 | group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (m_prevRadio)); | |
1069 | m_prevRadio = menuItem = gtk_radio_menu_item_new_with_label( group, wxGTK_CONV( text ) ); | |
1070 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); | |
1071 | #endif | |
1072 | } | |
1073 | break; | |
1074 | } | |
1075 | ||
1076 | default: | |
1077 | wxFAIL_MSG( _T("unexpected menu item kind") ); | |
1078 | // fall through | |
1079 | ||
1080 | case wxITEM_NORMAL: | |
1081 | { | |
1082 | #ifdef __WXGTK20__ | |
1083 | menuItem = gtk_menu_item_new_with_mnemonic( wxGTK_CONV( text ) ); | |
1084 | #else | |
1085 | menuItem = gtk_menu_item_new_with_label( wxGTK_CONV( text ) ); | |
1086 | label = GTK_LABEL( GTK_BIN(menuItem)->child ); | |
1087 | #endif | |
1088 | m_prevRadio = NULL; | |
1089 | break; | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | } | |
1094 | ||
1095 | guint accel_key; | |
1096 | GdkModifierType accel_mods; | |
1097 | wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*mitem) ); | |
1098 | ||
1099 | // wxPrintf( wxT("item: %s hotkey %s\n"), mitem->GetText().c_str(), GetGtkHotKey(*mitem).c_str() ); | |
1100 | gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods); | |
1101 | if (accel_key != 0) | |
1102 | { | |
1103 | gtk_widget_add_accelerator (GTK_WIDGET(menuItem), | |
1104 | "activate", | |
1105 | m_accel, | |
1106 | accel_key, | |
1107 | accel_mods, | |
1108 | GTK_ACCEL_VISIBLE); | |
1109 | } | |
1110 | ||
1111 | if (pos == -1) | |
1112 | gtk_menu_shell_append(GTK_MENU_SHELL(m_menu), menuItem); | |
1113 | else | |
1114 | gtk_menu_shell_insert(GTK_MENU_SHELL(m_menu), menuItem, pos); | |
1115 | ||
1116 | gtk_widget_show( menuItem ); | |
1117 | ||
1118 | if ( !mitem->IsSeparator() ) | |
1119 | { | |
1120 | wxASSERT_MSG( menuItem, wxT("invalid menuitem") ); | |
1121 | ||
1122 | gtk_signal_connect( GTK_OBJECT(menuItem), "select", | |
1123 | GTK_SIGNAL_FUNC(gtk_menu_hilight_callback), | |
1124 | (gpointer)this ); | |
1125 | ||
1126 | gtk_signal_connect( GTK_OBJECT(menuItem), "deselect", | |
1127 | GTK_SIGNAL_FUNC(gtk_menu_nolight_callback), | |
1128 | (gpointer)this ); | |
1129 | ||
1130 | if ( mitem->IsSubMenu() && mitem->GetKind() != wxITEM_RADIO && mitem->GetKind() != wxITEM_CHECK ) | |
1131 | { | |
1132 | gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu ); | |
1133 | ||
1134 | gtk_widget_show( mitem->GetSubMenu()->m_menu ); | |
1135 | ||
1136 | // if adding a submenu to a menu already existing in the menu bar, we | |
1137 | // must set invoking window to allow processing events from this | |
1138 | // submenu | |
1139 | if ( m_invokingWindow ) | |
1140 | wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow); | |
1141 | } | |
1142 | else | |
1143 | { | |
1144 | gtk_signal_connect( GTK_OBJECT(menuItem), "activate", | |
1145 | GTK_SIGNAL_FUNC(gtk_menu_clicked_callback), | |
1146 | (gpointer)this ); | |
1147 | } | |
1148 | ||
1149 | #ifndef __WXGTK20__ | |
1150 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( text ) ); | |
1151 | if (accel_key != GDK_VoidSymbol) | |
1152 | { | |
1153 | gtk_widget_add_accelerator (menuItem, | |
1154 | "activate_item", | |
1155 | gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menu)), | |
1156 | accel_key, | |
1157 | GDK_MOD1_MASK, | |
1158 | GTK_ACCEL_LOCKED); | |
1159 | } | |
1160 | #endif | |
1161 | } | |
1162 | ||
1163 | mitem->SetMenuItem(menuItem); | |
1164 | ||
1165 | if (ms_locked) | |
1166 | { | |
1167 | // This doesn't even exist! | |
1168 | // gtk_widget_lock_accelerators(mitem->GetMenuItem()); | |
1169 | } | |
1170 | ||
1171 | return TRUE; | |
1172 | } | |
1173 | ||
1174 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *mitem) | |
1175 | { | |
1176 | if (!GtkAppend(mitem)) | |
1177 | return NULL; | |
1178 | ||
1179 | return wxMenuBase::DoAppend(mitem); | |
1180 | } | |
1181 | ||
1182 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
1183 | { | |
1184 | if ( !wxMenuBase::DoInsert(pos, item) ) | |
1185 | return NULL; | |
1186 | ||
1187 | // TODO | |
1188 | if ( !GtkAppend(item, (int)pos) ) | |
1189 | return NULL; | |
1190 | ||
1191 | return item; | |
1192 | } | |
1193 | ||
1194 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
1195 | { | |
1196 | if ( !wxMenuBase::DoRemove(item) ) | |
1197 | return (wxMenuItem *)NULL; | |
1198 | ||
1199 | // TODO: this code doesn't delete the item factory item and this seems | |
1200 | // impossible as of GTK 1.2.6. | |
1201 | gtk_widget_destroy( item->GetMenuItem() ); | |
1202 | ||
1203 | return item; | |
1204 | } | |
1205 | ||
1206 | int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const | |
1207 | { | |
1208 | wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); | |
1209 | while (node) | |
1210 | { | |
1211 | wxMenuItem *item = node->GetData(); | |
1212 | if (item->GetMenuItem() == menuItem) | |
1213 | return item->GetId(); | |
1214 | node = node->GetNext(); | |
1215 | } | |
1216 | ||
1217 | return wxNOT_FOUND; | |
1218 | } | |
1219 | ||
1220 | // ---------------------------------------------------------------------------- | |
1221 | // helpers | |
1222 | // ---------------------------------------------------------------------------- | |
1223 | ||
1224 | #if wxUSE_ACCEL | |
1225 | ||
1226 | static wxString GetGtkHotKey( const wxMenuItem& item ) | |
1227 | { | |
1228 | wxString hotkey; | |
1229 | ||
1230 | wxAcceleratorEntry *accel = item.GetAccel(); | |
1231 | if ( accel ) | |
1232 | { | |
1233 | int flags = accel->GetFlags(); | |
1234 | if ( flags & wxACCEL_ALT ) | |
1235 | hotkey += wxT("<alt>"); | |
1236 | if ( flags & wxACCEL_CTRL ) | |
1237 | hotkey += wxT("<control>"); | |
1238 | if ( flags & wxACCEL_SHIFT ) | |
1239 | hotkey += wxT("<shift>"); | |
1240 | ||
1241 | int code = accel->GetKeyCode(); | |
1242 | switch ( code ) | |
1243 | { | |
1244 | case WXK_F1: | |
1245 | case WXK_F2: | |
1246 | case WXK_F3: | |
1247 | case WXK_F4: | |
1248 | case WXK_F5: | |
1249 | case WXK_F6: | |
1250 | case WXK_F7: | |
1251 | case WXK_F8: | |
1252 | case WXK_F9: | |
1253 | case WXK_F10: | |
1254 | case WXK_F11: | |
1255 | case WXK_F12: | |
1256 | case WXK_F13: | |
1257 | case WXK_F14: | |
1258 | case WXK_F15: | |
1259 | case WXK_F16: | |
1260 | case WXK_F17: | |
1261 | case WXK_F18: | |
1262 | case WXK_F19: | |
1263 | case WXK_F20: | |
1264 | case WXK_F21: | |
1265 | case WXK_F22: | |
1266 | case WXK_F23: | |
1267 | case WXK_F24: | |
1268 | hotkey += wxString::Format(wxT("F%d"), code - WXK_F1 + 1); | |
1269 | break; | |
1270 | ||
1271 | // TODO: we should use gdk_keyval_name() (a.k.a. | |
1272 | // XKeysymToString) here as well as hardcoding the keysym | |
1273 | // names this might be not portable | |
1274 | case WXK_INSERT: | |
1275 | hotkey << wxT("Insert" ); | |
1276 | break; | |
1277 | case WXK_DELETE: | |
1278 | hotkey << wxT("Delete" ); | |
1279 | break; | |
1280 | case WXK_UP: | |
1281 | hotkey << wxT("Up" ); | |
1282 | break; | |
1283 | case WXK_DOWN: | |
1284 | hotkey << wxT("Down" ); | |
1285 | break; | |
1286 | case WXK_PAGEUP: | |
1287 | case WXK_PRIOR: | |
1288 | hotkey << wxT("Prior" ); | |
1289 | break; | |
1290 | case WXK_PAGEDOWN: | |
1291 | case WXK_NEXT: | |
1292 | hotkey << wxT("Next" ); | |
1293 | break; | |
1294 | case WXK_LEFT: | |
1295 | hotkey << wxT("Left" ); | |
1296 | break; | |
1297 | case WXK_RIGHT: | |
1298 | hotkey << wxT("Right" ); | |
1299 | break; | |
1300 | case WXK_HOME: | |
1301 | hotkey << wxT("Home" ); | |
1302 | break; | |
1303 | case WXK_END: | |
1304 | hotkey << wxT("End" ); | |
1305 | break; | |
1306 | case WXK_RETURN: | |
1307 | hotkey << wxT("Return" ); | |
1308 | break; | |
1309 | case WXK_BACK: | |
1310 | hotkey << wxT("BackSpace" ); | |
1311 | break; | |
1312 | case WXK_TAB: | |
1313 | hotkey << wxT("Tab" ); | |
1314 | break; | |
1315 | case WXK_ESCAPE: | |
1316 | hotkey << wxT("Esc" ); | |
1317 | break; | |
1318 | case WXK_SPACE: | |
1319 | hotkey << wxT("space" ); | |
1320 | break; | |
1321 | case WXK_MULTIPLY: | |
1322 | hotkey << wxT("Multiply" ); | |
1323 | break; | |
1324 | case WXK_ADD: | |
1325 | hotkey << wxT("Add" ); | |
1326 | break; | |
1327 | case WXK_SEPARATOR: | |
1328 | hotkey << wxT("Separator" ); | |
1329 | break; | |
1330 | case WXK_SUBTRACT: | |
1331 | hotkey << wxT("Subtract" ); | |
1332 | break; | |
1333 | case WXK_DECIMAL: | |
1334 | hotkey << wxT("Decimal" ); | |
1335 | break; | |
1336 | case WXK_DIVIDE: | |
1337 | hotkey << wxT("Divide" ); | |
1338 | break; | |
1339 | case WXK_CANCEL: | |
1340 | hotkey << wxT("Cancel" ); | |
1341 | break; | |
1342 | case WXK_CLEAR: | |
1343 | hotkey << wxT("Clear" ); | |
1344 | break; | |
1345 | case WXK_MENU: | |
1346 | hotkey << wxT("Menu" ); | |
1347 | break; | |
1348 | case WXK_PAUSE: | |
1349 | hotkey << wxT("Pause" ); | |
1350 | break; | |
1351 | case WXK_CAPITAL: | |
1352 | hotkey << wxT("Capital" ); | |
1353 | break; | |
1354 | case WXK_SELECT: | |
1355 | hotkey << wxT("Select" ); | |
1356 | break; | |
1357 | case WXK_PRINT: | |
1358 | hotkey << wxT("Print" ); | |
1359 | break; | |
1360 | case WXK_EXECUTE: | |
1361 | hotkey << wxT("Execute" ); | |
1362 | break; | |
1363 | case WXK_SNAPSHOT: | |
1364 | hotkey << wxT("Snapshot" ); | |
1365 | break; | |
1366 | case WXK_HELP: | |
1367 | hotkey << wxT("Help" ); | |
1368 | break; | |
1369 | case WXK_NUMLOCK: | |
1370 | hotkey << wxT("Num_Lock" ); | |
1371 | break; | |
1372 | case WXK_SCROLL: | |
1373 | hotkey << wxT("Scroll_Lock" ); | |
1374 | break; | |
1375 | case WXK_NUMPAD_INSERT: | |
1376 | hotkey << wxT("KP_Insert" ); | |
1377 | break; | |
1378 | case WXK_NUMPAD_DELETE: | |
1379 | hotkey << wxT("KP_Delete" ); | |
1380 | break; | |
1381 | case WXK_NUMPAD_SPACE: | |
1382 | hotkey << wxT("KP_Space" ); | |
1383 | break; | |
1384 | case WXK_NUMPAD_TAB: | |
1385 | hotkey << wxT("KP_Tab" ); | |
1386 | break; | |
1387 | case WXK_NUMPAD_ENTER: | |
1388 | hotkey << wxT("KP_Enter" ); | |
1389 | break; | |
1390 | case WXK_NUMPAD_F1: case WXK_NUMPAD_F2: case WXK_NUMPAD_F3: | |
1391 | case WXK_NUMPAD_F4: | |
1392 | hotkey += wxString::Format(wxT("KP_F%d"), code - WXK_NUMPAD_F1 + 1); | |
1393 | break; | |
1394 | case WXK_NUMPAD_HOME: | |
1395 | hotkey << wxT("KP_Home" ); | |
1396 | break; | |
1397 | case WXK_NUMPAD_LEFT: | |
1398 | hotkey << wxT("KP_Left" ); | |
1399 | break; | |
1400 | case WXK_NUMPAD_UP: | |
1401 | hotkey << wxT("KP_Up" ); | |
1402 | break; | |
1403 | case WXK_NUMPAD_RIGHT: | |
1404 | hotkey << wxT("KP_Right" ); | |
1405 | break; | |
1406 | case WXK_NUMPAD_DOWN: | |
1407 | hotkey << wxT("KP_Down" ); | |
1408 | break; | |
1409 | case WXK_NUMPAD_PRIOR: case WXK_NUMPAD_PAGEUP: | |
1410 | hotkey << wxT("KP_Prior" ); | |
1411 | break; | |
1412 | case WXK_NUMPAD_NEXT: case WXK_NUMPAD_PAGEDOWN: | |
1413 | hotkey << wxT("KP_Next" ); | |
1414 | break; | |
1415 | case WXK_NUMPAD_END: | |
1416 | hotkey << wxT("KP_End" ); | |
1417 | break; | |
1418 | case WXK_NUMPAD_BEGIN: | |
1419 | hotkey << wxT("KP_Begin" ); | |
1420 | break; | |
1421 | case WXK_NUMPAD_EQUAL: | |
1422 | hotkey << wxT("KP_Equal" ); | |
1423 | break; | |
1424 | case WXK_NUMPAD_MULTIPLY: | |
1425 | hotkey << wxT("KP_Multiply" ); | |
1426 | break; | |
1427 | case WXK_NUMPAD_ADD: | |
1428 | hotkey << wxT("KP_Add" ); | |
1429 | break; | |
1430 | case WXK_NUMPAD_SEPARATOR: | |
1431 | hotkey << wxT("KP_Separator" ); | |
1432 | break; | |
1433 | case WXK_NUMPAD_SUBTRACT: | |
1434 | hotkey << wxT("KP_Subtract" ); | |
1435 | break; | |
1436 | case WXK_NUMPAD_DECIMAL: | |
1437 | hotkey << wxT("KP_Decimal" ); | |
1438 | break; | |
1439 | case WXK_NUMPAD_DIVIDE: | |
1440 | hotkey << wxT("KP_Divide" ); | |
1441 | break; | |
1442 | case WXK_NUMPAD0: case WXK_NUMPAD1: case WXK_NUMPAD2: | |
1443 | case WXK_NUMPAD3: case WXK_NUMPAD4: case WXK_NUMPAD5: | |
1444 | case WXK_NUMPAD6: case WXK_NUMPAD7: case WXK_NUMPAD8: case WXK_NUMPAD9: | |
1445 | hotkey += wxString::Format(wxT("KP_%d"), code - WXK_NUMPAD0); | |
1446 | break; | |
1447 | case WXK_WINDOWS_LEFT: | |
1448 | hotkey << wxT("Super_L" ); | |
1449 | break; | |
1450 | case WXK_WINDOWS_RIGHT: | |
1451 | hotkey << wxT("Super_R" ); | |
1452 | break; | |
1453 | case WXK_WINDOWS_MENU: | |
1454 | hotkey << wxT("Menu" ); | |
1455 | break; | |
1456 | case WXK_COMMAND: | |
1457 | hotkey << wxT("Command" ); | |
1458 | break; | |
1459 | /* These probably wouldn't work as there is no SpecialX in gdk/keynames.txt | |
1460 | case WXK_SPECIAL1: case WXK_SPECIAL2: case WXK_SPECIAL3: case WXK_SPECIAL4: | |
1461 | case WXK_SPECIAL5: case WXK_SPECIAL6: case WXK_SPECIAL7: case WXK_SPECIAL8: | |
1462 | case WXK_SPECIAL9: case WXK_SPECIAL10: case WXK_SPECIAL11: case WXK_SPECIAL12: | |
1463 | case WXK_SPECIAL13: case WXK_SPECIAL14: case WXK_SPECIAL15: case WXK_SPECIAL16: | |
1464 | case WXK_SPECIAL17: case WXK_SPECIAL18: case WXK_SPECIAL19: case WXK_SPECIAL20: | |
1465 | hotkey += wxString::Format(wxT("Special%d"), code - WXK_SPECIAL1 + 1); | |
1466 | break; | |
1467 | */ | |
1468 | // if there are any other keys wxGetAccelFromString() may | |
1469 | // return, we should process them here | |
1470 | ||
1471 | default: | |
1472 | if ( code < 127 ) | |
1473 | { | |
1474 | wxString name = wxGTK_CONV_BACK( gdk_keyval_name((guint)code) ); | |
1475 | if ( name ) | |
1476 | { | |
1477 | hotkey << name; | |
1478 | break; | |
1479 | } | |
1480 | } | |
1481 | ||
1482 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
1483 | } | |
1484 | ||
1485 | delete accel; | |
1486 | } | |
1487 | ||
1488 | return hotkey; | |
1489 | } | |
1490 | ||
1491 | #endif // wxUSE_ACCEL |