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