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