]> git.saurik.com Git - wxWidgets.git/blob - src/mac/menuitem.cpp
Removed a deprecated wxBitmap constructor, and some
[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 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
103 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
104 if( mhandle == NULL || index == 0)
105 return ;
106
107 UMAEnableMenuItem( mhandle , index , m_isEnabled ) ;
108 if ( IsCheckable() && IsChecked() )
109 ::SetItemMark( mhandle , index , 0x12 ) ; // checkmark
110 else
111 ::SetItemMark( mhandle , index , 0 ) ; // no mark
112
113 UMASetMenuItemText( mhandle , index , m_text ) ;
114 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
115 UMASetMenuItemShortcut( mhandle , index , entry ) ;
116 delete entry ;
117 }
118
119 void wxMenuItem::UpdateItemText()
120 {
121 if ( !m_parentMenu )
122 return ;
123
124 MenuHandle mhandle = MAC_WXHMENU(m_parentMenu->GetHMenu()) ;
125 MenuItemIndex index = m_parentMenu->MacGetIndexFromItem( this ) ;
126 if( mhandle == NULL || index == 0)
127 return ;
128
129 UMASetMenuItemText( mhandle , index , m_text ) ;
130 wxAcceleratorEntry *entry = wxGetAccelFromString( m_text ) ;
131 UMASetMenuItemShortcut( mhandle , index , entry ) ;
132 delete entry ;
133 }
134
135
136 void wxMenuItem::Enable(bool bDoEnable)
137 {
138 if ( m_isEnabled != bDoEnable )
139 {
140 wxMenuItemBase::Enable( bDoEnable ) ;
141 UpdateItemStatus() ;
142 }
143 }
144 void wxMenuItem::UncheckRadio()
145 {
146 if ( m_isChecked )
147 {
148 wxMenuItemBase::Check( false ) ;
149 UpdateItemStatus() ;
150 }
151 }
152
153 void wxMenuItem::Check(bool bDoCheck)
154 {
155 wxCHECK_RET( IsCheckable(), wxT("only checkable items may be checked") );
156
157 if ( m_isChecked != bDoCheck )
158 {
159 if ( GetKind() == wxITEM_RADIO )
160 {
161 if ( bDoCheck )
162 {
163 wxMenuItemBase::Check( bDoCheck ) ;
164 UpdateItemStatus() ;
165
166 // get the index of this item in the menu
167 const wxMenuItemList& items = m_parentMenu->GetMenuItems();
168 int pos = items.IndexOf(this);
169 wxCHECK_RET( pos != wxNOT_FOUND,
170 _T("menuitem not found in the menu items list?") );
171
172 // get the radio group range
173 int start,
174 end;
175
176 if ( m_isRadioGroupStart )
177 {
178 // we already have all information we need
179 start = pos;
180 end = m_radioGroup.end;
181 }
182 else // next radio group item
183 {
184 // get the radio group end from the start item
185 start = m_radioGroup.start;
186 end = items.Item(start)->GetData()->m_radioGroup.end;
187 }
188
189 // also uncheck all the other items in this radio group
190 wxMenuItemList::Node *node = items.Item(start);
191 for ( int n = start; n <= end && node; n++ )
192 {
193 if ( n != pos )
194 {
195 ((wxMenuItem*)node->GetData())->UncheckRadio();
196 }
197 node = node->GetNext();
198 }
199 }
200 }
201 else
202 {
203 wxMenuItemBase::Check( bDoCheck ) ;
204 UpdateItemStatus() ;
205 }
206 }
207 }
208
209 void wxMenuItem::SetText(const wxString& text)
210 {
211 // don't do anything if label didn't change
212 if ( m_text == text )
213 return;
214
215 wxMenuItemBase::SetText(text);
216
217 UpdateItemText() ;
218 }
219
220 // radio group stuff
221 // -----------------
222
223 void wxMenuItem::SetAsRadioGroupStart()
224 {
225 m_isRadioGroupStart = TRUE;
226 }
227
228 void wxMenuItem::SetRadioGroupStart(int start)
229 {
230 wxASSERT_MSG( !m_isRadioGroupStart,
231 _T("should only be called for the next radio items") );
232
233 m_radioGroup.start = start;
234 }
235
236 void wxMenuItem::SetRadioGroupEnd(int end)
237 {
238 wxASSERT_MSG( m_isRadioGroupStart,
239 _T("should only be called for the first radio item") );
240
241 m_radioGroup.end = end;
242 }
243
244 // ----------------------------------------------------------------------------
245 // wxMenuItemBase
246 // ----------------------------------------------------------------------------
247
248 /* static */
249 wxString wxMenuItemBase::GetLabelFromText(const wxString& text)
250 {
251 return wxStripMenuCodes(text);
252 }
253
254 wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu,
255 int id,
256 const wxString& name,
257 const wxString& help,
258 wxItemKind kind,
259 wxMenu *subMenu)
260 {
261 return new wxMenuItem(parentMenu, id, name, help, kind, subMenu);
262 }