]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/menu.cpp
1. corrected uninitialised variable (which led to crash) in wxListBox
[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/menu.h"
16 #include "wx/log.h"
17 #include "wx/intl.h"
18 #include "wx/app.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 void 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
288 static int FindMenuItemRecursive( const wxMenu *menu, const wxString &menuString, const wxString &itemString )
289 {
290 if (menu->GetTitle() == menuString)
291 {
292 int res = menu->FindItem( itemString );
293 if (res != wxNOT_FOUND)
294 return res;
295 }
296
297 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
298 while (node)
299 {
300 wxMenuItem *item = (wxMenuItem*)node->Data();
301 if (item->IsSubMenu())
302 return FindMenuItemRecursive(item->GetSubMenu(), menuString, itemString);
303
304 node = node->Next();
305 }
306
307 return wxNOT_FOUND;
308 }
309
310 wxMenuItem *wxMenuBar::FindItemForId(int itemId, wxMenu **menuForItem ) const
311 {
312 if ( menuForItem )
313 {
314 // TODO return the pointer to the menu
315
316 *menuForItem = NULL;
317 }
318
319 return FindItem(itemId);
320 }
321
322 int wxMenuBar::FindMenuItem( const wxString &menuString, const wxString &itemString ) const
323 {
324 wxNode *node = m_menus.First();
325 while (node)
326 {
327 wxMenu *menu = (wxMenu*)node->Data();
328 int res = FindMenuItemRecursive( menu, menuString, itemString);
329 if (res != -1) return res;
330 node = node->Next();
331 }
332 return -1;
333 }
334
335 // Find a wxMenuItem using its id. Recurses down into sub-menus
336 static wxMenuItem* FindMenuItemByIdRecursive(const wxMenu* menu, int id)
337 {
338 wxMenuItem* result = menu->FindItem(id);
339
340 wxNode *node = ((wxMenu *)menu)->GetItems().First(); // const_cast
341 while ( node && result == NULL )
342 {
343 wxMenuItem *item = (wxMenuItem*)node->Data();
344 if (item->IsSubMenu())
345 {
346 result = FindMenuItemByIdRecursive( item->GetSubMenu(), id );
347 }
348 node = node->Next();
349 }
350
351 return result;
352 }
353
354 wxMenuItem* wxMenuBar::FindItem( int id ) const
355 {
356 wxMenuItem* result = 0;
357 wxNode *node = m_menus.First();
358 while (node && result == 0)
359 {
360 wxMenu *menu = (wxMenu*)node->Data();
361 result = FindMenuItemByIdRecursive( menu, id );
362 node = node->Next();
363 }
364
365 return result;
366 }
367
368 void wxMenuBar::Check( int id, bool check )
369 {
370 wxMenuItem* item = FindMenuItemById( id );
371
372 wxCHECK_RET( item, wxT("wxMenuBar::Check: no such item") );
373
374 item->Check(check);
375 }
376
377 bool wxMenuBar::IsChecked( int id ) const
378 {
379 wxMenuItem* item = FindMenuItemById( id );
380
381 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked: no such item") );
382
383 return item->IsChecked();
384 }
385
386 void wxMenuBar::Enable( int id, bool enable )
387 {
388 wxMenuItem* item = FindMenuItemById( id );
389
390 wxCHECK_RET( item, wxT("wxMenuBar::Enable: no such item") );
391
392 item->Enable(enable);
393 }
394
395 bool wxMenuBar::IsEnabled( int id ) const
396 {
397 wxMenuItem* item = FindMenuItemById( id );
398
399 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled: no such item") );
400
401 return item->IsEnabled();
402 }
403
404 wxString wxMenuBar::GetLabel( int id ) const
405 {
406 wxMenuItem* item = FindMenuItemById( id );
407
408 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel: no such item") );
409
410 return item->GetText();
411 }
412
413 void wxMenuBar::SetLabel( int id, const wxString &label )
414 {
415 wxMenuItem* item = FindMenuItemById( id );
416
417 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel: no such item") );
418
419 item->SetText( label );
420 }
421
422 void wxMenuBar::EnableTop( int pos, bool flag )
423 {
424 wxNode *node = m_menus.Nth( pos );
425
426 wxCHECK_RET( node, wxT("menu not found") );
427
428 wxMenu* menu = (wxMenu*)node->Data();
429
430 if (menu->m_owner)
431 gtk_widget_set_sensitive( menu->m_owner, flag );
432 }
433
434 wxString wxMenuBar::GetLabelTop( int pos ) const
435 {
436 wxNode *node = m_menus.Nth( pos );
437
438 wxCHECK_MSG( node, wxT("invalid"), wxT("menu not found") );
439
440 wxMenu* menu = (wxMenu*)node->Data();
441
442 return menu->GetTitle();
443 }
444
445 void wxMenuBar::SetLabelTop( int pos, const wxString& label )
446 {
447 wxNode *node = m_menus.Nth( pos );
448
449 wxCHECK_RET( node, wxT("menu not found") );
450
451 wxMenu* menu = (wxMenu*)node->Data();
452
453 menu->SetTitle( label );
454 }
455
456 void wxMenuBar::SetHelpString( int id, const wxString& helpString )
457 {
458 wxMenuItem* item = FindMenuItemById( id );
459
460 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString: no such item") );
461
462 item->SetHelp( helpString );
463 }
464
465 wxString wxMenuBar::GetHelpString( int id ) const
466 {
467 wxMenuItem* item = FindMenuItemById( id );
468
469 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString: no such item") );
470
471 return item->GetHelp();
472 }
473
474 //-----------------------------------------------------------------------------
475 // "activate"
476 //-----------------------------------------------------------------------------
477
478 static void gtk_menu_clicked_callback( GtkWidget *widget, wxMenu *menu )
479 {
480 if (g_isIdle) wxapp_install_idle_handler();
481
482 int id = menu->FindMenuIdByMenuItem(widget);
483
484 /* should find it for normal (not popup) menu */
485 wxASSERT( (id != -1) || (menu->GetInvokingWindow() != NULL) );
486
487 if (!menu->IsEnabled(id))
488 return;
489
490 wxMenuItem* item = menu->FindItem( id );
491 wxCHECK_RET( item, wxT("error in menu item callback") );
492
493 if (item->IsCheckable())
494 {
495 bool isReallyChecked = item->IsChecked();
496 if ( item->wxMenuItemBase::IsChecked() == isReallyChecked )
497 {
498 /* the menu item has been checked by calling wxMenuItem->Check() */
499 return;
500 }
501 else
502 {
503 /* the user pressed on the menu item -> report and make consistent
504 * again */
505 item->wxMenuItemBase::Check(isReallyChecked);
506 }
507 }
508
509 wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, id );
510 event.SetEventObject( menu );
511 event.SetInt(id );
512
513 if (menu->GetCallback())
514 {
515 (void) (*(menu->GetCallback())) (*menu, event);
516 return;
517 }
518
519 if (menu->GetEventHandler()->ProcessEvent(event))
520 return;
521
522 wxWindow *win = menu->GetInvokingWindow();
523 if (win)
524 win->GetEventHandler()->ProcessEvent( event );
525 }
526
527 //-----------------------------------------------------------------------------
528 // "select"
529 //-----------------------------------------------------------------------------
530
531 static void gtk_menu_hilight_callback( GtkWidget *widget, wxMenu *menu )
532 {
533 if (g_isIdle) wxapp_install_idle_handler();
534
535 int id = menu->FindMenuIdByMenuItem(widget);
536
537 wxASSERT( id != -1 ); // should find it!
538
539 if (!menu->IsEnabled(id))
540 return;
541
542 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, id );
543 event.SetEventObject( menu );
544
545 if (menu->GetEventHandler()->ProcessEvent(event))
546 return;
547
548 wxWindow *win = menu->GetInvokingWindow();
549 if (win) win->GetEventHandler()->ProcessEvent( event );
550 }
551
552 //-----------------------------------------------------------------------------
553 // "deselect"
554 //-----------------------------------------------------------------------------
555
556 static void gtk_menu_nolight_callback( GtkWidget *widget, wxMenu *menu )
557 {
558 if (g_isIdle) wxapp_install_idle_handler();
559
560 int id = menu->FindMenuIdByMenuItem(widget);
561
562 wxASSERT( id != -1 ); // should find it!
563
564 if (!menu->IsEnabled(id))
565 return;
566
567 wxMenuEvent event( wxEVT_MENU_HIGHLIGHT, -1 );
568 event.SetEventObject( menu );
569
570 if (menu->GetEventHandler()->ProcessEvent(event))
571 return;
572
573 wxWindow *win = menu->GetInvokingWindow();
574 if (win)
575 win->GetEventHandler()->ProcessEvent( event );
576 }
577
578 //-----------------------------------------------------------------------------
579 // wxMenuItem
580 //-----------------------------------------------------------------------------
581
582 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxMenuItemBase)
583
584 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
585 int id,
586 const wxString& name,
587 const wxString& help,
588 bool isCheckable,
589 wxMenu *subMenu)
590 {
591 return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu);
592 }
593
594 wxMenuItem::wxMenuItem(wxMenu *parentMenu,
595 int id,
596 const wxString& text,
597 const wxString& help,
598 bool isCheckable,
599 wxMenu *subMenu)
600 {
601 m_id = id;
602 m_isCheckable = isCheckable;
603 m_isChecked = FALSE;
604 m_isEnabled = TRUE;
605 m_subMenu = subMenu;
606 m_parentMenu = parentMenu;
607 m_help = help;
608
609 m_menuItem = (GtkWidget *) NULL;
610
611 // call it after initializing m_menuItem to NULL
612 DoSetText(text);
613 }
614
615 wxMenuItem::~wxMenuItem()
616 {
617 // don't delete menu items, the menus take care of that
618 }
619
620 // it's valid for this function to be called even if m_menuItem == NULL
621 void wxMenuItem::DoSetText( const wxString& str )
622 {
623 /* '\t' is the deliminator indicating a hot key */
624 m_text.Empty();
625 const wxChar *pc = str;
626 for (; (*pc != wxT('\0')) && (*pc != wxT('\t')); pc++ )
627 {
628 if (*pc == wxT('&'))
629 {
630 #if (GTK_MINOR_VERSION > 0)
631 m_text << wxT('_');
632 }
633 else if ( *pc == wxT('_') ) // escape underscores
634 {
635 m_text << wxT("__");
636 }
637 else if (*pc == wxT('/')) /* we have to filter out slashes ... */
638 {
639 m_text << wxT('\\'); /* ... and replace them with back slashes */
640 #endif
641 }
642 else
643 m_text << *pc;
644 }
645
646 /* only GTK 1.2 knows about hot keys */
647 m_hotKey = wxT("");
648 #if (GTK_MINOR_VERSION > 0)
649 if(*pc == wxT('\t'))
650 {
651 pc++;
652 m_hotKey = pc;
653 }
654 #endif
655
656 if (m_menuItem)
657 {
658 GtkLabel *label = GTK_LABEL( GTK_BIN(m_menuItem)->child );
659 gtk_label_set( label, m_text.mb_str());
660 }
661 }
662
663 void wxMenuItem::Check( bool check )
664 {
665 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
666
667 wxCHECK_RET( IsCheckable(), wxT("Can't check uncheckable item!") )
668
669 if (check == m_isChecked)
670 return;
671
672 wxMenuItemBase::Check( check );
673 gtk_check_menu_item_set_state( (GtkCheckMenuItem*)m_menuItem, (gint)check );
674 }
675
676 void wxMenuItem::Enable( bool enable )
677 {
678 wxCHECK_RET( m_menuItem, wxT("invalid menu item") );
679
680 gtk_widget_set_sensitive( m_menuItem, enable );
681 wxMenuItemBase::Enable( enable );
682 }
683
684 bool wxMenuItem::IsChecked() const
685 {
686 wxCHECK_MSG( m_menuItem, FALSE, wxT("invalid menu item") );
687
688 wxCHECK_MSG( IsCheckable(), FALSE,
689 wxT("can't get state of uncheckable item!") );
690
691 return ((GtkCheckMenuItem*)m_menuItem)->active != 0;
692 }
693
694 //-----------------------------------------------------------------------------
695 // wxMenu
696 //-----------------------------------------------------------------------------
697
698 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
699
700 void
701 wxMenu::Init( const wxString& title,
702 long style,
703 const wxFunction func
704 )
705 {
706 m_title = title;
707 m_items.DeleteContents( TRUE );
708 m_invokingWindow = (wxWindow *) NULL;
709 m_style = style;
710
711 #if (GTK_MINOR_VERSION > 0)
712 m_accel = gtk_accel_group_new();
713 m_factory = gtk_item_factory_new( GTK_TYPE_MENU, "<main>", m_accel );
714 m_menu = gtk_item_factory_get_widget( m_factory, "<main>" );
715 #else
716 m_menu = gtk_menu_new(); // Do not show!
717 #endif
718
719 m_callback = func;
720
721 m_eventHandler = this;
722 m_clientData = (void*) NULL;
723
724 if (m_title.IsNull()) m_title = wxT("");
725 if (m_title != wxT(""))
726 {
727 Append(-2, m_title);
728 AppendSeparator();
729 }
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
751 wxMenu::~wxMenu()
752 {
753 wxNode *node = m_items.First();
754 while (node)
755 {
756 wxMenuItem *item = (wxMenuItem*)node->Data();
757 wxMenu *submenu = item->GetSubMenu();
758 if (submenu)
759 delete submenu;
760 node = node->Next();
761 }
762
763 gtk_widget_destroy( m_menu );
764
765 gtk_object_unref( GTK_OBJECT(m_factory) );
766 }
767
768 void wxMenu::SetTitle( const wxString& title )
769 {
770 // TODO Waiting for something better
771 m_title = title;
772 }
773
774 const wxString wxMenu::GetTitle() const
775 {
776 return m_title;
777 }
778
779 void wxMenu::AppendSeparator()
780 {
781 wxMenuItem *mitem = new wxMenuItem(this, wxID_SEPARATOR);
782
783 #if (GTK_MINOR_VERSION > 0)
784 GtkItemFactoryEntry entry;
785 entry.path = "/sep";
786 entry.callback = (GtkItemFactoryCallback) NULL;
787 entry.callback_action = 0;
788 entry.item_type = "<Separator>";
789 entry.accelerator = (gchar*) NULL;
790
791 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
792
793 /* this will be wrong for more than one separator. do we care? */
794 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, "<main>/sep" );
795 #else
796 GtkWidget *menuItem = gtk_menu_item_new();
797 gtk_menu_append( GTK_MENU(m_menu), menuItem );
798 gtk_widget_show( menuItem );
799 #endif
800
801 mitem->SetMenuItem(menuItem);
802 m_items.Append( mitem );
803 }
804
805 #if (GTK_MINOR_VERSION > 0) && wxUSE_ACCEL
806 static wxString GetHotKey( const wxMenuItem& item )
807 {
808 wxString hotkey;
809
810 // as wxGetAccelFromString() looks for TAB, insert a dummy one here
811 wxString label;
812 label << wxT('\t') << item.GetHotKey();
813
814 // but if the hotkey is empty don't do anything
815 if ( label.length() > 1 )
816 {
817 wxAcceleratorEntry *accel = wxGetAccelFromString(label);
818 if ( accel )
819 {
820 int flags = accel->GetFlags();
821 if ( flags & wxACCEL_ALT )
822 hotkey += wxT("<alt>");
823 if ( flags & wxACCEL_CTRL )
824 hotkey += wxT("<control>");
825 if ( flags & wxACCEL_SHIFT )
826 hotkey += wxT("<shift>");
827
828 int code = accel->GetKeyCode();
829 switch ( code )
830 {
831 case WXK_F1:
832 case WXK_F2:
833 case WXK_F3:
834 case WXK_F4:
835 case WXK_F5:
836 case WXK_F6:
837 case WXK_F7:
838 case WXK_F8:
839 case WXK_F9:
840 case WXK_F10:
841 case WXK_F11:
842 case WXK_F12:
843 hotkey << wxT('F') << code = WXK_F1 + 1;
844 break;
845
846 // if there are any other keys wxGetAccelFromString() may return,
847 // we should process them here
848
849 default:
850 if ( wxIsalnum(code) )
851 {
852 hotkey << (wxChar)code;
853
854 break;
855 }
856
857 wxFAIL_MSG( wxT("unknown keyboard accel") );
858 }
859
860 delete accel;
861 }
862 }
863
864 return hotkey;
865 }
866 #endif // wxUSE_ACCEL
867
868 void wxMenu::Append( int id, const wxString &item, const wxString &helpStr, bool checkable )
869 {
870 wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, checkable);
871
872 #if (GTK_MINOR_VERSION > 0)
873 /* text has "_" instead of "&" after mitem->SetText() */
874 wxString text( mitem->GetText() );
875
876 /* local buffer in multibyte form */
877 char buf[200];
878 strcpy( buf, "/" );
879 strcat( buf, text.mb_str() );
880
881 GtkItemFactoryEntry entry;
882 entry.path = buf;
883 entry.callback = (GtkItemFactoryCallback) gtk_menu_clicked_callback;
884 entry.callback_action = 0;
885 if (checkable)
886 entry.item_type = "<CheckItem>";
887 else
888 entry.item_type = "<Item>";
889
890 #if wxUSE_ACCEL
891 // due to an apparent bug in GTK+, we have to use a static buffer here -
892 // otherwise GTK+ 1.2.2 manages to override the memory we pass to it
893 // somehow! (VZ)
894 static char s_accel[32]; // must be big enough for <control><alt><shift>F12
895 strncpy(s_accel, GetHotKey(*mitem).mb_str(), WXSIZEOF(s_accel));
896 entry.accelerator = s_accel;
897 #else
898 entry.accelerator = NULL;
899 #endif
900
901 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
902
903 /* in order to get the pointer to the item we need the item text _without_ underscores */
904 wxString s = wxT("<main>/");
905 for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ )
906 {
907 while (*pc == wxT('_')) pc++; /* skip it */
908 s << *pc;
909 }
910
911 GtkWidget *menuItem = gtk_item_factory_get_widget( m_factory, s.mb_str() );
912
913 #else
914
915 GtkWidget *menuItem = checkable ? gtk_check_menu_item_new_with_label( mitem->GetText().mb_str() )
916 : gtk_menu_item_new_with_label( mitem->GetText().mb_str() );
917
918 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
919 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
920 (gpointer)this );
921
922 gtk_menu_append( GTK_MENU(m_menu), menuItem );
923 gtk_widget_show( menuItem );
924
925 #endif
926
927 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
928 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
929 (gpointer)this );
930
931 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
932 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
933 (gpointer)this );
934
935 mitem->SetMenuItem(menuItem);
936
937 m_items.Append( mitem );
938 }
939
940 void wxMenu::Append( int id, const wxString &item, wxMenu *subMenu, const wxString &helpStr )
941 {
942 wxMenuItem *mitem = new wxMenuItem(this, id, item, helpStr, FALSE, subMenu);
943
944 #if (GTK_MINOR_VERSION > 0)
945 /* text has "_" instead of "&" after mitem->SetText() */
946 wxString text( mitem->GetText() );
947
948 /* local buffer in multibyte form */
949 char buf[200];
950 strcpy( buf, "/" );
951 strcat( buf, text.mb_str() );
952
953 GtkItemFactoryEntry entry;
954 entry.path = buf;
955 entry.callback = (GtkItemFactoryCallback) 0;
956 entry.callback_action = 0;
957 entry.item_type = "<Branch>";
958
959 gtk_item_factory_create_item( m_factory, &entry, (gpointer) this, 2 ); /* what is 2 ? */
960
961 /* in order to get the pointer to the item we need the item text _without_ underscores */
962 wxString s = wxT("<main>/");
963 for ( const wxChar *pc = text; *pc != wxT('\0'); pc++ )
964 {
965 if (*pc == wxT('_')) pc++; /* skip it */
966 s << *pc;
967 }
968
969 GtkWidget *menuItem = gtk_item_factory_get_item( m_factory, s.mb_str() );
970
971 #else
972
973 GtkWidget *menuItem = gtk_menu_item_new_with_label(mitem->GetText().mbc_str());
974
975 gtk_menu_append( GTK_MENU(m_menu), menuItem );
976 gtk_widget_show( menuItem );
977
978 #endif
979
980 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
981 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
982 (gpointer*)this );
983
984 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
985 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
986 (gpointer*)this );
987
988 gtk_menu_item_set_submenu( GTK_MENU_ITEM(menuItem), subMenu->m_menu );
989
990 mitem->SetMenuItem(menuItem);
991
992 m_items.Append( mitem );
993 }
994
995 void wxMenu::Append( wxMenuItem *item )
996 {
997 m_items.Append( item );
998
999 GtkWidget *menuItem = (GtkWidget*) NULL;
1000
1001 if (item->IsSeparator())
1002 menuItem = gtk_menu_item_new();
1003 else if (item->IsSubMenu())
1004 menuItem = gtk_menu_item_new_with_label(item->GetText().mbc_str());
1005 else
1006 menuItem = item->IsCheckable() ? gtk_check_menu_item_new_with_label(item->GetText().mbc_str())
1007 : gtk_menu_item_new_with_label(item->GetText().mbc_str());
1008
1009 if (!item->IsSeparator())
1010 {
1011 gtk_signal_connect( GTK_OBJECT(menuItem), "select",
1012 GTK_SIGNAL_FUNC(gtk_menu_hilight_callback),
1013 (gpointer*)this );
1014
1015 gtk_signal_connect( GTK_OBJECT(menuItem), "deselect",
1016 GTK_SIGNAL_FUNC(gtk_menu_nolight_callback),
1017 (gpointer*)this );
1018
1019 if (!item->IsSubMenu())
1020 {
1021 gtk_signal_connect( GTK_OBJECT(menuItem), "activate",
1022 GTK_SIGNAL_FUNC(gtk_menu_clicked_callback),
1023 (gpointer*)this );
1024 }
1025 }
1026
1027 gtk_menu_append( GTK_MENU(m_menu), menuItem );
1028 gtk_widget_show( menuItem );
1029 item->SetMenuItem(menuItem);
1030 }
1031
1032 void wxMenu::Delete( int id )
1033 {
1034 wxNode *node = m_items.First();
1035 while (node)
1036 {
1037 wxMenuItem *item = (wxMenuItem*)node->Data();
1038 if (item->GetId() == id)
1039 {
1040 gtk_widget_destroy( item->GetMenuItem() );
1041 m_items.DeleteNode( node );
1042 return;
1043 }
1044 node = node->Next();
1045 }
1046 }
1047
1048 int wxMenu::FindItem( const wxString itemString ) const
1049 {
1050 wxString s = wxT("");
1051 for ( const wxChar *pc = itemString; *pc != wxT('\0'); pc++ )
1052 {
1053 if (*pc == wxT('&'))
1054 {
1055 pc++; /* skip it */
1056 #if (GTK_MINOR_VERSION > 0)
1057 s << wxT('_');
1058 #endif
1059 }
1060 s << *pc;
1061 }
1062
1063 wxNode *node = m_items.First();
1064 while (node)
1065 {
1066 wxMenuItem *item = (wxMenuItem*)node->Data();
1067 if (item->GetText() == s)
1068 {
1069 return item->GetId();
1070 }
1071 node = node->Next();
1072 }
1073
1074 return wxNOT_FOUND;
1075 }
1076
1077 void wxMenu::Enable( int id, bool enable )
1078 {
1079 wxMenuItem *item = FindItem(id);
1080
1081 wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") );
1082
1083 item->Enable(enable);
1084 }
1085
1086 bool wxMenu::IsEnabled( int id ) const
1087 {
1088 wxMenuItem *item = FindItem(id);
1089
1090 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") );
1091
1092 return item->IsEnabled();
1093 }
1094
1095 void wxMenu::Check( int id, bool enable )
1096 {
1097 wxMenuItem *item = FindItem(id);
1098
1099 wxCHECK_RET( item, wxT("wxMenu::Check: no such item") );
1100
1101 item->Check(enable);
1102 }
1103
1104 bool wxMenu::IsChecked( int id ) const
1105 {
1106 wxMenuItem *item = FindItem(id);
1107
1108 wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") );
1109
1110 return item->IsChecked();
1111 }
1112
1113 void wxMenu::SetLabel( int id, const wxString &label )
1114 {
1115 wxMenuItem *item = FindItem(id);
1116
1117 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
1118
1119 item->SetText(label);
1120 }
1121
1122 wxString wxMenu::GetLabel( int id ) const
1123 {
1124 wxMenuItem *item = FindItem(id);
1125
1126 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") );
1127
1128 return item->GetText();
1129 }
1130
1131 void wxMenu::SetHelpString( int id, const wxString& helpString )
1132 {
1133 wxMenuItem *item = FindItem(id);
1134
1135 wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") );
1136
1137 item->SetHelp( helpString );
1138 }
1139
1140 wxString wxMenu::GetHelpString( int id ) const
1141 {
1142 wxMenuItem *item = FindItem(id);
1143
1144 wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") );
1145
1146 return item->GetHelp();
1147 }
1148
1149 int wxMenu::FindMenuIdByMenuItem( GtkWidget *menuItem ) const
1150 {
1151 wxNode *node = m_items.First();
1152 while (node)
1153 {
1154 wxMenuItem *item = (wxMenuItem*)node->Data();
1155 if (item->GetMenuItem() == menuItem)
1156 return item->GetId();
1157 node = node->Next();
1158 }
1159
1160 return wxNOT_FOUND;
1161 }
1162
1163 wxMenuItem *wxMenu::FindItem(int id) const
1164 {
1165 wxNode *node = m_items.First();
1166 while (node)
1167 {
1168 wxMenuItem *item = (wxMenuItem*)node->Data();
1169 if (item->GetId() == id)
1170 {
1171 return item;
1172 }
1173 node = node->Next();
1174 }
1175
1176 /* Not finding anything here can be correct
1177 * when search the entire menu system for
1178 * an entry -> no error message. */
1179
1180 return (wxMenuItem *) NULL;
1181 }
1182
1183 void wxMenu::SetInvokingWindow( wxWindow *win )
1184 {
1185 m_invokingWindow = win;
1186 }
1187
1188 wxWindow *wxMenu::GetInvokingWindow()
1189 {
1190 return m_invokingWindow;
1191 }
1192
1193 // Update a menu and all submenus recursively. source is the object that has
1194 // the update event handlers defined for it. If NULL, the menu or associated
1195 // window will be used.
1196 void wxMenu::UpdateUI(wxEvtHandler* source)
1197 {
1198 if (!source && GetInvokingWindow())
1199 source = GetInvokingWindow()->GetEventHandler();
1200 if (!source)
1201 source = GetEventHandler();
1202 if (!source)
1203 source = this;
1204
1205 wxNode* node = GetItems().First();
1206 while (node)
1207 {
1208 wxMenuItem* item = (wxMenuItem*) node->Data();
1209 if ( !item->IsSeparator() )
1210 {
1211 wxWindowID id = item->GetId();
1212 wxUpdateUIEvent event(id);
1213 event.SetEventObject( source );
1214
1215 if (source->ProcessEvent(event))
1216 {
1217 if (event.GetSetText())
1218 SetLabel(id, event.GetText());
1219 if (event.GetSetChecked())
1220 Check(id, event.GetChecked());
1221 if (event.GetSetEnabled())
1222 Enable(id, event.GetEnabled());
1223 }
1224
1225 if (item->GetSubMenu())
1226 item->GetSubMenu()->UpdateUI(source);
1227 }
1228 node = node->Next();
1229 }
1230 }
1231