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)
45 wxAutoNSAutoreleasePool pool;
46 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: [NSString stringWithCString: title.c_str()]];
52 [m_cocoaNSMenu release];
55 bool wxMenu::DoAppend(wxMenuItem *item)
57 wxAutoNSAutoreleasePool pool;
58 if(!wxMenuBase::DoAppend(item))
60 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
64 bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
66 wxAutoNSAutoreleasePool pool;
67 if(!wxMenuBase::DoInsert(pos,item))
69 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
73 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
75 wxAutoNSAutoreleasePool pool;
76 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
77 wxASSERT(retitem->GetNSMenuItem());
78 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
82 // ============================================================================
83 // wxMenuBar implementation
84 // ============================================================================
85 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
87 bool wxMenuBar::Create(long style)
89 wxAutoNSAutoreleasePool pool;
90 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"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()
102 [m_cocoaNSMenu release];
105 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
107 wxAutoNSAutoreleasePool pool;
108 wxLogDebug(wxT("append menu=%p, title=%s"),menu,title.c_str());
109 if(!wxMenuBarBase::Append(menu,title))
112 wxASSERT(menu->GetNSMenu());
113 NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(title).c_str()];
114 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
115 [menu->GetNSMenu() setTitle:menuTitle];
116 [newItem setSubmenu:menu->GetNSMenu()];
118 [m_cocoaNSMenu addItem:newItem];
125 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
127 wxAutoNSAutoreleasePool pool;
128 wxLogDebug(wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
129 // Get the current menu at this position
130 wxMenu *nextmenu = GetMenu(pos);
131 if(!wxMenuBarBase::Insert(pos,menu,title))
134 wxASSERT(menu->GetNSMenu());
135 NSString *menuTitle = [[NSString alloc] initWithCString: title.c_str()];
136 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
137 [menu->GetNSMenu() setTitle:menuTitle];
138 [newItem setSubmenu:menu->GetNSMenu()];
140 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
141 wxASSERT(itemindex>=0);
142 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
149 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
154 wxMenu *wxMenuBar::Remove(size_t pos)
156 wxMenu *menu = wxMenuBarBase::Remove(pos);
158 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
159 wxASSERT(itemindex>=0);
160 [m_cocoaNSMenu removeItemAtIndex:itemindex];
165 void wxMenuBar::EnableTop(size_t pos, bool enable)
169 bool wxMenuBar::IsEnabledTop(size_t pos) const
174 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
178 wxString wxMenuBar::GetLabelTop(size_t pos) const
180 wxMenu *menu = GetMenu(pos);
181 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
182 wxASSERT(itemindex>=0);
183 return wxString([[[m_cocoaNSMenu itemAtIndex:itemindex] title] lossyCString]);
186 void wxMenuBar::Attach(wxFrame *frame)
188 wxMenuBarBase::Attach(frame);
191 void wxMenuBar::Detach()
193 wxMenuBarBase::Detach();
196 wxSize wxMenuBar::DoGetBestClientSize() const
198 return wxDefaultSize;
201 #endif // wxUSE_MENUS