Renamed kpengine to jben_kpengine and made its data dir relocatable.
[jben.git] / panel_kanjidict.cpp
blob193b36d89e55692e54b0e1321eb9a97f97e58c7d
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 "jben.h"
26 #include "kdict.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 currentSearchString = searchString;
104 int len = currentSearchString.length();
105 if(len>1) {
106 currentKanji = _T('\0');
107 currentIndex = -1;
108 } else {
109 currentKanji = searchString[0];
110 currentIndex = jben->kanjiList->GetIndexByChar(searchString[0]);
114 void PanelKanjiDict::Redraw() {
115 /* If currentIndex has been changed, update any necessary data. */
116 if(currentIndex!=-1) {
117 wxChar newKanji = (*jben->kanjiList)[currentIndex];
118 if(newKanji==_T('\0')) /* The returned may be 0 if the currentIndex no longer refers to a valid character. */
119 currentIndex = -1; /* In this case, we'll reset our index to -1. */
120 else if(currentKanji!=newKanji)
121 SetSearchString(wxString(newKanji));
124 /* Update most controls */
125 textSearch->ChangeValue(currentSearchString);
126 textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));
127 lblIndex->SetLabel(wxString::Format(_T(" of %d kanji"), jben->kanjiList->Size()));
128 /* We need to tell our sizer to refresh to accomodate the resizing of the label. */
129 this->GetSizer()->Layout();
131 /* Update our output window */
132 UpdateHtmlOutput(); /* Might want to make this conditionally called in the future for performance. */
135 void PanelKanjiDict::UpdateHtmlOutput() {
136 const KDict* kd = KDict::Get();
137 wxString html = _T("<html><body><font face=\"Serif\">");
138 wxString htmlContent, kanjiEntry;
140 wxChar c;
141 int len = currentSearchString.length();
142 for(int i=0;i<len;i++) {
143 c = currentSearchString[i];
144 kanjiEntry = kd->GetKanjidicStr(c);
145 if(kanjiEntry.length()>0) {
146 htmlContent.append(KDict::KanjidicToHtml(kanjiEntry.c_str()));
150 if(htmlContent.length()>0)
151 html.append(htmlContent);
152 else
153 html.append(_T("No kanji have been selected."));
155 html.append(_T("</font></body></html>"));
156 htmlOutput->SetPage(html);
159 void PanelKanjiDict::OnPrevious(wxCommandEvent& event) {
160 currentIndex--;
161 if(currentIndex<0) currentIndex = jben->kanjiList->Size()-1;
163 this->Redraw();
166 void PanelKanjiDict::OnNext(wxCommandEvent& event) {
167 int listSize = jben->kanjiList->Size();
168 currentIndex++;
169 if(currentIndex>=listSize) currentIndex=0;
170 if(listSize==0) currentIndex=-1;
171 this->Redraw();
174 /* Very quick and dirty pseudorandom jump */
175 void PanelKanjiDict::OnRandom(wxCommandEvent& event) {
176 int listSize = jben->kanjiList->Size();
177 if(listSize>0) {
178 currentIndex = rand() % listSize;
179 this->Redraw();
183 void PanelKanjiDict::OnIndexUpdate(wxCommandEvent& event) {
184 long l;
185 wxString str = textIndex->GetValue();
186 if(str.ToLong(&l) && (l>0) && (l <= jben->kanjiList->Size())) {
187 currentIndex = (int)(l-1);
188 this->Redraw();
189 } else textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));