Reworked (most of) windows/control.c to play with the portable control base. Not too happy with having to store the uiControl in the singleHWND for the subclass procedure, but... I think I've reached the breaking point with this particular object-oriented model.

This commit is contained in:
Pietro Gagliardi 2015-05-18 22:32:22 -04:00
parent 2ff913cd58
commit ca697fa533
3 changed files with 43 additions and 128 deletions

View File

@ -27,6 +27,7 @@ baseHFILES = \
baseCFILES = \ baseCFILES = \
box.c \ box.c \
control.c \
ptrarray.c \ ptrarray.c \
shouldquit.c \ shouldquit.c \
types.c \ types.c \

View File

@ -1,5 +1,6 @@
// 6 april 2015 // 6 april 2015
#include "uipriv_windows.h" #include "out/ui.h"
#include "uipriv.h"
struct singleControl { struct singleControl {
void *internal; void *internal;
@ -16,7 +17,7 @@ static void singleDestroy(uiControl *c)
if (s->parent != NULL) if (s->parent != NULL)
complain("attempt to destroy a uiControl at %p while it still has a parent", c); complain("attempt to destroy a uiControl at %p while it still has a parent", c);
osOnDestroy(s->internal); osSingleDestroy(s->internal);
uiFree(s); uiFree(s);
} }
@ -159,7 +160,7 @@ void makeControl(uiControl *c, void *internal)
uiControl(c)->Handle = singleHandle; uiControl(c)->Handle = singleHandle;
uiControl(c)->Parent = singleParent; uiControl(c)->Parent = singleParent;
uiControl(c)->SetParent = singleSetParent; uiControl(c)->SetParent = singleSetParent;
// PreferredSize() implemented by the individual controls // PreferredSize() implemented by subclasses
uiControl(c)->Resize = singleResize; uiControl(c)->Resize = singleResize;
uiControl(c)->QueueResize = singleQueueResize; uiControl(c)->QueueResize = singleQueueResize;
uiControl(c)->Sizing = singleSizing; uiControl(c)->Sizing = singleSizing;
@ -172,4 +173,5 @@ void makeControl(uiControl *c, void *internal)
uiControl(c)->Disable = singleDisable; uiControl(c)->Disable = singleDisable;
uiControl(c)->ContainerEnable = singleContainerEnable; uiControl(c)->ContainerEnable = singleContainerEnable;
uiControl(c)->ContainerDisable = singleContainerDisable; uiControl(c)->ContainerDisable = singleContainerDisable;
// SysFunc and SetZOrder implemented by subclasses
} }

View File

@ -2,159 +2,84 @@
#include "uipriv_windows.h" #include "uipriv_windows.h"
struct singleHWND { struct singleHWND {
uiControl *c;
HWND hwnd; HWND hwnd;
BOOL (*onWM_COMMAND)(uiControl *, WORD, LRESULT *); BOOL (*onWM_COMMAND)(uiControl *, WORD, LRESULT *);
BOOL (*onWM_NOTIFY)(uiControl *, NMHDR *, LRESULT *); BOOL (*onWM_NOTIFY)(uiControl *, NMHDR *, LRESULT *);
void (*onDestroy)(void *); void (*onDestroy)(void *);
void *onDestroyData; void *onDestroyData;
uiControl *parent;
int userHidden;
int containerHidden;
int userDisabled;
int containerDisabled;
}; };
static void singleDestroy(uiControl *c) void osSingleDestroy(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
if (s->parent != NULL)
complain("attempt to destroy a uiControl at %p while it still has a parent", c);
(*(s->onDestroy))(s->onDestroyData); (*(s->onDestroy))(s->onDestroyData);
if (DestroyWindow(s->hwnd) == 0) if (DestroyWindow(s->hwnd) == 0)
logLastError("error destroying control in singleDestroy()"); logLastError("error destroying control in singleDestroy()");
uiFree(s); uiFree(s);
} }
static uintptr_t singleHandle(uiControl *c) uintptr_t osSingleHandle(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
return (uintptr_t) (s->hwnd); return (uintptr_t) (s->hwnd);
} }
static uiControl *singleParent(uiControl *c) void osSingleSetParent(void *internal, uiControl *oldParent, uiControl *newParent)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
return s->parent;
}
static void singleSetParent(uiControl *c, uiControl *parent)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
uiControl *oldparent;
HWND newParentHWND; HWND newParentHWND;
oldparent = s->parent;
s->parent = parent;
newParentHWND = utilWindow; newParentHWND = utilWindow;
if (s->parent != NULL) if (newParent != NULL)
newParentHWND = (HWND) uiControlHandle(uiControl(s->parent)); newParentHWND = (HWND) uiControlHandle(newParent);
if (SetParent(s->hwnd, newParentHWND) == NULL) if (SetParent(s->hwnd, newParentHWND) == NULL)
logLastError("error setting control parent in singleSetParent()"); logLastError("error setting control parent in osSingleSetParent()");
} }
static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d) void osSingleResize(void *internal, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
moveWindow(s->hwnd, x, y, width, height, d); moveWindow(s->hwnd, x, y, width, height, d);
} }
static void singleQueueResize(uiControl *c) uiSizing *osSingleSizing(void *internal, uiControl *c)
{
queueResize(c);
}
static uiSizing *singleSizing(uiControl *c)
{ {
return uiWindowsSizing(c); return uiWindowsSizing(c);
} }
static int singleContainerVisible(uiControl *c) void osSingleShow(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
return !s->userHidden && !s->containerHidden;
}
static void singleShow(uiControl *c)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
s->userHidden = 0;
if (!s->containerHidden)
ShowWindow(s->hwnd, SW_SHOW); ShowWindow(s->hwnd, SW_SHOW);
if (s->parent != NULL)
uiControlQueueResize(s->parent);
} }
static void singleHide(uiControl *c) void osSingleHide(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
s->userHidden = 1;
ShowWindow(s->hwnd, SW_HIDE); ShowWindow(s->hwnd, SW_HIDE);
if (s->parent != NULL)
uiControlQueueResize(s->parent);
} }
static void singleContainerShow(uiControl *c) void osSingleEnable(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
s->containerHidden = 0;
if (!s->userHidden)
ShowWindow(s->hwnd, SW_SHOW);
if (s->parent != NULL)
uiControlQueueResize(s->parent);
}
static void singleContainerHide(uiControl *c)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
s->containerHidden = 1;
ShowWindow(s->hwnd, SW_HIDE);
if (s->parent != NULL)
uiControlQueueResize(s->parent);
}
static void singleEnable(uiControl *c)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
s->userDisabled = 0;
if (!s->containerDisabled)
EnableWindow(s->hwnd, TRUE); EnableWindow(s->hwnd, TRUE);
} }
static void singleDisable(uiControl *c) void osSingleDisable(void *internal)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) internal;
s->userDisabled = 1;
EnableWindow(s->hwnd, FALSE);
}
static void singleContainerEnable(uiControl *c)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
s->containerDisabled = 0;
if (!s->userDisabled)
EnableWindow(s->hwnd, TRUE);
}
static void singleContainerDisable(uiControl *c)
{
struct singleHWND *s = (struct singleHWND *) (c->Internal);
s->containerDisabled = 1;
EnableWindow(s->hwnd, FALSE); EnableWindow(s->hwnd, FALSE);
} }
// TODO integrate these two with the main control.c
static void singleSysFunc(uiControl *c, uiControlSysFuncParams *p) static void singleSysFunc(uiControl *c, uiControlSysFuncParams *p)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); struct singleHWND *s = (struct singleHWND *) (c->Internal);
@ -180,17 +105,16 @@ static int singleStartZOrder(uiControl *c, uiControlSysFuncParams *p)
static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) static LRESULT CALLBACK singleSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{ {
uiControl *c = (uiControl *) dwRefData; struct singleHWND *s = (struct singleHWND *) dwRefData;
struct singleHWND *s = (struct singleHWND *) (c->Internal);
LRESULT lResult; LRESULT lResult;
switch (uMsg) { switch (uMsg) {
case msgCOMMAND: case msgCOMMAND:
if ((*(s->onWM_COMMAND))(c, HIWORD(wParam), &lResult) != FALSE) if ((*(s->onWM_COMMAND))(s->c, HIWORD(wParam), &lResult) != FALSE)
return lResult; return lResult;
break; break;
case msgNOTIFY: case msgNOTIFY:
if ((*(s->onWM_NOTIFY))(c, (NMHDR *) lParam, &lResult) != FALSE) if ((*(s->onWM_NOTIFY))(s->c, (NMHDR *) lParam, &lResult) != FALSE)
return lResult; return lResult;
break; break;
case WM_NCDESTROY: case WM_NCDESTROY:
@ -206,6 +130,7 @@ void uiWindowsMakeControl(uiControl *c, uiWindowsMakeControlParams *p)
struct singleHWND *s; struct singleHWND *s;
s = uiNew(struct singleHWND); s = uiNew(struct singleHWND);
s->c = c;
s->hwnd = CreateWindowExW(p->dwExStyle, s->hwnd = CreateWindowExW(p->dwExStyle,
p->lpClassName, p->lpWindowName, p->lpClassName, p->lpWindowName,
p->dwStyle | WS_CHILD | WS_VISIBLE, p->dwStyle | WS_CHILD | WS_VISIBLE,
@ -225,38 +150,24 @@ void uiWindowsMakeControl(uiControl *c, uiWindowsMakeControlParams *p)
SendMessageW(s->hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE); SendMessageW(s->hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE);
// this handles redirected notification messages // this handles redirected notification messages
if (SetWindowSubclass(s->hwnd, singleSubclassProc, 0, (DWORD_PTR) c) == FALSE) if (SetWindowSubclass(s->hwnd, singleSubclassProc, 0, (DWORD_PTR) s) == FALSE)
logLastError("error subclassing Windows control in uiWindowsMakeControl()"); logLastError("error subclassing Windows control in uiWindowsMakeControl()");
uiControl(c)->Internal = s; makeControl(uiControl(c), s);
uiControl(c)->Destroy = singleDestroy;
uiControl(c)->Handle = singleHandle;
uiControl(c)->Parent = singleParent;
uiControl(c)->SetParent = singleSetParent;
// PreferredSize() implemented by the individual controls // PreferredSize() implemented by the individual controls
uiControl(c)->Resize = singleResize;
uiControl(c)->QueueResize = singleQueueResize;
uiControl(c)->Sizing = singleSizing;
uiControl(c)->ContainerVisible = singleContainerVisible;
uiControl(c)->Show = singleShow;
uiControl(c)->Hide = singleHide;
uiControl(c)->ContainerShow = singleContainerShow;
uiControl(c)->ContainerHide = singleContainerHide;
uiControl(c)->Enable = singleEnable;
uiControl(c)->Disable = singleDisable;
uiControl(c)->ContainerEnable = singleContainerEnable;
uiControl(c)->ContainerDisable = singleContainerDisable;
uiControl(c)->SysFunc = singleSysFunc; uiControl(c)->SysFunc = singleSysFunc;
uiControl(c)->StartZOrder = singleStartZOrder; uiControl(c)->StartZOrder = singleStartZOrder;
} }
char *uiWindowsControlText(uiControl *c) char *uiWindowsControlText(uiControl *c)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); HWND hwnd;
WCHAR *wtext; WCHAR *wtext;
char *text; char *text;
wtext = windowText(s->hwnd); hwnd = (HWND) uiControlHandle(c);
wtext = windowText(hwnd);
text = toUTF8(wtext); text = toUTF8(wtext);
uiFree(wtext); uiFree(wtext);
return text; return text;
@ -264,11 +175,12 @@ char *uiWindowsControlText(uiControl *c)
void uiWindowsControlSetText(uiControl *c, const char *text) void uiWindowsControlSetText(uiControl *c, const char *text)
{ {
struct singleHWND *s = (struct singleHWND *) (c->Internal); HWND hwnd;
WCHAR *wtext; WCHAR *wtext;
hwnd = (HWND) uiControlHandle(c);
wtext = toUTF16(text); wtext = toUTF16(text);
if (SetWindowTextW(s->hwnd, wtext) == 0) if (SetWindowTextW(hwnd, wtext) == 0)
logLastError("error setting control text in uiWindowsControlSetText()"); logLastError("error setting control text in uiWindowsControlSetText()");
uiFree(wtext); uiFree(wtext);
} }