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