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