1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenuItem implementation
4 // Author: David Elliott
8 // Copyright: 2002 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>
39 // ----------------------------------------------------------------------------
40 // functions prototypes
41 // ----------------------------------------------------------------------------
43 // ============================================================================
44 // @class wxNSMenuItemTarget
45 // ============================================================================
46 @interface wxNSMenuItemTarget : NSObject
50 - (void)wxMenuItemAction: (id)sender;
51 - (BOOL)validateMenuItem: (id)menuItem;
52 @end //interface wxNSMenuItemTarget
54 @implementation wxNSMenuItemTarget : NSObject
56 - (void)wxMenuItemAction: (id)sender
58 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
59 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
60 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
62 wxMenu *menu = item->GetMenu();
63 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
64 wxMenuBar *menubar = menu->GetMenuBar();
67 wxFrame *frame = menubar->GetFrame();
68 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
69 frame->ProcessCommand(item->GetId());
73 - (BOOL)validateMenuItem: (id)menuItem
75 // TODO: Do wxWindows validation here and avoid sending during idle time
76 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
77 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
78 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
79 return item->IsEnabled();
82 @end //implementation wxNSMenuItemTarget
84 // ============================================================================
85 // @class wxPoserNSMenuItem
86 // ============================================================================
87 @interface wxPoserNSMenuItem : NSMenuItem
91 @end // wxPoserNSMenuItem
93 WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
94 @implementation wxPoserNSMenuItem : NSMenuItem
96 @end // wxPoseRNSMenuItem
98 // ============================================================================
99 // wxMenuItemCocoa implementation
100 // ============================================================================
101 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
102 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
104 struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
112 const wxString& name,
113 const wxString& help,
117 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
121 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
123 return wxStripMenuCodes(text);
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
129 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
131 const wxString& strName,
132 const wxString& strHelp,
135 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
137 wxAutoNSAutoreleasePool pool;
138 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
139 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""];
140 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
141 [m_cocoaNSMenuItem setTarget:sm_cocoaTarget];
144 wxASSERT(pSubMenu->GetNSMenu());
145 [pSubMenu->GetNSMenu() setTitle:menuTitle];
146 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
151 wxMenuItem::~wxMenuItem()
153 sm_cocoaHash.erase(m_cocoaNSMenuItem);
154 [m_cocoaNSMenuItem release];
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
164 void wxMenuItem::Enable(bool bDoEnable)
166 wxMenuItemBase::Enable(bDoEnable);
169 void wxMenuItem::Check(bool bDoCheck)
171 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
172 wxMenuItemBase::Check(bDoCheck);
175 void wxMenuItem::SetText(const wxString& label)
177 wxMenuItemBase::SetText(label);
180 void wxMenuItem::SetCheckable(bool checkable)
182 wxMenuItemBase::SetCheckable(checkable);
185 #endif // wxUSE_MENUS