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