PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/poedit-1.4.6.1/src/prefsdlg.cpp

#
C++ | 423 lines | 329 code | 51 blank | 43 comment | 38 complexity | dea184cde0c5adddbd1f338061675724 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. * This file is part of Poedit (http://www.poedit.net)
  3. *
  4. * Copyright (C) 2000-2005 Vaclav Slavik
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * $Id$
  25. *
  26. * Preferences settings dialog
  27. *
  28. */
  29. #include <wx/wxprec.h>
  30. #include "wxeditablelistbox.h"
  31. #include <wx/textctrl.h>
  32. #include <wx/button.h>
  33. #include <wx/tokenzr.h>
  34. #include <wx/config.h>
  35. #include <wx/choicdlg.h>
  36. #include <wx/spinctrl.h>
  37. #include <wx/notebook.h>
  38. #include <wx/fontdlg.h>
  39. #include <wx/fontutil.h>
  40. #include <wx/xrc/xmlres.h>
  41. #include "prefsdlg.h"
  42. #include "isocodes.h"
  43. #include "transmem.h"
  44. #include "transmemupd.h"
  45. #include "transmemupd_wizard.h"
  46. #include "chooselang.h"
  47. #ifdef USE_SPARKLE
  48. #include "osx/userdefaults.h"
  49. #endif // USE_SPARKLE
  50. PreferencesDialog::PreferencesDialog(wxWindow *parent)
  51. {
  52. wxXmlResource::Get()->LoadDialog(this, parent, _T("preferences"));
  53. #ifdef USE_TRANSMEM
  54. wxXmlResource::Get()->AttachUnknownControl(_T("tm_langs"),
  55. new wxEditableListBox(this, -1, _("My Languages")));
  56. #else
  57. // remove "Translation Memory" page if support not compiled-in
  58. // Must first look for the index of the TM page and then delete
  59. // it from the notebook.
  60. // We must rely on the fact that the TM page has a name from
  61. // the XRC resources because it's text may have been localized.
  62. wxNotebook* nb = XRCCTRL(*this, "notebook", wxNotebook);
  63. wxNotebookPage* tmPage = XRCCTRL(*this, "tm_page", wxWindow);
  64. int tmIndex = -1;
  65. int i = 0;
  66. while (i < nb->GetPageCount() && tmIndex == -1)
  67. {
  68. if (nb->GetPage(i) == tmPage)
  69. tmIndex = i;
  70. i++;
  71. }
  72. // this test may be a bit superfluous, but is a safeguard in case
  73. // the page isn't even available from the resources
  74. if (tmIndex != -1)
  75. XRCCTRL(*this, "notebook", wxNotebook)->DeletePage(tmIndex);
  76. #endif
  77. #if !USE_SPELLCHECKING
  78. // remove "Automatic spellchecking" checkbox:
  79. wxWindow *spellcheck = XRCCTRL(*this, "enable_spellchecking", wxCheckBox);
  80. spellcheck->GetContainingSizer()->Show(spellcheck, false);
  81. // re-layout the dialog:
  82. GetSizer()->Fit(this);
  83. #endif
  84. #if !NEED_CHOOSELANG_UI
  85. // remove (defunct on Unix) "Change UI language" button:
  86. XRCCTRL(*this, "ui_language", wxButton)->Show(false);
  87. #endif
  88. #if defined(__WXMSW__) || defined(__WXMAC__)
  89. // FIXME
  90. SetSize(GetSize().x+1,GetSize().y+1);
  91. #endif
  92. }
  93. void PreferencesDialog::TransferTo(wxConfigBase *cfg)
  94. {
  95. XRCCTRL(*this, "user_name", wxTextCtrl)->SetValue(
  96. cfg->Read(_T("translator_name"), wxEmptyString));
  97. XRCCTRL(*this, "user_email", wxTextCtrl)->SetValue(
  98. cfg->Read(_T("translator_email"), wxEmptyString));
  99. XRCCTRL(*this, "compile_mo", wxCheckBox)->SetValue(
  100. cfg->Read(_T("compile_mo"), true));
  101. XRCCTRL(*this, "show_summary", wxCheckBox)->SetValue(
  102. cfg->Read(_T("show_summary"), true));
  103. XRCCTRL(*this, "manager_startup", wxCheckBox)->SetValue(
  104. (bool)cfg->Read(_T("manager_startup"), (long)false));
  105. XRCCTRL(*this, "focus_to_text", wxCheckBox)->SetValue(
  106. (bool)cfg->Read(_T("focus_to_text"), (long)false));
  107. XRCCTRL(*this, "comment_window_editable", wxCheckBox)->SetValue(
  108. (bool)cfg->Read(_T("comment_window_editable"), (long)false));
  109. XRCCTRL(*this, "ext_editor", wxComboBox)->SetValue(
  110. cfg->Read(_T("ext_editor"), wxEmptyString));
  111. XRCCTRL(*this, "open_editor_immediately", wxCheckBox)->SetValue(
  112. cfg->Read(_T("open_editor_immediately"), (long)false));
  113. XRCCTRL(*this, "keep_crlf", wxCheckBox)->SetValue(
  114. (bool)cfg->Read(_T("keep_crlf"), true));
  115. #ifdef USE_SPELLCHECKING
  116. XRCCTRL(*this, "enable_spellchecking", wxCheckBox)->SetValue(
  117. (bool)cfg->Read(_T("enable_spellchecking"), true));
  118. #endif
  119. XRCCTRL(*this, "use_font_list", wxCheckBox)->SetValue(
  120. (bool)cfg->Read(_T("custom_font_list_use"), (long)false));
  121. XRCCTRL(*this, "use_font_text", wxCheckBox)->SetValue(
  122. (bool)cfg->Read(_T("custom_font_text_use"), (long)false));
  123. XRCCTRL(*this, "fontname_list", wxTextCtrl)->SetValue(
  124. cfg->Read(_T("custom_font_list_name"), wxEmptyString));
  125. XRCCTRL(*this, "fontname_text", wxTextCtrl)->SetValue(
  126. cfg->Read(_T("custom_font_text_name"), wxEmptyString));
  127. wxString format = cfg->Read(_T("crlf_format"), _T("unix"));
  128. int sel;
  129. if (format == _T("win")) sel = 1;
  130. else if (format == _T("mac")) sel = 2;
  131. else if (format == _T("native")) sel = 3;
  132. else /* _T("unix") */ sel = 0;
  133. XRCCTRL(*this, "crlf_format", wxChoice)->SetSelection(sel);
  134. m_parsers.Read(cfg);
  135. wxListBox *list = XRCCTRL(*this, "parsers_list", wxListBox);
  136. for (unsigned i = 0; i < m_parsers.GetCount(); i++)
  137. list->Append(m_parsers[i].Name);
  138. if (m_parsers.GetCount() == 0)
  139. {
  140. XRCCTRL(*this, "parser_edit", wxButton)->Enable(false);
  141. XRCCTRL(*this, "parser_delete", wxButton)->Enable(false);
  142. }
  143. else
  144. list->SetSelection(0);
  145. #ifdef USE_TRANSMEM
  146. XRCCTRL(*this, "tm_dbpath", wxTextCtrl)->SetValue(
  147. cfg->Read(_T("TM/database_path"), wxEmptyString));
  148. wxStringTokenizer tkn(cfg->Read(_T("TM/languages"), wxEmptyString), _T(":"));
  149. wxArrayString langs;
  150. while (tkn.HasMoreTokens()) langs.Add(tkn.GetNextToken());
  151. XRCCTRL(*this, "tm_langs", wxEditableListBox)->SetStrings(langs);
  152. XRCCTRL(*this, "tm_omits", wxSpinCtrl)->SetValue(
  153. cfg->Read(_T("TM/max_omitted"), 2));
  154. XRCCTRL(*this, "tm_delta", wxSpinCtrl)->SetValue(
  155. cfg->Read(_T("TM/max_delta"), 2));
  156. XRCCTRL(*this, "tm_automatic", wxCheckBox)->SetValue(
  157. cfg->Read(_T("use_tm_when_updating"), true));
  158. #endif
  159. #ifdef USE_SPARKLE
  160. XRCCTRL(*this, "auto_updates", wxCheckBox)->SetValue(
  161. UserDefaults::GetBoolValue("SUEnableAutomaticChecks"));
  162. #endif // USE_SPARKLE
  163. }
  164. void PreferencesDialog::TransferFrom(wxConfigBase *cfg)
  165. {
  166. cfg->Write(_T("translator_name"),
  167. XRCCTRL(*this, "user_name", wxTextCtrl)->GetValue());
  168. cfg->Write(_T("translator_email"),
  169. XRCCTRL(*this, "user_email", wxTextCtrl)->GetValue());
  170. cfg->Write(_T("compile_mo"),
  171. XRCCTRL(*this, "compile_mo", wxCheckBox)->GetValue());
  172. cfg->Write(_T("show_summary"),
  173. XRCCTRL(*this, "show_summary", wxCheckBox)->GetValue());
  174. cfg->Write(_T("manager_startup"),
  175. XRCCTRL(*this, "manager_startup", wxCheckBox)->GetValue());
  176. cfg->Write(_T("focus_to_text"),
  177. XRCCTRL(*this, "focus_to_text", wxCheckBox)->GetValue());
  178. cfg->Write(_T("comment_window_editable"),
  179. XRCCTRL(*this, "comment_window_editable", wxCheckBox)->GetValue());
  180. cfg->Write(_T("ext_editor"),
  181. XRCCTRL(*this, "ext_editor", wxComboBox)->GetValue());
  182. cfg->Write(_T("open_editor_immediately"),
  183. XRCCTRL(*this, "open_editor_immediately", wxCheckBox)->GetValue());
  184. cfg->Write(_T("keep_crlf"),
  185. XRCCTRL(*this, "keep_crlf", wxCheckBox)->GetValue());
  186. #ifdef USE_SPELLCHECKING
  187. cfg->Write(_T("enable_spellchecking"),
  188. XRCCTRL(*this, "enable_spellchecking", wxCheckBox)->GetValue());
  189. #endif
  190. cfg->Write(_T("custom_font_list_use"),
  191. XRCCTRL(*this, "use_font_list", wxCheckBox)->GetValue());
  192. cfg->Write(_T("custom_font_text_use"),
  193. XRCCTRL(*this, "use_font_text", wxCheckBox)->GetValue());
  194. cfg->Write(_T("custom_font_list_name"),
  195. XRCCTRL(*this, "fontname_list", wxTextCtrl)->GetValue());
  196. cfg->Write(_T("custom_font_text_name"),
  197. XRCCTRL(*this, "fontname_text", wxTextCtrl)->GetValue());
  198. static const wxChar *formats[] =
  199. { _T("unix"), _T("win"), _T("mac"), _T("native") };
  200. cfg->Write(_T("crlf_format"), formats[
  201. XRCCTRL(*this, "crlf_format", wxChoice)->GetSelection()]);
  202. m_parsers.Write(cfg);
  203. #ifdef USE_TRANSMEM
  204. wxArrayString langs;
  205. XRCCTRL(*this, "tm_langs", wxEditableListBox)->GetStrings(langs);
  206. wxString languages;
  207. for (size_t i = 0; i < langs.GetCount(); i++)
  208. {
  209. if (i != 0) languages << _T(':');
  210. languages << langs[i];
  211. }
  212. cfg->Write(_T("TM/languages"), languages);
  213. cfg->Write(_T("TM/database_path"),
  214. XRCCTRL(*this, "tm_dbpath", wxTextCtrl)->GetValue());
  215. cfg->Write(_T("TM/max_omitted"),
  216. (long)XRCCTRL(*this, "tm_omits", wxSpinCtrl)->GetValue());
  217. cfg->Write(_T("TM/max_delta"),
  218. (long)XRCCTRL(*this, "tm_delta", wxSpinCtrl)->GetValue());
  219. cfg->Write(_T("use_tm_when_updating"),
  220. XRCCTRL(*this, "tm_automatic", wxCheckBox)->GetValue());
  221. #endif
  222. #ifdef USE_SPARKLE
  223. UserDefaults::SetBoolValue("SUEnableAutomaticChecks",
  224. XRCCTRL(*this, "auto_updates", wxCheckBox)->GetValue());
  225. #endif // USE_SPARKLE
  226. }
  227. BEGIN_EVENT_TABLE(PreferencesDialog, wxDialog)
  228. EVT_BUTTON(XRCID("parser_new"), PreferencesDialog::OnNewParser)
  229. EVT_BUTTON(XRCID("parser_edit"), PreferencesDialog::OnEditParser)
  230. EVT_BUTTON(XRCID("parser_delete"), PreferencesDialog::OnDeleteParser)
  231. EVT_BUTTON(XRCID("fontpicker_list"), PreferencesDialog::OnChooseListFont)
  232. EVT_BUTTON(XRCID("fontpicker_text"), PreferencesDialog::OnChooseTextFont)
  233. #ifdef USE_TRANSMEM
  234. EVT_BUTTON(XRCID("tm_addlang"), PreferencesDialog::OnTMAddLang)
  235. EVT_BUTTON(XRCID("tm_browsedbpath"), PreferencesDialog::OnTMBrowseDbPath)
  236. EVT_BUTTON(XRCID("tm_generate"), PreferencesDialog::OnTMGenerate)
  237. #endif
  238. #if NEED_CHOOSELANG_UI
  239. EVT_BUTTON(XRCID("ui_language"), PreferencesDialog::OnUILanguage)
  240. #endif
  241. END_EVENT_TABLE()
  242. #if NEED_CHOOSELANG_UI
  243. void PreferencesDialog::OnUILanguage(wxCommandEvent& event)
  244. {
  245. ChangeUILanguage();
  246. }
  247. #endif
  248. bool PreferencesDialog::EditParser(int num)
  249. {
  250. wxDialog dlg;
  251. wxXmlResource::Get()->LoadDialog(&dlg, this, _T("edit_parser"));
  252. dlg.Centre();
  253. Parser& nfo = m_parsers[num];
  254. XRCCTRL(dlg, "parser_language", wxTextCtrl)->SetValue(nfo.Name);
  255. XRCCTRL(dlg, "parser_extensions", wxTextCtrl)->SetValue(nfo.Extensions);
  256. XRCCTRL(dlg, "parser_command", wxTextCtrl)->SetValue(nfo.Command);
  257. XRCCTRL(dlg, "parser_keywords", wxTextCtrl)->SetValue(nfo.KeywordItem);
  258. XRCCTRL(dlg, "parser_files", wxTextCtrl)->SetValue(nfo.FileItem);
  259. XRCCTRL(dlg, "parser_charset", wxTextCtrl)->SetValue(nfo.CharsetItem);
  260. if (dlg.ShowModal() == wxID_OK)
  261. {
  262. nfo.Name = XRCCTRL(dlg, "parser_language", wxTextCtrl)->GetValue();
  263. nfo.Extensions = XRCCTRL(dlg, "parser_extensions", wxTextCtrl)->GetValue();
  264. nfo.Command = XRCCTRL(dlg, "parser_command", wxTextCtrl)->GetValue();
  265. nfo.KeywordItem = XRCCTRL(dlg, "parser_keywords", wxTextCtrl)->GetValue();
  266. nfo.FileItem = XRCCTRL(dlg, "parser_files", wxTextCtrl)->GetValue();
  267. nfo.CharsetItem = XRCCTRL(dlg, "parser_charset", wxTextCtrl)->GetValue();
  268. XRCCTRL(*this, "parsers_list", wxListBox)->SetString(num, nfo.Name);
  269. return true;
  270. }
  271. else
  272. return false;
  273. }
  274. void PreferencesDialog::OnNewParser(wxCommandEvent& event)
  275. {
  276. Parser info;
  277. m_parsers.Add(info);
  278. XRCCTRL(*this, "parsers_list", wxListBox)->Append(wxEmptyString);
  279. size_t index = m_parsers.GetCount()-1;
  280. if (!EditParser(index))
  281. {
  282. XRCCTRL(*this, "parsers_list", wxListBox)->Delete(index);
  283. m_parsers.RemoveAt(index);
  284. }
  285. else
  286. {
  287. XRCCTRL(*this, "parser_edit", wxButton)->Enable(true);
  288. XRCCTRL(*this, "parser_delete", wxButton)->Enable(true);
  289. }
  290. }
  291. void PreferencesDialog::OnEditParser(wxCommandEvent& event)
  292. {
  293. EditParser(XRCCTRL(*this, "parsers_list", wxListBox)->GetSelection());
  294. }
  295. void PreferencesDialog::OnDeleteParser(wxCommandEvent& event)
  296. {
  297. size_t index = XRCCTRL(*this, "parsers_list", wxListBox)->GetSelection();
  298. m_parsers.RemoveAt(index);
  299. XRCCTRL(*this, "parsers_list", wxListBox)->Delete(index);
  300. if (m_parsers.GetCount() == 0)
  301. {
  302. XRCCTRL(*this, "parser_edit", wxButton)->Enable(false);
  303. XRCCTRL(*this, "parser_delete", wxButton)->Enable(false);
  304. }
  305. }
  306. #ifdef USE_TRANSMEM
  307. void PreferencesDialog::OnTMAddLang(wxCommandEvent& event)
  308. {
  309. wxArrayString lngs;
  310. int index;
  311. for (const LanguageStruct *i = isoLanguages; i->lang != NULL; i++)
  312. lngs.Add(wxString(i->iso) + _T(" (") + i->lang + _T(")"));
  313. index = wxGetSingleChoiceIndex(_("Select language"),
  314. _("Please select language ISO code:"),
  315. lngs, this);
  316. if (index != -1)
  317. {
  318. wxArrayString a;
  319. XRCCTRL(*this, "tm_langs", wxEditableListBox)->GetStrings(a);
  320. a.Add(isoLanguages[index].iso);
  321. XRCCTRL(*this, "tm_langs", wxEditableListBox)->SetStrings(a);
  322. }
  323. }
  324. void PreferencesDialog::OnTMBrowseDbPath(wxCommandEvent& event)
  325. {
  326. wxDirDialog dlg(this, _("Select directory"),
  327. XRCCTRL(*this, "tm_dbpath", wxTextCtrl)->GetValue());
  328. if (dlg.ShowModal() == wxID_OK)
  329. XRCCTRL(*this, "tm_dbpath", wxTextCtrl)->SetValue(dlg.GetPath());
  330. }
  331. void PreferencesDialog::OnTMGenerate(wxCommandEvent& event)
  332. {
  333. wxString dbPath = XRCCTRL(*this, "tm_dbpath", wxTextCtrl)->GetValue();
  334. // VS: we can't get it from TM/database_path key in wxConfig object
  335. // because it wasn't update yet with information from the dialog
  336. // (which happens when the users presses OK) but we still won't
  337. // to use the path entered by the user...
  338. wxArrayString langs;
  339. XRCCTRL(*this, "tm_langs", wxEditableListBox)->GetStrings(langs);
  340. RunTMUpdateWizard(this, dbPath, langs);
  341. }
  342. #endif // USE_TRANSMEM
  343. void PreferencesDialog::DoChooseFont(wxTextCtrl *nameField)
  344. {
  345. wxFont font;
  346. wxString fontname = nameField->GetValue();
  347. if (!fontname.empty())
  348. {
  349. wxNativeFontInfo fi;
  350. fi.FromString(fontname);
  351. font.SetNativeFontInfo(fi);
  352. }
  353. wxFontData data;
  354. data.EnableEffects(false);
  355. data.SetAllowSymbols(false);
  356. data.SetInitialFont(font);
  357. wxFontDialog dlg(this, data);
  358. if (dlg.ShowModal() == wxID_OK)
  359. {
  360. data = dlg.GetFontData();
  361. font = data.GetChosenFont();
  362. nameField->SetValue(font.GetNativeFontInfoDesc());
  363. }
  364. }
  365. void PreferencesDialog::OnChooseListFont(wxCommandEvent& event)
  366. {
  367. DoChooseFont(XRCCTRL(*this, "fontname_list", wxTextCtrl));
  368. }
  369. void PreferencesDialog::OnChooseTextFont(wxCommandEvent& event)
  370. {
  371. DoChooseFont(XRCCTRL(*this, "fontname_text", wxTextCtrl));
  372. }