libui/windows/table.cpp

366 lines
11 KiB
C++
Raw Normal View History

2018-05-29 19:26:48 -05:00
#include "uipriv_windows.hpp"
#include "table.hpp"
2018-05-29 19:26:48 -05:00
static uiTableTextColumnOptionalParams defaultTextColumnOptionalParams = {
/*TODO.ColorModelColumn = */-1,
};
2018-05-29 19:26:48 -05:00
uiTableModel *uiNewTableModel(uiTableModelHandler *mh)
{
uiTableModel *m;
m = uiprivNew(uiTableModel);
2018-05-29 19:26:48 -05:00
m->mh = mh;
m->tables = new std::vector<uiTable *>;
2018-05-29 19:26:48 -05:00
return m;
}
void uiFreeTableModel(uiTableModel *m)
{
delete m->tables;
uiprivFree(m);
2018-05-29 19:26:48 -05:00
}
// TODO document that when this is called, the model must return the new row count when asked
2018-05-29 19:26:48 -05:00
void uiTableModelRowInserted(uiTableModel *m, int newIndex)
{
LVITEMW item;
int newCount;
2018-05-29 19:26:48 -05:00
newCount = (*(m->mh->NumRows))(m->mh, m);
2018-05-29 19:26:48 -05:00
ZeroMemory(&item, sizeof (LVITEMW));
item.mask = 0;
item.iItem = newIndex;
item.iSubItem = 0;
for (auto t : *(m->tables)) {
// actually insert the rows
if (SendMessageW(t->hwnd, LVM_SETITEMCOUNT, (WPARAM) newCount, LVSICF_NOINVALIDATEALL) == 0)
logLastError(L"error calling LVM_SETITEMCOUNT in uiTableModelRowInserted()");
// and redraw every row from the new row down to simulate adding it
if (SendMessageW(t->hwnd, LVM_REDRAWITEMS, (WPARAM) newIndex, (LPARAM) (newCount - 1)) == FALSE)
logLastError(L"error calling LVM_REDRAWITEMS in uiTableModelRowInserted()");
// update selection state
2018-05-29 19:26:48 -05:00
if (SendMessageW(t->hwnd, LVM_INSERTITEM, 0, (LPARAM) (&item)) == (LRESULT) (-1))
logLastError(L"error calling LVM_INSERTITEM in uiTableModelRowInserted() to update selection state");
}
2018-05-29 19:26:48 -05:00
}
// TODO compare LVM_UPDATE and LVM_REDRAWITEMS
2018-05-29 19:26:48 -05:00
void uiTableModelRowChanged(uiTableModel *m, int index)
{
for (auto t : *(m->tables))
2018-05-29 19:26:48 -05:00
if (SendMessageW(t->hwnd, LVM_UPDATE, (WPARAM) index, 0) == (LRESULT) (-1))
logLastError(L"error calling LVM_UPDATE in uiTableModelRowChanged()");
}
// TODO document that when this is called, the model must return the OLD row count when asked
// TODO for this and the above, see what GTK+ requires and adjust accordingly
2018-05-29 19:26:48 -05:00
void uiTableModelRowDeleted(uiTableModel *m, int oldIndex)
{
int newCount;
newCount = (*(m->mh->NumRows))(m->mh, m);
newCount--;
for (auto t : *(m->tables)) {
// update selection state
2018-05-29 19:26:48 -05:00
if (SendMessageW(t->hwnd, LVM_DELETEITEM, (WPARAM) oldIndex, 0) == (LRESULT) (-1))
logLastError(L"error calling LVM_DELETEITEM in uiTableModelRowDeleted() to update selection state");
// actually delete the rows
if (SendMessageW(t->hwnd, LVM_SETITEMCOUNT, (WPARAM) newCount, LVSICF_NOINVALIDATEALL) == 0)
logLastError(L"error calling LVM_SETITEMCOUNT in uiTableModelRowDeleted()");
// and redraw every row from the new nth row down to simulate removing the old nth row
if (SendMessageW(t->hwnd, LVM_REDRAWITEMS, (WPARAM) oldIndex, (LPARAM) (newCount - 1)) == FALSE)
logLastError(L"error calling LVM_REDRAWITEMS in uiTableModelRowDeleted()");
}
2018-05-29 19:26:48 -05:00
}
// TODO explain all this
static LRESULT CALLBACK tableSubProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIDSubclass, DWORD_PTR dwRefData)
{
uiTable *t = (uiTable *) dwRefData;
switch (uMsg) {
case WM_TIMER:
if (wParam != (WPARAM) t)
break;
// TODO only increment and update if visible?
for (auto &i : *(t->indeterminatePositions)) {
i.second++;
// TODO check errors
SendMessageW(hwnd, LVM_UPDATE, (WPARAM) (i.first.first), 0);
}
return 0;
case WM_NCDESTROY:
if (RemoveWindowSubclass(hwnd, tableSubProc, uIDSubclass) == FALSE)
logLastError(L"RemoveWindowSubclass()");
// fall through
}
return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}
int uiprivTableProgress(uiTable *t, int item, int subitem, int modelColumn, LONG *pos)
{
uiTableData *data;
int progress;
std::pair<int, int> p;
std::map<std::pair<int, int>, LONG>::iterator iter;
bool startTimer = false;
bool stopTimer = false;
data = (*(t->model->mh->CellValue))(t->model->mh, t->model, item, modelColumn);
progress = uiTableDataInt(data);
uiFreeTableData(data);
p.first = item;
p.second = subitem;
iter = t->indeterminatePositions->find(p);
if (iter == t->indeterminatePositions->end()) {
if (progress == -1) {
startTimer = t->indeterminatePositions->size() == 0;
(*(t->indeterminatePositions))[p] = 0;
if (pos != NULL)
*pos = 0;
}
} else
if (progress != -1) {
t->indeterminatePositions->erase(p);
stopTimer = t->indeterminatePositions->size() == 0;
} else if (pos != NULL)
*pos = iter->second;
if (startTimer)
// the interval shown here is PBM_SETMARQUEE's default
// TODO should we pass a function here instead? it seems to be called by DispatchMessage(), not DefWindowProc(), but I'm still unsure
if (SetTimer(t->hwnd, (UINT_PTR) t, 30, NULL) == 0)
logLastError(L"SetTimer()");
if (stopTimer)
if (KillTimer(t->hwnd, (UINT_PTR) (&t)) == 0)
logLastError(L"KillTimer()");
return progress;
}
static BOOL onWM_NOTIFY(uiControl *c, HWND hwnd, NMHDR *nmhdr, LRESULT *lResult)
2018-05-29 19:26:48 -05:00
{
uiTable *t = uiTable(c);
HRESULT hr;
2018-05-29 19:26:48 -05:00
switch (nmhdr->code) {
case LVN_GETDISPINFO:
hr = uiprivTableHandleLVN_GETDISPINFO(t, (NMLVDISPINFOW *) nmhdr, lResult);
if (hr != S_OK) {
// TODO
return FALSE;
}
return TRUE;
case NM_CUSTOMDRAW:
hr = uiprivTableHandleNM_CUSTOMDRAW(t, (NMLVCUSTOMDRAW *) nmhdr, lResult);
if (hr != S_OK) {
// TODO
return FALSE;
}
return TRUE;
}
return FALSE;
2018-05-29 19:26:48 -05:00
}
static void uiTableDestroy(uiControl *c)
{
uiTable *t = uiTable(c);
uiTableModel *model = t->model;
std::vector<uiTable *>::iterator it;
uiWindowsUnregisterWM_NOTIFYHandler(t->hwnd);
uiWindowsEnsureDestroyWindow(t->hwnd);
// detach table from model
for (it = model->tables->begin(); it != model->tables->end(); it++) {
2018-05-29 19:26:48 -05:00
if (*it == t) {
model->tables->erase(it);
2018-05-29 19:26:48 -05:00
break;
}
}
// free the columns
for (auto col : *(t->columns))
2018-05-29 19:26:48 -05:00
uiprivFree(col);
delete t->columns;
// t->imagelist will be automatically destroyed
delete t->indeterminatePositions;
2018-05-29 19:26:48 -05:00
uiFreeControl(uiControl(t));
}
uiWindowsControlAllDefaultsExceptDestroy(uiTable)
2018-05-29 19:26:48 -05:00
// suggested listview sizing from http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing:
// "columns widths that avoid truncated data x an integral number of items"
// Don't think that'll cut it when some cells have overlong data (eg
// stupidly long URLs). So for now, just hardcode a minimum.
2018-06-11 07:01:18 -05:00
// TODO Investigate using LVM_GETHEADER/HDM_LAYOUT here
// TODO investigate using LVM_APPROXIMATEVIEWRECT here
2018-05-29 19:26:48 -05:00
#define tableMinWidth 107 /* in line with other controls */
#define tableMinHeight (14 * 3) /* header + 2 lines (roughly) */
2018-05-29 19:26:48 -05:00
static void uiTableMinimumSize(uiWindowsControl *c, int *width, int *height)
{
uiTable *t = uiTable(c);
uiWindowsSizing sizing;
int x, y;
x = tableMinWidth;
y = tableMinHeight;
uiWindowsGetSizing(t->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, &x, &y);
*width = x;
*height = y;
2018-06-08 20:45:30 -05:00
}
2018-05-29 19:26:48 -05:00
static uiprivTableColumnParams *appendColumn(uiTable *t, const char *name, int colfmt)
2018-05-29 19:26:48 -05:00
{
WCHAR *wstr;
LVCOLUMNW lvc;
uiprivTableColumnParams *p;
2018-05-29 19:26:48 -05:00
ZeroMemory(&lvc, sizeof (LVCOLUMNW));
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
lvc.fmt = colfmt;
lvc.cx = 120; // TODO
wstr = toUTF16(name);
lvc.pszText = wstr;
if (SendMessageW(t->hwnd, LVM_INSERTCOLUMNW, t->nColumns, (LPARAM) (&lvc)) == (LRESULT) (-1))
logLastError(L"error calling LVM_INSERTCOLUMNW in appendColumn()");
uiprivFree(wstr);
t->nColumns++;
p = uiprivNew(uiprivTableColumnParams);
p->textModelColumn = -1;
p->textEditableColumn = -1;
p->textParams = defaultTextColumnOptionalParams;
p->imageModelColumn = -1;
p->checkboxModelColumn = -1;
p->checkboxEditableColumn = -1;
p->progressBarModelColumn = -1;
p->buttonModelColumn = -1;
t->columns->push_back(p);
return p;
}
void uiTableAppendTextColumn(uiTable *t, const char *name, int textModelColumn, int textEditableModelColumn, uiTableTextColumnOptionalParams *params)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->textModelColumn = textModelColumn;
p->textEditableColumn = textEditableModelColumn;
if (params != NULL)
p->textParams = *params;
}
void uiTableAppendImageColumn(uiTable *t, const char *name, int imageModelColumn)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->imageModelColumn = imageModelColumn;
}
void uiTableAppendImageTextColumn(uiTable *t, const char *name, int imageModelColumn, int textModelColumn, int textEditableModelColumn, uiTableTextColumnOptionalParams *textParams)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->textModelColumn = textModelColumn;
p->textEditableColumn = textEditableModelColumn;
if (textParams != NULL)
p->textParams = *textParams;
p->imageModelColumn = imageModelColumn;
}
void uiTableAppendCheckboxColumn(uiTable *t, const char *name, int checkboxModelColumn, int checkboxEditableModelColumn)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->checkboxModelColumn = checkboxModelColumn;
p->checkboxEditableColumn = checkboxEditableModelColumn;
}
void uiTableAppendCheckboxTextColumn(uiTable *t, const char *name, int checkboxModelColumn, int checkboxEditableModelColumn, int textModelColumn, int textEditableModelColumn, uiTableTextColumnOptionalParams *textParams)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->textModelColumn = textModelColumn;
p->textEditableColumn = textEditableModelColumn;
if (textParams != NULL)
p->textParams = *textParams;
p->checkboxModelColumn = checkboxModelColumn;
p->checkboxEditableColumn = checkboxEditableModelColumn;
}
void uiTableAppendProgressBarColumn(uiTable *t, const char *name, int progressModelColumn)
{
uiprivTableColumnParams *p;
p = appendColumn(t, name, LVCFMT_LEFT);
p->progressBarModelColumn = progressModelColumn;
}
void uiTableAppendButtonColumn(uiTable *t, const char *name, int buttonTextModelColumn, int buttonClickableModelColumn)
{
uiprivTableColumnParams *p;
// TODO see if we can get rid of this parameter
p = appendColumn(t, name, LVCFMT_LEFT);
p->buttonModelColumn = buttonTextModelColumn;
p->buttonClickableModelColumn = buttonClickableModelColumn;
}
void uiTableSetRowBackgroundColorModelColumn(uiTable *t, int modelColumn)
{
// TODO make the names consistent
t->backgroundColumn = modelColumn;
// TODO redraw?
2018-05-29 19:26:48 -05:00
}
uiTable *uiNewTable(uiTableModel *model)
{
uiTable *t;
int n;
HRESULT hr;
2018-05-29 19:26:48 -05:00
uiWindowsNewControl(uiTable, t);
t->columns = new std::vector<uiprivTableColumnParams *>;
2018-05-29 19:26:48 -05:00
t->model = model;
t->hwnd = uiWindowsEnsureCreateControlHWND(WS_EX_CLIENTEDGE,
WC_LISTVIEW, L"",
LVS_REPORT | LVS_OWNERDATA | LVS_SINGLESEL | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL,
hInstance, NULL,
TRUE);
model->tables->push_back(t);
2018-05-29 19:26:48 -05:00
uiWindowsRegisterWM_NOTIFYHandler(t->hwnd, onWM_NOTIFY, uiControl(t));
// TODO: try LVS_EX_AUTOSIZECOLUMNS
// TODO check error
2018-05-29 19:26:48 -05:00
SendMessageW(t->hwnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
(WPARAM) (LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP | LVS_EX_SUBITEMIMAGES),
(LPARAM) (LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP | LVS_EX_SUBITEMIMAGES));
2018-05-29 19:26:48 -05:00
n = (*(model->mh->NumRows))(model->mh, model);
if (SendMessageW(t->hwnd, LVM_SETITEMCOUNT, (WPARAM) n, 0) == 0)
logLastError(L"error calling LVM_SETITEMCOUNT in uiNewTable()");
t->backgroundColumn = -1;
hr = uiprivUpdateImageListSize(t);
if (hr != S_OK) {
// TODO
}
t->indeterminatePositions = new std::map<std::pair<int, int>, LONG>;
if (SetWindowSubclass(t->hwnd, tableSubProc, 0, (DWORD_PTR) t) == FALSE)
logLastError(L"SetWindowSubclass()");
2018-05-29 19:26:48 -05:00
return t;
}