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