No changes, just use RAII-based classes in MSW owner drawn menu code.
Add helper HDC{TextCol,BgCol,BgMode}Changer classes which ensure that the corresponding HDC attribute is reset on scope exit instead of manually calling the corresponding MSW functions to set and reset it. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66014 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
parent
2a9a4f5be9
commit
b146648622
@ -65,6 +65,78 @@ UINT GetMenuState(HMENU hMenu, UINT id, UINT flags) ;
|
|||||||
// hide the ugly cast
|
// hide the ugly cast
|
||||||
#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
|
#define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// helper classes for temporarily changing HDC parameters
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
// This class just stores an HDC.
|
||||||
|
class HDCHandler
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
HDCHandler(HDC hdc) : m_hdc(hdc) { }
|
||||||
|
|
||||||
|
const HDC m_hdc;
|
||||||
|
};
|
||||||
|
|
||||||
|
class HDCTextColChanger : HDCHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HDCTextColChanger(HDC hdc, COLORREF col)
|
||||||
|
: HDCHandler(hdc),
|
||||||
|
m_colOld(::SetTextColor(hdc, col))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~HDCTextColChanger()
|
||||||
|
{
|
||||||
|
::SetTextColor(m_hdc, m_colOld);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
COLORREF m_colOld;
|
||||||
|
};
|
||||||
|
|
||||||
|
class HDCBgColChanger : HDCHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HDCBgColChanger(HDC hdc, COLORREF col)
|
||||||
|
: HDCHandler(hdc),
|
||||||
|
m_colOld(::SetBkColor(hdc, col))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~HDCBgColChanger()
|
||||||
|
{
|
||||||
|
::SetBkColor(m_hdc, m_colOld);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
COLORREF m_colOld;
|
||||||
|
};
|
||||||
|
|
||||||
|
class HDCBgModeChanger : HDCHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
HDCBgModeChanger(HDC hdc, int mode)
|
||||||
|
: HDCHandler(hdc),
|
||||||
|
m_modeOld(::SetBkMode(hdc, mode))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~HDCBgModeChanger()
|
||||||
|
{
|
||||||
|
::SetBkMode(m_hdc, m_modeOld);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_modeOld;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// implementation
|
// implementation
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@ -860,11 +932,8 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
|
|||||||
wxFont font;
|
wxFont font;
|
||||||
GetFontToUse(font);
|
GetFontToUse(font);
|
||||||
|
|
||||||
wxColour colText1, colBack1;
|
wxColour colText, colBack;
|
||||||
GetColourToUse(stat, colText1, colBack1);
|
GetColourToUse(stat, colText, colBack);
|
||||||
|
|
||||||
DWORD colText = wxColourToPalRGB(colText1);
|
|
||||||
DWORD colBack = wxColourToPalRGB(colBack1);
|
|
||||||
|
|
||||||
// calculate metrics of item parts
|
// calculate metrics of item parts
|
||||||
RECT rcSelection = rect;
|
RECT rcSelection = rect;
|
||||||
@ -942,7 +1011,7 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
AutoHBRUSH hbr(colBack);
|
AutoHBRUSH hbr(colBack.GetPixel());
|
||||||
SelectInHDC selBrush(hdc, hbr);
|
SelectInHDC selBrush(hdc, hbr);
|
||||||
::FillRect(hdc, &rcSelection, hbr);
|
::FillRect(hdc, &rcSelection, hbr);
|
||||||
}
|
}
|
||||||
@ -951,21 +1020,20 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
|
|||||||
// draw text label
|
// draw text label
|
||||||
// using native API because it recognizes '&'
|
// using native API because it recognizes '&'
|
||||||
|
|
||||||
COLORREF colOldText = ::SetTextColor(hdc, colText);
|
HDCTextColChanger changeTextCol(hdc, colText.GetPixel());
|
||||||
COLORREF colOldBack = ::SetBkColor(hdc, colBack);
|
HDCBgColChanger changeBgCol(hdc, colBack.GetPixel());
|
||||||
|
HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
|
||||||
int prevMode = SetBkMode(hdc, TRANSPARENT);
|
|
||||||
|
|
||||||
SelectInHDC selFont(hdc, GetHfontOf(font));
|
SelectInHDC selFont(hdc, GetHfontOf(font));
|
||||||
|
|
||||||
|
|
||||||
// item text name without menemonic for calculating size
|
// item text name without mnemonic for calculating size
|
||||||
wxString text = GetName();
|
wxString text = GetName();
|
||||||
|
|
||||||
SIZE textSize;
|
SIZE textSize;
|
||||||
::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
|
::GetTextExtentPoint32(hdc, text.c_str(), text.length(), &textSize);
|
||||||
|
|
||||||
// item text name with menemonic
|
// item text name with mnemonic
|
||||||
text = GetItemLabel().BeforeFirst('\t');
|
text = GetItemLabel().BeforeFirst('\t');
|
||||||
|
|
||||||
int flags = DST_PREFIXTEXT;
|
int flags = DST_PREFIXTEXT;
|
||||||
@ -1013,10 +1081,6 @@ bool wxMenuItem::OnDrawItem(wxDC& dc, const wxRect& rc,
|
|||||||
::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
|
::DrawState(hdc, NULL, NULL, (LPARAM)accel.wx_str(),
|
||||||
accel.length(), x, y, 0, 0, flags);
|
accel.length(), x, y, 0, 0, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
::SetBkMode(hdc, prevMode);
|
|
||||||
::SetBkColor(hdc, colOldBack);
|
|
||||||
::SetTextColor(hdc, colOldText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -1103,9 +1167,9 @@ void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask,
|
|||||||
const COLORREF colBlack = RGB(0, 0, 0);
|
const COLORREF colBlack = RGB(0, 0, 0);
|
||||||
const COLORREF colWhite = RGB(255, 255, 255);
|
const COLORREF colWhite = RGB(255, 255, 255);
|
||||||
|
|
||||||
COLORREF colOldText = ::SetTextColor(hdc, colBlack);
|
HDCTextColChanger changeTextCol(hdc, colBlack);
|
||||||
COLORREF colOldBack = ::SetBkColor(hdc, colWhite);
|
HDCBgColChanger changeBgCol(hdc, colWhite);
|
||||||
int prevMode = SetBkMode(hdc, TRANSPARENT);
|
HDCBgModeChanger changeBgMode(hdc, TRANSPARENT);
|
||||||
|
|
||||||
// memory DC for color bitmap
|
// memory DC for color bitmap
|
||||||
MemoryHDC hdcMem(hdc);
|
MemoryHDC hdcMem(hdc);
|
||||||
@ -1132,10 +1196,6 @@ void DrawColorCheckMark(HDC hdc, int x, int y, int cx, int cy, HDC hdcCheckMask,
|
|||||||
::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND);
|
::BitBlt(hdc, x, y, cx, cy, hdcCheckMask, 0, 0, SRCAND);
|
||||||
::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT);
|
::BitBlt(hdc, x, y, cx, cy, hdcMem, 0, 0, SRCPAINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
::SetBkMode(hdc, prevMode);
|
|
||||||
::SetBkColor(hdc, colOldBack);
|
|
||||||
::SetTextColor(hdc, colOldText);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // anonymous namespace
|
} // anonymous namespace
|
||||||
|
Loading…
Reference in New Issue
Block a user