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