]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c626a8b7 | 21 | #pragma implementation "menu.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c626a8b7 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_MENUS |
32 | ||
2bda0e17 | 33 | #ifndef WX_PRECOMP |
c626a8b7 VZ |
34 | #include "wx/frame.h" |
35 | #include "wx/menu.h" | |
36 | #include "wx/utils.h" | |
0c589ad0 | 37 | #include "wx/intl.h" |
717a57c2 | 38 | #include "wx/log.h" |
2bda0e17 KB |
39 | #endif |
40 | ||
47d67540 | 41 | #if wxUSE_OWNER_DRAWN |
c626a8b7 | 42 | #include "wx/ownerdrw.h" |
2bda0e17 KB |
43 | #endif |
44 | ||
45 | #include "wx/msw/private.h" | |
2bda0e17 | 46 | |
39d2f9a7 JS |
47 | #ifdef __WXWINCE__ |
48 | #include <windows.h> | |
49 | #include <windowsx.h> | |
50 | #include <tchar.h> | |
51 | #include <ole2.h> | |
eae4425d | 52 | #include <shellapi.h> |
39d2f9a7 | 53 | #include <commctrl.h> |
2d36b3d8 | 54 | #if _WIN32_WCE < 400 |
39d2f9a7 | 55 | #include <aygshell.h> |
39d2f9a7 JS |
56 | #endif |
57 | ||
2d36b3d8 JS |
58 | #include "wx/msw/wince/missing.h" |
59 | ||
39d2f9a7 JS |
60 | #endif |
61 | ||
2bda0e17 | 62 | // other standard headers |
2bda0e17 KB |
63 | #include <string.h> |
64 | ||
c626a8b7 VZ |
65 | // ---------------------------------------------------------------------------- |
66 | // global variables | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | extern wxMenu *wxCurrentPopupMenu; | |
70 | ||
b8d3a4f1 VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // constants | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | // the (popup) menu title has this special id | |
c25f373e | 76 | static const int idMenuTitle = -3; |
b8d3a4f1 VZ |
77 | |
78 | // ---------------------------------------------------------------------------- | |
0472ece7 | 79 | // private functions |
b8d3a4f1 | 80 | // ---------------------------------------------------------------------------- |
c626a8b7 | 81 | |
0472ece7 VZ |
82 | // make the given menu item default |
83 | static void SetDefaultMenuItem(HMENU hmenu, UINT id) | |
84 | { | |
4676948b | 85 | #ifndef __WXWINCE__ |
0472ece7 VZ |
86 | MENUITEMINFO mii; |
87 | wxZeroMemory(mii); | |
88 | mii.cbSize = sizeof(MENUITEMINFO); | |
89 | mii.fMask = MIIM_STATE; | |
90 | mii.fState = MFS_DEFAULT; | |
91 | ||
92 | if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) ) | |
93 | { | |
94 | wxLogLastError(wxT("SetMenuItemInfo")); | |
95 | } | |
4676948b JS |
96 | #endif |
97 | } | |
98 | ||
99 | #ifdef __WXWINCE__ | |
100 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) | |
101 | { | |
102 | MENUITEMINFO info; | |
103 | wxZeroMemory(info); | |
104 | info.cbSize = sizeof(info); | |
105 | info.fMask = MIIM_STATE; | |
106 | if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) ) | |
107 | wxLogLastError(wxT("GetMenuItemInfo")); | |
108 | return info.fState; | |
0472ece7 | 109 | } |
4676948b | 110 | #endif |
2bda0e17 KB |
111 | |
112 | // ============================================================================ | |
113 | // implementation | |
114 | // ============================================================================ | |
115 | ||
e70b4f10 SC |
116 | #include <wx/listimpl.cpp> |
117 | ||
118 | WX_DEFINE_LIST( wxMenuInfoList ) ; | |
119 | ||
120 | #if wxUSE_EXTENDED_RTTI | |
121 | ||
122 | WX_DEFINE_FLAGS( wxMenuStyle ) | |
123 | ||
3ff066a4 SC |
124 | wxBEGIN_FLAGS( wxMenuStyle ) |
125 | wxFLAGS_MEMBER(wxMENU_TEAROFF) | |
126 | wxEND_FLAGS( wxMenuStyle ) | |
e70b4f10 SC |
127 | |
128 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h") | |
129 | ||
3ff066a4 | 130 | wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ; |
e70b4f10 SC |
131 | |
132 | template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value) | |
133 | { | |
134 | wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ; | |
135 | } | |
136 | ||
3ff066a4 SC |
137 | wxBEGIN_PROPERTIES_TABLE(wxMenu) |
138 | wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent) | |
139 | wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) | |
af498247 | 140 | wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 SC |
141 | wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
142 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 143 | |
3ff066a4 SC |
144 | wxBEGIN_HANDLERS_TABLE(wxMenu) |
145 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 146 | |
3ff066a4 | 147 | wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle ) |
e70b4f10 SC |
148 | |
149 | WX_DEFINE_FLAGS( wxMenuBarStyle ) | |
150 | ||
3ff066a4 SC |
151 | wxBEGIN_FLAGS( wxMenuBarStyle ) |
152 | wxFLAGS_MEMBER(wxMB_DOCKABLE) | |
153 | wxEND_FLAGS( wxMenuBarStyle ) | |
e70b4f10 SC |
154 | |
155 | // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise | |
2341cf5f | 156 | bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & ) |
e70b4f10 SC |
157 | { |
158 | return true ; | |
159 | } | |
160 | ||
161 | IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback) | |
162 | ||
163 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" ) | |
164 | ||
3ff066a4 | 165 | wxBEGIN_PROPERTIES_TABLE(wxMenuInfo) |
af498247 | 166 | wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
3ff066a4 SC |
167 | wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
168 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 169 | |
3ff066a4 SC |
170 | wxBEGIN_HANDLERS_TABLE(wxMenuInfo) |
171 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 172 | |
3ff066a4 | 173 | wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title ) |
e70b4f10 | 174 | |
3ff066a4 | 175 | wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ; |
e70b4f10 SC |
176 | |
177 | template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value) | |
178 | { | |
179 | wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ; | |
180 | } | |
181 | ||
3ff066a4 SC |
182 | wxBEGIN_PROPERTIES_TABLE(wxMenuBar) |
183 | wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
184 | wxEND_PROPERTIES_TABLE() | |
e70b4f10 | 185 | |
3ff066a4 SC |
186 | wxBEGIN_HANDLERS_TABLE(wxMenuBar) |
187 | wxEND_HANDLERS_TABLE() | |
e70b4f10 | 188 | |
3ff066a4 | 189 | wxCONSTRUCTOR_DUMMY( wxMenuBar ) |
e70b4f10 SC |
190 | |
191 | #else | |
0472ece7 VZ |
192 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
193 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow) | |
e70b4f10 SC |
194 | IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject) |
195 | #endif | |
0472ece7 | 196 | |
e70b4f10 SC |
197 | const wxMenuInfoList& wxMenuBar::GetMenuInfos() const |
198 | { | |
199 | wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ; | |
200 | WX_CLEAR_LIST( wxMenuInfoList , *list ) ; | |
201 | for( size_t i = 0 ; i < GetMenuCount() ; ++i ) | |
202 | { | |
203 | wxMenuInfo* info = new wxMenuInfo() ; | |
204 | info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetLabelTop(i) ) ; | |
205 | list->Append( info ) ; | |
206 | } | |
207 | return m_menuInfos ; | |
208 | } | |
066f1b7a | 209 | |
c2dcfdef VZ |
210 | // --------------------------------------------------------------------------- |
211 | // wxMenu construction, adding and removing menu items | |
212 | // --------------------------------------------------------------------------- | |
2bda0e17 KB |
213 | |
214 | // Construct a menu with optional title (then use append) | |
717a57c2 | 215 | void wxMenu::Init() |
c626a8b7 | 216 | { |
ad9bb75f | 217 | m_doBreak = FALSE; |
0472ece7 | 218 | m_startRadioGroup = -1; |
c626a8b7 | 219 | |
717a57c2 VZ |
220 | // create the menu |
221 | m_hMenu = (WXHMENU)CreatePopupMenu(); | |
222 | if ( !m_hMenu ) | |
223 | { | |
f6bcfd97 | 224 | wxLogLastError(wxT("CreatePopupMenu")); |
717a57c2 VZ |
225 | } |
226 | ||
227 | // if we have a title, insert it in the beginning of the menu | |
c626a8b7 VZ |
228 | if ( !!m_title ) |
229 | { | |
ad9bb75f VZ |
230 | Append(idMenuTitle, m_title); |
231 | AppendSeparator(); | |
c626a8b7 | 232 | } |
2bda0e17 KB |
233 | } |
234 | ||
235 | // The wxWindow destructor will take care of deleting the submenus. | |
b8d3a4f1 | 236 | wxMenu::~wxMenu() |
2bda0e17 | 237 | { |
717a57c2 VZ |
238 | // we should free Windows resources only if Windows doesn't do it for us |
239 | // which happens if we're attached to a menubar or a submenu of another | |
240 | // menu | |
241 | if ( !IsAttached() && !GetParent() ) | |
c2dcfdef | 242 | { |
ad9bb75f VZ |
243 | if ( !::DestroyMenu(GetHmenu()) ) |
244 | { | |
f6bcfd97 | 245 | wxLogLastError(wxT("DestroyMenu")); |
ad9bb75f | 246 | } |
c2dcfdef | 247 | } |
c626a8b7 | 248 | |
ad9bb75f VZ |
249 | #if wxUSE_ACCEL |
250 | // delete accels | |
251 | WX_CLEAR_ARRAY(m_accels); | |
252 | #endif // wxUSE_ACCEL | |
2bda0e17 KB |
253 | } |
254 | ||
b8d3a4f1 | 255 | void wxMenu::Break() |
2bda0e17 | 256 | { |
717a57c2 | 257 | // this will take effect during the next call to Append() |
c2dcfdef | 258 | m_doBreak = TRUE; |
2bda0e17 KB |
259 | } |
260 | ||
0472ece7 VZ |
261 | void wxMenu::Attach(wxMenuBarBase *menubar) |
262 | { | |
263 | wxMenuBase::Attach(menubar); | |
264 | ||
265 | EndRadioGroup(); | |
266 | } | |
267 | ||
717a57c2 VZ |
268 | #if wxUSE_ACCEL |
269 | ||
270 | int wxMenu::FindAccel(int id) const | |
271 | { | |
272 | size_t n, count = m_accels.GetCount(); | |
273 | for ( n = 0; n < count; n++ ) | |
274 | { | |
275 | if ( m_accels[n]->m_command == id ) | |
276 | return n; | |
277 | } | |
278 | ||
279 | return wxNOT_FOUND; | |
280 | } | |
281 | ||
282 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
2bda0e17 | 283 | { |
f6bcfd97 | 284 | if ( item->IsSubMenu() ) |
717a57c2 | 285 | { |
f6bcfd97 | 286 | wxMenu *submenu = item->GetSubMenu(); |
222ed1d6 | 287 | wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst(); |
f6bcfd97 BP |
288 | while ( node ) |
289 | { | |
290 | UpdateAccel(node->GetData()); | |
291 | ||
292 | node = node->GetNext(); | |
293 | } | |
717a57c2 | 294 | } |
f6bcfd97 | 295 | else if ( !item->IsSeparator() ) |
717a57c2 | 296 | { |
f6bcfd97 BP |
297 | // find the (new) accel for this item |
298 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
717a57c2 | 299 | if ( accel ) |
f6bcfd97 BP |
300 | accel->m_command = item->GetId(); |
301 | ||
302 | // find the old one | |
303 | int n = FindAccel(item->GetId()); | |
304 | if ( n == wxNOT_FOUND ) | |
305 | { | |
306 | // no old, add new if any | |
307 | if ( accel ) | |
308 | m_accels.Add(accel); | |
309 | else | |
310 | return; // skipping RebuildAccelTable() below | |
311 | } | |
717a57c2 | 312 | else |
f6bcfd97 BP |
313 | { |
314 | // replace old with new or just remove the old one if no new | |
315 | delete m_accels[n]; | |
316 | if ( accel ) | |
317 | m_accels[n] = accel; | |
318 | else | |
b54e41c5 | 319 | m_accels.RemoveAt(n); |
f6bcfd97 | 320 | } |
717a57c2 | 321 | |
f6bcfd97 BP |
322 | if ( IsAttached() ) |
323 | { | |
4224f059 | 324 | GetMenuBar()->RebuildAccelTable(); |
f6bcfd97 | 325 | } |
42e69d6b | 326 | } |
f6bcfd97 | 327 | //else: it is a separator, they can't have accels, nothing to do |
717a57c2 VZ |
328 | } |
329 | ||
330 | #endif // wxUSE_ACCEL | |
331 | ||
332 | // append a new item or submenu to the menu | |
333 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
334 | { | |
335 | #if wxUSE_ACCEL | |
336 | UpdateAccel(pItem); | |
d427503c | 337 | #endif // wxUSE_ACCEL |
42e69d6b | 338 | |
c626a8b7 | 339 | UINT flags = 0; |
2bda0e17 | 340 | |
c2dcfdef VZ |
341 | // if "Break" has just been called, insert a menu break before this item |
342 | // (and don't forget to reset the flag) | |
c626a8b7 VZ |
343 | if ( m_doBreak ) { |
344 | flags |= MF_MENUBREAK; | |
345 | m_doBreak = FALSE; | |
346 | } | |
347 | ||
348 | if ( pItem->IsSeparator() ) { | |
349 | flags |= MF_SEPARATOR; | |
350 | } | |
2bda0e17 | 351 | |
c2dcfdef VZ |
352 | // id is the numeric id for normal menu items and HMENU for submenus as |
353 | // required by ::AppendMenu() API | |
c626a8b7 | 354 | UINT id; |
c2dcfdef VZ |
355 | wxMenu *submenu = pItem->GetSubMenu(); |
356 | if ( submenu != NULL ) { | |
717a57c2 VZ |
357 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); |
358 | ||
359 | submenu->SetParent(this); | |
2bda0e17 | 360 | |
c2dcfdef | 361 | id = (UINT)submenu->GetHMenu(); |
2bda0e17 | 362 | |
c626a8b7 VZ |
363 | flags |= MF_POPUP; |
364 | } | |
365 | else { | |
366 | id = pItem->GetId(); | |
367 | } | |
2bda0e17 | 368 | |
39d2f9a7 JS |
369 | #ifdef __WXWINCE__ |
370 | wxString strippedString; | |
371 | #endif | |
372 | ||
837e5743 | 373 | LPCTSTR pData; |
2bda0e17 | 374 | |
47d67540 | 375 | #if wxUSE_OWNER_DRAWN |
c626a8b7 VZ |
376 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? |
377 | // item draws itself, pass pointer to it in data parameter | |
378 | flags |= MF_OWNERDRAW; | |
837e5743 | 379 | pData = (LPCTSTR)pItem; |
c626a8b7 VZ |
380 | } |
381 | else | |
2bda0e17 | 382 | #endif |
c626a8b7 VZ |
383 | { |
384 | // menu is just a normal string (passed in data parameter) | |
385 | flags |= MF_STRING; | |
8fb3a512 | 386 | |
39d2f9a7 JS |
387 | #ifdef __WXWINCE__ |
388 | strippedString = wxStripMenuCodes(pItem->GetText()); | |
389 | pData = (wxChar*)strippedString.c_str(); | |
390 | #else | |
f5166ed4 | 391 | pData = (wxChar*)pItem->GetText().c_str(); |
39d2f9a7 | 392 | #endif |
c626a8b7 | 393 | } |
2bda0e17 | 394 | |
717a57c2 VZ |
395 | BOOL ok; |
396 | if ( pos == (size_t)-1 ) | |
397 | { | |
398 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); | |
399 | } | |
400 | else | |
401 | { | |
402 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); | |
403 | } | |
404 | ||
405 | if ( !ok ) | |
c626a8b7 | 406 | { |
f6bcfd97 | 407 | wxLogLastError(wxT("Insert or AppendMenu")); |
717a57c2 VZ |
408 | |
409 | return FALSE; | |
c626a8b7 | 410 | } |
42e69d6b | 411 | |
0472ece7 VZ |
412 | // if we just appended the title, highlight it |
413 | #ifdef __WIN32__ | |
414 | if ( (int)id == idMenuTitle ) | |
415 | { | |
416 | // visually select the menu title | |
417 | SetDefaultMenuItem(GetHmenu(), id); | |
418 | } | |
42e69d6b VZ |
419 | #endif // __WIN32__ |
420 | ||
0472ece7 | 421 | // if we're already attached to the menubar, we must update it |
4224f059 | 422 | if ( IsAttached() && GetMenuBar()->IsAttached() ) |
0472ece7 | 423 | { |
4224f059 | 424 | GetMenuBar()->Refresh(); |
0472ece7 VZ |
425 | } |
426 | ||
427 | return TRUE; | |
428 | } | |
429 | ||
430 | void wxMenu::EndRadioGroup() | |
431 | { | |
0472ece7 VZ |
432 | // we're not inside a radio group any longer |
433 | m_startRadioGroup = -1; | |
2bda0e17 KB |
434 | } |
435 | ||
9add9367 | 436 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) |
2bda0e17 | 437 | { |
9add9367 | 438 | wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") ); |
0472ece7 | 439 | |
be15b995 VZ |
440 | bool check = FALSE; |
441 | ||
546bfbea | 442 | if ( item->GetKind() == wxITEM_RADIO ) |
0472ece7 | 443 | { |
be15b995 VZ |
444 | int count = GetMenuItemCount(); |
445 | ||
0472ece7 VZ |
446 | if ( m_startRadioGroup == -1 ) |
447 | { | |
448 | // start a new radio group | |
be15b995 VZ |
449 | m_startRadioGroup = count; |
450 | ||
451 | // for now it has just one element | |
452 | item->SetAsRadioGroupStart(); | |
453 | item->SetRadioGroupEnd(m_startRadioGroup); | |
454 | ||
455 | // ensure that we have a checked item in the radio group | |
456 | check = TRUE; | |
457 | } | |
458 | else // extend the current radio group | |
459 | { | |
460 | // we need to update its end item | |
461 | item->SetRadioGroupStart(m_startRadioGroup); | |
222ed1d6 | 462 | wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup); |
be15b995 VZ |
463 | |
464 | if ( node ) | |
465 | { | |
466 | node->GetData()->SetRadioGroupEnd(count); | |
467 | } | |
468 | else | |
469 | { | |
470 | wxFAIL_MSG( _T("where is the radio group start item?") ); | |
471 | } | |
0472ece7 VZ |
472 | } |
473 | } | |
474 | else // not a radio item | |
475 | { | |
476 | EndRadioGroup(); | |
477 | } | |
478 | ||
be15b995 VZ |
479 | if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) ) |
480 | { | |
9add9367 | 481 | return NULL; |
be15b995 VZ |
482 | } |
483 | ||
484 | if ( check ) | |
485 | { | |
486 | // check the item initially | |
487 | item->Check(TRUE); | |
488 | } | |
489 | ||
9add9367 | 490 | return item; |
2bda0e17 KB |
491 | } |
492 | ||
9add9367 | 493 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) |
2bda0e17 | 494 | { |
9add9367 RD |
495 | if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos)) |
496 | return item; | |
497 | else | |
498 | return NULL; | |
2bda0e17 KB |
499 | } |
500 | ||
717a57c2 | 501 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) |
2bda0e17 | 502 | { |
717a57c2 VZ |
503 | // we need to find the items position in the child list |
504 | size_t pos; | |
222ed1d6 | 505 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 | 506 | for ( pos = 0; node; pos++ ) |
c626a8b7 | 507 | { |
717a57c2 | 508 | if ( node->GetData() == item ) |
c626a8b7 | 509 | break; |
717a57c2 VZ |
510 | |
511 | node = node->GetNext(); | |
c626a8b7 VZ |
512 | } |
513 | ||
717a57c2 VZ |
514 | // DoRemove() (unlike Remove) can only be called for existing item! |
515 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
c626a8b7 | 516 | |
717a57c2 VZ |
517 | #if wxUSE_ACCEL |
518 | // remove the corresponding accel from the accel table | |
519 | int n = FindAccel(item->GetId()); | |
520 | if ( n != wxNOT_FOUND ) | |
521 | { | |
522 | delete m_accels[n]; | |
c626a8b7 | 523 | |
b54e41c5 | 524 | m_accels.RemoveAt(n); |
c626a8b7 | 525 | } |
717a57c2 VZ |
526 | //else: this item doesn't have an accel, nothing to do |
527 | #endif // wxUSE_ACCEL | |
528 | ||
529 | // remove the item from the menu | |
530 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
531 | { | |
f6bcfd97 | 532 | wxLogLastError(wxT("RemoveMenu")); |
c626a8b7 VZ |
533 | } |
534 | ||
4224f059 | 535 | if ( IsAttached() && GetMenuBar()->IsAttached() ) |
717a57c2 VZ |
536 | { |
537 | // otherwise, the chane won't be visible | |
4224f059 | 538 | GetMenuBar()->Refresh(); |
717a57c2 | 539 | } |
2bda0e17 | 540 | |
717a57c2 VZ |
541 | // and from internal data structures |
542 | return wxMenuBase::DoRemove(item); | |
543 | } | |
d427503c | 544 | |
42e69d6b VZ |
545 | // --------------------------------------------------------------------------- |
546 | // accelerator helpers | |
547 | // --------------------------------------------------------------------------- | |
548 | ||
717a57c2 VZ |
549 | #if wxUSE_ACCEL |
550 | ||
42e69d6b VZ |
551 | // create the wxAcceleratorEntries for our accels and put them into provided |
552 | // array - return the number of accels we have | |
553 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
554 | { | |
555 | size_t count = GetAccelCount(); | |
556 | for ( size_t n = 0; n < count; n++ ) | |
557 | { | |
974e8d94 | 558 | *accels++ = *m_accels[n]; |
42e69d6b VZ |
559 | } |
560 | ||
561 | return count; | |
562 | } | |
563 | ||
d427503c VZ |
564 | #endif // wxUSE_ACCEL |
565 | ||
c2dcfdef | 566 | // --------------------------------------------------------------------------- |
717a57c2 | 567 | // set wxMenu title |
c2dcfdef VZ |
568 | // --------------------------------------------------------------------------- |
569 | ||
2bda0e17 KB |
570 | void wxMenu::SetTitle(const wxString& label) |
571 | { | |
c626a8b7 VZ |
572 | bool hasNoTitle = m_title.IsEmpty(); |
573 | m_title = label; | |
b8d3a4f1 | 574 | |
c50f1fb9 | 575 | HMENU hMenu = GetHmenu(); |
b8d3a4f1 | 576 | |
c626a8b7 | 577 | if ( hasNoTitle ) |
b8d3a4f1 | 578 | { |
c626a8b7 VZ |
579 | if ( !label.IsEmpty() ) |
580 | { | |
717a57c2 VZ |
581 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, |
582 | (unsigned)idMenuTitle, m_title) || | |
583 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
c626a8b7 | 584 | { |
f6bcfd97 | 585 | wxLogLastError(wxT("InsertMenu")); |
c626a8b7 VZ |
586 | } |
587 | } | |
b8d3a4f1 VZ |
588 | } |
589 | else | |
590 | { | |
c626a8b7 VZ |
591 | if ( label.IsEmpty() ) |
592 | { | |
593 | // remove the title and the separator after it | |
594 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
595 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
596 | { | |
f6bcfd97 | 597 | wxLogLastError(wxT("RemoveMenu")); |
c626a8b7 VZ |
598 | } |
599 | } | |
600 | else | |
601 | { | |
602 | // modify the title | |
4676948b JS |
603 | #ifdef __WXWINCE__ |
604 | MENUITEMINFO info; | |
605 | wxZeroMemory(info); | |
606 | info.cbSize = sizeof(info); | |
607 | info.fMask = MIIM_TYPE; | |
608 | info.fType = MFT_STRING; | |
609 | info.cch = m_title.Length(); | |
610 | info.dwTypeData = (LPTSTR) m_title.c_str(); | |
611 | if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) ) | |
612 | { | |
613 | wxLogLastError(wxT("SetMenuItemInfo")); | |
614 | } | |
615 | #else | |
c626a8b7 | 616 | if ( !ModifyMenu(hMenu, 0u, |
717a57c2 VZ |
617 | MF_BYPOSITION | MF_STRING, |
618 | (unsigned)idMenuTitle, m_title) ) | |
c626a8b7 | 619 | { |
f6bcfd97 | 620 | wxLogLastError(wxT("ModifyMenu")); |
c626a8b7 | 621 | } |
4676948b | 622 | #endif |
c626a8b7 | 623 | } |
b8d3a4f1 | 624 | } |
b8d3a4f1 | 625 | |
42e69d6b | 626 | #ifdef __WIN32__ |
c626a8b7 VZ |
627 | // put the title string in bold face |
628 | if ( !m_title.IsEmpty() ) | |
a3f4e9e8 | 629 | { |
0472ece7 | 630 | SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); |
a3f4e9e8 | 631 | } |
717a57c2 | 632 | #endif // Win32 |
2bda0e17 KB |
633 | } |
634 | ||
c2dcfdef VZ |
635 | // --------------------------------------------------------------------------- |
636 | // event processing | |
637 | // --------------------------------------------------------------------------- | |
2bda0e17 | 638 | |
debe6624 | 639 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) |
2bda0e17 | 640 | { |
a3f4e9e8 VZ |
641 | // ignore commands from the menu title |
642 | ||
643 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
644 | if ( id != (WXWORD)idMenuTitle ) | |
645 | { | |
3ca6a5f0 BP |
646 | // VZ: previosuly, the command int was set to id too which was quite |
647 | // useless anyhow (as it could be retrieved using GetId()) and | |
648 | // uncompatible with wxGTK, so now we use the command int instead | |
649 | // to pass the checked status | |
4676948b JS |
650 | UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ; |
651 | SendEvent(id, menuState & MF_CHECKED); | |
a3f4e9e8 VZ |
652 | } |
653 | ||
654 | return TRUE; | |
2bda0e17 KB |
655 | } |
656 | ||
c2dcfdef VZ |
657 | // --------------------------------------------------------------------------- |
658 | // other | |
659 | // --------------------------------------------------------------------------- | |
2bda0e17 | 660 | |
717a57c2 VZ |
661 | wxWindow *wxMenu::GetWindow() const |
662 | { | |
663 | if ( m_invokingWindow != NULL ) | |
664 | return m_invokingWindow; | |
4224f059 DE |
665 | else if ( GetMenuBar() != NULL) |
666 | return GetMenuBar()->GetFrame(); | |
717a57c2 VZ |
667 | |
668 | return NULL; | |
c2dcfdef VZ |
669 | } |
670 | ||
671 | // --------------------------------------------------------------------------- | |
2bda0e17 | 672 | // Menu Bar |
c2dcfdef VZ |
673 | // --------------------------------------------------------------------------- |
674 | ||
675 | void wxMenuBar::Init() | |
2bda0e17 | 676 | { |
c626a8b7 | 677 | m_eventHandler = this; |
c626a8b7 | 678 | m_hMenu = 0; |
a9928e9d | 679 | #if wxUSE_TOOLBAR && defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 | 680 | m_toolBar = NULL; |
a96b4743 JS |
681 | #endif |
682 | // Not using a combined wxToolBar/wxMenuBar? then use | |
683 | // a commandbar in WinCE .NET just to implement the | |
684 | // menubar. | |
a9928e9d | 685 | #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) |
a96b4743 | 686 | m_commandBar = NULL; |
a9928e9d | 687 | m_adornmentsAdded = false; |
39d2f9a7 | 688 | #endif |
cba2db0c | 689 | } |
2bda0e17 | 690 | |
c2dcfdef VZ |
691 | wxMenuBar::wxMenuBar() |
692 | { | |
693 | Init(); | |
694 | } | |
695 | ||
cba2db0c JS |
696 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) |
697 | { | |
c2dcfdef | 698 | Init(); |
2bda0e17 KB |
699 | } |
700 | ||
c2dcfdef | 701 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) |
2bda0e17 | 702 | { |
c2dcfdef VZ |
703 | Init(); |
704 | ||
a8cfd0cb | 705 | m_titles.Alloc(count); |
c2dcfdef | 706 | |
a8cfd0cb VZ |
707 | for ( int i = 0; i < count; i++ ) |
708 | { | |
709 | m_menus.Append(menus[i]); | |
710 | m_titles.Add(titles[i]); | |
2bda0e17 | 711 | |
a8cfd0cb VZ |
712 | menus[i]->Attach(this); |
713 | } | |
2bda0e17 KB |
714 | } |
715 | ||
b8d3a4f1 | 716 | wxMenuBar::~wxMenuBar() |
2bda0e17 | 717 | { |
a96b4743 | 718 | // In Windows CE (not .NET), the menubar is always associated |
39d2f9a7 | 719 | // with a toolbar, which destroys the menu implicitly. |
a9928e9d | 720 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
bf95a04f JS |
721 | if (GetToolBar()) |
722 | GetToolBar()->SetMenuBar(NULL); | |
723 | #else | |
7a0363dd JS |
724 | // we should free Windows resources only if Windows doesn't do it for us |
725 | // which happens if we're attached to a frame | |
726 | if (m_hMenu && !IsAttached()) | |
727 | { | |
a9928e9d | 728 | #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) |
a96b4743 JS |
729 | ::DestroyWindow((HWND) m_commandBar); |
730 | m_commandBar = (WXHWND) NULL; | |
731 | #else | |
7a0363dd | 732 | ::DestroyMenu((HMENU)m_hMenu); |
a96b4743 | 733 | #endif |
7a0363dd JS |
734 | m_hMenu = (WXHMENU)NULL; |
735 | } | |
39d2f9a7 | 736 | #endif |
c2dcfdef | 737 | } |
2bda0e17 | 738 | |
c2dcfdef VZ |
739 | // --------------------------------------------------------------------------- |
740 | // wxMenuBar helpers | |
741 | // --------------------------------------------------------------------------- | |
742 | ||
743 | void wxMenuBar::Refresh() | |
744 | { | |
065de612 | 745 | wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); |
c2dcfdef | 746 | |
a9928e9d | 747 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 JS |
748 | if (GetToolBar()) |
749 | { | |
750 | CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0); | |
751 | } | |
a9928e9d | 752 | #elif defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) |
a96b4743 JS |
753 | if (m_commandBar) |
754 | DrawMenuBar((HWND) m_commandBar); | |
39d2f9a7 | 755 | #else |
1e6feb95 | 756 | DrawMenuBar(GetHwndOf(GetFrame())); |
39d2f9a7 | 757 | #endif |
c2dcfdef VZ |
758 | } |
759 | ||
760 | WXHMENU wxMenuBar::Create() | |
761 | { | |
beb471b5 JS |
762 | // Note: this totally doesn't work on Smartphone, |
763 | // since you have to use resources. | |
764 | // We'll have to find another way to add a menu | |
765 | // by changing/adding menu items to an existing menu. | |
a9928e9d | 766 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 JS |
767 | if ( m_hMenu != 0 ) |
768 | return m_hMenu; | |
769 | ||
770 | if (!GetToolBar()) | |
771 | return 0; | |
772 | ||
773 | HWND hCommandBar = (HWND) GetToolBar()->GetHWND(); | |
774 | HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0); | |
775 | if (hMenu) | |
776 | { | |
777 | TBBUTTON tbButton; | |
778 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
779 | tbButton.iBitmap = I_IMAGENONE; | |
780 | tbButton.fsState = TBSTATE_ENABLED; | |
781 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
782 | ||
783 | size_t i; | |
784 | for (i = 0; i < GetMenuCount(); i++) | |
785 | { | |
786 | HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ; | |
787 | tbButton.dwData = (DWORD)hPopupMenu; | |
788 | wxString label = wxStripMenuCodes(GetLabelTop(i)); | |
789 | tbButton.iString = (int) label.c_str(); | |
beb471b5 JS |
790 | |
791 | int position = i; | |
39d2f9a7 JS |
792 | |
793 | tbButton.idCommand = NewControlId(); | |
beb471b5 | 794 | if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton)) |
39d2f9a7 JS |
795 | { |
796 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
797 | } | |
798 | } | |
799 | } | |
800 | m_hMenu = (WXHMENU) hMenu; | |
801 | return m_hMenu; | |
802 | #else | |
3ca6a5f0 | 803 | if ( m_hMenu != 0 ) |
717a57c2 | 804 | return m_hMenu; |
1cf27c63 | 805 | |
c2dcfdef | 806 | m_hMenu = (WXHMENU)::CreateMenu(); |
2bda0e17 | 807 | |
c2dcfdef | 808 | if ( !m_hMenu ) |
c626a8b7 | 809 | { |
f6bcfd97 | 810 | wxLogLastError(wxT("CreateMenu")); |
c626a8b7 | 811 | } |
c2dcfdef | 812 | else |
c626a8b7 | 813 | { |
222ed1d6 MB |
814 | size_t count = GetMenuCount(), i; |
815 | wxMenuList::iterator it; | |
816 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
c2dcfdef VZ |
817 | { |
818 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
222ed1d6 | 819 | (UINT)(*it)->GetHMenu(), |
c2dcfdef VZ |
820 | m_titles[i]) ) |
821 | { | |
f6bcfd97 | 822 | wxLogLastError(wxT("AppendMenu")); |
c2dcfdef VZ |
823 | } |
824 | } | |
c626a8b7 | 825 | } |
c626a8b7 | 826 | |
c2dcfdef | 827 | return m_hMenu; |
39d2f9a7 | 828 | #endif |
2bda0e17 KB |
829 | } |
830 | ||
b2c5f143 DE |
831 | int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos) |
832 | { | |
833 | wxASSERT(menu); | |
834 | wxASSERT(menu->GetHMenu()); | |
835 | wxASSERT(m_hMenu); | |
8cc4850c JS |
836 | |
837 | #if defined(__WXWINCE__) | |
838 | int totalMSWItems = GetMenuCount(); | |
839 | #else | |
b2c5f143 | 840 | int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu); |
8cc4850c JS |
841 | #endif |
842 | ||
b2c5f143 DE |
843 | int i; // For old C++ compatibility |
844 | for(i=wxpos; i<totalMSWItems; i++) | |
845 | { | |
846 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
847 | return i; | |
848 | } | |
849 | for(i=0; i<wxpos; i++) | |
850 | { | |
851 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
852 | return i; | |
853 | } | |
854 | wxFAIL; | |
855 | return -1; | |
856 | } | |
857 | ||
c2dcfdef | 858 | // --------------------------------------------------------------------------- |
3dfac970 | 859 | // wxMenuBar functions to work with the top level submenus |
c2dcfdef VZ |
860 | // --------------------------------------------------------------------------- |
861 | ||
3dfac970 VZ |
862 | // NB: we don't support owner drawn top level items for now, if we do these |
863 | // functions would have to be changed to use wxMenuItem as well | |
2bda0e17 | 864 | |
a8cfd0cb | 865 | void wxMenuBar::EnableTop(size_t pos, bool enable) |
2bda0e17 | 866 | { |
717a57c2 | 867 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); |
b2c5f143 | 868 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
717a57c2 VZ |
869 | |
870 | int flag = enable ? MF_ENABLED : MF_GRAYED; | |
2bda0e17 | 871 | |
b2c5f143 | 872 | EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag); |
adc6fb16 VZ |
873 | |
874 | Refresh(); | |
2bda0e17 KB |
875 | } |
876 | ||
a8cfd0cb | 877 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) |
2bda0e17 | 878 | { |
717a57c2 VZ |
879 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); |
880 | ||
881 | m_titles[pos] = label; | |
882 | ||
883 | if ( !IsAttached() ) | |
884 | { | |
885 | return; | |
886 | } | |
887 | //else: have to modify the existing menu | |
888 | ||
b2c5f143 DE |
889 | int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos); |
890 | ||
8cd85069 | 891 | UINT id; |
b2c5f143 | 892 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION); |
c2dcfdef | 893 | if ( flagsOld == 0xFFFFFFFF ) |
c626a8b7 | 894 | { |
223d09f6 | 895 | wxLogLastError(wxT("GetMenuState")); |
c2dcfdef VZ |
896 | |
897 | return; | |
898 | } | |
899 | ||
900 | if ( flagsOld & MF_POPUP ) | |
901 | { | |
902 | // HIBYTE contains the number of items in the submenu in this case | |
ad9bb75f | 903 | flagsOld &= 0xff; |
b2c5f143 | 904 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos); |
c626a8b7 VZ |
905 | } |
906 | else | |
8cd85069 VZ |
907 | { |
908 | id = pos; | |
909 | } | |
910 | ||
4676948b JS |
911 | #ifdef __WXWINCE__ |
912 | MENUITEMINFO info; | |
913 | wxZeroMemory(info); | |
914 | info.cbSize = sizeof(info); | |
915 | info.fMask = MIIM_TYPE; | |
916 | info.fType = MFT_STRING; | |
917 | info.cch = label.Length(); | |
918 | info.dwTypeData = (LPTSTR) label.c_str(); | |
919 | if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) ) | |
920 | { | |
921 | wxLogLastError(wxT("SetMenuItemInfo")); | |
922 | } | |
923 | ||
924 | #else | |
b2c5f143 | 925 | if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld, |
4676948b | 926 | id, label) == (int)0xFFFFFFFF ) |
c2dcfdef | 927 | { |
f6bcfd97 | 928 | wxLogLastError(wxT("ModifyMenu")); |
c2dcfdef | 929 | } |
4676948b | 930 | #endif |
717a57c2 VZ |
931 | |
932 | Refresh(); | |
2bda0e17 KB |
933 | } |
934 | ||
a8cfd0cb | 935 | wxString wxMenuBar::GetLabelTop(size_t pos) const |
2bda0e17 | 936 | { |
717a57c2 VZ |
937 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, |
938 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
8cd85069 | 939 | |
4a1c207c | 940 | return wxMenuItem::GetLabelFromText(m_titles[pos]); |
2bda0e17 KB |
941 | } |
942 | ||
ad9bb75f VZ |
943 | // --------------------------------------------------------------------------- |
944 | // wxMenuBar construction | |
945 | // --------------------------------------------------------------------------- | |
946 | ||
a8cfd0cb | 947 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) |
1cf27c63 | 948 | { |
ad9bb75f VZ |
949 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); |
950 | if ( !menuOld ) | |
f7f50f49 VZ |
951 | return NULL; |
952 | ||
ad9bb75f | 953 | m_titles[pos] = title; |
a8cfd0cb | 954 | |
ad9bb75f | 955 | if ( IsAttached() ) |
a8cfd0cb | 956 | { |
b2c5f143 DE |
957 | int mswpos = MSWPositionForWxMenu(menuOld,pos); |
958 | ||
ad9bb75f | 959 | // can't use ModifyMenu() because it deletes the submenu it replaces |
b2c5f143 | 960 | if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) ) |
a8cfd0cb | 961 | { |
f6bcfd97 | 962 | wxLogLastError(wxT("RemoveMenu")); |
a8cfd0cb | 963 | } |
1cf27c63 | 964 | |
b2c5f143 | 965 | if ( !::InsertMenu(GetHmenu(), (UINT)mswpos, |
ad9bb75f VZ |
966 | MF_BYPOSITION | MF_POPUP | MF_STRING, |
967 | (UINT)GetHmenuOf(menu), title) ) | |
968 | { | |
f6bcfd97 | 969 | wxLogLastError(wxT("InsertMenu")); |
ad9bb75f VZ |
970 | } |
971 | ||
717a57c2 VZ |
972 | #if wxUSE_ACCEL |
973 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
974 | { | |
975 | // need to rebuild accell table | |
976 | RebuildAccelTable(); | |
977 | } | |
978 | #endif // wxUSE_ACCEL | |
979 | ||
ad9bb75f | 980 | Refresh(); |
a8cfd0cb | 981 | } |
ad9bb75f VZ |
982 | |
983 | return menuOld; | |
1cf27c63 UM |
984 | } |
985 | ||
a8cfd0cb | 986 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) |
1cf27c63 | 987 | { |
b2c5f143 DE |
988 | // Find out which MSW item before which we'll be inserting before |
989 | // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu. | |
3d27b574 DE |
990 | // If IsAttached() is false this won't be used anyway |
991 | int mswpos = (!IsAttached() || (pos == m_menus.GetCount())) | |
b2c5f143 DE |
992 | ? -1 // append the menu |
993 | : MSWPositionForWxMenu(GetMenu(pos),pos); | |
994 | ||
ad9bb75f | 995 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) |
a8cfd0cb | 996 | return FALSE; |
1cf27c63 | 997 | |
ad9bb75f VZ |
998 | m_titles.Insert(title, pos); |
999 | ||
ad9bb75f VZ |
1000 | if ( IsAttached() ) |
1001 | { | |
a9928e9d | 1002 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 JS |
1003 | if (!GetToolBar()) |
1004 | return FALSE; | |
1005 | TBBUTTON tbButton; | |
1006 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1007 | tbButton.iBitmap = I_IMAGENONE; | |
1008 | tbButton.fsState = TBSTATE_ENABLED; | |
1009 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1010 | ||
1011 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1012 | tbButton.dwData = (DWORD)hPopupMenu; | |
1013 | wxString label = wxStripMenuCodes(title); | |
1014 | tbButton.iString = (int) label.c_str(); | |
1015 | ||
1016 | tbButton.idCommand = NewControlId(); | |
1017 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1018 | { | |
1019 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1020 | return FALSE; | |
1021 | } | |
1022 | #else | |
b2c5f143 | 1023 | if ( !::InsertMenu(GetHmenu(), mswpos, |
ad9bb75f VZ |
1024 | MF_BYPOSITION | MF_POPUP | MF_STRING, |
1025 | (UINT)GetHmenuOf(menu), title) ) | |
1026 | { | |
f6bcfd97 | 1027 | wxLogLastError(wxT("InsertMenu")); |
ad9bb75f | 1028 | } |
39d2f9a7 | 1029 | #endif |
717a57c2 VZ |
1030 | #if wxUSE_ACCEL |
1031 | if ( menu->HasAccels() ) | |
1032 | { | |
1033 | // need to rebuild accell table | |
1034 | RebuildAccelTable(); | |
1035 | } | |
1036 | #endif // wxUSE_ACCEL | |
1037 | ||
ad9bb75f | 1038 | Refresh(); |
a8cfd0cb | 1039 | } |
ad9bb75f VZ |
1040 | |
1041 | return TRUE; | |
1cf27c63 UM |
1042 | } |
1043 | ||
ad9bb75f | 1044 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) |
2bda0e17 | 1045 | { |
ad9bb75f VZ |
1046 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; |
1047 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
2bda0e17 | 1048 | |
717a57c2 VZ |
1049 | if ( !wxMenuBarBase::Append(menu, title) ) |
1050 | return FALSE; | |
1051 | ||
717a57c2 VZ |
1052 | m_titles.Add(title); |
1053 | ||
ad9bb75f VZ |
1054 | if ( IsAttached() ) |
1055 | { | |
a9928e9d | 1056 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 JS |
1057 | if (!GetToolBar()) |
1058 | return FALSE; | |
1059 | TBBUTTON tbButton; | |
1060 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1061 | tbButton.iBitmap = I_IMAGENONE; | |
1062 | tbButton.fsState = TBSTATE_ENABLED; | |
1063 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1064 | ||
1065 | size_t pos = GetMenuCount(); | |
1066 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1067 | tbButton.dwData = (DWORD)hPopupMenu; | |
1068 | wxString label = wxStripMenuCodes(title); | |
1069 | tbButton.iString = (int) label.c_str(); | |
1070 | ||
1071 | tbButton.idCommand = NewControlId(); | |
1072 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1073 | { | |
1074 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1075 | return FALSE; | |
1076 | } | |
1077 | #else | |
ad9bb75f VZ |
1078 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, |
1079 | (UINT)submenu, title) ) | |
1080 | { | |
1081 | wxLogLastError(wxT("AppendMenu")); | |
1082 | } | |
39d2f9a7 | 1083 | #endif |
ad9bb75f | 1084 | |
717a57c2 VZ |
1085 | #if wxUSE_ACCEL |
1086 | if ( menu->HasAccels() ) | |
1087 | { | |
39d2f9a7 | 1088 | // need to rebuild accelerator table |
717a57c2 VZ |
1089 | RebuildAccelTable(); |
1090 | } | |
1091 | #endif // wxUSE_ACCEL | |
1092 | ||
ad9bb75f VZ |
1093 | Refresh(); |
1094 | } | |
2bda0e17 | 1095 | |
a8cfd0cb | 1096 | return TRUE; |
2bda0e17 KB |
1097 | } |
1098 | ||
a8cfd0cb | 1099 | wxMenu *wxMenuBar::Remove(size_t pos) |
2bda0e17 | 1100 | { |
a8cfd0cb VZ |
1101 | wxMenu *menu = wxMenuBarBase::Remove(pos); |
1102 | if ( !menu ) | |
1103 | return NULL; | |
c626a8b7 | 1104 | |
ad9bb75f VZ |
1105 | if ( IsAttached() ) |
1106 | { | |
a9928e9d | 1107 | #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)) |
39d2f9a7 JS |
1108 | if (GetToolBar()) |
1109 | { | |
1110 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0)) | |
1111 | { | |
1112 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
1113 | } | |
1114 | } | |
1115 | #else | |
b2c5f143 | 1116 | if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) ) |
ad9bb75f | 1117 | { |
f6bcfd97 | 1118 | wxLogLastError(wxT("RemoveMenu")); |
ad9bb75f | 1119 | } |
39d2f9a7 | 1120 | #endif |
c4053ed3 | 1121 | |
717a57c2 VZ |
1122 | #if wxUSE_ACCEL |
1123 | if ( menu->HasAccels() ) | |
1124 | { | |
1125 | // need to rebuild accell table | |
1126 | RebuildAccelTable(); | |
1127 | } | |
1128 | #endif // wxUSE_ACCEL | |
1129 | ||
ad9bb75f VZ |
1130 | Refresh(); |
1131 | } | |
2bda0e17 | 1132 | |
c4053ed3 RN |
1133 | |
1134 | m_titles.RemoveAt(pos); | |
2bda0e17 | 1135 | |
a8cfd0cb | 1136 | return menu; |
2bda0e17 KB |
1137 | } |
1138 | ||
d427503c | 1139 | #if wxUSE_ACCEL |
717a57c2 VZ |
1140 | |
1141 | void wxMenuBar::RebuildAccelTable() | |
1142 | { | |
1143 | // merge the accelerators of all menus into one accel table | |
42e69d6b | 1144 | size_t nAccelCount = 0; |
a8cfd0cb | 1145 | size_t i, count = GetMenuCount(); |
222ed1d6 MB |
1146 | wxMenuList::iterator it; |
1147 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
42e69d6b | 1148 | { |
222ed1d6 | 1149 | nAccelCount += (*it)->GetAccelCount(); |
42e69d6b VZ |
1150 | } |
1151 | ||
5df1250b | 1152 | if ( nAccelCount ) |
42e69d6b | 1153 | { |
5df1250b | 1154 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; |
42e69d6b | 1155 | |
5df1250b | 1156 | nAccelCount = 0; |
222ed1d6 | 1157 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) |
5df1250b | 1158 | { |
222ed1d6 | 1159 | nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]); |
5df1250b VZ |
1160 | } |
1161 | ||
1162 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
42e69d6b | 1163 | |
5df1250b VZ |
1164 | delete [] accelEntries; |
1165 | } | |
717a57c2 VZ |
1166 | } |
1167 | ||
1168 | #endif // wxUSE_ACCEL | |
1169 | ||
1170 | void wxMenuBar::Attach(wxFrame *frame) | |
1171 | { | |
1e6feb95 | 1172 | wxMenuBarBase::Attach(frame); |
717a57c2 | 1173 | |
960b193e | 1174 | #if defined(__WXWINCE__) |
a96b4743 JS |
1175 | if (!m_hMenu) |
1176 | this->Create(); | |
a9928e9d | 1177 | #if _WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__) |
960b193e JS |
1178 | |
1179 | // No idea why this was here, but it seems to be obsolete. | |
1180 | // Remove after testing with other WinCE combinations - April 2004 | |
1181 | #if 0 | |
a96b4743 JS |
1182 | if (GetToolBar()) |
1183 | { | |
1184 | HWND hCommandBar = (HWND) GetToolBar()->GetHWND(); | |
1185 | if (!CommandBar_InsertMenubarEx(hCommandBar, NULL, (LPTSTR) m_hMenu, 0)) | |
1186 | { | |
1187 | wxLogLastError(wxT("CommandBar_InsertMenubarEx")); | |
1188 | } | |
1189 | } | |
960b193e | 1190 | #endif |
a96b4743 JS |
1191 | #else |
1192 | if (!m_commandBar) | |
1193 | m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId()); | |
1194 | if (m_commandBar) | |
1195 | { | |
1196 | if (m_hMenu) | |
1197 | { | |
1198 | if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0)) | |
1199 | { | |
1200 | wxLogLastError(wxT("CommandBar_InsertMenubarEx")); | |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | #endif | |
960b193e | 1205 | // PSPC/WFSP |
a96b4743 JS |
1206 | #endif |
1207 | // __WXWINCE__ && _WIN32_WCE >= 400 | |
1208 | ||
717a57c2 VZ |
1209 | #if wxUSE_ACCEL |
1210 | RebuildAccelTable(); | |
d427503c | 1211 | #endif // wxUSE_ACCEL |
42e69d6b VZ |
1212 | } |
1213 | ||
a9928e9d JS |
1214 | #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)) |
1215 | bool wxMenuBar::AddAdornments(long style) | |
1216 | { | |
1217 | if (m_adornmentsAdded || !m_commandBar) | |
1218 | return false; | |
1219 | ||
1220 | if (style & wxCLOSE_BOX) | |
1221 | { | |
1222 | if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0)) | |
1223 | wxLogLastError(wxT("CommandBar_AddAdornments")); | |
1224 | else | |
1225 | return true; | |
1226 | } | |
1227 | return false; | |
1228 | } | |
1229 | #endif | |
1230 | ||
1cf27c63 UM |
1231 | void wxMenuBar::Detach() |
1232 | { | |
1e6feb95 | 1233 | wxMenuBarBase::Detach(); |
2bda0e17 KB |
1234 | } |
1235 | ||
1e6feb95 | 1236 | #endif // wxUSE_MENUS |