]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/menuitem.mm
Digital Mars compilation warnings and Unicode fixes (patch 884587)
[wxWidgets.git] / src / cocoa / menuitem.mm
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"
26 #include "wx/log.h"
27 #endif
28
29 #include "wx/cocoa/ObjcPose.h"
30 #include "wx/cocoa/autorelease.h"
31 #include "wx/cocoa/string.h"
32
33 #import <AppKit/NSMenuItem.h>
34 #import <AppKit/NSMenu.h>
35 #import <Foundation/NSString.h>
36
37 #if wxUSE_MENUS
38
39 // ----------------------------------------------------------------------------
40 // functions prototypes
41 // ----------------------------------------------------------------------------
42
43 // ============================================================================
44 // @class wxNSMenuItemTarget
45 // ============================================================================
46 @interface wxNSMenuItemTarget : NSObject
47 {
48 }
49
50 - (void)wxMenuItemAction: (id)sender;
51 - (BOOL)validateMenuItem: (id)menuItem;
52 @end //interface wxNSMenuItemTarget
53
54 @implementation wxNSMenuItemTarget : NSObject
55
56 - (void)wxMenuItemAction: (id)sender
57 {
58 wxLogDebug(wxT("wxMenuItemAction"));
59 wxMenuItem *item = wxMenuItem::GetFromCocoa(sender);
60 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
61
62 wxMenu *menu = item->GetMenu();
63 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu"));
64 wxMenuBar *menubar = menu->GetMenuBar();
65 if(menubar)
66 {
67 wxFrame *frame = menubar->GetFrame();
68 wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!"));
69 frame->ProcessCommand(item->GetId());
70 }
71 }
72
73 - (BOOL)validateMenuItem: (id)menuItem
74 {
75 // TODO: Do wxWindows validation here and avoid sending during idle time
76 wxLogDebug(wxT("wxMenuItemAction"));
77 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
78 wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!"));
79 return item->IsEnabled();
80 }
81
82 @end //implementation wxNSMenuItemTarget
83
84 // ============================================================================
85 // @class wxPoserNSMenuItem
86 // ============================================================================
87 @interface wxPoserNSMenuItem : NSMenuItem
88 {
89 }
90
91 @end // wxPoserNSMenuItem
92
93 WX_IMPLEMENT_POSER(wxPoserNSMenuItem);
94 @implementation wxPoserNSMenuItem : NSMenuItem
95
96 @end // wxPoseRNSMenuItem
97
98 // ============================================================================
99 // wxMenuItemCocoa implementation
100 // ============================================================================
101 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
102 wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash;
103
104 struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init];
105
106 // ----------------------------------------------------------------------------
107 // wxMenuItemBase
108 // ----------------------------------------------------------------------------
109
110 wxMenuItem *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 */
121 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
122 {
123 return wxStripMenuCodes(text);
124 }
125
126 // ----------------------------------------------------------------------------
127 // ctor & dtor
128 // ----------------------------------------------------------------------------
129 wxMenuItemCocoa::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 {
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];
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
151 wxMenuItem::~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
164 void wxMenuItem::Enable(bool bDoEnable)
165 {
166 wxMenuItemBase::Enable(bDoEnable);
167 }
168
169 void wxMenuItem::Check(bool bDoCheck)
170 {
171 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
172 wxMenuItemBase::Check(bDoCheck);
173 }
174
175 void wxMenuItem::SetText(const wxString& label)
176 {
177 wxMenuItemBase::SetText(label);
178 }
179
180 void wxMenuItem::SetCheckable(bool checkable)
181 {
182 wxMenuItemBase::SetCheckable(checkable);
183 }
184
185 #endif // wxUSE_MENUS