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