Set up the foundation for the new Windows uiParent.

This commit is contained in:
Pietro Gagliardi 2015-04-12 19:19:06 -04:00
parent 2209039a6b
commit 5c6d2533ac
6 changed files with 247 additions and 208 deletions

View File

@ -1,151 +0,0 @@
// 7 april 2015
#include "uipriv_windows.h"
// TODOs
// - wiith CTLCOLOR handler: [12:24] <ZeroOne> There's flickering between tabs
// - with CTLCOLOR handler: [12:24] <ZeroOne> And setting the button text blanked out the entire GUI until I ran my mouse over the elements / [12:25] <ZeroOne> https://dl.dropboxusercontent.com/u/15144168/GUI%20stuff.png / [12:41] <ZeroOne> https://dl.dropboxusercontent.com/u/15144168/stack.png here have another screenshot
// - I get this too
/*
all container windows (including the message-only window, hence this is not in container_windows.c) have to call the sharedWndProc() to ensure messages go in the right place and control colors are handled properly
*/
/*
all controls that have events receive the events themselves through subclasses
to do this, all container windows (including the message-only window; see http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q104069) forward WM_COMMAND to each control with this function, WM_NOTIFY with forwardNotify, etc.
*/
static LRESULT forwardCommand(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND control = (HWND) lParam;
// don't generate an event if the control (if there is one) is unparented (a child of the initial parent window)
if (control != NULL && IsChild(initialParent, control) == 0)
return SendMessageW(control, msgCOMMAND, wParam, lParam);
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
static LRESULT forwardNotify(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
NMHDR *nmhdr = (NMHDR *) lParam;
HWND control = nmhdr->hwndFrom;
// don't generate an event if the control (if there is one) is unparented (a child of the initial parent window)
if (control != NULL && IsChild(initialParent, control) == 0)
return SendMessageW(control, msgNOTIFY, wParam, lParam);
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
static void paintControlBackground(HWND hwnd, HDC dc)
{
HWND parent;
RECT r;
POINT pOrig;
DWORD le;
parent = hwnd;
for (;;) {
parent = GetParent(parent);
if (parent == NULL)
logLastError("error getting parent control of control in paintControlBackground()");
// wine sends these messages early, yay...
if (parent == initialParent)
return;
// skip groupboxes; they're (supposed to be) transparent
if (windowClassOf(parent, L"button", NULL) != 0)
break;
}
if (GetWindowRect(hwnd, &r) == 0)
logLastError("error getting control's window rect in paintControlBackground()");
// the above is a window rect in screen coordinates; convert to parent coordinates
SetLastError(0);
if (MapWindowRect(NULL, parent, &r) == 0) {
le = GetLastError();
SetLastError(le); // just to be safe
if (le != 0)
logLastError("error getting client origin of control in paintControlBackground()");
}
if (SetWindowOrgEx(dc, r.left, r.top, &pOrig) == 0)
logLastError("error moving window origin in paintControlBackground()");
SendMessageW(parent, WM_PRINTCLIENT, (WPARAM) dc, PRF_CLIENT);
if (SetWindowOrgEx(dc, pOrig.x, pOrig.y, NULL) == 0)
logLastError("error resetting window origin in paintControlBackground()");
}
BOOL sharedWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT *lResult)
{
switch (uMsg) {
case WM_COMMAND:
*lResult = forwardCommand(hwnd, uMsg, wParam, lParam);
return TRUE;
case WM_NOTIFY:
*lResult = forwardNotify(hwnd, uMsg, wParam, lParam);
return TRUE;
case WM_CTLCOLORSTATIC:
case WM_CTLCOLORBTN:
/*TODO // read-only TextFields and Textboxes are exempt
// this is because read-only edit controls count under WM_CTLCOLORSTATIC
if (windowClassOf((HWND) lParam, L"edit", NULL) == 0)
if (textfieldReadOnly((HWND) lParam))
return FALSE;
*/ if (SetBkMode((HDC) wParam, TRANSPARENT) == 0)
logLastError("error setting transparent background mode to controls in sharedWndProc()");
paintControlBackground((HWND) lParam, (HDC) wParam);
*lResult = (LRESULT) hollowBrush;
return TRUE;
}
return FALSE;
}
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing and https://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
// this X value is really only for buttons but I don't see a better one :/
#define winXPadding 4
#define winYPadding 4
void resize(uiControl *control, HWND parent, RECT r, RECT margin)
{
uiSizing d;
uiSizingSys sys;
HDC dc;
HFONT prevfont;
TEXTMETRICW tm;
SIZE size;
size.cx = 0;
size.cy = 0;
ZeroMemory(&tm, sizeof (TEXTMETRICW));
dc = GetDC(parent);
if (dc == NULL)
logLastError("error getting DC in resize()");
prevfont = (HFONT) SelectObject(dc, hMessageFont);
if (prevfont == NULL)
logLastError("error loading control font into device context in resize()");
if (GetTextMetricsW(dc, &tm) == 0)
logLastError("error getting text metrics in resize()");
if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
logLastError("error getting text extent point in resize()");
sys.baseX = (int) ((size.cx / 26 + 1) / 2);
sys.baseY = (int) tm.tmHeight;
sys.internalLeading = tm.tmInternalLeading;
if (SelectObject(dc, prevfont) != hMessageFont)
logLastError("error restoring previous font into device context in resize()");
if (ReleaseDC(parent, dc) == 0)
logLastError("error releasing DC in resize()");
r.left += uiDlgUnitsToX(margin.left, sys.baseX);
r.top += uiDlgUnitsToY(margin.top, sys.baseY);
r.right -= uiDlgUnitsToX(margin.right, sys.baseX);
r.bottom -= uiDlgUnitsToY(margin.bottom, sys.baseY);
d.xPadding = uiDlgUnitsToX(winXPadding, sys.baseX);
d.yPadding = uiDlgUnitsToY(winYPadding, sys.baseY);
d.sys = &sys;
uiControlResize(control, r.left, r.top, r.right - r.left, r.bottom - r.top, &d);
}
void updateParent(uintptr_t h)
{
HWND hwnd;
if (h == 0) // no parent
return;
hwnd = (HWND) h;
SendMessageW(hwnd, msgUpdateChild, 0, 0);
}

View File

@ -95,7 +95,7 @@ const char *uiInit(uiInitOptions *o)
if (hMessageFont == NULL) if (hMessageFont == NULL)
return loadLastError("loading default messagebox font; this is the default UI font"); return loadLastError("loading default messagebox font; this is the default UI font");
ce = initInitialParent(hDefaultIcon, hDefaultCursor); ce = initParent(hDefaultIcon, hDefaultCursor);
if (ce != NULL) if (ce != NULL)
return loadLastError(ce); return loadLastError(ce);

View File

@ -1,48 +0,0 @@
// 10 april 2015
#include "uipriv_windows.h"
// for maximum safety, all controls that don't have a parent are made children of this, the "initial parent"
// it behaves like other containers due to bugs described in container_windows.c, but is never seen, is disabled, and cannot be interacted with by end users
// despite being called the initial parent, it is used whenever a control has no parent, even if it loses its parent at some later point during the execution of the program
#define uiInitialParentClass L"uiInitialParentClass"
HWND initialParent;
static LRESULT CALLBACK initialParentWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult;
if (sharedWndProc(hwnd, uMsg, wParam, lParam, &lResult) != FALSE)
return lResult;
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
const char *initInitialParent(HICON hDefaultIcon, HCURSOR hDefaultCursor)
{
WNDCLASSW wc;
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = uiInitialParentClass;
wc.lpfnWndProc = initialParentWndProc;
wc.hInstance = hInstance;
wc.hIcon = hDefaultIcon;
wc.hCursor = hDefaultCursor;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
if (RegisterClassW(&wc) == 0)
return "registering initial parent window class";
initialParent = CreateWindowExW(0,
uiInitialParentClass, L"",
WS_OVERLAPPEDWINDOW,
0, 0,
100, 100,
NULL, NULL, hInstance, NULL);
if (initialParent == NULL)
return "creating initial parent window";
// just to be safe, disable the initial parent so it can't be interacted with accidentally
// if this causes issues for our controls, we can remove it
EnableWindow(initialParent, FALSE);
return NULL;
}

238
parent_windows.c Normal file
View File

@ -0,0 +1,238 @@
// 10 april 2015
#include "uipriv_windows.h"
// All controls in package ui are children of a window of this class.
// This keeps everything together, makes hiding controls en masse (tab page switching, for instance) easy, and makes the overall design cleaner.
// In addition, controls that are first created or don't have a parent are considered children of the "initial parent", which is also of this class.
// This paxxxxxxxxxxxxxxxxxrent is invisible, disabled, and should not be interacted with.
// TODOs
// - wiith CTLCOLOR handler: [12:24] <ZeroOne> There's flickering between tabs
// - with CTLCOLOR handler: [12:24] <ZeroOne> And setting the button text blanked out the entire GUI until I ran my mouse over the elements / [12:25] <ZeroOne> https://dl.dropboxusercontent.com/u/15144168/GUI%20stuff.png / [12:41] <ZeroOne> https://dl.dropboxusercontent.com/u/15144168/stack.png here have another screenshot
// - I get this too
#define uiParentClass L"uiParentClass"
HWND initialParent;
static void paintControlBackground(HWND hwnd, HDC dc)
{
HWND parent;
RECT r;
POINT pOrig;
DWORD le;
parent = hwnd;
for (;;) {
parent = GetParent(parent);
if (parent == NULL)
logLastError("error getting parent control of control in paintControlBackground()");
// wine sends these messages early, yay...
if (parent == initialParent)
return;
// skip groupboxes; they're (supposed to be) transparent
if (windowClassOf(parent, L"button", NULL) != 0)
break;
}
if (GetWindowRect(hwnd, &r) == 0)
logLastError("error getting control's window rect in paintControlBackground()");
// the above is a window rect in screen coordinates; convert to parent coordinates
SetLastError(0);
if (MapWindowRect(NULL, parent, &r) == 0) {
le = GetLastError();
SetLastError(le); // just to be safe
if (le != 0)
logLastError("error getting client origin of control in paintControlBackground()");
}
if (SetWindowOrgEx(dc, r.left, r.top, &pOrig) == 0)
logLastError("error moving window origin in paintControlBackground()");
SendMessageW(parent, WM_PRINTCLIENT, (WPARAM) dc, PRF_CLIENT);
if (SetWindowOrgEx(dc, pOrig.x, pOrig.y, NULL) == 0)
logLastError("error resetting window origin in paintControlBackground()");
}
// from https://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing and https://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
// this X value is really only for buttons but I don't see a better one :/
#define winXPadding 4
#define winYPadding 4
static void resize(uiControl *control, HWND parent, RECT r, RECT margin)
{
uiSizing d;
uiSizingSys sys;
HDC dc;
HFONT prevfont;
TEXTMETRICW tm;
SIZE size;
size.cx = 0;
size.cy = 0;
ZeroMemory(&tm, sizeof (TEXTMETRICW));
dc = GetDC(parent);
if (dc == NULL)
logLastError("error getting DC in resize()");
prevfont = (HFONT) SelectObject(dc, hMessageFont);
if (prevfont == NULL)
logLastError("error loading control font into device context in resize()");
if (GetTextMetricsW(dc, &tm) == 0)
logLastError("error getting text metrics in resize()");
if (GetTextExtentPoint32W(dc, L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52, &size) == 0)
logLastError("error getting text extent point in resize()");
sys.baseX = (int) ((size.cx / 26 + 1) / 2);
sys.baseY = (int) tm.tmHeight;
sys.internalLeading = tm.tmInternalLeading;
if (SelectObject(dc, prevfont) != hMessageFont)
logLastError("error restoring previous font into device context in resize()");
if (ReleaseDC(parent, dc) == 0)
logLastError("error releasing DC in resize()");
r.left += uiDlgUnitsToX(margin.left, sys.baseX);
r.top += uiDlgUnitsToY(margin.top, sys.baseY);
r.right -= uiDlgUnitsToX(margin.right, sys.baseX);
r.bottom -= uiDlgUnitsToY(margin.bottom, sys.baseY);
d.xPadding = uiDlgUnitsToX(winXPadding, sys.baseX);
d.yPadding = uiDlgUnitsToY(winYPadding, sys.baseY);
d.sys = &sys;
uiControlResize(control, r.left, r.top, r.right - r.left, r.bottom - r.top, &d);
}
struct parent {
HWND hwnd;
uiControl *child;
intmax_t leftMargin;
intmax_t topMargin;
intmax_t rightMargin;
intmax_t bottomMargin;
};
static LRESULT CALLBACK parentWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
struct parent *p;
CREATESTRUCTW *cs = (CREATESTRUCTW *) lParam;
HWND control;
NMHDR *nm = (NMHDR *) lParam;
RECT r, margin;
p = (struct parent *) GetWindowLongPtrW(hwnd, GWLP_USERDATA);
if (p == NULL) {
if (uMsg == WM_NCCREATE) {
p = (struct parent *) (cs->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) p);
// fall through to DefWindowProcW()
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
switch (uMsg) {
case WM_NCDESTROY:
// no need to explicitly destroy children; they're already gone by this point (and so are their data structures; they clean up after themselves)
uiFree(p);
break; // fall through to DefWindowPocW()
case WM_COMMAND:
// bounce back to the control in question
// except if to the initial parent, in which case act as if the message was ignored
control = (HWND) lParam;
if (control != NULL && IsChild(initialParent, control) == 0)
return SendMessageW(control, msgCOMMAND, wParam, lParam);
break; // fall through to DefWindowPocW()
case WM_NOTIFY:
// same as WM_COMMAND
control = nm->hwndFrom;
if (control != NULL && IsChild(initialParent, control) == 0)
return SendMessageW(control, msgNOTIFY, wParam, lParam);
break; // fall through to DefWindowProcW()
case WM_CTLCOLORSTATIC:
case WM_CTLCOLORBTN:
/*TODO // read-only TextFields and Textboxes are exempt
// this is because read-only edit controls count under WM_CTLCOLORSTATIC
if (windowClassOf((HWND) lParam, L"edit", NULL) == 0)
if (textfieldReadOnly((HWND) lParam))
break; // fall through to DefWindowProcW()
*/ if (SetBkMode((HDC) wParam, TRANSPARENT) == 0)
logLastError("error setting transparent background mode to controls in sharedWndProc()");
paintControlBackground((HWND) lParam, (HDC) wParam);
return (LRESULT) hollowBrush;
case WM_WINDOWPOSCHANGED:
if ((wp->flags & SWP_NOSIZE) != 0)
break;
// fall through
case msgUpdateChild:
if (p->child == NULL)
break;
if (GetClientRect(p->hwnd, &r) == 0)
logLastError("error getting client rect for resize in parentWndProc()");
margin.left = p->marginLeft;
margin.top = p->marginTop;
margin.right = p->marginRight;
margin.bottom = p->marginBottom;
resize(p->child, p->hwnd, r, margin);
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
const char *initParent(HICON hDefaultIcon, HCURSOR hDefaultCursor)
{
WNDCLASSW wc;
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = uiInitialParentClass;
wc.lpfnWndProc = initialParentWndProc;
wc.hInstance = hInstance;
wc.hIcon = hDefaultIcon;
wc.hCursor = hDefaultCursor;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
if (RegisterClassW(&wc) == 0)
return "registering parent window class";
initialParent = CreateWindowExW(0,
uiParentClass, L"",
WS_OVERLAPPEDWINDOW,
0, 0,
100, 100,
NULL, NULL, hInstance, NULL);
if (initialParent == NULL)
return "creating initial parent window";
// just to be safe, disable the initial parent so it can't be interacted with accidentally
// if this causes issues for our controls, we can remove it
EnableWindow(initialParent, FALSE);
return NULL;
}
static uintptr_t parentHandle(uiParent *p)
{
struct parent *pp = (struct parent *) (p->Internal);
return (uintptr_t) (p->hwnd);
}
static void parentSetChild(uiParent *p, uiChild *child)
{
struct parent *pp = (struct parent *) (p->internal);
pp->child = child;
if (pp->child != NULL)
uiControlSetParent(child, p);
}
static void parentSetMargins(uiParent *p, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom)
{
struct parent *pp = (struct parent *) (p->internal);
pp->marginLeft = left;
pp->marginTop = top;
pp->marginRight = right;
pp->marginBottom = bottom;
}
static void parentUpdate(uiParent *p)
{
struct parent *pp = (struct parent *) (p->internal);
SendMessageW(pp->hwnd, msgUpdateChild, 0, 0);
}
uiParent *uiNewParent(uintptr_t osParent)
{
}

8
ui.h
View File

@ -86,12 +86,16 @@ struct uiParent {
#define uiParentHandle(p) ((*((p)->Handle))((p))) #define uiParentHandle(p) ((*((p)->Handle))((p)))
// SetChild sets the uiControl that this uiParent relegates. // SetChild sets the uiControl that this uiParent relegates.
// It calls uiControl.SetParent(). // It calls uiControl.SetParent() which should, in turn, call uiParent.Update().
// The uiParent should already not have a child and the uiControl should already not have a parent.
//
// child can be NULL, in which case the uiParent has no children.
// This form should be called by uiControl.RemoveParent().
void (*SetChild)(uiParent *p, uiControl *child); void (*SetChild)(uiParent *p, uiControl *child);
#define uiParentSetChild(p, child) ((*((p)->SetChild))((p), (child))) #define uiParentSetChild(p, child) ((*((p)->SetChild))((p), (child)))
// SetMargins sets the margins of the uiParent to the given margins. // SetMargins sets the margins of the uiParent to the given margins.
// It then updates the uiParent to make the margins take effect. // It does not call uiParent.Update(); its caller must.
// The units of the margins are backend-defined. // The units of the margins are backend-defined.
// The initial margins are all 0. // The initial margins are all 0.
void (*SetMargins)(uiParent *p, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom); void (*SetMargins)(uiParent *p, intmax_t left, intmax_t top, intmax_t right, intmax_t bottom);

View File

@ -55,10 +55,6 @@ extern WCHAR *toUTF16(const char *);
extern char *toUTF8(const WCHAR *); extern char *toUTF8(const WCHAR *);
extern WCHAR *windowText(HWND); extern WCHAR *windowText(HWND);
// container_windows.c
extern BOOL sharedWndProc(HWND, UINT, WPARAM, LPARAM, LRESULT *);
extern void resize(uiControl *, HWND, RECT, RECT);
// comctl32_windows.c // comctl32_windows.c
extern BOOL (*WINAPI fv_SetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR); extern BOOL (*WINAPI fv_SetWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR, DWORD_PTR);
extern BOOL (*WINAPI fv_RemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR); extern BOOL (*WINAPI fv_RemoveWindowSubclass)(HWND, SUBCLASSPROC, UINT_PTR);
@ -68,6 +64,6 @@ extern const char *initCommonControls(void);
// window_windows.c // window_windows.c
extern ATOM registerWindowClass(HICON, HCURSOR); extern ATOM registerWindowClass(HICON, HCURSOR);
// initparent_windows.c // parent_windows.c
extern HWND initialParent; extern HWND initialParent;
extern const char *initInitialParent(HICON, HCURSOR); extern const char *initParent(HICON, HCURSOR);