settings-basic-general.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <util/bmem.h>
  15. #include "obs-app.hpp"
  16. #include "settings-basic.hpp"
  17. #include "window-settings-basic.hpp"
  18. #include "wx-wrappers.hpp"
  19. #include "platform.hpp"
  20. class BasicGenData : public BasicSettingsData {
  21. ConfigFile localeIni;
  22. WXConnector languageBoxConnector;
  23. void LanguageChanged(wxCommandEvent &event);
  24. int AddLanguage(const char *tag);
  25. void FillLanguageList(const char *currentLang);
  26. public:
  27. BasicGenData(OBSBasicSettings *window);
  28. virtual void Apply();
  29. };
  30. class LanguageInfo : public wxClientData {
  31. public:
  32. const char *tag;
  33. const char *name;
  34. const char *subLang;
  35. bool isDefault;
  36. inline LanguageInfo(config_t config, const char *tag)
  37. : wxClientData (),
  38. tag (tag),
  39. name (config_get_string(config, tag, "Name")),
  40. subLang (config_get_string(config, tag, "SubLang")),
  41. isDefault (config_get_bool(config, tag, "DefaultSubLang"))
  42. {
  43. }
  44. };
  45. int BasicGenData::AddLanguage(const char *tag)
  46. {
  47. LanguageInfo *info = new LanguageInfo(localeIni, tag);
  48. return window->languageList->Append(wxString(info->name, wxConvUTF8),
  49. info);
  50. }
  51. void BasicGenData::FillLanguageList(const char *currentLang)
  52. {
  53. window->languageList->Clear();
  54. size_t numSections = config_num_sections(localeIni);
  55. for (size_t i = 0; i < numSections; i++) {
  56. const char *lang = config_get_section(localeIni, i);
  57. int idx = AddLanguage(lang);
  58. if (strcmp(lang, currentLang) == 0)
  59. window->languageList->SetSelection(idx);
  60. }
  61. }
  62. BasicGenData::BasicGenData(OBSBasicSettings *window)
  63. : BasicSettingsData (window)
  64. {
  65. string path;
  66. if (!GetDataFilePath("locale/locale.ini", path))
  67. throw "Could not find locale/locale.ini path";
  68. if (localeIni.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  69. throw "Could not open locale.ini";
  70. const char *currentLang = config_get_string(GetGlobalConfig(),
  71. "General", "Language");
  72. FillLanguageList(currentLang);
  73. languageBoxConnector.Connect(
  74. window->languageList,
  75. wxEVT_COMBOBOX,
  76. wxCommandEventHandler(BasicGenData::LanguageChanged),
  77. NULL,
  78. this);
  79. window->generalChangedText->Hide();
  80. }
  81. void BasicGenData::LanguageChanged(wxCommandEvent &event)
  82. {
  83. SetChanged();
  84. window->generalChangedText->SetLabel(
  85. WXStr("Settings.General.ProgramRestart"));
  86. window->generalChangedText->Show();
  87. }
  88. void BasicGenData::Apply()
  89. {
  90. int sel = window->languageList->GetSelection();
  91. if (sel == wxNOT_FOUND)
  92. return;
  93. LanguageInfo *info = static_cast<LanguageInfo*>(
  94. window->languageList->GetClientObject(sel));
  95. config_set_string(GetGlobalConfig(), "General", "Language", info->tag);
  96. config_save(GetGlobalConfig());
  97. SetSaved();
  98. }
  99. BasicSettingsData *CreateBasicGeneralSettings(OBSBasicSettings *window)
  100. {
  101. BasicSettingsData *data = NULL;
  102. try {
  103. data = new BasicGenData(window);
  104. } catch (const char *error) {
  105. blog(LOG_ERROR, "CreateBasicGeneralSettings failed: %s", error);
  106. }
  107. return data;
  108. }