1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/menu.cpp
3 // Purpose: wxMenu and wxMenuBar implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
28 #include "wx/cocoa/autorelease.h"
29 #include "wx/cocoa/string.h"
31 #import <Foundation/NSString.h>
32 #include "wx/cocoa/objc/NSMenu.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // ============================================================================
41 // wxMenu implementation
42 // ============================================================================
44 bool wxMenu::Create(const wxString& title, long style)
46 wxAutoNSAutoreleasePool pool;
47 m_cocoaNSMenu = [[WX_GET_OBJC_CLASS(WXNSMenu) alloc] initWithTitle: wxNSStringWithWxString(title)];
48 AssociateNSMenu(m_cocoaNSMenu);
54 DisassociateNSMenu(m_cocoaNSMenu);
56 [m_cocoaNSMenu release];
59 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
61 wxAutoNSAutoreleasePool pool;
62 if(!wxMenuBase::DoAppend(item))
64 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
68 wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
70 wxAutoNSAutoreleasePool pool;
71 if(!wxMenuBase::DoInsert(pos,item))
73 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
77 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
79 wxAutoNSAutoreleasePool pool;
80 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
81 wxASSERT(retitem->GetNSMenuItem());
82 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
86 // This autoreleases the menu on the assumption that something is
87 // going to retain it shortly (for instance, it is going to be returned from
88 // an overloaded [NSStatusItem menu] or from the applicationDockMenu:
89 // NSApplication delegate method.
91 // It then sets a bool flag m_cocoaDeletes. When the NSMenu is dealloc'd
92 // (dealloc is the Cocoa destructor) we delete ourselves. In this manner we
93 // can be available for Cocoa calls until Cocoa is finished with us.
95 // I can see very few reasons to undo this. Nevertheless, it is implemented.
96 void wxMenu::SetCocoaDeletes(bool cocoaDeletes)
98 if(m_cocoaDeletes==cocoaDeletes)
100 m_cocoaDeletes = cocoaDeletes;
102 [m_cocoaNSMenu autorelease];
104 [m_cocoaNSMenu retain];
107 void wxMenu::Cocoa_dealloc()
113 // ============================================================================
114 // wxMenuBar implementation
115 // ============================================================================
117 bool wxMenuBar::Create(long style)
119 wxAutoNSAutoreleasePool pool;
120 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
122 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
123 /* Note: title gets clobbered by app name anyway */
124 action:nil keyEquivalent:@""];
125 [m_cocoaNSMenu addItem:dummyItem];
130 wxMenuBar::wxMenuBar(size_t n,
132 const wxString titles[],
137 for ( size_t i = 0; i < n; ++i )
138 Append(menus[i], titles[i]);
141 wxMenuBar::~wxMenuBar()
143 [m_cocoaNSMenu release];
146 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
148 wxAutoNSAutoreleasePool pool;
149 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
150 if(!wxMenuBarBase::Append(menu,title))
153 wxASSERT(menu->GetNSMenu());
154 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
155 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
156 [menu->GetNSMenu() setTitle:menuTitle];
157 [newItem setSubmenu:menu->GetNSMenu()];
159 [m_cocoaNSMenu addItem:newItem];
166 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
168 wxAutoNSAutoreleasePool pool;
169 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
170 // Get the current menu at this position
171 wxMenu *nextmenu = GetMenu(pos);
172 if(!wxMenuBarBase::Insert(pos,menu,title))
175 wxASSERT(menu->GetNSMenu());
176 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
177 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
178 [menu->GetNSMenu() setTitle:menuTitle];
179 [newItem setSubmenu:menu->GetNSMenu()];
181 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
182 wxASSERT(itemindex>=0);
183 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
190 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
195 wxMenu *wxMenuBar::Remove(size_t pos)
197 wxMenu *menu = wxMenuBarBase::Remove(pos);
199 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
200 wxASSERT(itemindex>=0);
201 [m_cocoaNSMenu removeItemAtIndex:itemindex];
206 void wxMenuBar::EnableTop(size_t pos, bool enable)
210 bool wxMenuBar::IsEnabledTop(size_t pos) const
215 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
219 wxString wxMenuBar::GetMenuLabel(size_t pos) const
221 wxMenu *menu = GetMenu(pos);
222 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
223 wxASSERT(itemindex>=0);
224 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
227 void wxMenuBar::Attach(wxFrame *frame)
229 wxMenuBarBase::Attach(frame);
232 void wxMenuBar::Detach()
234 wxMenuBarBase::Detach();
237 wxSize wxMenuBar::DoGetBestClientSize() const
239 return wxDefaultSize;
242 #endif // wxUSE_MENUS