libui/redo/windows/control.c

204 lines
4.8 KiB
C
Raw Normal View History

2015-05-29 13:56:11 -05:00
// 27 may 2015
#include "uipriv_windows.h"
2015-05-29 13:56:11 -05:00
// TODO Edit ,s/Util/HWND/g ?
2015-05-29 13:56:11 -05:00
HWND uiWindowsUtilCreateControlHWND(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, HINSTANCE hInstance, LPVOID lpParam, BOOL useStandardControlFont)
{
2015-05-29 13:56:11 -05:00
HWND hwnd;
2015-05-29 13:56:11 -05:00
hwnd = CreateWindowExW(dwExStyle,
2015-05-29 19:53:12 -05:00
lpClassName, lpWindowName,
2015-05-29 13:56:11 -05:00
dwStyle | WS_CHILD | WS_VISIBLE,
0, 0,
// use a nonzero initial size just in case some control breaks with a zero initial size
100, 100,
utilWindow, NULL, hInstance, lpParam);
if (hwnd == NULL)
logLastError("error creating window in uiWindowsUtilCreateControlHWND()");
if (useStandardControlFont)
SendMessageW(hwnd, WM_SETFONT, (WPARAM) hMessageFont, (LPARAM) TRUE);
return hwnd;
}
2015-05-29 13:56:11 -05:00
#define HWND(c) ((HWND) uiControlHandle((c)))
void uiWindowsUtilDestroy(HWND hwnd)
{
2015-05-29 13:56:11 -05:00
if (DestroyWindow(hwnd) == 0)
logLastError("error destroying window in uiWindowsUtilDestroyWindow()");
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitDestroy(uiControl *c)
{
uiWindowsUtilDestroy(HWND(c));
}
2015-05-29 13:56:11 -05:00
void uiWindowsUtilSetParent(HWND hwnd, uiControl *parent)
{
2015-05-29 13:56:11 -05:00
HWND newParent;
2015-05-29 13:56:11 -05:00
newParent = utilWindow;
if (parent != NULL)
newParent = HWND(parent);
if (SetParent(hwnd, newParent) == 0)
logLastError("error changing window parent in uiWindowsUtilSetParent()");
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitSetParent(uiControl *c, uiControl *parent)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilSetParent(HWND(c), parent);
}
2015-05-29 13:56:11 -05:00
void uiWindowsUtilResize(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
moveWindow(hwnd, x, y, width, height, d);
}
static void singleHWNDResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilResize(HWND(c), x, y, width, height, d);
}
2015-05-29 13:56:11 -05:00
static uiSizing *singleHWNDSizing(uiControl *c)
{
2015-05-29 13:56:11 -05:00
// TODO change this to take a HWND and the parent
return uiWindowsSizing(c);
}
2015-05-29 19:53:12 -05:00
void uiWindowsUtilShow(HWND hwnd)
2015-05-29 13:56:11 -05:00
{
ShowWindow(hwnd, SW_SHOW);
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitShow(uiControl *c)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilShow(HWND(c));
}
2015-05-29 13:56:11 -05:00
void uiWindowsUtilHide(HWND hwnd)
{
ShowWindow(hwnd, SW_HIDE);
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitHide(uiControl *c)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilHide(HWND(c));
}
2015-05-29 19:53:12 -05:00
void uiWindowsUtilEnable(HWND hwnd)
2015-05-29 13:56:11 -05:00
{
EnableWindow(hwnd, TRUE);
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitEnable(uiControl *c)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilEnable(HWND(c));
}
2015-05-29 13:56:11 -05:00
void uiWindowsUtilDisable(HWND hwnd)
{
EnableWindow(hwnd, FALSE);
}
2015-05-29 13:56:11 -05:00
static void singleHWNDCommitDisable(uiControl *c)
{
2015-05-29 13:56:11 -05:00
uiWindowsUtilDisable(HWND(c));
}
2015-05-30 16:17:20 -05:00
uintptr_t uiWindowsUtilStartZOrder(HWND hwnd)
2015-05-29 13:56:11 -05:00
{
2015-05-30 16:17:20 -05:00
HWND insertAfter;
DWORD le;
2015-05-30 16:17:20 -05:00
// see http://stackoverflow.com/questions/30491418/
// also, the window at the beginning of the z-order has no previous window, so GetWindow() returns NULL
// we have to differentiate these error states
SetLastError(0);
2015-05-30 16:17:20 -05:00
insertAfter = GetWindow(hwnd, GW_HWNDPREV);
if (insertAfter == NULL) {
le = GetLastError();
SetLastError(le); // just in case
if (le != 0)
logLastError("error getting insert after window in uiWindowsUtilStartZOrder()");
}
2015-05-30 16:17:20 -05:00
return (uintptr_t) insertAfter;
}
2015-05-30 16:17:20 -05:00
static uintptr_t singleHWNDStartZOrder(uiControl *c)
{
2015-05-30 16:17:20 -05:00
return uiWindowsUtilStartZOrder(HWND(c));
}
2015-05-30 16:17:20 -05:00
uintptr_t uiWindowsUtilSetZOrder(HWND hwnd, uintptr_t insertAfter)
{
2015-05-30 16:17:20 -05:00
setWindowInsertAfter(hwnd, (HWND) insertAfter);
return (uintptr_t) hwnd;
}
2015-05-30 16:17:20 -05:00
static uintptr_t singleHWNDSetZOrder(uiControl *c, uintptr_t insertAfter)
{
return uiWindowsUtilSetZOrder(HWND(c), insertAfter);
}
int uiWindowsUtilHasTabStops(HWND hwnd)
{
return (getStyle(hwnd) & WS_TABSTOP) != 0;
}
2015-05-30 16:17:20 -05:00
static int singleHWNDHasTabStops(uiControl *c)
{
2015-05-30 16:17:20 -05:00
return uiWindowsUtilHasTabStops(HWND(c));
}
void setSingleHWNDFuncs(uiControl *c)
{
2015-05-29 13:56:11 -05:00
uiControl(c)->CommitDestroy = singleHWNDCommitDestroy;
uiControl(c)->CommitSetParent = singleHWNDCommitSetParent;
uiControl(c)->Resize = singleHWNDResize;
uiControl(c)->Sizing = singleHWNDSizing;
uiControl(c)->CommitShow = singleHWNDCommitShow;
2015-05-29 19:53:12 -05:00
uiControl(c)->CommitHide = singleHWNDCommitHide;
2015-05-29 13:56:11 -05:00
uiControl(c)->CommitEnable = singleHWNDCommitEnable;
uiControl(c)->CommitDisable = singleHWNDCommitDisable;
uiControl(c)->StartZOrder = singleHWNDStartZOrder;
2015-05-30 16:17:20 -05:00
uiControl(c)->SetZOrder = singleHWNDSetZOrder;
uiControl(c)->HasTabStops = singleHWNDHasTabStops;
}
uiControl *uiWindowsNewSingleHWNDControl(uintmax_t type)
{
uiControl *c;
c = uiNewControl(type);
setSingleHWNDFuncs(c);
2015-05-29 13:56:11 -05:00
return c;
}
2015-05-30 02:28:50 -05:00
// TODO migrate these to the system set up above
char *uiWindowsControlText(uiControl *c)
{
HWND hwnd;
WCHAR *wtext;
char *text;
hwnd = (HWND) uiControlHandle(c);
wtext = windowText(hwnd);
text = toUTF8(wtext);
uiFree(wtext);
return text;
}
void uiWindowsControlSetText(uiControl *c, const char *text)
{
HWND hwnd;
WCHAR *wtext;
hwnd = (HWND) uiControlHandle(c);
wtext = toUTF16(text);
if (SetWindowTextW(hwnd, wtext) == 0)
logLastError("error setting control text in uiWindowsControlSetText()");
uiFree(wtext);
}