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