settings-basic-general.cpp 3.7 KB

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