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