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