Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
5.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "workspacebrowser.h"
  8. #include "workspace.h"
  9. #include "project.h"
  10. #include <windows.h>
  11. #include "resource.h"
  12. #include "project.h"
  13. #include "vcdfile.h"
  14. #include "soundentry.h"
  15. #include "scene.h"
  16. #include "workspacemanager.h"
  17. enum
  18. {
  19. IDC_WSB_TREE = 101,
  20. };
  21. class CBrowserTree : public mxTreeView
  22. {
  23. public:
  24. CBrowserTree( mxWindow *parent, int id = 0 )
  25. : mxTreeView( parent, 0, 0, 0, 0, id )
  26. {
  27. // SendMessage ( (HWND)getHandle(), WM_SETFONT, (WPARAM) (HFONT) GetStockObject (ANSI_FIXED_FONT), MAKELPARAM (TRUE, 0));
  28. }
  29. };
  30. int CALLBACK CWorkspaceBrowser::CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
  31. {
  32. ITreeItem const *item1 = (ITreeItem *)lParam1;
  33. ITreeItem const *item2 = (ITreeItem *)lParam2;
  34. Assert( item1 && item2 );
  35. if ( item1->GetOrdinal() < item2->GetOrdinal() )
  36. return -1;
  37. else if ( item1->GetOrdinal() > item2->GetOrdinal() )
  38. return 1;
  39. // Ok, just compare pointers... sigh
  40. return ( item1 < item2 ) ? -1 : 1;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. // Input : *parent -
  45. //-----------------------------------------------------------------------------
  46. CWorkspaceBrowser::CWorkspaceBrowser( mxWindow *parent, CWorkspaceManager *manager, int id ) :
  47. BaseClass( parent, 0, 0, 0, 0, "Workspace Browser", id )
  48. {
  49. m_pManager = manager;
  50. SceneManager_MakeToolWindow( this, false );
  51. m_pLastSelected = NULL;
  52. m_pCurrentWorkspace = NULL;
  53. m_pTree = new CBrowserTree( this, IDC_WSB_TREE );
  54. HIMAGELIST list = GetWorkspaceManager()->CreateImageList();
  55. // Associate the image list with the tree-view control.
  56. m_pTree->setImageList( (void *)list );
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. // Input : *event -
  61. // Output : int
  62. //-----------------------------------------------------------------------------
  63. int CWorkspaceBrowser::handleEvent( mxEvent *event )
  64. {
  65. int iret = 0;
  66. switch ( event->event )
  67. {
  68. default:
  69. break;
  70. case mxEvent::Action:
  71. {
  72. switch ( event->action )
  73. {
  74. default:
  75. break;
  76. case IDC_WSB_TREE:
  77. {
  78. iret = 1;
  79. bool rightmouse = ( event->flags == mxEvent::RightClicked ) ? true : false;
  80. bool doubleclicked = ( event->flags == mxEvent::DoubleClicked ) ? true : false;
  81. OnTreeItemSelected( event->x, event->y, rightmouse, doubleclicked );
  82. }
  83. break;
  84. }
  85. }
  86. break;
  87. case mxEvent::Size:
  88. {
  89. m_pTree->setBounds( 0, 0, w2(), h2() );
  90. GetWorkspaceManager()->SetWorkspaceDirty();
  91. iret = 1;
  92. }
  93. break;
  94. case mxEvent::Close:
  95. {
  96. iret = 1;
  97. }
  98. break;
  99. }
  100. return iret;
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. // Output : CWorkspace
  105. //-----------------------------------------------------------------------------
  106. CWorkspace *CWorkspaceBrowser::GetWorkspace()
  107. {
  108. return m_pCurrentWorkspace;
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose:
  112. //-----------------------------------------------------------------------------
  113. void CWorkspaceBrowser::PopulateTree()
  114. {
  115. m_pLastSelected = NULL;
  116. CWorkspace *w = m_pCurrentWorkspace;
  117. if ( !w )
  118. return;
  119. // Add root element
  120. char sz[ 128 ];
  121. if ( w->GetProjectCount() == 1 )
  122. {
  123. Q_snprintf( sz, sizeof( sz ), "Workspace '%s': %i project", w->GetName(), w->GetProjectCount() );
  124. }
  125. else
  126. {
  127. Q_snprintf( sz, sizeof( sz ), "Workspace '%s': %i projects", w->GetName(), w->GetProjectCount() );
  128. }
  129. mxTreeViewItem *root = w->FindItem( m_pTree, NULL );
  130. if ( !root )
  131. {
  132. root = m_pTree->add( NULL, sz );
  133. }
  134. // Reset the label
  135. m_pTree->setLabel( root, sz );
  136. m_pTree->setImages( root, w->GetIconIndex(), w->GetIconIndex() );
  137. m_pTree->setUserData( root, w );
  138. w->ValidateTree( m_pTree, root );
  139. m_pTree->setOpen( root, true );
  140. m_pTree->sortTree( root, true, CompareFunc, 0 );
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Purpose:
  144. // Input : *w -
  145. //-----------------------------------------------------------------------------
  146. void CWorkspaceBrowser::SetWorkspace( CWorkspace *w )
  147. {
  148. m_pCurrentWorkspace = w;
  149. PopulateTree();
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose:
  153. // Input : *project -
  154. //-----------------------------------------------------------------------------
  155. void CWorkspaceBrowser::AddProject( CProject *project )
  156. {
  157. if ( !m_pCurrentWorkspace )
  158. return;
  159. m_pCurrentWorkspace->AddProject( project );
  160. PopulateTree();
  161. }
  162. ITreeItem *CWorkspaceBrowser::GetSelectedItem()
  163. {
  164. return m_pLastSelected;
  165. }
  166. CWorkspaceManager *CWorkspaceBrowser::GetManager()
  167. {
  168. return m_pManager;
  169. }
  170. void CWorkspaceBrowser::OnTreeItemSelected( int x, int y, bool rightmouse, bool doubleclick )
  171. {
  172. mxTreeViewItem *item = m_pTree->getSelectedItem();
  173. if ( !item )
  174. {
  175. Con_Printf( "No item selected\n" );
  176. return;
  177. }
  178. ITreeItem *p = (ITreeItem *)m_pTree->getUserData( item );
  179. if ( !p )
  180. {
  181. Con_Printf( "No userdata for item\n" );
  182. return;
  183. }
  184. m_pLastSelected = p;
  185. GetManager()->UpdateMenus();
  186. if ( p->GetSoundEntry() || p->GetWaveFile() )
  187. {
  188. GetManager()->OnSoundShowInBrowsers();
  189. }
  190. if ( !rightmouse )
  191. {
  192. if ( doubleclick )
  193. {
  194. GetManager()->OnDoubleClicked( GetSelectedItem() );
  195. }
  196. return;
  197. }
  198. POINT pt;
  199. GetCursorPos( &pt );
  200. ScreenToClient( (HWND)GetManager()->getHandle(), &pt );
  201. GetManager()->ShowContextMenu( pt.x, pt.y, GetSelectedItem() );
  202. }
  203. void CWorkspaceBrowser::JumpTo( ITreeItem *item )
  204. {
  205. mxTreeViewItem *found = item->FindItem( m_pTree, NULL, true );
  206. if ( found )
  207. {
  208. m_pTree->scrollTo( found );
  209. }
  210. }