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