Skip to content

Commit

Permalink
Fixed allocation of TreeItem without ID eclipse-platform#822
Browse files Browse the repository at this point in the history
  • Loading branch information
basilevs committed Nov 19, 2023
1 parent fb6640e commit 6376da1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,13 @@ TreeItem _getItem (long iter) {
return items [id];
}

TreeItem _getItem (int index) {
long parentHandle = 0;
TreeItem _getItem (TreeItem parentItem, int index) {
long iter = OS.g_malloc (GTK.GtkTreeIter_sizeof ());
GTK.gtk_tree_model_iter_nth_child(modelHandle, iter, parentHandle, index);
GTK.gtk_tree_model_iter_nth_child(modelHandle, iter, parentItem == null ? 0 : parentItem.handle, index);
int id = getId (iter, true);
OS.g_free (iter);
if (items [id] != null) return items [id];
return items [id] = new TreeItem (this, null, SWT.NONE, index, false);
return items [id] = new TreeItem (this, parentItem, SWT.NONE, index, false);
}

void reallocateIds(int newSize) {
Expand Down Expand Up @@ -244,9 +243,10 @@ int findAvailableId() {

int getId (long iter, boolean queryModel) {
if (queryModel) {
int[] value = new int[1];
GTK.gtk_tree_model_get (modelHandle, iter, ID_COLUMN, value, -1);
if (value [0] != -1) return value [0];
int id = queryItemId(iter);
if (id >= 0) {
return id;
}
}

int id = findAvailableId();
Expand Down Expand Up @@ -1749,7 +1749,7 @@ public TreeItem getItem (int index) {
if (!(0 <= index && index < GTK.gtk_tree_model_iter_n_children (modelHandle, 0))) {
error (SWT.ERROR_INVALID_RANGE);
}
return _getItem (index);
return _getItem (null, index);
}

/**
Expand Down Expand Up @@ -1926,7 +1926,7 @@ int getItemHeightInPixels () {
if (length == 0) return result;
if ((style & SWT.VIRTUAL) != 0) {
for (int i=0; i<length; i++) {
result [i] = _getItem (i);
result [i] = _getItem (null, i);
}
} else {
int i = 0;
Expand All @@ -1937,7 +1937,7 @@ int getItemHeightInPixels () {
GTK.gtk_tree_model_get (modelHandle, iter, ID_COLUMN, index, -1);
TreeItem item;
if (index[0] < 0) {
item = new TreeItem(this, null, SWT.NONE, i, false);
item = _getItem(null, i);
} else {
item = items [index [0]];
}
Expand Down Expand Up @@ -4347,4 +4347,27 @@ public void dispose() {
headerCSSProvider = 0;
}
}

int queryItemId(long itemIter) {
int[] value = new int[1];
GTK.gtk_tree_model_get (modelHandle, itemIter, ID_COLUMN, value, -1);
return value [0];
}

boolean verifyItem(TreeItem item) {
if (item == null) {
return false;
}
int id = queryItemId(item.handle);
if (id < 0) {
return false;
}
if (items[id] == null) {
return false;
}
if (items[id] != item) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ public TreeItem (TreeItem parentItem, int style, int index) {
parentItem.items.add(index, this);
} else {
ensureSizeAtLeast(parentItem.items, index + 1);
parentItem.items.set(index, this);
TreeItem old = parentItem.items.set(index, this);
assert old == null;
if (old != null) {
old.dispose();
}
}
}
if (create) {
Expand Down Expand Up @@ -831,7 +835,7 @@ public TreeItem getItem (int index) {
if (index >= itemCount) error (SWT.ERROR_INVALID_RANGE);
TreeItem result = items.get(index);
if (result == null) {
items.set(index, result = new TreeItem(parent, this, SWT.NONE, index, false));
items.set(index, result = parent._getItem(this, index));
}
return result;
}
Expand All @@ -858,11 +862,10 @@ public TreeItem getItem (int index) {
for (ListIterator<TreeItem> i = items.listIterator(); i.hasNext();) {
TreeItem next = i.next();
if (next == null) {
new TreeItem(parent, this, SWT.NONE, i.previousIndex(), false);
i.set(parent._getItem(this, i.previousIndex()));
}
}
return items.toArray(new TreeItem[items.size()]);

}

@Override
Expand Down Expand Up @@ -1774,4 +1777,10 @@ public void setText (String [] strings) {
if (string != null) setText (i, string);
}
}

@Override
protected void checkWidget() {
super.checkWidget();
assert parent.verifyItem(this);
}
}

0 comments on commit 6376da1

Please sign in to comment.