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