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