]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menu.mm
byte length for interim UniChar String corrected
[wxWidgets.git] / src / cocoa / menu.mm
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
26 #include "wx/cocoa/autorelease.h"
27 #include "wx/cocoa/string.h"
28
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
42 IMPLEMENT_DYNAMIC_CLASS(wxMenu,wxEvtHandler)
43
44 bool wxMenu::Create(const wxString& title, long style)
45 {
46 wxAutoNSAutoreleasePool pool;
47 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: wxNSStringWithWxString(title)];
48 return true;
49 }
50
51 wxMenu::~wxMenu()
52 {
53 [m_cocoaNSMenu release];
54 }
55
56 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
57 {
58 wxAutoNSAutoreleasePool pool;
59 if(!wxMenuBase::DoAppend(item))
60 return NULL;
61 [m_cocoaNSMenu addItem: item->GetNSMenuItem()];
62 return item;
63 }
64
65 wxMenuItem* wxMenu::DoInsert(unsigned long pos, wxMenuItem *item)
66 {
67 wxAutoNSAutoreleasePool pool;
68 if(!wxMenuBase::DoInsert(pos,item))
69 return NULL;
70 [m_cocoaNSMenu insertItem:item->GetNSMenuItem() atIndex:pos];
71 return item;
72 }
73
74 wxMenuItem* wxMenu::DoRemove(wxMenuItem *item)
75 {
76 wxAutoNSAutoreleasePool pool;
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 // ============================================================================
86 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar,wxWindow)
87
88 bool wxMenuBar::Create(long style)
89 {
90 wxAutoNSAutoreleasePool pool;
91 m_cocoaNSMenu = [[NSMenu alloc] initWithTitle: @"wxMenuBar"];
92
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];
98 return true;
99 }
100
101 wxMenuBar::~wxMenuBar()
102 {
103 [m_cocoaNSMenu release];
104 }
105
106 bool wxMenuBar::Append( wxMenu *menu, const wxString &title )
107 {
108 wxAutoNSAutoreleasePool pool;
109 wxLogTrace(wxTRACE_COCOA,wxT("append menu=%p, title=%s"),menu,title.c_str());
110 if(!wxMenuBarBase::Append(menu,title))
111 return false;
112 wxASSERT(menu);
113 wxASSERT(menu->GetNSMenu());
114 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], wxStripMenuCodes(title));
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
126 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
127 {
128 wxAutoNSAutoreleasePool pool;
129 wxLogTrace(wxTRACE_COCOA,wxT("insert pos=%lu, menu=%p, title=%s"),pos,menu,title.c_str());
130 // Get the current menu at this position
131 wxMenu *nextmenu = GetMenu(pos);
132 if(!wxMenuBarBase::Insert(pos,menu,title))
133 return false;
134 wxASSERT(menu);
135 wxASSERT(menu->GetNSMenu());
136 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc], title);
137 NSMenuItem *newItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:NULL keyEquivalent:@""];
138 [menu->GetNSMenu() setTitle:menuTitle];
139 [newItem setSubmenu:menu->GetNSMenu()];
140
141 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:nextmenu->GetNSMenu()];
142 wxASSERT(itemindex>=0);
143 [m_cocoaNSMenu insertItem:newItem atIndex:itemindex];
144
145 [menuTitle release];
146 [newItem release];
147 return true;
148 }
149
150 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
151 {
152 return NULL;
153 }
154
155 wxMenu *wxMenuBar::Remove(size_t pos)
156 {
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;
163 }
164
165
166 void wxMenuBar::EnableTop(size_t pos, bool enable)
167 {
168 }
169
170 bool wxMenuBar::IsEnabledTop(size_t pos) const
171 {
172 return false;
173 }
174
175 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
176 {
177 }
178
179 wxString wxMenuBar::GetLabelTop(size_t pos) const
180 {
181 wxMenu *menu = GetMenu(pos);
182 int itemindex = [m_cocoaNSMenu indexOfItemWithSubmenu:menu->GetNSMenu()];
183 wxASSERT(itemindex>=0);
184 return wxStringWithNSString([[m_cocoaNSMenu itemAtIndex:itemindex] title]);
185 }
186
187 void wxMenuBar::Attach(wxFrame *frame)
188 {
189 wxMenuBarBase::Attach(frame);
190 }
191
192 void wxMenuBar::Detach()
193 {
194 wxMenuBarBase::Detach();
195 }
196
197 wxSize wxMenuBar::DoGetBestClientSize() const
198 {
199 return wxDefaultSize;
200 }
201
202 #endif // wxUSE_MENUS