Fixed to run with recent versions of gtkmm.
[jben.git] / src / dialog_kanjipretest.cpp
blob6bad782fe416fe3499064f5a7944f0ce66d54029
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_kanjipretest.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_kanjipretest.h"
25 #include <gtkmm/stock.h>
26 #include <gtkmm/frame.h>
27 #include <glibmm/i18n.h>
28 #include "listmanager.h"
30 DialogKanjiPreTest::DialogKanjiPreTest(Gtk::Window& parent)
31 : StoredDialog(_("Kanji Test: Settings"), parent, "gui.dlg.kanjipretest.size"),
32 rdoRandom(_("Choose randomly from list")),
33 rdoIndex(_("Start at index:")),
34 rdoReading(_("Reading kanji")),
35 rdoWriting(_("Writing kanji")),
36 btnCancel(Gtk::Stock::CANCEL),
37 btnStart(_("Start Test"))
39 Gtk::RadioButton::Group g = rdoRandom.get_group();
40 rdoIndex.set_group(g);
41 g = rdoReading.get_group();
42 rdoWriting.set_group(g);
44 spnCount.signal_value_changed()
45 .connect(sigc::mem_fun(*this, &DialogKanjiPreTest::OnKanjiCountChange));
46 rdoRandom.signal_toggled()
47 .connect(sigc::mem_fun(*this, &DialogKanjiPreTest::OnRdoRandom));
48 rdoIndex.signal_toggled()
49 .connect(sigc::mem_fun(*this, &DialogKanjiPreTest::OnRdoStartIndex));
50 btnCancel.signal_clicked()
51 .connect(sigc::mem_fun(*this, &DialogKanjiPreTest::OnCancel));
52 btnStart.signal_clicked()
53 .connect(sigc::mem_fun(*this, &DialogKanjiPreTest::OnStart));
55 int iListSize = ListManager::Get()->KList()->Size();
56 spnIndex.set_sensitive(false);
57 spnCount.set_range(0.0, (double)iListSize);
58 spnCount.set_numeric();
59 spnIndex.set_numeric();
60 spnCount.set_digits(0);
61 spnIndex.set_digits(0);
62 spnCount.set_range(0.0, (double)iListSize);
63 spnCount.set_value(min(20, iListSize));
64 Update(); /* Updates spin button ranges */
65 spnCount.set_width_chars(5);
66 spnIndex.set_width_chars(5);
67 spnCount.set_increments(1,10);
68 spnIndex.set_increments(1,10);
70 Gtk::Frame* pfKanjiSelect
71 = manage(new Gtk::Frame(_("Choose Kanji to Test")));
72 Gtk::Frame* pfTestSelect
73 = manage(new Gtk::Frame(_("Choose Test Type")));
74 Gtk::HBox* phbCount = manage(new Gtk::HBox(false, 5));
75 Gtk::HBox* phbIndex = manage(new Gtk::HBox(false, 5));
76 Gtk::VBox* pvbKanjiSelect = manage(new Gtk::VBox(false, 5));
77 Gtk::VBox* pvbTestSelect = manage(new Gtk::VBox(false, 5));
78 Gtk::Label* plblCount
79 = manage(new Gtk::Label(_("Number of kanji to test:")));
81 Gtk::VBox* pvb = get_vbox();
82 pvb->set_spacing(5);
83 pvb->set_border_width(5);
84 pvb->pack_start(*pfKanjiSelect, Gtk::PACK_SHRINK);
85 pvb->pack_start(*pfTestSelect, Gtk::PACK_SHRINK);
87 pfKanjiSelect->add(*pvbKanjiSelect);
88 pfTestSelect ->add(*pvbTestSelect);
90 pvbKanjiSelect->set_border_width(5);
91 pvbKanjiSelect->pack_start(*phbCount, Gtk::PACK_SHRINK);
92 pvbKanjiSelect->pack_start(rdoRandom, Gtk::PACK_SHRINK);
93 pvbKanjiSelect->pack_start(*phbIndex, Gtk::PACK_SHRINK);
95 pvbTestSelect->set_border_width(5);
96 pvbTestSelect->pack_start(rdoReading, Gtk::PACK_SHRINK);
97 pvbTestSelect->pack_start(rdoWriting, Gtk::PACK_SHRINK);
99 phbCount->pack_start(*plblCount, Gtk::PACK_SHRINK);
100 phbCount->pack_start(spnCount, Gtk::PACK_SHRINK);
102 phbIndex->pack_start(rdoIndex, Gtk::PACK_SHRINK);
103 phbIndex->pack_start(spnIndex, Gtk::PACK_SHRINK);
105 Gtk::ButtonBox* phbb = get_action_area();
106 phbb->pack_start(btnCancel);
107 phbb->pack_start(btnStart);
109 OnKanjiCountChange(); /* Force a button update */
110 show_all_children();
113 void DialogKanjiPreTest::Update() {
114 int iListSize = ListManager::Get()->KList()->Size();
115 if(iListSize>0) {
116 int oldIndex = spnIndex.get_value_as_int();
117 int maxIndex = iListSize - spnCount.get_value_as_int() + 1;
118 spnIndex.set_range(1.0, (double)maxIndex);
119 if(oldIndex < 1) spnIndex.set_value(1.0);
120 if(oldIndex > maxIndex) spnIndex.set_value((double)maxIndex);
121 } else {
122 spnIndex.set_range(0.0, 0.0);
123 spnIndex.set_value(0.0);
127 void DialogKanjiPreTest::OnKanjiCountChange() {
128 int iListSize = ListManager::Get()->KList()->Size();
129 int iCount = spnCount.get_value_as_int();
130 bool bCanStart = btnStart.is_sensitive();
131 if(iListSize > 0) {
132 double dVal = spnIndex.get_value();
133 double dMax = (double)iListSize - (double)iCount + 1.0;
134 spnIndex.set_range(1.0, dMax);
135 dVal = max(dVal, 1.0);
136 dVal = min(dVal, dMax);
137 spnIndex.set_value(dVal);
138 } else {
139 spnIndex.set_range(0.0, 0.0);
140 spnIndex.set_value(0.0);
142 if(iCount > 0) {
143 if(!bCanStart) btnStart.set_sensitive(true);
144 } else {
145 if(bCanStart) btnStart.set_sensitive(false);
149 void DialogKanjiPreTest::OnRdoRandom() {
150 spnIndex.set_sensitive(false);
153 void DialogKanjiPreTest::OnRdoStartIndex() {
154 spnIndex.set_sensitive(true);
157 void DialogKanjiPreTest::OnCancel() {
158 response(Gtk::RESPONSE_CANCEL);
161 void DialogKanjiPreTest::OnStart() {
162 response(Gtk::RESPONSE_OK);