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"
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
)
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
);
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
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
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());
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."));
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();
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
149 Later: wxWidgets Rich Text */
150 resultString
= jben
->edict
->ResultToHTML(resultString
);
151 html
.append(resultString
);
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
) {
163 if(currentIndex
<0) currentIndex
= jben
->vocabList
->Size()-1;
168 void PanelWordDict::OnNext(wxCommandEvent
& event
) {
169 int listSize
= jben
->vocabList
->Size();
171 if(currentIndex
>=listSize
) currentIndex
=0;
172 if(listSize
==0) currentIndex
=-1;
176 /* Very quick and dirty pseudorandom jump */
177 void PanelWordDict::OnRandom(wxCommandEvent
& event
) {
178 int listSize
= jben
->vocabList
->Size();
180 currentIndex
= rand() % listSize
;
185 void PanelWordDict::OnIndexUpdate(wxCommandEvent
& event
) {
187 wxString str
= textIndex
->GetValue();
188 if(str
.ToLong(&l
) && (l
>0) && (l
<= jben
->vocabList
->Size())) {
189 currentIndex
= (int)(l
-1);
191 } else textIndex
->ChangeValue(wxString::Format(_T("%d"), currentIndex
+1));