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