]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menu.mm
another fix
[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)];
605c7e7e 48 return true;
fb896a32
DE
49}
50
51wxMenu::~wxMenu()
52{
605c7e7e 53 [m_cocoaNSMenu release];
fb896a32
DE
54}
55
fe4a107d 56wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
fb896a32 57{
7fc77f30 58 wxAutoNSAutoreleasePool pool;
fb896a32 59 if(!wxMenuBase::DoAppend(item))
fe4a107d 60 return NULL;
fb896a32 61 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
fe4a107d 62 return item;
fb896a32
DE
63}
64
fe4a107d 65wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
fb896a32 66{
7fc77f30 67 wxAutoNSAutoreleasePool pool;
fb896a32 68 if(!wxMenuBase::DoInsert(pos,item))
fe4a107d 69 return NULL;
fb896a32 70 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
fe4a107d 71 return item;
fb896a32
DE
72}
73
74wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
75{
7fc77f30 76 wxAutoNSAutoreleasePool pool;
fb896a32
DE
77 wxMenuItem *retitem = wxMenuBase::DoRemove(item);
78 wxASSERT(retitem->GetNSMenuItem());
79 [m_cocoaNSMenu removeItem:retitem->GetNSMenuItem()];
80 return retitem;
81}
82
83// ============================================================================
84// wxMenuBar implementation
85// ============================================================================
86IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
87
88bool wxMenuBar::Create(long style)
89{
605c7e7e
DE
90 wxAutoNSAutoreleasePool pool;
91 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
92
97351e17
DE
93 NSMenuItem *dummyItem = [[NSMenuItem alloc] initWithTitle:@"App menu"
94 /* Note: title gets clobbered by app name anyway */
95 action:nil keyEquivalent:@""];
96 [m_cocoaNSMenu addItem:dummyItem];
97 [dummyItem release];
fb896a32
DE
98 return true;
99}
100
101wxMenuBar::~wxMenuBar()
102{
605c7e7e 103 [m_cocoaNSMenu release];
fb896a32
DE
104}
105
106bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
107{
7fc77f30 108 wxAutoNSAutoreleasePool pool;
48580976 109 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
fb896a32
DE
110 if(!wxMenuBarBase::Append(menu,title))
111 return false;
112 wxASSERT(menu);
113 wxASSERT(menu->GetNSMenu());
b0c0a393 114 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
fb896a32
DE
115 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
116 [menu->GetNSMenu() setTitle:menuTitle];
117 [newItem setSubmenu:menu->GetNSMenu()];
118
119 [m_cocoaNSMenu addItem:newItem];
120
121 [menuTitle release];
122 [newItem release];
123 return true;
124}
125
126bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
127{
7fc77f30 128 wxAutoNSAutoreleasePool pool;
48580976 129 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
97351e17
DE
130 // Get the current menu at this position
131 wxMenu *nextmenu = GetMenu(pos);
fb896a32
DE
132 if(!wxMenuBarBase::Insert(pos,menu,title))
133 return false;
134 wxASSERT(menu);
135 wxASSERT(menu->GetNSMenu());
b0c0a393 136 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
fb896a32
DE
137 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
138 [menu->GetNSMenu() setTitle:menuTitle];
139 [newItem setSubmenu:menu->GetNSMenu()];
140
97351e17
DE
141 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
142 wxASSERT(itemindex>=0);
143 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
fb896a32
DE
144
145 [menuTitle release];
146 [newItem release];
147 return true;
148}
149
150wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
151{
152 return NULL;
153}
154
155wxMenu *wxMenuBar::Remove(size_t pos)
156{
97351e17
DE
157 wxMenu *menu = wxMenuBarBase::Remove(pos);
158 wxASSERT(menu);
159 int itemindex = [GetNSMenu() indexOfItemWithSubmenu:menu->GetNSMenu()];
160 wxASSERT(itemindex>=0);
161 [m_cocoaNSMenu removeItemAtIndex:itemindex];
162 return menu;
fb896a32
DE
163}
164
165
166void wxMenuBar::EnableTop(size_t pos, bool enable)
167{
168}
169
170bool wxMenuBar::IsEnabledTop(size_t pos) const
171{
172 return false;
173}
174
175void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
176{
177}
178
179wxString wxMenuBar::GetLabelTop(size_t pos) const
180{
97351e17
DE
181 wxMenu *menu = GetMenu(pos);
182 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
183 wxASSERT(itemindex>=0);
b0c0a393 184 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
fb896a32
DE
185}
186
187void wxMenuBar::Attach(wxFrame *frame)
188{
2fc2d511 189 wxMenuBarBase::Attach(frame);
fb896a32
DE
190}
191
192void wxMenuBar::Detach()
193{
2fc2d511 194 wxMenuBarBase::Detach();
fb896a32
DE
195}
196
197wxSize wxMenuBar::DoGetBestClientSize() const
198{
199 return wxDefaultSize;
200}
201
202#endif // wxUSE_MENUS