Counter Strike : Global Offensive Source Code
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.

316 lines
10 KiB

  1. //========= Copyright � 1996-2005, 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. int nRightOffset = 80;
  100. int nBottomOffset = 28;
  101. int nButtonWidth = 72;
  102. int nButtonHeight = 24;
  103. int nButtonGap = 80;
  104. if ( IsProportional() )
  105. {
  106. nRightOffset = scheme()->GetProportionalScaledValueEx( GetScheme(), nRightOffset );
  107. nBottomOffset = scheme()->GetProportionalScaledValueEx( GetScheme(), nBottomOffset );
  108. nButtonWidth = scheme()->GetProportionalScaledValueEx( GetScheme(), nButtonWidth );
  109. nButtonHeight = scheme()->GetProportionalScaledValueEx( GetScheme(), nButtonHeight );
  110. nButtonGap = scheme()->GetProportionalScaledValueEx( GetScheme(), nButtonGap );
  111. }
  112. // move the buttons to the bottom-right corner
  113. int xpos = x + wide - nRightOffset;
  114. int ypos = tall + y - nBottomOffset;
  115. if (_applyButton->IsVisible())
  116. {
  117. _applyButton->SetBounds(xpos, ypos, nButtonWidth, nButtonHeight );
  118. xpos -= nButtonGap;
  119. }
  120. if (_cancelButton->IsVisible())
  121. {
  122. _cancelButton->SetBounds(xpos, ypos, nButtonWidth, nButtonHeight );
  123. xpos -= nButtonGap;
  124. }
  125. _okButton->SetBounds(xpos, ypos, nButtonWidth, nButtonHeight );
  126. _propertySheet->InvalidateLayout(); // tell the propertysheet to redraw!
  127. Repaint();
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: Handles command text from the buttons
  131. //-----------------------------------------------------------------------------
  132. void PropertyDialog::OnCommand(const char *command)
  133. {
  134. if (!stricmp(command, "OK"))
  135. {
  136. if ( OnOK(false) )
  137. {
  138. OnCommand("Close");
  139. }
  140. _applyButton->SetEnabled(false);
  141. }
  142. else if (!stricmp(command, "Cancel"))
  143. {
  144. OnCancel();
  145. Close();
  146. }
  147. else if (!stricmp(command, "Apply"))
  148. {
  149. OnOK(true);
  150. _applyButton->SetEnabled(false);
  151. InvalidateLayout();
  152. }
  153. else
  154. {
  155. BaseClass::OnCommand(command);
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. // Purpose: called when the Cancel button is pressed
  160. //-----------------------------------------------------------------------------
  161. void PropertyDialog::OnCancel()
  162. {
  163. // designed to be overridden
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose:
  167. // Input : code -
  168. //-----------------------------------------------------------------------------
  169. void PropertyDialog::OnKeyCodeTyped(KeyCode code)
  170. {
  171. // this has been removed, since it conflicts with how we use the escape key in the game
  172. // if (code == KEY_ESCAPE)
  173. // {
  174. // OnCommand("Cancel");
  175. // }
  176. // else
  177. {
  178. BaseClass::OnKeyCodeTyped(code);
  179. }
  180. }
  181. //-----------------------------------------------------------------------------
  182. // Purpose: Command handler
  183. //-----------------------------------------------------------------------------
  184. bool PropertyDialog::OnOK(bool applyOnly)
  185. {
  186. // the sheet should have the pages apply changes before we tell the world
  187. _propertySheet->ApplyChanges();
  188. // this should tell anybody who's watching us that we're done
  189. PostActionSignal(new KeyValues("ApplyChanges"));
  190. // default to closing
  191. return true;
  192. }
  193. //-----------------------------------------------------------------------------
  194. // Purpose: Overrides build mode so it edits the sub panel
  195. //-----------------------------------------------------------------------------
  196. void PropertyDialog::ActivateBuildMode()
  197. {
  198. // no subpanel, no build mode
  199. EditablePanel *panel = dynamic_cast<EditablePanel *>(GetActivePage());
  200. if (!panel)
  201. return;
  202. panel->ActivateBuildMode();
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Purpose: sets the text on the OK/Cancel buttons, overriding the default
  206. //-----------------------------------------------------------------------------
  207. void PropertyDialog::SetOKButtonText(const char *text)
  208. {
  209. _okButton->SetText(text);
  210. }
  211. //-----------------------------------------------------------------------------
  212. // Purpose: sets the text on the OK/Cancel buttons, overriding the default
  213. //-----------------------------------------------------------------------------
  214. void PropertyDialog::SetCancelButtonText(const char *text)
  215. {
  216. _cancelButton->SetText(text);
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Purpose: sets the text on the apply buttons, overriding the default
  220. //-----------------------------------------------------------------------------
  221. void PropertyDialog::SetApplyButtonText(const char *text)
  222. {
  223. _applyButton->SetText(text);
  224. }
  225. //-----------------------------------------------------------------------------
  226. // Purpose: changes the visibility of the buttons
  227. //-----------------------------------------------------------------------------
  228. void PropertyDialog::SetOKButtonVisible(bool state)
  229. {
  230. _okButton->SetVisible(state);
  231. InvalidateLayout();
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose: changes the visibility of the buttons
  235. //-----------------------------------------------------------------------------
  236. void PropertyDialog::SetCancelButtonVisible(bool state)
  237. {
  238. _cancelButton->SetVisible(state);
  239. InvalidateLayout();
  240. }
  241. //-----------------------------------------------------------------------------
  242. // Purpose: changes the visibility of the buttons
  243. //-----------------------------------------------------------------------------
  244. void PropertyDialog::SetApplyButtonVisible(bool state)
  245. {
  246. _applyButton->SetVisible(state);
  247. InvalidateLayout();
  248. }
  249. //-----------------------------------------------------------------------------
  250. // Purpose: when a sheet changes, enable the apply button
  251. //-----------------------------------------------------------------------------
  252. void PropertyDialog::OnApplyButtonEnable()
  253. {
  254. if (_applyButton->IsEnabled())
  255. return;
  256. EnableApplyButton(true);
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Purpose: enable/disable the apply button
  260. //-----------------------------------------------------------------------------
  261. void PropertyDialog::EnableApplyButton(bool bEnable)
  262. {
  263. _applyButton->SetEnabled(bEnable);
  264. InvalidateLayout();
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Purpose:
  268. //-----------------------------------------------------------------------------
  269. void PropertyDialog::RequestFocus(int direction)
  270. {
  271. _propertySheet->RequestFocus(direction);
  272. }