]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Fixed problem with CaptureMouse if cursor is null.
[wxWidgets.git] / src / gtk / menu.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose:
4// Author: Robert Roebling
96fd301f 5// Id: $Id$
a81258be 6// Copyright: (c) 1998 Robert Roebling
96fd301f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
c801d85f
KB
10#ifdef __GNUG__
11#pragma implementation "menu.h"
6ca41e57 12#pragma implementation "menuitem.h"
c801d85f
KB
13#endif
14
96fd301f 15#include "wx/log.h"
30dea054 16#include "wx/intl.h"
06cfab17 17#include "wx/app.h"
3dfac970 18#include "wx/menu.h"
c801d85f 19
974e8d94
VZ
20#if wxUSE_ACCEL
21 #include "wx/accel.h"
22#endif // wxUSE_ACCEL
23
16c1f79c
RR
24#include <gdk/gdk.h>
25#include <gtk/gtk.h>
83624f79 26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
717a57c2
VZ
34#if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
35static wxString GetHotKey( const wxMenuItem& item );
36#endif
37
c801d85f
KB
38//-----------------------------------------------------------------------------
39// wxMenuBar
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
43
3502e687
RR
44wxMenuBar::wxMenuBar( long style )
45{
1e133b7d 46 /* the parent window is known after wxFrame::SetMenu() */
23280650 47 m_needParent = FALSE;
ae53c98c 48 m_style = style;
9c884972 49 m_invokingWindow = (wxWindow*) NULL;
23280650 50
4dcaf11a 51 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 52 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 53 {
223d09f6 54 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 55 return;
4dcaf11a 56 }
3502e687
RR
57
58 m_menus.DeleteContents( TRUE );
59
1e133b7d
RR
60 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
61#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
62 m_accel = gtk_accel_group_new();
63 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
64 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 65#else
3502e687 66 m_menubar = gtk_menu_bar_new();
1e133b7d 67#endif
3502e687
RR
68
69 if (style & wxMB_DOCKABLE)
70 {
71 m_widget = gtk_handle_box_new();
c626a8b7
VZ
72 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
73 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
74 }
75 else
76 {
77 m_widget = GTK_WIDGET(m_menubar);
78 }
79
80 PostCreation();
c4608a8a 81
db434467 82 ApplyWidgetStyle();
3502e687
RR
83}
84
96fd301f 85wxMenuBar::wxMenuBar()
c801d85f 86{
1e133b7d
RR
87 /* the parent window is known after wxFrame::SetMenu() */
88 m_needParent = FALSE;
ae53c98c 89 m_style = 0;
9c884972 90 m_invokingWindow = (wxWindow*) NULL;
23280650 91
4dcaf11a 92 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
223d09f6 93 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, wxT("menubar") ))
4dcaf11a 94 {
223d09f6 95 wxFAIL_MSG( wxT("wxMenuBar creation failed") );
455fadaa 96 return;
4dcaf11a 97 }
974e8d94 98
83624f79 99 m_menus.DeleteContents( TRUE );
96fd301f 100
1e133b7d
RR
101 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
102#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
103 m_accel = gtk_accel_group_new();
104 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
105 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 106#else
83624f79 107 m_menubar = gtk_menu_bar_new();
1e133b7d 108#endif
8bbe427f 109
828f655f 110 m_widget = GTK_WIDGET(m_menubar);
96fd301f 111
83624f79 112 PostCreation();
c4608a8a 113
db434467 114 ApplyWidgetStyle();
6de97a3b 115}
c801d85f 116
1e133b7d
RR
117wxMenuBar::~wxMenuBar()
118{
d1b15f03 119// gtk_object_unref( GTK_OBJECT(m_factory) ); why not ?
1e133b7d
RR
120}
121
5bd9e519
RR
122static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
123{
124 menu->SetInvokingWindow( (wxWindow*) NULL );
125
126#if (GTK_MINOR_VERSION > 0)
127 wxWindow *top_frame = win;
8487f887 128 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 129 top_frame = top_frame->GetParent();
5bd9e519
RR
130
131 /* support for native hot keys */
132 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
133#endif
134
1987af7e 135 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
136 while (node)
137 {
1987af7e 138 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
139 if (menuitem->IsSubMenu())
140 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 141 node = node->GetNext();
5bd9e519
RR
142 }
143}
144
145static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
146{
147 menu->SetInvokingWindow( win );
148
b4da05a6 149#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
5bd9e519 150 wxWindow *top_frame = win;
8487f887 151 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 152 top_frame = top_frame->GetParent();
5bd9e519
RR
153
154 /* support for native hot keys */
b4da05a6
VZ
155 GtkObject *obj = GTK_OBJECT(top_frame->m_widget);
156 if ( !g_slist_find( menu->m_accel->attach_objects, obj ) )
157 gtk_accel_group_attach( menu->m_accel, obj );
5bd9e519
RR
158#endif
159
1987af7e 160 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
5bd9e519
RR
161 while (node)
162 {
1987af7e 163 wxMenuItem *menuitem = node->GetData();
5bd9e519
RR
164 if (menuitem->IsSubMenu())
165 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
1987af7e 166 node = node->GetNext();
5bd9e519
RR
167 }
168}
169
170void wxMenuBar::SetInvokingWindow( wxWindow *win )
171{
9c884972 172 m_invokingWindow = win;
5bd9e519
RR
173#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
174 wxWindow *top_frame = win;
8487f887 175 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 176 top_frame = top_frame->GetParent();
5bd9e519
RR
177
178 /* support for native key accelerators indicated by underscroes */
b4da05a6
VZ
179 GtkObject *obj = GTK_OBJECT(top_frame->m_widget);
180 if ( !g_slist_find( m_accel->attach_objects, obj ) )
181 gtk_accel_group_attach( m_accel, obj );
5bd9e519
RR
182#endif
183
1987af7e 184 wxMenuList::Node *node = m_menus.GetFirst();
5bd9e519
RR
185 while (node)
186 {
1987af7e 187 wxMenu *menu = node->GetData();
5bd9e519 188 wxMenubarSetInvokingWindow( menu, win );
1987af7e 189 node = node->GetNext();
5bd9e519
RR
190 }
191}
192
193void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
194{
9c884972 195 m_invokingWindow = (wxWindow*) NULL;
5bd9e519
RR
196#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
197 wxWindow *top_frame = win;
8487f887 198 while (top_frame->GetParent() && !(top_frame->IsTopLevel()))
bd77da97 199 top_frame = top_frame->GetParent();
5bd9e519
RR
200
201 /* support for native key accelerators indicated by underscroes */
202 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
203#endif
204
1987af7e 205 wxMenuList::Node *node = m_menus.GetFirst();
5bd9e519
RR
206 while (node)
207 {
1987af7e 208 wxMenu *menu = node->GetData();
5bd9e519 209 wxMenubarUnsetInvokingWindow( menu, win );
1987af7e 210 node = node->GetNext();
5bd9e519
RR
211 }
212}
213
3dfac970 214bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
c801d85f 215{
f03ec224
VZ
216 if ( !wxMenuBarBase::Append( menu, title ) )
217 return FALSE;
218
219 return GtkAppend(menu, title);
220}
23280650 221
f03ec224
VZ
222bool wxMenuBar::GtkAppend(wxMenu *menu, const wxString& title)
223{
a533f5c1 224 const wxChar *pc;
23280650 225
1e133b7d
RR
226 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
227 wxString str;
223d09f6 228 for ( pc = title; *pc != wxT('\0'); pc++ )
83624f79 229 {
223d09f6 230 if (*pc == wxT('&'))
23280650 231 {
1e133b7d 232#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
223d09f6 233 str << wxT('_');
455fadaa 234 }
223d09f6 235 else if (*pc == wxT('/'))
23280650 236 {
223d09f6 237 str << wxT('\\');
034be888 238#endif
23280650 239 }
d38ceae8 240 else
455fadaa 241 {
90a53a3a 242#if __WXGTK12__
223d09f6 243 if ( *pc == wxT('_') )
455fadaa
VZ
244 {
245 // underscores must be doubled to prevent them from being
246 // interpreted as accelerator character prefix by GTK
247 str << *pc;
248 }
90a53a3a 249#endif // GTK+ 1.2
455fadaa
VZ
250
251 str << *pc;
252 }
1e133b7d
RR
253 }
254
255 /* this doesn't have much effect right now */
256 menu->SetTitle( str );
23280650 257
1e133b7d
RR
258 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
259#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
260
261 /* local buffer in multibyte form */
2b2edbed 262 wxString buf;
223d09f6 263 buf << wxT('/') << str.c_str();
c980c992 264
dc6c62a9 265 char *cbuf = new char[buf.Length()+1];
c980c992
GL
266 strcpy(cbuf, buf.mbc_str());
267
1e133b7d 268 GtkItemFactoryEntry entry;
c980c992 269 entry.path = (gchar *)cbuf; // const_cast
1e133b7d
RR
270 entry.accelerator = (gchar*) NULL;
271 entry.callback = (GtkItemFactoryCallback) NULL;
272 entry.callback_action = 0;
2b2edbed
KB
273 entry.item_type = "<Branch>";
274
1e133b7d 275 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
1e133b7d 276 /* in order to get the pointer to the item we need the item text _without_ underscores */
223d09f6
KB
277 wxString tmp = wxT("<main>/");
278 for ( pc = str; *pc != wxT('\0'); pc++ )
1e133b7d 279 {
455fadaa
VZ
280 // contrary to the common sense, we must throw out _all_ underscores,
281 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
974e8d94 282 // might naively think). IMHO it's a bug in GTK+ (VZ)
223d09f6 283 while (*pc == wxT('_'))
455fadaa 284 pc++;
2b2edbed 285 tmp << *pc;
034be888 286 }
1e133b7d 287 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
1e133b7d 288 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
2b2edbed 289 delete [] cbuf;
1e133b7d 290#else
96fd301f 291
1e133b7d 292 menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() );
2b1c162e
RR
293 gtk_widget_show( menu->m_owner );
294 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
96fd301f 295
2b1c162e 296 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
23280650 297
1e133b7d 298#endif
9c884972
RR
299
300 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
301 // adding menu later on.
302 if (m_invokingWindow)
303 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
3dfac970
VZ
304
305 return TRUE;
306}
307
308bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
309{
310 if ( !wxMenuBarBase::Insert(pos, menu, title) )
311 return FALSE;
312
f03ec224
VZ
313#if __WXGTK12__
314 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
315 // of version 1.2.6), so we first append the item and then change its
316 // index
317 if ( !GtkAppend(menu, title) )
318 return FALSE;
319
186baeb2
RR
320 if (pos+1 >= m_menus.GetCount())
321 return TRUE;
322
f03ec224
VZ
323 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
324 gpointer data = g_list_last(menu_shell->children)->data;
325 menu_shell->children = g_list_remove(menu_shell->children, data);
326 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
327
328 return TRUE;
329#else // GTK < 1.2
330 // this should be easy to do with GTK 1.0 - can use standard functions for
331 // this and don't need any hacks like above, but as I don't have GTK 1.0
332 // any more I can't do it
333 wxFAIL_MSG( wxT("TODO") );
3dfac970
VZ
334
335 return FALSE;
f03ec224 336#endif // GTK 1.2/1.0
3dfac970
VZ
337}
338
339wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
340{
f03ec224
VZ
341 // remove the old item and insert a new one
342 wxMenu *menuOld = Remove(pos);
343 if ( menuOld && !Insert(pos, menu, title) )
344 {
1d62a8b4 345 return (wxMenu*) NULL;
f03ec224 346 }
3dfac970 347
f03ec224
VZ
348 // either Insert() succeeded or Remove() failed and menuOld is NULL
349 return menuOld;
3dfac970
VZ
350}
351
352wxMenu *wxMenuBar::Remove(size_t pos)
353{
f03ec224
VZ
354 wxMenu *menu = wxMenuBarBase::Remove(pos);
355 if ( !menu )
1d62a8b4 356 return (wxMenu*) NULL;
f03ec224 357
589c9fff 358/*
c4608a8a 359 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
589c9fff 360
1d62a8b4
RR
361 printf( "factory entries before %d\n", (int)g_slist_length(m_factory->items) );
362 printf( "menu shell entries before %d\n", (int)g_list_length( menu_shell->children ) );
589c9fff 363*/
c4608a8a 364
1d62a8b4
RR
365 // unparent calls unref() and that would delete the widget so we raise
366 // the ref count to 2 artificially before invoking unparent.
367 gtk_widget_ref( menu->m_menu );
368 gtk_widget_unparent( menu->m_menu );
c4608a8a 369
1d62a8b4 370 gtk_widget_destroy( menu->m_owner );
c4608a8a 371
589c9fff 372/*
1d62a8b4
RR
373 printf( "factory entries after %d\n", (int)g_slist_length(m_factory->items) );
374 printf( "menu shell entries after %d\n", (int)g_list_length( menu_shell->children ) );
589c9fff 375*/
c4608a8a 376
1d62a8b4 377 return menu;
6de97a3b 378}
96fd301f 379
716b7364 380static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 381{
c626a8b7 382 if (menu->GetTitle() == menuString)
83624f79
RR
383 {
384 int res = menu->FindItem( itemString );
c626a8b7
VZ
385 if (res != wxNOT_FOUND)
386 return res;
83624f79 387 }
c626a8b7 388
1987af7e 389 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
83624f79
RR
390 while (node)
391 {
1987af7e 392 wxMenuItem *item = node->GetData();
83624f79
RR
393 if (item->IsSubMenu())
394 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 395
1987af7e 396 node = node->GetNext();
83624f79
RR
397 }
398
c626a8b7
VZ
399 return wxNOT_FOUND;
400}
401
c801d85f
KB
402int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
403{
1987af7e 404 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
405 while (node)
406 {
1987af7e 407 wxMenu *menu = node->GetData();
83624f79 408 int res = FindMenuItemRecursive( menu, menuString, itemString);
1987af7e
VZ
409 if (res != -1)
410 return res;
411 node = node->GetNext();
83624f79 412 }
1987af7e
VZ
413
414 return wxNOT_FOUND;
6de97a3b 415}
c801d85f 416
c626a8b7 417// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 418static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 419{
717a57c2 420 wxMenuItem* result = menu->FindChildItem(id);
716b7364 421
1987af7e 422 wxMenuItemList::Node *node = menu->GetMenuItems().GetFirst();
c626a8b7 423 while ( node && result == NULL )
83624f79 424 {
1987af7e 425 wxMenuItem *item = node->GetData();
83624f79 426 if (item->IsSubMenu())
c626a8b7 427 {
83624f79 428 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 429 }
1987af7e 430 node = node->GetNext();
83624f79 431 }
96fd301f 432
83624f79 433 return result;
6de97a3b 434}
716b7364 435
3dfac970 436wxMenuItem* wxMenuBar::FindItem( int id, wxMenu **menuForItem ) const
716b7364 437{
83624f79 438 wxMenuItem* result = 0;
1987af7e 439 wxMenuList::Node *node = m_menus.GetFirst();
83624f79
RR
440 while (node && result == 0)
441 {
1987af7e 442 wxMenu *menu = node->GetData();
83624f79 443 result = FindMenuItemByIdRecursive( menu, id );
1987af7e 444 node = node->GetNext();
83624f79 445 }
c626a8b7 446
3dfac970
VZ
447 if ( menuForItem )
448 {
449 *menuForItem = result ? result->GetMenu() : (wxMenu *)NULL;
450 }
c626a8b7 451
3dfac970 452 return result;
bbe0af5b
RR
453}
454
3dfac970 455void wxMenuBar::EnableTop( size_t pos, bool flag )
bbe0af5b 456{
3dfac970 457 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 458
223d09f6 459 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 460
3dfac970 461 wxMenu* menu = node->GetData();
c626a8b7
VZ
462
463 if (menu->m_owner)
464 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
465}
466
3dfac970 467wxString wxMenuBar::GetLabelTop( size_t pos ) const
bbe0af5b 468{
3dfac970 469 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 470
223d09f6 471 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
c626a8b7 472
3dfac970 473 wxMenu* menu = node->GetData();
c626a8b7 474
2b1c162e 475 return menu->GetTitle();
bbe0af5b
RR
476}
477
3dfac970 478void wxMenuBar::SetLabelTop( size_t pos, const wxString& label )
bbe0af5b 479{
3dfac970 480 wxMenuList::Node *node = m_menus.Item( pos );
c626a8b7 481
223d09f6 482 wxCHECK_RET( node, wxT("menu not found") );
c626a8b7 483
3dfac970 484 wxMenu* menu = node->GetData();
c626a8b7 485
2b1c162e 486 menu->SetTitle( label );
bbe0af5b
RR
487}
488
c801d85f 489//-----------------------------------------------------------------------------
cf7a7e13 490// "activate"
c801d85f
KB
491//-----------------------------------------------------------------------------
492
6de97a3b 493static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 494{
acfd422a 495 if (g_isIdle) wxapp_install_idle_handler();
c4608a8a 496
83624f79 497 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 498
83624f79 499 /* should find it for normal (not popup) menu */
c626a8b7 500 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
96fd301f 501
c626a8b7
VZ
502 if (!menu->IsEnabled(id))
503 return;
96fd301f 504
717a57c2 505 wxMenuItem* item = menu->FindChildItem( id );
223d09f6 506 wxCHECK_RET( item, wxT("error in menu item callback") );
c626a8b7
VZ
507
508 if (item->IsCheckable())
2d17d68f 509 {
974e8d94
VZ
510 bool isReallyChecked = item->IsChecked();
511 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
2d17d68f 512 {
c626a8b7 513 /* the menu item has been checked by calling wxMenuItem->Check() */
2d17d68f 514 return;
c626a8b7
VZ
515 }
516 else
517 {
974e8d94
VZ
518 /* the user pressed on the menu item -> report and make consistent
519 * again */
520 item->wxMenuItemBase::Check(isReallyChecked);
c626a8b7 521 }
2d17d68f
RR
522 }
523
83624f79
RR
524 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
525 event.SetEventObject( menu );
526 event.SetInt(id );
8bbe427f 527
9739d9ee 528#if wxUSE_MENU_CALLBACK
c626a8b7 529 if (menu->GetCallback())
83624f79 530 {
c626a8b7 531 (void) (*(menu->GetCallback())) (*menu, event);
83624f79
RR
532 return;
533 }
9739d9ee 534#endif // wxUSE_MENU_CALLBACK
cf7a7e13 535
c626a8b7
VZ
536 if (menu->GetEventHandler()->ProcessEvent(event))
537 return;
cf7a7e13 538
83624f79 539 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
540 if (win)
541 win->GetEventHandler()->ProcessEvent( event );
cf7a7e13
RR
542}
543
544//-----------------------------------------------------------------------------
545// "select"
546//-----------------------------------------------------------------------------
547
548static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
549{
acfd422a
RR
550 if (g_isIdle) wxapp_install_idle_handler();
551
83624f79
RR
552 int id = menu->FindMenuIdByMenuItem(widget);
553
554 wxASSERT( id != -1 ); // should find it!
cf7a7e13 555
c626a8b7
VZ
556 if (!menu->IsEnabled(id))
557 return;
cf7a7e13 558
342b6a2f 559 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 560 event.SetEventObject( menu );
cf7a7e13 561
c626a8b7
VZ
562 if (menu->GetEventHandler()->ProcessEvent(event))
563 return;
6de97a3b 564
83624f79
RR
565 wxWindow *win = menu->GetInvokingWindow();
566 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 567}
c801d85f 568
cd743a6f
RR
569//-----------------------------------------------------------------------------
570// "deselect"
571//-----------------------------------------------------------------------------
572
573static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
574{
acfd422a
RR
575 if (g_isIdle) wxapp_install_idle_handler();
576
cd743a6f
RR
577 int id = menu->FindMenuIdByMenuItem(widget);
578
579 wxASSERT( id != -1 ); // should find it!
580
c626a8b7
VZ
581 if (!menu->IsEnabled(id))
582 return;
cd743a6f
RR
583
584 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
585 event.SetEventObject( menu );
586
c626a8b7
VZ
587 if (menu->GetEventHandler()->ProcessEvent(event))
588 return;
cd743a6f
RR
589
590 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
591 if (win)
592 win->GetEventHandler()->ProcessEvent( event );
cd743a6f
RR
593}
594
cf7a7e13 595//-----------------------------------------------------------------------------
db1b4961 596// wxMenuItem
cf7a7e13
RR
597//-----------------------------------------------------------------------------
598
974e8d94
VZ
599IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
600
601wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
602 int id,
603 const wxString& name,
604 const wxString& help,
605 bool isCheckable,
606 wxMenu *subMenu)
607{
608 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
609}
96fd301f 610
974e8d94
VZ
611wxMenuItem::wxMenuItem(wxMenu *parentMenu,
612 int id,
613 const wxString& text,
614 const wxString& help,
615 bool isCheckable,
616 wxMenu *subMenu)
c801d85f 617{
974e8d94
VZ
618 m_id = id;
619 m_isCheckable = isCheckable;
83624f79
RR
620 m_isChecked = FALSE;
621 m_isEnabled = TRUE;
974e8d94
VZ
622 m_subMenu = subMenu;
623 m_parentMenu = parentMenu;
624 m_help = help;
625
83624f79 626 m_menuItem = (GtkWidget *) NULL;
974e8d94 627
974e8d94 628 DoSetText(text);
6de97a3b 629}
c801d85f 630
d1b15f03
RR
631wxMenuItem::~wxMenuItem()
632{
633 // don't delete menu items, the menus take care of that
634}
635
717a57c2 636// return the menu item text without any menu accels
3b59cdbf
VZ
637/* static */
638wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
717a57c2
VZ
639{
640 wxString label;
641#if (GTK_MINOR_VERSION > 0)
3b59cdbf 642 for ( const wxChar *pc = text.c_str(); *pc; pc++ )
717a57c2 643 {
5f170f33 644 if ( *pc == wxT('_') || *pc == wxT('&') )
717a57c2 645 {
5f170f33
VZ
646 // '_' is the escape character for GTK+ and '&' is the one for
647 // wxWindows - skip both of them
717a57c2
VZ
648 continue;
649 }
650
651 label += *pc;
652 }
653#else // GTK+ 1.0
3b59cdbf 654 label = text;
717a57c2
VZ
655#endif // GTK+ 1.2/1.0
656
657 return label;
658}
659
660void wxMenuItem::SetText( const wxString& str )
661{
662 DoSetText(str);
354aa1e3
RR
663
664 if (m_menuItem)
665 {
666 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
717a57c2
VZ
667
668 /* set new text */
354aa1e3 669 gtk_label_set( label, m_text.mb_str());
717a57c2
VZ
670
671 /* reparse key accel */
1987af7e 672 (void)gtk_label_parse_uline (GTK_LABEL(label), m_text.mb_str() );
354aa1e3
RR
673 gtk_accel_label_refetch( GTK_ACCEL_LABEL(label) );
674 }
675}
676
c626a8b7 677// it's valid for this function to be called even if m_menuItem == NULL
974e8d94 678void wxMenuItem::DoSetText( const wxString& str )
716b7364 679{
ab46dc18 680 /* '\t' is the deliminator indicating a hot key */
974e8d94 681 m_text.Empty();
ab46dc18 682 const wxChar *pc = str;
223d09f6 683 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
83624f79 684 {
223d09f6 685 if (*pc == wxT('&'))
23280650 686 {
034be888 687#if (GTK_MINOR_VERSION > 0)
223d09f6 688 m_text << wxT('_');
572d7461 689 }
223d09f6 690 else if ( *pc == wxT('_') ) // escape underscores
572d7461 691 {
223d09f6 692 m_text << wxT("__");
572d7461 693 }
223d09f6 694 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
23280650 695 {
223d09f6 696 m_text << wxT('\\'); /* ... and replace them with back slashes */
034be888
RR
697#endif
698 }
d38ceae8 699 else
354aa1e3 700 m_text << *pc;
83624f79 701 }
23280650 702
837904f2 703 /* only GTK 1.2 knows about hot keys */
223d09f6 704 m_hotKey = wxT("");
ab46dc18 705#if (GTK_MINOR_VERSION > 0)
223d09f6 706 if(*pc == wxT('\t'))
d7dbc98a
KB
707 {
708 pc++;
709 m_hotKey = pc;
710 }
ab46dc18 711#endif
716b7364
RR
712}
713
717a57c2
VZ
714#if wxUSE_ACCEL
715
716wxAcceleratorEntry *wxMenuItem::GetAccel() const
717{
1987af7e 718 if ( !GetHotKey() )
717a57c2
VZ
719 {
720 // nothing
721 return (wxAcceleratorEntry *)NULL;
722 }
723
724 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
725 wxString label;
1987af7e 726 label << wxT('\t') << GetHotKey();
717a57c2
VZ
727
728 return wxGetAccelFromString(label);
729}
730
731#endif // wxUSE_ACCEL
732
96fd301f 733void wxMenuItem::Check( bool check )
716b7364 734{
223d09f6 735 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 736
223d09f6 737 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
96fd301f 738
974e8d94
VZ
739 if (check == m_isChecked)
740 return;
2d17d68f 741
974e8d94 742 wxMenuItemBase::Check( check );
83624f79 743 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
744}
745
8bbe427f
VZ
746void wxMenuItem::Enable( bool enable )
747{
223d09f6 748 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
db1b4961 749
83624f79 750 gtk_widget_set_sensitive( m_menuItem, enable );
974e8d94 751 wxMenuItemBase::Enable( enable );
a9c96bcc
RR
752}
753
96fd301f 754bool wxMenuItem::IsChecked() const
716b7364 755{
223d09f6 756 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
db1b4961 757
974e8d94
VZ
758 wxCHECK_MSG( IsCheckable(), FALSE,
759 wxT("can't get state of uncheckable item!") );
96fd301f 760
974e8d94 761 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
716b7364
RR
762}
763
354aa1e3
RR
764wxString wxMenuItem::GetFactoryPath() const
765{
f03ec224
VZ
766 /* in order to get the pointer to the item we need the item text _without_
767 underscores */
354aa1e3 768 wxString path( wxT("<main>/") );
717a57c2
VZ
769 path += GetLabel();
770
354aa1e3
RR
771 return path;
772}
773
db1b4961 774//-----------------------------------------------------------------------------
83624f79 775// wxMenu
db1b4961
RR
776//-----------------------------------------------------------------------------
777
c801d85f
KB
778IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
779
717a57c2 780void wxMenu::Init()
c801d85f 781{
034be888
RR
782#if (GTK_MINOR_VERSION > 0)
783 m_accel = gtk_accel_group_new();
784 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
785 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 786#else
83624f79 787 m_menu = gtk_menu_new(); // Do not show!
034be888 788#endif
8bbe427f 789
2b1c162e 790 m_owner = (GtkWidget*) NULL;
2b2edbed
KB
791
792#if (GTK_MINOR_VERSION > 0)
793 /* Tearoffs are entries, just like separators. So if we want this
794 menu to be a tear-off one, we just append a tearoff entry
795 immediately. */
796 if(m_style & wxMENU_TEAROFF)
797 {
798 GtkItemFactoryEntry entry;
799 entry.path = "/tearoff";
800 entry.callback = (GtkItemFactoryCallback) NULL;
801 entry.callback_action = 0;
802 entry.item_type = "<Tearoff>";
803 entry.accelerator = (gchar*) NULL;
804 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 805 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed
KB
806 }
807#endif
c801d85f 808
717a57c2
VZ
809 // append the title as the very first entry if we have it
810 if ( !!m_title )
d1b15f03 811 {
717a57c2
VZ
812 Append(-2, m_title);
813 AppendSeparator();
d1b15f03 814 }
717a57c2 815}
15a2076a 816
717a57c2
VZ
817wxMenu::~wxMenu()
818{
a583e623 819 m_items.Clear();
f03ec224 820
d1b15f03 821 gtk_widget_destroy( m_menu );
974e8d94 822
d1b15f03 823 gtk_object_unref( GTK_OBJECT(m_factory) );
c2dd8380
GL
824}
825
32db328c 826bool wxMenu::GtkAppend(wxMenuItem *mitem)
c2dd8380 827{
717a57c2 828 GtkWidget *menuItem;
96fd301f 829
717a57c2
VZ
830 if ( mitem->IsSeparator() )
831 {
08fc1744 832#if (GTK_MINOR_VERSION > 0)
717a57c2
VZ
833 GtkItemFactoryEntry entry;
834 entry.path = "/sep";
835 entry.callback = (GtkItemFactoryCallback) NULL;
836 entry.callback_action = 0;
837 entry.item_type = "<Separator>";
838 entry.accelerator = (gchar*) NULL;
839
840 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
841
842 /* this will be wrong for more than one separator. do we care? */
843 menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
844#else // GTK+ 1.0
845 menuItem = gtk_menu_item_new();
846#endif // GTK 1.2/1.0
847 }
848 else if ( mitem->IsSubMenu() )
837904f2 849 {
717a57c2
VZ
850#if (GTK_MINOR_VERSION > 0)
851 /* text has "_" instead of "&" after mitem->SetText() */
852 wxString text( mitem->GetText() );
974e8d94 853
717a57c2
VZ
854 /* local buffer in multibyte form */
855 char buf[200];
856 strcpy( buf, "/" );
857 strcat( buf, text.mb_str() );
50592885 858
717a57c2
VZ
859 GtkItemFactoryEntry entry;
860 entry.path = buf;
861 entry.callback = (GtkItemFactoryCallback) 0;
862 entry.callback_action = 0;
863 entry.item_type = "<Branch>";
a583e623 864 entry.accelerator = (gchar*) NULL;
50592885 865
717a57c2 866 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
50592885 867
717a57c2 868 wxString path( mitem->GetFactoryPath() );
1987af7e 869 menuItem = gtk_item_factory_get_item( m_factory, path.mb_str() );
717a57c2 870#else // GTK+ 1.0
1987af7e 871 menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
717a57c2 872#endif // GTK 1.2/1.0
50592885 873
1987af7e 874 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), mitem->GetSubMenu()->m_menu );
e7200790
VZ
875
876 // if adding a submenu to a menu already existing in the menu bar, we
877 // must set invoking window to allow processing events from this
878 // submenu
879 if ( m_invokingWindow )
880 wxMenubarSetInvokingWindow(mitem->GetSubMenu(), m_invokingWindow);
837904f2 881 }
717a57c2
VZ
882 else // a normal item
883 {
034be888 884#if (GTK_MINOR_VERSION > 0)
717a57c2
VZ
885 /* text has "_" instead of "&" after mitem->SetText() */
886 wxString text( mitem->GetText() );
887
888 /* local buffer in multibyte form */
889 char buf[200];
890 strcpy( buf, "/" );
891 strcat( buf, text.mb_str() );
892
893 GtkItemFactoryEntry entry;
894 entry.path = buf;
895 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
896 entry.callback_action = 0;
1987af7e 897 if ( mitem->IsCheckable() )
717a57c2
VZ
898 entry.item_type = "<CheckItem>";
899 else
900 entry.item_type = "<Item>";
a583e623 901 entry.accelerator = (gchar*) NULL;
23280650 902
974e8d94 903#if wxUSE_ACCEL
717a57c2
VZ
904 // due to an apparent bug in GTK+, we have to use a static buffer here -
905 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
906 // somehow! (VZ)
907 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
908 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
909 entry.accelerator = s_accel;
910#else // !wxUSE_ACCEL
911 entry.accelerator = (char*) NULL;
912#endif // wxUSE_ACCEL/!wxUSE_ACCEL
96fd301f 913
717a57c2 914 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
cf7a7e13 915
717a57c2 916 wxString path( mitem->GetFactoryPath() );
1987af7e 917 menuItem = gtk_item_factory_get_widget( m_factory, path.mb_str() );
717a57c2 918#else // GTK+ 1.0
1987af7e
VZ
919 menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
920 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
034be888 921
717a57c2
VZ
922 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
923 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
924 (gpointer)this );
925#endif // GTK+ 1.2/1.0
926 }
23280650 927
717a57c2
VZ
928 if ( !mitem->IsSeparator() )
929 {
930 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
931 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
932 (gpointer)this );
837904f2 933
717a57c2
VZ
934 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
935 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
936 (gpointer)this );
937 }
23280650 938
717a57c2 939#if GTK_MINOR_VERSION == 0
837904f2
RR
940 gtk_menu_append( GTK_MENU(m_menu), menuItem );
941 gtk_widget_show( menuItem );
717a57c2 942#endif // GTK+ 1.0
23280650 943
837904f2 944 mitem->SetMenuItem(menuItem);
837904f2 945
32db328c 946 return TRUE;
6de97a3b 947}
c801d85f 948
32db328c 949bool wxMenu::DoAppend(wxMenuItem *mitem)
828f655f 950{
32db328c 951 return GtkAppend(mitem) && wxMenuBase::DoAppend(mitem);
c33c4050
RR
952}
953
717a57c2 954bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
c33c4050 955{
717a57c2
VZ
956 if ( !wxMenuBase::DoInsert(pos, item) )
957 return FALSE;
c626a8b7 958
32db328c
VZ
959#ifdef __WXGTK12__
960 // GTK+ doesn't have a function to insert a menu using GtkItemFactory (as
961 // of version 1.2.6), so we first append the item and then change its
962 // index
963 if ( !GtkAppend(item) )
964 return FALSE;
965
966 GtkMenuShell *menu_shell = GTK_MENU_SHELL(m_factory->widget);
967 gpointer data = g_list_last(menu_shell->children)->data;
968 menu_shell->children = g_list_remove(menu_shell->children, data);
969 menu_shell->children = g_list_insert(menu_shell->children, data, pos);
970
971 return TRUE;
972#else // GTK < 1.2
973 // this should be easy to do...
974 wxFAIL_MSG( wxT("not implemented") );
c626a8b7 975
717a57c2 976 return FALSE;
96fa7876 977#endif // GTK 1.2/1.0
c33c4050
RR
978}
979
717a57c2 980wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
c33c4050 981{
717a57c2
VZ
982 if ( !wxMenuBase::DoRemove(item) )
983 return (wxMenuItem *)NULL;
c626a8b7 984
717a57c2
VZ
985 // TODO: this code doesn't delete the item factory item and this seems
986 // impossible as of GTK 1.2.6.
987 gtk_widget_destroy( item->GetMenuItem() );
c626a8b7 988
717a57c2 989 return item;
c33c4050
RR
990}
991
96fd301f
VZ
992int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
993{
83624f79
RR
994 wxNode *node = m_items.First();
995 while (node)
996 {
997 wxMenuItem *item = (wxMenuItem*)node->Data();
998 if (item->GetMenuItem() == menuItem)
999 return item->GetId();
1000 node = node->Next();
1001 }
96fd301f 1002
c626a8b7 1003 return wxNOT_FOUND;
6de97a3b 1004}
c801d85f 1005
717a57c2
VZ
1006// ----------------------------------------------------------------------------
1007// helpers
1008// ----------------------------------------------------------------------------
1009
1010#if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
1011static wxString GetHotKey( const wxMenuItem& item )
c801d85f 1012{
717a57c2
VZ
1013 wxString hotkey;
1014
1015 wxAcceleratorEntry *accel = item.GetAccel();
1016 if ( accel )
83624f79 1017 {
717a57c2
VZ
1018 int flags = accel->GetFlags();
1019 if ( flags & wxACCEL_ALT )
1020 hotkey += wxT("<alt>");
1021 if ( flags & wxACCEL_CTRL )
1022 hotkey += wxT("<control>");
1023 if ( flags & wxACCEL_SHIFT )
1024 hotkey += wxT("<shift>");
1025
1026 int code = accel->GetKeyCode();
1027 switch ( code )
c626a8b7 1028 {
717a57c2
VZ
1029 case WXK_F1:
1030 case WXK_F2:
1031 case WXK_F3:
1032 case WXK_F4:
1033 case WXK_F5:
1034 case WXK_F6:
1035 case WXK_F7:
1036 case WXK_F8:
1037 case WXK_F9:
1038 case WXK_F10:
1039 case WXK_F11:
1040 case WXK_F12:
1041 hotkey << wxT('F') << code - WXK_F1 + 1;
1042 break;
96fd301f 1043
717a57c2
VZ
1044 // if there are any other keys wxGetAccelFromString() may return,
1045 // we should process them here
8bbe427f 1046
717a57c2
VZ
1047 default:
1048 if ( wxIsalnum(code) )
1049 {
1050 hotkey << (wxChar)code;
c801d85f 1051
717a57c2
VZ
1052 break;
1053 }
c801d85f 1054
717a57c2
VZ
1055 wxFAIL_MSG( wxT("unknown keyboard accel") );
1056 }
c801d85f 1057
717a57c2 1058 delete accel;
631f1bfe 1059 }
717a57c2
VZ
1060
1061 return hotkey;
631f1bfe 1062}
717a57c2 1063#endif // wxUSE_ACCEL
631f1bfe 1064