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