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