]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/menu.cpp
Added XRC handler for wxTimePickerCtrl.
[wxWidgets.git] / src / gtk1 / menu.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
44014bcd 2// Name: src/gtk1/menu.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
96fd301f 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
e1bf3ad3 13#include "wx/menu.h"
ee0a94cf 14#include "wx/stockitem.h"
88a7a4e1
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
e4db172a 18 #include "wx/log.h"
670f9935 19 #include "wx/app.h"
0bca0373 20 #include "wx/bitmap.h"
88a7a4e1
WS
21#endif
22
974e8d94
VZ
23#if wxUSE_ACCEL
24 #include "wx/accel.h"
25#endif // wxUSE_ACCEL
26
3cbab641 27#include "wx/gtk1/private.h"
93591996 28#include "wx/gtk1/private/mnemonics.h"
9e691f46 29
cc47eed9 30#include <gdk/gdkkeysyms.h>
9e691f46 31
3cbab641
MR
32#define ACCEL_OBJECT GtkObject
33#define ACCEL_OBJECTS(a) (a)->attach_objects
34#define ACCEL_OBJ_CAST(obj) GTK_OBJECT(obj)
83624f79 35
9959e2b6
VZ
36// we use normal item but with a special id for the menu title
37static const int wxGTK_TITLE_ID = -3;
38
defdd888 39// defined in window.cpp
3cbab641 40extern guint32 wxGtkTimeLastClick;
defdd888 41
acfd422a
RR
42//-----------------------------------------------------------------------------
43// idle system
44//-----------------------------------------------------------------------------
45
46extern void wxapp_install_idle_handler();
47extern bool g_isIdle;
48
6d971354 49#if wxUSE_ACCEL
98f29783 50static wxString GetGtkHotKey( const wxMenuItem& item );
717a57c2
VZ
51#endif
52
f6bcfd97
BP
53//-----------------------------------------------------------------------------
54// idle system
55//-----------------------------------------------------------------------------
56
57static wxString wxReplaceUnderscore( const wxString& title )
58{
59 const wxChar *pc;
2368dcda 60
6d971354 61 // GTK 1.2 wants to have "_" instead of "&" for accelerators
f6bcfd97 62 wxString str;
2b5f62a0
VZ
63 pc = title;
64 while (*pc != wxT('\0'))
f6bcfd97 65 {
2b5f62a0
VZ
66 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
67 {
68 // "&" is doubled to indicate "&" instead of accelerator
69 ++pc;
70 str << wxT('&');
71 }
72 else if (*pc == wxT('&'))
f6bcfd97 73 {
f6bcfd97 74 str << wxT('_');
4e9cbd33 75 }
f6bcfd97
BP
76 else
77 {
f6bcfd97
BP
78 if ( *pc == wxT('_') )
79 {
80 // underscores must be doubled to prevent them from being
81 // interpreted as accelerator character prefix by GTK
82 str << *pc;
83 }
f6bcfd97
BP
84
85 str << *pc;
86 }
2b5f62a0 87 ++pc;
f6bcfd97 88 }
9959e2b6 89
6d971354 90 // wxPrintf( wxT("before %s after %s\n"), title.c_str(), str.c_str() );
9959e2b6 91
f6bcfd97
BP
92 return str;
93}
94
52af3158
JS
95static wxString wxConvertFromGTKToWXLabel(const wxString& gtkLabel)
96{
97 wxString label;
98 for ( const wxChar *pc = gtkLabel.c_str(); *pc; pc++ )
99 {
100 // '_' is the escape character for GTK+.
101
102 if ( *pc == wxT('_') && *(pc+1) == wxT('_'))
103 {
104 // An underscore was escaped.
105 label += wxT('_');
106 pc++;
107 }
108 else if ( *pc == wxT('_') )
109 {
110 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
111 label += wxT('&');
112 }
113 else if ( *pc == wxT('&') )
114 {
115 // Double the ampersand to escape it as far as wxWidgets is concerned
116 label += wxT("&&");
117 }
118 else
119 {
120 // don't remove ampersands '&' since if we have them in the menu title
121 // it means that they were doubled to indicate "&" instead of accelerator
122 label += *pc;
123 }
124 }
125
126 return label;
127}
128
129
e6396ed4
JS
130//-----------------------------------------------------------------------------
131// activate message from GTK
132//-----------------------------------------------------------------------------
133
cdf003d4 134static void DoCommonMenuCallbackCode(wxMenu *menu, wxMenuEvent& event)
e6396ed4 135{
cdf003d4
VZ
136 if (g_isIdle)
137 wxapp_install_idle_handler();
e6396ed4 138
e6396ed4
JS
139 event.SetEventObject( menu );
140
a01fe3d6
RD
141 wxEvtHandler* handler = menu->GetEventHandler();
142 if (handler && handler->ProcessEvent(event))
e6396ed4
JS
143 return;
144
61f09f56 145 wxWindow *win = menu->GetWindow();
cdf003d4 146 if (win)
937013e0 147 win->HandleWindowEvent( event );
cdf003d4
VZ
148}
149
150extern "C" {
151
89954433 152static void gtk_menu_open_callback( GtkWidget *WXUNUSED(widget), wxMenu *menu )
cdf003d4
VZ
153{
154 wxMenuEvent event(wxEVT_MENU_OPEN, -1, menu);
155
156 DoCommonMenuCallbackCode(menu, event);
157}
158
89954433 159static void gtk_menu_close_callback( GtkWidget *WXUNUSED(widget), wxMenuBar *menubar )
cdf003d4 160{
13d35112
VZ
161 if ( !menubar->GetMenuCount() )
162 {
163 // if menubar is empty we can't call GetMenu(0) below
164 return;
165 }
cdf003d4 166
13d35112
VZ
167 wxMenuEvent event( wxEVT_MENU_CLOSE, -1, NULL );
168
169 DoCommonMenuCallbackCode(menubar->GetMenu(0), event);
e6396ed4 170}
cdf003d4 171
865bb325 172}
e6396ed4 173
c801d85f
KB
174//-----------------------------------------------------------------------------
175// wxMenuBar
176//-----------------------------------------------------------------------------
177
294ea16d 178void wxMenuBar::Init(size_t n, wxMenu *menus[], const wxString titles[], long style)
3502e687 179{
6d971354 180 // the parent window is known after wxFrame::SetMenu()
44014bcd 181 m_needParent = false;
ae53c98c 182 m_style = style;
23280650 183
d3b9f782
VZ
184 if (!PreCreation( NULL, wxDefaultPosition, wxDefaultSize ) ||
185 !CreateBase( NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 186 {
223d09f6 187 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 188 return;
4dcaf11a 189 }
3502e687 190
3502e687 191 m_menubar = gtk_menu_bar_new();
6d971354 192 m_accel = gtk_accel_group_new();
3502e687
RR
193
194 if (style & wxMB_DOCKABLE)
195 {
196 m_widget = gtk_handle_box_new();
c626a8b7
VZ
197 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
198 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
199 }
200 else
201 {
202 m_widget = GTK_WIDGET(m_menubar);
203 }
204
205 PostCreation();
c4608a8a 206
db434467 207 ApplyWidgetStyle();
294ea16d
VZ
208
209 for (size_t i = 0; i < n; ++i )
210 Append(menus[i], titles[i]);
13d35112
VZ
211
212 // VZ: for some reason connecting to menus "deactivate" doesn't work (we
213 // don't get it when the menu is dismissed by clicking outside the
214 // toolbar) so we connect to the global one, even if it means that we
215 // can't pass the menu which was closed in wxMenuEvent object
216 gtk_signal_connect( GTK_OBJECT(GTK_MENU_SHELL(m_menubar)),
217 "deactivate",
218 GTK_SIGNAL_FUNC(gtk_menu_close_callback),
219 (gpointer)this );
220
3502e687
RR
221}
222
294ea16d 223wxMenuBar::wxMenuBar(size_t n, wxMenu *menus[], const wxString titles[], long style)
c801d85f 224{
294ea16d
VZ
225 Init(n, menus, titles, style);
226}
96fd301f 227
294ea16d
VZ
228wxMenuBar::wxMenuBar(long style)
229{
230 Init(0, NULL, NULL, style);
231}
c4608a8a 232
294ea16d
VZ
233wxMenuBar::wxMenuBar()
234{
235 Init(0, NULL, NULL, 0);
6de97a3b 236}
c801d85f 237
1e133b7d
RR
238wxMenuBar::~wxMenuBar()
239{
1e133b7d
RR
240}
241
61f09f56 242static void DetachFromFrame( wxMenu *menu, wxWindow *win )
5bd9e519 243{
5bd9e519 244 wxWindow *top_frame = win;
8487f887 245 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 246 top_frame = top_frame->GetParent();
5bd9e519 247
6d971354 248 // support for native hot keys
9e691f46 249 gtk_accel_group_detach( menu->m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
5bd9e519 250
222ed1d6 251 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
252 while (node)
253 {
1987af7e 254 wxMenuItem *menuitem = node->GetData();
5bd9e519 255 if (menuitem->IsSubMenu())
61f09f56 256 DetachFromFrame( menuitem->GetSubMenu(), win );
1987af7e 257 node = node->GetNext();
5bd9e519
RR
258 }
259}
260
61f09f56 261static void AttachToFrame( wxMenu *menu, wxWindow *win )
5bd9e519 262{
5bd9e519 263 wxWindow *top_frame = win;
8487f887 264 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 265 top_frame = top_frame->GetParent();
5bd9e519 266
6d971354 267 // support for native hot keys
9e691f46
VZ
268 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
269 if ( !g_slist_find( ACCEL_OBJECTS(menu->m_accel), obj ) )
b4da05a6 270 gtk_accel_group_attach( menu->m_accel, obj );
5bd9e519 271
222ed1d6 272 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
273 while (node)
274 {
1987af7e 275 wxMenuItem *menuitem = node->GetData();
5bd9e519 276 if (menuitem->IsSubMenu())
61f09f56 277 AttachToFrame( menuitem->GetSubMenu(), win );
1987af7e 278 node = node->GetNext();
5bd9e519
RR
279 }
280}
281
61f09f56 282void wxMenuBar::Attach( wxFrame *win )
5bd9e519 283{
61f09f56
VZ
284 wxMenuBarBase::Attach(win);
285
5bd9e519 286 wxWindow *top_frame = win;
8487f887 287 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 288 top_frame = top_frame->GetParent();
5bd9e519 289
6d971354 290 // support for native key accelerators indicated by underscroes
9e691f46
VZ
291 ACCEL_OBJECT *obj = ACCEL_OBJ_CAST(top_frame->m_widget);
292 if ( !g_slist_find( ACCEL_OBJECTS(m_accel), obj ) )
b4da05a6 293 gtk_accel_group_attach( m_accel, obj );
5bd9e519 294
222ed1d6 295 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
296 while (node)
297 {
1987af7e 298 wxMenu *menu = node->GetData();
61f09f56 299 AttachToFrame( menu, win );
1987af7e 300 node = node->GetNext();
5bd9e519
RR
301 }
302}
303
61f09f56 304void wxMenuBar::Detach()
5bd9e519 305{
61f09f56 306 wxWindow *top_frame = m_menuBarFrame;
8487f887 307 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 308 top_frame = top_frame->GetParent();
9959e2b6 309
d9e403cc 310 // support for native key accelerators indicated by underscroes
9e691f46 311 gtk_accel_group_detach( m_accel, ACCEL_OBJ_CAST(top_frame->m_widget) );
5bd9e519 312
222ed1d6 313 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
5bd9e519
RR
314 while (node)
315 {
1987af7e 316 wxMenu *menu = node->GetData();
61f09f56 317 DetachFromFrame( menu, top_frame );
1987af7e 318 node = node->GetNext();
5bd9e519 319 }
61f09f56
VZ
320
321 wxMenuBarBase::Detach();
5bd9e519
RR
322}
323
3dfac970 324bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
c801d85f 325{
f03ec224 326 if ( !wxMenuBarBase::Append( menu, title ) )
44014bcd 327 return false;
f03ec224
VZ
328
329 return GtkAppend(menu, title);
330}
23280650 331
49826dab 332bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title, int pos)
f03ec224 333{
f6bcfd97 334 wxString str( wxReplaceUnderscore( title ) );
1e133b7d 335
d9e403cc 336 // This doesn't have much effect right now.
1e133b7d 337 menu->SetTitle( str );
23280650 338
6d971354 339 // The "m_owner" is the "menu item"
6d971354
RR
340 menu->m_owner = gtk_menu_item_new_with_label( wxGTK_CONV( str ) );
341 GtkLabel *label = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
342 // set new text
343 gtk_label_set_text( label, wxGTK_CONV( str ) );
344 // reparse key accel
345 guint accel_key = gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV( str ) );
346 if (accel_key != GDK_VoidSymbol)
1e133b7d 347 {
6d971354 348 gtk_widget_add_accelerator (menu->m_owner,
88d19775
MR
349 "activate_item",
350 m_accel, //gtk_menu_ensure_uline_accel_group(GTK_MENU(m_menubar)),
351 accel_key,
352 GDK_MOD1_MASK,
353 GTK_ACCEL_LOCKED);
034be888 354 }
96fd301f 355
2b1c162e 356 gtk_widget_show( menu->m_owner );
9959e2b6 357
2b1c162e 358 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
9959e2b6 359
49826dab
RD
360 if (pos == -1)
361 gtk_menu_shell_append( GTK_MENU_SHELL(m_menubar), menu->m_owner );
362 else
363 gtk_menu_shell_insert( GTK_MENU_SHELL(m_menubar), menu->m_owner, pos );
9959e2b6 364
e6396ed4
JS
365 gtk_signal_connect( GTK_OBJECT(menu->m_owner), "activate",
366 GTK_SIGNAL_FUNC(gtk_menu_open_callback),
367 (gpointer)menu );
368
61f09f56 369 if (m_menuBarFrame)
2b5f62a0 370 {
61f09f56 371 AttachToFrame( menu, m_menuBarFrame );
3dfac970 372
2b5f62a0
VZ
373 // OPTIMISE ME: we should probably cache this, or pass it
374 // directly, but for now this is a minimal
375 // change to validate the new dynamic sizing.
376 // see (and refactor :) similar code in Remove
377 // below.
378
61f09f56 379 m_menuBarFrame->UpdateMenuBarSize();
2b5f62a0
VZ
380 }
381
44014bcd 382 return true;
3dfac970
VZ
383}
384
385bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
386{
387 if ( !wxMenuBarBase::Insert(pos, menu, title) )
44014bcd 388 return false;
3dfac970 389
6d971354 390 // TODO
9959e2b6 391
49826dab 392 if ( !GtkAppend(menu, title, (int)pos) )
44014bcd 393 return false;
f03ec224 394
44014bcd 395 return true;
3dfac970
VZ
396}
397
398wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
399{
f03ec224
VZ
400 // remove the old item and insert a new one
401 wxMenu *menuOld = Remove(pos);
402 if ( menuOld && !Insert(pos, menu, title) )
403 {
d3b9f782 404 return NULL;
f03ec224 405 }
3dfac970 406
f03ec224
VZ
407 // either Insert() succeeded or Remove() failed and menuOld is NULL
408 return menuOld;
3dfac970
VZ
409}
410
411wxMenu *wxMenuBar::Remove(size_t pos)
412{
f03ec224
VZ
413 wxMenu *menu = wxMenuBarBase::Remove(pos);
414 if ( !menu )
d3b9f782 415 return NULL;
f03ec224 416
defc0789
VS
417 gtk_menu_item_remove_submenu( GTK_MENU_ITEM(menu->m_owner) );
418 gtk_container_remove(GTK_CONTAINER(m_menubar), menu->m_owner);
c4608a8a 419
1d62a8b4 420 gtk_widget_destroy( menu->m_owner );
defc0789 421 menu->m_owner = NULL;
c4608a8a 422
61f09f56 423 if (m_menuBarFrame)
2b5f62a0 424 {
6d971354 425 // OPTIMISE ME: see comment in GtkAppend
61f09f56 426 m_menuBarFrame->UpdateMenuBarSize();
2b5f62a0
VZ
427 }
428
1d62a8b4 429 return menu;
6de97a3b 430}
96fd301f 431
716b7364 432static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 433{
7bc0ff86 434 if (wxMenuItem::GetLabelText(menu->GetTitle()) == wxMenuItem::GetLabelText(menuString))
83624f79
RR
435 {
436 int res = menu->FindItem( itemString );
c626a8b7
VZ
437 if (res != wxNOT_FOUND)
438 return res;
83624f79 439 }
c626a8b7 440
222ed1d6 441 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
83624f79
RR
442 while (node)
443 {
1987af7e 444 wxMenuItem *item = node->GetData();
83624f79
RR
445 if (item->IsSubMenu())
446 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 447
1987af7e 448 node = node->GetNext();
83624f79
RR
449 }
450
c626a8b7
VZ
451 return wxNOT_FOUND;
452}
453
c801d85f
KB
454int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
455{
222ed1d6 456 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
457 while (node)
458 {
1987af7e 459 wxMenu *menu = node->GetData();
83624f79 460 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
461 if (res != -1)
462 return res;
463 node = node->GetNext();
83624f79 464 }
1987af7e
VZ
465
466 return wxNOT_FOUND;
6de97a3b 467}
c801d85f 468
c626a8b7 469// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 470static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 471{
717a57c2 472 wxMenuItem* result = menu->FindChildItem(id);
716b7364 473
222ed1d6 474 wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetFirst();
c626a8b7 475 while ( node && result == NULL )
83624f79 476 {
1987af7e 477 wxMenuItem *item = node->GetData();
83624f79 478 if (item->IsSubMenu())
c626a8b7 479 {
83624f79 480 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 481 }
1987af7e 482 node = node->GetNext();
83624f79 483 }
96fd301f 484
83624f79 485 return result;
6de97a3b 486}
716b7364 487
3dfac970 488wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 489{
83624f79 490 wxMenuItem* result = 0;
222ed1d6 491 wxMenuList::compatibility_iterator node = m_menus.GetFirst();
83624f79
RR
492 while (node && result == 0)
493 {
1987af7e 494 wxMenu *menu = node->GetData();
83624f79 495 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 496 node = node->GetNext();
83624f79 497 }
c626a8b7 498
3dfac970
VZ
499 if ( menuForItem )
500 {
d3b9f782 501 *menuForItem = result ? result->GetMenu() : NULL;
3dfac970 502 }
c626a8b7 503
3dfac970 504 return result;
bbe0af5b
RR
505}
506
3dfac970 507void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 508{
222ed1d6 509 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 510
223d09f6 511 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 512
3dfac970 513 wxMenu* menu = node->GetData();
c626a8b7
VZ
514
515 if (menu->m_owner)
516 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
517}
518
52af3158 519wxString wxMenuBar::GetMenuLabel( size_t pos ) const
bbe0af5b 520{
222ed1d6 521 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 522
223d09f6 523 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 524
3dfac970 525 wxMenu* menu = node->GetData();
c626a8b7 526
7bc0ff86 527 return menu->GetTitle();
bbe0af5b
RR
528}
529
52af3158 530void wxMenuBar::SetMenuLabel( size_t pos, const wxString& label )
bbe0af5b 531{
222ed1d6 532 wxMenuList::compatibility_iterator node = m_menus.Item( pos );
c626a8b7 533
223d09f6 534 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 535
3dfac970 536 wxMenu* menu = node->GetData();
c626a8b7 537
4e115ed2 538 const wxString str( wxReplaceUnderscore( label ) );
f6bcfd97
BP
539
540 menu->SetTitle( str );
541
542 if (menu->m_owner)
543 {
4e115ed2 544 GtkLabel *glabel = GTK_LABEL( GTK_BIN(menu->m_owner)->child );
f6bcfd97
BP
545
546 /* set new text */
4e115ed2 547 gtk_label_set( glabel, wxGTK_CONV( str ) );
f6bcfd97
BP
548
549 /* reparse key accel */
4e115ed2
VZ
550 (void)gtk_label_parse_uline (GTK_LABEL(glabel), wxGTK_CONV( str ) );
551 gtk_accel_label_refetch( GTK_ACCEL_LABEL(glabel) );
f6bcfd97
BP
552 }
553
bbe0af5b
RR
554}
555
c801d85f 556//-----------------------------------------------------------------------------
cf7a7e13 557// "activate"
c801d85f
KB
558//-----------------------------------------------------------------------------
559
865bb325 560extern "C" {
6de97a3b 561static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 562{
1e6feb95
VZ
563 if (g_isIdle)
564 wxapp_install_idle_handler();
c4608a8a 565
83624f79 566 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 567
83624f79 568 /* should find it for normal (not popup) menu */
61f09f56 569 wxASSERT_MSG( (id != -1) || (menu->GetWindow() != NULL),
9a83f860 570 wxT("menu item not found in gtk_menu_clicked_callback") );
96fd301f 571
c626a8b7
VZ
572 if (!menu->IsEnabled(id))
573 return;
96fd301f 574
4e6beae5
RD
575 wxMenuItem* item = menu->FindChildItem( id );
576 wxCHECK_RET( item, wxT("error in menu item callback") );
577
9959e2b6
VZ
578 if ( item->GetId() == wxGTK_TITLE_ID )
579 {
580 // ignore events from the menu title
581 return;
582 }
583
4e6beae5
RD
584 if (item->IsCheckable())
585 {
586 bool isReallyChecked = item->IsChecked(),
587 isInternallyChecked = item->wxMenuItemBase::IsChecked();
588
589 // ensure that the internal state is always consistent with what is
590 // shown on the screen
591 item->wxMenuItemBase::Check(isReallyChecked);
592
593 // we must not report the events for the radio button going up nor the
594 // events resulting from the calls to wxMenuItem::Check()
595 if ( (item->GetKind() == wxITEM_RADIO && !isReallyChecked) ||
596 (isInternallyChecked == isReallyChecked) )
597 {
598 return;
599 }
600 }
601
602
02822c8c
RD
603 // Is this menu on a menubar? (possibly nested)
604 wxFrame* frame = NULL;
6edf1107
DE
605 if(menu->IsAttached())
606 frame = menu->GetMenuBar()->GetFrame();
02822c8c 607
da0b5338
VZ
608 // FIXME: why do we have to call wxFrame::GetEventHandler() directly here?
609 // normally wxMenu::SendEvent() should be enough, if it doesn't work
61f09f56 610 // in wxGTK then we have a bug in wxMenu::GetWindow() which
da0b5338 611 // should be fixed instead of working around it here...
02822c8c 612 if (frame)
2d17d68f 613 {
4e6beae5
RD
614 // If it is attached then let the frame send the event.
615 // Don't call frame->ProcessCommand(id) because it toggles
616 // checkable items and we've already done that above.
617 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id);
618 commandEvent.SetEventObject(frame);
619 if (item->IsCheckable())
620 commandEvent.SetInt(item->IsChecked());
da0b5338 621 commandEvent.SetEventObject(menu);
4e6beae5 622
937013e0 623 frame->HandleWindowEvent(commandEvent);
a01fe3d6
RD
624 }
625 else
626 {
4e6beae5 627 // otherwise let the menu have it
a01fe3d6 628 menu->SendEvent(id, item->IsCheckable() ? item->IsChecked() : -1);
2d17d68f 629 }
cf7a7e13 630}
865bb325 631}
cf7a7e13
RR
632
633//-----------------------------------------------------------------------------
634// "select"
635//-----------------------------------------------------------------------------
636
865bb325 637extern "C" {
cf7a7e13
RR
638static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
639{
acfd422a
RR
640 if (g_isIdle) wxapp_install_idle_handler();
641
83624f79
RR
642 int id = menu->FindMenuIdByMenuItem(widget);
643
644 wxASSERT( id != -1 ); // should find it!
cf7a7e13 645
c626a8b7
VZ
646 if (!menu->IsEnabled(id))
647 return;
cf7a7e13 648
342b6a2f 649 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 650 event.SetEventObject( menu );
cf7a7e13 651
a01fe3d6
RD
652 wxEvtHandler* handler = menu->GetEventHandler();
653 if (handler && handler->ProcessEvent(event))
c626a8b7 654 return;
6de97a3b 655
61f09f56 656 wxWindow *win = menu->GetWindow();
937013e0 657 if (win) win->HandleWindowEvent( event );
6de97a3b 658}
865bb325 659}
c801d85f 660
cd743a6f
RR
661//-----------------------------------------------------------------------------
662// "deselect"
663//-----------------------------------------------------------------------------
664
865bb325 665extern "C" {
cd743a6f
RR
666static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
667{
acfd422a
RR
668 if (g_isIdle) wxapp_install_idle_handler();
669
cd743a6f
RR
670 int id = menu->FindMenuIdByMenuItem(widget);
671
672 wxASSERT( id != -1 ); // should find it!
673
c626a8b7
VZ
674 if (!menu->IsEnabled(id))
675 return;
cd743a6f
RR
676
677 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
678 event.SetEventObject( menu );
679
a01fe3d6
RD
680 wxEvtHandler* handler = menu->GetEventHandler();
681 if (handler && handler->ProcessEvent(event))
c626a8b7 682 return;
cd743a6f 683
61f09f56 684 wxWindow *win = menu->GetWindow();
c626a8b7 685 if (win)
937013e0 686 win->HandleWindowEvent( event );
cd743a6f 687}
865bb325 688}
cd743a6f 689
cf7a7e13 690//-----------------------------------------------------------------------------
db1b4961 691// wxMenuItem
cf7a7e13
RR
692//-----------------------------------------------------------------------------
693
974e8d94
VZ
694wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
695 int id,
696 const wxString& name,
697 const wxString& help,
d65c269b 698 wxItemKind kind,
974e8d94
VZ
699 wxMenu *subMenu)
700{
d65c269b 701 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
974e8d94 702}
96fd301f 703
974e8d94
VZ
704wxMenuItem::wxMenuItem(wxMenu *parentMenu,
705 int id,
706 const wxString& text,
707 const wxString& help,
d65c269b 708 wxItemKind kind,
974e8d94 709 wxMenu *subMenu)
d65c269b 710 : wxMenuItemBase(parentMenu, id, text, help, kind, subMenu)
2368dcda 711{
010eb424 712 Init();
2368dcda
VZ
713}
714
715wxMenuItem::wxMenuItem(wxMenu *parentMenu,
716 int id,
717 const wxString& text,
718 const wxString& help,
719 bool isCheckable,
720 wxMenu *subMenu)
721 : wxMenuItemBase(parentMenu, id, text, help,
722 isCheckable ? wxITEM_CHECK : wxITEM_NORMAL, subMenu)
723{
010eb424 724 Init();
2368dcda
VZ
725}
726
010eb424 727void wxMenuItem::Init()
c801d85f 728{
d3b9f782
VZ
729 m_labelWidget = NULL;
730 m_menuItem = NULL;
974e8d94 731
010eb424 732 DoSetText(m_text);
6de97a3b 733}
c801d85f 734
d1b15f03
RR
735wxMenuItem::~wxMenuItem()
736{
737 // don't delete menu items, the menus take care of that
738}
739
52af3158
JS
740wxString wxMenuItem::GetItemLabel() const
741{
a738f87c
JS
742 wxString label = wxConvertFromGTKToWXLabel(m_text);
743 if (!m_hotKey.IsEmpty())
744 label = label + wxT("\t") + m_hotKey;
745 return label;
717a57c2
VZ
746}
747
52af3158 748void wxMenuItem::SetItemLabel( const wxString& string )
717a57c2 749{
ee0a94cf 750 wxString str = string;
345319d6 751 if ( str.empty() && !IsSeparator() )
ee0a94cf 752 {
4fc3eebc 753 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
345319d6
VZ
754 str = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR |
755 wxSTOCK_WITH_MNEMONIC);
ee0a94cf
RR
756 }
757
5869f93f
JS
758 // Some optimization to avoid flicker
759 wxString oldLabel = m_text;
98f29783 760 oldLabel = wxStripMenuCodes(oldLabel);
44014bcd 761 oldLabel.Replace(wxT("_"), wxEmptyString);
98f29783 762 wxString label1 = wxStripMenuCodes(str);
a8e607d9
RR
763 wxString oldhotkey = GetHotKey(); // Store the old hotkey in Ctrl-foo format
764 wxCharBuffer oldbuf = wxGTK_CONV( GetGtkHotKey(*this) ); // and as <control>foo
a01fe3d6 765
717a57c2 766 DoSetText(str);
354aa1e3 767
88d19775 768 if (oldLabel == label1 &&
a8e607d9 769 oldhotkey == GetHotKey()) // Make sure we can change a hotkey even if the label is unaltered
98f29783
RR
770 return;
771
354aa1e3
RR
772 if (m_menuItem)
773 {
37d403aa
JS
774 GtkLabel *label;
775 if (m_labelWidget)
2b5f62a0 776 label = (GtkLabel*) m_labelWidget;
37d403aa 777 else
2b5f62a0 778 label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2 779
2b5f62a0 780 // set new text
fab591c5 781 gtk_label_set( label, wxGTK_CONV( m_text ) );
717a57c2 782
2b5f62a0
VZ
783 // reparse key accel
784 (void)gtk_label_parse_uline (GTK_LABEL(label), wxGTK_CONV(m_text) );
354aa1e3
RR
785 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
786 }
98f29783 787
98f29783
RR
788 guint accel_key;
789 GdkModifierType accel_mods;
98f29783
RR
790 gtk_accelerator_parse( (const char*) oldbuf, &accel_key, &accel_mods);
791 if (accel_key != 0)
792 {
88d19775 793 gtk_widget_remove_accelerator( GTK_WIDGET(m_menuItem),
98f29783
RR
794 m_parentMenu->m_accel,
795 accel_key,
796 accel_mods );
797 }
798
799 wxCharBuffer buf = wxGTK_CONV( GetGtkHotKey(*this) );
800 gtk_accelerator_parse( (const char*) buf, &accel_key, &accel_mods);
801 if (accel_key != 0)
802 {
803 gtk_widget_add_accelerator( GTK_WIDGET(m_menuItem),
804 "activate",
805 m_parentMenu->m_accel,
806 accel_key,
807 accel_mods,
808 GTK_ACCEL_VISIBLE);
809 }
354aa1e3
RR
810}
811
c626a8b7 812// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 813void wxMenuItem::DoSetText( const wxString& str )
716b7364 814{
2b5f62a0 815 // '\t' is the deliminator indicating a hot key
010eb424
VZ
816 wxString text;
817 text.reserve(str.length());
818
ab46dc18 819 const wxChar *pc = str;
2b5f62a0 820 while ( (*pc != wxT('\0')) && (*pc != wxT('\t')) )
83624f79 821 {
2b5f62a0 822 if ((*pc == wxT('&')) && (*(pc+1) == wxT('&')))
23280650 823 {
2b5f62a0
VZ
824 // "&" is doubled to indicate "&" instead of accelerator
825 ++pc;
010eb424 826 text << wxT('&');
572d7461 827 }
2b5f62a0 828 else if (*pc == wxT('&'))
572d7461 829 {
010eb424 830 text << wxT('_');
572d7461 831 }
2b5f62a0
VZ
832 else if ( *pc == wxT('_') ) // escape underscores
833 {
010eb424 834 text << wxT("__");
2b5f62a0 835 }
9959e2b6 836 else
23280650 837 {
010eb424 838 text << *pc;
2b5f62a0
VZ
839 }
840 ++pc;
83624f79 841 }
a01fe3d6 842
44014bcd 843 m_hotKey = wxEmptyString;
9e691f46 844
010eb424 845 if ( *pc == wxT('\t') )
d7dbc98a
KB
846 {
847 pc++;
848 m_hotKey = pc;
849 }
010eb424
VZ
850
851 m_text = text;
716b7364
RR
852}
853
717a57c2
VZ
854#if wxUSE_ACCEL
855
856wxAcceleratorEntry *wxMenuItem::GetAccel() const
857{
1987af7e 858 if ( !GetHotKey() )
717a57c2
VZ
859 {
860 // nothing
d3b9f782 861 return NULL;
717a57c2
VZ
862 }
863
90527a50
VZ
864 // accelerator parsing code looks for them after a TAB, so insert a dummy
865 // one here
717a57c2 866 wxString label;
1987af7e 867 label << wxT('\t') << GetHotKey();
717a57c2 868
90527a50 869 return wxAcceleratorEntry::Create(label);
717a57c2
VZ
870}
871
872#endif // wxUSE_ACCEL
873
96fd301f 874void wxMenuItem::Check( bool check )
716b7364 875{
223d09f6 876 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 877
974e8d94
VZ
878 if (check == m_isChecked)
879 return;
2d17d68f 880
974e8d94 881 wxMenuItemBase::Check( check );
0472ece7 882
24bcaec3 883 switch ( GetKind() )
0472ece7 884 {
24bcaec3
VZ
885 case wxITEM_CHECK:
886 case wxITEM_RADIO:
887 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
888 break;
889
890 default:
9a83f860 891 wxFAIL_MSG( wxT("can't check this item") );
0472ece7 892 }
716b7364
RR
893}
894
8bbe427f
VZ
895void wxMenuItem::Enable( bool enable )
896{
223d09f6 897 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 898
83624f79 899 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 900 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
901}
902
96fd301f 903bool wxMenuItem::IsChecked() const
716b7364 904{
44014bcd 905 wxCHECK_MSG( m_menuItem, false, wxT("invalid menu item") );
db1b4961 906
44014bcd 907 wxCHECK_MSG( IsCheckable(), false,
974e8d94 908 wxT("can't get state of uncheckable item!") );
96fd301f 909
974e8d94 910 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
911}
912
db1b4961 913//-----------------------------------------------------------------------------
83624f79 914// wxMenu
db1b4961
RR
915//-----------------------------------------------------------------------------
916
717a57c2 917void wxMenu::Init()
c801d85f 918{
034be888 919 m_accel = gtk_accel_group_new();
6d971354 920 m_menu = gtk_menu_new();
defc0789
VS
921 // NB: keep reference to the menu so that it is not destroyed behind
922 // our back by GTK+ e.g. when it is removed from menubar:
923 gtk_widget_ref(m_menu);
8bbe427f 924
d3b9f782 925 m_owner = NULL;
2b2edbed 926
d9e403cc
RR
927 // Tearoffs are entries, just like separators. So if we want this
928 // menu to be a tear-off one, we just append a tearoff entry
929 // immediately.
9959e2b6 930 if ( m_style & wxMENU_TEAROFF )
2b2edbed 931 {
9959e2b6 932 GtkWidget *tearoff = gtk_tearoff_menu_item_new();
6d971354 933
9959e2b6
VZ
934 gtk_menu_append(GTK_MENU(m_menu), tearoff);
935 }
6d971354 936
9959e2b6 937 m_prevRadio = NULL;
c801d85f 938
717a57c2 939 // append the title as the very first entry if we have it
9959e2b6 940 if ( !m_title.empty() )
d1b15f03 941 {
9959e2b6 942 Append(wxGTK_TITLE_ID, m_title);
717a57c2 943 AppendSeparator();
d1b15f03 944 }
717a57c2 945}
15a2076a 946
717a57c2
VZ
947wxMenu::~wxMenu()
948{
222ed1d6 949 WX_CLEAR_LIST(wxMenuItemList, m_items);
f03ec224 950
368a4a47 951 if ( GTK_IS_WIDGET( m_menu ))
defc0789 952 {
a761df69 953 // see wxMenu::Init
88d19775 954 gtk_widget_unref( m_menu );
a761df69
VS
955 // if the menu is inserted in another menu at this time, there was
956 // one more reference to it:
957 if ( m_owner )
958 gtk_widget_destroy( m_menu );
defc0789 959 }
c2dd8380
GL
960}
961
96a3acb7 962wxString wxMenu::GetTitle() const
7bc0ff86
VZ
963{
964 return wxConvertMnemonicsFromGTK(wxMenuBase::GetTitle());
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 }
a1b806b9 979 else if (mitem->GetBitmap().IsOk())
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