Thesaurus implemented

Originally committed to SVN as r610.
This commit is contained in:
Rodrigo Braz Monteiro 2006-12-25 22:21:44 +00:00
parent 081c24efc7
commit 17f5c8e6d9
5 changed files with 87 additions and 26 deletions

View File

@ -404,6 +404,7 @@ BEGIN_EVENT_TABLE(SubsTextEditCtrl,wxScintilla)
EVT_MENU(EDIT_MENU_SELECT_ALL,SubsTextEditCtrl::OnSelectAll)
EVT_MENU(EDIT_MENU_ADD_TO_DICT,SubsTextEditCtrl::OnAddToDictionary)
EVT_MENU_RANGE(EDIT_MENU_SUGGESTIONS,EDIT_MENU_SUGGESTIONS+16,SubsTextEditCtrl::OnUseSuggestion)
EVT_MENU_RANGE(EDIT_MENU_THESAURUS_SUGS,EDIT_MENU_THESAURUS_SUGS+2000,SubsTextEditCtrl::OnUseThesaurusSuggestion)
END_EVENT_TABLE()
@ -461,16 +462,43 @@ void SubsTextEditCtrl::ShowPopupMenu(int activePos) {
// Thesaurus
if (thesaurus) {
// Get suggestions
// Get results
ThesaurusEntryArray result;
thesaurus->Lookup(currentWord,result);
// Compile list
thesSugs.Clear();
thesSugs = thesaurus->GetSuggestions(currentWord);
for (unsigned int i=0;i<result.size();i++) {
for (unsigned int j=0;j<result[i].words.Count();j++) {
thesSugs.Add(result[i].words[j]);
}
}
// Build menu
int nSugs = thesSugs.Count();
for (int i=0;i<nSugs;i++) menu.Append(EDIT_MENU_THESAURUS_SUGS+i,thesSugs[i]);
int curThesEntry = 0;
for (unsigned int i=0;i<result.size();i++) {
// Single word, insert directly
if (result[i].words.Count() == 1) {
menu.Append(EDIT_MENU_THESAURUS_SUGS+curThesEntry,result[i].name);
curThesEntry++;
}
// Multiple, create submenu
else {
// Insert entries
wxMenu *subMenu = new wxMenu();
for (unsigned int j=0;j<result[i].words.Count();j++) {
subMenu->Append(EDIT_MENU_THESAURUS_SUGS+curThesEntry,result[i].words[j]);
curThesEntry++;
}
// Insert submenu
menu.AppendSubMenu(subMenu,result[i].name);
}
}
// No suggestions
if (!nSugs) menu.Append(EDIT_MENU_THESAURUS,_("No thesaurus suggestions"))->Enable(false);
if (!result.size()) menu.Append(EDIT_MENU_THESAURUS,_("No thesaurus suggestions"))->Enable(false);
// Separator
menu.AppendSeparator();
@ -635,3 +663,24 @@ void SubsTextEditCtrl::OnUseSuggestion(wxCommandEvent &event) {
SetSelection(start,start+suggestion.Length());
SetFocus();
}
////////////////////////////
// Use thesaurus suggestion
void SubsTextEditCtrl::OnUseThesaurusSuggestion(wxCommandEvent &event) {
// Get suggestion
wxString suggestion = thesSugs[event.GetId()-EDIT_MENU_THESAURUS_SUGS];
// Get boundaries of text being replaced
int start,end;
GetBoundsOfWordAtPosition(currentWordPos,start,end);
// Replace
wxString text = GetText();
SetText(text.Left(MAX(0,start)) + suggestion + text.Mid(end+1));
// Set selection
SetSelection(start,start+suggestion.Length());
SetFocus();
}

View File

@ -79,6 +79,7 @@ private:
void OnSelectAll(wxCommandEvent &event);
void OnAddToDictionary(wxCommandEvent &event);
void OnUseSuggestion(wxCommandEvent &event);
void OnUseThesaurusSuggestion(wxCommandEvent &event);
public:
SubsEditBox *control;

View File

@ -40,6 +40,21 @@
///////////
// Headers
#include <wx/wxprec.h>
#include <vector>
/////////////////////////
// Thesaurus entry class
class ThesaurusEntry {
public:
wxString name;
wxArrayString words;
};
/////////////////////////
// Thesaurus entry array
typedef std::vector<ThesaurusEntry> ThesaurusEntryArray;
///////////////////////
@ -51,7 +66,7 @@ public:
Thesaurus() {}
virtual ~Thesaurus() {}
virtual wxArrayString GetSuggestions(wxString word)=0;
virtual void Lookup(wxString word,ThesaurusEntryArray &result)=0;
virtual wxArrayString GetLanguageList()=0;
virtual void SetLanguage(wxString language)=0;
};

View File

@ -60,29 +60,25 @@ MySpellThesaurus::~MySpellThesaurus() {
///////////////////
// Get suggestions
wxArrayString MySpellThesaurus::GetSuggestions(wxString word) {
// Array
wxArrayString suggestions;
void MySpellThesaurus::Lookup(wxString word,ThesaurusEntryArray &result) {
// Loaded?
if (!mythes) return;
// Get suggestions
if (mythes) {
// Grab raw from MyThes
mentry *me;
wxCharBuffer buf = word.mb_str(wxConvUTF8);
int n = mythes->Lookup(buf,strlen(buf),&me);
// Grab raw from MyThes
mentry *me;
wxCharBuffer buf = word.Lower().mb_str(wxConvUTF8);
int n = mythes->Lookup(buf,strlen(buf),&me);
// Each entry
for (int i=0;i<n;i++) {
suggestions.Add(wxString(me[i].defn,wxConvUTF8));
for (int j=0;j<me[i].count;j++) suggestions.Add(wxString(_T("+")) + wxString(me[i].psyns[j],wxConvUTF8));
}
// Delete
mythes->CleanUpAfterLookup(&me,n);
// Each entry
for (int i=0;i<n;i++) {
ThesaurusEntry entry;
entry.name = wxString(me[i].defn,wxConvUTF8);
for (int j=0;j<me[i].count;j++) entry.words.Add(wxString(me[i].psyns[j],wxConvUTF8));
result.push_back(entry);
}
// Return them
return suggestions;
// Clean up
mythes->CleanUpAfterLookup(&me,n);
}

View File

@ -57,7 +57,7 @@ public:
MySpellThesaurus();
~MySpellThesaurus();
wxArrayString GetSuggestions(wxString word);
void Lookup(wxString word,ThesaurusEntryArray &result);
wxArrayString GetLanguageList();
void SetLanguage(wxString language);
};