/*--------------------------------------------------------- WinTree2.cpp Use all cases for data functions for data handling. UNICODE Not fully supported in 98, so TCHAR will be used instead of char, windows will convert to char. If using fully supporting UNICODE OPSYS then convert all string calls to UNICODE ws*** string handlers. ---------------------------------------------------------*/ #include #include "WinTree2.h" // Holds classed, function definitions, and const. // Standard Petzold window WinMain... LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("WinTree2") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; RegisterClass (&wndclass) ; hwnd = CreateWindow (szAppName, TEXT ("Windows Last Left Threaded Tree 2.0"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } // Petzold WndProc Modified... LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static int cxChar, cxCaps, cyChar ; static TCHAR szBuffer_4[99] ; static TCHAR szBuffer_3[2] ; // szBuffer_* used to hold output, and special messages. static TCHAR szBuffer_2[80] ; static TCHAR szBuffer_1[2] ; HDC hdc ; PAINTSTRUCT ps ; // Standard Windows structors. TEXTMETRIC tm ; RECT rect ; rect.left = cxChar * 36 ; // Screen coordinates use in InvalidateRect (hwnd, &rect, TRUE) ; rect.top = 0 ; rect.right = cxChar * 160 ; rect.bottom = cyChar * 8 ; static TreeNode *pRoot = CreateTree(); // Tree is made up out of these classes. static TreeData *pTD = new TreeData(pRoot); // Holds Tree data, pointer to root... static bool fDoesTreeExist = FALSE ; static bool fTreeDataChanged ; static bool fDrawHeaders = TRUE ; int stop ; switch (message) { case WM_CREATE: // Petzold standard simple windows stuff. hdc = GetDC (hwnd) ; GetTextMetrics (hdc, &tm) ; cxChar = tm.tmAveCharWidth ; cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; cyChar = tm.tmHeight + tm.tmExternalLeading ; ReleaseDC (hwnd, hdc) ; // Null out the szBuffer_s at start up strcpy( szBuffer_1, "\0" ); strcpy( szBuffer_2, "\0" ); strcpy( szBuffer_3, "\0" ); strcpy( szBuffer_4, "\0" ); return 0 ; case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; // SetTextAlign (hdc, TA_RIGHT | TA_TOP) ; TextOut (hdc, cxChar * 36, cyChar * 2, szBuffer_1, strlen( szBuffer_1 ) ) ; TextOut (hdc, cxChar * 36, cyChar * 4, szBuffer_2, strlen( szBuffer_2 ) ) ; TextOut (hdc, cxChar * 36, cyChar * 6 , szBuffer_3, strlen( szBuffer_3 ) ) ; if( fDrawHeaders ) { // fDrawHeaders Set to FALSE in the WM_RBUTTONUP: so headers not redrawn, any other window activity will redraw headers along with rest of window. TextOut (hdc, cxChar, cyChar * 2, "Current Tree Root Data Member is. : ", strlen( "Current Tree Root Data Member is. : " ) ) ; TextOut (hdc, cxChar, cyChar * 4, "Current Tree Data Members are..... : ", strlen( "Current Tree Data Members are..... : " ) ) ; TextOut (hdc, cxChar, cyChar * 6, "Next Tree Members to be Removed.: ", strlen( "Next Tree Members to be Removed.: " ) ) ; TextOut (hdc, cxChar, cyChar * 10, "Last Left Threaded Data Tree is created at program start.", strlen( "Last Left Threaded Data Tree is created at program start." ) ) ; TextOut (hdc, cxChar, cyChar * 12, "Right click: display tree members.", strlen( "Right click: display tree members." ) ) ; TextOut (hdc, cxChar, cyChar * 13, "Left click : remove lest significant tree member.", strlen( "Left click : remove lest significant tree member." ) ) ; TextOut (hdc, cxChar, cyChar * 14, "Left Arrow = Left Click, Right Arrow = Right click", strlen( "Left Arrow = Left Click, Right Arrow = Right click" ) ) ; TextOut (hdc, cxChar, cyChar * 17, "Once the last data member is removed the tree no longer exist.", strlen( "Once the last data member is removed the tree no longer exist." ) ) ; } fDrawHeaders = TRUE ; // SetTextAlign (hdc, TA_LEFT | TA_TOP) ; EndPaint (hwnd, &ps) ; return 0 ; case WM_RBUTTONUP: // Right mouse button up will fetch the current tree data. strcpy( szBuffer_1, "\0" ); strcpy( szBuffer_2, "\0" ); strcpy( szBuffer_3, "\0" ); if( fDoesTreeExist = DoesTreeExist(pTD) ) // Check does tree exist function, if it does not exist, then { // no data to get. pTD->VoidOutCurrentTreeMembers() ; // pTD is pointer to class that is used to pick up all tree data. FetchAllTreeData(pTD) ; // null out the old tree data before you pick up the new. strcpy( szBuffer_1, ( ( pTD->FetchPointerToRoot() )->FetchTreeNodeData() ) ); // root data strcpy( szBuffer_2, pTD->FetchCurrentTreeMembers() ); // current tree data strcpy( szBuffer_3, ( (pTD->FetchPointerLestSignificantNode())->FetchTreeNodeData() ) ); // new lest sig node }else{ // Tree does not exist. strcpy( pTD->FetchCurrentTreeMembers(), "No Tree Exist!" ); // Yes this does work. strcpy( szBuffer_1, "\0" ); strcpy( szBuffer_2, pTD->FetchCurrentTreeMembers() ); strcpy( szBuffer_3, "\0" ); }// End of if...else... fDrawHeaders = FALSE ; // SO only draw the new tree data... InvalidateRect (hwnd, &rect, TRUE) ; // This will let windows paint the output area of the client space. UpdateWindow(hwnd); return 0 ; case WM_LBUTTONUP: /****************************************************************************** Left mouse button up will remove the lest significant node from the tree, then find and percolate the best candidate of the remaining nodes to take the place of the removed node. The tree's node pointers must be reordered when a node is removed, to maintain the tree's pointer integrity. *******************************************************************************/ if( fDoesTreeExist = DoesTreeExist(pTD) ) { pTD->VoidOutCurrentTreeMembers() ; FetchAllTreeData(pTD) ; RemoveLestSignificantNode(pTD); // Only call to RemoveLestSignificantNode in WinProc if( pTD->FetchPointerToRoot() != pRoot ) // If root node was removed/replaced reset WinProc's root pointer. pRoot = pTD->FetchPointerToRoot() ; pTD->VoidOutCurrentTreeMembers() ; if( fDoesTreeExist = DoesTreeExist(pTD) ) FetchAllTreeData(pTD) ; else { strcpy( szBuffer_1, " \0" ); strcpy( szBuffer_2, pTD->FetchCurrentTreeMembers() ); strcpy( szBuffer_3, " \0" ); } } // End of if... InvalidateRect (hwnd, &rect, TRUE) ; UpdateWindow(hwnd); return 0 ; case WM_KEYDOWN: stop = 1; switch (wParam) { case VK_LEFT: { SendMessage (hwnd, WM_LBUTTONUP, 0, 0 ); break ; } case VK_RIGHT: { SendMessage (hwnd, WM_RBUTTONUP, 0, 0 ); break ; } } return 0 ; case WM_DESTROY: delete pTD ; PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }