Migrated windows/tab.c to ptrarray and fixed the build.
This commit is contained in:
parent
b13d1ebd2c
commit
956b78718f
|
@ -24,6 +24,7 @@ baseHFILES = \
|
||||||
|
|
||||||
baseCFILES = \
|
baseCFILES = \
|
||||||
box.c \
|
box.c \
|
||||||
|
ptrarray.c \
|
||||||
$(osCFILES)
|
$(osCFILES)
|
||||||
|
|
||||||
baseMFILES = $(osMFILES)
|
baseMFILES = $(osMFILES)
|
||||||
|
|
1
TODO.md
1
TODO.md
|
@ -60,6 +60,7 @@
|
||||||
- readd the allocation tracking as just a list of pointers
|
- readd the allocation tracking as just a list of pointers
|
||||||
- add a final cleanup function (uiUninit() or uiCleanup())
|
- add a final cleanup function (uiUninit() or uiCleanup())
|
||||||
- whenever a list of things is destroyed, each successive item must be removed as it is destroyed, otherwise we might wind up in a situation where we access items after they're freed
|
- whenever a list of things is destroyed, each successive item must be removed as it is destroyed, otherwise we might wind up in a situation where we access items after they're freed
|
||||||
|
- make the name of the variable to refer to a single tab page consistent (already decided to make them all `page`)
|
||||||
|
|
||||||
ultimately:
|
ultimately:
|
||||||
- add some sort of runtime type checking
|
- add some sort of runtime type checking
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "uipriv.h"
|
#include "uipriv.h"
|
||||||
|
|
||||||
struct ptrArray newPtrArray(void)
|
struct ptrArray *newPtrArray(void)
|
||||||
{
|
{
|
||||||
return uiNew(struct ptrArray);
|
return uiNew(struct ptrArray);
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,6 @@ void ptrArrayAppend(struct ptrArray *p, void *d)
|
||||||
|
|
||||||
void ptrArrayInsertBefore(struct ptrArray *p, uintmax_t i, void *d)
|
void ptrArrayInsertBefore(struct ptrArray *p, uintmax_t i, void *d)
|
||||||
{
|
{
|
||||||
uintmax_t j;
|
|
||||||
|
|
||||||
if (i >= p->len)
|
if (i >= p->len)
|
||||||
complain("index out of range in ptrArrayInsertBefore()");
|
complain("index out of range in ptrArrayInsertBefore()");
|
||||||
if (p->len >= p->cap) {
|
if (p->len >= p->cap) {
|
||||||
|
@ -45,8 +43,6 @@ void ptrArrayInsertBefore(struct ptrArray *p, uintmax_t i, void *d)
|
||||||
|
|
||||||
void ptrArrayDelete(struct ptrArray *p, uintmax_t i)
|
void ptrArrayDelete(struct ptrArray *p, uintmax_t i)
|
||||||
{
|
{
|
||||||
uintmax_t j;
|
|
||||||
|
|
||||||
if (i >= p->len)
|
if (i >= p->len)
|
||||||
complain("index out of range in ptrArrayRemove()");
|
complain("index out of range in ptrArrayRemove()");
|
||||||
// thanks to ValleyBell
|
// thanks to ValleyBell
|
||||||
|
|
149
windows/tab.c
149
windows/tab.c
|
@ -4,15 +4,17 @@
|
||||||
struct tab {
|
struct tab {
|
||||||
uiTab t;
|
uiTab t;
|
||||||
HWND hwnd;
|
HWND hwnd;
|
||||||
uiContainer **pages;
|
struct ptrArray *pages;
|
||||||
int *margined;
|
|
||||||
uintmax_t len;
|
|
||||||
uintmax_t cap;
|
|
||||||
void (*baseEnable)(uiControl *);
|
void (*baseEnable)(uiControl *);
|
||||||
void (*baseDisable)(uiControl *);
|
void (*baseDisable)(uiControl *);
|
||||||
void (*baseSysFunc)(uiControl *, uiControlSysFuncParams *);
|
void (*baseSysFunc)(uiControl *, uiControlSysFuncParams *);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct tabPage {
|
||||||
|
uiContainer *bin;
|
||||||
|
int margined;
|
||||||
|
};
|
||||||
|
|
||||||
static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
|
static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
|
||||||
{
|
{
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -22,46 +24,50 @@ static BOOL onWM_COMMAND(uiControl *c, WORD code, LRESULT *lResult)
|
||||||
static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
|
static BOOL onWM_NOTIFY(uiControl *c, NMHDR *nm, LRESULT *lResult)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) c;
|
struct tab *t = (struct tab *) c;
|
||||||
|
struct tabPage *page;
|
||||||
LRESULT n;
|
LRESULT n;
|
||||||
|
|
||||||
switch (nm->code) {
|
if (nm->code != TCN_SELCHANGING && nm->code != TCN_SELCHANGE)
|
||||||
case TCN_SELCHANGING:
|
return FALSE;
|
||||||
n = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
n = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
||||||
if (n != (LRESULT) (-1)) // if we're changing to a real tab
|
if (n == (LRESULT) (-1)) // not changing from/to a page; nothing to do
|
||||||
uiControlHide(uiControl(t->pages[n]));
|
return FALSE;
|
||||||
|
page = ptrArrayIndex(t->pages, struct tabPage *, n);
|
||||||
|
if (nm->code == TCN_SELCHANGING) {
|
||||||
|
// changing from a real page
|
||||||
|
uiControlHide(uiControl(page->bin));
|
||||||
*lResult = FALSE; // and allow the change
|
*lResult = FALSE; // and allow the change
|
||||||
return TRUE;
|
return TRUE;
|
||||||
case TCN_SELCHANGE:
|
|
||||||
n = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
|
||||||
if (n != (LRESULT) (-1)) { // if we're changing to a real tab
|
|
||||||
uiControlShow(uiControl(t->pages[n]));
|
|
||||||
// because we only resize the current child on resize, we'll need to trigger an update here
|
|
||||||
// don't call uiParentUpdate(); doing that won't size the content area (so we'll still have a 0x0 content area, for instance)
|
|
||||||
SendMessageW(t->hwnd, msgUpdateChild, 0, 0);
|
|
||||||
}
|
|
||||||
*lResult = 0;
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
return FALSE;
|
// otherwise it's TCN_SELCHANGE
|
||||||
|
// and we're changing to a real page
|
||||||
|
uiControlShow(uiControl(page->bin));
|
||||||
|
// because we only resize the current child on resize, we'll need to trigger an update here
|
||||||
|
// don't call uiParentUpdate(); doing that won't size the content area (so we'll still have a 0x0 content area, for instance)
|
||||||
|
SendMessageW(t->hwnd, msgUpdateChild, 0, 0);
|
||||||
|
*lResult = 0;
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void onDestroy(void *data)
|
static void onDestroy(void *data)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) data;
|
struct tab *t = (struct tab *) data;
|
||||||
uintmax_t i;
|
struct tabPage *p;
|
||||||
|
|
||||||
// first, hide the widget to avoid flicker
|
// first, hide the widget to avoid flicker
|
||||||
ShowWindow(t->hwnd, SW_HIDE);
|
ShowWindow(t->hwnd, SW_HIDE);
|
||||||
// because the pages don't have by a libui paent, we can simply destroy them
|
// because the pages don't have by a libui paent, we can simply destroy them
|
||||||
// we don't have to worry about the Windows tab control holding a reference to our bin; there is no reference holding anyway
|
// we don't have to worry about the Windows tab control holding a reference to our bin; there is no reference holding anyway
|
||||||
for (i = 0; i < t->len; i++) {
|
while (t->pages->len != 0) {
|
||||||
|
p = ptrArrayIndex(t->pages, struct tabPage *, 0);
|
||||||
// we do have to remove the page from the tab control, though
|
// we do have to remove the page from the tab control, though
|
||||||
binSetParent(t->pages[i], 0);
|
binSetParent(p->bin, 0);
|
||||||
uiControlDestroy(uiControl(t->pages[i]));
|
uiControlDestroy(uiControl(p->bin));
|
||||||
|
ptrArrayDelete(t->pages, 0);
|
||||||
|
uiFree(p);
|
||||||
}
|
}
|
||||||
// and finally destroy ourselves
|
// and finally destroy ourselves
|
||||||
uiFree(t->pages);
|
ptrArrayDestroy(t->pages);
|
||||||
uiFree(t->margined);
|
|
||||||
uiFree(t);
|
uiFree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +75,7 @@ static void tabPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) c;
|
struct tab *t = (struct tab *) c;
|
||||||
LRESULT current;
|
LRESULT current;
|
||||||
uiContainer *curpage;
|
struct tabPage *curpage;
|
||||||
intmax_t curwid, curht;
|
intmax_t curwid, curht;
|
||||||
RECT r;
|
RECT r;
|
||||||
|
|
||||||
|
@ -77,11 +83,11 @@ static void tabPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_
|
||||||
r.top = 0;
|
r.top = 0;
|
||||||
r.right = 0;
|
r.right = 0;
|
||||||
r.bottom = 0;
|
r.bottom = 0;
|
||||||
if (t->len != 0) {
|
if (t->pages->len != 0) {
|
||||||
current = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
current = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
||||||
if (current != (LRESULT) (-1)) {
|
if (current != (LRESULT) (-1)) {
|
||||||
curpage = t->pages[current];
|
curpage = ptrArrayIndex(t->pages, struct tabPage *, current);
|
||||||
uiControlPreferredSize(uiControl(curpage), d, &curwid, &curht);
|
uiControlPreferredSize(uiControl(curpage->bin), d, &curwid, &curht);
|
||||||
r.right = curwid;
|
r.right = curwid;
|
||||||
r.bottom = curht;
|
r.bottom = curht;
|
||||||
}
|
}
|
||||||
|
@ -96,31 +102,40 @@ static void tabPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_
|
||||||
static void tabEnable(uiControl *c)
|
static void tabEnable(uiControl *c)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) c;
|
struct tab *t = (struct tab *) c;
|
||||||
|
struct tabPage *p;
|
||||||
uintmax_t i;
|
uintmax_t i;
|
||||||
|
|
||||||
(*(t->baseEnable))(uiControl(t));
|
(*(t->baseEnable))(uiControl(t));
|
||||||
for (i = 0; i < t->len; i++)
|
for (i = 0; i < t->pages->len; i++) {
|
||||||
uiControlEnable(uiControl(t->pages[i]));
|
p = ptrArrayIndex(t->pages, struct tabPage *, i);
|
||||||
|
uiControlEnable(uiControl(p->bin));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tabDisable(uiControl *c)
|
static void tabDisable(uiControl *c)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) c;
|
struct tab *t = (struct tab *) c;
|
||||||
|
struct tabPage *p;
|
||||||
uintmax_t i;
|
uintmax_t i;
|
||||||
|
|
||||||
(*(t->baseDisable))(uiControl(t));
|
(*(t->baseDisable))(uiControl(t));
|
||||||
for (i = 0; i < t->len; i++)
|
for (i = 0; i < t->pages->len; i++) {
|
||||||
uiControlDisable(uiControl(t->pages[i]));
|
p = ptrArrayIndex(t->pages, struct tabPage *, i);
|
||||||
|
uiControlDisable(uiControl(p->bin));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tabSysFunc(uiControl *c, uiControlSysFuncParams *p)
|
static void tabSysFunc(uiControl *c, uiControlSysFuncParams *p)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) c;
|
struct tab *t = (struct tab *) c;
|
||||||
|
struct tabPage *page;
|
||||||
uintmax_t i;
|
uintmax_t i;
|
||||||
|
|
||||||
(*(t->baseSysFunc))(uiControl(t), p);
|
(*(t->baseSysFunc))(uiControl(t), p);
|
||||||
for (i = 0; i < t->len; i++)
|
for (i = 0; i < t->pages->len; i++) {
|
||||||
uiControlSysFunc(uiControl(t->pages[i]), p);
|
page = ptrArrayIndex(t->pages, struct tabPage *, i);
|
||||||
|
uiControlSysFunc(uiControl(page->bin), p);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// common code for resizes
|
// common code for resizes
|
||||||
|
@ -128,6 +143,7 @@ static void resizeTab(struct tab *t, LONG width, LONG height)
|
||||||
{
|
{
|
||||||
LRESULT n;
|
LRESULT n;
|
||||||
RECT r;
|
RECT r;
|
||||||
|
struct tabPage *p;
|
||||||
HWND binHWND;
|
HWND binHWND;
|
||||||
|
|
||||||
n = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
n = SendMessageW(t->hwnd, TCM_GETCURSEL, 0, 0);
|
||||||
|
@ -143,7 +159,8 @@ static void resizeTab(struct tab *t, LONG width, LONG height)
|
||||||
// convert to the display rectangle
|
// convert to the display rectangle
|
||||||
SendMessageW(t->hwnd, TCM_ADJUSTRECT, FALSE, (LPARAM) (&r));
|
SendMessageW(t->hwnd, TCM_ADJUSTRECT, FALSE, (LPARAM) (&r));
|
||||||
|
|
||||||
binHWND = (HWND) uiControlHandle(uiControl(t->pages[n]));
|
p = ptrArrayIndex(t->pages, struct tabPage *, n);
|
||||||
|
binHWND = (HWND) uiControlHandle(uiControl(p->bin));
|
||||||
if (MoveWindow(binHWND, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE) == 0)
|
if (MoveWindow(binHWND, r.left, r.top, r.right - r.left, r.bottom - r.top, TRUE) == 0)
|
||||||
logLastError("error resizing uiTab page in resizeTab()");
|
logLastError("error resizing uiTab page in resizeTab()");
|
||||||
}
|
}
|
||||||
|
@ -187,24 +204,19 @@ static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
|
||||||
struct tab *t = (struct tab *) tt;
|
struct tab *t = (struct tab *) tt;
|
||||||
TCITEMW item;
|
TCITEMW item;
|
||||||
LRESULT n;
|
LRESULT n;
|
||||||
uiContainer *page;
|
struct tabPage *page;
|
||||||
WCHAR *wname;
|
WCHAR *wname;
|
||||||
|
|
||||||
if (t->len >= t->cap) {
|
page = uiNew(struct tabPage);
|
||||||
t->cap += tabCapGrow;
|
|
||||||
t->pages = (uiContainer **) uiRealloc(t->pages, t->cap * sizeof (uiContainer *));
|
|
||||||
t->margined = (int *) uiRealloc(t->margined, t->cap * sizeof (int));
|
|
||||||
}
|
|
||||||
|
|
||||||
n = SendMessageW(t->hwnd, TCM_GETITEMCOUNT, 0, 0);
|
n = SendMessageW(t->hwnd, TCM_GETITEMCOUNT, 0, 0);
|
||||||
|
|
||||||
page = newBin();
|
page->bin = newBin();
|
||||||
binSetMainControl(page, child);
|
binSetMainControl(page->bin, child);
|
||||||
binSetParent(page, (uintptr_t) (t->hwnd));
|
binSetParent(page->bin, (uintptr_t) (t->hwnd));
|
||||||
if (n != 0) // if this isn't the first page, we have to hide the other controls
|
if (n != 0) // if this isn't the first page, we have to hide the other controls
|
||||||
uiControlHide(uiControl(page));
|
uiControlHide(uiControl(page->bin));
|
||||||
t->pages[t->len] = page;
|
|
||||||
t->len++;
|
ptrArrayAppend(t->pages, page);
|
||||||
|
|
||||||
ZeroMemory(&item, sizeof (TCITEMW));
|
ZeroMemory(&item, sizeof (TCITEMW));
|
||||||
item.mask = TCIF_TEXT;
|
item.mask = TCIF_TEXT;
|
||||||
|
@ -224,8 +236,7 @@ static void tabAppendPage(uiTab *tt, const char *name, uiControl *child)
|
||||||
static void tabDeletePage(uiTab *tt, uintmax_t n)
|
static void tabDeletePage(uiTab *tt, uintmax_t n)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) tt;
|
struct tab *t = (struct tab *) tt;
|
||||||
uiContainer *page;
|
struct tabPage *page;
|
||||||
uintmax_t i;
|
|
||||||
|
|
||||||
// first delete the tab from the tab control
|
// first delete the tab from the tab control
|
||||||
// if this is the current tab, no tab will be selected, which is good
|
// if this is the current tab, no tab will be selected, which is good
|
||||||
|
@ -233,35 +244,32 @@ static void tabDeletePage(uiTab *tt, uintmax_t n)
|
||||||
logLastError("error deleting Tab page in tabDeletePage()");
|
logLastError("error deleting Tab page in tabDeletePage()");
|
||||||
|
|
||||||
// now delete the page itself
|
// now delete the page itself
|
||||||
page = t->pages[n];
|
page = ptrArrayIndex(t->pages, struct tabPage *, n);
|
||||||
for (i = n; i < t->len - 1; i++) {
|
ptrArrayDelete(t->pages, n);
|
||||||
t->pages[i] = t->pages[i + 1];
|
|
||||||
t->margined[i] = t->margined[i + 1];
|
|
||||||
}
|
|
||||||
t->pages[i] = NULL;
|
|
||||||
t->margined[i] = 0;
|
|
||||||
t->len--;
|
|
||||||
|
|
||||||
// make sure the page's control isn't destroyed
|
// make sure the page's control isn't destroyed
|
||||||
binSetMainControl(page, NULL);
|
binSetMainControl(page->bin, NULL);
|
||||||
|
|
||||||
// see tabDestroy() above for details
|
// see tabDestroy() above for details
|
||||||
binSetParent(page, 0);
|
binSetParent(page->bin, 0);
|
||||||
uiControlDestroy(uiControl(page));
|
uiControlDestroy(uiControl(page->bin));
|
||||||
|
uiFree(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uintmax_t tabNumPages(uiTab *tt)
|
static uintmax_t tabNumPages(uiTab *tt)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) tt;
|
struct tab *t = (struct tab *) tt;
|
||||||
|
|
||||||
return t->len;
|
return t->pages->len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tabMargined(uiTab *tt, uintmax_t n)
|
static int tabMargined(uiTab *tt, uintmax_t n)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) tt;
|
struct tab *t = (struct tab *) tt;
|
||||||
|
struct tabPage *page;
|
||||||
|
|
||||||
return t->margined[n];
|
page = ptrArrayIndex(t->pages, struct tabPage *, n);
|
||||||
|
return page->margined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
|
// from http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx
|
||||||
|
@ -270,12 +278,14 @@ static int tabMargined(uiTab *tt, uintmax_t n)
|
||||||
static void tabSetMargined(uiTab *tt, uintmax_t n, int margined)
|
static void tabSetMargined(uiTab *tt, uintmax_t n, int margined)
|
||||||
{
|
{
|
||||||
struct tab *t = (struct tab *) tt;
|
struct tab *t = (struct tab *) tt;
|
||||||
|
struct tabPage *page;
|
||||||
|
|
||||||
t->margined[n] = margined;
|
page = ptrArrayIndex(t->pages, struct tabPage *, n);
|
||||||
if (t->margined[n])
|
page->margined = margined;
|
||||||
binSetMargins(t->pages[n], tabMargin, tabMargin, tabMargin, tabMargin);
|
if (page->margined)
|
||||||
|
binSetMargins(page->bin, tabMargin, tabMargin, tabMargin, tabMargin);
|
||||||
else
|
else
|
||||||
binSetMargins(t->pages[n], 0, 0, 0, 0);
|
binSetMargins(page->bin, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uiTab *uiNewTab(void)
|
uiTab *uiNewTab(void)
|
||||||
|
@ -298,6 +308,7 @@ uiTab *uiNewTab(void)
|
||||||
uiWindowsMakeControl(uiControl(t), &p);
|
uiWindowsMakeControl(uiControl(t), &p);
|
||||||
|
|
||||||
t->hwnd = (HWND) uiControlHandle(uiControl(t));
|
t->hwnd = (HWND) uiControlHandle(uiControl(t));
|
||||||
|
t->pages = newPtrArray();
|
||||||
|
|
||||||
if ((*fv_SetWindowSubclass)(t->hwnd, tabSubProc, 0, (DWORD_PTR) t) == FALSE)
|
if ((*fv_SetWindowSubclass)(t->hwnd, tabSubProc, 0, (DWORD_PTR) t) == FALSE)
|
||||||
logLastError("error subclassing Tab to give it its own resize handler in uiNewTab()");
|
logLastError("error subclassing Tab to give it its own resize handler in uiNewTab()");
|
||||||
|
|
Loading…
Reference in New Issue