]>
Commit | Line | Data |
---|---|---|
fb896a32 | 1 | ///////////////////////////////////////////////////////////////////////////// |
3b3dc801 | 2 | // Name: src/cocoa/menu.cpp |
fb896a32 DE |
3 | // Purpose: wxMenu and wxMenuBar implementation |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2002/12/09 | |
3b3dc801 | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2002 David Elliott |
065e208e | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
3b3dc801 WS |
21 | |
22 | #include "wx/menu.h" | |
23 | ||
fb896a32 | 24 | #ifndef WX_PRECOMP |
fb896a32 DE |
25 | #include "wx/log.h" |
26 | #endif // WX_PRECOMP | |
27 | ||
7fc77f30 | 28 | #include "wx/cocoa/autorelease.h" |
b0c0a393 | 29 | #include "wx/cocoa/string.h" |
7fc77f30 | 30 | |
fb896a32 | 31 | #import <Foundation/NSString.h> |
829a2e95 | 32 | #include "wx/cocoa/objc/NSMenu.h" |
fb896a32 DE |
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 | { | |
605c7e7e | 48 | wxAutoNSAutoreleasePool pool; |
829a2e95 | 49 | m_cocoaNSMenu = [[WXNSMenu alloc] initWithTitle: wxNSStringWithWxString(title)]; |
950432e4 | 50 | AssociateNSMenu(m_cocoaNSMenu); |
605c7e7e | 51 | return true; |
fb896a32 DE |
52 | } |
53 | ||
54 | wxMenu::~wxMenu() | |
55 | { | |
950432e4 DE |
56 | DisassociateNSMenu(m_cocoaNSMenu); |
57 | if(!m_cocoaDeletes) | |
58 | [m_cocoaNSMenu release]; | |
fb896a32 DE |
59 | } |
60 | ||
fe4a107d | 61 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) |
fb896a32 | 62 | { |
7fc77f30 | 63 | wxAutoNSAutoreleasePool pool; |
fb896a32 | 64 | if(!wxMenuBase::DoAppend(item)) |
fe4a107d | 65 | return NULL; |
fb896a32 | 66 | [m_cocoaNSMenu addItem: item->GetNSMenuItem()]; |
fe4a107d | 67 | return item; |
fb896a32 DE |
68 | } |
69 | ||
fe4a107d | 70 | wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item) |
fb896a32 | 71 | { |
7fc77f30 | 72 | wxAutoNSAutoreleasePool pool; |
fb896a32 | 73 | if(!wxMenuBase::DoInsert(pos,item)) |
fe4a107d | 74 | return NULL; |
fb896a32 | 75 | [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos]; |
fe4a107d | 76 | return item; |
fb896a32 DE |
77 | } |
78 | ||
79 | wxMenuItem* wxMenu::DoRemove(wxMenuItem *item) | |
80 | { | |
7fc77f30 | 81 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
82 | wxMenuItem *retitem = wxMenuBase::DoRemove(item); |
83 | wxASSERT(retitem->GetNSMenuItem()); | |
84 | [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()]; | |
85 | return retitem; | |
86 | } | |
87 | ||
950432e4 DE |
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 | ||
fb896a32 DE |
115 | // ============================================================================ |
116 | // wxMenuBar implementation | |
117 | // ============================================================================ | |
118 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow) | |
119 | ||
120 | bool wxMenuBar::Create(long style) | |
121 | { | |
605c7e7e DE |
122 | wxAutoNSAutoreleasePool pool; |
123 | m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"]; | |
124 | ||
97351e17 DE |
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]; | |
fb896a32 DE |
130 | return true; |
131 | } | |
132 | ||
3b880f29 VZ |
133 | wxMenuBar::wxMenuBar(size_t n, |
134 | wxMenu *menus[], | |
135 | const wxString titles[], | |
136 | long style) | |
294ea16d VZ |
137 | { |
138 | Create(style); | |
139 | ||
3b880f29 | 140 | for ( size_t i = 0; i < n; ++i ) |
294ea16d VZ |
141 | Append(menus[i], titles[i]); |
142 | } | |
143 | ||
fb896a32 DE |
144 | wxMenuBar::~wxMenuBar() |
145 | { | |
605c7e7e | 146 | [m_cocoaNSMenu release]; |
fb896a32 DE |
147 | } |
148 | ||
149 | bool wxMenuBar::Append( wxMenu *menu, const wxString &title ) | |
150 | { | |
7fc77f30 | 151 | wxAutoNSAutoreleasePool pool; |
48580976 | 152 | wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str()); |
fb896a32 DE |
153 | if(!wxMenuBarBase::Append(menu,title)) |
154 | return false; | |
155 | wxASSERT(menu); | |
156 | wxASSERT(menu->GetNSMenu()); | |
b0c0a393 | 157 | NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title)); |
fb896a32 DE |
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 | { | |
7fc77f30 | 171 | wxAutoNSAutoreleasePool pool; |
48580976 | 172 | wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str()); |
97351e17 DE |
173 | // Get the current menu at this position |
174 | wxMenu *nextmenu = GetMenu(pos); | |
fb896a32 DE |
175 | if(!wxMenuBarBase::Insert(pos,menu,title)) |
176 | return false; | |
177 | wxASSERT(menu); | |
178 | wxASSERT(menu->GetNSMenu()); | |
b0c0a393 | 179 | NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title); |
fb896a32 DE |
180 | NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""]; |
181 | [menu->GetNSMenu() setTitle:menuTitle]; | |
182 | [newItem setSubmenu:menu->GetNSMenu()]; | |
183 | ||
97351e17 DE |
184 | int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()]; |
185 | wxASSERT(itemindex>=0); | |
186 | [m_cocoaNSMenu insertItem:newItem atIndex:itemindex]; | |
fb896a32 DE |
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 | { | |
97351e17 DE |
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; | |
fb896a32 DE |
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::SetLabelTop(size_t pos, const wxString& label) | |
219 | { | |
220 | } | |
221 | ||
222 | wxString wxMenuBar::GetLabelTop(size_t pos) const | |
223 | { | |
97351e17 DE |
224 | wxMenu *menu = GetMenu(pos); |
225 | int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()]; | |
226 | wxASSERT(itemindex>=0); | |
b0c0a393 | 227 | return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]); |
fb896a32 DE |
228 | } |
229 | ||
230 | void wxMenuBar::Attach(wxFrame *frame) | |
231 | { | |
2fc2d511 | 232 | wxMenuBarBase::Attach(frame); |
fb896a32 DE |
233 | } |
234 | ||
235 | void wxMenuBar::Detach() | |
236 | { | |
2fc2d511 | 237 | wxMenuBarBase::Detach(); |
fb896a32 DE |
238 | } |
239 | ||
240 | wxSize wxMenuBar::DoGetBestClientSize() const | |
241 | { | |
242 | return wxDefaultSize; | |
243 | } | |
244 | ||
245 | #endif // wxUSE_MENUS |