// This Really just Draws the menu bar items since it assumes 
//  that the background has already been drawn using DoDrawFrame
void cef_dark_window::DoDrawMenuBar(HDC hdc)
{
    HMENU menu = GetMenu();
    int items = ::GetMenuItemCount(menu);

    RECT menuBarRect;
    ComputeMenuBarRect(menuBarRect);

    for (int i = 0; i < items; i++) {
        // Determine the menu item state and ID
        MENUITEMINFO mmi = {0};
        mmi.cbSize = sizeof (mmi);
        mmi.fMask = MIIM_STATE|MIIM_ID;
        ::GetMenuItemInfo (menu, i, TRUE, &mmi);
        
        // Drawitem only works on ID
        MEASUREITEMSTRUCT mis = {0};
        mis.CtlType = ODT_MENU;
        mis.itemID = mmi.wID;

        RECT itemRect;
        ::SetRectEmpty(&itemRect);

        if (::GetMenuItemRect(mWnd, menu, (UINT)i, &itemRect)) {
            ScreenToNonClient(&itemRect);
            
            // Check to make sure it's actually in the 
            //  the correct location (fixes aero drawing issue)
            POINT pt = {itemRect.left, itemRect.top};
            if (!CanUseAeroGlass() || ::PtInRect(&menuBarRect, pt)) {
            
                // Draw the menu item
                DRAWITEMSTRUCT dis = {0};
                dis.CtlType = ODT_MENU;
                dis.itemID = mmi.wID;
                dis.hwndItem = (HWND)menu;
                dis.itemAction = ODA_DRAWENTIRE;
                dis.hDC = hdc;
                ::CopyRect(&dis.rcItem, &itemRect);

                if (mmi.fState & MFS_HILITE) {
                    dis.itemState |= ODS_SELECTED;
                } 
                if (mmi.fState & MFS_GRAYED) {
                    dis.itemState |= ODS_GRAYED;
                } 

                dis.itemState |= ODS_NOACCEL;

                HandleDrawItem(&dis);
            }
        }
    }
}
// This Really just Draws the menu bar items since it assumes 
//  that the background has already been drawn using DoDrawFrame
void cef_dark_window::DoDrawMenuBar(HDC hdc)
{
    HMENU menu = GetMenu();
    int items = ::GetMenuItemCount(menu);

    for (int i = 0; i < items; i++) {
        // Determine the menu item state and ID
        MENUITEMINFO mmi = {0};
        mmi.cbSize = sizeof (mmi);
        mmi.fMask = MIIM_STATE|MIIM_ID;
        ::GetMenuItemInfo (menu, i, TRUE, &mmi);
        
        // Drawitem only works on ID
        MEASUREITEMSTRUCT mis = {0};
        mis.CtlType = ODT_MENU;
        mis.itemID = mmi.wID;

        RECT itemRect;
        ::SetRectEmpty(&itemRect);
        if (::GetMenuItemRect(mWnd, menu, (UINT)i, &itemRect)) {
            ScreenToNonClient(&itemRect);
            
            // Draw the menu item
            DRAWITEMSTRUCT dis = {0};
            dis.CtlType = ODT_MENU;
            dis.itemID = mmi.wID;
            dis.hwndItem = (HWND)menu;
            dis.itemAction = ODA_DRAWENTIRE;
            dis.hDC = hdc;
            ::CopyRect(&dis.rcItem, &itemRect);

            if (mmi.fState & MFS_HILITE) {
                dis.itemState |= ODS_SELECTED;
            } 
            if (mmi.fState & MFS_GRAYED) {
                dis.itemState |= ODS_GRAYED;
            } 

            dis.itemState |= ODS_NOACCEL;

            HandleDrawItem(&dis);
        }
    }
}
// Draw the Caption Bar
void cef_dark_window::DoDrawTitlebarText(HDC hdc)
{
    if (mCaptionFont == 0) {
        mCaptionFont = ::CreateFontIndirect(&mNcMetrics.lfCaptionFont);
    }

    RECT textRect;
    ComputeWindowCaptionRect(textRect);

    HGDIOBJ hPreviousFont = ::SelectObject(hdc, mCaptionFont);        
    int oldBkMode = ::SetBkMode(hdc, TRANSPARENT);
    COLORREF oldRGB = ::SetTextColor(hdc, CEF_COLOR_NORMALTEXT);

    // Setup the rect to use to calculate the position
    RECT windowRect;
    GetWindowRect(&windowRect);
    ScreenToNonClient(&windowRect);

    // Get the title and the length
    int textLength = GetWindowTextLength() + 1;
    LPWSTR szCaption = new wchar_t [textLength + 1];
    ::ZeroMemory(szCaption, textLength + 1);
    int cchCaption = GetWindowText(szCaption, textLength);

    // Figure out how much space we need to draw ethe whole thing
    RECT rectTemp;
    ::SetRectEmpty(&rectTemp);
    ::DrawText(hdc, szCaption, ::wcslen(szCaption), &rectTemp, DT_SINGLELINE|DT_CALCRECT|DT_NOPREFIX);

    // Can it be centered within the window?
    if (((::RectWidth(windowRect) / 2) + (::RectWidth(rectTemp) / 2) + 1) < textRect.right) {
        windowRect.bottom = textRect.bottom;
        windowRect.top += textRect.top;
        ::DrawText(hdc, szCaption, cchCaption, &windowRect, DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
    } else {
        // Can't be centered so LEFT justify and add ellipsis when truncated
        ::DrawText(hdc, szCaption, cchCaption, &textRect, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX);
    }
 
    delete []szCaption;
    ::SetTextColor(hdc, oldRGB);
    ::SetBkMode(hdc, oldBkMode);
    ::SelectObject(hdc, hPreviousFont);
}
void cef_dark_window::ComputeRequiredMenuRect(RECT& rect)
{
    HMENU menu = GetMenu();
    int items = ::GetMenuItemCount(menu);

    ::SetRectEmpty(&rect);

    for (int i = 0; i < items; i++) {
        RECT itemRect;
        ::SetRectEmpty(&itemRect);
        if (::GetMenuItemRect(mWnd, menu, (UINT)i, &itemRect)) {
            ScreenToNonClient(&itemRect);
            RECT dest;
            if (::UnionRect(&dest, &rect, &itemRect)) {
                ::CopyRect(&rect, &dest);
            }
        }
    }
}
// The Aero version doesn't send us WM_DRAWITEM messages
//  to draw the item when hovering so we have to do that
// Pass NULL for pt to remove the highlight from any menu item
void cef_dark_aero_window::HiliteMenuItemAt(LPPOINT pt)
{
    HDC hdc = GetDC();
    HMENU menu = GetMenu();
    int items = ::GetMenuItemCount(menu);
    int oldMenuHilite = mMenuHiliteIndex;
    
    // reset this in case we don't get a hit below
    mMenuHiliteIndex = -1;

    for (int i = 0; i < items; i++) {
        // Determine the menu item state and ID
        MENUITEMINFO mmi = {0};
        mmi.cbSize = sizeof (mmi);
        mmi.fMask = MIIM_STATE|MIIM_ID;
        ::GetMenuItemInfo (menu, i, TRUE, &mmi);
        
        // Drawitem only works on ID
        MEASUREITEMSTRUCT mis = {0};
        mis.CtlType = ODT_MENU;
        mis.itemID = mmi.wID;

        RECT itemRect;
        ::SetRectEmpty(&itemRect);
        if (::GetMenuItemRect(mWnd, menu, (UINT)i, &itemRect)) {
            bool needsUpdate = false;

            if ((pt) && (::PtInRect(&itemRect, *pt))) {
                mmi.fState |= MFS_HILITE;
                mMenuHiliteIndex = i;
                needsUpdate = true;
            } else if (i == oldMenuHilite) {
                mmi.fState &= ~MFS_HILITE;
                needsUpdate = true;
            }

            if (needsUpdate) {
                // Draw the menu item
                DRAWITEMSTRUCT dis = {0};
                dis.CtlType = ODT_MENU;
                dis.itemID = mmi.wID;
                dis.hwndItem = (HWND)menu;
                dis.itemAction = ODA_DRAWENTIRE;
                dis.hDC = hdc;

               ScreenToNonClient(&itemRect);
                ::CopyRect(&dis.rcItem, &itemRect);

                if (mmi.fState & MFS_HILITE) {
                    dis.itemState |= ODS_SELECTED;
                } 
                if (mmi.fState & MFS_GRAYED) {
                    dis.itemState |= ODS_GRAYED;
                } 

                dis.itemState |= ODS_NOACCEL;

                HandleDrawItem(&dis);
            }
        }
    }

    ReleaseDC(hdc);

}