]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/menu.cpp
Compile fix
[wxWidgets.git] / src / osx / carbon / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
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 #include "wx/stockitem.h"
33
34 // other standard headers
35 // ----------------------
36 #include <string.h>
37
38 // under carbon there's no such thing as a MenuItemRef, everything is done
39 // on the 'parent' menu via index APIs (first line having index 1 !)
40 // so to make things still work, we store the wxMenuItemImpl instance as a
41 // RefCon at the respective menu line
42
43 class wxMenuItemCarbonImpl : public wxMenuItemImpl
44 {
45 public :
46 wxMenuItemCarbonImpl( wxMenuItem* peer ) : wxMenuItemImpl(peer)
47 {
48 // the parent menu ref is only set, once the item has been attached
49 m_parentMenuRef = NULL;
50 }
51
52 ~wxMenuItemCarbonImpl();
53
54 void SetBitmap( const wxBitmap& bitmap )
55 {
56 MenuItemIndex i = FindMenuItemIndex() ;
57 if ( i > 0 )
58 {
59 if ( bitmap.Ok() )
60 {
61 #if wxUSE_BMPBUTTON
62 ControlButtonContentInfo info ;
63 wxMacCreateBitmapButton( &info , bitmap ) ;
64 if ( info.contentType != kControlNoContent )
65 {
66 if ( info.contentType == kControlContentIconRef )
67 SetMenuItemIconHandle( m_parentMenuRef, i ,
68 kMenuIconRefType , (Handle) info.u.iconRef ) ;
69 else if ( info.contentType == kControlContentCGImageRef )
70 SetMenuItemIconHandle( m_parentMenuRef, i ,
71 kMenuCGImageRefType , (Handle) info.u.imageRef ) ;
72 }
73 wxMacReleaseBitmapButton( &info ) ;
74 #endif
75 }
76 }
77 }
78
79 void Enable( bool enable )
80 {
81 MenuItemIndex i = FindMenuItemIndex() ;
82 if ( i > 0 )
83 {
84
85 if ( GetWXPeer()->GetId() == wxApp::s_macPreferencesMenuItemId)
86 {
87 if ( enable )
88 EnableMenuCommand( NULL , kHICommandPreferences ) ;
89 else
90 DisableMenuCommand( NULL , kHICommandPreferences ) ;
91 }
92 else if ( GetWXPeer()->GetId() == wxApp::s_macExitMenuItemId)
93 {
94 if ( enable )
95 EnableMenuCommand( NULL , kHICommandQuit ) ;
96 else
97 DisableMenuCommand( NULL , kHICommandQuit ) ;
98 }
99
100 if ( enable )
101 EnableMenuItem(m_parentMenuRef , i);
102 else
103 DisableMenuItem(m_parentMenuRef , i);
104
105 if ( GetWXPeer()->IsSubMenu() )
106 {
107 UMAEnableMenuItem( GetWXPeer()->GetSubMenu()->GetHMenu() , 0 , enable ) ;
108 }
109 }
110 }
111
112 void Check( bool check )
113 {
114 MenuItemIndex i = FindMenuItemIndex() ;
115 if ( i > 0 )
116 {
117 if ( check )
118 ::SetItemMark( m_parentMenuRef, i, 0x12 ) ; // checkmark
119 else
120 ::SetItemMark( m_parentMenuRef, i, 0 ) ; // no mark
121 }
122 }
123
124 void Hide( bool hide )
125 {
126 MenuItemIndex i = FindMenuItemIndex() ;
127 if ( i > 0 )
128 {
129 if ( hide )
130 ChangeMenuItemAttributes( m_parentMenuRef, i, kMenuItemAttrHidden, 0 );
131 else
132 ChangeMenuItemAttributes( m_parentMenuRef, i, 0 , kMenuItemAttrHidden );
133 }
134 }
135
136 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
137 {
138 MenuItemIndex i = FindMenuItemIndex() ;
139 if ( i > 0 )
140 {
141 SetMenuItemTextWithCFString( m_parentMenuRef, i, wxCFStringRef(text));
142 UMASetMenuItemShortcut( m_parentMenuRef, i , entry ) ;
143 }
144 }
145
146 void * GetHMenuItem() { return NULL; }
147
148 // Carbon Only
149
150 void AttachToParent( MenuRef parentMenuRef, MenuItemIndex index )
151 {
152 m_parentMenuRef = parentMenuRef;
153 if ( m_parentMenuRef && index > 0 )
154 SetMenuItemRefCon( m_parentMenuRef, index, (URefCon) this );
155 }
156
157 MenuItemIndex FindMenuItemIndex()
158 {
159 MenuItemIndex hit = 0 ;
160 if ( m_parentMenuRef )
161 {
162 for ( MenuItemIndex i = 1 ; i <= CountMenuItems(m_parentMenuRef) ; ++i )
163 {
164 URefCon storedRef = 0;
165 GetMenuItemRefCon(m_parentMenuRef, i, &storedRef );
166 if ( storedRef == (URefCon) this )
167 {
168 hit = i;
169 break;
170 }
171 }
172 }
173 return hit;
174 }
175 protected :
176 MenuRef m_parentMenuRef;
177 } ;
178
179 //
180 // wxMenuImpl
181 //
182
183 class wxMenuCarbonImpl : public wxMenuImpl
184 {
185 public :
186 wxMenuCarbonImpl( wxMenu* peer , MenuRef menu) : wxMenuImpl(peer), m_osxMenu(menu)
187 {
188 }
189
190 virtual ~wxMenuCarbonImpl();
191
192
193 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
194 {
195 // MacOS counts menu items from 1 and inserts after, therefore having the
196 // same effect as wx 0 based and inserting before, we must correct pos
197 // after however for updates to be correct
198
199 MenuItemIndex index = pos;
200 if ( pos == (size_t) -1 )
201 index = CountMenuItems(m_osxMenu);
202
203 if ( pItem->IsSeparator() )
204 {
205 InsertMenuItemTextWithCFString( m_osxMenu, CFSTR(""), index, kMenuItemAttrSeparator, 0);
206 // now switch to the Carbon 1 based counting
207 index += 1 ;
208 }
209 else
210 {
211 InsertMenuItemTextWithCFString( m_osxMenu, CFSTR("placeholder"), index, 0, 0 );
212
213 // now switch to the Carbon 1 based counting
214 index += 1 ;
215 if ( pItem->IsSubMenu() )
216 {
217 MenuRef submenu = pItem->GetSubMenu()->GetHMenu();
218 SetMenuItemHierarchicalMenu(m_osxMenu, index, submenu);
219 // carbon is using the title of the submenu, eg in the menubar
220 SetMenuTitleWithCFString(submenu, wxCFStringRef(pItem->GetItemLabelText()));
221 }
222 else
223 {
224 SetMenuItemCommandID( m_osxMenu, index , wxIdToMacCommand(pItem->GetId()) ) ;
225 }
226 }
227
228 wxMenuItemCarbonImpl* impl = (wxMenuItemCarbonImpl*) pItem->GetPeer();
229 impl->AttachToParent( m_osxMenu, index );
230 // only now can all settings be updated correctly
231 pItem->UpdateItemText();
232 pItem->UpdateItemStatus();
233 pItem->UpdateItemBitmap();
234 }
235
236 virtual void Remove( wxMenuItem *pItem )
237 {
238 wxMenuItemCarbonImpl* impl = (wxMenuItemCarbonImpl*) pItem->GetPeer();
239 if ( impl )
240 {
241 MenuItemIndex i = impl->FindMenuItemIndex();
242 if ( i > 0 )
243 {
244 DeleteMenuItem(m_osxMenu , i);
245 impl->AttachToParent( NULL, 0 );
246 }
247 }
248 }
249
250 virtual void MakeRoot()
251 {
252 SetRootMenu( m_osxMenu );
253 }
254
255 virtual void SetTitle( const wxString& text )
256 {
257 SetMenuTitleWithCFString(m_osxMenu, wxCFStringRef(text));
258 }
259
260 WXHMENU GetHMenu() { return m_osxMenu; }
261
262 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
263 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
264 protected :
265 wxCFRef<MenuRef> m_osxMenu;
266 } ;
267
268 // static const short kwxMacAppleMenuId = 1 ;
269
270 // Find an item given the Macintosh Menu Reference
271
272 WX_DECLARE_HASH_MAP(WXHMENU, wxMenu*, wxPointerHash, wxPointerEqual, MacMenuMap);
273
274 static MacMenuMap wxWinMacMenuList;
275
276 wxMenu *wxFindMenuFromMacMenu(WXHMENU inMenuRef)
277 {
278 MacMenuMap::iterator node = wxWinMacMenuList.find(inMenuRef);
279
280 return (node == wxWinMacMenuList.end()) ? NULL : node->second;
281 }
282
283 void wxAssociateMenuWithMacMenu(WXHMENU inMenuRef, wxMenu *menu) ;
284 void wxAssociateMenuWithMacMenu(WXHMENU inMenuRef, wxMenu *menu)
285 {
286 // adding NULL MenuRef is (first) surely a result of an error and
287 // (secondly) breaks menu command processing
288 wxCHECK_RET( inMenuRef != (WXHMENU) NULL, wxT("attempt to add a NULL MenuRef to menu list") );
289
290 wxWinMacMenuList[inMenuRef] = menu;
291 }
292
293 void wxRemoveMacMenuAssociation(wxMenu *menu) ;
294 void wxRemoveMacMenuAssociation(wxMenu *menu)
295 {
296 // iterate over all the elements in the class
297 MacMenuMap::iterator it;
298 for ( it = wxWinMacMenuList.begin(); it != wxWinMacMenuList.end(); ++it )
299 {
300 if ( it->second == menu )
301 {
302 wxWinMacMenuList.erase(it);
303 break;
304 }
305 }
306 }
307
308 wxMenuCarbonImpl::~wxMenuCarbonImpl()
309 {
310 wxRemoveMacMenuAssociation( GetWXPeer() );
311 }
312
313 wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
314 {
315 // create the menu
316 static SInt16 s_macNextMenuId = 3;
317 WXHMENU menu = NULL;
318 CreateNewMenu( s_macNextMenuId++ , 0 , &menu ) ;
319 if ( !menu )
320 {
321 wxLogLastError(wxT("CreateNewMenu failed"));
322 return NULL;
323 }
324
325 wxMenuImpl* c = new wxMenuCarbonImpl( peer, menu );
326 c->SetTitle(title);
327 wxAssociateMenuWithMacMenu( menu , peer ) ;
328 return c;
329 }
330
331 //
332 //
333 //
334
335 wxMenuItemCarbonImpl::~wxMenuItemCarbonImpl()
336 {
337 }
338
339
340 wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer,
341 wxMenu * WXUNUSED(pParentMenu),
342 int WXUNUSED(id),
343 const wxString& WXUNUSED(text),
344 wxAcceleratorEntry *WXUNUSED(entry),
345 const wxString& WXUNUSED(strHelp),
346 wxItemKind WXUNUSED(kind),
347 wxMenu *WXUNUSED(pSubMenu) )
348 {
349 wxMenuItemImpl* c = NULL;
350
351 c = new wxMenuItemCarbonImpl( peer );
352 return c;
353 }
354
355 void wxInsertMenuItemsInMenu(wxMenu* menu, MenuRef wm, MenuItemIndex insertAfter)
356 {
357 wxMenuItemList::compatibility_iterator node;
358 wxMenuItem *item;
359 wxMenu *subMenu = NULL ;
360 bool newItems = false;
361
362 for (node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext())
363 {
364 item = (wxMenuItem *)node->GetData();
365 subMenu = item->GetSubMenu() ;
366 if (subMenu)
367 {
368 wxInsertMenuItemsInMenu(subMenu, (MenuRef)subMenu->GetHMenu(), 0);
369 }
370 if ( item->IsSeparator() )
371 {
372 if ( wm && newItems)
373 InsertMenuItemTextWithCFString( wm,
374 CFSTR(""), insertAfter, kMenuItemAttrSeparator, 0);
375
376 newItems = false;
377 }
378 else
379 {
380 wxAcceleratorEntry*
381 entry = wxAcceleratorEntry::Create( item->GetItemLabel() ) ;
382
383 MenuItemIndex winListPos = (MenuItemIndex)-1;
384 OSStatus err = GetIndMenuItemWithCommandID(wm,
385 wxIdToMacCommand ( item->GetId() ), 1, NULL, &winListPos);
386
387 if ( wm && err == menuItemNotFoundErr )
388 {
389 // NB: the only way to determine whether or not we should add
390 // a separator is to know if we've added menu items to the menu
391 // before the separator.
392 newItems = true;
393 UMAInsertMenuItem(wm, wxStripMenuCodes(item->GetItemLabel()) , wxFont::GetDefaultEncoding(), insertAfter, entry);
394 SetMenuItemCommandID( wm , insertAfter+1 , wxIdToMacCommand ( item->GetId() ) ) ;
395 SetMenuItemRefCon( wm , insertAfter+1 , (URefCon) item ) ;
396 }
397
398 delete entry ;
399 }
400 }
401 }
402
403