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