+#if wxUSE_ACCEL
+ UpdateAccel(pItem);
+#endif // wxUSE_ACCEL
+
+ UINT flags = 0;
+
+ // if "Break" has just been called, insert a menu break before this item
+ // (and don't forget to reset the flag)
+ if ( m_doBreak ) {
+ flags |= MF_MENUBREAK;
+ m_doBreak = false;
+ }
+
+ if ( pItem->IsSeparator() ) {
+ flags |= MF_SEPARATOR;
+ }
+
+ // id is the numeric id for normal menu items and HMENU for submenus as
+ // required by ::AppendMenu() API
+ UINT id;
+ wxMenu *submenu = pItem->GetSubMenu();
+ if ( submenu != NULL ) {
+ wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
+
+ submenu->SetParent(this);
+
+ id = (UINT)submenu->GetHMenu();
+
+ flags |= MF_POPUP;
+ }
+ else {
+ id = pItem->GetId();
+ }
+
+
+ // prepare to insert the item in the menu
+ wxString itemText = pItem->GetText();
+ LPCTSTR pData = NULL;
+ if ( pos == (size_t)-1 )
+ {
+ // append at the end (note that the item is already appended to
+ // internal data structures)
+ pos = GetMenuItemCount() - 1;
+ }
+
+ // adjust position to account for the title, if any
+ if ( !m_title.empty() )
+ pos += 2; // for the title itself and its separator
+
+ BOOL ok = false;
+
+ // check if we have something more than a simple text item
+#if wxUSE_OWNER_DRAWN
+ if ( pItem->IsOwnerDrawn() )
+ {
+ // is the item owner-drawn just because of the bitmap?
+ if ( pItem->GetBitmap().Ok() &&
+ !pItem->GetTextColour().Ok() &&
+ !pItem->GetBackgroundColour().Ok() &&
+ !pItem->GetFont().Ok() &&
+ !pItem->GetBitmap(true).Ok() )
+ {
+ // try to use InsertMenuItem() as it's guaranteed to look correct
+ // while our owner-drawn code is not
+
+ // first compile-time check
+#ifdef MIIM_BITMAP
+ WinStruct<MENUITEMINFO> mii;
+
+ // now run-time one: MIIM_BITMAP only works under WinME/2000+
+ if ( wxGetWinVersion() >= wxWinVersion_98 )
+ {
+ mii.fMask = MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
+ mii.cch = itemText.length();
+ mii.dwTypeData = wx_const_cast(wxChar *, itemText.c_str());
+
+ if (flags & MF_POPUP)
+ {
+ mii.fMask |= MIIM_SUBMENU;
+ mii.hSubMenu = (HMENU)pItem->GetSubMenu()->GetHMenu();
+ }
+ else
+ {
+ mii.fMask |= MIIM_ID;
+ mii.wID = id;
+ }
+
+ // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
+ // 1. we can't draw it with transparency then (this is not
+ // very important now but would be with themed menu bg)
+ // 2. worse, Windows inverts the bitmap for the selected
+ // item and this looks downright ugly
+ //
+ // so instead draw it ourselves in MSWOnDrawItem()
+ mii.dwItemData = wx_reinterpret_cast(ULONG_PTR, pItem);
+ mii.hbmpItem = HBMMENU_CALLBACK;
+
+ ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
+ if ( !ok )
+ {
+ wxLogLastError(wxT("InsertMenuItem()"));
+ }
+ else // InsertMenuItem() ok
+ {
+ // we need to remove the extra indent which is reserved for
+ // the checkboxes by default as it looks ugly unless check
+ // boxes are used together with bitmaps and this is not the
+ // case in wx API
+ WinStruct<MENUINFO> mi;
+
+ // don't call SetMenuInfo() directly, this would prevent
+ // the app from starting up under Windows 95/NT 4
+ typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
+
+ wxDynamicLibrary dllUser(_T("user32"));
+ wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
+ if ( pfnSetMenuInfo )
+ {
+ mi.fMask = MIM_STYLE;
+ mi.dwStyle = MNS_CHECKORBMP;
+ if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
+ wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
+ }
+
+ // tell the item that it's not really owner-drawn but only
+ // needs to draw its bitmap, the rest is done by Windows
+ pItem->ResetOwnerDrawn();
+ }
+ }
+#endif // MIIM_BITMAP
+ }
+
+ if ( !ok )
+ {
+ // item draws itself, pass pointer to it in data parameter
+ flags |= MF_OWNERDRAW;
+ pData = (LPCTSTR)pItem;
+ }
+ }
+ else
+#endif // wxUSE_OWNER_DRAWN
+ {
+ // item is just a normal string (passed in data parameter)
+ flags |= MF_STRING;
+
+#ifdef __WXWINCE__
+ itemText = wxMenuItem::GetLabelFromText(itemText);
+#endif
+
+ pData = (wxChar*)itemText.c_str();
+ }