1 ///////////////////////////////////////////////////////////////////////////////
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"
23 #include "wx/menuitem.h"
29 #include "wx/cocoa/ObjcPose.h"
30 #include "wx/cocoa/autorelease.h"
31 #include "wx/cocoa/string.h"
33 #import <AppKit/NSMenuItem.h>
34 #import <AppKit/NSMenu.h>
35 #import <Foundation/NSString.h>
36 #import <AppKit/NSCell.h> // NSOnState, NSOffState
40 // ----------------------------------------------------------------------------
41 // functions prototypes
42 // ----------------------------------------------------------------------------
44 // ============================================================================
45 // @class wxNSMenuItemTarget
46 // ============================================================================
47 @interface wxNSMenuItemTarget : NSObject
51 - (void)wxMenuItemAction: (id)sender;
52 - (BOOL)validateMenuItem: (id)menuItem;
53 @end //interface wxNSMenuItemTarget
55 @implementation wxNSMenuItemTarget : NSObject
57 - (void)wxMenuItemAction: (id)sender
59 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
60 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
61 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
62 item->CocoaItemSelected();
65 - (BOOL)validateMenuItem: (id)menuItem
67 // TODO: Do wxWidgets validation here and avoid sending during idle time
68 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
69 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
70 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
71 return item->Cocoa_validateMenuItem();
74 @end //implementation wxNSMenuItemTarget
76 // ============================================================================
77 // wxMenuItemCocoa implementation
78 // ============================================================================
79 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
80 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
82 wxObjcAutoRefFromAlloc<struct objc_object *> wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
95 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
99 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
101 return wxStripMenuCodes(text);
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
107 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
109 const wxString& strName,
110 const wxString& strHelp,
113 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
115 wxAutoNSAutoreleasePool pool;
116 if(m_kind == wxITEM_SEPARATOR)
117 m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain];
120 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
125 action = @selector(wxMenuItemAction:);
126 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:action keyEquivalent:@""];
127 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
130 wxASSERT(pSubMenu->GetNSMenu());
131 [pSubMenu->GetNSMenu() setTitle:menuTitle];
132 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
135 [m_cocoaNSMenuItem setTarget: sm_cocoaTarget];
140 wxMenuItem::~wxMenuItem()
142 sm_cocoaHash.erase(m_cocoaNSMenuItem);
143 [m_cocoaNSMenuItem release];
146 void wxMenuItem::CocoaItemSelected()
148 wxMenu *menu = GetMenu();
149 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
150 wxMenuBar *menubar = menu->GetMenuBar();
153 wxFrame *frame = menubar->GetFrame();
154 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
155 frame->ProcessCommand(GetId());
161 GetMenu()->SendEvent(GetId(), IsCheckable()?IsChecked():-1);
165 bool wxMenuItem::Cocoa_validateMenuItem()
167 // TODO: do more sanity checking
168 // TODO: Do wxWindows validation here and avoid sending during idle time
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked,
177 const wxBitmap& bmpUnchecked)
179 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps."));
180 wxAutoNSAutoreleasePool pool;
181 m_bmpChecked = bmpChecked;
182 m_bmpUnchecked = bmpUnchecked;
185 [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)];
186 [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)];
190 wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap"));
191 [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)];
198 void wxMenuItem::Enable(bool bDoEnable)
200 wxMenuItemBase::Enable(bDoEnable);
201 // NOTE: Nothing to do, we respond to validateMenuItem instead
204 void wxMenuItem::Check(bool check)
206 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
207 if(m_isChecked == check)
209 wxAutoNSAutoreleasePool pool;
210 if(GetKind() == wxITEM_RADIO)
212 // it doesn't make sense to uncheck a radio item - what would this do?
215 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
216 // First search backwards for other radio items
217 wxMenuItemList::compatibility_iterator radioStart = items.Find(this);
218 for(wxMenuItemList::compatibility_iterator prevNode = radioStart;
219 prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO);
220 prevNode = prevNode->GetPrevious())
222 radioStart = prevNode;
224 // Now starting there set the state of every item until we're
225 // out of radio items to set.
226 for(wxMenuItemList::compatibility_iterator node = radioStart;
227 node && (node->GetData()->GetKind() == wxITEM_RADIO);
228 node = node->GetNext())
230 wxMenuItem *item = node->GetData();
231 bool checkItem = (item == this);
232 item->wxMenuItemBase::Check(checkItem);
233 [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState];
236 else // normal check (non-radio) item
238 wxMenuItemBase::Check(check);
239 [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState];
243 void wxMenuItem::SetText(const wxString& label)
245 wxMenuItemBase::SetText(label);
246 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles."));
247 [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))];
250 void wxMenuItem::SetCheckable(bool checkable)
252 wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items."));
253 wxMenuItemBase::SetCheckable(checkable);
254 // NOTE: Cocoa does not discern between unchecked and normal items
257 #endif // wxUSE_MENUS