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