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