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 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
46 bool wxMenu::Create(const wxString& title, long style)
48 wxAutoNSAutoreleasePool pool;
49 m_cocoaNSMenu = [[WX_GET_OBJC_CLASS(WXNSMenu) alloc] initWithTitle: wxNSStringWithWxString(title)];
50 AssociateNSMenu(m_cocoaNSMenu);
56 DisassociateNSMenu(m_cocoaNSMenu);
58 [m_cocoaNSMenu release];
61 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
63 wxAutoNSAutoreleasePool pool;
64 if(!wxMenuBase::DoAppend(item))
66 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
70 wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
72 wxAutoNSAutoreleasePool pool;
73 if(!wxMenuBase::DoInsert(pos,item))
75 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
79 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
81 wxAutoNSAutoreleasePool pool;
82 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
83 wxASSERT(retitem->GetNSMenuItem());
84 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
88 // This autoreleases the menu on the assumption that something is
89 // going to retain it shortly (for instance, it is going to be returned from
90 // an overloaded [NSStatusItem menu] or from the applicationDockMenu:
91 // NSApplication delegate method.
93 // It then sets a bool flag m_cocoaDeletes. When the NSMenu is dealloc'd
94 // (dealloc is the Cocoa destructor) we delete ourselves. In this manner we
95 // can be available for Cocoa calls until Cocoa is finished with us.
97 // I can see very few reasons to undo this. Nevertheless, it is implemented.
98 void wxMenu::SetCocoaDeletes(bool cocoaDeletes)
100 if(m_cocoaDeletes==cocoaDeletes)
102 m_cocoaDeletes = cocoaDeletes;
104 [m_cocoaNSMenu autorelease];
106 [m_cocoaNSMenu retain];
109 void wxMenu::Cocoa_dealloc()
115 // ============================================================================
116 // wxMenuBar implementation
117 // ============================================================================
118 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
120 bool wxMenuBar::Create(long style)
122 wxAutoNSAutoreleasePool pool;
123 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
125 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
126 /* Note: title gets clobbered by app name anyway */
127 action:nil keyEquivalent:@""];
128 [m_cocoaNSMenu addItem:dummyItem];
133 wxMenuBar::wxMenuBar(size_t n,
135 const wxString titles[],
140 for ( size_t i = 0; i < n; ++i )
141 Append(menus[i], titles[i]);
144 wxMenuBar::~wxMenuBar()
146 [m_cocoaNSMenu release];
149 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
151 wxAutoNSAutoreleasePool pool;
152 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
153 if(!wxMenuBarBase::Append(menu,title))
156 wxASSERT(menu->GetNSMenu());
157 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
158 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
159 [menu->GetNSMenu() setTitle:menuTitle];
160 [newItem setSubmenu:menu->GetNSMenu()];
162 [m_cocoaNSMenu addItem:newItem];
169 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
171 wxAutoNSAutoreleasePool pool;
172 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
173 // Get the current menu at this position
174 wxMenu *nextmenu = GetMenu(pos);
175 if(!wxMenuBarBase::Insert(pos,menu,title))
178 wxASSERT(menu->GetNSMenu());
179 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
180 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
181 [menu->GetNSMenu() setTitle:menuTitle];
182 [newItem setSubmenu:menu->GetNSMenu()];
184 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
185 wxASSERT(itemindex>=0);
186 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
193 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
198 wxMenu *wxMenuBar::Remove(size_t pos)
200 wxMenu *menu = wxMenuBarBase::Remove(pos);
202 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
203 wxASSERT(itemindex>=0);
204 [m_cocoaNSMenu removeItemAtIndex:itemindex];
209 void wxMenuBar::EnableTop(size_t pos, bool enable)
213 bool wxMenuBar::IsEnabledTop(size_t pos) const
218 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
222 wxString wxMenuBar::GetMenuLabel(size_t pos) const
224 wxMenu *menu = GetMenu(pos);
225 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
226 wxASSERT(itemindex>=0);
227 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
230 void wxMenuBar::Attach(wxFrame *frame)
232 wxMenuBarBase::Attach(frame);
235 void wxMenuBar::Detach()
237 wxMenuBarBase::Detach();
240 wxSize wxMenuBar::DoGetBestClientSize() const
242 return wxDefaultSize;
245 #endif // wxUSE_MENUS