]>
Commit | Line | Data |
---|---|---|
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 | |
065e208e | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
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> | |
1ee4156a | 36 | #import <AppKit/NSCell.h> // NSOnState, NSOffState |
fb896a32 DE |
37 | |
38 | #if wxUSE_MENUS | |
39 | ||
fb896a32 DE |
40 | // ---------------------------------------------------------------------------- |
41 | // functions prototypes | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
2fc2d511 DE |
44 | // ============================================================================ |
45 | // @class wxNSMenuItemTarget | |
46 | // ============================================================================ | |
47 | @interface wxNSMenuItemTarget : NSObject | |
48 | { | |
49 | } | |
50 | ||
51 | - (void)wxMenuItemAction: (id)sender; | |
42036ca8 | 52 | - (BOOL)validateMenuItem: (id)menuItem; |
2fc2d511 DE |
53 | @end //interface wxNSMenuItemTarget |
54 | ||
55 | @implementation wxNSMenuItemTarget : NSObject | |
56 | ||
57 | - (void)wxMenuItemAction: (id)sender | |
58 | { | |
48580976 | 59 | wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction")); |
2fc2d511 | 60 | wxMenuItem *item = wxMenuItem::GetFromCocoa(sender); |
2b030203 | 61 | wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!")); |
2fc2d511 DE |
62 | |
63 | wxMenu *menu = item->GetMenu(); | |
2b030203 | 64 | wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu")); |
2fc2d511 DE |
65 | wxMenuBar *menubar = menu->GetMenuBar(); |
66 | if(menubar) | |
67 | { | |
68 | wxFrame *frame = menubar->GetFrame(); | |
2b030203 | 69 | wxCHECK_RET(frame, wxT("wxMenuBar MUST be attached to a wxFrame!")); |
7424a637 | 70 | frame->ProcessCommand(item->GetId()); |
2fc2d511 DE |
71 | } |
72 | } | |
73 | ||
42036ca8 DE |
74 | - (BOOL)validateMenuItem: (id)menuItem |
75 | { | |
065e208e | 76 | // TODO: Do wxWidgets validation here and avoid sending during idle time |
48580976 | 77 | wxLogTrace(wxTRACE_COCOA,wxT("wxMenuItemAction")); |
42036ca8 | 78 | wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem); |
2b030203 | 79 | wxCHECK_MSG(item,NO,wxT("validateMenuItem received but no wxMenuItem exists!")); |
42036ca8 DE |
80 | return item->IsEnabled(); |
81 | } | |
82 | ||
2fc2d511 DE |
83 | @end //implementation wxNSMenuItemTarget |
84 | ||
fb896a32 DE |
85 | // ============================================================================ |
86 | // @class wxPoserNSMenuItem | |
87 | // ============================================================================ | |
88 | @interface wxPoserNSMenuItem : NSMenuItem | |
89 | { | |
90 | } | |
91 | ||
92 | @end // wxPoserNSMenuItem | |
93 | ||
94 | WX_IMPLEMENT_POSER(wxPoserNSMenuItem); | |
95 | @implementation wxPoserNSMenuItem : NSMenuItem | |
96 | ||
97 | @end // wxPoseRNSMenuItem | |
98 | ||
99 | // ============================================================================ | |
100 | // wxMenuItemCocoa implementation | |
101 | // ============================================================================ | |
102 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
2fc2d511 DE |
103 | wxMenuItemCocoaHash wxMenuItemCocoa::sm_cocoaHash; |
104 | ||
105 | struct objc_object *wxMenuItemCocoa::sm_cocoaTarget = [[wxNSMenuItemTarget alloc] init]; | |
fb896a32 DE |
106 | |
107 | // ---------------------------------------------------------------------------- | |
108 | // wxMenuItemBase | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
111 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
112 | int itemid, | |
113 | const wxString& name, | |
114 | const wxString& help, | |
115 | wxItemKind kind, | |
116 | wxMenu *subMenu) | |
117 | { | |
118 | return new wxMenuItem(parentMenu, itemid, name, help, kind, subMenu); | |
119 | } | |
120 | ||
121 | /* static */ | |
122 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
123 | { | |
124 | return wxStripMenuCodes(text); | |
125 | } | |
126 | ||
127 | // ---------------------------------------------------------------------------- | |
128 | // ctor & dtor | |
129 | // ---------------------------------------------------------------------------- | |
130 | wxMenuItemCocoa::wxMenuItemCocoa(wxMenu *pParentMenu, | |
131 | int itemid, | |
132 | const wxString& strName, | |
133 | const wxString& strHelp, | |
134 | wxItemKind kind, | |
135 | wxMenu *pSubMenu) | |
136 | : wxMenuItemBase(pParentMenu, itemid, strName, strHelp, kind, pSubMenu) | |
137 | { | |
7fc77f30 | 138 | wxAutoNSAutoreleasePool pool; |
67352554 DE |
139 | if(m_kind == wxITEM_SEPARATOR) |
140 | m_cocoaNSMenuItem = [[NSMenuItem separatorItem] retain]; | |
141 | else | |
fb896a32 | 142 | { |
67352554 DE |
143 | NSString *menuTitle = wxInitNSStringWithWxString([NSString alloc],wxStripMenuCodes(strName)); |
144 | m_cocoaNSMenuItem = [[NSMenuItem alloc] initWithTitle:menuTitle action:@selector(wxMenuItemAction:) keyEquivalent:@""]; | |
145 | sm_cocoaHash.insert(wxMenuItemCocoaHash::value_type(m_cocoaNSMenuItem,this)); | |
146 | [m_cocoaNSMenuItem setTarget:sm_cocoaTarget]; | |
147 | if(pSubMenu) | |
148 | { | |
149 | wxASSERT(pSubMenu->GetNSMenu()); | |
150 | [pSubMenu->GetNSMenu() setTitle:menuTitle]; | |
151 | [m_cocoaNSMenuItem setSubmenu:pSubMenu->GetNSMenu()]; | |
152 | } | |
153 | [menuTitle release]; | |
fb896a32 | 154 | } |
fb896a32 DE |
155 | } |
156 | ||
157 | wxMenuItem::~wxMenuItem() | |
158 | { | |
159 | sm_cocoaHash.erase(m_cocoaNSMenuItem); | |
160 | [m_cocoaNSMenuItem release]; | |
161 | } | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // misc | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
982fc427 DE |
167 | void wxMenuItem::SetBitmaps(const wxBitmap& bmpChecked, |
168 | const wxBitmap& bmpUnchecked) | |
169 | { | |
170 | wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have bitmaps.")); | |
171 | wxAutoNSAutoreleasePool pool; | |
172 | m_bmpChecked = bmpChecked; | |
173 | m_bmpUnchecked = bmpUnchecked; | |
174 | if(IsCheckable()) | |
175 | { | |
176 | [m_cocoaNSMenuItem setOnStateImage: bmpChecked.GetNSImage(true)]; | |
177 | [m_cocoaNSMenuItem setOffStateImage: bmpUnchecked.GetNSImage(true)]; | |
178 | } | |
179 | else | |
180 | { | |
181 | wxASSERT_MSG(!bmpUnchecked.Ok(),wxT("Normal menu items should only have one bitmap")); | |
182 | [m_cocoaNSMenuItem setImage: bmpChecked.GetNSImage(true)]; | |
183 | } | |
184 | } | |
185 | ||
fb896a32 DE |
186 | // change item state |
187 | // ----------------- | |
188 | ||
189 | void wxMenuItem::Enable(bool bDoEnable) | |
190 | { | |
191 | wxMenuItemBase::Enable(bDoEnable); | |
e03a77c6 | 192 | // NOTE: Nothing to do, we respond to validateMenuItem instead |
fb896a32 DE |
193 | } |
194 | ||
1ee4156a | 195 | void wxMenuItem::Check(bool check) |
fb896a32 | 196 | { |
2b030203 | 197 | wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") ); |
1ee4156a DE |
198 | if(m_isChecked == check) |
199 | return; | |
200 | wxAutoNSAutoreleasePool pool; | |
201 | if(GetKind() == wxITEM_RADIO) | |
202 | { | |
203 | // it doesn't make sense to uncheck a radio item - what would this do? | |
204 | if(!check) | |
205 | return; | |
206 | const wxMenuItemList& items = m_parentMenu->GetMenuItems(); | |
207 | // First search backwards for other radio items | |
208 | wxMenuItemList::compatibility_iterator radioStart = items.Find(this); | |
209 | for(wxMenuItemList::compatibility_iterator prevNode = radioStart; | |
210 | prevNode && (prevNode->GetData()->GetKind() == wxITEM_RADIO); | |
211 | prevNode = prevNode->GetPrevious()) | |
212 | { | |
213 | radioStart = prevNode; | |
214 | } | |
215 | // Now starting there set the state of every item until we're | |
216 | // out of radio items to set. | |
217 | for(wxMenuItemList::compatibility_iterator node = radioStart; | |
218 | node && (node->GetData()->GetKind() == wxITEM_RADIO); | |
219 | node = node->GetNext()) | |
220 | { | |
221 | wxMenuItem *item = node->GetData(); | |
222 | bool checkItem = (item == this); | |
223 | item->wxMenuItemBase::Check(checkItem); | |
224 | [item->m_cocoaNSMenuItem setState: checkItem?NSOnState:NSOffState]; | |
225 | } | |
226 | } | |
227 | else // normal check (non-radio) item | |
228 | { | |
229 | wxMenuItemBase::Check(check); | |
230 | [m_cocoaNSMenuItem setState: check?NSOnState:NSOffState]; | |
231 | } | |
fb896a32 DE |
232 | } |
233 | ||
234 | void wxMenuItem::SetText(const wxString& label) | |
235 | { | |
236 | wxMenuItemBase::SetText(label); | |
e03a77c6 DE |
237 | wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items do not have titles.")); |
238 | [m_cocoaNSMenuItem setTitle: wxNSStringWithWxString(wxStripMenuCodes(label))]; | |
fb896a32 DE |
239 | } |
240 | ||
241 | void wxMenuItem::SetCheckable(bool checkable) | |
242 | { | |
1ee4156a | 243 | wxCHECK_RET(m_kind != wxITEM_SEPARATOR, wxT("Separator items cannot be turned into normal menu items.")); |
fb896a32 | 244 | wxMenuItemBase::SetCheckable(checkable); |
1ee4156a | 245 | // NOTE: Cocoa does not discern between unchecked and normal items |
fb896a32 DE |
246 | } |
247 | ||
248 | #endif // wxUSE_MENUS |