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