]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/menuitem.cpp
[ 1563875 ] wxTreeCtrl::FindItem asserts when item not found
[wxWidgets.git] / src / mac / classic / menuitem.cpp
CommitLineData
2646f485 1///////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/mac/classic/menuitem.cpp
2646f485
SC
3// Purpose: wxMenuItem implementation
4// Author: Stefan Csomor
670f9935 5// Modified by:
2646f485
SC
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
2646f485
SC
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
670f9935
WS
16#include "wx/wxprec.h"
17
2646f485 18#include "wx/menuitem.h"
ee0a94cf 19#include "wx/stockitem.h"
2646f485 20
670f9935
WS
21#ifndef WX_PRECOMP
22 #include "wx/app.h"
3b3dc801 23 #include "wx/menu.h"
670f9935
WS
24#endif
25
2646f485
SC
26#include "wx/mac/uma.h"
27// ============================================================================
28// implementation
29// ============================================================================
30
31// ----------------------------------------------------------------------------
32// dynamic classes implementation
33// ----------------------------------------------------------------------------
34
62f864c3 35IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject)
2646f485
SC
36
37// ----------------------------------------------------------------------------
38// wxMenuItem
39// ----------------------------------------------------------------------------
40
41//
42// ctor & dtor
43// -----------
44
45wxMenuItem::wxMenuItem(wxMenu *pParentMenu,
46 int id,
47 const wxString& text,
48 const wxString& strHelp,
49 wxItemKind kind,
670f9935 50 wxMenu *pSubMenu)
2646f485
SC
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") ) ;
670f9935 55
2646f485
SC
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;
670f9935 64 m_isRadioGroupStart = false;
2646f485
SC
65}
66
670f9935 67wxMenuItem::~wxMenuItem()
2646f485
SC
68{
69}
70
71// change item state
72// -----------------
73
670f9935
WS
74void wxMenuItem::SetBitmap(const wxBitmap& bitmap)
75{
76 m_bitmap = bitmap;
2646f485
SC
77 UpdateItemBitmap() ;
78}
79
670f9935 80void wxMenuItem::UpdateItemBitmap()
2646f485
SC
81{
82 if ( !m_parentMenu )
83 return ;
670f9935 84
2646f485
SC
85 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
86 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
87 if( mhandle == NULL || index == 0)
88 return ;
670f9935 89
2646f485
SC
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 )
670f9935 97 SetMenuItemIconHandle( mhandle , index ,
2646f485
SC
98 kMenuColorIconType , (Handle) info.u.cIconHandle ) ;
99 }
670f9935 100
2646f485
SC
101 }
102}
103
670f9935 104void wxMenuItem::UpdateItemStatus()
2646f485
SC
105{
106 if ( !m_parentMenu )
107 return ;
670f9935 108
2646f485
SC
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
670f9935 125 {
2646f485
SC
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
670f9935 137 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
90527a50 138 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( m_text ) ;
2646f485
SC
139 UMASetMenuItemShortcut( mhandle , index , entry ) ;
140 delete entry ;
141 }
142}
143
670f9935 144void wxMenuItem::UpdateItemText()
2646f485
SC
145{
146 if ( !m_parentMenu )
147 return ;
670f9935 148
2646f485
SC
149 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
150 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
151 if( mhandle == NULL || index == 0)
152 return ;
153
ee0a94cf
RR
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() ) ;
90527a50 162 wxAcceleratorEntry *entry = wxAcceleratorEntry::Create( text ) ;
2646f485
SC
163 UMASetMenuItemShortcut( mhandle , index , entry ) ;
164 delete entry ;
165}
166
167
168void wxMenuItem::Enable(bool bDoEnable)
169{
670f9935 170 if ( m_isEnabled != bDoEnable )
2646f485
SC
171 {
172 wxMenuItemBase::Enable( bDoEnable ) ;
173 UpdateItemStatus() ;
174 }
175}
176void wxMenuItem::UncheckRadio()
177{
670f9935 178 if ( m_isChecked )
2646f485
SC
179 {
180 wxMenuItemBase::Check( false ) ;
181 UpdateItemStatus() ;
182 }
183}
184
185void wxMenuItem::Check(bool bDoCheck)
186{
187 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
188
670f9935 189 if ( m_isChecked != bDoCheck )
2646f485
SC
190 {
191 if ( GetKind() == wxITEM_RADIO )
192 {
193 if ( bDoCheck )
194 {
195 wxMenuItemBase::Check( bDoCheck ) ;
196 UpdateItemStatus() ;
670f9935 197
2646f485
SC
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
241void 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);
670f9935 248
2646f485
SC
249 UpdateItemText() ;
250}
251
252// radio group stuff
253// -----------------
254
255void wxMenuItem::SetAsRadioGroupStart()
256{
670f9935 257 m_isRadioGroupStart = true;
2646f485
SC
258}
259
260void 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
268void 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 */
281wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
282{
283 return wxStripMenuCodes(text);
284}
285
286wxMenuItem *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}