Removed underscore from KanjiPad Search/Clear buttons on Windows builds until properl...
[jben.git] / panel_worddict.cpp
blob30f3f8ec576e8b66a337d7603355b303d58c3cb8
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_worddict.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_worddict.h"
25 #include "global.h"
26 #include "jutils.h"
28 enum {
29 ID_textSearch=1,
30 ID_btnPrev,
31 ID_btnNext,
32 ID_btnRand,
33 ID_textIndex
36 BEGIN_EVENT_TABLE(PanelWordDict, wxPanel)
37 EVT_TEXT_ENTER(ID_textSearch, PanelWordDict::OnSearch)
38 EVT_BUTTON(ID_btnPrev, PanelWordDict::OnPrevious)
39 EVT_BUTTON(ID_btnNext, PanelWordDict::OnNext)
40 EVT_BUTTON(ID_btnRand, PanelWordDict::OnRandom)
41 EVT_TEXT_ENTER(ID_textIndex, PanelWordDict::OnIndexUpdate)
42 END_EVENT_TABLE()
44 PanelWordDict::PanelWordDict(wxWindow *owner) : RedrawablePanel(owner) {
45 /* searchBox controls */
46 wxStaticText *labelSearch = new wxStaticText(this,-1,_T("Enter word or expression:"));
47 textSearch = new wxTextCtrl(this, ID_textSearch, _T(""),
48 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
49 /* Our output window */
50 htmlOutput = new wxHtmlWindow(this, -1,
51 wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER);
52 /* navBox controls */
53 wxButton *btnPrev = new wxButton(this, ID_btnPrev, _T("<< Previous"));
54 wxButton *btnNext = new wxButton(this, ID_btnNext, _T("Next >>"));
55 wxButton *btnRand = new wxButton(this, ID_btnRand, _T("Random"));
56 /*btnRand->Enable(false);*/
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 vocab"));
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 vocab (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 PanelWordDict::OnSearch(wxCommandEvent& event) {
98 SetSearchString(textSearch->GetValue());
99 this->Redraw();
102 void PanelWordDict::SetSearchString(const wxString& searchString) {
103 currentSearchString = searchString;
104 currentIndex = jben->vocabList->GetIndexByWord(searchString);
107 void PanelWordDict::Redraw() {
108 /* If currentIndex has been changed, update any necessary data. */
109 if(currentIndex!=-1) {
110 wxString newVocab = (*jben->vocabList)[currentIndex];
111 if(newVocab==_T("")) /* The returned may be 0 if the currentIndex no longer refers to a valid character. */
112 currentIndex = -1; /* In this case, we'll reset our index to -1. */
113 else if(currentSearchString!=newVocab)
114 SetSearchString(newVocab);
117 /* Update most controls */
118 textSearch->ChangeValue(currentSearchString);
119 /* currentIndex is 0-based, so don't forget to adjust it. */
120 textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));
121 lblIndex->SetLabel(wxString::Format(_T(" of %d vocab"), jben->vocabList->Size()));
122 /* We need to tell our sizer to refresh to accomodate the resizing of the label. */
123 this->GetSizer()->Layout();
125 /* Update our output window */
126 UpdateHtmlOutput(); /* Might want to make this conditionally called in the future for performance. */
129 void PanelWordDict::UpdateHtmlOutput() {
130 list<int> resultList;
132 wxString html = _T("<html><body><font face=\"Serif\">");
133 if(currentSearchString.length()==0) {
134 html.append(_T("No search has been entered."));
135 } else {
136 /* Get search results string */
137 if(jben->edict->Search(currentSearchString, resultList)) {
138 /* Create merged wx-compatible results string */
139 wxString resultString, temp;
140 for(list<int>::iterator li = resultList.begin();
141 li!=resultList.end();
142 li++) {
143 if(resultString.length()!=0) resultString.append(_T('\n'));
144 UTF8ToWx(jben->edict->GetEdictString(*li), temp);
145 resultString.append(temp);
147 /* Convert search results to destination format
148 For now: HTML
149 Later: wxWidgets Rich Text */
150 resultString = jben->edict->ResultToHTML(resultString);
151 html.append(resultString);
152 } else {
153 html.append(_T("No results were found."));
156 html.append(_T("</font></body></html>"));
158 htmlOutput->SetPage(html);
161 void PanelWordDict::OnPrevious(wxCommandEvent& event) {
162 currentIndex--;
163 if(currentIndex<0) currentIndex = jben->vocabList->Size()-1;
165 this->Redraw();
168 void PanelWordDict::OnNext(wxCommandEvent& event) {
169 int listSize = jben->vocabList->Size();
170 currentIndex++;
171 if(currentIndex>=listSize) currentIndex=0;
172 if(listSize==0) currentIndex=-1;
173 this->Redraw();
176 /* Very quick and dirty pseudorandom jump */
177 void PanelWordDict::OnRandom(wxCommandEvent& event) {
178 int listSize = jben->vocabList->Size();
179 if(listSize>0) {
180 currentIndex = rand() % listSize;
181 this->Redraw();
185 void PanelWordDict::OnIndexUpdate(wxCommandEvent& event) {
186 long l;
187 wxString str = textIndex->GetValue();
188 if(str.ToLong(&l) && (l>0) && (l <= jben->vocabList->Size())) {
189 currentIndex = (int)(l-1);
190 this->Redraw();
191 } else textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));