1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/menu.cpp
3 // Purpose: wxMenu and wxMenuBar implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows license
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)];
53 [m_cocoaNSMenu release];
56 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
58 wxAutoNSAutoreleasePool pool;
59 if(!wxMenuBase::DoAppend(item))
61 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
65 wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
67 wxAutoNSAutoreleasePool pool;
68 if(!wxMenuBase::DoInsert(pos,item))
70 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
74 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
76 wxAutoNSAutoreleasePool pool;
77 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
78 wxASSERT(retitem->GetNSMenuItem());
79 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
83 // ============================================================================
84 // wxMenuBar implementation
85 // ============================================================================
86 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
88 bool wxMenuBar::Create(long style)
90 wxAutoNSAutoreleasePool pool;
91 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
93 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
94 /* Note: title gets clobbered by app name anyway */
95 action:nil keyEquivalent:@""];
96 [m_cocoaNSMenu addItem:dummyItem];
101 wxMenuBar::~wxMenuBar()
103 [m_cocoaNSMenu release];
106 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
108 wxAutoNSAutoreleasePool pool;
109 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
110 if(!wxMenuBarBase::Append(menu,title))
113 wxASSERT(menu->GetNSMenu());
114 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
115 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
116 [menu->GetNSMenu() setTitle:menuTitle];
117 [newItem setSubmenu:menu->GetNSMenu()];
119 [m_cocoaNSMenu addItem:newItem];
126 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
128 wxAutoNSAutoreleasePool pool;
129 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
130 // Get the current menu at this position
131 wxMenu *nextmenu = GetMenu(pos);
132 if(!wxMenuBarBase::Insert(pos,menu,title))
135 wxASSERT(menu->GetNSMenu());
136 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
137 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
138 [menu->GetNSMenu() setTitle:menuTitle];
139 [newItem setSubmenu:menu->GetNSMenu()];
141 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
142 wxASSERT(itemindex>=0);
143 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
150 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
155 wxMenu *wxMenuBar::Remove(size_t pos)
157 wxMenu *menu = wxMenuBarBase::Remove(pos);
159 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
160 wxASSERT(itemindex>=0);
161 [m_cocoaNSMenu removeItemAtIndex:itemindex];
166 void wxMenuBar::EnableTop(size_t pos, bool enable)
170 bool wxMenuBar::IsEnabledTop(size_t pos) const
175 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
179 wxString wxMenuBar::GetLabelTop(size_t pos) const
181 wxMenu *menu = GetMenu(pos);
182 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
183 wxASSERT(itemindex>=0);
184 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
187 void wxMenuBar::Attach(wxFrame *frame)
189 wxMenuBarBase::Attach(frame);
192 void wxMenuBar::Detach()
194 wxMenuBarBase::Detach();
197 wxSize wxMenuBar::DoGetBestClientSize() const
199 return wxDefaultSize;
202 #endif // wxUSE_MENUS