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.

303 lines
9.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <vgui/KeyCode.h>
  8. #include <KeyValues.h>
  9. #include <vgui_controls/Button.h>
  10. #include <vgui_controls/PropertyDialog.h>
  11. #include <vgui_controls/PropertySheet.h>
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Constructor
  17. //-----------------------------------------------------------------------------
  18. PropertyDialog::PropertyDialog(Panel *parent, const char *panelName) : Frame(parent, panelName)
  19. {
  20. // create the property sheet
  21. _propertySheet = new PropertySheet(this, "Sheet");
  22. _propertySheet->AddActionSignalTarget(this);
  23. _propertySheet->SetTabPosition(1);
  24. // add the buttons
  25. _okButton = new Button(this, "OKButton", "#PropertyDialog_OK");
  26. _okButton->AddActionSignalTarget(this);
  27. _okButton->SetTabPosition(2);
  28. _okButton->SetCommand("OK");
  29. GetFocusNavGroup().SetDefaultButton(_okButton);
  30. _cancelButton = new Button(this, "CancelButton", "#PropertyDialog_Cancel");
  31. _cancelButton->AddActionSignalTarget(this);
  32. _cancelButton->SetTabPosition(3);
  33. _cancelButton->SetCommand("Cancel");
  34. _applyButton = new Button(this, "ApplyButton", "#PropertyDialog_Apply");
  35. _applyButton->AddActionSignalTarget(this);
  36. _applyButton->SetTabPosition(4);
  37. _applyButton->SetVisible(false); // default to not visible
  38. _applyButton->SetEnabled(false); // default to not enabled
  39. _applyButton->SetCommand("Apply");
  40. SetSizeable(false);
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: Destructor
  44. //-----------------------------------------------------------------------------
  45. PropertyDialog::~PropertyDialog()
  46. {
  47. }
  48. //-----------------------------------------------------------------------------
  49. // Purpose: Returns a pointer to the PropertySheet this dialog encapsulates
  50. // Output : PropertySheet *
  51. //-----------------------------------------------------------------------------
  52. PropertySheet *PropertyDialog::GetPropertySheet()
  53. {
  54. return _propertySheet;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Gets a pointer to the currently active page.
  58. // Output : Panel
  59. //-----------------------------------------------------------------------------
  60. Panel *PropertyDialog::GetActivePage()
  61. {
  62. return _propertySheet->GetActivePage();
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Wrapped function
  66. //-----------------------------------------------------------------------------
  67. void PropertyDialog::AddPage(Panel *page, const char *title)
  68. {
  69. _propertySheet->AddPage(page, title);
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose: reloads the data in all the property page
  73. //-----------------------------------------------------------------------------
  74. void PropertyDialog::ResetAllData()
  75. {
  76. _propertySheet->ResetAllData();
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: Applies any changes
  80. //-----------------------------------------------------------------------------
  81. void PropertyDialog::ApplyChanges()
  82. {
  83. OnCommand("Apply");
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: Sets up the sheet
  87. //-----------------------------------------------------------------------------
  88. void PropertyDialog::PerformLayout()
  89. {
  90. BaseClass::PerformLayout();
  91. int iBottom = m_iSheetInsetBottom;
  92. if ( IsProportional() )
  93. {
  94. iBottom = scheme()->GetProportionalScaledValueEx( GetScheme(), iBottom );
  95. }
  96. int x, y, wide, tall;
  97. GetClientArea(x, y, wide, tall);
  98. _propertySheet->SetBounds(x, y, wide, tall - iBottom);
  99. // move the buttons to the bottom-right corner
  100. int xpos = x + wide - 80;
  101. int ypos = tall + y - 28;
  102. if (_applyButton->IsVisible())
  103. {
  104. _applyButton->SetBounds(xpos, ypos, 72, 24);
  105. xpos -= 80;
  106. }
  107. if (_cancelButton->IsVisible())
  108. {
  109. _cancelButton->SetBounds(xpos, ypos, 72, 24);
  110. xpos -= 80;
  111. }
  112. _okButton->SetBounds(xpos, ypos, 72, 24);
  113. _propertySheet->InvalidateLayout(); // tell the propertysheet to redraw!
  114. Repaint();
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose: Handles command text from the buttons
  118. //-----------------------------------------------------------------------------
  119. void PropertyDialog::OnCommand(const char *command)
  120. {
  121. if (!stricmp(command, "OK"))
  122. {
  123. if ( OnOK(false) )
  124. {
  125. OnCommand("Close");
  126. }
  127. _applyButton->SetEnabled(false);
  128. }
  129. else if (!stricmp(command, "Cancel"))
  130. {
  131. OnCancel();
  132. Close();
  133. }
  134. else if (!stricmp(command, "Apply"))
  135. {
  136. OnOK(true);
  137. _applyButton->SetEnabled(false);
  138. InvalidateLayout();
  139. }
  140. else
  141. {
  142. BaseClass::OnCommand(command);
  143. }
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose: called when the Cancel button is pressed
  147. //-----------------------------------------------------------------------------
  148. void PropertyDialog::OnCancel()
  149. {
  150. // designed to be overridden
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. // Input : code -
  155. //-----------------------------------------------------------------------------
  156. void PropertyDialog::OnKeyCodeTyped(KeyCode code)
  157. {
  158. // this has been removed, since it conflicts with how we use the escape key in the game
  159. // if (code == KEY_ESCAPE)
  160. // {
  161. // OnCommand("Cancel");
  162. // }
  163. // else
  164. {
  165. BaseClass::OnKeyCodeTyped(code);
  166. }
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose: Command handler
  170. //-----------------------------------------------------------------------------
  171. bool PropertyDialog::OnOK(bool applyOnly)
  172. {
  173. // the sheet should have the pages apply changes before we tell the world
  174. _propertySheet->ApplyChanges();
  175. // this should tell anybody who's watching us that we're done
  176. PostActionSignal(new KeyValues("ApplyChanges"));
  177. // default to closing
  178. return true;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose: Overrides build mode so it edits the sub panel
  182. //-----------------------------------------------------------------------------
  183. void PropertyDialog::ActivateBuildMode()
  184. {
  185. // no subpanel, no build mode
  186. EditablePanel *panel = dynamic_cast<EditablePanel *>(GetActivePage());
  187. if (!panel)
  188. return;
  189. panel->ActivateBuildMode();
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Purpose: sets the text on the OK/Cancel buttons, overriding the default
  193. //-----------------------------------------------------------------------------
  194. void PropertyDialog::SetOKButtonText(const char *text)
  195. {
  196. _okButton->SetText(text);
  197. }
  198. //-----------------------------------------------------------------------------
  199. // Purpose: sets the text on the OK/Cancel buttons, overriding the default
  200. //-----------------------------------------------------------------------------
  201. void PropertyDialog::SetCancelButtonText(const char *text)
  202. {
  203. _cancelButton->SetText(text);
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Purpose: sets the text on the apply buttons, overriding the default
  207. //-----------------------------------------------------------------------------
  208. void PropertyDialog::SetApplyButtonText(const char *text)
  209. {
  210. _applyButton->SetText(text);
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Purpose: changes the visibility of the buttons
  214. //-----------------------------------------------------------------------------
  215. void PropertyDialog::SetOKButtonVisible(bool state)
  216. {
  217. _okButton->SetVisible(state);
  218. InvalidateLayout();
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Purpose: changes the visibility of the buttons
  222. //-----------------------------------------------------------------------------
  223. void PropertyDialog::SetCancelButtonVisible(bool state)
  224. {
  225. _cancelButton->SetVisible(state);
  226. InvalidateLayout();
  227. }
  228. //-----------------------------------------------------------------------------
  229. // Purpose: changes the visibility of the buttons
  230. //-----------------------------------------------------------------------------
  231. void PropertyDialog::SetApplyButtonVisible(bool state)
  232. {
  233. _applyButton->SetVisible(state);
  234. InvalidateLayout();
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Purpose: when a sheet changes, enable the apply button
  238. //-----------------------------------------------------------------------------
  239. void PropertyDialog::OnApplyButtonEnable()
  240. {
  241. if (_applyButton->IsEnabled())
  242. return;
  243. EnableApplyButton(true);
  244. }
  245. //-----------------------------------------------------------------------------
  246. // Purpose: enable/disable the apply button
  247. //-----------------------------------------------------------------------------
  248. void PropertyDialog::EnableApplyButton(bool bEnable)
  249. {
  250. _applyButton->SetEnabled(bEnable);
  251. InvalidateLayout();
  252. }
  253. //-----------------------------------------------------------------------------
  254. // Purpose:
  255. //-----------------------------------------------------------------------------
  256. void PropertyDialog::RequestFocus(int direction)
  257. {
  258. _propertySheet->RequestFocus(direction);
  259. }