]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menu.mm
remove us from the parents children list in Destroy()
[wxWidgets.git] / src / cocoa / menu.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/menu.cpp
3 // Purpose: wxMenu and wxMenuBar implementation
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/09
7 // RCS-ID: $Id:
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22 #include "wx/menu.h"
23 #include "wx/log.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/cocoa/autorelease.h"
27
28 #import <Foundation/NSString.h>
29 #import <AppKit/NSMenu.h>
30
31 #if wxUSE_MENUS
32
33 // ----------------------------------------------------------------------------
34 // globals
35 // ----------------------------------------------------------------------------
36
37 // ============================================================================
38 // wxMenu implementation
39 // ============================================================================
40
41 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
42
43 bool wxMenu::Create(const wxString& title, long style)
44 {
45 wxAutoNSAutoreleasePool pool;
46 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: [NSString stringWithCString: title.c_str()]];
47 return true;
48 }
49
50 wxMenu::~wxMenu()
51 {
52 [m_cocoaNSMenu release];
53 }
54
55 bool wxMenu::DoAppend(wxMenuItem *item)
56 {
57 wxAutoNSAutoreleasePool pool;
58 if(!wxMenuBase::DoAppend(item))
59 return false;
60 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
61 return true;
62 }
63
64 bool wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
65 {
66 wxAutoNSAutoreleasePool pool;
67 if(!wxMenuBase::DoInsert(pos,item))
68 return false;
69 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
70 return true;
71 }
72
73 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
74 {
75 wxAutoNSAutoreleasePool pool;
76 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
77 wxASSERT(retitem->GetNSMenuItem());
78 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
79 return retitem;
80 }
81
82 // ============================================================================
83 // wxMenuBar implementation
84 // ============================================================================
85 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
86
87 bool wxMenuBar::Create(long style)
88 {
89 wxAutoNSAutoreleasePool pool;
90 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
91
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];
96 [dummyItem release];
97 return true;
98 }
99
100 wxMenuBar::~wxMenuBar()
101 {
102 [m_cocoaNSMenu release];
103 }
104
105 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
106 {
107 wxAutoNSAutoreleasePool pool;
108 wxLogDebug("append menu=%p, title=%s",menu,title.c_str());
109 if(!wxMenuBarBase::Append(menu,title))
110 return false;
111 wxASSERT(menu);
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()];
117
118 [m_cocoaNSMenu addItem:newItem];
119
120 [menuTitle release];
121 [newItem release];
122 return true;
123 }
124
125 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
126 {
127 wxAutoNSAutoreleasePool pool;
128 wxLogDebug("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))
132 return false;
133 wxASSERT(menu);
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()];
139
140 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
141 wxASSERT(itemindex>=0);
142 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
143
144 [menuTitle release];
145 [newItem release];
146 return true;
147 }
148
149 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
150 {
151 return NULL;
152 }
153
154 wxMenu *wxMenuBar::Remove(size_t pos)
155 {
156 wxMenu *menu = wxMenuBarBase::Remove(pos);
157 wxASSERT(menu);
158 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
159 wxASSERT(itemindex>=0);
160 [m_cocoaNSMenu removeItemAtIndex:itemindex];
161 return menu;
162 }
163
164
165 void wxMenuBar::EnableTop(size_t pos, bool enable)
166 {
167 }
168
169 bool wxMenuBar::IsEnabledTop(size_t pos) const
170 {
171 return false;
172 }
173
174 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
175 {
176 }
177
178 wxString wxMenuBar::GetLabelTop(size_t pos) const
179 {
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]);
184 }
185
186 void wxMenuBar::Attach(wxFrame *frame)
187 {
188 wxMenuBarBase::Attach(frame);
189 }
190
191 void wxMenuBar::Detach()
192 {
193 wxMenuBarBase::Detach();
194 }
195
196 wxSize wxMenuBar::DoGetBestClientSize() const
197 {
198 return wxDefaultSize;
199 }
200
201 #endif // wxUSE_MENUS