libui/redo/windows/tabpage.c

153 lines
4.1 KiB
C
Raw Normal View History

// 30 may 2015
#include "uipriv_windows.h"
// This is a special internal control type that handles tab pages.
// This doesn't use the container class, but rather a subclassed WC_DIALOG, as that supports tab textures properly.
// The standard property sheet control does the same thing.
2015-05-30 14:03:04 -05:00
struct tabPage {
uiControl c;
HWND hwnd;
2015-06-01 16:37:43 -05:00
uiControl *child;
int margined;
void (*baseResize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *);
};
uiDefineControlType(tabPage, tabPageType, struct tabPage)
// don't override CommitDestroy(); we destroy the child with a separate function
static uintptr_t tabPageHandle(uiControl *c)
{
struct tabPage *t = (struct tabPage *) c;
return (uintptr_t) (t->hwnd);
}
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
#define tabMargin 7
2015-06-01 18:07:00 -05:00
static void tabPagePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
struct tabPage *t = (struct tabPage *) c;
*width = 0;
*height = 0;
if (t->child != NULL)
uiControlPreferredSize(t->child, d, width, height);
if (t->margined) {
*width += 2 * uiWindowsDlgUnitsToX(tabMargin, d->Sys->BaseX);
*height += 2 * uiWindowsDlgUnitsToY(tabMargin, d->Sys->BaseY);
}
}
static void tabPageResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
struct tabPage *t = (struct tabPage *) c;
RECT r;
uiSizing *dchild;
(*(t->baseResize))(uiControl(t), x, y, width, height, d);
if (t->child == NULL)
return;
2015-05-30 12:32:59 -05:00
dchild = uiControlSizing(uiControl(t));
if (GetClientRect(t->hwnd, &r) == 0)
logLastError("error getting tab page client rect in tabPageResize()");
if (t->margined) {
r.left += uiWindowsDlgUnitsToX(tabMargin, d->Sys->BaseX);
r.top += uiWindowsDlgUnitsToY(tabMargin, d->Sys->BaseY);
r.right -= uiWindowsDlgUnitsToX(tabMargin, d->Sys->BaseX);
r.bottom -= uiWindowsDlgUnitsToY(tabMargin, d->Sys->BaseY);
}
2015-05-30 12:32:59 -05:00
// this rect is in client coordinates; we need toplevel window coordinates
mapWindowRect(t->hwnd, dchild->Sys->CoordFrom, &r);
2015-06-01 16:37:43 -05:00
uiControlResize(t->child, r.left, r.top, r.right - r.left, r.bottom - r.top, dchild);
uiFreeSizing(dchild);
}
2015-06-01 18:07:00 -05:00
static void tabPageContainerUpdateState(uiControl *c)
{
struct tabPage *t = (struct tabPage *) c;
if (t->child != NULL)
uiControlUpdateState(t->child);
}
2015-06-01 18:27:28 -05:00
// dummy dialog procedure; see below for details
// let's handle parent messages here to avoid needing to subclass
2015-06-01 18:07:00 -05:00
static INT_PTR CALLBACK dlgproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2015-05-30 14:01:21 -05:00
{
2015-06-01 18:27:28 -05:00
LRESULT lResult;
if (handleParentMessages(hwnd, uMsg, wParam, lParam, &lResult) != FALSE) {
SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (LONG_PTR) lResult);
return TRUE;
}
2015-05-30 14:01:21 -05:00
if (uMsg == WM_INITDIALOG)
return TRUE;
return FALSE;
}
uiControl *newTabPage(void)
{
struct tabPage *t;
HRESULT hr;
t = (struct tabPage *) uiWindowsNewSingleHWNDControl(tabPageType());
2015-05-30 14:01:21 -05:00
// unfortunately this needs to be a proper dialog for EnableThemeDialogTexture() to work; CreateWindowExW() won't suffice
t->hwnd = CreateDialogW(hInstance, MAKEINTRESOURCE(rcTabPageDialog),
utilWindow, dlgproc);
if (t->hwnd == NULL)
logLastError("error creating tab page in newTabPage()");
hr = EnableThemeDialogTexture(t->hwnd, ETDT_ENABLE | ETDT_USETABTEXTURE | ETDT_ENABLETAB);
if (hr != S_OK)
logHRESULT("error setting tab page background in newTabPage()", hr);
uiControl(t)->Handle = tabPageHandle;
2015-06-01 18:07:00 -05:00
uiControl(t)->PreferredSize = tabPagePreferredSize;
t->baseResize = uiControl(t)->Resize;
uiControl(t)->Resize = tabPageResize;
2015-06-01 18:07:00 -05:00
uiControl(t)->ContainerUpdateState = tabPageContainerUpdateState;
return uiControl(t);
}
2015-05-31 09:53:20 -05:00
int tabPageMargined(uiControl *c)
{
struct tabPage *t = (struct tabPage *) c;
return t->margined;
}
void tabPageSetMargined(uiControl *c, int margined)
{
struct tabPage *t = (struct tabPage *) c;
t->margined = margined;
}
2015-06-01 16:37:43 -05:00
void tabPageDestroyChild(uiControl *c)
{
struct tabPage *t = (struct tabPage *) c;
uiControlSetParent(t->child, NULL);
uiControlDestroy(t->child);
t->child = NULL;
2015-06-01 16:37:43 -05:00
}
void tabPageSetChild(uiControl *c, uiControl *child)
{
struct tabPage *t = (struct tabPage *) c;
t->child = child;
t->child = child;
uiControlSetParent(t->child, uiControl(t));
}