1
0

settings-basic-general.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-app.hpp"
  15. #include "settings-basic.hpp"
  16. #include "window-settings-basic.hpp"
  17. #include "wx-wrappers.hpp"
  18. #include "platform.hpp"
  19. class BasicGenData : public BasicSettingsData {
  20. ConfigFile localeIni;
  21. WXConnector languageBoxConnector;
  22. void LanguageChanged(wxCommandEvent &event);
  23. int AddLanguage(const char *tag);
  24. void FillLanguageList(const char *currentLang);
  25. public:
  26. BasicGenData(OBSBasicSettings *window);
  27. virtual void Apply();
  28. };
  29. class LanguageInfo : public wxClientData {
  30. public:
  31. const char *tag;
  32. const char *name;
  33. const char *subLang;
  34. bool isDefault;
  35. inline LanguageInfo(config_t config, const char *tag)
  36. : wxClientData (),
  37. tag (tag),
  38. name (config_get_string(config, tag, "Name")),
  39. subLang (config_get_string(config, tag, "SubLang")),
  40. isDefault (config_get_bool(config, tag, "DefaultSubLang"))
  41. {
  42. }
  43. };
  44. int BasicGenData::AddLanguage(const char *tag)
  45. {
  46. LanguageInfo *info = new LanguageInfo(localeIni, tag);
  47. return window->languageList->Append(wxString(info->name, wxConvUTF8),
  48. info);
  49. }
  50. void BasicGenData::FillLanguageList(const char *currentLang)
  51. {
  52. window->languageList->Clear();
  53. size_t numSections = config_num_sections(localeIni);
  54. for (size_t i = 0; i < numSections; i++) {
  55. const char *lang = config_get_section(localeIni, i);
  56. int idx = AddLanguage(lang);
  57. if (strcmp(lang, currentLang) == 0)
  58. window->languageList->SetSelection(idx);
  59. }
  60. }
  61. BasicGenData::BasicGenData(OBSBasicSettings *window)
  62. : BasicSettingsData (window)
  63. {
  64. string path;
  65. if (!GetDataFilePath("locale/locale.ini", path))
  66. throw "Could not find locale/locale.ini path";
  67. if (localeIni.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  68. throw "Could not open locale.ini";
  69. const char *currentLang = config_get_string(GetGlobalConfig(),
  70. "General", "Language");
  71. FillLanguageList(currentLang);
  72. languageBoxConnector.Connect(
  73. window->languageList,
  74. wxEVT_COMBOBOX,
  75. wxCommandEventHandler(BasicGenData::LanguageChanged),
  76. NULL,
  77. this);
  78. window->generalChangedText->Hide();
  79. }
  80. void BasicGenData::LanguageChanged(wxCommandEvent &event)
  81. {
  82. dataChanged = true;
  83. window->generalChangedText->SetLabel(
  84. WXStr("Settings.General.LanguageChanged"));
  85. window->generalChangedText->Show();
  86. }
  87. void BasicGenData::Apply()
  88. {
  89. int sel = window->languageList->GetSelection();
  90. if (sel == wxNOT_FOUND)
  91. return;
  92. LanguageInfo *info = static_cast<LanguageInfo*>(
  93. window->languageList->GetClientData(sel));
  94. config_set_string(GetGlobalConfig(), "General", "Language", info->tag);
  95. }
  96. BasicSettingsData *CreateBasicGeneralSettings(OBSBasicSettings *window)
  97. {
  98. BasicSettingsData *data = NULL;
  99. try {
  100. data = new BasicGenData(window);
  101. } catch (const char *error) {
  102. blog(LOG_ERROR, "CreateBasicGeneralSettings failed: %s", error);
  103. }
  104. return data;
  105. }