]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/carbon/menu.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / osx / carbon / menu.cpp
... / ...
CommitLineData
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// Copyright: (c) Stefan Csomor
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// headers & declarations
13// ============================================================================
14
15// wxWidgets headers
16// -----------------
17
18#include "wx/wxprec.h"
19
20#include "wx/menu.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/app.h"
25 #include "wx/utils.h"
26 #include "wx/frame.h"
27 #include "wx/menuitem.h"
28#endif
29
30#include "wx/osx/private.h"
31#include "wx/stockitem.h"
32
33// other standard headers
34// ----------------------
35#include <string.h>
36
37// under carbon there's no such thing as a MenuItemRef, everything is done
38// on the 'parent' menu via index APIs (first line having index 1 !)
39// so to make things still work, we store the wxMenuItemImpl instance as a
40// RefCon at the respective menu line
41
42class wxMenuItemCarbonImpl : public wxMenuItemImpl
43{
44public :
45 wxMenuItemCarbonImpl( wxMenuItem* peer ) : wxMenuItemImpl(peer)
46 {
47 // the parent menu ref is only set, once the item has been attached
48 m_parentMenuRef = NULL;
49 }
50
51 ~wxMenuItemCarbonImpl();
52
53 void SetBitmap( const wxBitmap& bitmap )
54 {
55 MenuItemIndex i = FindMenuItemIndex() ;
56 if ( i > 0 )
57 {
58 if ( bitmap.IsOk() )
59 {
60#if wxUSE_BMPBUTTON
61 ControlButtonContentInfo info ;
62 wxMacCreateBitmapButton( &info , bitmap ) ;
63 if ( info.contentType != kControlNoContent )
64 {
65 if ( info.contentType == kControlContentIconRef )
66 SetMenuItemIconHandle( m_parentMenuRef, i ,
67 kMenuIconRefType , (Handle) info.u.iconRef ) ;
68 else if ( info.contentType == kControlContentCGImageRef )
69 SetMenuItemIconHandle( m_parentMenuRef, i ,
70 kMenuCGImageRefType , (Handle) info.u.imageRef ) ;
71 }
72 wxMacReleaseBitmapButton( &info ) ;
73#endif
74 }
75 }
76 }
77
78 void Enable( bool enable )
79 {
80 MenuItemIndex i = FindMenuItemIndex() ;
81 if ( i > 0 )
82 {
83
84 if ( GetWXPeer()->GetId() == wxApp::s_macPreferencesMenuItemId)
85 {
86 if ( enable )
87 EnableMenuCommand( NULL , kHICommandPreferences ) ;
88 else
89 DisableMenuCommand( NULL , kHICommandPreferences ) ;
90 }
91 else if ( GetWXPeer()->GetId() == wxApp::s_macExitMenuItemId)
92 {
93 if ( enable )
94 EnableMenuCommand( NULL , kHICommandQuit ) ;
95 else
96 DisableMenuCommand( NULL , kHICommandQuit ) ;
97 }
98
99 if ( enable )
100 EnableMenuItem(m_parentMenuRef , i);
101 else
102 DisableMenuItem(m_parentMenuRef , i);
103
104 if ( GetWXPeer()->IsSubMenu() )
105 {
106 UMAEnableMenuItem( GetWXPeer()->GetSubMenu()->GetHMenu() , 0 , enable ) ;
107 }
108 }
109 }
110
111 void Check( bool check )
112 {
113 MenuItemIndex i = FindMenuItemIndex() ;
114 if ( i > 0 )
115 {
116 if ( check )
117 ::SetItemMark( m_parentMenuRef, i, 0x12 ) ; // checkmark
118 else
119 ::SetItemMark( m_parentMenuRef, i, 0 ) ; // no mark
120 }
121 }
122
123 void Hide( bool hide )
124 {
125 MenuItemIndex i = FindMenuItemIndex() ;
126 if ( i > 0 )
127 {
128 if ( hide )
129 ChangeMenuItemAttributes( m_parentMenuRef, i, kMenuItemAttrHidden, 0 );
130 else
131 ChangeMenuItemAttributes( m_parentMenuRef, i, 0 , kMenuItemAttrHidden );
132 }
133 }
134
135 void SetLabel( const wxString& text, wxAcceleratorEntry *entry )
136 {
137 MenuItemIndex i = FindMenuItemIndex() ;
138 if ( i > 0 )
139 {
140 SetMenuItemTextWithCFString( m_parentMenuRef, i, wxCFStringRef(text));
141 UMASetMenuItemShortcut( m_parentMenuRef, i , entry ) ;
142 }
143 }
144
145 void * GetHMenuItem() { return NULL; }
146
147 // Carbon Only
148
149 void AttachToParent( MenuRef parentMenuRef, MenuItemIndex index )
150 {
151 m_parentMenuRef = parentMenuRef;
152 if ( m_parentMenuRef && index > 0 )
153 SetMenuItemRefCon( m_parentMenuRef, index, (URefCon) m_peer );
154 }
155
156 MenuItemIndex FindMenuItemIndex()
157 {
158 MenuItemIndex hit = 0 ;
159 if ( m_parentMenuRef )
160 {
161 for ( MenuItemIndex i = 1 ; i <= CountMenuItems(m_parentMenuRef) ; ++i )
162 {
163 URefCon storedRef = 0;
164 GetMenuItemRefCon(m_parentMenuRef, i, &storedRef );
165 if ( storedRef == (URefCon) m_peer )
166 {
167 hit = i;
168 break;
169 }
170 }
171 }
172 return hit;
173 }
174protected :
175 MenuRef m_parentMenuRef;
176} ;
177
178//
179// wxMenuImpl
180//
181
182class wxMenuCarbonImpl : public wxMenuImpl
183{
184public :
185 wxMenuCarbonImpl( wxMenu* peer , MenuRef menu , MenuRef oldMenu , SInt16 menuId)
186 : wxMenuImpl(peer), m_osxMenu(menu), m_oldMenuRef(oldMenu), m_menuId(menuId)
187 {
188 }
189
190 virtual ~wxMenuCarbonImpl();
191
192 virtual void InsertOrAppend(wxMenuItem *pItem, size_t pos)
193 {
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
197
198 MenuItemIndex index = pos;
199 if ( pos == (size_t) -1 )
200 index = CountMenuItems(m_osxMenu);
201
202 if ( pItem->IsSeparator() )
203 {
204 InsertMenuItemTextWithCFString( m_osxMenu, CFSTR(""), index, kMenuItemAttrSeparator, 0);
205 // now switch to the Carbon 1 based counting
206 index += 1 ;
207 }
208 else
209 {
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() )
215 {
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
219 SetMenuTitleWithCFString(submenu, wxCFStringRef(pItem->GetItemLabelText()));
220 }
221 else
222 {
223 SetMenuItemCommandID( m_osxMenu, index , wxIdToMacCommand(pItem->GetId()) ) ;
224 }
225 }
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 )
239 {
240 MenuItemIndex i = impl->FindMenuItemIndex();
241 if ( i > 0 )
242 {
243 DeleteMenuItem(m_osxMenu , i);
244 impl->AttachToParent( NULL, 0 );
245 }
246 }
247 }
248
249 virtual void MakeRoot()
250 {
251 SetRootMenu( m_osxMenu );
252 }
253
254 virtual void SetTitle( const wxString& text )
255 {
256 SetMenuTitleWithCFString(m_osxMenu, wxCFStringRef(text));
257 }
258
259 WXHMENU GetHMenu() { return m_osxMenu; }
260
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
279 static wxMenuImpl* Create( wxMenu* peer, const wxString& title );
280 static wxMenuImpl* CreateRootMenu( wxMenu* peer );
281protected :
282 wxCFRef<MenuRef> m_osxMenu;
283 MenuRef m_oldMenuRef;
284 SInt16 m_menuId;
285} ;
286
287// static const short kwxMacAppleMenuId = 1 ;
288
289// Find an item given the Macintosh Menu Reference
290
291WX_DECLARE_HASH_MAP(WXHMENU, wxMenu*, wxPointerHash, wxPointerEqual, MacMenuMap);
292
293static MacMenuMap wxWinMacMenuList;
294
295wxMenu *wxFindMenuFromMacMenu(WXHMENU inMenuRef)
296{
297 MacMenuMap::iterator node = wxWinMacMenuList.find(inMenuRef);
298
299 return (node == wxWinMacMenuList.end()) ? NULL : node->second;
300}
301
302void wxAssociateMenuWithMacMenu(WXHMENU inMenuRef, wxMenu *menu) ;
303void wxAssociateMenuWithMacMenu(WXHMENU inMenuRef, wxMenu *menu)
304{
305 // adding NULL MenuRef is (first) surely a result of an error and
306 // (secondly) breaks menu command processing
307 wxCHECK_RET( inMenuRef != (WXHMENU) NULL, wxT("attempt to add a NULL MenuRef to menu list") );
308
309 wxWinMacMenuList[inMenuRef] = menu;
310}
311
312void wxRemoveMacMenuAssociation(wxMenu *menu) ;
313void wxRemoveMacMenuAssociation(wxMenu *menu)
314{
315 // iterate over all the elements in the class
316 MacMenuMap::iterator it;
317 for ( it = wxWinMacMenuList.begin(); it != wxWinMacMenuList.end(); ++it )
318 {
319 if ( it->second == menu )
320 {
321 wxWinMacMenuList.erase(it);
322 break;
323 }
324 }
325}
326
327wxMenuCarbonImpl::~wxMenuCarbonImpl()
328{
329 wxRemoveMacMenuAssociation( GetWXPeer() );
330 // restore previous menu
331 m_osxMenu.reset();
332 if ( m_menuId > 0 )
333 MacDeleteMenu(m_menuId);
334 if ( m_oldMenuRef )
335 MacInsertMenu(m_oldMenuRef, -1);
336}
337
338wxMenuImpl* wxMenuImpl::Create( wxMenu* peer, const wxString& title )
339{
340 // create the menu
341 static SInt16 s_macNextMenuId = 3;
342 SInt16 menuId = s_macNextMenuId++;
343 // save existing menu in case we're embedding into an application
344 // or sharing outside UI elements.
345 WXHMENU oldMenu = GetMenuHandle(menuId);
346 if ( oldMenu )
347 MacDeleteMenu(menuId);
348 WXHMENU menu = NULL;
349 CreateNewMenu( menuId , 0 , &menu ) ;
350 if ( !menu )
351 {
352 wxLogLastError(wxT("CreateNewMenu failed"));
353 if ( oldMenu )
354 MacInsertMenu(oldMenu, -1);
355 return NULL;
356 }
357
358 wxMenuImpl* c = new wxMenuCarbonImpl( peer, menu, oldMenu, menuId );
359 c->SetTitle(title);
360 wxAssociateMenuWithMacMenu( menu , peer ) ;
361 return c;
362}
363
364//
365//
366//
367
368wxMenuItemCarbonImpl::~wxMenuItemCarbonImpl()
369{
370}
371
372
373wxMenuItemImpl* wxMenuItemImpl::Create( wxMenuItem* peer,
374 wxMenu * WXUNUSED(pParentMenu),
375 int WXUNUSED(id),
376 const wxString& WXUNUSED(text),
377 wxAcceleratorEntry *WXUNUSED(entry),
378 const wxString& WXUNUSED(strHelp),
379 wxItemKind WXUNUSED(kind),
380 wxMenu *WXUNUSED(pSubMenu) )
381{
382 wxMenuItemImpl* c = NULL;
383
384 c = new wxMenuItemCarbonImpl( peer );
385 return c;
386}
387
388void wxInsertMenuItemsInMenu(wxMenu* menu, MenuRef wm, MenuItemIndex insertAfter)
389{
390 wxMenuItemList::compatibility_iterator node;
391 wxMenuItem *item;
392 wxMenu *subMenu = NULL ;
393 bool newItems = false;
394
395 for (node = menu->GetMenuItems().GetFirst(); node; node = node->GetNext())
396 {
397 item = (wxMenuItem *)node->GetData();
398 subMenu = item->GetSubMenu() ;
399 if (subMenu)
400 {
401 wxInsertMenuItemsInMenu(subMenu, (MenuRef)subMenu->GetHMenu(), 0);
402 }
403 if ( item->IsSeparator() )
404 {
405 if ( wm && newItems)
406 InsertMenuItemTextWithCFString( wm,
407 CFSTR(""), insertAfter, kMenuItemAttrSeparator, 0);
408
409 newItems = false;
410 }
411 else
412 {
413 wxAcceleratorEntry*
414 entry = wxAcceleratorEntry::Create( item->GetItemLabel() ) ;
415
416 MenuItemIndex winListPos = (MenuItemIndex)-1;
417 OSStatus err = GetIndMenuItemWithCommandID(wm,
418 wxIdToMacCommand ( item->GetId() ), 1, NULL, &winListPos);
419
420 if ( wm && err == menuItemNotFoundErr )
421 {
422 // NB: the only way to determine whether or not we should add
423 // a separator is to know if we've added menu items to the menu
424 // before the separator.
425 newItems = true;
426 UMAInsertMenuItem(wm, wxStripMenuCodes(item->GetItemLabel()) , wxFont::GetDefaultEncoding(), insertAfter, entry);
427 SetMenuItemCommandID( wm , insertAfter+1 , wxIdToMacCommand ( item->GetId() ) ) ;
428 SetMenuItemRefCon( wm , insertAfter+1 , (URefCon) item ) ;
429 }
430
431 delete entry ;
432 }
433 }
434}
435
436