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