X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fb896a322a2ed53d393ee738765ddd3f72cee004..04fdd8736a403bb3c6b2bd1430e48178dd8b7e45:/src/cocoa/menu.mm diff --git a/src/cocoa/menu.mm b/src/cocoa/menu.mm index 4b8dbf3153..3c69ea7481 100644 --- a/src/cocoa/menu.mm +++ b/src/cocoa/menu.mm @@ -23,6 +23,8 @@ #include "wx/log.h" #endif // WX_PRECOMP +#include "wx/cocoa/autorelease.h" + #import #import @@ -40,19 +42,19 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler) bool wxMenu::Create(const wxString& title, long style) { -#if 0 - if(!title) - return CocoaCreate("wxMenu"); -#endif - return CocoaCreate(title); + wxAutoNSAutoreleasePool pool; + m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: [NSString stringWithCString: title.c_str()]]; + return true; } wxMenu::~wxMenu() { + [m_cocoaNSMenu release]; } bool wxMenu::DoAppend(wxMenuItem *item) { + wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoAppend(item)) return false; [m_cocoaNSMenu addItem: item->GetNSMenuItem()]; @@ -61,6 +63,7 @@ bool wxMenu::DoAppend(wxMenuItem *item) bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) { + wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoInsert(pos,item)) return false; [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos]; @@ -69,6 +72,7 @@ bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) { + wxAutoNSAutoreleasePool pool; wxMenuItem *retitem = wxMenuBase::DoRemove(item); wxASSERT(retitem->GetNSMenuItem()); [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()]; @@ -82,17 +86,25 @@ IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) bool wxMenuBar::Create(long style) { - if(!CocoaCreate("wxMenuBar")) - return false; + wxAutoNSAutoreleasePool pool; + m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"]; + + NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu" + /* Note: title gets clobbered by app name anyway */ + action:nil keyEquivalent:@""]; + [m_cocoaNSMenu addItem:dummyItem]; + [dummyItem release]; return true; } wxMenuBar::~wxMenuBar() { + [m_cocoaNSMenu release]; } bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) { + wxAutoNSAutoreleasePool pool; wxLogDebug("append menu=%p, title=%s",menu,title.c_str()); if(!wxMenuBarBase::Append(menu,title)) return false; @@ -112,7 +124,10 @@ bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) { + wxAutoNSAutoreleasePool pool; wxLogDebug("insert pos=%lu, menu=%p, title=%s",pos,menu,title.c_str()); + // Get the current menu at this position + wxMenu *nextmenu = GetMenu(pos); if(!wxMenuBarBase::Insert(pos,menu,title)) return false; wxASSERT(menu); @@ -122,7 +137,9 @@ bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) [menu->GetNSMenu() setTitle:menuTitle]; [newItem setSubmenu:menu->GetNSMenu()]; - [m_cocoaNSMenu insertItem:newItem atIndex:pos]; + int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()]; + wxASSERT(itemindex>=0); + [m_cocoaNSMenu insertItem:newItem atIndex:itemindex]; [menuTitle release]; [newItem release]; @@ -136,7 +153,12 @@ wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) wxMenu *wxMenuBar::Remove(size_t pos) { - return NULL; + wxMenu *menu = wxMenuBarBase::Remove(pos); + wxASSERT(menu); + int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()]; + wxASSERT(itemindex>=0); + [m_cocoaNSMenu removeItemAtIndex:itemindex]; + return menu; } @@ -155,15 +177,20 @@ void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) wxString wxMenuBar::GetLabelTop(size_t pos) const { - return wxEmptyString; + wxMenu *menu = GetMenu(pos); + int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()]; + wxASSERT(itemindex>=0); + return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]); } void wxMenuBar::Attach(wxFrame *frame) { + wxMenuBarBase::Attach(frame); } void wxMenuBar::Detach() { + wxMenuBarBase::Detach(); } wxSize wxMenuBar::DoGetBestClientSize() const