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