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