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