]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/menuitem.cpp
Add class factories for filter streams. Also filters now follow the convention
[wxWidgets.git] / src / mac / classic / menuitem.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/menuitem.cpp
3 // Purpose: wxMenuItem implementation
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
15
16 #include "wx/wxprec.h"
17
18 #include "wx/menuitem.h"
19 #include "wx/stockitem.h"
20
21 #ifndef WX_PRECOMP
22 #include "wx/app.h"
23 #include "wx/menu.h"
24 #endif
25
26 #include "wx/mac/uma.h"
27 // ============================================================================
28 // implementation
29 // ============================================================================
30
31 // ----------------------------------------------------------------------------
32 // dynamic classes implementation
33 // ----------------------------------------------------------------------------
34
35 IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
36
37 // ----------------------------------------------------------------------------
38 // wxMenuItem
39 // ----------------------------------------------------------------------------
40
41 //
42 // ctor & dtor
43 // -----------
44
45 wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
46 int id,
47 const wxString& text,
48 const wxString& strHelp,
49 wxItemKind kind,
50 wxMenu *pSubMenu)
51 : wxMenuItemBase(pParentMenu, id, text, strHelp, kind, pSubMenu)
52 {
53 // TO DISCUSS on dev : whether we can veto id 0
54 // wxASSERT_MSG( id != 0 || pSubMenu != NULL , wxT("A MenuItem ID of Zero does not work under Mac") ) ;
55
56 // In other languages there is no difference in naming the Exit/Quit menu item between MacOS and Windows guidelines
57 // therefore these item must not be translated
58 if ( wxStripMenuCodes(m_text).Upper() == wxT("EXIT") )
59 {
60 m_text =wxT("Quit\tCtrl+Q") ;
61 }
62
63 m_radioGroup.start = -1;
64 m_isRadioGroupStart = false;
65 }
66
67 wxMenuItem::~wxMenuItem()
68 {
69 }
70
71 // change item state
72 // -----------------
73
74 void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
75 {
76 m_bitmap = bitmap;
77 UpdateItemBitmap() ;
78 }
79
80 void wxMenuItem::UpdateItemBitmap()
81 {
82 if ( !m_parentMenu )
83 return ;
84
85 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
86 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
87 if( mhandle == NULL || index == 0)
88 return ;
89
90 if ( m_bitmap.Ok() )
91 {
92 ControlButtonContentInfo info ;
93 wxMacCreateBitmapButton( &info , m_bitmap , kControlContentCIconHandle ) ;
94 if ( info.contentType != kControlNoContent )
95 {
96 if ( info.contentType == kControlContentCIconHandle )
97 SetMenuItemIconHandle( mhandle , index ,
98 kMenuColorIconType , (Handle) info.u.cIconHandle ) ;
99 }
100
101 }
102 }
103
104 void wxMenuItem::UpdateItemStatus()
105 {
106 if ( !m_parentMenu )
107 return ;
108
109 #if TARGET_CARBON
110 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macPreferencesMenuItemId)
111 {
112 if ( !IsEnabled() )
113 DisableMenuCommand( NULL , kHICommandPreferences ) ;
114 else
115 EnableMenuCommand( NULL , kHICommandPreferences ) ;
116 }
117 if ( UMAGetSystemVersion() >= 0x1000 && GetId() == wxApp::s_macExitMenuItemId)
118 {
119 if ( !IsEnabled() )
120 DisableMenuCommand( NULL , kHICommandQuit ) ;
121 else
122 EnableMenuCommand( NULL , kHICommandQuit ) ;
123 }
124 #endif
125 {
126 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
127 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
128 if( mhandle == NULL || index == 0)
129 return ;
130
131 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
132 if ( IsCheckable() && IsChecked() )
133 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
134 else
135 ::SetItemMark( mhandle , index , 0 ) ; // no mark
136
137 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
138 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( m_text ) ;
139 UMASetMenuItemShortcut( mhandle , index , entry ) ;
140 delete entry ;
141 }
142 }
143
144 void wxMenuItem::UpdateItemText()
145 {
146 if ( !m_parentMenu )
147 return ;
148
149 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
150 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
151 if( mhandle == NULL || index == 0)
152 return ;
153
154 wxString text = m_text;
155 if (text.IsEmpty() && !IsSeparator())
156 {
157 wxASSERT_MSG(wxIsStockID(GetId()), wxT("A non-stock menu item with an empty label?"));
158 text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR|wxSTOCK_WITH_MNEMONIC);
159 }
160
161 UMASetMenuItemText( mhandle , index , text , wxFont::GetDefaultEncoding() ) ;
162 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( text ) ;
163 UMASetMenuItemShortcut( mhandle , index , entry ) ;
164 delete entry ;
165 }
166
167
168 void wxMenuItem::Enable(bool bDoEnable)
169 {
170 if ( m_isEnabled != bDoEnable )
171 {
172 wxMenuItemBase::Enable( bDoEnable ) ;
173 UpdateItemStatus() ;
174 }
175 }
176 void wxMenuItem::UncheckRadio()
177 {
178 if ( m_isChecked )
179 {
180 wxMenuItemBase::Check( false ) ;
181 UpdateItemStatus() ;
182 }
183 }
184
185 void wxMenuItem::Check(bool bDoCheck)
186 {
187 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
188
189 if ( m_isChecked != bDoCheck )
190 {
191 if ( GetKind() == wxITEM_RADIO )
192 {
193 if ( bDoCheck )
194 {
195 wxMenuItemBase::Check( bDoCheck ) ;
196 UpdateItemStatus() ;
197
198 // get the index of this item in the menu
199 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
200 int pos = items.IndexOf(this);
201 wxCHECK_RET( pos != wxNOT_FOUND,
202 _T("menuitem not found in the menu items list?") );
203
204 // get the radio group range
205 int start,
206 end;
207
208 if ( m_isRadioGroupStart )
209 {
210 // we already have all information we need
211 start = pos;
212 end = m_radioGroup.end;
213 }
214 else // next radio group item
215 {
216 // get the radio group end from the start item
217 start = m_radioGroup.start;
218 end = items.Item(start)->GetData()->m_radioGroup.end;
219 }
220
221 // also uncheck all the other items in this radio group
222 wxMenuItemList::Node *node = items.Item(start);
223 for ( int n = start; n <= end && node; n++ )
224 {
225 if ( n != pos )
226 {
227 ((wxMenuItem*)node->GetData())->UncheckRadio();
228 }
229 node = node->GetNext();
230 }
231 }
232 }
233 else
234 {
235 wxMenuItemBase::Check( bDoCheck ) ;
236 UpdateItemStatus() ;
237 }
238 }
239 }
240
241 void wxMenuItem::SetText(const wxString& text)
242 {
243 // don't do anything if label didn't change
244 if ( m_text == text )
245 return;
246
247 wxMenuItemBase::SetText(text);
248
249 UpdateItemText() ;
250 }
251
252 // radio group stuff
253 // -----------------
254
255 void wxMenuItem::SetAsRadioGroupStart()
256 {
257 m_isRadioGroupStart = true;
258 }
259
260 void wxMenuItem::SetRadioGroupStart(int start)
261 {
262 wxASSERT_MSG( !m_isRadioGroupStart,
263 _T("should only be called for the next radio items") );
264
265 m_radioGroup.start = start;
266 }
267
268 void wxMenuItem::SetRadioGroupEnd(int end)
269 {
270 wxASSERT_MSG( m_isRadioGroupStart,
271 _T("should only be called for the first radio item") );
272
273 m_radioGroup.end = end;
274 }
275
276 // ----------------------------------------------------------------------------
277 // wxMenuItemBase
278 // ----------------------------------------------------------------------------
279
280 /* static */
281 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
282 {
283 return wxStripMenuCodes(text);
284 }
285
286 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
287 int id,
288 const wxString& name,
289 const wxString& help,
290 wxItemKind kind,
291 wxMenu *subMenu)
292 {
293 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
294 }