]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/menuitem.mm
load wsock32.dll dynamically, so that wxCore has no dependency on it
[wxWidgets.git] / src / cocoa / menuitem.mm
CommitLineData
fb896a32
DE
1///////////////////////////////////////////////////////////////////////////////
2// Name: menuitem.cpp
3// Purpose: wxMenuItem implementation
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/15
7// RCS-ID: $Id:
8// Copyright: 2002 David Elliott
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21#ifndef WX_PRECOMP
22 #include "wx/menu.h"
23 #include "wx/menuitem.h"
24 #include "wx/utils.h"
25 #include "wx/frame.h"
2fc2d511 26 #include "wx/log.h"
fb896a32
DE
27#endif
28
29#include "wx/cocoa/ObjcPose.h"
7fc77f30 30#include "wx/cocoa/autorelease.h"
b0c0a393 31#include "wx/cocoa/string.h"
fb896a32
DE
32
33#import <AppKit/NSMenuItem.h>
34#import <AppKit/NSMenu.h>
35#import <Foundation/NSString.h>
36
37#if wxUSE_MENUS
38
fb896a32
DE
39// ----------------------------------------------------------------------------
40// functions prototypes
41// ----------------------------------------------------------------------------
42
2fc2d511
DE
43// ============================================================================
44// @class wxNSMenuItemTarget
45// ============================================================================
46@interface wxNSMenuItemTarget : NSObject
47{
48}
49
50- (void)wxMenuItemAction: (id)sender;
42036ca8 51- (BOOL)validateMenuItem: (id)menuItem;
2fc2d511
DE
52@end //interface wxNSMenuItemTarget
53
54@implementation wxNSMenuItemTarget : NSObject
55
56- (void)wxMenuItemAction: (id)sender
57{
48580976 58 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
2fc2d511 59 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
2b030203 60 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
2fc2d511
DE
61
62 wxMenu *menu = item->GetMenu();
2b030203 63 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
2fc2d511
DE
64 wxMenuBar *menubar = menu->GetMenuBar();
65 if(menubar)
66 {
67 wxFrame *frame = menubar->GetFrame();
2b030203 68 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
7424a637 69 frame->ProcessCommand(item->GetId());
2fc2d511
DE
70 }
71}
72
42036ca8
DE
73- (BOOL)validateMenuItem: (id)menuItem
74{
75 // TODO: Do wxWindows validation here and avoid sending during idle time
48580976 76 wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction"));
42036ca8 77 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
2b030203 78 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
42036ca8
DE
79 return item->IsEnabled();
80}
81
2fc2d511
DE
82@end //implementation wxNSMenuItemTarget
83
fb896a32
DE
84// ============================================================================
85// @class wxPoserNSMenuItem
86// ============================================================================
87@interface wxPoserNSMenuItem : NSMenuItem
88{
89}
90
91@end // wxPoserNSMenuItem
92
93WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
94@implementation wxPoserNSMenuItem : NSMenuItem
95
96@end // wxPoseRNSMenuItem
97
98// ============================================================================
99// wxMenuItemCocoa implementation
100// ============================================================================
101IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2fc2d511
DE
102wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
103
104struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
fb896a32
DE
105
106// ----------------------------------------------------------------------------
107// wxMenuItemBase
108// ----------------------------------------------------------------------------
109
110wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
111 int itemid,
112 const wxString& name,
113 const wxString& help,
114 wxItemKind kind,
115 wxMenu *subMenu)
116{
117 return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu);
118}
119
120/* static */
121wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
122{
123 return wxStripMenuCodes(text);
124}
125
126// ----------------------------------------------------------------------------
127// ctor & dtor
128// ----------------------------------------------------------------------------
129wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu,
130 int itemid,
131 const wxString& strName,
132 const wxString& strHelp,
133 wxItemKind kind,
134 wxMenu *pSubMenu)
135 : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu)
136{
7fc77f30 137 wxAutoNSAutoreleasePool pool;
b0c0a393 138 NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName));
fb896a32
DE
139 m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""];
140 sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this));
2fc2d511 141 [m_cocoaNSMenuItem setTarget:sm_cocoaTarget];
fb896a32
DE
142 if(pSubMenu)
143 {
144 wxASSERT(pSubMenu->GetNSMenu());
145 [pSubMenu->GetNSMenu() setTitle:menuTitle];
146 [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()];
147 }
148 [menuTitle release];
149}
150
151wxMenuItem::~wxMenuItem()
152{
153 sm_cocoaHash.erase(m_cocoaNSMenuItem);
154 [m_cocoaNSMenuItem release];
155}
156
157// ----------------------------------------------------------------------------
158// misc
159// ----------------------------------------------------------------------------
160
161// change item state
162// -----------------
163
164void wxMenuItem::Enable(bool bDoEnable)
165{
166 wxMenuItemBase::Enable(bDoEnable);
167}
168
169void wxMenuItem::Check(bool bDoCheck)
170{
2b030203 171 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
fb896a32
DE
172 wxMenuItemBase::Check(bDoCheck);
173}
174
175void wxMenuItem::SetText(const wxString& label)
176{
177 wxMenuItemBase::SetText(label);
178}
179
180void wxMenuItem::SetCheckable(bool checkable)
181{
182 wxMenuItemBase::SetCheckable(checkable);
183}
184
185#endif // wxUSE_MENUS