]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/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$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_MENUS | |
28 | ||
29 | #include "wx/menu.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/frame.h" | |
33 | #include "wx/utils.h" | |
34 | #include "wx/intl.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/image.h" | |
37 | #endif | |
38 | ||
39 | #if wxUSE_OWNER_DRAWN | |
40 | #include "wx/ownerdrw.h" | |
41 | #endif | |
42 | ||
43 | #include "wx/scopedarray.h" | |
44 | #include "wx/vector.h" | |
45 | ||
46 | #include "wx/msw/private.h" | |
47 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
48 | ||
49 | #ifdef __WXWINCE__ | |
50 | #include <windows.h> | |
51 | #include <windowsx.h> | |
52 | #include <tchar.h> | |
53 | #include <ole2.h> | |
54 | #include <shellapi.h> | |
55 | #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__) | |
56 | #include <aygshell.h> | |
57 | #endif | |
58 | ||
59 | #include "wx/msw/wince/missing.h" | |
60 | ||
61 | #endif | |
62 | ||
63 | // other standard headers | |
64 | #include <string.h> | |
65 | ||
66 | #if wxUSE_OWNER_DRAWN | |
67 | #include "wx/dynlib.h" | |
68 | #endif | |
69 | ||
70 | #ifndef MNS_CHECKORBMP | |
71 | #define MNS_CHECKORBMP 0x04000000 | |
72 | #endif | |
73 | #ifndef MIM_STYLE | |
74 | #define MIM_STYLE 0x00000010 | |
75 | #endif | |
76 | ||
77 | // ---------------------------------------------------------------------------- | |
78 | // global variables | |
79 | // ---------------------------------------------------------------------------- | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // constants | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | // the (popup) menu title has this special id | |
86 | static const int idMenuTitle = wxID_NONE; | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | // private helper classes and functions | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
92 | // Contains the data about the radio items groups in the given menu. | |
93 | class wxMenuRadioItemsData | |
94 | { | |
95 | public: | |
96 | wxMenuRadioItemsData() { } | |
97 | ||
98 | // Default copy ctor, assignment operator and dtor are all ok. | |
99 | ||
100 | // Find the start and end of the group containing the given position or | |
101 | // return false if it's not inside any range. | |
102 | bool GetGroupRange(int pos, int *start, int *end) const | |
103 | { | |
104 | // We use a simple linear search here because there are not that many | |
105 | // items in a menu and hence even fewer radio items ranges anyhow, so | |
106 | // normally there is no need to do anything fancy (like keeping the | |
107 | // array sorted and using binary search). | |
108 | for ( Ranges::const_iterator it = m_ranges.begin(); | |
109 | it != m_ranges.end(); | |
110 | ++it ) | |
111 | { | |
112 | const Range& r = *it; | |
113 | ||
114 | if ( r.start <= pos && pos <= r.end ) | |
115 | { | |
116 | if ( start ) | |
117 | *start = r.start; | |
118 | if ( end ) | |
119 | *end = r.end; | |
120 | ||
121 | return true; | |
122 | } | |
123 | } | |
124 | ||
125 | return false; | |
126 | } | |
127 | ||
128 | // Take into account the new radio item about to be added at the given | |
129 | // position. | |
130 | // | |
131 | // Returns true if this item starts a new radio group, false if it extends | |
132 | // an existing one. | |
133 | bool UpdateOnInsert(int pos) | |
134 | { | |
135 | bool inExistingGroup = false; | |
136 | ||
137 | for ( Ranges::iterator it = m_ranges.begin(); | |
138 | it != m_ranges.end(); | |
139 | ++it ) | |
140 | { | |
141 | Range& r = *it; | |
142 | ||
143 | if ( pos < r.start ) | |
144 | { | |
145 | // Item is inserted before this range, update its indices. | |
146 | r.start++; | |
147 | r.end++; | |
148 | } | |
149 | else if ( pos <= r.end + 1 ) | |
150 | { | |
151 | // Item is inserted in the middle of this range or immediately | |
152 | // after it in which case it extends this range so make it span | |
153 | // one more item in any case. | |
154 | r.end++; | |
155 | ||
156 | inExistingGroup = true; | |
157 | } | |
158 | //else: Item is inserted after this range, nothing to do for it. | |
159 | } | |
160 | ||
161 | if ( inExistingGroup ) | |
162 | return false; | |
163 | ||
164 | // Make a new range for the group this item will belong to. | |
165 | Range r; | |
166 | r.start = pos; | |
167 | r.end = pos; | |
168 | m_ranges.push_back(r); | |
169 | ||
170 | return true; | |
171 | } | |
172 | ||
173 | private: | |
174 | // Contains the inclusive positions of the range start and end. | |
175 | struct Range | |
176 | { | |
177 | int start; | |
178 | int end; | |
179 | }; | |
180 | ||
181 | typedef wxVector<Range> Ranges; | |
182 | Ranges m_ranges; | |
183 | }; | |
184 | ||
185 | namespace | |
186 | { | |
187 | ||
188 | // make the given menu item default | |
189 | void SetDefaultMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu), | |
190 | UINT WXUNUSED_IN_WINCE(id)) | |
191 | { | |
192 | #ifndef __WXWINCE__ | |
193 | WinStruct<MENUITEMINFO> mii; | |
194 | mii.fMask = MIIM_STATE; | |
195 | mii.fState = MFS_DEFAULT; | |
196 | ||
197 | if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) ) | |
198 | { | |
199 | wxLogLastError(wxT("SetMenuItemInfo")); | |
200 | } | |
201 | #endif // !__WXWINCE__ | |
202 | } | |
203 | ||
204 | // make the given menu item owner-drawn | |
205 | void SetOwnerDrawnMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu), | |
206 | UINT WXUNUSED_IN_WINCE(id), | |
207 | ULONG_PTR WXUNUSED_IN_WINCE(data), | |
208 | BOOL WXUNUSED_IN_WINCE(byPositon = FALSE)) | |
209 | { | |
210 | #ifndef __WXWINCE__ | |
211 | WinStruct<MENUITEMINFO> mii; | |
212 | mii.fMask = MIIM_FTYPE | MIIM_DATA; | |
213 | mii.fType = MFT_OWNERDRAW; | |
214 | mii.dwItemData = data; | |
215 | ||
216 | if ( reinterpret_cast<wxMenuItem*>(data)->IsSeparator() ) | |
217 | mii.fType |= MFT_SEPARATOR; | |
218 | ||
219 | if ( !::SetMenuItemInfo(hmenu, id, byPositon, &mii) ) | |
220 | { | |
221 | wxLogLastError(wxT("SetMenuItemInfo")); | |
222 | } | |
223 | #endif // !__WXWINCE__ | |
224 | } | |
225 | ||
226 | #ifdef __WXWINCE__ | |
227 | UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) | |
228 | { | |
229 | WinStruct<MENUITEMINFO> info; | |
230 | info.fMask = MIIM_STATE; | |
231 | // MF_BYCOMMAND is zero so test MF_BYPOSITION | |
232 | if ( !::GetMenuItemInfo(hMenu, id, flags & MF_BYPOSITION ? TRUE : FALSE , & info) ) | |
233 | { | |
234 | wxLogLastError(wxT("GetMenuItemInfo")); | |
235 | } | |
236 | return info.fState; | |
237 | } | |
238 | #endif // __WXWINCE__ | |
239 | ||
240 | inline bool IsGreaterThanStdSize(const wxBitmap& bmp) | |
241 | { | |
242 | return bmp.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK) || | |
243 | bmp.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK); | |
244 | } | |
245 | ||
246 | } // anonymous namespace | |
247 | ||
248 | // ============================================================================ | |
249 | // implementation | |
250 | // ============================================================================ | |
251 | ||
252 | // --------------------------------------------------------------------------- | |
253 | // wxMenu construction, adding and removing menu items | |
254 | // --------------------------------------------------------------------------- | |
255 | ||
256 | // Construct a menu with optional title (then use append) | |
257 | void wxMenu::InitNoCreate() | |
258 | { | |
259 | m_radioData = NULL; | |
260 | m_doBreak = false; | |
261 | ||
262 | #if wxUSE_OWNER_DRAWN | |
263 | m_ownerDrawn = false; | |
264 | m_maxBitmapWidth = 0; | |
265 | m_maxAccelWidth = -1; | |
266 | #endif // wxUSE_OWNER_DRAWN | |
267 | } | |
268 | ||
269 | void wxMenu::Init() | |
270 | { | |
271 | InitNoCreate(); | |
272 | ||
273 | // create the menu | |
274 | m_hMenu = (WXHMENU)CreatePopupMenu(); | |
275 | if ( !m_hMenu ) | |
276 | { | |
277 | wxLogLastError(wxT("CreatePopupMenu")); | |
278 | } | |
279 | ||
280 | // if we have a title, insert it in the beginning of the menu | |
281 | if ( !m_title.empty() ) | |
282 | { | |
283 | const wxString title = m_title; | |
284 | m_title.clear(); // so that SetTitle() knows there was no title before | |
285 | SetTitle(title); | |
286 | } | |
287 | } | |
288 | ||
289 | wxMenu::wxMenu(WXHMENU hMenu) | |
290 | { | |
291 | InitNoCreate(); | |
292 | ||
293 | m_hMenu = hMenu; | |
294 | ||
295 | // Ensure that our internal idea of how many items we have corresponds to | |
296 | // the real number of items in the menu. | |
297 | // | |
298 | // We could also retrieve the real labels of the items here but it doesn't | |
299 | // seem to be worth the trouble. | |
300 | const int numExistingItems = ::GetMenuItemCount(m_hMenu); | |
301 | for ( int n = 0; n < numExistingItems; n++ ) | |
302 | { | |
303 | wxMenuBase::DoAppend(wxMenuItem::New(this, wxID_SEPARATOR)); | |
304 | } | |
305 | } | |
306 | ||
307 | // The wxWindow destructor will take care of deleting the submenus. | |
308 | wxMenu::~wxMenu() | |
309 | { | |
310 | // we should free Windows resources only if Windows doesn't do it for us | |
311 | // which happens if we're attached to a menubar or a submenu of another | |
312 | // menu | |
313 | if ( !IsAttached() && !GetParent() ) | |
314 | { | |
315 | if ( !::DestroyMenu(GetHmenu()) ) | |
316 | { | |
317 | wxLogLastError(wxT("DestroyMenu")); | |
318 | } | |
319 | } | |
320 | ||
321 | #if wxUSE_ACCEL | |
322 | // delete accels | |
323 | WX_CLEAR_ARRAY(m_accels); | |
324 | #endif // wxUSE_ACCEL | |
325 | ||
326 | delete m_radioData; | |
327 | } | |
328 | ||
329 | void wxMenu::Break() | |
330 | { | |
331 | // this will take effect during the next call to Append() | |
332 | m_doBreak = true; | |
333 | } | |
334 | ||
335 | #if wxUSE_ACCEL | |
336 | ||
337 | int wxMenu::FindAccel(int id) const | |
338 | { | |
339 | size_t n, count = m_accels.GetCount(); | |
340 | for ( n = 0; n < count; n++ ) | |
341 | { | |
342 | if ( m_accels[n]->m_command == id ) | |
343 | return n; | |
344 | } | |
345 | ||
346 | return wxNOT_FOUND; | |
347 | } | |
348 | ||
349 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
350 | { | |
351 | if ( item->IsSubMenu() ) | |
352 | { | |
353 | wxMenu *submenu = item->GetSubMenu(); | |
354 | wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst(); | |
355 | while ( node ) | |
356 | { | |
357 | UpdateAccel(node->GetData()); | |
358 | ||
359 | node = node->GetNext(); | |
360 | } | |
361 | } | |
362 | else if ( !item->IsSeparator() ) | |
363 | { | |
364 | // recurse upwards: we should only modify m_accels of the top level | |
365 | // menus, not of the submenus as wxMenuBar doesn't look at them | |
366 | // (alternative and arguable cleaner solution would be to recurse | |
367 | // downwards in GetAccelCount() and CopyAccels()) | |
368 | if ( GetParent() ) | |
369 | { | |
370 | GetParent()->UpdateAccel(item); | |
371 | return; | |
372 | } | |
373 | ||
374 | // find the (new) accel for this item | |
375 | wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetItemLabel()); | |
376 | if ( accel ) | |
377 | accel->m_command = item->GetId(); | |
378 | ||
379 | // find the old one | |
380 | int n = FindAccel(item->GetId()); | |
381 | if ( n == wxNOT_FOUND ) | |
382 | { | |
383 | // no old, add new if any | |
384 | if ( accel ) | |
385 | m_accels.Add(accel); | |
386 | else | |
387 | return; // skipping RebuildAccelTable() below | |
388 | } | |
389 | else | |
390 | { | |
391 | // replace old with new or just remove the old one if no new | |
392 | delete m_accels[n]; | |
393 | if ( accel ) | |
394 | m_accels[n] = accel; | |
395 | else | |
396 | m_accels.RemoveAt(n); | |
397 | } | |
398 | ||
399 | if ( IsAttached() ) | |
400 | { | |
401 | GetMenuBar()->RebuildAccelTable(); | |
402 | } | |
403 | ||
404 | #if wxUSE_OWNER_DRAWN | |
405 | ResetMaxAccelWidth(); | |
406 | #endif | |
407 | } | |
408 | //else: it is a separator, they can't have accels, nothing to do | |
409 | } | |
410 | ||
411 | #endif // wxUSE_ACCEL | |
412 | ||
413 | namespace | |
414 | { | |
415 | ||
416 | // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO | |
417 | HBITMAP GetHBitmapForMenu(wxMenuItem *pItem, bool checked = true) | |
418 | { | |
419 | // Under versions of Windows older than Vista we can't pass HBITMAP | |
420 | // directly as hbmpItem for 2 reasons: | |
421 | // 1. We can't draw it with transparency then (this is not | |
422 | // very important now but would be with themed menu bg) | |
423 | // 2. Worse, Windows inverts the bitmap for the selected | |
424 | // item and this looks downright ugly | |
425 | // | |
426 | // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using | |
427 | // HBMMENU_CALLBACK when inserting it | |
428 | // | |
429 | // However under Vista using HBMMENU_CALLBACK causes the entire menu to be | |
430 | // drawn using the classic theme instead of the current one and it does | |
431 | // handle transparency just fine so do use the real bitmap there | |
432 | #if wxUSE_IMAGE | |
433 | if ( wxGetWinVersion() >= wxWinVersion_Vista ) | |
434 | { | |
435 | #if wxUSE_OWNER_DRAWN | |
436 | wxBitmap bmp = pItem->GetBitmap(checked); | |
437 | if ( bmp.IsOk() ) | |
438 | { | |
439 | // we must use PARGB DIB for the menu bitmaps so ensure that we do | |
440 | wxImage img(bmp.ConvertToImage()); | |
441 | if ( !img.HasAlpha() ) | |
442 | { | |
443 | img.InitAlpha(); | |
444 | pItem->SetBitmap(img, checked); | |
445 | } | |
446 | ||
447 | return GetHbitmapOf(pItem->GetBitmap(checked)); | |
448 | } | |
449 | #endif // wxUSE_OWNER_DRAWN | |
450 | //else: bitmap is not set | |
451 | ||
452 | return NULL; | |
453 | } | |
454 | #endif // wxUSE_IMAGE | |
455 | ||
456 | return HBMMENU_CALLBACK; | |
457 | } | |
458 | ||
459 | } // anonymous namespace | |
460 | ||
461 | bool wxMenu::MSWGetRadioGroupRange(int pos, int *start, int *end) const | |
462 | { | |
463 | return m_radioData && m_radioData->GetGroupRange(pos, start, end); | |
464 | } | |
465 | ||
466 | // append a new item or submenu to the menu | |
467 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
468 | { | |
469 | #if wxUSE_ACCEL | |
470 | UpdateAccel(pItem); | |
471 | #endif // wxUSE_ACCEL | |
472 | ||
473 | // we should support disabling the item even prior to adding it to the menu | |
474 | UINT flags = pItem->IsEnabled() ? MF_ENABLED : MF_GRAYED; | |
475 | ||
476 | // if "Break" has just been called, insert a menu break before this item | |
477 | // (and don't forget to reset the flag) | |
478 | if ( m_doBreak ) { | |
479 | flags |= MF_MENUBREAK; | |
480 | m_doBreak = false; | |
481 | } | |
482 | ||
483 | if ( pItem->IsSeparator() ) { | |
484 | flags |= MF_SEPARATOR; | |
485 | } | |
486 | ||
487 | // id is the numeric id for normal menu items and HMENU for submenus as | |
488 | // required by ::AppendMenu() API | |
489 | UINT_PTR id; | |
490 | wxMenu *submenu = pItem->GetSubMenu(); | |
491 | if ( submenu != NULL ) { | |
492 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); | |
493 | ||
494 | submenu->SetParent(this); | |
495 | ||
496 | id = (UINT_PTR)submenu->GetHMenu(); | |
497 | ||
498 | flags |= MF_POPUP; | |
499 | } | |
500 | else { | |
501 | id = pItem->GetMSWId(); | |
502 | } | |
503 | ||
504 | ||
505 | // prepare to insert the item in the menu | |
506 | wxString itemText = pItem->GetItemLabel(); | |
507 | LPCTSTR pData = NULL; | |
508 | if ( pos == (size_t)-1 ) | |
509 | { | |
510 | // append at the end (note that the item is already appended to | |
511 | // internal data structures) | |
512 | pos = GetMenuItemCount() - 1; | |
513 | } | |
514 | ||
515 | // Update radio groups data if we're inserting a new radio item. | |
516 | // | |
517 | // NB: If we supported inserting non-radio items in the middle of existing | |
518 | // radio groups to break them into two subgroups, we'd need to update | |
519 | // m_radioData in this case too but currently this is not supported. | |
520 | bool checkInitially = false; | |
521 | if ( pItem->GetKind() == wxITEM_RADIO ) | |
522 | { | |
523 | if ( !m_radioData ) | |
524 | m_radioData = new wxMenuRadioItemsData; | |
525 | ||
526 | if ( m_radioData->UpdateOnInsert(pos) ) | |
527 | checkInitially = true; | |
528 | } | |
529 | ||
530 | // adjust position to account for the title of a popup menu, if any | |
531 | if ( !GetMenuBar() && !m_title.empty() ) | |
532 | pos += 2; // for the title itself and its separator | |
533 | ||
534 | BOOL ok = false; | |
535 | ||
536 | #if wxUSE_OWNER_DRAWN | |
537 | // Under older systems mixing owner-drawn and non-owner-drawn items results | |
538 | // in inconsistent margins, so we force this one to be owner-drawn if any | |
539 | // other items already are. | |
540 | if ( m_ownerDrawn ) | |
541 | pItem->SetOwnerDrawn(true); | |
542 | #endif // wxUSE_OWNER_DRAWN | |
543 | ||
544 | // check if we have something more than a simple text item | |
545 | #if wxUSE_OWNER_DRAWN | |
546 | if ( pItem->IsOwnerDrawn() ) | |
547 | { | |
548 | #ifndef __DMC__ | |
549 | ||
550 | if ( !m_ownerDrawn && !pItem->IsSeparator() ) | |
551 | { | |
552 | // MIIM_BITMAP only works under WinME/2000+ so we always use owner | |
553 | // drawn item under the previous versions and we also have to use | |
554 | // them in any case if the item has custom colours or font | |
555 | static const wxWinVersion winver = wxGetWinVersion(); | |
556 | bool mustUseOwnerDrawn = winver < wxWinVersion_98 || | |
557 | pItem->GetTextColour().IsOk() || | |
558 | pItem->GetBackgroundColour().IsOk() || | |
559 | pItem->GetFont().IsOk(); | |
560 | ||
561 | if ( !mustUseOwnerDrawn ) | |
562 | { | |
563 | const wxBitmap& bmpUnchecked = pItem->GetBitmap(false), | |
564 | bmpChecked = pItem->GetBitmap(true); | |
565 | ||
566 | if ( (bmpUnchecked.IsOk() && IsGreaterThanStdSize(bmpUnchecked)) || | |
567 | (bmpChecked.IsOk() && IsGreaterThanStdSize(bmpChecked)) ) | |
568 | { | |
569 | mustUseOwnerDrawn = true; | |
570 | } | |
571 | } | |
572 | ||
573 | // use InsertMenuItem() if possible as it's guaranteed to look | |
574 | // correct while our owner-drawn code is not | |
575 | if ( !mustUseOwnerDrawn ) | |
576 | { | |
577 | WinStruct<MENUITEMINFO> mii; | |
578 | mii.fMask = MIIM_STRING | MIIM_DATA; | |
579 | ||
580 | // don't set hbmpItem for the checkable items as it would | |
581 | // be used for both checked and unchecked state | |
582 | if ( pItem->IsCheckable() ) | |
583 | { | |
584 | mii.fMask |= MIIM_CHECKMARKS; | |
585 | mii.hbmpChecked = GetHBitmapForMenu(pItem, true); | |
586 | mii.hbmpUnchecked = GetHBitmapForMenu(pItem, false); | |
587 | } | |
588 | else if ( pItem->GetBitmap().IsOk() ) | |
589 | { | |
590 | mii.fMask |= MIIM_BITMAP; | |
591 | mii.hbmpItem = GetHBitmapForMenu(pItem); | |
592 | } | |
593 | ||
594 | mii.cch = itemText.length(); | |
595 | mii.dwTypeData = const_cast<wxChar *>(itemText.wx_str()); | |
596 | ||
597 | if ( flags & MF_POPUP ) | |
598 | { | |
599 | mii.fMask |= MIIM_SUBMENU; | |
600 | mii.hSubMenu = GetHmenuOf(pItem->GetSubMenu()); | |
601 | } | |
602 | else | |
603 | { | |
604 | mii.fMask |= MIIM_ID; | |
605 | mii.wID = id; | |
606 | } | |
607 | ||
608 | mii.dwItemData = reinterpret_cast<ULONG_PTR>(pItem); | |
609 | ||
610 | ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii); | |
611 | if ( !ok ) | |
612 | { | |
613 | wxLogLastError(wxT("InsertMenuItem()")); | |
614 | } | |
615 | else // InsertMenuItem() ok | |
616 | { | |
617 | // we need to remove the extra indent which is reserved for | |
618 | // the checkboxes by default as it looks ugly unless check | |
619 | // boxes are used together with bitmaps and this is not the | |
620 | // case in wx API | |
621 | WinStruct<MENUINFO> mi; | |
622 | ||
623 | // don't call SetMenuInfo() directly, this would prevent | |
624 | // the app from starting up under Windows 95/NT 4 | |
625 | typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *); | |
626 | ||
627 | wxDynamicLibrary dllUser(wxT("user32")); | |
628 | wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser); | |
629 | if ( pfnSetMenuInfo ) | |
630 | { | |
631 | mi.fMask = MIM_STYLE; | |
632 | mi.dwStyle = MNS_CHECKORBMP; | |
633 | if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) ) | |
634 | { | |
635 | wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)")); | |
636 | } | |
637 | } | |
638 | ||
639 | // tell the item that it's not really owner-drawn but only | |
640 | // needs to draw its bitmap, the rest is done by Windows | |
641 | pItem->SetOwnerDrawn(false); | |
642 | } | |
643 | } | |
644 | } | |
645 | #endif // __DMC__ | |
646 | ||
647 | if ( !ok ) | |
648 | { | |
649 | // item draws itself, pass pointer to it in data parameter | |
650 | flags |= MF_OWNERDRAW; | |
651 | pData = (LPCTSTR)pItem; | |
652 | ||
653 | bool updateAllMargins = false; | |
654 | ||
655 | // get size of bitmap always return valid value (0 for invalid bitmap), | |
656 | // so we don't needed check if bitmap is valid ;) | |
657 | int uncheckedW = pItem->GetBitmap(false).GetWidth(); | |
658 | int checkedW = pItem->GetBitmap(true).GetWidth(); | |
659 | ||
660 | if ( m_maxBitmapWidth < uncheckedW ) | |
661 | { | |
662 | m_maxBitmapWidth = uncheckedW; | |
663 | updateAllMargins = true; | |
664 | } | |
665 | ||
666 | if ( m_maxBitmapWidth < checkedW ) | |
667 | { | |
668 | m_maxBitmapWidth = checkedW; | |
669 | updateAllMargins = true; | |
670 | } | |
671 | ||
672 | // make other item ownerdrawn and update margin width for equals alignment | |
673 | if ( !m_ownerDrawn || updateAllMargins ) | |
674 | { | |
675 | // we must use position in SetOwnerDrawnMenuItem because | |
676 | // all separators have the same id | |
677 | int pos = 0; | |
678 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
679 | while (node) | |
680 | { | |
681 | wxMenuItem* item = node->GetData(); | |
682 | ||
683 | if ( !item->IsOwnerDrawn()) | |
684 | { | |
685 | item->SetOwnerDrawn(true); | |
686 | SetOwnerDrawnMenuItem(GetHmenu(), pos, | |
687 | reinterpret_cast<ULONG_PTR>(item), TRUE); | |
688 | } | |
689 | ||
690 | item->SetMarginWidth(m_maxBitmapWidth); | |
691 | ||
692 | node = node->GetNext(); | |
693 | pos++; | |
694 | } | |
695 | ||
696 | // set menu as ownerdrawn | |
697 | m_ownerDrawn = true; | |
698 | ||
699 | ResetMaxAccelWidth(); | |
700 | } | |
701 | // only update our margin for equals alignment to other item | |
702 | else if ( !updateAllMargins ) | |
703 | { | |
704 | pItem->SetMarginWidth(m_maxBitmapWidth); | |
705 | } | |
706 | } | |
707 | } | |
708 | else | |
709 | #endif // wxUSE_OWNER_DRAWN | |
710 | { | |
711 | // item is just a normal string (passed in data parameter) | |
712 | flags |= MF_STRING; | |
713 | ||
714 | #ifdef __WXWINCE__ | |
715 | itemText = wxMenuItem::GetLabelText(itemText); | |
716 | #endif | |
717 | ||
718 | pData = (wxChar*)itemText.wx_str(); | |
719 | } | |
720 | ||
721 | // item might have already been inserted by InsertMenuItem() above | |
722 | if ( !ok ) | |
723 | { | |
724 | if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) ) | |
725 | { | |
726 | wxLogLastError(wxT("InsertMenu[Item]()")); | |
727 | ||
728 | return false; | |
729 | } | |
730 | } | |
731 | ||
732 | ||
733 | // Check the item if it should be initially checked. | |
734 | if ( checkInitially ) | |
735 | pItem->Check(true); | |
736 | ||
737 | // if we just appended the title, highlight it | |
738 | if ( id == (UINT_PTR)idMenuTitle ) | |
739 | { | |
740 | // visually select the menu title | |
741 | SetDefaultMenuItem(GetHmenu(), id); | |
742 | } | |
743 | ||
744 | // if we're already attached to the menubar, we must update it | |
745 | if ( IsAttached() && GetMenuBar()->IsAttached() ) | |
746 | { | |
747 | GetMenuBar()->Refresh(); | |
748 | } | |
749 | ||
750 | return true; | |
751 | } | |
752 | ||
753 | wxMenuItem* wxMenu::DoAppend(wxMenuItem *item) | |
754 | { | |
755 | return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item) ? item : NULL; | |
756 | } | |
757 | ||
758 | wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
759 | { | |
760 | if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos)) | |
761 | return item; | |
762 | else | |
763 | return NULL; | |
764 | } | |
765 | ||
766 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
767 | { | |
768 | // we need to find the item's position in the child list | |
769 | size_t pos; | |
770 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
771 | for ( pos = 0; node; pos++ ) | |
772 | { | |
773 | if ( node->GetData() == item ) | |
774 | break; | |
775 | ||
776 | node = node->GetNext(); | |
777 | } | |
778 | ||
779 | // DoRemove() (unlike Remove) can only be called for an existing item! | |
780 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
781 | ||
782 | #if wxUSE_ACCEL | |
783 | // remove the corresponding accel from the accel table | |
784 | int n = FindAccel(item->GetId()); | |
785 | if ( n != wxNOT_FOUND ) | |
786 | { | |
787 | delete m_accels[n]; | |
788 | ||
789 | m_accels.RemoveAt(n); | |
790 | ||
791 | #if wxUSE_OWNER_DRAWN | |
792 | ResetMaxAccelWidth(); | |
793 | #endif | |
794 | } | |
795 | //else: this item doesn't have an accel, nothing to do | |
796 | #endif // wxUSE_ACCEL | |
797 | ||
798 | // remove the item from the menu | |
799 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
800 | { | |
801 | wxLogLastError(wxT("RemoveMenu")); | |
802 | } | |
803 | ||
804 | if ( IsAttached() && GetMenuBar()->IsAttached() ) | |
805 | { | |
806 | // otherwise, the change won't be visible | |
807 | GetMenuBar()->Refresh(); | |
808 | } | |
809 | ||
810 | // and from internal data structures | |
811 | return wxMenuBase::DoRemove(item); | |
812 | } | |
813 | ||
814 | // --------------------------------------------------------------------------- | |
815 | // accelerator helpers | |
816 | // --------------------------------------------------------------------------- | |
817 | ||
818 | #if wxUSE_ACCEL | |
819 | ||
820 | // create the wxAcceleratorEntries for our accels and put them into the provided | |
821 | // array - return the number of accels we have | |
822 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
823 | { | |
824 | size_t count = GetAccelCount(); | |
825 | for ( size_t n = 0; n < count; n++ ) | |
826 | { | |
827 | *accels++ = *m_accels[n]; | |
828 | } | |
829 | ||
830 | return count; | |
831 | } | |
832 | ||
833 | wxAcceleratorTable *wxMenu::CreateAccelTable() const | |
834 | { | |
835 | const size_t count = m_accels.size(); | |
836 | wxScopedArray<wxAcceleratorEntry> accels(new wxAcceleratorEntry[count]); | |
837 | CopyAccels(accels.get()); | |
838 | ||
839 | return new wxAcceleratorTable(count, accels.get()); | |
840 | } | |
841 | ||
842 | #endif // wxUSE_ACCEL | |
843 | ||
844 | // --------------------------------------------------------------------------- | |
845 | // ownerdrawn helpers | |
846 | // --------------------------------------------------------------------------- | |
847 | ||
848 | #if wxUSE_OWNER_DRAWN | |
849 | ||
850 | void wxMenu::CalculateMaxAccelWidth() | |
851 | { | |
852 | wxASSERT_MSG( m_maxAccelWidth == -1, wxT("it's really needed?") ); | |
853 | ||
854 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); | |
855 | while (node) | |
856 | { | |
857 | wxMenuItem* item = node->GetData(); | |
858 | ||
859 | if ( item->IsOwnerDrawn() ) | |
860 | { | |
861 | int width = item->MeasureAccelWidth(); | |
862 | if (width > m_maxAccelWidth ) | |
863 | m_maxAccelWidth = width; | |
864 | } | |
865 | ||
866 | node = node->GetNext(); | |
867 | } | |
868 | } | |
869 | ||
870 | #endif // wxUSE_OWNER_DRAWN | |
871 | ||
872 | // --------------------------------------------------------------------------- | |
873 | // set wxMenu title | |
874 | // --------------------------------------------------------------------------- | |
875 | ||
876 | void wxMenu::SetTitle(const wxString& label) | |
877 | { | |
878 | bool hasNoTitle = m_title.empty(); | |
879 | m_title = label; | |
880 | ||
881 | HMENU hMenu = GetHmenu(); | |
882 | ||
883 | if ( hasNoTitle ) | |
884 | { | |
885 | if ( !label.empty() ) | |
886 | { | |
887 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
888 | (UINT_PTR)idMenuTitle, m_title.wx_str()) || | |
889 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
890 | { | |
891 | wxLogLastError(wxT("InsertMenu")); | |
892 | } | |
893 | } | |
894 | } | |
895 | else | |
896 | { | |
897 | if ( label.empty() ) | |
898 | { | |
899 | // remove the title and the separator after it | |
900 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
901 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
902 | { | |
903 | wxLogLastError(wxT("RemoveMenu")); | |
904 | } | |
905 | } | |
906 | else | |
907 | { | |
908 | // modify the title | |
909 | #ifdef __WXWINCE__ | |
910 | WinStruct<MENUITEMINFO> info; | |
911 | info.fMask = MIIM_TYPE; | |
912 | info.fType = MFT_STRING; | |
913 | info.cch = m_title.length(); | |
914 | info.dwTypeData = const_cast<wxChar *>(m_title.wx_str()); | |
915 | if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) ) | |
916 | { | |
917 | wxLogLastError(wxT("SetMenuItemInfo")); | |
918 | } | |
919 | #else | |
920 | if ( !ModifyMenu(hMenu, 0u, | |
921 | MF_BYPOSITION | MF_STRING, | |
922 | (UINT_PTR)idMenuTitle, m_title.wx_str()) ) | |
923 | { | |
924 | wxLogLastError(wxT("ModifyMenu")); | |
925 | } | |
926 | #endif | |
927 | } | |
928 | } | |
929 | ||
930 | #ifdef __WIN32__ | |
931 | // put the title string in bold face | |
932 | if ( !m_title.empty() ) | |
933 | { | |
934 | SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle); | |
935 | } | |
936 | #endif // Win32 | |
937 | } | |
938 | ||
939 | // --------------------------------------------------------------------------- | |
940 | // event processing | |
941 | // --------------------------------------------------------------------------- | |
942 | ||
943 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_) | |
944 | { | |
945 | const int id = (signed short)id_; | |
946 | ||
947 | // ignore commands from the menu title | |
948 | if ( id != idMenuTitle ) | |
949 | { | |
950 | // Default value for uncheckable items. | |
951 | int checked = -1; | |
952 | ||
953 | // update the check item when it's clicked | |
954 | wxMenuItem * const item = FindItem(id); | |
955 | if ( item && item->IsCheckable() ) | |
956 | { | |
957 | item->Toggle(); | |
958 | ||
959 | // Get the status of the menu item: note that it has been just changed | |
960 | // by Toggle() above so here we already get the new state of the item. | |
961 | // | |
962 | // Also notice that we must pass unsigned id_ and not sign-extended id | |
963 | // to ::GetMenuState() as this is what it expects. | |
964 | UINT menuState = ::GetMenuState(GetHmenu(), id_, MF_BYCOMMAND); | |
965 | checked = (menuState & MF_CHECKED) != 0; | |
966 | } | |
967 | ||
968 | SendEvent(id, checked); | |
969 | } | |
970 | ||
971 | return true; | |
972 | } | |
973 | ||
974 | // get the menu with given handle (recursively) | |
975 | #if wxUSE_OWNER_DRAWN | |
976 | wxMenu* wxMenu::MSWGetMenu(WXHMENU hMenu) | |
977 | { | |
978 | // check self | |
979 | if ( GetHMenu() == hMenu ) | |
980 | return this; | |
981 | ||
982 | // recursively query submenus | |
983 | for ( size_t n = 0 ; n < GetMenuItemCount(); ++n ) | |
984 | { | |
985 | wxMenuItem* item = FindItemByPosition(n); | |
986 | wxMenu* submenu = item->GetSubMenu(); | |
987 | if ( submenu ) | |
988 | { | |
989 | submenu = submenu->MSWGetMenu(hMenu); | |
990 | if (submenu) | |
991 | return submenu; | |
992 | } | |
993 | } | |
994 | ||
995 | // unknown hMenu | |
996 | return NULL; | |
997 | } | |
998 | #endif // wxUSE_OWNER_DRAWN | |
999 | ||
1000 | // --------------------------------------------------------------------------- | |
1001 | // Menu Bar | |
1002 | // --------------------------------------------------------------------------- | |
1003 | ||
1004 | void wxMenuBar::Init() | |
1005 | { | |
1006 | m_eventHandler = this; | |
1007 | m_hMenu = 0; | |
1008 | #if wxUSE_TOOLBAR && defined(__WXWINCE__) | |
1009 | m_toolBar = NULL; | |
1010 | #endif | |
1011 | // Not using a combined wxToolBar/wxMenuBar? then use | |
1012 | // a commandbar in WinCE .NET just to implement the | |
1013 | // menubar. | |
1014 | #if defined(WINCE_WITH_COMMANDBAR) | |
1015 | m_commandBar = NULL; | |
1016 | m_adornmentsAdded = false; | |
1017 | #endif | |
1018 | } | |
1019 | ||
1020 | wxMenuBar::wxMenuBar() | |
1021 | { | |
1022 | Init(); | |
1023 | } | |
1024 | ||
1025 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
1026 | { | |
1027 | Init(); | |
1028 | } | |
1029 | ||
1030 | wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style)) | |
1031 | { | |
1032 | Init(); | |
1033 | ||
1034 | for ( size_t i = 0; i < count; i++ ) | |
1035 | { | |
1036 | // We just want to store the menu title in the menu itself, not to | |
1037 | // show it as a dummy item in the menu itself as we do with the popup | |
1038 | // menu titles in overridden wxMenu::SetTitle(). | |
1039 | menus[i]->wxMenuBase::SetTitle(titles[i]); | |
1040 | m_menus.Append(menus[i]); | |
1041 | ||
1042 | menus[i]->Attach(this); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | wxMenuBar::~wxMenuBar() | |
1047 | { | |
1048 | // In Windows CE (not .NET), the menubar is always associated | |
1049 | // with a toolbar, which destroys the menu implicitly. | |
1050 | #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__) | |
1051 | if (GetToolBar()) | |
1052 | { | |
1053 | wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar); | |
1054 | if (toolMenuBar) | |
1055 | toolMenuBar->SetMenuBar(NULL); | |
1056 | } | |
1057 | #else | |
1058 | // we should free Windows resources only if Windows doesn't do it for us | |
1059 | // which happens if we're attached to a frame | |
1060 | if (m_hMenu && !IsAttached()) | |
1061 | { | |
1062 | #if defined(WINCE_WITH_COMMANDBAR) | |
1063 | ::DestroyWindow((HWND) m_commandBar); | |
1064 | m_commandBar = (WXHWND) NULL; | |
1065 | #else | |
1066 | ::DestroyMenu((HMENU)m_hMenu); | |
1067 | #endif | |
1068 | m_hMenu = (WXHMENU)NULL; | |
1069 | } | |
1070 | #endif | |
1071 | } | |
1072 | ||
1073 | // --------------------------------------------------------------------------- | |
1074 | // wxMenuBar helpers | |
1075 | // --------------------------------------------------------------------------- | |
1076 | ||
1077 | void wxMenuBar::Refresh() | |
1078 | { | |
1079 | if ( IsFrozen() ) | |
1080 | return; | |
1081 | ||
1082 | wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") ); | |
1083 | ||
1084 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1085 | if (GetToolBar()) | |
1086 | { | |
1087 | CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0); | |
1088 | } | |
1089 | #elif defined(WINCE_WITH_COMMANDBAR) | |
1090 | if (m_commandBar) | |
1091 | DrawMenuBar((HWND) m_commandBar); | |
1092 | #else | |
1093 | DrawMenuBar(GetHwndOf(GetFrame())); | |
1094 | #endif | |
1095 | } | |
1096 | ||
1097 | WXHMENU wxMenuBar::Create() | |
1098 | { | |
1099 | // Note: this doesn't work at all on Smartphone, | |
1100 | // since you have to use resources. | |
1101 | // We'll have to find another way to add a menu | |
1102 | // by changing/adding menu items to an existing menu. | |
1103 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1104 | if ( m_hMenu != 0 ) | |
1105 | return m_hMenu; | |
1106 | ||
1107 | wxToolMenuBar * const bar = static_cast<wxToolMenuBar *>(GetToolBar()); | |
1108 | if ( !bar ) | |
1109 | return NULL; | |
1110 | ||
1111 | HWND hCommandBar = GetHwndOf(bar); | |
1112 | ||
1113 | // notify comctl32.dll about the version of the headers we use before using | |
1114 | // any other TB_XXX messages | |
1115 | SendMessage(hCommandBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); | |
1116 | ||
1117 | TBBUTTON tbButton; | |
1118 | wxZeroMemory(tbButton); | |
1119 | tbButton.iBitmap = I_IMAGENONE; | |
1120 | tbButton.fsState = TBSTATE_ENABLED; | |
1121 | tbButton.fsStyle = TBSTYLE_DROPDOWN | | |
1122 | TBSTYLE_NO_DROPDOWN_ARROW | | |
1123 | TBSTYLE_AUTOSIZE; | |
1124 | ||
1125 | for ( unsigned i = 0; i < GetMenuCount(); i++ ) | |
1126 | { | |
1127 | HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu(); | |
1128 | tbButton.dwData = (DWORD)hPopupMenu; | |
1129 | wxString label = wxStripMenuCodes(GetMenuLabel(i)); | |
1130 | tbButton.iString = (int) label.wx_str(); | |
1131 | ||
1132 | tbButton.idCommand = NewControlId(); | |
1133 | if ( !::SendMessage(hCommandBar, TB_INSERTBUTTON, i, (LPARAM)&tbButton) ) | |
1134 | { | |
1135 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | m_hMenu = bar->GetHMenu(); | |
1140 | return m_hMenu; | |
1141 | #else // !__WXWINCE__ | |
1142 | if ( m_hMenu != 0 ) | |
1143 | return m_hMenu; | |
1144 | ||
1145 | m_hMenu = (WXHMENU)::CreateMenu(); | |
1146 | ||
1147 | if ( !m_hMenu ) | |
1148 | { | |
1149 | wxLogLastError(wxT("CreateMenu")); | |
1150 | } | |
1151 | else | |
1152 | { | |
1153 | for ( wxMenuList::iterator it = m_menus.begin(); | |
1154 | it != m_menus.end(); | |
1155 | ++it ) | |
1156 | { | |
1157 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
1158 | (UINT_PTR)(*it)->GetHMenu(), | |
1159 | (*it)->GetTitle().wx_str()) ) | |
1160 | { | |
1161 | wxLogLastError(wxT("AppendMenu")); | |
1162 | } | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | return m_hMenu; | |
1167 | #endif // __WXWINCE__/!__WXWINCE__ | |
1168 | } | |
1169 | ||
1170 | int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos) | |
1171 | { | |
1172 | wxASSERT(menu); | |
1173 | wxASSERT(menu->GetHMenu()); | |
1174 | wxASSERT(m_hMenu); | |
1175 | ||
1176 | #if defined(__WXWINCE__) | |
1177 | int totalMSWItems = GetMenuCount(); | |
1178 | #else | |
1179 | int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu); | |
1180 | #endif | |
1181 | ||
1182 | int i; // For old C++ compatibility | |
1183 | for(i=wxpos; i<totalMSWItems; i++) | |
1184 | { | |
1185 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
1186 | return i; | |
1187 | } | |
1188 | for(i=0; i<wxpos; i++) | |
1189 | { | |
1190 | if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu()) | |
1191 | return i; | |
1192 | } | |
1193 | wxFAIL; | |
1194 | return -1; | |
1195 | } | |
1196 | ||
1197 | // --------------------------------------------------------------------------- | |
1198 | // wxMenuBar functions to work with the top level submenus | |
1199 | // --------------------------------------------------------------------------- | |
1200 | ||
1201 | // NB: we don't support owner drawn top level items for now, if we do these | |
1202 | // functions would have to be changed to use wxMenuItem as well | |
1203 | ||
1204 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
1205 | { | |
1206 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); | |
1207 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
1208 | ||
1209 | int flag = enable ? MF_ENABLED : MF_GRAYED; | |
1210 | ||
1211 | EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag); | |
1212 | ||
1213 | Refresh(); | |
1214 | } | |
1215 | ||
1216 | bool wxMenuBar::IsEnabledTop(size_t pos) const | |
1217 | { | |
1218 | wxCHECK_MSG( pos < GetMenuCount(), false, wxS("invalid menu index") ); | |
1219 | WinStruct<MENUITEMINFO> mii; | |
1220 | mii.fMask = MIIM_STATE; | |
1221 | if ( !::GetMenuItemInfo(GetHmenu(), pos, TRUE, &mii) ) | |
1222 | { | |
1223 | wxLogLastError(wxS("GetMenuItemInfo(menubar)")); | |
1224 | } | |
1225 | ||
1226 | return !(mii.fState & MFS_GRAYED); | |
1227 | } | |
1228 | ||
1229 | void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label) | |
1230 | { | |
1231 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
1232 | ||
1233 | m_menus[pos]->wxMenuBase::SetTitle(label); | |
1234 | ||
1235 | if ( !IsAttached() ) | |
1236 | { | |
1237 | return; | |
1238 | } | |
1239 | //else: have to modify the existing menu | |
1240 | ||
1241 | int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos); | |
1242 | ||
1243 | UINT_PTR id; | |
1244 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION); | |
1245 | if ( flagsOld == 0xFFFFFFFF ) | |
1246 | { | |
1247 | wxLogLastError(wxT("GetMenuState")); | |
1248 | ||
1249 | return; | |
1250 | } | |
1251 | ||
1252 | if ( flagsOld & MF_POPUP ) | |
1253 | { | |
1254 | // HIBYTE contains the number of items in the submenu in this case | |
1255 | flagsOld &= 0xff; | |
1256 | id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos); | |
1257 | } | |
1258 | else | |
1259 | { | |
1260 | id = pos; | |
1261 | } | |
1262 | ||
1263 | #ifdef __WXWINCE__ | |
1264 | WinStruct<MENUITEMINFO> info; | |
1265 | info.fMask = MIIM_TYPE; | |
1266 | info.fType = MFT_STRING; | |
1267 | info.cch = label.length(); | |
1268 | info.dwTypeData = const_cast<wxChar *>(label.wx_str()); | |
1269 | if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) ) | |
1270 | { | |
1271 | wxLogLastError(wxT("SetMenuItemInfo")); | |
1272 | } | |
1273 | ||
1274 | #else | |
1275 | if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld, | |
1276 | id, label.wx_str()) == (int)0xFFFFFFFF ) | |
1277 | { | |
1278 | wxLogLastError(wxT("ModifyMenu")); | |
1279 | } | |
1280 | #endif | |
1281 | ||
1282 | Refresh(); | |
1283 | } | |
1284 | ||
1285 | wxString wxMenuBar::GetMenuLabel(size_t pos) const | |
1286 | { | |
1287 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, | |
1288 | wxT("invalid menu index in wxMenuBar::GetMenuLabel") ); | |
1289 | ||
1290 | return m_menus[pos]->GetTitle(); | |
1291 | } | |
1292 | ||
1293 | // --------------------------------------------------------------------------- | |
1294 | // wxMenuBar construction | |
1295 | // --------------------------------------------------------------------------- | |
1296 | ||
1297 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
1298 | { | |
1299 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); | |
1300 | if ( !menuOld ) | |
1301 | return NULL; | |
1302 | ||
1303 | menu->wxMenuBase::SetTitle(title); | |
1304 | ||
1305 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1306 | if (IsAttached()) | |
1307 | #else | |
1308 | if (GetHmenu()) | |
1309 | #endif | |
1310 | { | |
1311 | int mswpos = MSWPositionForWxMenu(menuOld,pos); | |
1312 | ||
1313 | // can't use ModifyMenu() because it deletes the submenu it replaces | |
1314 | if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) ) | |
1315 | { | |
1316 | wxLogLastError(wxT("RemoveMenu")); | |
1317 | } | |
1318 | ||
1319 | if ( !::InsertMenu(GetHmenu(), (UINT)mswpos, | |
1320 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
1321 | (UINT_PTR)GetHmenuOf(menu), title.wx_str()) ) | |
1322 | { | |
1323 | wxLogLastError(wxT("InsertMenu")); | |
1324 | } | |
1325 | ||
1326 | #if wxUSE_ACCEL | |
1327 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
1328 | { | |
1329 | // need to rebuild accell table | |
1330 | RebuildAccelTable(); | |
1331 | } | |
1332 | #endif // wxUSE_ACCEL | |
1333 | ||
1334 | if (IsAttached()) | |
1335 | Refresh(); | |
1336 | } | |
1337 | ||
1338 | return menuOld; | |
1339 | } | |
1340 | ||
1341 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
1342 | { | |
1343 | // Find out which MSW item before which we'll be inserting before | |
1344 | // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu. | |
1345 | // If IsAttached() is false this won't be used anyway | |
1346 | bool isAttached = | |
1347 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1348 | IsAttached(); | |
1349 | #else | |
1350 | (GetHmenu() != 0); | |
1351 | #endif | |
1352 | ||
1353 | int mswpos = (!isAttached || (pos == m_menus.GetCount())) | |
1354 | ? -1 // append the menu | |
1355 | : MSWPositionForWxMenu(GetMenu(pos),pos); | |
1356 | ||
1357 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
1358 | return false; | |
1359 | ||
1360 | menu->wxMenuBase::SetTitle(title); | |
1361 | ||
1362 | if ( isAttached ) | |
1363 | { | |
1364 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1365 | if (!GetToolBar()) | |
1366 | return false; | |
1367 | TBBUTTON tbButton; | |
1368 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1369 | tbButton.iBitmap = I_IMAGENONE; | |
1370 | tbButton.fsState = TBSTATE_ENABLED; | |
1371 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1372 | ||
1373 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1374 | tbButton.dwData = (DWORD)hPopupMenu; | |
1375 | wxString label = wxStripMenuCodes(title); | |
1376 | tbButton.iString = (int) label.wx_str(); | |
1377 | ||
1378 | tbButton.idCommand = NewControlId(); | |
1379 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1380 | { | |
1381 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1382 | return false; | |
1383 | } | |
1384 | wxUnusedVar(mswpos); | |
1385 | #else | |
1386 | if ( !::InsertMenu(GetHmenu(), mswpos, | |
1387 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
1388 | (UINT_PTR)GetHmenuOf(menu), title.wx_str()) ) | |
1389 | { | |
1390 | wxLogLastError(wxT("InsertMenu")); | |
1391 | } | |
1392 | #endif | |
1393 | #if wxUSE_ACCEL | |
1394 | if ( menu->HasAccels() ) | |
1395 | { | |
1396 | // need to rebuild accell table | |
1397 | RebuildAccelTable(); | |
1398 | } | |
1399 | #endif // wxUSE_ACCEL | |
1400 | ||
1401 | if (IsAttached()) | |
1402 | Refresh(); | |
1403 | } | |
1404 | ||
1405 | return true; | |
1406 | } | |
1407 | ||
1408 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
1409 | { | |
1410 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
1411 | wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") ); | |
1412 | ||
1413 | if ( !wxMenuBarBase::Append(menu, title) ) | |
1414 | return false; | |
1415 | ||
1416 | menu->wxMenuBase::SetTitle(title); | |
1417 | ||
1418 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1419 | if (IsAttached()) | |
1420 | #else | |
1421 | if (GetHmenu()) | |
1422 | #endif | |
1423 | { | |
1424 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1425 | if (!GetToolBar()) | |
1426 | return false; | |
1427 | TBBUTTON tbButton; | |
1428 | memset(&tbButton, 0, sizeof(TBBUTTON)); | |
1429 | tbButton.iBitmap = I_IMAGENONE; | |
1430 | tbButton.fsState = TBSTATE_ENABLED; | |
1431 | tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE; | |
1432 | ||
1433 | size_t pos = GetMenuCount(); | |
1434 | HMENU hPopupMenu = (HMENU) menu->GetHMenu() ; | |
1435 | tbButton.dwData = (DWORD)hPopupMenu; | |
1436 | wxString label = wxStripMenuCodes(title); | |
1437 | tbButton.iString = (int) label.wx_str(); | |
1438 | ||
1439 | tbButton.idCommand = NewControlId(); | |
1440 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton)) | |
1441 | { | |
1442 | wxLogLastError(wxT("TB_INSERTBUTTON")); | |
1443 | return false; | |
1444 | } | |
1445 | #else | |
1446 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, | |
1447 | (UINT_PTR)submenu, title.wx_str()) ) | |
1448 | { | |
1449 | wxLogLastError(wxT("AppendMenu")); | |
1450 | } | |
1451 | #endif | |
1452 | ||
1453 | #if wxUSE_ACCEL | |
1454 | if ( menu->HasAccels() ) | |
1455 | { | |
1456 | // need to rebuild accelerator table | |
1457 | RebuildAccelTable(); | |
1458 | } | |
1459 | #endif // wxUSE_ACCEL | |
1460 | ||
1461 | if (IsAttached()) | |
1462 | Refresh(); | |
1463 | } | |
1464 | ||
1465 | return true; | |
1466 | } | |
1467 | ||
1468 | wxMenu *wxMenuBar::Remove(size_t pos) | |
1469 | { | |
1470 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
1471 | if ( !menu ) | |
1472 | return NULL; | |
1473 | ||
1474 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1475 | if (IsAttached()) | |
1476 | #else | |
1477 | if (GetHmenu()) | |
1478 | #endif | |
1479 | { | |
1480 | #if defined(WINCE_WITHOUT_COMMANDBAR) | |
1481 | if (GetToolBar()) | |
1482 | { | |
1483 | if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0)) | |
1484 | { | |
1485 | wxLogLastError(wxT("TB_DELETEBUTTON")); | |
1486 | } | |
1487 | } | |
1488 | #else | |
1489 | if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) ) | |
1490 | { | |
1491 | wxLogLastError(wxT("RemoveMenu")); | |
1492 | } | |
1493 | #endif | |
1494 | ||
1495 | #if wxUSE_ACCEL | |
1496 | if ( menu->HasAccels() ) | |
1497 | { | |
1498 | // need to rebuild accell table | |
1499 | RebuildAccelTable(); | |
1500 | } | |
1501 | #endif // wxUSE_ACCEL | |
1502 | ||
1503 | if (IsAttached()) | |
1504 | Refresh(); | |
1505 | } | |
1506 | ||
1507 | return menu; | |
1508 | } | |
1509 | ||
1510 | #if wxUSE_ACCEL | |
1511 | ||
1512 | void wxMenuBar::RebuildAccelTable() | |
1513 | { | |
1514 | // merge the accelerators of all menus into one accel table | |
1515 | size_t nAccelCount = 0; | |
1516 | size_t i, count = GetMenuCount(); | |
1517 | wxMenuList::iterator it; | |
1518 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
1519 | { | |
1520 | nAccelCount += (*it)->GetAccelCount(); | |
1521 | } | |
1522 | ||
1523 | if ( nAccelCount ) | |
1524 | { | |
1525 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; | |
1526 | ||
1527 | nAccelCount = 0; | |
1528 | for ( i = 0, it = m_menus.begin(); i < count; i++, it++ ) | |
1529 | { | |
1530 | nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]); | |
1531 | } | |
1532 | ||
1533 | SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries)); | |
1534 | ||
1535 | delete [] accelEntries; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | #endif // wxUSE_ACCEL | |
1540 | ||
1541 | void wxMenuBar::Attach(wxFrame *frame) | |
1542 | { | |
1543 | wxMenuBarBase::Attach(frame); | |
1544 | ||
1545 | #if defined(WINCE_WITH_COMMANDBAR) | |
1546 | if (!m_hMenu) | |
1547 | this->Create(); | |
1548 | if (!m_commandBar) | |
1549 | m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId()); | |
1550 | if (m_commandBar) | |
1551 | { | |
1552 | if (m_hMenu) | |
1553 | { | |
1554 | if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0)) | |
1555 | { | |
1556 | wxLogLastError(wxT("CommandBar_InsertMenubarEx")); | |
1557 | } | |
1558 | } | |
1559 | } | |
1560 | #endif | |
1561 | ||
1562 | #if wxUSE_ACCEL | |
1563 | RebuildAccelTable(); | |
1564 | #endif // wxUSE_ACCEL | |
1565 | } | |
1566 | ||
1567 | #if defined(WINCE_WITH_COMMANDBAR) | |
1568 | bool wxMenuBar::AddAdornments(long style) | |
1569 | { | |
1570 | if (m_adornmentsAdded || !m_commandBar) | |
1571 | return false; | |
1572 | ||
1573 | if (style & wxCLOSE_BOX) | |
1574 | { | |
1575 | if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0)) | |
1576 | { | |
1577 | wxLogLastError(wxT("CommandBar_AddAdornments")); | |
1578 | } | |
1579 | else | |
1580 | { | |
1581 | return true; | |
1582 | } | |
1583 | } | |
1584 | return false; | |
1585 | } | |
1586 | #endif | |
1587 | ||
1588 | void wxMenuBar::Detach() | |
1589 | { | |
1590 | wxMenuBarBase::Detach(); | |
1591 | } | |
1592 | ||
1593 | // get the menu with given handle (recursively) | |
1594 | wxMenu* wxMenuBar::MSWGetMenu(WXHMENU hMenu) | |
1595 | { | |
1596 | wxCHECK_MSG( GetHMenu() != hMenu, NULL, | |
1597 | wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") ); | |
1598 | ||
1599 | #if wxUSE_OWNER_DRAWN | |
1600 | // query all menus | |
1601 | for ( size_t n = 0 ; n < GetMenuCount(); ++n ) | |
1602 | { | |
1603 | wxMenu* menu = GetMenu(n)->MSWGetMenu(hMenu); | |
1604 | if ( menu ) | |
1605 | return menu; | |
1606 | } | |
1607 | #endif | |
1608 | ||
1609 | // unknown hMenu | |
1610 | return NULL; | |
1611 | } | |
1612 | ||
1613 | #endif // wxUSE_MENUS |