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