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/ObjcPose.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
58 @implementation wxNSMenuItemTarget : NSObject
60 - (void)wxMenuItemAction: (id)sender
62 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
63 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
64 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
65 item->CocoaItemSelected();
68 - (BOOL)validateMenuItem: (id)menuItem
70 // TODO: Do wxWidgets validation here and avoid sending during idle time
71 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
72 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
73 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
74 return item->Cocoa_validateMenuItem();
77 @end //implementation wxNSMenuItemTarget
79 // ============================================================================
80 // wxMenuItemCocoa implementation
81 // ============================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
83 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
85 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
98 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
102 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
104 return wxStripMenuCodes(text);
107 void wxMenuItemCocoa::CocoaSetKeyEquivalent()
109 wxAcceleratorEntry *accel = GetAccel();
113 int accelFlags = accel->GetFlags();
114 int keyModifierMask = 0;
115 if(accelFlags & wxACCEL_ALT)
116 keyModifierMask |= NSAlternateKeyMask;
117 if(accelFlags & wxACCEL_CTRL)
118 keyModifierMask |= NSCommandKeyMask;
119 int keyCode = accel->GetKeyCode();
121 { // For alpha characters use upper/lower rather than NSShiftKeyMask
123 if(accelFlags & wxACCEL_SHIFT)
124 alphaChar = toupper(keyCode);
126 alphaChar = tolower(keyCode);
127 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
128 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
132 if(accelFlags & wxACCEL_SHIFT)
133 keyModifierMask |= NSShiftKeyMask;
134 if(keyCode < 128) // low ASCII includes backspace/tab/etc.
135 { char alphaChar = keyCode;
136 [m_cocoaNSMenuItem setKeyEquivalent:[NSString stringWithCString:&alphaChar length:1]];
141 [m_cocoaNSMenuItem setKeyEquivalentModifierMask:keyModifierMask];
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
148 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
150 const wxString& strName,
151 const wxString& strHelp,
154 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
156 wxAutoNSAutoreleasePool pool;
157 if(m_kind == wxITEM_SEPARATOR)
158 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
161 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
166 action = @selector(wxMenuItemAction:);
167 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
168 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
171 wxASSERT(pSubMenu->GetNSMenu());
172 [pSubMenu->GetNSMenu() setTitle:menuTitle];
173 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
176 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
178 CocoaSetKeyEquivalent();
182 wxMenuItem::~wxMenuItem()
184 sm_cocoaHash.erase(m_cocoaNSMenuItem);
185 [m_cocoaNSMenuItem release];
188 void wxMenuItem::CocoaItemSelected()
190 wxMenu *menu = GetMenu();
191 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
192 wxMenuBar *menubar = menu->GetMenuBar();
195 wxFrame *frame = menubar->GetFrame();
196 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
197 frame->ProcessCommand(GetId());
203 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
207 bool wxMenuItem::Cocoa_validateMenuItem()
209 // TODO: do more sanity checking
210 // TODO: Do wxWindows validation here and avoid sending during idle time
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
219 const wxBitmap& bmpUnchecked)
221 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
222 wxAutoNSAutoreleasePool pool;
223 m_bmpChecked = bmpChecked;
224 m_bmpUnchecked = bmpUnchecked;
227 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
228 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
232 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
233 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
240 void wxMenuItem::Enable(bool bDoEnable)
242 wxMenuItemBase::Enable(bDoEnable);
243 // NOTE: Nothing to do, we respond to validateMenuItem instead
246 void wxMenuItem::Check(bool check)
248 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
249 if(m_isChecked == check)
251 wxAutoNSAutoreleasePool pool;
252 if(GetKind() == wxITEM_RADIO)
254 // it doesn't make sense to uncheck a radio item - what would this do?
257 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
258 // First search backwards for other radio items
259 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
260 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
261 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
262 prevNode = prevNode->GetPrevious())
264 radioStart = prevNode;
266 // Now starting there set the state of every item until we're
267 // out of radio items to set.
268 for(wxMenuItemList::compatibility_iterator node = radioStart;
269 node && (node->GetData()->GetKind() == wxITEM_RADIO);
270 node = node->GetNext())
272 wxMenuItem *item = node->GetData();
273 bool checkItem = (item == this);
274 item->wxMenuItemBase::Check(checkItem);
275 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
278 else // normal check (non-radio) item
280 wxMenuItemBase::Check(check);
281 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
285 void wxMenuItem::SetText(const wxString& label)
287 wxMenuItemBase::SetText(label);
288 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
289 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
290 CocoaSetKeyEquivalent();
293 void wxMenuItem::SetCheckable(bool checkable)
295 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
296 wxMenuItemBase::SetCheckable(checkable);
297 // NOTE: Cocoa does not discern between unchecked and normal items
300 #endif // wxUSE_MENUS