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"
26 #include "wx/cocoa/objc/objc_uniquifying.h"
35 #include "wx/cocoa/autorelease.h"
36 #include "wx/cocoa/string.h"
38 #import <AppKit/NSMenuItem.h>
39 #import <AppKit/NSMenu.h>
40 #import <Foundation/NSString.h>
41 #import <AppKit/NSCell.h> // NSOnState, NSOffState
42 #import <AppKit/NSEvent.h> // modifier key masks
44 // ----------------------------------------------------------------------------
45 // functions prototypes
46 // ----------------------------------------------------------------------------
48 // ============================================================================
49 // @class wxNSMenuItemTarget
50 // ============================================================================
51 @interface wxNSMenuItemTarget : NSObject
55 - (void)wxMenuItemAction: (id)sender;
56 - (BOOL)validateMenuItem: (id)menuItem;
57 @end //interface wxNSMenuItemTarget
58 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
60 @implementation wxNSMenuItemTarget : NSObject
62 - (void)wxMenuItemAction: (id)sender
64 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
65 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
66 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
67 item->CocoaItemSelected();
70 - (BOOL)validateMenuItem: (id)menuItem
72 // TODO: Do wxWidgets validation here and avoid sending during idle time
73 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
74 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
75 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
76 return item->Cocoa_validateMenuItem();
79 @end //implementation wxNSMenuItemTarget
80 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
82 // ============================================================================
83 // wxMenuItemCocoa implementation
84 // ============================================================================
85 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
87 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
100 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
103 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
105 wxAcceleratorEntry *accel = GetAccel();
109 int accelFlags = accel->GetFlags();
110 int keyModifierMask = 0;
111 if(accelFlags & wxACCEL_ALT)
112 keyModifierMask |= NSAlternateKeyMask;
113 if(accelFlags & wxACCEL_CTRL)
114 keyModifierMask |= NSCommandKeyMask;
115 int keyCode = accel->GetKeyCode();
117 { // For alpha characters use upper/lower rather than NSShiftKeyMask
119 if(accelFlags & wxACCEL_SHIFT)
120 alphaChar = toupper(keyCode);
122 alphaChar = tolower(keyCode);
123 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
124 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
128 if(accelFlags & wxACCEL_SHIFT)
129 keyModifierMask |= NSShiftKeyMask;
130 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
131 { char alphaChar = keyCode;
132 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
137 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
144 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
146 const wxString& strName,
147 const wxString& strHelp,
150 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
152 wxAutoNSAutoreleasePool pool;
153 if(m_kind == wxITEM_SEPARATOR)
154 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
157 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
162 action = @selector(wxMenuItemAction:);
163 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
164 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
167 wxASSERT(pSubMenu->GetNSMenu());
168 [pSubMenu->GetNSMenu() setTitle:menuTitle];
169 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
172 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
174 CocoaSetKeyEquivalent();
178 wxMenuItem::~wxMenuItem()
180 sm_cocoaHash.erase(m_cocoaNSMenuItem);
181 [m_cocoaNSMenuItem release];
184 void wxMenuItem::CocoaItemSelected()
186 wxMenu *menu = GetMenu();
187 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
188 wxMenuBar *menubar = menu->GetMenuBar();
191 wxFrame *frame = menubar->GetFrame();
192 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
193 frame->ProcessCommand(GetId());
199 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
203 bool wxMenuItem::Cocoa_validateMenuItem()
205 // TODO: do more sanity checking
206 // TODO: Do wxWindows validation here and avoid sending during idle time
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
215 const wxBitmap& bmpUnchecked)
217 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
218 wxAutoNSAutoreleasePool pool;
219 m_bmpChecked = bmpChecked;
220 m_bmpUnchecked = bmpUnchecked;
223 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
224 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
228 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
229 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
236 void wxMenuItem::Enable(bool bDoEnable)
238 wxMenuItemBase::Enable(bDoEnable);
239 // NOTE: Nothing to do, we respond to validateMenuItem instead
242 void wxMenuItem::Check(bool check)
244 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
245 if(m_isChecked == check)
247 wxAutoNSAutoreleasePool pool;
248 if(GetKind() == wxITEM_RADIO)
250 // it doesn't make sense to uncheck a radio item - what would this do?
253 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
254 // First search backwards for other radio items
255 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
256 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
257 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
258 prevNode = prevNode->GetPrevious())
260 radioStart = prevNode;
262 // Now starting there set the state of every item until we're
263 // out of radio items to set.
264 for(wxMenuItemList::compatibility_iterator node = radioStart;
265 node && (node->GetData()->GetKind() == wxITEM_RADIO);
266 node = node->GetNext())
268 wxMenuItem *item = node->GetData();
269 bool checkItem = (item == this);
270 item->wxMenuItemBase::Check(checkItem);
271 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
274 else // normal check (non-radio) item
276 wxMenuItemBase::Check(check);
277 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
281 void wxMenuItem::SetItemLabel(const wxString& label)
283 wxMenuItemBase::SetItemLabel(label);
284 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
285 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
286 CocoaSetKeyEquivalent();
289 void wxMenuItem::SetCheckable(bool checkable)
291 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
292 wxMenuItemBase::SetCheckable(checkable);
293 // NOTE: Cocoa does not discern between unchecked and normal items
296 #endif // wxUSE_MENUS