1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/menuitem.mm
3 // Purpose: wxMenuItem implementation
4 // Author: David Elliott
7 // Copyright: 2002-2004 David Elliott
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
23 #include "wx/menuitem.h"
25 #include "wx/cocoa/objc/objc_uniquifying.h"
34 #include "wx/cocoa/autorelease.h"
35 #include "wx/cocoa/string.h"
37 #import <AppKit/NSMenuItem.h>
38 #import <AppKit/NSMenu.h>
39 #import <Foundation/NSString.h>
40 #import <AppKit/NSCell.h> // NSOnState, NSOffState
41 #import <AppKit/NSEvent.h> // modifier key masks
43 // ----------------------------------------------------------------------------
44 // functions prototypes
45 // ----------------------------------------------------------------------------
47 // ============================================================================
48 // @class wxNSMenuItemTarget
49 // ============================================================================
50 @interface wxNSMenuItemTarget : NSObject
54 - (void)wxMenuItemAction: (id)sender;
55 - (BOOL)validateMenuItem: (id)menuItem;
56 @end //interface wxNSMenuItemTarget
57 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
59 @implementation wxNSMenuItemTarget : NSObject
61 - (void)wxMenuItemAction: (id)sender
63 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
64 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
65 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
66 item->CocoaItemSelected();
69 - (BOOL)validateMenuItem: (id)menuItem
71 // TODO: Do wxWidgets validation here and avoid sending during idle time
72 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
73 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
74 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
75 return item->Cocoa_validateMenuItem();
78 @end //implementation wxNSMenuItemTarget
79 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuItemTarget,NSObject)
81 // ============================================================================
82 // wxMenuItemCocoa implementation
83 // ============================================================================
84 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
86 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
99 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
102 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
104 wxAcceleratorEntry *accel = GetAccel();
108 int accelFlags = accel->GetFlags();
109 int keyModifierMask = 0;
110 if(accelFlags & wxACCEL_ALT)
111 keyModifierMask |= NSAlternateKeyMask;
112 if(accelFlags & wxACCEL_CTRL)
113 keyModifierMask |= NSCommandKeyMask;
114 int keyCode = accel->GetKeyCode();
116 { // For alpha characters use upper/lower rather than NSShiftKeyMask
118 if(accelFlags & wxACCEL_SHIFT)
119 alphaChar = toupper(keyCode);
121 alphaChar = tolower(keyCode);
122 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
123 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
127 if(accelFlags & wxACCEL_SHIFT)
128 keyModifierMask |= NSShiftKeyMask;
129 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
130 { char alphaChar = keyCode;
131 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
136 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
143 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
145 const wxString& strName,
146 const wxString& strHelp,
149 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
151 wxAutoNSAutoreleasePool pool;
152 if(m_kind == wxITEM_SEPARATOR)
153 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
156 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
161 action = @selector(wxMenuItemAction:);
162 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
163 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
166 wxASSERT(pSubMenu->GetNSMenu());
167 [pSubMenu->GetNSMenu() setTitle:menuTitle];
168 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
171 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
173 CocoaSetKeyEquivalent();
177 wxMenuItem::~wxMenuItem()
179 sm_cocoaHash.erase(m_cocoaNSMenuItem);
180 [m_cocoaNSMenuItem release];
183 void wxMenuItem::CocoaItemSelected()
185 wxMenu *menu = GetMenu();
186 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
187 wxMenuBar *menubar = menu->GetMenuBar();
190 wxFrame *frame = menubar->GetFrame();
191 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
192 frame->ProcessCommand(GetId());
198 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
202 bool wxMenuItem::Cocoa_validateMenuItem()
204 // TODO: do more sanity checking
205 // TODO: Do wxWindows validation here and avoid sending during idle time
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
214 const wxBitmap& bmpUnchecked)
216 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
217 wxAutoNSAutoreleasePool pool;
218 m_bmpChecked = bmpChecked;
219 m_bmpUnchecked = bmpUnchecked;
222 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
223 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
227 wxASSERT_MSG(!bmpUnchecked.IsOk(),wxT("Normal menu items should only have one bitmap"));
228 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
235 void wxMenuItem::Enable(bool bDoEnable)
237 wxMenuItemBase::Enable(bDoEnable);
238 // NOTE: Nothing to do, we respond to validateMenuItem instead
241 void wxMenuItem::Check(bool check)
243 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
244 if(m_isChecked == check)
246 wxAutoNSAutoreleasePool pool;
247 if(GetKind() == wxITEM_RADIO)
249 // it doesn't make sense to uncheck a radio item - what would this do?
252 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
253 // First search backwards for other radio items
254 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
255 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
256 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
257 prevNode = prevNode->GetPrevious())
259 radioStart = prevNode;
261 // Now starting there set the state of every item until we're
262 // out of radio items to set.
263 for(wxMenuItemList::compatibility_iterator node = radioStart;
264 node && (node->GetData()->GetKind() == wxITEM_RADIO);
265 node = node->GetNext())
267 wxMenuItem *item = node->GetData();
268 bool checkItem = (item == this);
269 item->wxMenuItemBase::Check(checkItem);
270 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
273 else // normal check (non-radio) item
275 wxMenuItemBase::Check(check);
276 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
280 void wxMenuItem::SetItemLabel(const wxString& label)
282 wxMenuItemBase::SetItemLabel(label);
283 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
284 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
285 CocoaSetKeyEquivalent();
288 void wxMenuItem::SetCheckable(bool checkable)
290 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
291 wxMenuItemBase::SetCheckable(checkable);
292 // NOTE: Cocoa does not discern between unchecked and normal items
295 #endif // wxUSE_MENUS