]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/menuitem.cpp
pango_font_description_get_family() can return NULL
[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"
22#endif
23
24#include "wx/menu.h"
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() ) ;
2646f485
SC
138 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
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
670f9935
WS
154 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
155 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
2646f485
SC
156 UMASetMenuItemShortcut( mhandle , index , entry ) ;
157 delete entry ;
158}
159
160
161void wxMenuItem::Enable(bool bDoEnable)
162{
670f9935 163 if ( m_isEnabled != bDoEnable )
2646f485
SC
164 {
165 wxMenuItemBase::Enable( bDoEnable ) ;
166 UpdateItemStatus() ;
167 }
168}
169void wxMenuItem::UncheckRadio()
170{
670f9935 171 if ( m_isChecked )
2646f485
SC
172 {
173 wxMenuItemBase::Check( false ) ;
174 UpdateItemStatus() ;
175 }
176}
177
178void wxMenuItem::Check(bool bDoCheck)
179{
180 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
181
670f9935 182 if ( m_isChecked != bDoCheck )
2646f485
SC
183 {
184 if ( GetKind() == wxITEM_RADIO )
185 {
186 if ( bDoCheck )
187 {
188 wxMenuItemBase::Check( bDoCheck ) ;
189 UpdateItemStatus() ;
670f9935 190
2646f485
SC
191 // get the index of this item in the menu
192 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
193 int pos = items.IndexOf(this);
194 wxCHECK_RET( pos != wxNOT_FOUND,
195 _T("menuitem not found in the menu items list?") );
196
197 // get the radio group range
198 int start,
199 end;
200
201 if ( m_isRadioGroupStart )
202 {
203 // we already have all information we need
204 start = pos;
205 end = m_radioGroup.end;
206 }
207 else // next radio group item
208 {
209 // get the radio group end from the start item
210 start = m_radioGroup.start;
211 end = items.Item(start)->GetData()->m_radioGroup.end;
212 }
213
214 // also uncheck all the other items in this radio group
215 wxMenuItemList::Node *node = items.Item(start);
216 for ( int n = start; n <= end && node; n++ )
217 {
218 if ( n != pos )
219 {
220 ((wxMenuItem*)node->GetData())->UncheckRadio();
221 }
222 node = node->GetNext();
223 }
224 }
225 }
226 else
227 {
228 wxMenuItemBase::Check( bDoCheck ) ;
229 UpdateItemStatus() ;
230 }
231 }
232}
233
234void wxMenuItem::SetText(const wxString& text)
235{
236 // don't do anything if label didn't change
237 if ( m_text == text )
238 return;
239
240 wxMenuItemBase::SetText(text);
670f9935 241
2646f485
SC
242 UpdateItemText() ;
243}
244
245// radio group stuff
246// -----------------
247
248void wxMenuItem::SetAsRadioGroupStart()
249{
670f9935 250 m_isRadioGroupStart = true;
2646f485
SC
251}
252
253void wxMenuItem::SetRadioGroupStart(int start)
254{
255 wxASSERT_MSG( !m_isRadioGroupStart,
256 _T("should only be called for the next radio items") );
257
258 m_radioGroup.start = start;
259}
260
261void wxMenuItem::SetRadioGroupEnd(int end)
262{
263 wxASSERT_MSG( m_isRadioGroupStart,
264 _T("should only be called for the first radio item") );
265
266 m_radioGroup.end = end;
267}
268
269// ----------------------------------------------------------------------------
270// wxMenuItemBase
271// ----------------------------------------------------------------------------
272
273/* static */
274wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
275{
276 return wxStripMenuCodes(text);
277}
278
279wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
280 int id,
281 const wxString& name,
282 const wxString& help,
283 wxItemKind kind,
284 wxMenu *subMenu)
285{
286 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
287}