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