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