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.

296 lines
9.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "LocalizationDialog.h"
  8. #include "CreateTokenDialog.h"
  9. #include "vgui_controls/Button.h"
  10. #include "vgui_controls/ListPanel.h"
  11. #include"vgui_controls/TextEntry.h"
  12. #include "VGUI/IVGui.h"
  13. #include "VGUI/ILocalize.h"
  14. #include "VGUI/ISurface.h"
  15. #include "tier1/KeyValues.h"
  16. #include "vgui_controls/Menu.h"
  17. #include "vgui_controls/MenuButton.h"
  18. #include "vgui_controls/MessageBox.h"
  19. #include "vgui_controls/FileOpenDialog.h"
  20. #include <stdio.h>
  21. using namespace vgui;
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. //-----------------------------------------------------------------------------
  25. CLocalizationDialog::CLocalizationDialog(const char *fileName) : Frame(NULL, "LocalizationDialog")
  26. {
  27. m_iCurrentToken = -1;
  28. m_pTokenList = new ListPanel(this, "TokenList");
  29. m_pTokenList->AddColumnHeader(0, "Token", "Token Name", 128, 128, 1024, 0 );
  30. m_pLanguageEdit = new TextEntry(this, "LanguageEdit");
  31. m_pLanguageEdit->SetMultiline(true);
  32. m_pLanguageEdit->SetVerticalScrollbar(true);
  33. m_pLanguageEdit->SetCatchEnterKey(true);
  34. m_pEnglishEdit = new TextEntry(this, "EnglishEdit");
  35. m_pEnglishEdit->SetMultiline(true);
  36. m_pEnglishEdit->SetVerticalScrollbar(true);
  37. m_pEnglishEdit->SetVerticalScrollbar(true);
  38. m_pFileMenu = new Menu(this, "FileMenu");
  39. m_pFileMenu->AddMenuItem(" &Open File ", new KeyValues("FileOpen"), this);
  40. m_pFileMenu->AddMenuItem(" &Save File ", new KeyValues("FileSave"), this);
  41. m_pFileMenu->AddMenuItem(" E&xit Localizer ", new KeyValues("Close"), this);
  42. m_pFileMenuButton = new MenuButton(this, "FileMenuButton", "File");
  43. m_pFileMenuButton->SetMenu(m_pFileMenu);
  44. m_pApplyButton = new Button(this, "ApplyButton", "Apply");
  45. m_pApplyButton->SetCommand(new KeyValues("ApplyChanges"));
  46. m_pTestLabel = new Label(this, "TestLabel", "");
  47. LoadControlSettings("Resource/LocalizationDialog.res");
  48. strcpy(m_szFileName, fileName);
  49. char buf[512];
  50. Q_snprintf(buf, sizeof( buf ), "%s - Localization Editor", m_szFileName);
  51. SetTitle(buf, true);
  52. // load in the string table
  53. if (!g_pVGuiLocalize->AddFile( m_szFileName ) )
  54. {
  55. MessageBox *msg = new MessageBox("Fatal error", "couldn't load specified file");
  56. msg->SetCommand("Close");
  57. msg->AddActionSignalTarget(this);
  58. msg->DoModal();
  59. return;
  60. }
  61. // populate the dialog with the strings
  62. StringIndex_t idx = g_pVGuiLocalize->GetFirstStringIndex();
  63. while ( idx != INVALID_LOCALIZE_STRING_INDEX )
  64. {
  65. // adds the strings into the table, along with the indexes
  66. m_pTokenList->AddItem(new KeyValues("LString", "Token", g_pVGuiLocalize->GetNameByIndex(idx)), idx, false, false);
  67. // move to the next string
  68. idx = g_pVGuiLocalize->GetNextStringIndex(idx);
  69. }
  70. // sort the table
  71. m_pTokenList->SetSortColumn(0);
  72. m_pTokenList->SortList();
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Destructor
  76. //-----------------------------------------------------------------------------
  77. CLocalizationDialog::~CLocalizationDialog()
  78. {
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose: Handles closing of the dialog - shuts down the whole app
  82. //-----------------------------------------------------------------------------
  83. void CLocalizationDialog::OnClose()
  84. {
  85. BaseClass::OnClose();
  86. // Stop vgui running
  87. vgui::ivgui()->Stop();
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: lays out the dialog
  91. //-----------------------------------------------------------------------------
  92. void CLocalizationDialog::PerformLayout()
  93. {
  94. OnTextChanged();
  95. BaseClass::PerformLayout();
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Purpose: Sets the currently selected token
  99. //-----------------------------------------------------------------------------
  100. void CLocalizationDialog::OnTokenSelected()
  101. {
  102. if (m_pTokenList->GetSelectedItemsCount() != 1)
  103. {
  104. // clear the list
  105. m_pLanguageEdit->SetText("");
  106. m_pEnglishEdit->SetText("");
  107. //!! unicode test label
  108. m_pTestLabel->SetText("");
  109. m_iCurrentToken = -1;
  110. }
  111. else
  112. {
  113. // get the data
  114. int itemId = m_pTokenList->GetSelectedItem(0);
  115. vgui::ListPanelItem *data = m_pTokenList->GetItemData( itemId );
  116. Assert( data );
  117. m_iCurrentToken = data->userData;
  118. wchar_t *unicodeString = g_pVGuiLocalize->GetValueByIndex(m_iCurrentToken);
  119. char value[2048];
  120. g_pVGuiLocalize->ConvertUnicodeToANSI(unicodeString, value, sizeof(value));
  121. //!! unicode test label
  122. m_pTestLabel->SetText(unicodeString);
  123. // set the text
  124. m_pLanguageEdit->SetText(value);
  125. m_pEnglishEdit->SetText(value);
  126. }
  127. m_pApplyButton->SetEnabled(false);
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose: Checks to see if any text has changed
  131. //-----------------------------------------------------------------------------
  132. void CLocalizationDialog::OnTextChanged()
  133. {
  134. static char buf1[1024], buf2[1024];
  135. m_pLanguageEdit->GetText( buf1, sizeof( buf1 ) );
  136. m_pEnglishEdit->GetText( buf2, sizeof( buf2 ) );
  137. if (!strcmp(buf1, buf2))
  138. {
  139. m_pApplyButton->SetEnabled(false);
  140. }
  141. else
  142. {
  143. m_pApplyButton->SetEnabled(true);
  144. }
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose: Copies any changes made into the main database
  148. //-----------------------------------------------------------------------------
  149. void CLocalizationDialog::OnApplyChanges()
  150. {
  151. if (m_iCurrentToken < 0)
  152. return;
  153. static char buf1[1024];
  154. static wchar_t unicodeString[1024];
  155. m_pLanguageEdit->GetText( buf1, sizeof( buf1 ) );
  156. g_pVGuiLocalize->ConvertANSIToUnicode(buf1, unicodeString, sizeof(unicodeString) / sizeof(wchar_t));
  157. //!! unicode test label
  158. m_pTestLabel->SetText(unicodeString);
  159. // apply the text change to the database
  160. g_pVGuiLocalize->SetValueByIndex(m_iCurrentToken, unicodeString);
  161. // disable the apply button
  162. m_pApplyButton->SetEnabled(false);
  163. // reselect the token
  164. OnTokenSelected();
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Purpose: Message handler for saving current file
  168. //-----------------------------------------------------------------------------
  169. void CLocalizationDialog::OnFileSave()
  170. {
  171. if (g_pVGuiLocalize->SaveToFile( m_szFileName ) )
  172. {
  173. // success
  174. MessageBox *box = new MessageBox("Save Successful - VLocalize", "File was successfully saved.", false);
  175. box->DoModal();
  176. }
  177. else
  178. {
  179. // failure
  180. MessageBox *box = new MessageBox("Error during save - VLocalize", "Error - File was not successfully saved.", false);
  181. box->DoModal();
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. // Purpose: Message handler for loading a file
  186. //-----------------------------------------------------------------------------
  187. void CLocalizationDialog::OnFileOpen()
  188. {
  189. FileOpenDialog *box = new FileOpenDialog( this, "Open", true );
  190. box->SetStartDirectory("u:\\");
  191. box->AddFilter("*.*", "All Files (*.*)", true );
  192. box->DoModal(false);
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose: Handles a token created message
  196. // Input : *tokenName - the name of the newly created token
  197. //-----------------------------------------------------------------------------
  198. void CLocalizationDialog::OnTokenCreated(const char *tokenName)
  199. {
  200. // add the new string table token to the token list
  201. int idx = g_pVGuiLocalize->FindIndex(tokenName);
  202. int itemId = m_pTokenList->AddItem(new KeyValues("LString", "Token", g_pVGuiLocalize->GetNameByIndex(idx)), idx, true, true );
  203. // make that currently selected
  204. m_pTokenList->SetSingleSelectedItem( itemId );
  205. OnTokenSelected();
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose: Creates a new token
  209. //-----------------------------------------------------------------------------
  210. void CLocalizationDialog::OnCreateToken()
  211. {
  212. CCreateTokenDialog *dlg = new CCreateTokenDialog( this );
  213. dlg->AddActionSignalTarget(this);
  214. dlg->CreateSingleToken();
  215. }
  216. char const *CLocalizationDialog::GetFileName() const
  217. {
  218. return m_szFileName;
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Purpose:
  222. // Input : *command -
  223. //-----------------------------------------------------------------------------
  224. void CLocalizationDialog::OnCommand(const char *command)
  225. {
  226. if (!stricmp(command, "CreateToken"))
  227. {
  228. OnCreateToken();
  229. }
  230. else
  231. {
  232. BaseClass::OnCommand(command);
  233. }
  234. }
  235. //-----------------------------------------------------------------------------
  236. // Purpose: empty message map
  237. //-----------------------------------------------------------------------------
  238. MessageMapItem_t CLocalizationDialog::m_MessageMap[] =
  239. {
  240. MAP_MESSAGE( CLocalizationDialog, "RowSelected", OnTokenSelected ), // message from the m_pTokenList
  241. MAP_MESSAGE( CLocalizationDialog, "TextChanged", OnTextChanged ), // message from the text entry
  242. MAP_MESSAGE( CLocalizationDialog, "ApplyChanges", OnApplyChanges ), // message from the text entry
  243. MAP_MESSAGE( CLocalizationDialog, "FileSave", OnFileSave ),
  244. MAP_MESSAGE( CLocalizationDialog, "FileOpen", OnFileOpen ),
  245. MAP_MESSAGE_CONSTCHARPTR( CLocalizationDialog, "TokenCreated", OnTokenCreated, "name" ),
  246. };
  247. IMPLEMENT_PANELMAP(CLocalizationDialog, BaseClass);