Added adding/sorting kanji by JLPT grade. A few other minor changes.
[jben.git] / src / dialog_addkanjibyjlpt.cpp
blob3488eaad90d58edbea8ee6f6f8002f4f2139f790
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_addkanjibyjlpt.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_addkanjibyjlpt.h"
26 #include <glibmm/i18n.h>
27 #include <gtkmm/stock.h>
28 #include <gtkmm/messagedialog.h>
29 #include <cassert>
31 Glib::ustring jlptStrs[] = {
32 _("Level 4 (Easy)"),
33 _("Level 3"),
34 _("Level 2"),
35 _("Level 1 (Hard)")
37 int jlptStrCount = 4;
39 DialogAddKanjiByJLPT::DialogAddKanjiByJLPT(Gtk::Window& parent)
40 : StoredDialog(_("Add Kanji By JLPT Level"), parent, "gui.dlg.addkanjibyfreq.size"),
41 btnOK(Gtk::Stock::OK),
42 btnCancel(Gtk::Stock::CANCEL)
44 for(int i=0; i<jlptStrCount; i++) {
45 comboLowLevel.append_text(jlptStrs[i]);
46 comboHighLevel.append_text(jlptStrs[i]);
48 comboLowLevel.set_active_text(jlptStrs[0]);
49 comboHighLevel.set_active_text(jlptStrs[0]);
51 comboLowLevel.signal_changed()
52 .connect(sigc::mem_fun(*this, &DialogAddKanjiByJLPT::OnLowValChange));
53 comboHighLevel.signal_changed()
54 .connect(sigc::mem_fun(*this, &DialogAddKanjiByJLPT::OnHighValChange));
55 btnOK.signal_clicked()
56 .connect(sigc::mem_fun(*this, &DialogAddKanjiByJLPT::OnOK));
57 btnCancel.signal_clicked()
58 .connect(sigc::mem_fun(*this, &DialogAddKanjiByJLPT::OnCancel));
60 set_border_width(5);
62 Gtk::VBox* pvb = get_vbox();
63 pvb->set_spacing(5);
64 pvb->pack_start(comboLowLevel);
65 pvb->pack_start(comboHighLevel);
67 Gtk::HButtonBox* phbb = get_action_area();
68 phbb->pack_start(btnCancel);
69 phbb->pack_start(btnOK);
71 show_all_children();
74 int ComboBoxToJLPT(const Gtk::ComboBoxText& c) {
75 int i;
76 Glib::ustring uStr = c.get_active_text();
77 for(i=0;i<jlptStrCount;i++)
78 if(uStr==jlptStrs[i]) break;
79 if(i<4) return 4-i; /* 0-3 -> JLPT Level 4-1 */
80 assert(0 && "Error: i>=4, should be less than 4.");
81 return 0; /* No jlpt listed in KANJIDIC */
84 int DialogAddKanjiByJLPT::GetLowLevel() {
85 return ComboBoxToJLPT(comboLowLevel);
88 int DialogAddKanjiByJLPT::GetHighLevel() {
89 return ComboBoxToJLPT(comboHighLevel);
92 void DialogAddKanjiByJLPT::OnOK() {
93 OKProc();
96 void DialogAddKanjiByJLPT::OnCancel() {
97 CancelProc();
100 void DialogAddKanjiByJLPT::OnLowValChange() {
101 int low, high;
102 low = GetLowLevel();
103 high = GetHighLevel();
104 /* Remember: with JLPT levels, a lower value is a higher level */
105 if(low<high)
106 comboHighLevel.set_active_text(comboLowLevel.get_active_text());
109 void DialogAddKanjiByJLPT::OnHighValChange() {
110 int low, high;
111 low = GetLowLevel();
112 high = GetHighLevel();
113 /* See note in above function. */
114 if(high>low)
115 comboLowLevel.set_active_text(comboHighLevel.get_active_text());
118 void DialogAddKanjiByJLPT::OKProc() {
119 int l = GetLowLevel();
120 int h = GetHighLevel();
121 if(h>l) {
122 Gtk::MessageDialog md
123 (*this, _("Please specify your level range from highest to lowest "
124 "level number. (Example: Level 4 to level 3)"));
125 md.set_title(_("Bad JLPT level range"));
126 md.run();
127 } else
128 response(Gtk::RESPONSE_OK);
131 void DialogAddKanjiByJLPT::CancelProc() {
132 response(Gtk::RESPONSE_CANCEL);