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