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