]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menu.mm
final sweep over docs - replace & with \&
[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: wxWidgets licence
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 #include "wx/cocoa/string.h"
28
29 #import <Foundation/NSString.h>
30 #import <AppKit/NSMenu.h>
31
32 #if wxUSE_MENUS
33
34 // ----------------------------------------------------------------------------
35 // globals
36 // ----------------------------------------------------------------------------
37
38 // ============================================================================
39 // wxMenu implementation
40 // ============================================================================
41
42 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
43
44 bool wxMenu::Create(const wxString& title, long style)
45 {
46 wxAutoNSAutoreleasePool pool;
47 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: wxNSStringWithWxString(title)];
48 AssociateNSMenu(m_cocoaNSMenu);
49 return true;
50 }
51
52 wxMenu::~wxMenu()
53 {
54 DisassociateNSMenu(m_cocoaNSMenu);
55 if(!m_cocoaDeletes)
56 [m_cocoaNSMenu release];
57 }
58
59 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
60 {
61 wxAutoNSAutoreleasePool pool;
62 if(!wxMenuBase::DoAppend(item))
63 return NULL;
64 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
65 return item;
66 }
67
68 wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
69 {
70 wxAutoNSAutoreleasePool pool;
71 if(!wxMenuBase::DoInsert(pos,item))
72 return NULL;
73 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
74 return item;
75 }
76
77 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
78 {
79 wxAutoNSAutoreleasePool pool;
80 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
81 wxASSERT(retitem->GetNSMenuItem());
82 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
83 return retitem;
84 }
85
86 // This autoreleases the menu on the assumption that something is
87 // going to retain it shortly (for instance, it is going to be returned from
88 // an overloaded [NSStatusItem menu] or from the applicationDockMenu:
89 // NSApplication delegate method.
90 //
91 // It then sets a bool flag m_cocoaDeletes. When the NSMenu is dealloc'd
92 // (dealloc is the Cocoa destructor) we delete ourselves. In this manner we
93 // can be available for Cocoa calls until Cocoa is finished with us.
94 //
95 // I can see very few reasons to undo this. Nevertheless, it is implemented.
96 void wxMenu::SetCocoaDeletes(bool cocoaDeletes)
97 {
98 if(m_cocoaDeletes==cocoaDeletes)
99 return;
100 m_cocoaDeletes = cocoaDeletes;
101 if(m_cocoaDeletes)
102 [m_cocoaNSMenu autorelease];
103 else
104 [m_cocoaNSMenu retain];
105 }
106
107 void wxMenu::Cocoa_dealloc()
108 {
109 if(m_cocoaDeletes)
110 delete this;
111 }
112
113 // ============================================================================
114 // wxMenuBar implementation
115 // ============================================================================
116 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
117
118 bool wxMenuBar::Create(long style)
119 {
120 wxAutoNSAutoreleasePool pool;
121 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
122
123 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
124 /* Note: title gets clobbered by app name anyway */
125 action:nil keyEquivalent:@""];
126 [m_cocoaNSMenu addItem:dummyItem];
127 [dummyItem release];
128 return true;
129 }
130
131 wxMenuBar::~wxMenuBar()
132 {
133 [m_cocoaNSMenu release];
134 }
135
136 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
137 {
138 wxAutoNSAutoreleasePool pool;
139 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
140 if(!wxMenuBarBase::Append(menu,title))
141 return false;
142 wxASSERT(menu);
143 wxASSERT(menu->GetNSMenu());
144 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
145 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
146 [menu->GetNSMenu() setTitle:menuTitle];
147 [newItem setSubmenu:menu->GetNSMenu()];
148
149 [m_cocoaNSMenu addItem:newItem];
150
151 [menuTitle release];
152 [newItem release];
153 return true;
154 }
155
156 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
157 {
158 wxAutoNSAutoreleasePool pool;
159 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
160 // Get the current menu at this position
161 wxMenu *nextmenu = GetMenu(pos);
162 if(!wxMenuBarBase::Insert(pos,menu,title))
163 return false;
164 wxASSERT(menu);
165 wxASSERT(menu->GetNSMenu());
166 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
167 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
168 [menu->GetNSMenu() setTitle:menuTitle];
169 [newItem setSubmenu:menu->GetNSMenu()];
170
171 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
172 wxASSERT(itemindex>=0);
173 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
174
175 [menuTitle release];
176 [newItem release];
177 return true;
178 }
179
180 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
181 {
182 return NULL;
183 }
184
185 wxMenu *wxMenuBar::Remove(size_t pos)
186 {
187 wxMenu *menu = wxMenuBarBase::Remove(pos);
188 wxASSERT(menu);
189 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
190 wxASSERT(itemindex>=0);
191 [m_cocoaNSMenu removeItemAtIndex:itemindex];
192 return menu;
193 }
194
195
196 void wxMenuBar::EnableTop(size_t pos, bool enable)
197 {
198 }
199
200 bool wxMenuBar::IsEnabledTop(size_t pos) const
201 {
202 return false;
203 }
204
205 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
206 {
207 }
208
209 wxString wxMenuBar::GetLabelTop(size_t pos) const
210 {
211 wxMenu *menu = GetMenu(pos);
212 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
213 wxASSERT(itemindex>=0);
214 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
215 }
216
217 void wxMenuBar::Attach(wxFrame *frame)
218 {
219 wxMenuBarBase::Attach(frame);
220 }
221
222 void wxMenuBar::Detach()
223 {
224 wxMenuBarBase::Detach();
225 }
226
227 wxSize wxMenuBar::DoGetBestClientSize() const
228 {
229 return wxDefaultSize;
230 }
231
232 #endif // wxUSE_MENUS