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