]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/menuitem.cpp
Include wx/dcclient.h according to precompiled headers of wx/wx.h (with other minor...
[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
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #endif
23
24 #include "wx/menu.h"
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 = wxGetAccelFromString( 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 UMASetMenuItemText( mhandle , index , m_text , wxFont::GetDefaultEncoding() ) ;
155 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
156 UMASetMenuItemShortcut( mhandle , index , entry ) ;
157 delete entry ;
158 }
159
160
161 void wxMenuItem::Enable(bool bDoEnable)
162 {
163 if ( m_isEnabled != bDoEnable )
164 {
165 wxMenuItemBase::Enable( bDoEnable ) ;
166 UpdateItemStatus() ;
167 }
168 }
169 void wxMenuItem::UncheckRadio()
170 {
171 if ( m_isChecked )
172 {
173 wxMenuItemBase::Check( false ) ;
174 UpdateItemStatus() ;
175 }
176 }
177
178 void wxMenuItem::Check(bool bDoCheck)
179 {
180 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
181
182 if ( m_isChecked != bDoCheck )
183 {
184 if ( GetKind() == wxITEM_RADIO )
185 {
186 if ( bDoCheck )
187 {
188 wxMenuItemBase::Check( bDoCheck ) ;
189 UpdateItemStatus() ;
190
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
234 void 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);
241
242 UpdateItemText() ;
243 }
244
245 // radio group stuff
246 // -----------------
247
248 void wxMenuItem::SetAsRadioGroupStart()
249 {
250 m_isRadioGroupStart = true;
251 }
252
253 void 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
261 void 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 */
274 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
275 {
276 return wxStripMenuCodes(text);
277 }
278
279 wxMenuItem *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 }