Various changes to preferences object, file loading, and error logging.
[jben.git] / panel_config.cpp
blobcf9032032991f1cfb16014d59d063d395efbeb80
1 /*
2 Project: J-Ben
3 Author: Paul Goins
4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: panel_config.cpp
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include "panel_config.h"
25 #include "jben.h"
26 #include "kdict.h"
27 #include "file_utils.h"
29 enum {
30 ID_chkReadings=1,
31 ID_chkMeanings,
32 ID_chkHighImportance,
33 ID_chkVocabCrossRef,
34 ID_chkLowImportance,
35 ID_chkUseSODs,
36 ID_chkUseSODAs,
38 ID_scrlDictionaries,
39 ID_btnApply,
41 ID_chkDictionaries,
42 ID_chkDictBase
45 BEGIN_EVENT_TABLE(PanelConfig, wxPanel)
46 EVT_BUTTON(ID_btnApply, PanelConfig::OnApply)
47 EVT_CHECKBOX(ID_chkDictionaries, PanelConfig::OnDictionaryToggle)
48 END_EVENT_TABLE()
50 PanelConfig::PanelConfig(wxWindow *owner) : RedrawablePanel(owner) {
51 /* Make main controls */
52 chkReadings = new wxCheckBox(this, ID_chkReadings, _T("Include on-yomi, kun-yomi, and nanori (name) readings"));
53 chkMeanings = new wxCheckBox(this, ID_chkMeanings, _T("Include English meanings"));
54 chkHighImportance = new wxCheckBox(this, ID_chkHighImportance, _T("Include stroke count, Jouyou grade and newspaper frequency rank"));
55 chkDictionaries = new wxCheckBox(this, ID_chkDictionaries, _T("Include dictionary reference codes"));
56 scrlDictionaries = new wxScrolledWindow(this, ID_scrlDictionaries, wxDefaultPosition, wxDefaultSize, wxVSCROLL | wxHSCROLL | wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
57 scrlDictionaries->SetBackgroundColour(*wxWHITE);
58 chkVocabCrossRef = new wxCheckBox(this, ID_chkVocabCrossRef, _T("Show vocab from your study list which use this kanji"));
59 chkLowImportance = new wxCheckBox(this, ID_chkLowImportance, _T("Other information (Radical #'s, Korean and Pinyin romanization)"));
60 chkUseSODs = new wxCheckBox(this, ID_chkUseSODs, _T("Use KanjiCafe.com stroke order diagrams"));
61 chkUseSODAs = new wxCheckBox(this, ID_chkUseSODAs, _T("Use KanjiCafe.com animated stroke order diagrams"));
62 wxButton *btnApply = new wxButton(this, ID_btnApply, _T("Apply Changes"));
64 /* Create our list of dictionaries */
65 dictionaryList.Add(_T("\"New Japanese-English Character Dictionary\" by Jack Halpern."));
66 dictionaryList.Add(_T("\"Modern Reader's Japanese-English Character Dictionary\" by Andrew Nelson"));
67 dictionaryList.Add(_T("\"The New Nelson Japanese-English Character Dictionary\" by John Haig"));
68 dictionaryList.Add(_T("\"Japanese for Busy People\" by AJLT"));
69 dictionaryList.Add(_T("\"The Kanji Way to Japanese Language Power\" by Dale Crowley"));
70 dictionaryList.Add(_T("\"Kodansha Compact Kanji Guide\""));
71 dictionaryList.Add(_T("\"A Guide To Reading and Writing Japanese\" by Ken Hensall et al."));
72 dictionaryList.Add(_T("\"Kanji in Context\" by Nishiguchi and Kono"));
73 dictionaryList.Add(_T("\"Kanji Learner's Dictionary\" by Jack Halpern"));
74 dictionaryList.Add(_T("\"Essential Kanji\" by P.G. O'Neill"));
75 dictionaryList.Add(_T("\"2001 Kanji\" by Father Joseph De Roo"));
76 dictionaryList.Add(_T("\"A Guide To Reading and Writing Japanese\" by Florence Sakade"));
77 dictionaryList.Add(_T("\"Tuttle Kanji Cards\" by Alexander Kask"));
78 dictionaryList.Add(_T("\"Japanese Kanji Flashcards\" by White Rabbit Press"));
79 dictionaryList.Add(_T("(Not yet supported) SKIP codes used by Halpern dictionaries"));
80 dictionaryList.Add(_T("\"Kanji Dictionary\" by Spahn && Hadamitzky"));
81 dictionaryList.Add(_T("\"Kanji && Kana\" by Spahn && Hadamitzky"));
82 dictionaryList.Add(_T("Four Corner code (various dictionaries)"));
83 dictionaryList.Add(_T("\"Morohashi Daikanwajiten\" (index number)"));
84 dictionaryList.Add(_T("\"Morohashi Daikanwajiten\" (volume.page number)"));
85 dictionaryList.Add(_T("\"A Guide to Remembering Japanese Characters\" by Kenneth G. Henshal"));
86 dictionaryList.Add(_T("Gakken Kanji Dictionary (\"A New Dictionary of Kanji Usage\")"));
87 dictionaryList.Add(_T("\"Remembering The Kanji\" by James Heisig"));
88 dictionaryList.Add(_T("\"Japanese Names\" by P.G. O'Neill"));
90 /* Make controls for dictionary stuff */
91 int dictCount = dictionaryList.Count();
92 if(dictCount>0) chkarrayDictionaries = new ScrolledCheck*[dictCount];
93 else chkarrayDictionaries = NULL;
94 wxBoxSizer *dictSizer = new wxBoxSizer(wxVERTICAL);
95 int i;
96 for(i=0;i<dictCount;i++) {
97 chkarrayDictionaries[i] = new ScrolledCheck(scrlDictionaries, ID_chkDictBase+i, i, dictionaryList[i]);
98 dictSizer->Add(chkarrayDictionaries[i]);
100 if(dictCount>0)
101 scrlDictionaries->SetScrollRate(25, chkarrayDictionaries[0]->GetSize().GetHeight());
102 scrlDictionaries->SetSizer(dictSizer);
104 /* Connect event handlers for all checkboxes (except the parent dictionary checkbox,
105 since it already has a special handler configured.) */
106 /* NOTE:
107 Here's the Connect call used in the Event sample:
108 Connect(Event_Dynamic, wxID_ANY, wxEVT_COMMAND_MENU_SELECTED,
109 (wxObjectEventFunction)
110 (wxEventFunction)
111 (wxCommandEventFunction)&MyFrame::OnDynamic);
112 In practice, only the (wxObjectEventFunction) cast appears to be necessary (under GNU gcc). */
113 this->Connect(ID_chkReadings, ID_chkUseSODAs,
114 wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction)&PanelConfig::OnCheckboxToggle);
115 this->Connect(ID_chkDictBase, ID_chkDictBase+dictCount-1,
116 wxEVT_COMMAND_CHECKBOX_CLICKED, (wxObjectEventFunction)&PanelConfig::OnCheckboxToggle);
118 /* Add everything to the main panel */
119 wxBoxSizer *panelSizer = new wxBoxSizer(wxVERTICAL);
120 panelSizer->Add(chkReadings, 0, wxTOP | wxLEFT | wxRIGHT, 10);
121 panelSizer->Add(chkMeanings, 0, wxLEFT | wxRIGHT, 10);
122 panelSizer->Add(chkHighImportance, 0, wxLEFT | wxRIGHT, 10);
123 panelSizer->Add(chkDictionaries, 0, wxTOP | wxLEFT | wxRIGHT, 10);
124 panelSizer->Add(scrlDictionaries, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10);
125 panelSizer->Add(chkVocabCrossRef, 0, wxLEFT | wxRIGHT, 10);
126 panelSizer->Add(chkLowImportance, 0, wxLEFT | wxRIGHT, 10);
127 panelSizer->Add(chkUseSODs, 0, wxLEFT | wxRIGHT, 10);
128 panelSizer->Add(chkUseSODAs, 0, wxLEFT | wxRIGHT, 10);
129 panelSizer->Add(btnApply, 0, wxALIGN_CENTER | wxALL, 10);
131 changeDetected = false;
132 processingRedraw = false;
133 dictionariesEnabled =
134 Preferences::Get()->kanjidicOptions & KDO_DICTIONARIES;
135 UpdateDictionaryControls(dictionariesEnabled);
136 this->SetSizerAndFit(panelSizer);
139 PanelConfig::~PanelConfig() {
140 if(chkarrayDictionaries) delete[] chkarrayDictionaries;
143 void PanelConfig::OnApply(wxCommandEvent& event) {
144 Commit();
147 void PanelConfig::OnCheckboxToggle(wxCommandEvent& event) {
148 if(!processingRedraw) {
149 changeDetected = true;
153 void PanelConfig::OnDictionaryToggle(wxCommandEvent& event) {
154 if(!processingRedraw) {
155 bool value = chkDictionaries->GetValue();
156 if(value!=dictionariesEnabled) {
157 dictionariesEnabled = value;
158 UpdateDictionaryControls(dictionariesEnabled);
159 OnCheckboxToggle(event);
164 void PanelConfig::Commit() {
165 changeDetected = false;
166 unsigned long options=0, dictionaries=0;
167 if(chkReadings->GetValue()) options |= KDO_READINGS;
168 if(chkMeanings->GetValue()) options |= KDO_MEANINGS;
169 if(chkHighImportance->GetValue()) options |= KDO_HIGHIMPORTANCE;
170 if(chkDictionaries->GetValue()) options |= KDO_DICTIONARIES;
171 if(chkVocabCrossRef->GetValue()) options |= KDO_VOCABCROSSREF;
172 if(chkLowImportance->GetValue()) options |= KDO_LOWIMPORTANCE;
173 if(chkUseSODs->GetValue()) options |= KDO_SOD_STATIC;
174 if(chkUseSODAs->GetValue()) options |= KDO_SOD_ANIM;
176 int dictCount = dictionaryList.Count();
177 for(int i=0; i<dictCount; i++) {
178 if(chkarrayDictionaries[i]->GetValue()) dictionaries |= (1ul << i);
180 Preferences *prefs = Preferences::Get();
181 prefs->kanjidicOptions = options;
182 prefs->kanjidicDictionaries = dictionaries;
185 void PanelConfig::Revert() {
186 changeDetected = false;
187 /* We could have a Redraw() call here, but currently Revert is only called when changing away from this panel,
188 and thus it's a pointless call since Redraw will be done when this panel is selected again. */
191 void PanelConfig::Redraw() {
192 processingRedraw = true;
193 Preferences *prefs = Preferences::Get();
194 int options = prefs->kanjidicOptions;
195 int dictionaries = prefs->kanjidicDictionaries;
197 /* Set appropriate checkboxes */
198 chkReadings->SetValue(options & KDO_READINGS);
199 chkMeanings->SetValue(options & KDO_MEANINGS);
200 chkHighImportance->SetValue(options & KDO_HIGHIMPORTANCE);
201 chkDictionaries->SetValue(options & KDO_DICTIONARIES);
202 chkVocabCrossRef->SetValue(options & KDO_VOCABCROSSREF);
203 chkLowImportance->SetValue(options & KDO_LOWIMPORTANCE);
204 chkUseSODs->SetValue(options & KDO_SOD_STATIC);
205 chkUseSODAs->SetValue(options & KDO_SOD_ANIM);
207 /* Enable/disable SOD/SODA checkboxes based on existance of directory */
208 string sodDir = Preferences::Get()->GetSetting("sod_dir");
209 chkUseSODs->Enable(
210 FileExists(
211 string(sodDir)
212 .append(1,DSCHAR)
213 .append("sod-utf8-hex")
214 .append(1,DSCHAR)
215 .append("README-License").c_str()));
216 chkUseSODAs->Enable(
217 FileExists(
218 string(sodDir)
219 .append(1,DSCHAR)
220 .append("soda-utf8-hex")
221 .append(1,DSCHAR)
222 .append("README-License").c_str()));
224 int dictCount = dictionaryList.Count();
225 for(int i=0;i<dictCount;i++) {
226 chkarrayDictionaries[i]->SetValue(dictionaries & (1ul << i));
229 scrlDictionaries->Scroll(0,0);
231 processingRedraw = false;
232 dictionariesEnabled = chkDictionaries->GetValue();
235 bool PanelConfig::ChangeDetected() {return changeDetected;}
237 void PanelConfig::UpdateDictionaryControls(bool state) {
238 if(state) {
239 scrlDictionaries->Enable();
240 } else {
241 scrlDictionaries->Disable();