2015-05-14 11:49:02 -05:00
|
|
|
// 26 april 2015
|
2016-04-22 17:11:20 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
2015-05-14 11:49:02 -05:00
|
|
|
|
2015-08-30 17:38:22 -05:00
|
|
|
// This contains code used by all uiControls that contain other controls.
|
|
|
|
// It also contains the code to draw the background of a container.c container, as that is a variant of the WM_CTLCOLORxxx handler code.
|
2015-05-14 11:49:02 -05:00
|
|
|
|
|
|
|
static HBRUSH parentBrush = NULL;
|
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
static HWND parentWithBackground(HWND hwnd)
|
2015-05-14 11:49:02 -05:00
|
|
|
{
|
|
|
|
HWND parent;
|
2016-04-23 23:33:42 -05:00
|
|
|
int cls;
|
2015-05-14 11:49:02 -05:00
|
|
|
|
|
|
|
parent = hwnd;
|
|
|
|
for (;;) {
|
2016-04-22 17:11:20 -05:00
|
|
|
parent = parentOf(parent);
|
2015-05-14 11:49:02 -05:00
|
|
|
// skip groupboxes; they're (supposed to be) transparent
|
|
|
|
// skip uiContainers; they don't draw anything
|
2016-04-23 23:33:42 -05:00
|
|
|
cls = windowClassOf(parent, L"button", containerClass, NULL);
|
|
|
|
if (cls != 0 && cls != 1)
|
2015-05-14 11:49:02 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct parentDraw {
|
|
|
|
HDC cdc;
|
|
|
|
HBITMAP bitmap;
|
|
|
|
HBITMAP prevbitmap;
|
|
|
|
};
|
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
static HRESULT parentDraw(HDC dc, HWND parent, struct parentDraw *pd)
|
2015-05-14 11:49:02 -05:00
|
|
|
{
|
|
|
|
RECT r;
|
|
|
|
|
2016-04-26 23:54:22 -05:00
|
|
|
if (getClientRect(parent, &r) == 0)
|
2016-04-22 17:11:20 -05:00
|
|
|
return logLastError(L"error getting parent's client rect");
|
2015-05-14 11:49:02 -05:00
|
|
|
pd->cdc = CreateCompatibleDC(dc);
|
|
|
|
if (pd->cdc == NULL)
|
2016-04-22 17:11:20 -05:00
|
|
|
return logLastError(L"error creating compatible DC");
|
2015-05-14 11:49:02 -05:00
|
|
|
pd->bitmap = CreateCompatibleBitmap(dc, r.right - r.left, r.bottom - r.top);
|
|
|
|
if (pd->bitmap == NULL)
|
2016-04-22 17:11:20 -05:00
|
|
|
return logLastError(L"error creating compatible bitmap");
|
2016-04-23 23:33:42 -05:00
|
|
|
pd->prevbitmap = (HBITMAP) SelectObject(pd->cdc, pd->bitmap);
|
2015-05-14 11:49:02 -05:00
|
|
|
if (pd->prevbitmap == NULL)
|
2016-04-22 17:11:20 -05:00
|
|
|
return logLastError(L"error selecting bitmap into compatible DC");
|
2015-05-14 11:49:02 -05:00
|
|
|
SendMessageW(parent, WM_PRINTCLIENT, (WPARAM) (pd->cdc), PRF_CLIENT);
|
2016-04-22 17:11:20 -05:00
|
|
|
return S_OK;
|
2015-05-14 11:49:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void endParentDraw(struct parentDraw *pd)
|
|
|
|
{
|
2016-04-22 17:11:20 -05:00
|
|
|
// continue in case of any error
|
|
|
|
if (pd->prevbitmap != NULL)
|
2016-04-23 23:33:42 -05:00
|
|
|
if (((HBITMAP) SelectObject(pd->cdc, pd->prevbitmap)) != pd->bitmap)
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error selecting previous bitmap back into compatible DC");
|
|
|
|
if (pd->bitmap != NULL)
|
|
|
|
if (DeleteObject(pd->bitmap) == 0)
|
|
|
|
logLastError(L"error deleting compatible bitmap");
|
|
|
|
if (pd->cdc != NULL)
|
|
|
|
if (DeleteDC(pd->cdc) == 0)
|
|
|
|
logLastError(L"error deleting compatible DC");
|
2015-05-14 11:49:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// see http://www.codeproject.com/Articles/5978/Correctly-drawn-themed-dialogs-in-WinXP
|
|
|
|
static HBRUSH getControlBackgroundBrush(HWND hwnd, HDC dc)
|
|
|
|
{
|
|
|
|
HWND parent;
|
|
|
|
RECT hwndScreenRect;
|
|
|
|
struct parentDraw pd;
|
|
|
|
HBRUSH brush;
|
2016-04-23 23:33:42 -05:00
|
|
|
HRESULT hr;
|
2015-05-14 11:49:02 -05:00
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
parent = parentWithBackground(hwnd);
|
2015-05-14 11:49:02 -05:00
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
hr = parentDraw(dc, parent, &pd);
|
|
|
|
if (hr != S_OK)
|
|
|
|
return NULL;
|
2015-05-14 11:49:02 -05:00
|
|
|
brush = CreatePatternBrush(pd.bitmap);
|
2016-04-22 17:11:20 -05:00
|
|
|
if (brush == NULL) {
|
|
|
|
logLastError(L"error creating pattern brush");
|
|
|
|
endParentDraw(&pd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-05-14 11:49:02 -05:00
|
|
|
endParentDraw(&pd);
|
|
|
|
|
|
|
|
// now figure out where the control is relative to the parent so we can align the brush properly
|
2016-04-22 17:11:20 -05:00
|
|
|
// if anything fails, give up and return the brush as-is
|
2016-04-26 23:54:22 -05:00
|
|
|
if (getWindowRect(hwnd, &hwndScreenRect) == 0) {
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error getting control window rect");
|
|
|
|
return brush;
|
|
|
|
}
|
2015-05-14 11:49:02 -05:00
|
|
|
// this will be in screen coordinates; convert to parent coordinates
|
|
|
|
mapWindowRect(NULL, parent, &hwndScreenRect);
|
|
|
|
if (SetBrushOrgEx(dc, -hwndScreenRect.left, -hwndScreenRect.top, NULL) == 0)
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error setting brush origin");
|
2015-05-14 11:49:02 -05:00
|
|
|
|
|
|
|
return brush;
|
|
|
|
}
|
|
|
|
|
2015-05-24 13:50:25 -05:00
|
|
|
void paintContainerBackground(HWND hwnd, HDC dc, RECT *paintRect)
|
2015-05-14 11:49:02 -05:00
|
|
|
{
|
|
|
|
HWND parent;
|
|
|
|
RECT paintRectParent;
|
|
|
|
struct parentDraw pd;
|
2016-04-22 17:11:20 -05:00
|
|
|
HRESULT hr;
|
2015-05-14 11:49:02 -05:00
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
parent = parentWithBackground(hwnd);
|
|
|
|
hr = parentDraw(dc, parent, &pd);
|
|
|
|
if (hr != S_OK) // we couldn't get it; draw nothing
|
2016-04-23 23:33:42 -05:00
|
|
|
return;
|
2015-05-14 11:49:02 -05:00
|
|
|
|
|
|
|
paintRectParent = *paintRect;
|
|
|
|
mapWindowRect(hwnd, parent, &paintRectParent);
|
|
|
|
if (BitBlt(dc, paintRect->left, paintRect->top, paintRect->right - paintRect->left, paintRect->bottom - paintRect->top,
|
|
|
|
pd.cdc, paintRectParent.left, paintRectParent.top,
|
|
|
|
SRCCOPY) == 0)
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error drawing parent background over uiContainer");
|
2015-05-14 11:49:02 -05:00
|
|
|
|
|
|
|
endParentDraw(&pd);
|
|
|
|
}
|
|
|
|
|
2016-04-22 17:11:20 -05:00
|
|
|
// TODO make this public if we want custom containers
|
2015-05-14 11:49:02 -05:00
|
|
|
// why have this to begin with? http://blogs.msdn.com/b/oldnewthing/archive/2010/03/16/9979112.aspx
|
|
|
|
BOOL handleParentMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *lResult)
|
|
|
|
{
|
|
|
|
switch (uMsg) {
|
|
|
|
case WM_COMMAND:
|
2015-05-20 21:46:50 -05:00
|
|
|
return runWM_COMMAND(wParam, lParam, lResult);
|
2015-05-14 11:49:02 -05:00
|
|
|
case WM_NOTIFY:
|
2015-05-21 10:29:25 -05:00
|
|
|
return runWM_NOTIFY(wParam, lParam, lResult);
|
2015-05-19 22:16:46 -05:00
|
|
|
case WM_HSCROLL:
|
2015-05-21 10:29:25 -05:00
|
|
|
return runWM_HSCROLL(wParam, lParam, lResult);
|
2015-06-06 15:01:32 -05:00
|
|
|
case WM_CTLCOLORSTATIC:
|
|
|
|
case WM_CTLCOLORBTN:
|
2015-05-14 11:49:02 -05:00
|
|
|
if (parentBrush != NULL)
|
2015-05-16 00:55:34 -05:00
|
|
|
if (DeleteObject(parentBrush) == 0)
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error deleting old background brush()"); // but continue anyway; we will leak a brush but whatever
|
2015-05-14 11:49:02 -05:00
|
|
|
if (SetBkMode((HDC) wParam, TRANSPARENT) == 0)
|
2016-04-22 17:11:20 -05:00
|
|
|
logLastError(L"error setting transparent background mode to controls"); // but continue anyway; text will be wrong
|
2015-05-14 11:49:02 -05:00
|
|
|
parentBrush = getControlBackgroundBrush((HWND) lParam, (HDC) wParam);
|
2016-04-22 17:11:20 -05:00
|
|
|
if (parentBrush == NULL) // failed; just do default behavior
|
|
|
|
return FALSE;
|
2015-05-14 11:49:02 -05:00
|
|
|
*lResult = (LRESULT) parentBrush;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|