Major directory structure changes, plus a bug fix.
[jben.git] / src / panel_worddict.cpp
blob98e20c190a9e95a77dc902760471916a68d6aad0
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 "jutils.h"
26 #include "wdict.h"
27 #include "listmanager.h"
28 #include "encoding_convert.h"
29 #include "errorlog.h"
31 enum {
32 ID_textSearch=1,
33 ID_btnPrev,
34 ID_btnNext,
35 ID_btnRand,
36 ID_textIndex
39 BEGIN_EVENT_TABLE(PanelWordDict, wxPanel)
40 EVT_TEXT_ENTER(ID_textSearch, PanelWordDict::OnSearch)
41 EVT_BUTTON(ID_btnPrev, PanelWordDict::OnPrevious)
42 EVT_BUTTON(ID_btnNext, PanelWordDict::OnNext)
43 EVT_BUTTON(ID_btnRand, PanelWordDict::OnRandom)
44 EVT_TEXT_ENTER(ID_textIndex, PanelWordDict::OnIndexUpdate)
45 END_EVENT_TABLE()
47 PanelWordDict::PanelWordDict(wxWindow *owner) : RedrawablePanel(owner) {
48 /* searchBox controls */
49 wxStaticText *labelSearch = new wxStaticText(this,-1,_T("Enter word or expression:"));
50 textSearch = new wxTextCtrl(this, ID_textSearch, _T(""),
51 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
52 /* Our output window */
53 htmlOutput = new wxHtmlWindow(this, -1,
54 wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER);
55 /* navBox controls */
56 wxButton *btnPrev = new wxButton(this, ID_btnPrev, _T("<< Previous"));
57 wxButton *btnNext = new wxButton(this, ID_btnNext, _T("Next >>"));
58 wxButton *btnRand = new wxButton(this, ID_btnRand, _T("Random"));
59 /*btnRand->Enable(false);*/
60 textIndex = new wxTextCtrl(this, ID_textIndex, _T("0"),
61 wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
62 textIndex->SetMaxLength(4);
63 lblIndex = new wxStaticText(this, wxID_ANY, _T(" of 0 vocab"));
65 /* searchBox: Add both components horizonally, vertically centered,
66 and space of 10px between them. No outer spacing; this'll be
67 handled in the parent sizer. */
68 wxBoxSizer *searchBox = new wxBoxSizer(wxHORIZONTAL);
69 searchBox->Add(labelSearch, 0, wxALIGN_CENTER_VERTICAL);
70 searchBox->Add(textSearch, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
71 | wxLEFT, 10);
73 /* navBox: */
74 wxBoxSizer *navBox = new wxBoxSizer(wxHORIZONTAL);
75 navBox->Add(btnPrev, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
76 navBox->Add(btnNext, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
77 navBox->Add(btnRand, 0, wxALIGN_CENTER_VERTICAL);
78 navBox->AddStretchSpacer(1);
79 navBox->Add(textIndex, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
80 navBox->Add(lblIndex, 0, wxALIGN_CENTER_VERTICAL);
82 /* windowBox: 10px around/between all controls. */
83 wxBoxSizer *windowBox = new wxBoxSizer(wxVERTICAL);
84 windowBox->Add(searchBox, 0, wxEXPAND
85 | wxLEFT | wxTOP | wxRIGHT, 10);
86 windowBox->Add(htmlOutput, 1, wxEXPAND
87 | wxALL, 10);
88 windowBox->Add(navBox, 0, wxEXPAND
89 | wxLEFT | wxBOTTOM | wxRIGHT, 10);
91 /* Other initialization */
92 currentIndex = -1; /* Currently selected vocab (none) is not in the list, so set to -1 */
93 UpdateHtmlOutput(); /* Do an initial update of the output window */
95 /* Finally: Apply window sizer and fit window */
96 this->SetSizerAndFit(windowBox);
100 void PanelWordDict::OnSearch(wxCommandEvent& event) {
101 SetSearchString(textSearch->GetValue());
102 this->Redraw();
105 void PanelWordDict::SetSearchString(const wxString& searchString) {
106 ListManager* lm = ListManager::Get();
107 currentSearchString = searchString;
108 currentIndex = lm->VList()->GetIndexByWord(searchString.c_str());
111 void PanelWordDict::Redraw() {
112 /* If currentIndex has been changed, update any necessary data. */
113 ListManager* lm = ListManager::Get();
114 if(currentIndex!=-1) {
115 wxString newVocab = (*lm->VList())[currentIndex];
116 if(newVocab==_T("")) /* The returned may be 0 if the currentIndex no longer refers to a valid character. */
117 currentIndex = -1; /* In this case, we'll reset our index to -1. */
118 else if(currentSearchString!=newVocab)
119 SetSearchString(newVocab);
122 /* Update most controls */
123 textSearch->ChangeValue(currentSearchString);
124 /* currentIndex is 0-based, so don't forget to adjust it. */
125 textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));
126 lblIndex->SetLabel(wxString::Format(_T(" of %d vocab"), lm->VList()->Size()));
127 /* We need to tell our sizer to refresh to accomodate the resizing of the label. */
128 this->GetSizer()->Layout();
130 /* Update our output window */
131 UpdateHtmlOutput(); /* Might want to make this conditionally called in the future for performance. */
134 void PanelWordDict::UpdateHtmlOutput() {
135 list<int> resultList;
136 const WDict *wd = WDict::Get();
138 wxString html = _T("<html><body><font face=\"Serif\">");
139 if(currentSearchString.length()==0) {
140 html.append(_T("No search has been entered."));
141 } else {
142 /* Get search results string */
143 if(wd->Search(currentSearchString.c_str(), resultList)) {
144 /* Create merged wx-compatible results string */
145 wxString resultString, temp;
146 for(list<int>::iterator li = resultList.begin();
147 li!=resultList.end();
148 li++) {
149 if(resultString.length()!=0) resultString.append(_T('\n'));
150 temp = utfconv_mw(wd->GetEdictString(*li));
151 resultString.append(temp);
153 /* Convert search results to destination format
154 For now: HTML
155 Later: wxWidgets Rich Text */
156 resultString = wd->ResultToHTML(resultString.c_str());
157 html.append(resultString);
158 } else {
159 html.append(_T("No results were found."));
162 html.append(_T("</font></body></html>"));
164 htmlOutput->SetPage(html);
167 void PanelWordDict::OnPrevious(wxCommandEvent& event) {
168 ListManager* lm = ListManager::Get();
169 currentIndex--;
170 if(currentIndex<0) currentIndex = lm->VList()->Size()-1;
172 this->Redraw();
175 void PanelWordDict::OnNext(wxCommandEvent& event) {
176 ListManager* lm = ListManager::Get();
177 int listSize = lm->VList()->Size();
178 currentIndex++;
179 if(currentIndex>=listSize) currentIndex=0;
180 if(listSize==0) currentIndex=-1;
181 this->Redraw();
184 /* Very quick and dirty pseudorandom jump */
185 void PanelWordDict::OnRandom(wxCommandEvent& event) {
186 ListManager* lm = ListManager::Get();
187 int listSize = lm->VList()->Size();
188 if(listSize>0) {
189 currentIndex = rand() % listSize;
190 this->Redraw();
194 void PanelWordDict::OnIndexUpdate(wxCommandEvent& event) {
195 ListManager* lm = ListManager::Get();
196 long l;
197 wxString str = textIndex->GetValue();
198 if(str.ToLong(&l) && (l>0) && (l <= lm->VList()->Size())) {
199 currentIndex = (int)(l-1);
200 this->Redraw();
201 } else textIndex->ChangeValue(wxString::Format(_T("%d"), currentIndex+1));