]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/menu.cpp
Removed bug that made wxWindow call OnPaint
[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;
23280650 41
16bcc879 42 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, style, "menu" );
3502e687
RR
43
44 m_menus.DeleteContents( TRUE );
45
1e133b7d
RR
46 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
47#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
48 m_accel = gtk_accel_group_new();
49 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
50 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 51#else
3502e687 52 m_menubar = gtk_menu_bar_new();
1e133b7d 53#endif
3502e687
RR
54
55 if (style & wxMB_DOCKABLE)
56 {
57 m_widget = gtk_handle_box_new();
c626a8b7
VZ
58 gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_menubar) );
59 gtk_widget_show( GTK_WIDGET(m_menubar) );
3502e687
RR
60 }
61 else
62 {
63 m_widget = GTK_WIDGET(m_menubar);
64 }
65
66 PostCreation();
3502e687
RR
67}
68
96fd301f 69wxMenuBar::wxMenuBar()
c801d85f 70{
1e133b7d
RR
71 /* the parent window is known after wxFrame::SetMenu() */
72 m_needParent = FALSE;
ae53c98c 73 m_style = 0;
23280650 74
83624f79 75 PreCreation( (wxWindow *) NULL, -1, wxDefaultPosition, wxDefaultSize, 0, "menu" );
c801d85f 76
83624f79 77 m_menus.DeleteContents( TRUE );
96fd301f 78
1e133b7d
RR
79 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
80#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
81 m_accel = gtk_accel_group_new();
82 m_factory = gtk_item_factory_new( GTK_TYPE_MENU_BAR, "<main>", m_accel );
83 m_menubar = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 84#else
83624f79 85 m_menubar = gtk_menu_bar_new();
1e133b7d 86#endif
8bbe427f 87
828f655f 88 m_widget = GTK_WIDGET(m_menubar);
96fd301f 89
83624f79 90 PostCreation();
6de97a3b 91}
c801d85f 92
1e133b7d
RR
93wxMenuBar::~wxMenuBar()
94{
95 // how to destroy a GtkItemFactory ?
96}
97
5bd9e519
RR
98static void wxMenubarUnsetInvokingWindow( wxMenu *menu, wxWindow *win )
99{
100 menu->SetInvokingWindow( (wxWindow*) NULL );
101
102#if (GTK_MINOR_VERSION > 0)
103 wxWindow *top_frame = win;
104 while (top_frame->GetParent()) top_frame = top_frame->GetParent();
105
106 /* support for native hot keys */
107 gtk_accel_group_detach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
108#endif
109
110 wxNode *node = menu->GetItems().First();
111 while (node)
112 {
113 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
114 if (menuitem->IsSubMenu())
115 wxMenubarUnsetInvokingWindow( menuitem->GetSubMenu(), win );
116 node = node->Next();
117 }
118}
119
120static void wxMenubarSetInvokingWindow( wxMenu *menu, wxWindow *win )
121{
122 menu->SetInvokingWindow( win );
123
124#if (GTK_MINOR_VERSION > 0)
125 wxWindow *top_frame = win;
126 while (top_frame->GetParent())
127 top_frame = top_frame->GetParent();
128
129 /* support for native hot keys */
130 gtk_accel_group_attach( menu->m_accel, GTK_OBJECT(top_frame->m_widget) );
131#endif
132
133 wxNode *node = menu->GetItems().First();
134 while (node)
135 {
136 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
137 if (menuitem->IsSubMenu())
138 wxMenubarSetInvokingWindow( menuitem->GetSubMenu(), win );
139 node = node->Next();
140 }
141}
142
143void wxMenuBar::SetInvokingWindow( wxWindow *win )
144{
145#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
146 wxWindow *top_frame = win;
147 while (top_frame->GetParent())
148 top_frame = top_frame->GetParent();
149
150 /* support for native key accelerators indicated by underscroes */
151 gtk_accel_group_attach( m_accel, GTK_OBJECT(top_frame->m_widget) );
152#endif
153
154 wxNode *node = m_menus.First();
155 while (node)
156 {
157 wxMenu *menu = (wxMenu*)node->Data();
158 wxMenubarSetInvokingWindow( menu, win );
159 node = node->Next();
160 }
161}
162
163void wxMenuBar::UnsetInvokingWindow( wxWindow *win )
164{
165#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
166 wxWindow *top_frame = win;
167 while (top_frame->GetParent())
168 top_frame = top_frame->GetParent();
169
170 /* support for native key accelerators indicated by underscroes */
171 gtk_accel_group_detach( m_accel, GTK_OBJECT(top_frame->m_widget) );
172#endif
173
174 wxNode *node = m_menus.First();
175 while (node)
176 {
177 wxMenu *menu = (wxMenu*)node->Data();
178 wxMenubarUnsetInvokingWindow( menu, win );
179 node = node->Next();
180 }
181}
182
c801d85f
KB
183void wxMenuBar::Append( wxMenu *menu, const wxString &title )
184{
83624f79 185 m_menus.Append( menu );
23280650 186
a533f5c1 187 const wxChar *pc;
23280650 188
1e133b7d
RR
189 /* GTK 1.2 wants to have "_" instead of "&" for accelerators */
190 wxString str;
a533f5c1 191 for ( pc = title; *pc != _T('\0'); pc++ )
83624f79 192 {
b019151f 193 if (*pc == _T('&'))
23280650 194 {
1e133b7d
RR
195#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
196 str << _T('_');
23280650
VZ
197 } else
198 if (*pc == _T('/'))
199 {
fac4253c 200 str << _T('\\');
034be888 201#endif
23280650 202 }
d38ceae8
KB
203 else
204 str << *pc;
1e133b7d
RR
205 }
206
207 /* this doesn't have much effect right now */
208 menu->SetTitle( str );
23280650 209
1e133b7d
RR
210 /* GTK 1.2.0 doesn't have gtk_item_factory_get_item(), but GTK 1.2.1 has. */
211#if (GTK_MINOR_VERSION > 0) && (GTK_MICRO_VERSION > 0)
212
213 /* local buffer in multibyte form */
2b2edbed
KB
214 wxString buf;
215 buf << '/' << str.mb_str();
216 char *cbuf = new char[buf.Length()];
1e133b7d 217 GtkItemFactoryEntry entry;
23280650 218 entry.path = (gchar *)buf.c_str(); // const_cast
1e133b7d
RR
219 entry.accelerator = (gchar*) NULL;
220 entry.callback = (GtkItemFactoryCallback) NULL;
221 entry.callback_action = 0;
2b2edbed
KB
222 entry.item_type = "<Branch>";
223
1e133b7d 224 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
1e133b7d
RR
225 /* in order to get the pointer to the item we need the item text _without_ underscores */
226 wxString tmp = _T("<main>/");
a533f5c1 227 for ( pc = str; *pc != _T('\0'); pc++ )
1e133b7d 228 {
2b2edbed
KB
229 if (*pc == _T('_')) pc++; /* skip it */
230 tmp << *pc;
034be888 231 }
1e133b7d 232 menu->m_owner = gtk_item_factory_get_item( m_factory, tmp.mb_str() );
1e133b7d 233 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menu->m_owner), menu->m_menu );
2b2edbed 234 delete [] cbuf;
1e133b7d 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 );
23280650 242
1e133b7d 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('&'))
23280650 568 {
034be888 569#if (GTK_MINOR_VERSION > 0)
b019151f 570 m_text << _T('_');
23280650
VZ
571 } else
572 if (*pc == _T('/')) /* we have to filter out slashes ... */
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 }
23280650 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
ae53c98c
KB
635wxMenu::wxMenu( const wxString& title, const wxFunction func, long style )
636{
637 Init(title, func, style);
638}
639
640wxMenu::wxMenu(long style)
641{
642 Init(wxEmptyString, (wxFunction) NULL, style);
643}
644
645void
646wxMenu::Init( const wxString& title, const wxFunction func, long style )
c801d85f 647{
83624f79
RR
648 m_title = title;
649 m_items.DeleteContents( TRUE );
650 m_invokingWindow = (wxWindow *) NULL;
ae53c98c 651 m_style = style;
23280650 652
034be888
RR
653#if (GTK_MINOR_VERSION > 0)
654 m_accel = gtk_accel_group_new();
655 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
656 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
23280650 657#else
83624f79 658 m_menu = gtk_menu_new(); // Do not show!
034be888 659#endif
8bbe427f 660
83624f79
RR
661 m_callback = func;
662 m_eventHandler = this;
663 m_clientData = (void*) NULL;
8bbe427f 664
b019151f
OK
665 if (m_title.IsNull()) m_title = _T("");
666 if (m_title != _T(""))
83624f79
RR
667 {
668 Append(-2, m_title);
669 AppendSeparator();
670 }
c626a8b7 671
2b1c162e 672 m_owner = (GtkWidget*) NULL;
2b2edbed
KB
673
674#if (GTK_MINOR_VERSION > 0)
675 /* Tearoffs are entries, just like separators. So if we want this
676 menu to be a tear-off one, we just append a tearoff entry
677 immediately. */
678 if(m_style & wxMENU_TEAROFF)
679 {
680 GtkItemFactoryEntry entry;
681 entry.path = "/tearoff";
682 entry.callback = (GtkItemFactoryCallback) NULL;
683 entry.callback_action = 0;
684 entry.item_type = "<Tearoff>";
685 entry.accelerator = (gchar*) NULL;
686 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 687 //GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/tearoff" );
2b2edbed
KB
688 }
689#endif
6de97a3b 690}
c801d85f 691
034be888
RR
692wxMenu::~wxMenu()
693{
694 /* how do we delete an item-factory ? */
695}
696
c2dd8380
GL
697void wxMenu::SetTitle( const wxString& title )
698{
c626a8b7 699 // TODO Waiting for something better
83624f79 700 m_title = title;
c2dd8380
GL
701}
702
703const wxString wxMenu::GetTitle() const
704{
83624f79 705 return m_title;
c2dd8380
GL
706}
707
96fd301f 708void wxMenu::AppendSeparator()
c801d85f 709{
83624f79
RR
710 wxMenuItem *mitem = new wxMenuItem();
711 mitem->SetId(ID_SEPARATOR);
96fd301f 712
08fc1744
RR
713#if (GTK_MINOR_VERSION > 0)
714 GtkItemFactoryEntry entry;
715 entry.path = "/sep";
716 entry.callback = (GtkItemFactoryCallback) NULL;
717 entry.callback_action = 0;
718 entry.item_type = "<Separator>";
719 entry.accelerator = (gchar*) NULL;
23280650 720
08fc1744 721 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 722
08fc1744
RR
723 /* this will be wrong for more than one separator. do we care? */
724 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
725#else
83624f79
RR
726 GtkWidget *menuItem = gtk_menu_item_new();
727 gtk_menu_append( GTK_MENU(m_menu), menuItem );
728 gtk_widget_show( menuItem );
08fc1744 729#endif
23280650 730
83624f79
RR
731 mitem->SetMenuItem(menuItem);
732 m_items.Append( mitem );
6de97a3b 733}
c801d85f 734
837904f2
RR
735static char* GetHotKey( const wxString &hotkey, char *hotbuf )
736{
737 if (hotkey.IsEmpty()) return (char*) NULL;
23280650 738
837904f2
RR
739 switch (hotkey[0])
740 {
741 case _T('a'): /* Alt */
23280650
VZ
742 case _T('A'):
743 case _T('m'): /* Meta */
744 case _T('M'):
745 {
746 strcpy( hotbuf, "<alt>" );
747 wxString last = hotkey.Right(1);
748 strcat( hotbuf, last.mb_str() );
837904f2 749 return hotbuf;
23280650
VZ
750 }
751 case _T('c'): /* Ctrl */
752 case _T('C'):
753 case _T('s'): /* Strg, yeah man, I'm German */
754 case _T('S'):
755 {
756 strcpy( hotbuf, "<control>" );
757 wxString last = hotkey.Right(1);
758 strcat( hotbuf, last.mb_str() );
837904f2
RR
759 return hotbuf;
760 }
23280650
VZ
761 case _T('F'): /* function keys */
762 {
763 strcpy( hotbuf, hotkey.mb_str() );
837904f2 764 return hotbuf;
23280650
VZ
765 }
766 default:
767 {
768 }
837904f2
RR
769 }
770 return (char*) NULL;
771}
772
debe6624 773void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
c801d85f 774{
83624f79
RR
775 wxMenuItem *mitem = new wxMenuItem();
776 mitem->SetId(id);
777 mitem->SetText(item);
778 mitem->SetHelp(helpStr);
779 mitem->SetCheckable(checkable);
23280650 780
034be888 781#if (GTK_MINOR_VERSION > 0)
23280650 782 /* text has "_" instead of "&" after mitem->SetText() */
1e133b7d 783 wxString text( mitem->GetText() );
23280650 784
1e133b7d
RR
785 /* local buffer in multibyte form */
786 char buf[200];
787 strcpy( buf, "/" );
788 strcat( buf, text.mb_str() );
23280650 789
034be888 790 GtkItemFactoryEntry entry;
1e133b7d 791 entry.path = buf;
034be888
RR
792 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
793 entry.callback_action = 0;
794 if (checkable)
795 entry.item_type = "<CheckItem>";
796 else
797 entry.item_type = "<Item>";
23280650 798
ab46dc18 799 char hotbuf[50];
837904f2 800 entry.accelerator = GetHotKey( mitem->GetHotKey(), hotbuf );
23280650 801
034be888 802 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 803
034be888 804 /* in order to get the pointer to the item we need the item text _without_ underscores */
b019151f
OK
805 wxString s = _T("<main>/");
806 for ( const wxChar *pc = text; *pc != _T('\0'); pc++ )
034be888 807 {
b019151f 808 if (*pc == _T('_')) pc++; /* skip it */
034be888
RR
809 s << *pc;
810 }
23280650 811
1e133b7d 812 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() );
23280650 813
034be888
RR
814#else
815
d38ceae8
KB
816 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
817 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
23280650 818
83624f79
RR
819 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
820 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
034be888 821 (gpointer)this );
23280650 822
034be888
RR
823 gtk_menu_append( GTK_MENU(m_menu), menuItem );
824 gtk_widget_show( menuItem );
23280650 825
034be888 826#endif
96fd301f 827
83624f79
RR
828 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
829 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
034be888 830 (gpointer)this );
cf7a7e13 831
cd743a6f
RR
832 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
833 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
034be888
RR
834 (gpointer)this );
835
836 mitem->SetMenuItem(menuItem);
cd743a6f 837
83624f79 838 m_items.Append( mitem );
6de97a3b 839}
c801d85f 840
837904f2 841void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr )
c801d85f 842{
83624f79
RR
843 wxMenuItem *mitem = new wxMenuItem();
844 mitem->SetId(id);
837904f2 845 mitem->SetText(item);
828f655f 846 mitem->SetHelp(helpStr);
96fd301f 847
837904f2 848#if (GTK_MINOR_VERSION > 0)
23280650 849 /* text has "_" instead of "&" after mitem->SetText() */
837904f2 850 wxString text( mitem->GetText() );
23280650 851
837904f2
RR
852 /* local buffer in multibyte form */
853 char buf[200];
854 strcpy( buf, "/" );
855 strcat( buf, text.mb_str() );
23280650 856
837904f2
RR
857 GtkItemFactoryEntry entry;
858 entry.path = buf;
859 entry.callback = (GtkItemFactoryCallback) 0;
860 entry.callback_action = 0;
2b2edbed 861 entry.item_type = "<Branch>";
23280650 862
837904f2 863 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
23280650 864
837904f2
RR
865 /* in order to get the pointer to the item we need the item text _without_ underscores */
866 wxString s = _T("<main>/");
867 for ( const wxChar *pc = text; *pc != _T('\0'); pc++ )
868 {
869 if (*pc == _T('_')) pc++; /* skip it */
870 s << *pc;
871 }
23280650 872
837904f2 873 GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() );
23280650 874
837904f2
RR
875#else
876
b019151f 877 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
23280650 878
837904f2
RR
879 gtk_menu_append( GTK_MENU(m_menu), menuItem );
880 gtk_widget_show( menuItem );
23280650
VZ
881
882#endif
96fd301f 883
cd743a6f
RR
884 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
885 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
886 (gpointer*)this );
887
888 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
889 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
890 (gpointer*)this );
891
83624f79 892 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
23280650 893
837904f2
RR
894 mitem->SetMenuItem(menuItem);
895 mitem->SetSubMenu(subMenu);
896
83624f79 897 m_items.Append( mitem );
6de97a3b 898}
c801d85f 899
828f655f
RR
900void wxMenu::Append( wxMenuItem *item )
901{
902 m_items.Append( item );
c626a8b7 903
828f655f
RR
904 GtkWidget *menuItem = (GtkWidget*) NULL;
905
c626a8b7 906 if (item->IsSeparator())
828f655f 907 menuItem = gtk_menu_item_new();
c626a8b7 908 else if (item->IsSubMenu())
b019151f 909 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
c626a8b7 910 else
b019151f
OK
911 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
912 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
828f655f
RR
913
914 if (!item->IsSeparator())
915 {
916 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
917 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
918 (gpointer*)this );
919
920 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
921 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
922 (gpointer*)this );
c626a8b7
VZ
923
924 if (!item->IsSubMenu())
925 {
828f655f
RR
926 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
927 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
928 (gpointer*)this );
c626a8b7 929 }
828f655f 930 }
c626a8b7 931
828f655f
RR
932 gtk_menu_append( GTK_MENU(m_menu), menuItem );
933 gtk_widget_show( menuItem );
934 item->SetMenuItem(menuItem);
935}
936
c801d85f
KB
937int wxMenu::FindItem( const wxString itemString ) const
938{
b019151f
OK
939 wxString s = _T("");
940 for ( const wxChar *pc = itemString; *pc != _T('\0'); pc++ )
83624f79 941 {
b019151f 942 if (*pc == _T('&'))
23280650
VZ
943 {
944 pc++; /* skip it */
034be888 945#if (GTK_MINOR_VERSION > 0)
b019151f 946 s << _T('_');
034be888
RR
947#endif
948 }
949 s << *pc;
950 }
96fd301f 951
83624f79
RR
952 wxNode *node = m_items.First();
953 while (node)
954 {
955 wxMenuItem *item = (wxMenuItem*)node->Data();
956 if (item->GetText() == s)
c626a8b7 957 {
83624f79 958 return item->GetId();
c626a8b7 959 }
83624f79
RR
960 node = node->Next();
961 }
96fd301f 962
c626a8b7 963 return wxNOT_FOUND;
6de97a3b 964}
c801d85f 965
96fd301f 966void wxMenu::Enable( int id, bool enable )
716b7364 967{
83624f79 968 wxMenuItem *item = FindItem(id);
c626a8b7 969
b019151f 970 wxCHECK_RET( item, _T("wxMenu::Enable: no such item") );
c626a8b7
VZ
971
972 item->Enable(enable);
6de97a3b 973}
716b7364 974
96fd301f 975bool wxMenu::IsEnabled( int id ) const
e2414cbe 976{
83624f79 977 wxMenuItem *item = FindItem(id);
c626a8b7 978
b019151f 979 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsEnabled: no such item") );
c626a8b7
VZ
980
981 return item->IsEnabled();
6de97a3b 982}
e2414cbe 983
96fd301f 984void wxMenu::Check( int id, bool enable )
c801d85f 985{
83624f79 986 wxMenuItem *item = FindItem(id);
c626a8b7 987
b019151f 988 wxCHECK_RET( item, _T("wxMenu::Check: no such item") );
c626a8b7
VZ
989
990 item->Check(enable);
6de97a3b 991}
c801d85f 992
96fd301f 993bool wxMenu::IsChecked( int id ) const
c801d85f 994{
83624f79 995 wxMenuItem *item = FindItem(id);
c626a8b7 996
b019151f 997 wxCHECK_MSG( item, FALSE, _T("wxMenu::IsChecked: no such item") );
c626a8b7
VZ
998
999 return item->IsChecked();
6de97a3b 1000}
c801d85f 1001
debe6624 1002void wxMenu::SetLabel( int id, const wxString &label )
c801d85f 1003{
83624f79 1004 wxMenuItem *item = FindItem(id);
c626a8b7 1005
b019151f 1006 wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") );
c626a8b7
VZ
1007
1008 item->SetText(label);
6de97a3b 1009}
96fd301f 1010
c33c4050
RR
1011wxString wxMenu::GetLabel( int id ) const
1012{
83624f79 1013 wxMenuItem *item = FindItem(id);
c626a8b7 1014
b019151f 1015 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetLabel: no such item") );
c626a8b7
VZ
1016
1017 return item->GetText();
c33c4050
RR
1018}
1019
1020void wxMenu::SetHelpString( int id, const wxString& helpString )
1021{
83624f79 1022 wxMenuItem *item = FindItem(id);
c626a8b7 1023
b019151f 1024 wxCHECK_RET( item, _T("wxMenu::SetHelpString: no such item") );
c626a8b7
VZ
1025
1026 item->SetHelp( helpString );
c33c4050
RR
1027}
1028
1029wxString wxMenu::GetHelpString( int id ) const
1030{
83624f79 1031 wxMenuItem *item = FindItem(id);
c626a8b7 1032
b019151f 1033 wxCHECK_MSG( item, _T(""), _T("wxMenu::GetHelpString: no such item") );
c626a8b7
VZ
1034
1035 return item->GetHelp();
c33c4050
RR
1036}
1037
96fd301f
VZ
1038int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1039{
83624f79
RR
1040 wxNode *node = m_items.First();
1041 while (node)
1042 {
1043 wxMenuItem *item = (wxMenuItem*)node->Data();
1044 if (item->GetMenuItem() == menuItem)
1045 return item->GetId();
1046 node = node->Next();
1047 }
96fd301f 1048
c626a8b7 1049 return wxNOT_FOUND;
6de97a3b 1050}
c801d85f 1051
96fd301f 1052wxMenuItem *wxMenu::FindItem(int id) const
c801d85f 1053{
83624f79 1054 wxNode *node = m_items.First();
c626a8b7 1055 while (node)
83624f79
RR
1056 {
1057 wxMenuItem *item = (wxMenuItem*)node->Data();
1058 if (item->GetId() == id)
c626a8b7 1059 {
83624f79 1060 return item;
c626a8b7 1061 }
83624f79
RR
1062 node = node->Next();
1063 }
96fd301f 1064
83624f79
RR
1065 /* Not finding anything here can be correct
1066 * when search the entire menu system for
1067 * an entry -> no error message. */
8bbe427f 1068
83624f79 1069 return (wxMenuItem *) NULL;
96fd301f 1070}
c801d85f
KB
1071
1072void wxMenu::SetInvokingWindow( wxWindow *win )
1073{
83624f79 1074 m_invokingWindow = win;
6de97a3b 1075}
c801d85f 1076
96fd301f 1077wxWindow *wxMenu::GetInvokingWindow()
c801d85f 1078{
83624f79 1079 return m_invokingWindow;
6de97a3b 1080}
c801d85f 1081
c626a8b7
VZ
1082// Update a menu and all submenus recursively. source is the object that has
1083// the update event handlers defined for it. If NULL, the menu or associated
1084// window will be used.
631f1bfe
JS
1085void wxMenu::UpdateUI(wxEvtHandler* source)
1086{
1087 if (!source && GetInvokingWindow())
1088 source = GetInvokingWindow()->GetEventHandler();
1089 if (!source)
1090 source = GetEventHandler();
1091 if (!source)
1092 source = this;
1093
1094 wxNode* node = GetItems().First();
1095 while (node)
1096 {
1097 wxMenuItem* item = (wxMenuItem*) node->Data();
1098 if ( !item->IsSeparator() )
1099 {
1100 wxWindowID id = item->GetId();
1101 wxUpdateUIEvent event(id);
1102 event.SetEventObject( source );
1103
1104 if (source->ProcessEvent(event))
1105 {
1106 if (event.GetSetText())
1107 SetLabel(id, event.GetText());
1108 if (event.GetSetChecked())
1109 Check(id, event.GetChecked());
1110 if (event.GetSetEnabled())
1111 Enable(id, event.GetEnabled());
1112 }
1113
1114 if (item->GetSubMenu())
1115 item->GetSubMenu()->UpdateUI(source);
1116 }
1117 node = node->Next();
1118 }
1119}
1120