]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
//... => /* ... */
[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
15#include "wx/menu.h"
96fd301f 16#include "wx/log.h"
30dea054 17#include "wx/intl.h"
06cfab17 18#include "wx/app.h"
c801d85f 19
83624f79
RR
20#include "gdk/gdk.h"
21#include "gtk/gtk.h"
22
acfd422a
RR
23//-----------------------------------------------------------------------------
24// idle system
25//-----------------------------------------------------------------------------
26
27extern void wxapp_install_idle_handler();
28extern bool g_isIdle;
29
c801d85f
KB
30//-----------------------------------------------------------------------------
31// wxMenuBar
32//-----------------------------------------------------------------------------
33
34IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
35
3502e687
RR
36wxMenuBar::wxMenuBar( long style )
37{
1e133b7d 38 /* the parent window is known after wxFrame::SetMenu() */
23280650 39 m_needParent = FALSE;
ae53c98c 40 m_style = style;
9c884972 41 m_invokingWindow = (wxWindow*) NULL;
23280650 42
4dcaf11a
RR
43 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
44 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, style, wxDefaultValidator, _T("menubar") ))
45 {
46 wxFAIL_MSG( _T("wxMenuBar creation failed") );
455fadaa 47 return;
4dcaf11a 48 }
3502e687
RR
49
50 m_menus.DeleteContents( TRUE );
51
1e133b7d
RR
52 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
53#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
54 m_accel = gtk_accel_group_new();
55 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
56 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 57#else
3502e687 58 m_menubar = gtk_menu_bar_new();
1e133b7d 59#endif
3502e687
RR
60
61 if (style & wxMB_DOCKABLE)
62 {
63 m_widget = gtk_handle_box_new();
c626a8b7
VZ
64 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
65 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
66 }
67 else
68 {
69 m_widget = GTK_WIDGET(m_menubar);
70 }
71
72 PostCreation();
3502e687
RR
73}
74
96fd301f 75wxMenuBar::wxMenuBar()
c801d85f 76{
1e133b7d
RR
77 /* the parent window is known after wxFrame::SetMenu() */
78 m_needParent = FALSE;
ae53c98c 79 m_style = 0;
9c884972 80 m_invokingWindow = (wxWindow*) NULL;
23280650 81
4dcaf11a
RR
82 if (!PreCreation( (wxWindow*) NULL, wxDefaultPosition, wxDefaultSize ) ||
83 !CreateBase( (wxWindow*) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("menubar") ))
84 {
85 wxFAIL_MSG( _T("wxMenuBar creation failed") );
455fadaa 86 return;
4dcaf11a
RR
87 }
88
83624f79 89 m_menus.DeleteContents( TRUE );
96fd301f 90
1e133b7d
RR
91 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
92#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
93 m_accel = gtk_accel_group_new();
94 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
95 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 96#else
83624f79 97 m_menubar = gtk_menu_bar_new();
1e133b7d 98#endif
8bbe427f 99
828f655f 100 m_widget = GTK_WIDGET(m_menubar);
96fd301f 101
83624f79 102 PostCreation();
6de97a3b 103}
c801d85f 104
1e133b7d
RR
105wxMenuBar::~wxMenuBar()
106{
107 // how to destroy a GtkItemFactory ?
108}
109
5bd9e519
RR
110static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
111{
112 menu->SetInvokingWindow( (wxWindow*) NULL );
113
114#if (GTK_MINOR_VERSION > 0)
115 wxWindow *top_frame = win;
116 while (top_frame->GetParent()) top_frame = top_frame->GetParent();
117
118 /* support for native hot keys */
119 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
120#endif
121
122 wxNode *node = menu->GetItems().First();
123 while (node)
124 {
125 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
126 if (menuitem->IsSubMenu())
127 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
128 node = node->Next();
129 }
130}
131
132static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
133{
134 menu->SetInvokingWindow( win );
135
136#if (GTK_MINOR_VERSION > 0)
137 wxWindow *top_frame = win;
138 while (top_frame->GetParent())
139 top_frame = top_frame->GetParent();
140
141 /* support for native hot keys */
142 gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
143#endif
144
145 wxNode *node = menu->GetItems().First();
146 while (node)
147 {
148 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
149 if (menuitem->IsSubMenu())
150 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
151 node = node->Next();
152 }
153}
154
155void wxMenuBar::SetInvokingWindow( wxWindow *win )
156{
9c884972 157 m_invokingWindow = win;
5bd9e519
RR
158#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
159 wxWindow *top_frame = win;
160 while (top_frame->GetParent())
161 top_frame = top_frame->GetParent();
162
163 /* support for native key accelerators indicated by underscroes */
164 gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
165#endif
166
167 wxNode *node = m_menus.First();
168 while (node)
169 {
170 wxMenu *menu = (wxMenu*)node->Data();
171 wxMenubarSetInvokingWindow( menu, win );
172 node = node->Next();
173 }
174}
175
176void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
177{
9c884972 178 m_invokingWindow = (wxWindow*) NULL;
5bd9e519
RR
179#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
180 wxWindow *top_frame = win;
181 while (top_frame->GetParent())
182 top_frame = top_frame->GetParent();
183
184 /* support for native key accelerators indicated by underscroes */
185 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
186#endif
187
188 wxNode *node = m_menus.First();
189 while (node)
190 {
191 wxMenu *menu = (wxMenu*)node->Data();
192 wxMenubarUnsetInvokingWindow( menu, win );
193 node = node->Next();
194 }
195}
196
c801d85f
KB
197void wxMenuBar::Append( wxMenu *menu, const wxString &title )
198{
83624f79 199 m_menus.Append( menu );
23280650 200
a533f5c1 201 const wxChar *pc;
23280650 202
1e133b7d
RR
203 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
204 wxString str;
a533f5c1 205 for ( pc = title; *pc != _T('\0'); pc++ )
83624f79 206 {
b019151f 207 if (*pc == _T('&'))
23280650 208 {
1e133b7d
RR
209#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
210 str << _T('_');
455fadaa
VZ
211 }
212 else if (*pc == _T('/'))
23280650 213 {
fac4253c 214 str << _T('\\');
034be888 215#endif
23280650 216 }
d38ceae8 217 else
455fadaa 218 {
90a53a3a 219#if __WXGTK12__
455fadaa
VZ
220 if ( *pc == _T('_') )
221 {
222 // underscores must be doubled to prevent them from being
223 // interpreted as accelerator character prefix by GTK
224 str << *pc;
225 }
90a53a3a 226#endif // GTK+ 1.2
455fadaa
VZ
227
228 str << *pc;
229 }
1e133b7d
RR
230 }
231
232 /* this doesn't have much effect right now */
233 menu->SetTitle( str );
23280650 234
1e133b7d
RR
235 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
236#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
237
238 /* local buffer in multibyte form */
2b2edbed 239 wxString buf;
c980c992
GL
240 buf << _T('/') << str.c_str();
241
dc6c62a9 242 char *cbuf = new char[buf.Length()+1];
c980c992
GL
243 strcpy(cbuf, buf.mbc_str());
244
1e133b7d 245 GtkItemFactoryEntry entry;
c980c992 246 entry.path = (gchar *)cbuf; // const_cast
1e133b7d
RR
247 entry.accelerator = (gchar*) NULL;
248 entry.callback = (GtkItemFactoryCallback) NULL;
249 entry.callback_action = 0;
2b2edbed
KB
250 entry.item_type = "<Branch>";
251
1e133b7d 252 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
1e133b7d
RR
253 /* in order to get the pointer to the item we need the item text _without_ underscores */
254 wxString tmp = _T("<main>/");
a533f5c1 255 for ( pc = str; *pc != _T('\0'); pc++ )
1e133b7d 256 {
455fadaa
VZ
257 // contrary to the common sense, we must throw out _all_ underscores,
258 // (i.e. "Hello__World" => "HelloWorld" and not "Hello_World" as we
259 // might naively think). IMHO it's a bug in GTK+ (VZ)
260 while (*pc == _T('_'))
261 pc++;
2b2edbed 262 tmp << *pc;
034be888 263 }
1e133b7d 264 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
1e133b7d 265 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
2b2edbed 266 delete [] cbuf;
1e133b7d 267#else
96fd301f 268
1e133b7d 269 menu->m_owner = gtk_menu_item_new_with_label( str.mb_str() );
2b1c162e
RR
270 gtk_widget_show( menu->m_owner );
271 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
96fd301f 272
2b1c162e 273 gtk_menu_bar_append( GTK_MENU_BAR(m_menubar), menu->m_owner );
23280650 274
1e133b7d 275#endif
9c884972
RR
276
277 // m_invokingWindow is set after wxFrame::SetMenuBar(). This call enables
278 // adding menu later on.
279 if (m_invokingWindow)
280 wxMenubarSetInvokingWindow( menu, m_invokingWindow );
6de97a3b 281}
96fd301f 282
716b7364 283static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
c801d85f 284{
c626a8b7 285 if (menu->GetTitle() == menuString)
83624f79
RR
286 {
287 int res = menu->FindItem( itemString );
c626a8b7
VZ
288 if (res != wxNOT_FOUND)
289 return res;
83624f79 290 }
c626a8b7
VZ
291
292 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
83624f79
RR
293 while (node)
294 {
295 wxMenuItem *item = (wxMenuItem*)node->Data();
296 if (item->IsSubMenu())
297 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
2b1c162e 298
83624f79
RR
299 node = node->Next();
300 }
301
c626a8b7
VZ
302 return wxNOT_FOUND;
303}
304
80ec928c 305wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem ) const
c626a8b7
VZ
306{
307 if ( menuForItem )
308 {
309 // TODO return the pointer to the menu
310
311 *menuForItem = NULL;
312 }
313
314 return FindItem(itemId);
6de97a3b 315}
c801d85f
KB
316
317int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
318{
83624f79
RR
319 wxNode *node = m_menus.First();
320 while (node)
321 {
322 wxMenu *menu = (wxMenu*)node->Data();
323 int res = FindMenuItemRecursive( menu, menuString, itemString);
324 if (res != -1) return res;
325 node = node->Next();
326 }
327 return -1;
6de97a3b 328}
c801d85f 329
c626a8b7 330// Find a wxMenuItem using its id. Recurses down into sub-menus
96fd301f 331static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
716b7364 332{
83624f79 333 wxMenuItem* result = menu->FindItem(id);
716b7364 334
c626a8b7
VZ
335 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
336 while ( node && result == NULL )
83624f79
RR
337 {
338 wxMenuItem *item = (wxMenuItem*)node->Data();
339 if (item->IsSubMenu())
c626a8b7 340 {
83624f79 341 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
c626a8b7 342 }
83624f79
RR
343 node = node->Next();
344 }
96fd301f 345
83624f79 346 return result;
6de97a3b 347}
716b7364 348
c626a8b7 349wxMenuItem* wxMenuBar::FindItem( int id ) const
716b7364 350{
83624f79
RR
351 wxMenuItem* result = 0;
352 wxNode *node = m_menus.First();
353 while (node && result == 0)
354 {
355 wxMenu *menu = (wxMenu*)node->Data();
356 result = FindMenuItemByIdRecursive( menu, id );
357 node = node->Next();
358 }
c626a8b7 359
83624f79 360 return result;
716b7364
RR
361}
362
54ff4a70
RR
363void wxMenuBar::Check( int id, bool check )
364{
83624f79 365 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 366
b019151f 367 wxCHECK_RET( item, _T("wxMenuBar::Check: no such item") );
c626a8b7
VZ
368
369 item->Check(check);
6de97a3b 370}
54ff4a70 371
c626a8b7 372bool wxMenuBar::IsChecked( int id ) const
716b7364 373{
83624f79 374 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 375
b019151f 376 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked: no such item") );
c626a8b7
VZ
377
378 return item->IsChecked();
6de97a3b 379}
716b7364 380
54ff4a70
RR
381void wxMenuBar::Enable( int id, bool enable )
382{
83624f79 383 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 384
b019151f 385 wxCHECK_RET( item, _T("wxMenuBar::Enable: no such item") );
c626a8b7
VZ
386
387 item->Enable(enable);
6de97a3b 388}
54ff4a70 389
c626a8b7 390bool wxMenuBar::IsEnabled( int id ) const
716b7364 391{
83624f79 392 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 393
b019151f 394 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled: no such item") );
c626a8b7
VZ
395
396 return item->IsEnabled();
6de97a3b 397}
716b7364 398
bbe0af5b
RR
399wxString wxMenuBar::GetLabel( int id ) const
400{
401 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 402
b019151f 403 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel: no such item") );
c626a8b7
VZ
404
405 return item->GetText();
bbe0af5b
RR
406}
407
408void wxMenuBar::SetLabel( int id, const wxString &label )
409{
410 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 411
b019151f 412 wxCHECK_RET( item, _T("wxMenuBar::SetLabel: no such item") );
c626a8b7
VZ
413
414 item->SetText( label );
bbe0af5b
RR
415}
416
2b1c162e 417void wxMenuBar::EnableTop( int pos, bool flag )
bbe0af5b 418{
2b1c162e 419 wxNode *node = m_menus.Nth( pos );
c626a8b7 420
b019151f 421 wxCHECK_RET( node, _T("menu not found") );
c626a8b7 422
2b1c162e 423 wxMenu* menu = (wxMenu*)node->Data();
c626a8b7
VZ
424
425 if (menu->m_owner)
426 gtk_widget_set_sensitive( menu->m_owner, flag );
bbe0af5b
RR
427}
428
2b1c162e 429wxString wxMenuBar::GetLabelTop( int pos ) const
bbe0af5b 430{
2b1c162e 431 wxNode *node = m_menus.Nth( pos );
c626a8b7 432
b019151f 433 wxCHECK_MSG( node, _T("invalid"), _T("menu not found") );
c626a8b7 434
2b1c162e 435 wxMenu* menu = (wxMenu*)node->Data();
c626a8b7 436
2b1c162e 437 return menu->GetTitle();
bbe0af5b
RR
438}
439
2b1c162e 440void wxMenuBar::SetLabelTop( int pos, const wxString& label )
bbe0af5b 441{
2b1c162e 442 wxNode *node = m_menus.Nth( pos );
c626a8b7 443
b019151f 444 wxCHECK_RET( node, _T("menu not found") );
c626a8b7 445
2b1c162e 446 wxMenu* menu = (wxMenu*)node->Data();
c626a8b7 447
2b1c162e 448 menu->SetTitle( label );
bbe0af5b
RR
449}
450
342b6a2f
RR
451void wxMenuBar::SetHelpString( int id, const wxString& helpString )
452{
453 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 454
b019151f 455 wxCHECK_RET( item, _T("wxMenuBar::SetHelpString: no such item") );
c626a8b7
VZ
456
457 item->SetHelp( helpString );
342b6a2f
RR
458}
459
460wxString wxMenuBar::GetHelpString( int id ) const
461{
462 wxMenuItem* item = FindMenuItemById( id );
c626a8b7 463
b019151f 464 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString: no such item") );
c626a8b7
VZ
465
466 return item->GetHelp();
342b6a2f
RR
467}
468
c801d85f 469//-----------------------------------------------------------------------------
cf7a7e13 470// "activate"
c801d85f
KB
471//-----------------------------------------------------------------------------
472
6de97a3b 473static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
c801d85f 474{
acfd422a
RR
475 if (g_isIdle) wxapp_install_idle_handler();
476
83624f79 477 int id = menu->FindMenuIdByMenuItem(widget);
96fd301f 478
83624f79 479 /* should find it for normal (not popup) menu */
c626a8b7 480 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
96fd301f 481
c626a8b7
VZ
482 if (!menu->IsEnabled(id))
483 return;
96fd301f 484
2d17d68f 485 wxMenuItem* item = menu->FindItem( id );
b019151f 486 wxCHECK_RET( item, _T("error in menu item callback") );
c626a8b7
VZ
487
488 if (item->IsCheckable())
2d17d68f 489 {
c626a8b7 490 if (item->GetCheckedFlag() == item->IsChecked())
2d17d68f 491 {
c626a8b7 492 /* the menu item has been checked by calling wxMenuItem->Check() */
2d17d68f 493 return;
c626a8b7
VZ
494 }
495 else
496 {
497 /* the user pressed on the menu item -> report */
498 item->SetCheckedFlag(item->IsChecked()); /* make consistent again */
499 }
2d17d68f
RR
500 }
501
83624f79
RR
502 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
503 event.SetEventObject( menu );
504 event.SetInt(id );
8bbe427f 505
c626a8b7 506 if (menu->GetCallback())
83624f79 507 {
c626a8b7 508 (void) (*(menu->GetCallback())) (*menu, event);
83624f79
RR
509 return;
510 }
cf7a7e13 511
c626a8b7
VZ
512 if (menu->GetEventHandler()->ProcessEvent(event))
513 return;
cf7a7e13 514
83624f79 515 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
516 if (win)
517 win->GetEventHandler()->ProcessEvent( event );
cf7a7e13
RR
518}
519
520//-----------------------------------------------------------------------------
521// "select"
522//-----------------------------------------------------------------------------
523
524static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
525{
acfd422a
RR
526 if (g_isIdle) wxapp_install_idle_handler();
527
83624f79
RR
528 int id = menu->FindMenuIdByMenuItem(widget);
529
530 wxASSERT( id != -1 ); // should find it!
cf7a7e13 531
c626a8b7
VZ
532 if (!menu->IsEnabled(id))
533 return;
cf7a7e13 534
342b6a2f 535 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
83624f79 536 event.SetEventObject( menu );
cf7a7e13 537
c626a8b7
VZ
538 if (menu->GetEventHandler()->ProcessEvent(event))
539 return;
6de97a3b 540
83624f79
RR
541 wxWindow *win = menu->GetInvokingWindow();
542 if (win) win->GetEventHandler()->ProcessEvent( event );
6de97a3b 543}
c801d85f 544
cd743a6f
RR
545//-----------------------------------------------------------------------------
546// "deselect"
547//-----------------------------------------------------------------------------
548
549static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
550{
acfd422a
RR
551 if (g_isIdle) wxapp_install_idle_handler();
552
cd743a6f
RR
553 int id = menu->FindMenuIdByMenuItem(widget);
554
555 wxASSERT( id != -1 ); // should find it!
556
c626a8b7
VZ
557 if (!menu->IsEnabled(id))
558 return;
cd743a6f
RR
559
560 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
561 event.SetEventObject( menu );
562
c626a8b7
VZ
563 if (menu->GetEventHandler()->ProcessEvent(event))
564 return;
cd743a6f
RR
565
566 wxWindow *win = menu->GetInvokingWindow();
c626a8b7
VZ
567 if (win)
568 win->GetEventHandler()->ProcessEvent( event );
cd743a6f
RR
569}
570
cf7a7e13 571//-----------------------------------------------------------------------------
db1b4961 572// wxMenuItem
cf7a7e13
RR
573//-----------------------------------------------------------------------------
574
c801d85f 575IMPLEMENT_DYNAMIC_CLASS(wxMenuItem,wxObject)
96fd301f
VZ
576
577wxMenuItem::wxMenuItem()
c801d85f 578{
83624f79
RR
579 m_id = ID_SEPARATOR;
580 m_isCheckMenu = FALSE;
581 m_isChecked = FALSE;
582 m_isEnabled = TRUE;
583 m_subMenu = (wxMenu *) NULL;
584 m_menuItem = (GtkWidget *) NULL;
6de97a3b 585}
c801d85f 586
c626a8b7 587// it's valid for this function to be called even if m_menuItem == NULL
83624f79 588void wxMenuItem::SetName( const wxString& str )
716b7364 589{
ab46dc18 590 /* '\t' is the deliminator indicating a hot key */
b019151f 591 m_text = _T("");
ab46dc18
RR
592 const wxChar *pc = str;
593 for (; (*pc != _T('\0')) && (*pc != _T('\t')); pc++ )
83624f79 594 {
b019151f 595 if (*pc == _T('&'))
23280650 596 {
034be888 597#if (GTK_MINOR_VERSION > 0)
b019151f 598 m_text << _T('_');
572d7461
VZ
599 }
600 else if ( *pc == _T('_') ) // escape underscores
601 {
602 m_text << _T("__");
603 }
604 else if (*pc == _T('/')) /* we have to filter out slashes ... */
23280650 605 {
6bc8a1c8 606 m_text << _T('\\'); /* ... and replace them with back slashes */
034be888
RR
607#endif
608 }
d38ceae8
KB
609 else
610 m_text << *pc;
83624f79 611 }
23280650 612
837904f2 613 /* only GTK 1.2 knows about hot keys */
ab46dc18
RR
614 m_hotKey = _T("");
615#if (GTK_MINOR_VERSION > 0)
d7dbc98a
KB
616 if(*pc == _T('\t'))
617 {
618 pc++;
619 m_hotKey = pc;
620 }
ab46dc18 621#endif
96fd301f 622
83624f79
RR
623 if (m_menuItem)
624 {
625 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
1e133b7d 626 gtk_label_set( label, m_text.mb_str());
83624f79 627 }
716b7364
RR
628}
629
96fd301f 630void wxMenuItem::Check( bool check )
716b7364 631{
b019151f 632 wxCHECK_RET( m_menuItem, _T("invalid menu item") );
db1b4961 633
b019151f 634 wxCHECK_RET( IsCheckable(), _T("Can't check uncheckable item!") )
96fd301f 635
2d17d68f
RR
636 if (check == m_isChecked) return;
637
83624f79
RR
638 m_isChecked = check;
639 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
716b7364
RR
640}
641
8bbe427f
VZ
642void wxMenuItem::Enable( bool enable )
643{
b019151f 644 wxCHECK_RET( m_menuItem, _T("invalid menu item") );
db1b4961 645
83624f79
RR
646 gtk_widget_set_sensitive( m_menuItem, enable );
647 m_isEnabled = enable;
a9c96bcc
RR
648}
649
96fd301f 650bool wxMenuItem::IsChecked() const
716b7364 651{
b019151f 652 wxCHECK_MSG( m_menuItem, FALSE, _T("invalid menu item") );
db1b4961 653
83624f79 654 wxCHECK( IsCheckable(), FALSE ); // can't get state of uncheckable item!
96fd301f 655
83624f79 656 bool bIsChecked = ((GtkCheckMenuItem*)m_menuItem)->active != 0;
96fd301f 657
83624f79 658 return bIsChecked;
716b7364
RR
659}
660
db1b4961 661//-----------------------------------------------------------------------------
83624f79 662// wxMenu
db1b4961
RR
663//-----------------------------------------------------------------------------
664
c801d85f
KB
665IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
666
ae53c98c 667void
b908d224 668wxMenu::Init( const wxString& title,
33961d59 669 long style,
455fadaa
VZ
670 const wxFunction func
671 )
c801d85f 672{
83624f79
RR
673 m_title = title;
674 m_items.DeleteContents( TRUE );
675 m_invokingWindow = (wxWindow *) NULL;
ae53c98c 676 m_style = style;
23280650 677
034be888
RR
678#if (GTK_MINOR_VERSION > 0)
679 m_accel = gtk_accel_group_new();
680 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
681 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 682#else
83624f79 683 m_menu = gtk_menu_new(); // Do not show!
034be888 684#endif
8bbe427f 685
83624f79 686 m_callback = func;
b908d224 687
83624f79
RR
688 m_eventHandler = this;
689 m_clientData = (void*) NULL;
8bbe427f 690
b019151f
OK
691 if (m_title.IsNull()) m_title = _T("");
692 if (m_title != _T(""))
83624f79
RR
693 {
694 Append(-2, m_title);
695 AppendSeparator();
696 }
c626a8b7 697
2b1c162e 698 m_owner = (GtkWidget*) NULL;
2b2edbed
KB
699
700#if (GTK_MINOR_VERSION > 0)
701 /* Tearoffs are entries, just like separators. So if we want this
702 menu to be a tear-off one, we just append a tearoff entry
703 immediately. */
704 if(m_style & wxMENU_TEAROFF)
705 {
706 GtkItemFactoryEntry entry;
707 entry.path = "/tearoff";
708 entry.callback = (GtkItemFactoryCallback) NULL;
709 entry.callback_action = 0;
710 entry.item_type = "<Tearoff>";
711 entry.accelerator = (gchar*) NULL;
712 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 713 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed
KB
714 }
715#endif
6de97a3b 716}
c801d85f 717
034be888
RR
718wxMenu::~wxMenu()
719{
15a2076a
KB
720 /* how do we delete an item-factory ? */
721 gtk_widget_destroy( m_menu );
722
034be888
RR
723}
724
c2dd8380
GL
725void wxMenu::SetTitle( const wxString& title )
726{
c626a8b7 727 // TODO Waiting for something better
83624f79 728 m_title = title;
c2dd8380
GL
729}
730
731const wxString wxMenu::GetTitle() const
732{
83624f79 733 return m_title;
c2dd8380
GL
734}
735
96fd301f 736void wxMenu::AppendSeparator()
c801d85f 737{
83624f79
RR
738 wxMenuItem *mitem = new wxMenuItem();
739 mitem->SetId(ID_SEPARATOR);
96fd301f 740
08fc1744
RR
741#if (GTK_MINOR_VERSION > 0)
742 GtkItemFactoryEntry entry;
743 entry.path = "/sep";
744 entry.callback = (GtkItemFactoryCallback) NULL;
745 entry.callback_action = 0;
746 entry.item_type = "<Separator>";
747 entry.accelerator = (gchar*) NULL;
23280650 748
08fc1744 749 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 750
08fc1744
RR
751 /* this will be wrong for more than one separator. do we care? */
752 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
753#else
83624f79
RR
754 GtkWidget *menuItem = gtk_menu_item_new();
755 gtk_menu_append( GTK_MENU(m_menu), menuItem );
756 gtk_widget_show( menuItem );
08fc1744 757#endif
23280650 758
83624f79
RR
759 mitem->SetMenuItem(menuItem);
760 m_items.Append( mitem );
6de97a3b 761}
c801d85f 762
fd9811b1 763#if (GTK_MINOR_VERSION > 0)
837904f2
RR
764static char* GetHotKey( const wxString &hotkey, char *hotbuf )
765{
766 if (hotkey.IsEmpty()) return (char*) NULL;
23280650 767
837904f2
RR
768 switch (hotkey[0])
769 {
770 case _T('a'): /* Alt */
23280650
VZ
771 case _T('A'):
772 case _T('m'): /* Meta */
773 case _T('M'):
774 {
775 strcpy( hotbuf, "<alt>" );
776 wxString last = hotkey.Right(1);
777 strcat( hotbuf, last.mb_str() );
837904f2 778 return hotbuf;
23280650
VZ
779 }
780 case _T('c'): /* Ctrl */
781 case _T('C'):
782 case _T('s'): /* Strg, yeah man, I'm German */
783 case _T('S'):
784 {
785 strcpy( hotbuf, "<control>" );
786 wxString last = hotkey.Right(1);
787 strcat( hotbuf, last.mb_str() );
837904f2
RR
788 return hotbuf;
789 }
23280650
VZ
790 case _T('F'): /* function keys */
791 {
792 strcpy( hotbuf, hotkey.mb_str() );
837904f2 793 return hotbuf;
23280650
VZ
794 }
795 default:
796 {
797 }
837904f2
RR
798 }
799 return (char*) NULL;
800}
fd9811b1 801#endif
837904f2 802
debe6624 803void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f 804{
83624f79
RR
805 wxMenuItem *mitem = new wxMenuItem();
806 mitem->SetId(id);
807 mitem->SetText(item);
808 mitem->SetHelp(helpStr);
809 mitem->SetCheckable(checkable);
23280650 810
034be888 811#if (GTK_MINOR_VERSION > 0)
23280650 812 /* text has "_" instead of "&" after mitem->SetText() */
1e133b7d 813 wxString text( mitem->GetText() );
23280650 814
1e133b7d
RR
815 /* local buffer in multibyte form */
816 char buf[200];
817 strcpy( buf, "/" );
818 strcat( buf, text.mb_str() );
23280650 819
034be888 820 GtkItemFactoryEntry entry;
1e133b7d 821 entry.path = buf;
034be888
RR
822 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
823 entry.callback_action = 0;
824 if (checkable)
825 entry.item_type = "<CheckItem>";
826 else
827 entry.item_type = "<Item>";
23280650 828
ab46dc18 829 char hotbuf[50];
837904f2 830 entry.accelerator = GetHotKey( mitem->GetHotKey(), hotbuf );
23280650 831
034be888 832 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 833
034be888 834 /* in order to get the pointer to the item we need the item text _without_ underscores */
b019151f
OK
835 wxString s = _T("<main>/");
836 for ( const wxChar *pc = text; *pc != _T('\0'); pc++ )
034be888 837 {
572d7461 838 while (*pc == _T('_')) pc++; /* skip it */
034be888
RR
839 s << *pc;
840 }
23280650 841
1e133b7d 842 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() );
23280650 843
034be888
RR
844#else
845
d38ceae8
KB
846 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
847 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
23280650 848
83624f79
RR
849 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
850 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
034be888 851 (gpointer)this );
23280650 852
034be888
RR
853 gtk_menu_append( GTK_MENU(m_menu), menuItem );
854 gtk_widget_show( menuItem );
23280650 855
034be888 856#endif
96fd301f 857
83624f79
RR
858 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
859 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
034be888 860 (gpointer)this );
cf7a7e13 861
cd743a6f
RR
862 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
863 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
034be888
RR
864 (gpointer)this );
865
866 mitem->SetMenuItem(menuItem);
cd743a6f 867
83624f79 868 m_items.Append( mitem );
6de97a3b 869}
c801d85f 870
837904f2 871void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr )
c801d85f 872{
83624f79
RR
873 wxMenuItem *mitem = new wxMenuItem();
874 mitem->SetId(id);
837904f2 875 mitem->SetText(item);
828f655f 876 mitem->SetHelp(helpStr);
96fd301f 877
837904f2 878#if (GTK_MINOR_VERSION > 0)
23280650 879 /* text has "_" instead of "&" after mitem->SetText() */
837904f2 880 wxString text( mitem->GetText() );
23280650 881
837904f2
RR
882 /* local buffer in multibyte form */
883 char buf[200];
884 strcpy( buf, "/" );
885 strcat( buf, text.mb_str() );
23280650 886
837904f2
RR
887 GtkItemFactoryEntry entry;
888 entry.path = buf;
889 entry.callback = (GtkItemFactoryCallback) 0;
890 entry.callback_action = 0;
2b2edbed 891 entry.item_type = "<Branch>";
23280650 892
837904f2 893 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 894
837904f2
RR
895 /* in order to get the pointer to the item we need the item text _without_ underscores */
896 wxString s = _T("<main>/");
897 for ( const wxChar *pc = text; *pc != _T('\0'); pc++ )
898 {
899 if (*pc == _T('_')) pc++; /* skip it */
900 s << *pc;
901 }
23280650 902
837904f2 903 GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() );
23280650 904
837904f2
RR
905#else
906
b019151f 907 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
23280650 908
837904f2
RR
909 gtk_menu_append( GTK_MENU(m_menu), menuItem );
910 gtk_widget_show( menuItem );
23280650
VZ
911
912#endif
96fd301f 913
cd743a6f
RR
914 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
915 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
916 (gpointer*)this );
917
918 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
919 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
920 (gpointer*)this );
921
83624f79 922 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
23280650 923
837904f2
RR
924 mitem->SetMenuItem(menuItem);
925 mitem->SetSubMenu(subMenu);
926
83624f79 927 m_items.Append( mitem );
6de97a3b 928}
c801d85f 929
828f655f
RR
930void wxMenu::Append( wxMenuItem *item )
931{
932 m_items.Append( item );
c626a8b7 933
828f655f
RR
934 GtkWidget *menuItem = (GtkWidget*) NULL;
935
c626a8b7 936 if (item->IsSeparator())
828f655f 937 menuItem = gtk_menu_item_new();
c626a8b7 938 else if (item->IsSubMenu())
b019151f 939 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
c626a8b7 940 else
b019151f
OK
941 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
942 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
828f655f
RR
943
944 if (!item->IsSeparator())
945 {
946 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
947 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
948 (gpointer*)this );
949
950 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
951 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
952 (gpointer*)this );
c626a8b7
VZ
953
954 if (!item->IsSubMenu())
955 {
828f655f
RR
956 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
957 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
958 (gpointer*)this );
c626a8b7 959 }
828f655f 960 }
c626a8b7 961
828f655f
RR
962 gtk_menu_append( GTK_MENU(m_menu), menuItem );
963 gtk_widget_show( menuItem );
964 item->SetMenuItem(menuItem);
965}
966
c801d85f
KB
967int wxMenu::FindItem( const wxString itemString ) const
968{
b019151f
OK
969 wxString s = _T("");
970 for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ )
83624f79 971 {
b019151f 972 if (*pc == _T('&'))
23280650
VZ
973 {
974 pc++; /* skip it */
034be888 975#if (GTK_MINOR_VERSION > 0)
b019151f 976 s << _T('_');
034be888
RR
977#endif
978 }
979 s << *pc;
980 }
96fd301f 981
83624f79
RR
982 wxNode *node = m_items.First();
983 while (node)
984 {
985 wxMenuItem *item = (wxMenuItem*)node->Data();
986 if (item->GetText() == s)
c626a8b7 987 {
83624f79 988 return item->GetId();
c626a8b7 989 }
83624f79
RR
990 node = node->Next();
991 }
96fd301f 992
c626a8b7 993 return wxNOT_FOUND;
6de97a3b 994}
c801d85f 995
96fd301f 996void wxMenu::Enable( int id, bool enable )
716b7364 997{
83624f79 998 wxMenuItem *item = FindItem(id);
c626a8b7 999
b019151f 1000 wxCHECK_RET( item, _T("wxMenu::Enable: no such item") );
c626a8b7
VZ
1001
1002 item->Enable(enable);
6de97a3b 1003}
716b7364 1004
96fd301f 1005bool wxMenu::IsEnabled( int id ) const
e2414cbe 1006{
83624f79 1007 wxMenuItem *item = FindItem(id);
c626a8b7 1008
b019151f 1009 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") );
c626a8b7
VZ
1010
1011 return item->IsEnabled();
6de97a3b 1012}
e2414cbe 1013
96fd301f 1014void wxMenu::Check( int id, bool enable )
c801d85f 1015{
83624f79 1016 wxMenuItem *item = FindItem(id);
c626a8b7 1017
b019151f 1018 wxCHECK_RET( item, _T("wxMenu::Check: no such item") );
c626a8b7
VZ
1019
1020 item->Check(enable);
6de97a3b 1021}
c801d85f 1022
96fd301f 1023bool wxMenu::IsChecked( int id ) const
c801d85f 1024{
83624f79 1025 wxMenuItem *item = FindItem(id);
c626a8b7 1026
b019151f 1027 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") );
c626a8b7
VZ
1028
1029 return item->IsChecked();
6de97a3b 1030}
c801d85f 1031
debe6624 1032void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 1033{
83624f79 1034 wxMenuItem *item = FindItem(id);
c626a8b7 1035
b019151f 1036 wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") );
c626a8b7
VZ
1037
1038 item->SetText(label);
6de97a3b 1039}
96fd301f 1040
c33c4050
RR
1041wxString wxMenu::GetLabel( int id ) const
1042{
83624f79 1043 wxMenuItem *item = FindItem(id);
c626a8b7 1044
b019151f 1045 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") );
c626a8b7
VZ
1046
1047 return item->GetText();
c33c4050
RR
1048}
1049
1050void wxMenu::SetHelpString( int id, const wxString& helpString )
1051{
83624f79 1052 wxMenuItem *item = FindItem(id);
c626a8b7 1053
b019151f 1054 wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") );
c626a8b7
VZ
1055
1056 item->SetHelp( helpString );
c33c4050
RR
1057}
1058
1059wxString wxMenu::GetHelpString( int id ) const
1060{
83624f79 1061 wxMenuItem *item = FindItem(id);
c626a8b7 1062
b019151f 1063 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") );
c626a8b7
VZ
1064
1065 return item->GetHelp();
c33c4050
RR
1066}
1067
96fd301f
VZ
1068int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1069{
83624f79
RR
1070 wxNode *node = m_items.First();
1071 while (node)
1072 {
1073 wxMenuItem *item = (wxMenuItem*)node->Data();
1074 if (item->GetMenuItem() == menuItem)
1075 return item->GetId();
1076 node = node->Next();
1077 }
96fd301f 1078
c626a8b7 1079 return wxNOT_FOUND;
6de97a3b 1080}
c801d85f 1081
96fd301f 1082wxMenuItem *wxMenu::FindItem(int id) const
c801d85f 1083{
83624f79 1084 wxNode *node = m_items.First();
c626a8b7 1085 while (node)
83624f79
RR
1086 {
1087 wxMenuItem *item = (wxMenuItem*)node->Data();
1088 if (item->GetId() == id)
c626a8b7 1089 {
83624f79 1090 return item;
c626a8b7 1091 }
83624f79
RR
1092 node = node->Next();
1093 }
96fd301f 1094
83624f79
RR
1095 /* Not finding anything here can be correct
1096 * when search the entire menu system for
1097 * an entry -> no error message. */
8bbe427f 1098
83624f79 1099 return (wxMenuItem *) NULL;
96fd301f 1100}
c801d85f
KB
1101
1102void wxMenu::SetInvokingWindow( wxWindow *win )
1103{
83624f79 1104 m_invokingWindow = win;
6de97a3b 1105}
c801d85f 1106
96fd301f 1107wxWindow *wxMenu::GetInvokingWindow()
c801d85f 1108{
83624f79 1109 return m_invokingWindow;
6de97a3b 1110}
c801d85f 1111
c626a8b7
VZ
1112// Update a menu and all submenus recursively. source is the object that has
1113// the update event handlers defined for it. If NULL, the menu or associated
1114// window will be used.
631f1bfe
JS
1115void wxMenu::UpdateUI(wxEvtHandler* source)
1116{
1117 if (!source && GetInvokingWindow())
1118 source = GetInvokingWindow()->GetEventHandler();
1119 if (!source)
1120 source = GetEventHandler();
1121 if (!source)
1122 source = this;
1123
1124 wxNode* node = GetItems().First();
1125 while (node)
1126 {
1127 wxMenuItem* item = (wxMenuItem*) node->Data();
1128 if ( !item->IsSeparator() )
1129 {
1130 wxWindowID id = item->GetId();
1131 wxUpdateUIEvent event(id);
1132 event.SetEventObject( source );
1133
1134 if (source->ProcessEvent(event))
1135 {
1136 if (event.GetSetText())
1137 SetLabel(id, event.GetText());
1138 if (event.GetSetChecked())
1139 Check(id, event.GetChecked());
1140 if (event.GetSetEnabled())
1141 Enable(id, event.GetEnabled());
1142 }
1143
1144 if (item->GetSubMenu())
1145 item->GetSubMenu()->UpdateUI(source);
1146 }
1147 node = node->Next();
1148 }
1149}
1150