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