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