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