]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/menu.mm
define MIIM_BITMAP &c in wx/msw/missing.h instead of msw/menu.cpp as menuitem.cpp...
[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
dbeddfb9
SC
37@implementation wxNSMenu
38
39- (id) init
40{
41 [super init];
42 return self;
43}
44
45- (void)setImplementation: (wxMenuImpl *) theImplementation
46{
47 impl = theImplementation;
48}
49
50- (wxMenuImpl*) implementation
51{
52 return impl;
53}
54
55@end
56
57@interface wxNSMenuController : NSObject
58{
59}
60
61- (void)menuWillOpen:(NSMenu *)menu;
62- (void)menuDidClose:(NSMenu *)menu;
63- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item;
64
65@end
66
67@implementation wxNSMenuController
68
69- (id) init
70{
71 [super init];
72 return self;
73}
74
75- (void)menuWillOpen:(NSMenu *)smenu
76{
77 wxNSMenu* menu = (wxNSMenu*) smenu;
78 wxMenuImpl* menuimpl = [menu implementation];
79 if ( menuimpl )
80 {
81 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
3fe48198
SC
82 if ( wxpeer )
83 wxpeer->HandleMenuOpened();
dbeddfb9
SC
84 }
85}
86
87- (void)menuDidClose:(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->HandleMenuClosed();
dbeddfb9
SC
96 }
97}
98
99- (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
100{
101 wxNSMenu* menu = (wxNSMenu*) smenu;
102 wxMenuImpl* menuimpl = [menu implementation];
103 if ( menuimpl )
104 {
105 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
106 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
107 {
108 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
109 if ( wxpeer && menuitemimpl )
110 {
111 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
112 }
113 }
114 }
115}
116
117@end
118
ffad7b0d
SC
119@interface NSApplication(MissingAppleMenuCall)
120- (void)setAppleMenu:(NSMenu *)menu;
121@end
122
dbeddfb9
SC
123class wxMenuCocoaImpl : public wxMenuImpl
124{
125public :
126 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
127 {
128 }
129
130 virtual ~wxMenuCocoaImpl();
131
132 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
133 {
134 if ( pos == (size_t) -1 )
135 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
136 else
137 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
138 }
139
140 virtual void Remove( wxMenuItem *pItem )
141 {
142 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
143 }
144
145 virtual void MakeRoot()
146 {
147 [NSApp setMainMenu:m_osxMenu];
148 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
149 }
150
151 virtual void Enable( bool enable )
152 {
153 }
154
155 virtual void SetTitle( const wxString& text )
156 {
157 wxCFStringRef cfText(text);
158 [m_osxMenu setTitle:cfText.AsNSString()];
159 }
160
2cb5d2d2
SC
161 virtual void PopUp( wxWindow *win, int x, int y )
162 {
163 win->ScreenToClient( &x , &y ) ;
164 NSView *view = win->GetPeer()->GetWXWidget();
165 NSRect frame = [view frame];
166 frame.origin.x = x;
167 frame.origin.y = y;
168 frame.size.width = 1;
169 frame.size.height = 1;
170 NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
171 [popUpButtonCell setAutoenablesItems:NO];
172 [popUpButtonCell setAltersStateOfSelectedItem:NO];
173 [popUpButtonCell setMenu:m_osxMenu];
174 [popUpButtonCell selectItem:nil];
175 [popUpButtonCell performClickWithFrame:frame inView:view];
176 [popUpButtonCell release];
177 }
178
dbeddfb9
SC
179 WXHMENU GetHMenu() { return m_osxMenu; }
180
181 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
182 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
183protected :
184 NSMenu* m_osxMenu;
185} ;
186
187wxMenuCocoaImpl::~wxMenuCocoaImpl()
188{
189 [m_osxMenu release];
190}
191
192wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
193{
194 static wxNSMenuController* controller = NULL;
195 if ( controller == NULL )
196 {
197 controller = [[wxNSMenuController alloc] init];
198 }
199 wxCFStringRef cfText( title );
200 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
201 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
202 [menu setDelegate:controller];
203 [menu setImplementation:c];
204 return c;
205}