2015-08-31 10:31:42 -05:00
|
|
|
// 16 may 2015
|
|
|
|
#include "uipriv_windows.h"
|
|
|
|
|
|
|
|
// TODO
|
2015-09-02 12:19:34 -05:00
|
|
|
// - write comment here about how tabs and parents work
|
|
|
|
// - make sure parent Z-orders are always above tab Z-orders
|
2015-09-02 15:43:20 -05:00
|
|
|
// - commit show/hide/enable/disable
|
2015-08-31 10:31:42 -05:00
|
|
|
|
|
|
|
struct uiTab {
|
|
|
|
uiWindowsControl c;
|
|
|
|
HWND hwnd;
|
|
|
|
struct ptrArray *pages;
|
2015-09-02 12:19:34 -05:00
|
|
|
HWND parent;
|
2015-08-31 10:31:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static void onDestroy(uiTab *);
|
|
|
|
|
2015-08-31 11:33:44 -05:00
|
|
|
uiWindowsDefineControlWithOnDestroy(
|
|
|
|
uiTab, // type name
|
|
|
|
uiTabType, // type function
|
|
|
|
onDestroy(this); // on destroy
|
|
|
|
)
|
2015-08-31 10:31:42 -05:00
|
|
|
|
|
|
|
// utility functions
|
|
|
|
|
|
|
|
static LRESULT curpage(uiTab *t)
|
|
|
|
{
|
|
|
|
return SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showHidePage(uiTab *t, LRESULT which, int hide)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
|
|
|
|
if (which == (LRESULT) (-1))
|
|
|
|
return;
|
|
|
|
page = ptrArrayIndex(t->pages, struct child *, which);
|
|
|
|
if (hide)
|
|
|
|
ShowWindow(childTabPage(page), SW_HIDE);
|
|
|
|
else {
|
|
|
|
ShowWindow(childTabPage(page), SW_SHOW);
|
|
|
|
// we only resize the current page, so we have to do this here
|
2015-09-01 06:21:18 -05:00
|
|
|
childQueueRelayout(page);
|
2015-08-31 10:31:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// control implementation
|
|
|
|
|
|
|
|
static BOOL onWM_NOTIFY(uiControl *c, HWND hwnd, NMHDR *nm, LRESULT *lResult)
|
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
|
|
|
|
if (nm->code != TCN_SELCHANGING && nm->code != TCN_SELCHANGE)
|
|
|
|
return FALSE;
|
|
|
|
showHidePage(t, curpage(t), nm->code == TCN_SELCHANGING);
|
|
|
|
*lResult = 0;
|
|
|
|
if (nm->code == TCN_SELCHANGING)
|
|
|
|
*lResult = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void onDestroy(uiTab *t)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
|
|
|
|
while (t->pages->len != 0) {
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, 0);
|
2015-08-31 10:31:42 -05:00
|
|
|
ptrArrayDelete(t->pages, 0);
|
|
|
|
childDestroy(page);
|
|
|
|
}
|
|
|
|
ptrArrayDestroy(t->pages);
|
|
|
|
uiWindowsUnregisterWM_NOTIFYHandler(t->hwnd);
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:19:34 -05:00
|
|
|
static void tabCommitSetParent(uiWindowsControl *c, HWND parent)
|
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
struct child *page;
|
|
|
|
HWND pagehwnd;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
t->parent = parent;
|
|
|
|
uiWindowsEnsureSetParent(t->hwnd, t->parent);
|
|
|
|
for (i = 0; i < t->pages->len; i++) {
|
|
|
|
page = ptrArrayIndex(t->pages, struct child *, i);
|
|
|
|
pagehwnd = childTabPage(page);
|
|
|
|
uiWindowsEnsureSetParent(pagehwnd, t->parent);
|
|
|
|
}
|
|
|
|
uiWindowsRearrangeControlIDsZOrder(uiControl(t));
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
|
2015-08-31 10:31:42 -05:00
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
intmax_t pagewid, pageht;
|
2015-09-02 15:02:06 -05:00
|
|
|
struct child *page;
|
|
|
|
LRESULT n;
|
2015-08-31 10:31:42 -05:00
|
|
|
RECT r;
|
|
|
|
|
2015-09-02 15:02:06 -05:00
|
|
|
// only consider the current page
|
|
|
|
pagewid = 0;
|
|
|
|
pageht = 0;
|
|
|
|
n = curpage(t);
|
|
|
|
if (n != (LRESULT) (-1)) {
|
|
|
|
page = ptrArrayIndex(t->pages, struct child *, n);
|
2015-09-01 06:21:18 -05:00
|
|
|
childMinimumSize(page, d, &pagewid, &pageht);
|
2015-08-31 10:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
r.left = 0;
|
|
|
|
r.top = 0;
|
2015-09-02 15:02:06 -05:00
|
|
|
r.right = pagewid;
|
|
|
|
r.bottom = pageht;
|
2015-08-31 10:31:42 -05:00
|
|
|
// this also includes the tabs themselves
|
|
|
|
SendMessageW(t->hwnd, TCM_ADJUSTRECT, (WPARAM) TRUE, (LPARAM) (&r));
|
|
|
|
*width = r.right - r.left;
|
|
|
|
*height = r.bottom - r.top;
|
|
|
|
}
|
|
|
|
|
2015-09-01 06:21:18 -05:00
|
|
|
static void tabRelayout(uiWindowsControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
|
2015-08-31 10:31:42 -05:00
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
LRESULT n;
|
2015-09-01 08:39:25 -05:00
|
|
|
struct child *page;
|
2015-08-31 10:31:42 -05:00
|
|
|
RECT r;
|
|
|
|
|
2015-09-01 08:39:25 -05:00
|
|
|
uiWindowsEnsureMoveWindow(t->hwnd, x, y, width, height);
|
2015-08-31 10:31:42 -05:00
|
|
|
n = curpage(t);
|
|
|
|
if (n == (LRESULT) (-1))
|
|
|
|
return;
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, n);
|
2015-08-31 10:31:42 -05:00
|
|
|
|
|
|
|
// now we need to figure out what rect the child goes
|
2015-09-01 08:39:25 -05:00
|
|
|
// this rect needs to be in parent window coordinates, but TCM_ADJUSTRECT wants a window rect, which is screen coordinates
|
2015-09-02 12:19:34 -05:00
|
|
|
// because we have each page as a sibling of the tab, use the tab's own rect as the input rect
|
2015-08-31 10:31:42 -05:00
|
|
|
r.left = x;
|
|
|
|
r.top = y;
|
|
|
|
r.right = x + width;
|
|
|
|
r.bottom = y + height;
|
2015-09-01 08:39:25 -05:00
|
|
|
mapWindowRect(t->hwnd, NULL, &r);
|
2015-08-31 10:31:42 -05:00
|
|
|
SendMessageW(t->hwnd, TCM_ADJUSTRECT, (WPARAM) FALSE, (LPARAM) (&r));
|
2015-09-01 08:39:25 -05:00
|
|
|
mapWindowRect(NULL, t->hwnd, &r);
|
2015-09-01 10:46:53 -05:00
|
|
|
|
2015-09-01 08:39:25 -05:00
|
|
|
childRelayout(page, r.left, r.top, r.right - r.left, r.bottom - r.top);
|
2015-08-31 10:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tabContainerUpdateState(uiControl *c)
|
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
struct child *page;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < t->pages->len; i++) {
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, i);
|
2015-08-31 10:31:42 -05:00
|
|
|
childUpdateState(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 11:59:57 -05:00
|
|
|
static void tabArrangeChildrenControlIDsZOrder(uiWindowsControl *c)
|
|
|
|
{
|
|
|
|
uiTab *t = uiTab(c);
|
|
|
|
struct child *page;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < t->pages->len; i++) {
|
|
|
|
page = ptrArrayIndex(t->pages, struct child *, i);
|
|
|
|
childSetSoleControlID(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:31:42 -05:00
|
|
|
void uiTabAppend(uiTab *t, const char *name, uiControl *child)
|
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
uiTabInsertAt(t, name, t->pages->len, child);
|
2015-08-31 10:31:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabInsertAt(uiTab *t, const char *name, uintmax_t n, uiControl *child)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
LRESULT hide, show;
|
|
|
|
TCITEMW item;
|
|
|
|
WCHAR *wname;
|
|
|
|
|
|
|
|
// see below
|
|
|
|
hide = curpage(t);
|
|
|
|
|
2015-09-02 12:19:34 -05:00
|
|
|
page = newChildWithTabPage(child, uiControl(t), t->parent);
|
2015-09-02 08:18:49 -05:00
|
|
|
childSetSoleControlID(page);
|
2015-08-31 10:31:42 -05:00
|
|
|
ptrArrayInsertAt(t->pages, n, page);
|
|
|
|
|
|
|
|
ZeroMemory(&item, sizeof (TCITEMW));
|
|
|
|
item.mask = TCIF_TEXT;
|
|
|
|
wname = toUTF16(name);
|
|
|
|
item.pszText = wname;
|
|
|
|
if (SendMessageW(t->hwnd, TCM_INSERTITEM, (WPARAM) n, (LPARAM) (&item)) == (LRESULT) -1)
|
|
|
|
logLastError("error adding tab to uiTab in uiTabInsertAt()");
|
|
|
|
uiFree(wname);
|
|
|
|
|
|
|
|
// we need to do this because adding the first tab doesn't send a TCN_SELCHANGE; it just shows the page
|
|
|
|
show = curpage(t);
|
|
|
|
if (show != hide) {
|
|
|
|
showHidePage(t, hide, 1);
|
|
|
|
showHidePage(t, show, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabDelete(uiTab *t, uintmax_t n)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
|
|
|
|
// first delete the tab from the tab control
|
|
|
|
// if this is the current tab, no tab will be selected, which is good
|
|
|
|
if (SendMessageW(t->hwnd, TCM_DELETEITEM, (WPARAM) n, 0) == FALSE)
|
|
|
|
logLastError("error deleting uiTab tab in tabDelete()");
|
|
|
|
|
|
|
|
// now delete the page itself
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, n);
|
2015-08-31 10:31:42 -05:00
|
|
|
ptrArrayDelete(t->pages, n);
|
|
|
|
childRemove(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
uintmax_t uiTabNumPages(uiTab *t)
|
|
|
|
{
|
|
|
|
return t->pages->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uiTabMargined(uiTab *t, uintmax_t n)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, n);
|
2015-08-31 10:31:42 -05:00
|
|
|
return childMargined(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uiTabSetMargined(uiTab *t, uintmax_t n, int margined)
|
|
|
|
{
|
|
|
|
struct child *page;
|
|
|
|
|
2015-09-01 06:21:18 -05:00
|
|
|
page = ptrArrayIndex(t->pages, struct child *, n);
|
2015-08-31 10:31:42 -05:00
|
|
|
childSetMargined(page, margined);
|
|
|
|
}
|
|
|
|
|
|
|
|
uiTab *uiNewTab(void)
|
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
uiTab *t;
|
2015-08-31 10:31:42 -05:00
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
t = (uiTab *) uiNewControl(uiTabType());
|
2015-08-31 10:31:42 -05:00
|
|
|
|
2015-09-02 08:41:32 -05:00
|
|
|
t->hwnd = uiWindowsEnsureCreateControlHWND(0,
|
2015-08-31 10:31:42 -05:00
|
|
|
WC_TABCONTROLW, L"",
|
2015-09-02 08:41:32 -05:00
|
|
|
TCS_TOOLTIPS | WS_TABSTOP,
|
2015-08-31 10:31:42 -05:00
|
|
|
hInstance, NULL,
|
|
|
|
TRUE);
|
|
|
|
|
|
|
|
uiWindowsRegisterWM_NOTIFYHandler(t->hwnd, onWM_NOTIFY, uiControl(t));
|
|
|
|
|
|
|
|
t->pages = newPtrArray();
|
|
|
|
|
2015-09-02 12:19:34 -05:00
|
|
|
t->parent = utilWindow;
|
|
|
|
|
2015-08-31 11:33:44 -05:00
|
|
|
uiWindowsFinishNewControl(t, uiTab);
|
2015-08-31 10:31:42 -05:00
|
|
|
uiControl(t)->ContainerUpdateState = tabContainerUpdateState;
|
2015-09-02 12:19:34 -05:00
|
|
|
uiWindowsControl(t)->CommitSetParent = tabCommitSetParent;
|
2015-08-31 10:31:42 -05:00
|
|
|
uiWindowsControl(t)->Relayout = tabRelayout;
|
2015-09-02 11:59:57 -05:00
|
|
|
uiWindowsControl(t)->ArrangeChildrenControlIDsZOrder = tabArrangeChildrenControlIDsZOrder;
|
2015-08-31 10:31:42 -05:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|