]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menu.mm
check for wxUSE_REARRANGECTRL being defined
[wxWidgets.git] / src / osx / cocoa / menu.mm
CommitLineData
dbeddfb9
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/menu.mm
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: menu.cpp 54129 2008-06-11 19:30:52Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
16// wxWidgets headers
17// -----------------
18
19#include "wx/wxprec.h"
20
21#include "wx/menu.h"
22
23#ifndef WX_PRECOMP
24 #include "wx/log.h"
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/frame.h"
28 #include "wx/menuitem.h"
29#endif
30
31#include "wx/osx/private.h"
32
33// other standard headers
34// ----------------------
35#include <string.h>
36
37@class wxNSMenuItem;
38
39@interface wxNSMenu : NSMenu
40{
41 wxMenuImpl* impl;
42}
43
44- (void) setImplementation:(wxMenuImpl*) item;
45- (wxMenuImpl*) implementation;
46
47@end
48
49@implementation wxNSMenu
50
51- (id) init
52{
53 [super init];
54 return self;
55}
56
57- (void)setImplementation: (wxMenuImpl *) theImplementation
58{
59 impl = theImplementation;
60}
61
62- (wxMenuImpl*) implementation
63{
64 return impl;
65}
66
67@end
68
69@interface wxNSMenuController : NSObject
70{
71}
72
73- (void)menuWillOpen:(NSMenu *)menu;
74- (void)menuDidClose:(NSMenu *)menu;
75- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
76
77@end
78
79@implementation wxNSMenuController
80
81- (id) init
82{
83 [super init];
84 return self;
85}
86
87- (void)menuWillOpen:(NSMenu *)smenu
88{
89 wxNSMenu* menu = (wxNSMenu*) smenu;
90 wxMenuImpl* menuimpl = [menu implementation];
91 if ( menuimpl )
92 {
93 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
94 if ( wxpeer )
95 wxpeer->HandleMenuOpened();
dbeddfb9
SC
96 }
97}
98
99- (void)menuDidClose:(NSMenu *)smenu
100{
101 wxNSMenu* menu = (wxNSMenu*) smenu;
102 wxMenuImpl* menuimpl = [menu implementation];
103 if ( menuimpl )
104 {
105 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
106 if ( wxpeer )
107 wxpeer->HandleMenuClosed();
dbeddfb9
SC
108 }
109}
110
111- (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
112{
113 wxNSMenu* menu = (wxNSMenu*) smenu;
114 wxMenuImpl* menuimpl = [menu implementation];
115 if ( menuimpl )
116 {
117 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
118 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
119 {
120 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
121 if ( wxpeer && menuitemimpl )
122 {
123 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
124 }
125 }
126 }
127}
128
129@end
130
131class wxMenuCocoaImpl : public wxMenuImpl
132{
133public :
134 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
135 {
136 }
137
138 virtual ~wxMenuCocoaImpl();
139
140 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
141 {
142 if ( pos == (size_t) -1 )
143 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
144 else
145 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
146 }
147
148 virtual void Remove( wxMenuItem *pItem )
149 {
150 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
151 }
152
153 virtual void MakeRoot()
154 {
155 [NSApp setMainMenu:m_osxMenu];
156 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
157 }
158
159 virtual void Enable( bool enable )
160 {
161 }
162
163 virtual void SetTitle( const wxString& text )
164 {
165 wxCFStringRef cfText(text);
166 [m_osxMenu setTitle:cfText.AsNSString()];
167 }
168
2cb5d2d2
SC
169 virtual void PopUp( wxWindow *win, int x, int y )
170 {
171 win->ScreenToClient( &x , &y ) ;
172 NSView *view = win->GetPeer()->GetWXWidget();
173 NSRect frame = [view frame];
174 frame.origin.x = x;
175 frame.origin.y = y;
176 frame.size.width = 1;
177 frame.size.height = 1;
178 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
179 [popUpButtonCell setAutoenablesItems:NO];
180 [popUpButtonCell setAltersStateOfSelectedItem:NO];
181 [popUpButtonCell setMenu:m_osxMenu];
182 [popUpButtonCell selectItem:nil];
183 [popUpButtonCell performClickWithFrame:frame inView:view];
184 [popUpButtonCell release];
185 }
186
dbeddfb9
SC
187 WXHMENU GetHMenu() { return m_osxMenu; }
188
189 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
190 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
191protected :
192 NSMenu* m_osxMenu;
193} ;
194
195wxMenuCocoaImpl::~wxMenuCocoaImpl()
196{
197 [m_osxMenu release];
198}
199
200wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
201{
202 static wxNSMenuController* controller = NULL;
203 if ( controller == NULL )
204 {
205 controller = [[wxNSMenuController alloc] init];
206 }
207 wxCFStringRef cfText( title );
208 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
209 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
210 [menu setDelegate:controller];
211 [menu setImplementation:c];
212 return c;
213}