X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2fc2d511d0f095ab5de3713cff6042832f886d15..7b28e0ede49ca78e05df6bc6af2d2d8c508f8455:/src/cocoa/menu.mm diff --git a/src/cocoa/menu.mm b/src/cocoa/menu.mm index 57db00ae15..d8ed61c0a7 100644 --- a/src/cocoa/menu.mm +++ b/src/cocoa/menu.mm @@ -24,6 +24,7 @@ #endif // WX_PRECOMP #include "wx/cocoa/autorelease.h" +#include "wx/cocoa/string.h" #import #import @@ -42,33 +43,32 @@ 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: wxNSStringWithWxString(title)]; + return true; } wxMenu::~wxMenu() { + [m_cocoaNSMenu release]; } -bool wxMenu::DoAppend(wxMenuItem *item) +wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) { wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoAppend(item)) - return false; + return NULL; [m_cocoaNSMenu addItem: item->GetNSMenuItem()]; - return true; + return item; } -bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) +wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) { wxAutoNSAutoreleasePool pool; if(!wxMenuBase::DoInsert(pos,item)) - return false; + return NULL; [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos]; - return true; + return item; } wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) @@ -87,24 +87,31 @@ 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()); + wxLogDebug(wxT("append menu=%p, title=%s"),menu,title.c_str()); if(!wxMenuBarBase::Append(menu,title)) return false; wxASSERT(menu); wxASSERT(menu->GetNSMenu()); - NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()]; + NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title)); NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; [menu->GetNSMenu() setTitle:menuTitle]; [newItem setSubmenu:menu->GetNSMenu()]; @@ -119,17 +126,21 @@ 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()); + wxLogDebug(wxT("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); wxASSERT(menu->GetNSMenu()); - NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()]; + NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title); NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; [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]; @@ -143,7 +154,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; } @@ -162,7 +178,10 @@ 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 wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]); } void wxMenuBar::Attach(wxFrame *frame)