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