1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/menu.cpp
3 // Purpose: wxMenu and wxMenuBar implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/string.h"
29 #import <Foundation/NSString.h>
30 #import <AppKit/NSMenu.h>
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // ============================================================================
39 // wxMenu implementation
40 // ============================================================================
42 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
44 bool wxMenu::Create(const wxString& title, long style)
46 wxAutoNSAutoreleasePool pool;
47 m_cocoaNSMenu = [[NSMenu 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 // ============================================================================
116 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
118 bool wxMenuBar::Create(long style)
120 wxAutoNSAutoreleasePool pool;
121 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
123 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
124 /* Note: title gets clobbered by app name anyway */
125 action:nil keyEquivalent:@""];
126 [m_cocoaNSMenu addItem:dummyItem];
131 wxMenuBar::wxMenuBar(size_t n,
133 const wxString titles[],
138 for ( size_t i = 0; i < n; ++i )
139 Append(menus[i], titles[i]);
142 wxMenuBar::~wxMenuBar()
144 [m_cocoaNSMenu release];
147 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
149 wxAutoNSAutoreleasePool pool;
150 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
151 if(!wxMenuBarBase::Append(menu,title))
154 wxASSERT(menu->GetNSMenu());
155 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
156 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
157 [menu->GetNSMenu() setTitle:menuTitle];
158 [newItem setSubmenu:menu->GetNSMenu()];
160 [m_cocoaNSMenu addItem:newItem];
167 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
169 wxAutoNSAutoreleasePool pool;
170 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
171 // Get the current menu at this position
172 wxMenu *nextmenu = GetMenu(pos);
173 if(!wxMenuBarBase::Insert(pos,menu,title))
176 wxASSERT(menu->GetNSMenu());
177 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
178 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
179 [menu->GetNSMenu() setTitle:menuTitle];
180 [newItem setSubmenu:menu->GetNSMenu()];
182 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
183 wxASSERT(itemindex>=0);
184 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
191 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
196 wxMenu *wxMenuBar::Remove(size_t pos)
198 wxMenu *menu = wxMenuBarBase::Remove(pos);
200 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
201 wxASSERT(itemindex>=0);
202 [m_cocoaNSMenu removeItemAtIndex:itemindex];
207 void wxMenuBar::EnableTop(size_t pos, bool enable)
211 bool wxMenuBar::IsEnabledTop(size_t pos) const
216 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
220 wxString wxMenuBar::GetLabelTop(size_t pos) const
222 wxMenu *menu = GetMenu(pos);
223 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
224 wxASSERT(itemindex>=0);
225 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
228 void wxMenuBar::Attach(wxFrame *frame)
230 wxMenuBarBase::Attach(frame);
233 void wxMenuBar::Detach()
235 wxMenuBarBase::Detach();
238 wxSize wxMenuBar::DoGetBestClientSize() const
240 return wxDefaultSize;
243 #endif // wxUSE_MENUS