]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menu.mm
missing commit
[wxWidgets.git] / src / cocoa / menu.mm
CommitLineData
fb896a32 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/cocoa/menu.mm
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
526954c5 9// Licence: wxWindows 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
fb896a32
DE
44bool wxMenu::Create(const wxString& title, long style)
45{
605c7e7e 46 wxAutoNSAutoreleasePool pool;
e7e1ad7d 47 m_cocoaNSMenu = [[WX_GET_OBJC_CLASS(WXNSMenu) alloc] initWithTitle: wxNSStringWithWxString(title)];
950432e4 48 AssociateNSMenu(m_cocoaNSMenu);
605c7e7e 49 return true;
fb896a32
DE
50}
51
52wxMenu::~wxMenu()
53{
950432e4
DE
54 DisassociateNSMenu(m_cocoaNSMenu);
55 if(!m_cocoaDeletes)
56 [m_cocoaNSMenu release];
fb896a32
DE
57}
58
fe4a107d 59wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
fb896a32 60{
7fc77f30 61 wxAutoNSAutoreleasePool pool;
fb896a32 62 if(!wxMenuBase::DoAppend(item))
fe4a107d 63 return NULL;
fb896a32 64 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
fe4a107d 65 return item;
fb896a32
DE
66}
67
fe4a107d 68wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
fb896a32 69{
7fc77f30 70 wxAutoNSAutoreleasePool pool;
fb896a32 71 if(!wxMenuBase::DoInsert(pos,item))
fe4a107d 72 return NULL;
fb896a32 73 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
fe4a107d 74 return item;
fb896a32
DE
75}
76
77wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
78{
7fc77f30 79 wxAutoNSAutoreleasePool pool;
fb896a32
DE
80 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
81 wxASSERT(retitem->GetNSMenuItem());
82 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
83 return retitem;
84}
85
950432e4
DE
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.
96void 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
107void wxMenu::Cocoa_dealloc()
108{
109 if(m_cocoaDeletes)
110 delete this;
111}
112
fb896a32
DE
113// ============================================================================
114// wxMenuBar implementation
115// ============================================================================
fb896a32
DE
116
117bool wxMenuBar::Create(long style)
118{
605c7e7e
DE
119 wxAutoNSAutoreleasePool pool;
120 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
121
97351e17
DE
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];
fb896a32
DE
127 return true;
128}
129
3b880f29
VZ
130wxMenuBar::wxMenuBar(size_t n,
131 wxMenu *menus[],
132 const wxString titles[],
133 long style)
294ea16d
VZ
134{
135 Create(style);
136
3b880f29 137 for ( size_t i = 0; i < n; ++i )
294ea16d
VZ
138 Append(menus[i], titles[i]);
139}
140
fb896a32
DE
141wxMenuBar::~wxMenuBar()
142{
605c7e7e 143 [m_cocoaNSMenu release];
fb896a32
DE
144}
145
146bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
147{
7fc77f30 148 wxAutoNSAutoreleasePool pool;
48580976 149 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
fb896a32
DE
150 if(!wxMenuBarBase::Append(menu,title))
151 return false;
152 wxASSERT(menu);
153 wxASSERT(menu->GetNSMenu());
b0c0a393 154 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
fb896a32
DE
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
166bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
167{
7fc77f30 168 wxAutoNSAutoreleasePool pool;
48580976 169 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
97351e17
DE
170 // Get the current menu at this position
171 wxMenu *nextmenu = GetMenu(pos);
fb896a32
DE
172 if(!wxMenuBarBase::Insert(pos,menu,title))
173 return false;
174 wxASSERT(menu);
175 wxASSERT(menu->GetNSMenu());
b0c0a393 176 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
fb896a32
DE
177 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
178 [menu->GetNSMenu() setTitle:menuTitle];
179 [newItem setSubmenu:menu->GetNSMenu()];
180
97351e17
DE
181 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
182 wxASSERT(itemindex>=0);
183 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
fb896a32
DE
184
185 [menuTitle release];
186 [newItem release];
187 return true;
188}
189
190wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
191{
192 return NULL;
193}
194
195wxMenu *wxMenuBar::Remove(size_t pos)
196{
97351e17
DE
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;
fb896a32
DE
203}
204
205
206void wxMenuBar::EnableTop(size_t pos, bool enable)
207{
208}
209
210bool wxMenuBar::IsEnabledTop(size_t pos) const
211{
212 return false;
213}
214
52af3158 215void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
fb896a32
DE
216{
217}
218
52af3158 219wxString wxMenuBar::GetMenuLabel(size_t pos) const
fb896a32 220{
97351e17
DE
221 wxMenu *menu = GetMenu(pos);
222 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
223 wxASSERT(itemindex>=0);
b0c0a393 224 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
fb896a32
DE
225}
226
227void wxMenuBar::Attach(wxFrame *frame)
228{
2fc2d511 229 wxMenuBarBase::Attach(frame);
fb896a32
DE
230}
231
232void wxMenuBar::Detach()
233{
2fc2d511 234 wxMenuBarBase::Detach();
fb896a32
DE
235}
236
237wxSize wxMenuBar::DoGetBestClientSize() const
238{
239 return wxDefaultSize;
240}
241
242#endif // wxUSE_MENUS