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