]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menu.mm
Add wxTEST_DIALOG for testing of modal dialogs.
[wxWidgets.git] / src / cocoa / menu.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/menu.mm
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 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 bool wxMenu::Create(const wxString& title, long style)
45 {
46 wxAutoNSAutoreleasePool pool;
47 m_cocoaNSMenu = [[WX_GET_OBJC_CLASS(WXNSMenu) 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
117 bool wxMenuBar::Create(long style)
118 {
119 wxAutoNSAutoreleasePool pool;
120 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
121
122 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
123 /* Note: title gets clobbered by app name anyway */
124 action:nil keyEquivalent:@""];
125 [m_cocoaNSMenu addItem:dummyItem];
126 [dummyItem release];
127 return true;
128 }
129
130 wxMenuBar::wxMenuBar(size_t n,
131 wxMenu *menus[],
132 const wxString titles[],
133 long style)
134 {
135 Create(style);
136
137 for ( size_t i = 0; i < n; ++i )
138 Append(menus[i], titles[i]);
139 }
140
141 wxMenuBar::~wxMenuBar()
142 {
143 [m_cocoaNSMenu release];
144 }
145
146 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
147 {
148 wxAutoNSAutoreleasePool pool;
149 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
150 if(!wxMenuBarBase::Append(menu,title))
151 return false;
152 wxASSERT(menu);
153 wxASSERT(menu->GetNSMenu());
154 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
155 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
156 [menu->GetNSMenu() setTitle:menuTitle];
157 [newItem setSubmenu:menu->GetNSMenu()];
158
159 [m_cocoaNSMenu addItem:newItem];
160
161 [menuTitle release];
162 [newItem release];
163 return true;
164 }
165
166 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
167 {
168 wxAutoNSAutoreleasePool pool;
169 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
170 // Get the current menu at this position
171 wxMenu *nextmenu = GetMenu(pos);
172 if(!wxMenuBarBase::Insert(pos,menu,title))
173 return false;
174 wxASSERT(menu);
175 wxASSERT(menu->GetNSMenu());
176 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
177 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
178 [menu->GetNSMenu() setTitle:menuTitle];
179 [newItem setSubmenu:menu->GetNSMenu()];
180
181 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
182 wxASSERT(itemindex>=0);
183 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
184
185 [menuTitle release];
186 [newItem release];
187 return true;
188 }
189
190 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
191 {
192 return NULL;
193 }
194
195 wxMenu *wxMenuBar::Remove(size_t pos)
196 {
197 wxMenu *menu = wxMenuBarBase::Remove(pos);
198 wxASSERT(menu);
199 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
200 wxASSERT(itemindex>=0);
201 [m_cocoaNSMenu removeItemAtIndex:itemindex];
202 return menu;
203 }
204
205
206 void wxMenuBar::EnableTop(size_t pos, bool enable)
207 {
208 }
209
210 bool wxMenuBar::IsEnabledTop(size_t pos) const
211 {
212 return false;
213 }
214
215 void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
216 {
217 }
218
219 wxString wxMenuBar::GetMenuLabel(size_t pos) const
220 {
221 wxMenu *menu = GetMenu(pos);
222 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
223 wxASSERT(itemindex>=0);
224 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
225 }
226
227 void wxMenuBar::Attach(wxFrame *frame)
228 {
229 wxMenuBarBase::Attach(frame);
230 }
231
232 void wxMenuBar::Detach()
233 {
234 wxMenuBarBase::Detach();
235 }
236
237 wxSize wxMenuBar::DoGetBestClientSize() const
238 {
239 return wxDefaultSize;
240 }
241
242 #endif // wxUSE_MENUS