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 - (BOOL)validateMenuItem: (id)menuItem;
51 @end //interface wxNSMenuItemTarget
53 @implementation wxNSMenuItemTarget : NSObject
55 - (void)wxMenuItemAction: (id)sender
57 wxLogDebug("wxMenuItemAction");
58 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
59 wxCHECK_RET(item,"wxMenuItemAction received but no wxMenuItem exists!");
61 wxMenu *menu = item->GetMenu();
62 wxCHECK_RET(menu,"wxMenuItemAction received but wxMenuItem is not in a wxMenu");
63 wxMenuBar *menubar = menu->GetMenuBar();
66 wxFrame *frame = menubar->GetFrame();
67 wxCHECK_RET(frame, "wxMenuBar MUST be attached to a wxFrame!");
68 frame->Command(item->GetId());
72 - (BOOL)validateMenuItem: (id)menuItem
74 // TODO: Do wxWindows validation here and avoid sending during idle time
75 wxLogDebug("wxMenuItemAction");
76 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
77 wxCHECK_MSG(item,NO,"validateMenuItem received but no wxMenuItem exists!");
78 return item->IsEnabled();
81 @end //implementation wxNSMenuItemTarget
83 // ============================================================================
84 // @class wxPoserNSMenuItem
85 // ============================================================================
86 @interface wxPoserNSMenuItem : NSMenuItem
90 @end // wxPoserNSMenuItem
92 WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
93 @implementation wxPoserNSMenuItem : NSMenuItem
95 @end // wxPoseRNSMenuItem
97 // ============================================================================
98 // wxMenuItemCocoa implementation
99 // ============================================================================
100 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
101 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
103 struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
111 const wxString& name,
112 const wxString& help,
116 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
120 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
122 return wxStripMenuCodes(text);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
128 wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
130 const wxString& strName,
131 const wxString& strHelp,
134 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
136 wxAutoNSAutoreleasePool pool;
137 NSString *menuTitle = [[NSString alloc] initWithCString: wxStripMenuCodes(strName).c_str()];
138 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""];
139 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
140 [m_cocoaNSMenuItem setTarget:sm_cocoaTarget];
143 wxASSERT(pSubMenu->GetNSMenu());
144 [pSubMenu->GetNSMenu() setTitle:menuTitle];
145 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
150 wxMenuItem::~wxMenuItem()
152 sm_cocoaHash.erase(m_cocoaNSMenuItem);
153 [m_cocoaNSMenuItem release];
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
163 void wxMenuItem::Enable(bool bDoEnable)
165 wxMenuItemBase::Enable(bDoEnable);
168 void wxMenuItem::Check(bool bDoCheck)
170 wxCHECK_RET( IsCheckable(), "only checkable items may be checked" );
171 wxMenuItemBase::Check(bDoCheck);
174 void wxMenuItem::SetText(const wxString& label)
176 wxMenuItemBase::SetText(label);
179 void wxMenuItem::SetCheckable(bool checkable)
181 wxMenuItemBase::SetCheckable(checkable);
184 #endif // wxUSE_MENUS