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 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
86 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
88 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[WX_GET_OBJC_CLASS(wxNSMenuItemTarget) alloc] init];
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
101 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
105 wxString wxMenuItemBase::GetLabelText(const wxString& text)
107 return wxStripMenuCodes(text);
110 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
112 wxAcceleratorEntry *accel = GetAccel();
116 int accelFlags = accel->GetFlags();
117 int keyModifierMask = 0;
118 if(accelFlags & wxACCEL_ALT)
119 keyModifierMask |= NSAlternateKeyMask;
120 if(accelFlags & wxACCEL_CTRL)
121 keyModifierMask |= NSCommandKeyMask;
122 int keyCode = accel->GetKeyCode();
124 { // For alpha characters use upper/lower rather than NSShiftKeyMask
126 if(accelFlags & wxACCEL_SHIFT)
127 alphaChar = toupper(keyCode);
129 alphaChar = tolower(keyCode);
130 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
131 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
135 if(accelFlags & wxACCEL_SHIFT)
136 keyModifierMask |= NSShiftKeyMask;
137 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
138 { char alphaChar = keyCode;
139 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
144 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
151 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
153 const wxString& strName,
154 const wxString& strHelp,
157 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
159 wxAutoNSAutoreleasePool pool;
160 if(m_kind == wxITEM_SEPARATOR)
161 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
164 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
169 action = @selector(wxMenuItemAction:);
170 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
171 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
174 wxASSERT(pSubMenu->GetNSMenu());
175 [pSubMenu->GetNSMenu() setTitle:menuTitle];
176 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
179 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
181 CocoaSetKeyEquivalent();
185 wxMenuItem::~wxMenuItem()
187 sm_cocoaHash.erase(m_cocoaNSMenuItem);
188 [m_cocoaNSMenuItem release];
191 void wxMenuItem::CocoaItemSelected()
193 wxMenu *menu = GetMenu();
194 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
195 wxMenuBar *menubar = menu->GetMenuBar();
198 wxFrame *frame = menubar->GetFrame();
199 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
200 frame->ProcessCommand(GetId());
206 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
210 bool wxMenuItem::Cocoa_validateMenuItem()
212 // TODO: do more sanity checking
213 // TODO: Do wxWindows validation here and avoid sending during idle time
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
222 const wxBitmap& bmpUnchecked)
224 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
225 wxAutoNSAutoreleasePool pool;
226 m_bmpChecked = bmpChecked;
227 m_bmpUnchecked = bmpUnchecked;
230 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
231 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
235 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
236 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
243 void wxMenuItem::Enable(bool bDoEnable)
245 wxMenuItemBase::Enable(bDoEnable);
246 // NOTE: Nothing to do, we respond to validateMenuItem instead
249 void wxMenuItem::Check(bool check)
251 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
252 if(m_isChecked == check)
254 wxAutoNSAutoreleasePool pool;
255 if(GetKind() == wxITEM_RADIO)
257 // it doesn't make sense to uncheck a radio item - what would this do?
260 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
261 // First search backwards for other radio items
262 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
263 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
264 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
265 prevNode = prevNode->GetPrevious())
267 radioStart = prevNode;
269 // Now starting there set the state of every item until we're
270 // out of radio items to set.
271 for(wxMenuItemList::compatibility_iterator node = radioStart;
272 node && (node->GetData()->GetKind() == wxITEM_RADIO);
273 node = node->GetNext())
275 wxMenuItem *item = node->GetData();
276 bool checkItem = (item == this);
277 item->wxMenuItemBase::Check(checkItem);
278 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
281 else // normal check (non-radio) item
283 wxMenuItemBase::Check(check);
284 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
288 void wxMenuItem::SetItemLabel(const wxString& label)
290 wxMenuItemBase::SetItemLabel(label);
291 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
292 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
293 CocoaSetKeyEquivalent();
296 void wxMenuItem::SetCheckable(bool checkable)
298 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
299 wxMenuItemBase::SetCheckable(checkable);
300 // NOTE: Cocoa does not discern between unchecked and normal items
303 #endif // wxUSE_MENUS