Various changes to preferences object, file loading, and error logging.
[jben.git] / dialog_addkanjibygrade.cpp
blobeec488f574552aa86cc0041f7b588ea7a3e5e675
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: dialog_addkanjibygrade.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 "dialog_addkanjibygrade.h"
26 enum {
27 ID_buttonOK=wxID_OK,
28 ID_buttonCancel=wxID_CANCEL,
29 ID_comboLow=1,
30 ID_comboHigh
33 BEGIN_EVENT_TABLE(DialogAddKanjiByGrade, wxDialog)
34 EVT_BUTTON(ID_buttonOK, DialogAddKanjiByGrade::OnOK)
35 EVT_BUTTON(ID_buttonCancel, DialogAddKanjiByGrade::OnCancel)
36 EVT_KEY_DOWN(DialogAddKanjiByGrade::OnKeyDown)
37 EVT_COMBOBOX(ID_comboLow, DialogAddKanjiByGrade::OnLowValChange)
38 EVT_COMBOBOX(ID_comboHigh, DialogAddKanjiByGrade::OnHighValChange)
39 END_EVENT_TABLE()
41 wxString comboStrings[] = {
42 _T("Jouyou Kanji Grade 1"),
43 _T("Jouyou Kanji Grade 2"),
44 _T("Jouyou Kanji Grade 3"),
45 _T("Jouyou Kanji Grade 4"),
46 _T("Jouyou Kanji Grade 5"),
47 _T("Jouyou Kanji Grade 6"),
48 _T("Jouyou Kanji, General Usage"),
49 _T("Jinmeiyou Kanji (for names)"),
50 _T("Non-Jouyou/Non-Jinmeiyou Kanji")
52 int comboStringCount = 9;
54 DialogAddKanjiByGrade::DialogAddKanjiByGrade(wxWindow *parent)
55 : wxDialog(parent, wxID_ANY, wxString(_T("Add Kanji By Grade"))) {
56 comboLowGrade = new wxComboBox(this, ID_comboLow, comboStrings[0], wxDefaultPosition, wxDefaultSize, 9, comboStrings, wxCB_DROPDOWN | wxCB_READONLY);
57 comboHighGrade = new wxComboBox(this, ID_comboHigh, comboStrings[0], wxDefaultPosition, wxDefaultSize, 9, comboStrings, wxCB_DROPDOWN | wxCB_READONLY);
59 btnOK = new wxButton(this, ID_buttonOK, _T("OK"));
60 btnCancel = new wxButton(this, ID_buttonCancel, _T("Cancel"));
61 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
62 mainSizer->Add(comboLowGrade, 0, wxALL, 10);
63 mainSizer->Add(comboHighGrade, 0, wxLEFT | wxRIGHT | wxBOTTOM, 10);
64 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
65 buttonSizer->Add(btnOK, 0, wxRIGHT, 10);
66 buttonSizer->Add(btnCancel, 0);
67 mainSizer->Add(buttonSizer, 0, wxLEFT | wxRIGHT | wxBOTTOM, 10);
68 this->SetSizerAndFit(mainSizer);
69 comboLowGrade->SetFocus();
72 int ComboBoxToKanjidicGrade(wxComboBox *c) {
73 int i;
74 for(i=0;i<comboStringCount;i++)
75 if(c->GetValue()==comboStrings[i]) break;
76 if(i<6) return i+1; /* G1-G6 in KANJIDIC */
77 if(i==6) return 8; /* G8 (Jouyou Jr. High) in KANJIDIC */
78 if(i==7) return 9; /* G9 (Jinmeiyou) in KANJIDIC */
79 return 0; /* No grade listed in KANJIDIC */
82 int DialogAddKanjiByGrade::GetLowGrade() {
83 return ComboBoxToKanjidicGrade(comboLowGrade);
86 int DialogAddKanjiByGrade::GetHighGrade() {
87 return ComboBoxToKanjidicGrade(comboHighGrade);
90 void DialogAddKanjiByGrade::OKProc() {
91 int l = GetLowGrade();
92 int h = GetHighGrade();
93 /* 0 is a special code for non-graded kanji and is treated
94 as the highest grade level here. */
95 if((h<l && h!=0) || (l==0 && h!=0))
96 wxMessageBox(_T("Your high grade level cannot be less than your low grade level."), _T("Bad grade range"), wxOK | wxICON_WARNING, this);
97 else
98 EndModal(ID_buttonOK);
101 void DialogAddKanjiByGrade::CancelProc() {
102 EndModal(ID_buttonCancel);
105 void DialogAddKanjiByGrade::OnOK(wxCommandEvent& ev) {OKProc();}
106 void DialogAddKanjiByGrade::OnCancel(wxCommandEvent& ev) {CancelProc();}
108 void DialogAddKanjiByGrade::OnKeyDown(wxKeyEvent& ev) {
109 int i = ev.GetKeyCode();
110 switch(i) {
111 case WXK_ESCAPE:
112 CancelProc();
113 break;
114 case WXK_RETURN:
115 OKProc();
116 break;
117 default:
118 ev.Skip();
122 void DialogAddKanjiByGrade::OnLowValChange(wxCommandEvent& ev) {
123 int low, high;
124 low = GetLowGrade();
125 high = GetHighGrade();
126 if(high!=0 && (low>high || low==0))
127 comboHighGrade->SetValue(comboLowGrade->GetValue());
130 void DialogAddKanjiByGrade::OnHighValChange(wxCommandEvent& ev) {
131 int low, high;
132 low = GetLowGrade();
133 high = GetHighGrade();
134 if(high!=0 && (high<low || low==0))
135 comboLowGrade->SetValue(comboHighGrade->GetValue());