]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menu.mm
wxOS2 warning fixes after daily build log.
[wxWidgets.git] / src / cocoa / menu.mm
CommitLineData
fb896a32
DE
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
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
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
7fc77f30 26#include "wx/cocoa/autorelease.h"
b0c0a393 27#include "wx/cocoa/string.h"
7fc77f30 28
fb896a32
DE
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
42IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
43
44bool wxMenu::Create(const wxString& title, long style)
45{
605c7e7e 46 wxAutoNSAutoreleasePool pool;
b0c0a393 47 m_cocoaNSMenu = [[NSMenu 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// ============================================================================
116IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
117
118bool wxMenuBar::Create(long style)
119{
605c7e7e
DE
120 wxAutoNSAutoreleasePool pool;
121 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
122
97351e17
DE
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];
fb896a32
DE
128 return true;
129}
130
3b880f29
VZ
131wxMenuBar::wxMenuBar(size_t n,
132 wxMenu *menus[],
133 const wxString titles[],
134 long style)
294ea16d
VZ
135{
136 Create(style);
137
3b880f29 138 for ( size_t i = 0; i < n; ++i )
294ea16d
VZ
139 Append(menus[i], titles[i]);
140}
141
fb896a32
DE
142wxMenuBar::~wxMenuBar()
143{
605c7e7e 144 [m_cocoaNSMenu release];
fb896a32
DE
145}
146
147bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
148{
7fc77f30 149 wxAutoNSAutoreleasePool pool;
48580976 150 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
fb896a32
DE
151 if(!wxMenuBarBase::Append(menu,title))
152 return false;
153 wxASSERT(menu);
154 wxASSERT(menu->GetNSMenu());
b0c0a393 155 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
fb896a32
DE
156 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
157 [menu->GetNSMenu() setTitle:menuTitle];
158 [newItem setSubmenu:menu->GetNSMenu()];
159
160 [m_cocoaNSMenu addItem:newItem];
161
162 [menuTitle release];
163 [newItem release];
164 return true;
165}
166
167bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
168{
7fc77f30 169 wxAutoNSAutoreleasePool pool;
48580976 170 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
97351e17
DE
171 // Get the current menu at this position
172 wxMenu *nextmenu = GetMenu(pos);
fb896a32
DE
173 if(!wxMenuBarBase::Insert(pos,menu,title))
174 return false;
175 wxASSERT(menu);
176 wxASSERT(menu->GetNSMenu());
b0c0a393 177 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
fb896a32
DE
178 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
179 [menu->GetNSMenu() setTitle:menuTitle];
180 [newItem setSubmenu:menu->GetNSMenu()];
181
97351e17
DE
182 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
183 wxASSERT(itemindex>=0);
184 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
fb896a32
DE
185
186 [menuTitle release];
187 [newItem release];
188 return true;
189}
190
191wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
192{
193 return NULL;
194}
195
196wxMenu *wxMenuBar::Remove(size_t pos)
197{
97351e17
DE
198 wxMenu *menu = wxMenuBarBase::Remove(pos);
199 wxASSERT(menu);
200 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
201 wxASSERT(itemindex>=0);
202 [m_cocoaNSMenu removeItemAtIndex:itemindex];
203 return menu;
fb896a32
DE
204}
205
206
207void wxMenuBar::EnableTop(size_t pos, bool enable)
208{
209}
210
211bool wxMenuBar::IsEnabledTop(size_t pos) const
212{
213 return false;
214}
215
216void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
217{
218}
219
220wxString wxMenuBar::GetLabelTop(size_t pos) const
221{
97351e17
DE
222 wxMenu *menu = GetMenu(pos);
223 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
224 wxASSERT(itemindex>=0);
b0c0a393 225 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
fb896a32
DE
226}
227
228void wxMenuBar::Attach(wxFrame *frame)
229{
2fc2d511 230 wxMenuBarBase::Attach(frame);
fb896a32
DE
231}
232
233void wxMenuBar::Detach()
234{
2fc2d511 235 wxMenuBarBase::Detach();
fb896a32
DE
236}
237
238wxSize wxMenuBar::DoGetBestClientSize() const
239{
240 return wxDefaultSize;
241}
242
243#endif // wxUSE_MENUS