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"
32 #import <AppKit/NSMenuItem.h>
33 #import <AppKit/NSMenu.h>
34 #import <Foundation/NSString.h>
38 // ----------------------------------------------------------------------------
39 // functions prototypes
40 // ----------------------------------------------------------------------------
42 // ============================================================================
43 // @class wxNSMenuItemTarget
44 // ============================================================================
45 @interface wxNSMenuItemTarget : NSObject
49 - (void)wxMenuItemAction: (id)sender;
50 @end //interface wxNSMenuItemTarget
52 @implementation wxNSMenuItemTarget : NSObject
54 - (void)wxMenuItemAction: (id)sender
56 wxLogDebug("wxMenuItemAction");
57 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
58 wxCHECK_RET(item,"wxMenuItemAction received but no wxMenuItem exists!");
60 wxMenu *menu = item->GetMenu();
61 wxCHECK_RET(menu,"wxMenuItemAction received but wxMenuItem is not in a wxMenu");
62 wxMenuBar *menubar = menu->GetMenuBar();
65 wxFrame *frame = menubar->GetFrame();
66 wxCHECK_RET(frame, "wxMenuBar MUST be attached to a wxFrame!");
67 frame->Command(item->GetId());
71 @end //implementation wxNSMenuItemTarget
73 // ============================================================================
74 // @class wxPoserNSMenuItem
75 // ============================================================================
76 @interface wxPoserNSMenuItem : NSMenuItem
80 @end // wxPoserNSMenuItem
82 WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
83 @implementation wxPoserNSMenuItem : NSMenuItem
85 @end // wxPoseRNSMenuItem
87 // ============================================================================
88 // wxMenuItemCocoa implementation
89 // ============================================================================
90 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
91 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
93 struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
101 const wxString& name,
102 const wxString& help,
106 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
110 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
112 return wxStripMenuCodes(text);
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
118 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
120 const wxString& strName,
121 const wxString& strHelp,
124 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
126 wxAutoNSAutoreleasePool pool;
127 NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(strName).c_str()];
128 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""];
129 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
130 [m_cocoaNSMenuItem setTarget:sm_cocoaTarget];
133 wxASSERT(pSubMenu->GetNSMenu());
134 [pSubMenu->GetNSMenu() setTitle:menuTitle];
135 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
140 wxMenuItem::~wxMenuItem()
142 sm_cocoaHash.erase(m_cocoaNSMenuItem);
143 [m_cocoaNSMenuItem release];
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
153 void wxMenuItem::Enable(bool bDoEnable)
155 wxMenuItemBase::Enable(bDoEnable);
158 void wxMenuItem::Check(bool bDoCheck)
160 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
161 wxMenuItemBase::Check(bDoCheck);
164 void wxMenuItem::SetText(const wxString& label)
166 wxMenuItemBase::SetText(label);
169 void wxMenuItem::SetCheckable(bool checkable)
171 wxMenuItemBase::SetCheckable(checkable);
174 #endif // wxUSE_MENUS