1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/menuitem.mm
3 // Purpose: wxMenuItem implementation
4 // Author: David Elliott
8 // Copyright: 2002-2004 David Elliott
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
24 #include "wx/menuitem.h"
33 #include "wx/cocoa/autorelease.h"
34 #include "wx/cocoa/string.h"
36 #import <AppKit/NSMenuItem.h>
37 #import <AppKit/NSMenu.h>
38 #import <Foundation/NSString.h>
39 #import <AppKit/NSCell.h> // NSOnState, NSOffState
40 #import <AppKit/NSEvent.h> // modifier key masks
42 // ----------------------------------------------------------------------------
43 // functions prototypes
44 // ----------------------------------------------------------------------------
46 // ============================================================================
47 // @class wxNSMenuItemTarget
48 // ============================================================================
49 @interface wxNSMenuItemTarget : NSObject
53 - (void)wxMenuItemAction: (id)sender;
54 - (BOOL)validateMenuItem: (id)menuItem;
55 @end //interface wxNSMenuItemTarget
57 @implementation wxNSMenuItemTarget : NSObject
59 - (void)wxMenuItemAction: (id)sender
61 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
62 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
63 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
64 item->CocoaItemSelected();
67 - (BOOL)validateMenuItem: (id)menuItem
69 // TODO: Do wxWidgets validation here and avoid sending during idle time
70 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
71 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
72 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
73 return item->Cocoa_validateMenuItem();
76 @end //implementation wxNSMenuItemTarget
78 // ============================================================================
79 // wxMenuItemCocoa implementation
80 // ============================================================================
81 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
82 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
84 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
97 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
101 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
103 return wxStripMenuCodes(text);
106 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
108 wxAcceleratorEntry *accel = GetAccel();
112 int accelFlags = accel->GetFlags();
113 int keyModifierMask = 0;
114 if(accelFlags & wxACCEL_ALT)
115 keyModifierMask |= NSAlternateKeyMask;
116 if(accelFlags & wxACCEL_CTRL)
117 keyModifierMask |= NSCommandKeyMask;
118 int keyCode = accel->GetKeyCode();
120 { // For alpha characters use upper/lower rather than NSShiftKeyMask
122 if(accelFlags & wxACCEL_SHIFT)
123 alphaChar = toupper(keyCode);
125 alphaChar = tolower(keyCode);
126 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
127 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
131 if(accelFlags & wxACCEL_SHIFT)
132 keyModifierMask |= NSShiftKeyMask;
133 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
134 { char alphaChar = keyCode;
135 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
140 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
147 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
149 const wxString& strName,
150 const wxString& strHelp,
153 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
155 wxAutoNSAutoreleasePool pool;
156 if(m_kind == wxITEM_SEPARATOR)
157 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
160 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
165 action = @selector(wxMenuItemAction:);
166 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
167 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
170 wxASSERT(pSubMenu->GetNSMenu());
171 [pSubMenu->GetNSMenu() setTitle:menuTitle];
172 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
175 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
177 CocoaSetKeyEquivalent();
181 wxMenuItem::~wxMenuItem()
183 sm_cocoaHash.erase(m_cocoaNSMenuItem);
184 [m_cocoaNSMenuItem release];
187 void wxMenuItem::CocoaItemSelected()
189 wxMenu *menu = GetMenu();
190 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
191 wxMenuBar *menubar = menu->GetMenuBar();
194 wxFrame *frame = menubar->GetFrame();
195 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
196 frame->ProcessCommand(GetId());
202 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
206 bool wxMenuItem::Cocoa_validateMenuItem()
208 // TODO: do more sanity checking
209 // TODO: Do wxWindows validation here and avoid sending during idle time
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
218 const wxBitmap& bmpUnchecked)
220 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
221 wxAutoNSAutoreleasePool pool;
222 m_bmpChecked = bmpChecked;
223 m_bmpUnchecked = bmpUnchecked;
226 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
227 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
231 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
232 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
239 void wxMenuItem::Enable(bool bDoEnable)
241 wxMenuItemBase::Enable(bDoEnable);
242 // NOTE: Nothing to do, we respond to validateMenuItem instead
245 void wxMenuItem::Check(bool check)
247 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
248 if(m_isChecked == check)
250 wxAutoNSAutoreleasePool pool;
251 if(GetKind() == wxITEM_RADIO)
253 // it doesn't make sense to uncheck a radio item - what would this do?
256 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
257 // First search backwards for other radio items
258 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
259 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
260 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
261 prevNode = prevNode->GetPrevious())
263 radioStart = prevNode;
265 // Now starting there set the state of every item until we're
266 // out of radio items to set.
267 for(wxMenuItemList::compatibility_iterator node = radioStart;
268 node && (node->GetData()->GetKind() == wxITEM_RADIO);
269 node = node->GetNext())
271 wxMenuItem *item = node->GetData();
272 bool checkItem = (item == this);
273 item->wxMenuItemBase::Check(checkItem);
274 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
277 else // normal check (non-radio) item
279 wxMenuItemBase::Check(check);
280 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
284 void wxMenuItem::SetText(const wxString& label)
286 wxMenuItemBase::SetText(label);
287 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
288 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
289 CocoaSetKeyEquivalent();
292 void wxMenuItem::SetCheckable(bool checkable)
294 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
295 wxMenuItemBase::SetCheckable(checkable);
296 // NOTE: Cocoa does not discern between unchecked and normal items
299 #endif // wxUSE_MENUS