home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / treeview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  6.8 KB  |  295 lines

  1. #include "mviewpch.h"
  2.  
  3. #include <commdlg.h>
  4. #include <tchar.h>
  5. #include <commctrl.h>
  6.  
  7. #include "common.h"
  8. #include "treeview.h"
  9.  
  10. #ifdef _DEBUG
  11. static bool g_bInstanceCreated = false;
  12. #endif
  13.  
  14.  
  15. bool
  16. InitFrameViewClass(HINSTANCE hInstance)
  17. {
  18.     WNDCLASS wndclass;
  19.  
  20.     GXASSERT(!g_bInstanceCreated);
  21.  
  22.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  23.     wndclass.lpfnWndProc   = (WNDPROC) CFrameView::WndProc;
  24.     wndclass.cbClsExtra    = 0;
  25.     wndclass.cbWndExtra    = sizeof(void *);
  26.     wndclass.hInstance     = hInstance;
  27.     wndclass.hIcon         = (HICON) NULL;
  28.     wndclass.hCursor       = (HCURSOR) NULL;
  29.     wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  30.     wndclass.lpszMenuName  = NULL;
  31.     wndclass.lpszClassName = "FrameViewWindow";
  32.     if (! RegisterClass(&wndclass) )
  33.     {
  34.         return false;
  35.     }
  36.  
  37. #ifdef _DEBUG
  38.     g_bInstanceCreated = true;
  39. #endif
  40.  
  41.     return true;
  42. }
  43.  
  44.  
  45. HRESULT
  46. CreateFrameView(RECT &rTreeView, HWND hwndParent, HINSTANCE hInst, 
  47.                 PFNSELECTED pfnSelected, PVOID pvCallbackData, WNDPROC pfnKeyPress, CFrameView **ppfvNew)
  48. {
  49.     HRESULT hr = S_OK;
  50.     CFrameView *pfvNew = NULL;
  51.     HWND hwndFrame;
  52.  
  53.     GXASSERT(g_bInstanceCreated);
  54.  
  55.     pfvNew = new CFrameView();
  56.     if (pfvNew == NULL)
  57.     {
  58.         hr = E_OUTOFMEMORY;
  59.         goto e_Exit;
  60.     }
  61.  
  62.     hwndFrame = CreateWindow("FrameViewWindow", NULL,
  63.                                 WS_POPUP | WS_CAPTION | WS_CLIPCHILDREN | WS_SIZEBOX | WS_VISIBLE,
  64.                                 rTreeView.left, rTreeView.top,rTreeView.right - rTreeView.left, rTreeView.bottom - rTreeView.top,
  65.                                 hwndParent, NULL, hInst, NULL);
  66.     if (hwndFrame == NULL) 
  67.     {
  68.         MessageBox (NULL, "Failed to create the TreeView Frame window!", NULL, MB_OK );
  69.         return FALSE;
  70.     }
  71.     SetWindowLongPtr(hwndFrame, 0, (LONG_PTR)pfvNew);
  72.  
  73.     hr = pfvNew->Init(hwndFrame, hInst, pfnSelected, pfnKeyPress, pvCallbackData);
  74.     if (FAILED(hr))
  75.         goto e_Exit;
  76.  
  77.     *ppfvNew = pfvNew;
  78.     pfvNew = NULL;
  79.  
  80.     ShowWindow(hwndFrame, SW_HIDE);
  81.     //ShowWindow(hwndFrame, SW_SHOW);
  82. e_Exit:
  83.     delete pfvNew;
  84.  
  85.     return hr;
  86. }
  87.  
  88. BOOL
  89. CFrameView::ToggleVisible()
  90. {
  91.     m_bVisible = !m_bVisible;    
  92.     ShowWindow(m_hwndFrame, m_bVisible ? SW_SHOW : SW_HIDE );
  93.     ShowWindow(m_hwndTree, m_bVisible ? SW_SHOW : SW_HIDE );
  94.  
  95.     return m_bVisible;
  96. }
  97.  
  98. void
  99. CFrameView::GetWindowRect(RECT *pRect)
  100. {
  101.     ::GetWindowRect(m_hwndFrame, pRect);
  102. }
  103.  
  104. HRESULT
  105. CFrameView::Init(HWND hwndFrame, HINSTANCE hInst, PFNSELECTED pfnSelected, WNDPROC pfnKeyPress, PVOID pvCallbackData)
  106. {
  107.     HRESULT hr = S_OK;
  108.     RECT rTreeView;
  109.  
  110.     GetClientRect(hwndFrame, &rTreeView);
  111.  
  112.     InitCommonControls();
  113.  
  114.     m_hwndFrame = hwndFrame;
  115.     m_bVisible = FALSE;
  116.  
  117.     m_bCurrentlyDeleting = false;
  118.     m_iWidth = rTreeView.right - rTreeView.left;
  119.     m_iHeight = rTreeView.bottom - rTreeView.top;
  120.     m_pfnSelected = pfnSelected;    
  121.     m_pvCallbackData = pvCallbackData;
  122.     m_pfnKeyPress = pfnKeyPress;
  123.  
  124.     m_hwndTree = CreateWindowEx(0, WC_TREEVIEW, "Hierarchy",
  125.                       /*WS_VISIBLE | */WS_CHILD | WS_DLGFRAME | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_DISABLEDRAGDROP/*| TVS_EDITLABELS */| TVS_SHOWSELALWAYS,
  126.                       rTreeView.left, rTreeView.top, rTreeView.right, rTreeView.bottom,
  127.                       hwndFrame, (HMENU) NULL, hInst, NULL);
  128.     if (m_hwndTree == NULL)
  129.     {
  130.         hr = E_FAIL;
  131.         goto e_Exit;
  132.     }
  133.  
  134.     //InitTreeViewImageLists(hwndTV);
  135.  
  136. e_Exit:
  137.     return hr;
  138. }
  139.  
  140. CFrameView::~CFrameView()
  141. {
  142.     SetWindowLong(m_hwndFrame, 0, 0);
  143.  
  144.     DestroyWindow(m_hwndFrame);
  145. }
  146.  
  147. HTREEITEM
  148. CFrameView::InsertIntoTree(HTREEITEM htreeParent, char *szString, PVOID pvItemId)
  149. {
  150.     TV_INSERTSTRUCT ins;
  151.  
  152.     ins.hParent = htreeParent;
  153.     ins.hInsertAfter = TVI_LAST;
  154.     ins.item.mask = TVIF_TEXT | TVIF_PARAM;
  155.     ins.item.lParam = (LPARAM)pvItemId;
  156.     ins.item.cchTextMax = strlen(szString);
  157.     ins.item.pszText = szString;
  158.  
  159.     return TreeView_InsertItem(m_hwndTree, &ins);
  160. }
  161.  
  162. void
  163. CFrameView::SelectItem(HTREEITEM htreeSelect)
  164. {
  165.     TreeView_SelectItem(m_hwndTree, htreeSelect);
  166. }
  167.  
  168.  
  169. HRESULT
  170. CFrameView::RemoveAllItems()
  171. {
  172.     m_bCurrentlyDeleting = true;
  173.     TreeView_DeleteAllItems(m_hwndTree);
  174.     m_bCurrentlyDeleting = false;
  175.  
  176.     return S_OK;
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /*
  196.  * Handle messages for the viewer treeview frame window
  197.  */
  198. LRESULT WINAPI
  199. CFrameView::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
  200. {
  201.     CFrameView *pfv;
  202.  
  203.     pfv = (CFrameView *) GetWindowLongPtr(hwnd, 0);
  204.  
  205.     if (pfv != NULL)
  206.     {
  207.  
  208.         switch (msg) {
  209.         case WM_NOTIFY:
  210.             {
  211.                 LPNMHDR pnmh = (LPNMHDR) lparam;
  212.                 if (pnmh->hwndFrom == pfv->m_hwndTree) {
  213.                     LPNM_TREEVIEW lpnmtv = (LPNM_TREEVIEW) lparam;
  214.                     switch (pnmh->code) 
  215.                     {
  216.                     case TVN_SELCHANGED:
  217.                         {
  218.                             if (!pfv->m_bCurrentlyDeleting)
  219.                             {
  220.                                 pfv->m_pfnSelected(pfv->m_pvCallbackData, (PVOID)lpnmtv->itemNew.lParam);
  221.                             }
  222.                         }
  223.                         break;
  224.                     case TVN_KEYDOWN:
  225.                         NMTVKEYDOWN *tvKeyDown = (LPNMTVKEYDOWN)lparam;
  226.                         pfv->m_pfnKeyPress(hwnd, WM_KEYDOWN, tvKeyDown->wVKey, 0);
  227.                         return 1;
  228.  
  229.                     //case TVN_ITEMEXPANDED:
  230.                         //TreeViewExpandItem(&(lpnmtv->itemNew), lpnmtv->action & TVE_EXPAND);
  231.                         //break;
  232.                     }
  233.                 }
  234.             }
  235.             break;
  236.         case WM_KEYDOWN:
  237.             return pfv->m_pfnKeyPress(hwnd, msg, wparam, lparam);
  238.  
  239.         case WM_KEYUP:
  240.             return pfv->m_pfnKeyPress(hwnd, msg, wparam, lparam);
  241.  
  242.         case WM_WINDOWPOSCHANGING:
  243.     #if 0
  244.             {
  245.                 WINDOWPOS *pos = (WINDOWPOS *)lparam;
  246.                 RECT rc;
  247.                 GetClientRect(m_hwndParent, &rc);
  248.                 if (pos->x > 0)
  249.                     pos->cx = pfv->m_iWidth;
  250.                 pos->x = 0;
  251.                 pos->y = toolBarHt;
  252.                 if (pos->cy < (rc.bottom - rc.top - toolBarHt))
  253.                     pos->cy = rc.bottom - rc.top - toolBarHt;
  254.             }
  255.             return 0;
  256.     #endif
  257.             break;
  258.         case WM_SIZE:
  259.             {
  260.                 if (pfv == NULL)
  261.                     return 0;
  262.                 UINT iWidth = LOWORD(lparam);
  263.                 UINT iHeight = HIWORD(lparam);
  264.                 BOOL bSizeChanged = FALSE;
  265.  
  266.                 if (iWidth != pfv->m_iWidth) 
  267.                 {
  268.                     pfv->m_iWidth = iWidth;
  269.                     bSizeChanged = TRUE;
  270.                 }
  271.  
  272.                 if (iHeight != pfv->m_iHeight) 
  273.                 {
  274.                     pfv->m_iHeight = iHeight;
  275.                     bSizeChanged = TRUE;
  276.                 }
  277.  
  278.                 if (bSizeChanged) 
  279.                 {
  280.                     SetWindowPos(pfv->m_hwndTree, NULL, 0, 0, iWidth, iHeight, SWP_NOZORDER);
  281.                 }
  282.             }
  283.             break;
  284.  
  285.         case WM_DESTROY:
  286.         default:
  287.  
  288.             break;
  289.         
  290.         }
  291.     }
  292.  
  293.     return DefWindowProc(hwnd, msg, wparam, lparam);
  294. }
  295.