Seems I've spoken a bit to soon (who didn't see that coming?) ... It seems that all the child members of the tree are being changed. I only have one realloc and 2 malloc things though ... Here is all the code --
Code:
int DrawTree( HWND hwnd, TREESTRUCT *tree ) {
HDC dc = GetDC( hwnd );
HBRUSH brush = GetSysColorBrush( COLOR_WINDOW );
RECT rc;
GetClientRect( hwnd, &rc );
FillRect( dc, &rc, brush );
DeleteObject( brush );
for( int i = 0; i < tree->nNodes; i++ ) {
DrawText( dc, tree->nodes[i].lpText, -1, &tree->nodes[i].rc, DT_SINGLELINE | DT_LEFT | DT_VCENTER );
}
ReleaseDC( hwnd, dc );
return 0;
}
int SizeTree( HWND hwnd, TREESTRUCT *tree ) {
RECT rc = { 0, 0, 0, 0 };
for( int i = 0; i < tree->nNodes; i++ ) {
tree->nodes[i].rc.top = rc.bottom + tree->iSpaceY;
tree->nodes[i].rc.bottom = rc.bottom + tree->iCharHeight;
tree->nodes[i].rc.right = ( tree->iCharWidth * ( lstrlen( tree->nodes[i].lpText ) + 1 ) );
tree->nodes[i].rc.left = (tree->nodes[i].parent < 0 ? tree->iSpaceX :tree->nodes[tree->nodes[i].parent].rc.left + tree->iSpaceX);
rc.bottom = tree->nodes[i].rc.bottom;
}
return 0;
}
LRESULT CALLBACK TreeProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) {
TREESTRUCT *tree = (TREESTRUCT*)GetWindowLong( hwnd, 0 );
switch( msg ) {
case WM_CREATE: {
tree = (TREESTRUCT*)malloc( sizeof( TREESTRUCT ) );
SetWindowLong( hwnd, 0, (LONG)tree );
tree->bSet = false;
tree->nNodes = 0;
TEXTMETRIC tm;
HDC dc = GetDC( hwnd );
SetBkMode( dc, TRANSPARENT );
GetTextMetrics( dc, &tm );
ReleaseDC( hwnd, dc );
tree->iSpaceX = 2;
tree->iSpaceY = 2;
tree->iCharWidth = tm.tmAveCharWidth;
tree->iCharHeight = tm.tmHeight;
tree->nodes = (TREENODESTRUCT*)malloc( sizeof( TREENODESTRUCT ) );
return 0;
}
case M32_CREATENODE: {
tree->nNodes++;
TREENODESTRUCT* tmpPtr = (TREENODESTRUCT*)realloc( tree->nodes, sizeof( TREENODESTRUCT ) * tree->nNodes );
if( tmpPtr != NULL ) {
tree->nodes = tmpPtr;
free( tmpPtr );
}else{
//Panic
}
tree->nodes[tree->nNodes-1] = *(TREENODESTRUCT*)lParam;
SizeTree( hwnd, tree );
return 0;
}
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint( hwnd, &ps );
DrawTree( hwnd, tree );
EndPaint( hwnd, &ps );
}
case WM_DESTROY: {
free( tree );
return 0;
}
}
return DefWindowProc( hwnd, msg, wParam, lParam );
}
As you can see I'm trying to build a treeview control that works without recursion. The paint function is only a temp one till I get this damn bug sorted.