]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/menu.mm
A few checks for null pointers to avoid problems during app shutdown.
[wxWidgets.git] / src / osx / cocoa / menu.mm
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();
94 wxpeer->HandleMenuOpened();
95 }
96 }
97
98 - (void)menuDidClose:(NSMenu *)smenu
99 {
100 wxNSMenu* menu = (wxNSMenu*) smenu;
101 wxMenuImpl* menuimpl = [menu implementation];
102 if ( menuimpl )
103 {
104 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
105 wxpeer->HandleMenuClosed();
106 }
107 }
108
109 - (void)menu:(NSMenu *)smenu willHighlightItem:(NSMenuItem *)item
110 {
111 wxNSMenu* menu = (wxNSMenu*) smenu;
112 wxMenuImpl* menuimpl = [menu implementation];
113 if ( menuimpl )
114 {
115 wxMenu* wxpeer = (wxMenu*) menuimpl->GetWXPeer();
116 if ( [ item isKindOfClass:[wxNSMenuItem class] ] )
117 {
118 wxMenuItemImpl* menuitemimpl = (wxMenuItemImpl*) [ (wxNSMenuItem*) item implementation ];
119 if ( wxpeer && menuitemimpl )
120 {
121 wxpeer->HandleMenuItemHighlighted( menuitemimpl->GetWXPeer() );
122 }
123 }
124 }
125 }
126
127 @end
128
129 class wxMenuCocoaImpl : public wxMenuImpl
130 {
131 public :
132 wxMenuCocoaImpl( wxMenu* peer , NSMenu* menu) : wxMenuImpl(peer), m_osxMenu(menu)
133 {
134 }
135
136 virtual ~wxMenuCocoaImpl();
137
138 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
139 {
140 if ( pos == (size_t) -1 )
141 [m_osxMenu addItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() ];
142 else
143 [m_osxMenu insertItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem() atIndex:pos];
144 }
145
146 virtual void Remove( wxMenuItem *pItem )
147 {
148 [m_osxMenu removeItem:(NSMenuItem*) pItem->GetPeer()->GetHMenuItem()];
149 }
150
151 virtual void MakeRoot()
152 {
153 [NSApp setMainMenu:m_osxMenu];
154 [NSApp setAppleMenu:[[m_osxMenu itemAtIndex:0] submenu]];
155 }
156
157 virtual void Enable( bool enable )
158 {
159 }
160
161 virtual void SetTitle( const wxString& text )
162 {
163 wxCFStringRef cfText(text);
164 [m_osxMenu setTitle:cfText.AsNSString()];
165 }
166
167 WXHMENU GetHMenu() { return m_osxMenu; }
168
169 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
170 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
171 protected :
172 NSMenu* m_osxMenu;
173 } ;
174
175 wxMenuCocoaImpl::~wxMenuCocoaImpl()
176 {
177 [m_osxMenu release];
178 }
179
180 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
181 {
182 static wxNSMenuController* controller = NULL;
183 if ( controller == NULL )
184 {
185 controller = [[wxNSMenuController alloc] init];
186 }
187 wxCFStringRef cfText( title );
188 wxNSMenu* menu = [[wxNSMenu alloc] initWithTitle:cfText.AsNSString()];
189 wxMenuImpl* c = new wxMenuCocoaImpl( peer, menu );
190 [menu setDelegate:controller];
191 [menu setImplementation:c];
192 return c;
193 }