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