Major directory structure changes, plus a bug fix.
[jben.git] / src / panel_kanjidict.cpp
blob6a4bb8b65b38156eacda4e8d475c251e7fe45a5c
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_kanjidict.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_kanjidict.h"
25 #include "kdict.h"
26 #include "listmanager.h"
27 #include <cstdlib>
29 enum {
30 ID_textSearch=1,
31 ID_btnPrev,
32 ID_btnNext,
33 ID_btnRand,
34 ID_textIndex
37 BEGIN_EVENT_TABLE(PanelKanjiDict, wxPanel)
38 EVT_TEXT_ENTER(ID_textSearch, PanelKanjiDict::OnSearch)
39 EVT_BUTTON(ID_btnPrev, PanelKanjiDict::OnPrevious)
40 EVT_BUTTON(ID_btnNext, PanelKanjiDict::OnNext)
41 EVT_BUTTON(ID_btnRand, PanelKanjiDict::OnRandom)
42 EVT_TEXT_ENTER(ID_textIndex, PanelKanjiDict::OnIndexUpdate)
43 END_EVENT_TABLE()
45 PanelKanjiDict::PanelKanjiDict(wxWindow *owner) : RedrawablePanel(owner) {
46 /* searchBox controls */
47 wxStaticText *labelSearch = new wxStaticText(this,-1,_T("Enter kanji:"));
48 textSearch = new wxTextCtrl(this, ID_textSearch, _T(""),
49 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
50 /* Our output window */
51 htmlOutput = new wxHtmlWindow(this, -1,
52 wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER);
53 /* navBox controls */
54 wxButton *btnPrev = new wxButton(this, ID_btnPrev, _T("<< Previous"));
55 wxButton *btnNext = new wxButton(this, ID_btnNext, _T("Next >>"));
56 wxButton *btnRand = new wxButton(this, ID_btnRand, _T("Random"));
57 textIndex = new wxTextCtrl(this, ID_textIndex, _T("0"),
58 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
59 textIndex->SetMaxLength(4);
60 lblIndex = new wxStaticText(this, wxID_ANY, _T(" of 0 kanji"));
62 /* searchBox: Add both components horizonally, vertically centered,
63 and space of 10px between them. No outer spacing; this'll be
64 handled in the parent sizer. */
65 wxBoxSizer *searchBox = new wxBoxSizer(wxHORIZONTAL);
66 searchBox->Add(labelSearch, 0, wxALIGN_CENTER_VERTICAL);
67 searchBox->Add(textSearch, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
68 | wxLEFT, 10);
70 /* navBox: */
71 wxBoxSizer *navBox = new wxBoxSizer(wxHORIZONTAL);
72 navBox->Add(btnPrev, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
73 navBox->Add(btnNext, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
74 navBox->Add(btnRand, 0, wxALIGN_CENTER_VERTICAL);
75 navBox->AddStretchSpacer(1);
76 navBox->Add(textIndex, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
77 navBox->Add(lblIndex, 0, wxALIGN_CENTER_VERTICAL);
79 /* windowBox: 10px around/between all controls. */
80 wxBoxSizer *windowBox = new wxBoxSizer(wxVERTICAL);
81 windowBox->Add(searchBox, 0, wxEXPAND
82 | wxLEFT | wxTOP | wxRIGHT, 10);
83 windowBox->Add(htmlOutput, 1, wxEXPAND
84 | wxALL, 10);
85 windowBox->Add(navBox, 0, wxEXPAND
86 | wxLEFT | wxBOTTOM | wxRIGHT, 10);
88 /* Other initialization */
89 currentIndex = -1; /* Currently selected kanji (none) is not in the list, so set to -1 */
90 UpdateHtmlOutput(); /* Do an initial update of the output window */
92 /* Finally: Apply window sizer and fit window */
93 this->SetSizerAndFit(windowBox);
97 void PanelKanjiDict::OnSearch(wxCommandEvent& event) {
98 SetSearchString(textSearch->GetValue());
99 this->Redraw();
102 void PanelKanjiDict::SetSearchString(const wxString& searchString) {
103 ListManager* lm = ListManager::Get();
104 currentSearchString = searchString;
105 int len = currentSearchString.length();
106 if(len>1) {
107 currentKanji = _T('\0');
108 currentIndex = -1;
109 } else {
110 currentKanji = searchString[0];
111 currentIndex = lm->KList()->GetIndexByChar(searchString[0]);
115 void PanelKanjiDict::Redraw() {
116 /* If currentIndex has been changed, update any necessary data. */
117 ListManager* lm = ListManager::Get();
118 if(currentIndex!=-1) {
119 wxChar newKanji = (*lm->KList())[currentIndex];
120 if(newKanji==_T('\0')) /* The returned may be 0 if the currentIndex no longer refers to a valid character. */
121 currentIndex = -1; /* In this case, we'll reset our index to -1. */
122 else if(currentKanji!=newKanji)
123 SetSearchString(wxString(newKanji));
126 /* Update most controls */
127 textSearch->ChangeValue(currentSearchString);
128 textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));
129 lblIndex->SetLabel(wxString::Format(_T(" of %d kanji"), lm->KList()->Size()));
130 /* We need to tell our sizer to refresh to accomodate the resizing of the label. */
131 this->GetSizer()->Layout();
133 /* Update our output window */
134 UpdateHtmlOutput(); /* Might want to make this conditionally called in the future for performance. */
137 void PanelKanjiDict::UpdateHtmlOutput() {
138 const KDict* kd = KDict::Get();
139 wxString html = _T("<html><body><font face=\"Serif\">");
140 wxString htmlContent;
141 const KInfo *ki;
143 wxChar c;
144 int len = currentSearchString.length();
145 for(int i=0;i<len;i++) {
146 c = currentSearchString[i];
147 ki = kd->GetEntry(c);
148 if(ki) htmlContent.append(KDict::KInfoToHtml(*ki));
151 if(htmlContent.length()>0)
152 html.append(htmlContent);
153 else
154 html.append(_T("No kanji have been selected."));
156 html.append(_T("</font></body></html>"));
157 htmlOutput->SetPage(html);
160 void PanelKanjiDict::OnPrevious(wxCommandEvent& event) {
161 ListManager* lm = ListManager::Get();
162 currentIndex--;
163 if(currentIndex<0) currentIndex = lm->KList()->Size()-1;
165 this->Redraw();
168 void PanelKanjiDict::OnNext(wxCommandEvent& event) {
169 ListManager* lm = ListManager::Get();
170 int listSize = lm->KList()->Size();
171 currentIndex++;
172 if(currentIndex>=listSize) currentIndex=0;
173 if(listSize==0) currentIndex=-1;
174 this->Redraw();
177 /* Very quick and dirty pseudorandom jump */
178 void PanelKanjiDict::OnRandom(wxCommandEvent& event) {
179 ListManager* lm = ListManager::Get();
180 int listSize = lm->KList()->Size();
181 if(listSize>0) {
182 currentIndex = rand() % listSize;
183 this->Redraw();
187 void PanelKanjiDict::OnIndexUpdate(wxCommandEvent& event) {
188 ListManager* lm = ListManager::Get();
189 long l;
190 wxString str = textIndex->GetValue();
191 if(str.ToLong(&l) && (l>0) && (l <= lm->KList()->Size())) {
192 currentIndex = (int)(l-1);
193 this->Redraw();
194 } else textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));