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"
28 #import <Foundation/NSString.h>
29 #import <AppKit/NSMenu.h>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // ============================================================================
38 // wxMenu implementation
39 // ============================================================================
41 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
43 bool wxMenu::Create(const wxString& title, long style)
47 return CocoaCreate("wxMenu");
49 return CocoaCreate(title);
56 bool wxMenu::DoAppend(wxMenuItem *item)
58 wxAutoNSAutoreleasePool pool;
59 if(!wxMenuBase::DoAppend(item))
61 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
65 bool 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 if(!CocoaCreate("wxMenuBar"))
92 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
93 /* Note: title gets clobbered by app name anyway */
94 action:nil keyEquivalent:@""];
95 [m_cocoaNSMenu addItem:dummyItem];
100 wxMenuBar::~wxMenuBar()
104 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
106 wxAutoNSAutoreleasePool pool;
107 wxLogDebug("append menu=%p, title=%s",menu,title.c_str());
108 if(!wxMenuBarBase::Append(menu,title))
111 wxASSERT(menu->GetNSMenu());
112 NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()];
113 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
114 [menu->GetNSMenu() setTitle:menuTitle];
115 [newItem setSubmenu:menu->GetNSMenu()];
117 [m_cocoaNSMenu addItem:newItem];
124 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
126 wxAutoNSAutoreleasePool pool;
127 wxLogDebug("insert pos=%lu, menu=%p, title=%s",pos,menu,title.c_str());
128 // Get the current menu at this position
129 wxMenu *nextmenu = GetMenu(pos);
130 if(!wxMenuBarBase::Insert(pos,menu,title))
133 wxASSERT(menu->GetNSMenu());
134 NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()];
135 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
136 [menu->GetNSMenu() setTitle:menuTitle];
137 [newItem setSubmenu:menu->GetNSMenu()];
139 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
140 wxASSERT(itemindex>=0);
141 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
148 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
153 wxMenu *wxMenuBar::Remove(size_t pos)
155 wxMenu *menu = wxMenuBarBase::Remove(pos);
157 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
158 wxASSERT(itemindex>=0);
159 [m_cocoaNSMenu removeItemAtIndex:itemindex];
164 void wxMenuBar::EnableTop(size_t pos, bool enable)
168 bool wxMenuBar::IsEnabledTop(size_t pos) const
173 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
177 wxString wxMenuBar::GetLabelTop(size_t pos) const
179 wxMenu *menu = GetMenu(pos);
180 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
181 wxASSERT(itemindex>=0);
182 return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]);
185 void wxMenuBar::Attach(wxFrame *frame)
187 wxMenuBarBase::Attach(frame);
190 void wxMenuBar::Detach()
192 wxMenuBarBase::Detach();
195 wxSize wxMenuBar::DoGetBestClientSize() const
197 return wxDefaultSize;
200 #endif // wxUSE_MENUS