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