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_SELECT_ALL,SubsTextEditCtrl::OnSelectAll)
EVT_MENU(EDIT_MENU_ADD_TO_DICT,SubsTextEditCtrl::OnAddToDictionary) 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_SUGGESTIONS,EDIT_MENU_SUGGESTIONS+16,SubsTextEditCtrl::OnUseSuggestion)
EVT_MENU_RANGE(EDIT_MENU_THESAURUS_SUGS,EDIT_MENU_THESAURUS_SUGS+2000,SubsTextEditCtrl::OnUseThesaurusSuggestion)
END_EVENT_TABLE() END_EVENT_TABLE()
@ -461,16 +462,43 @@ void SubsTextEditCtrl::ShowPopupMenu(int activePos) {
// Thesaurus // Thesaurus
if (thesaurus) { if (thesaurus) {
// Get suggestions // Get results
ThesaurusEntryArray result;
thesaurus->Lookup(currentWord,result);
// Compile list
thesSugs.Clear(); 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 // Build menu
int nSugs = thesSugs.Count(); int curThesEntry = 0;
for (int i=0;i<nSugs;i++) menu.Append(EDIT_MENU_THESAURUS_SUGS+i,thesSugs[i]); 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 // 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 // Separator
menu.AppendSeparator(); menu.AppendSeparator();
@ -635,3 +663,24 @@ void SubsTextEditCtrl::OnUseSuggestion(wxCommandEvent &event) {
SetSelection(start,start+suggestion.Length()); SetSelection(start,start+suggestion.Length());
SetFocus(); 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 OnSelectAll(wxCommandEvent &event);
void OnAddToDictionary(wxCommandEvent &event); void OnAddToDictionary(wxCommandEvent &event);
void OnUseSuggestion(wxCommandEvent &event); void OnUseSuggestion(wxCommandEvent &event);
void OnUseThesaurusSuggestion(wxCommandEvent &event);
public: public:
SubsEditBox *control; SubsEditBox *control;

View File

@ -40,6 +40,21 @@
/////////// ///////////
// Headers // Headers
#include <wx/wxprec.h> #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() {} Thesaurus() {}
virtual ~Thesaurus() {} virtual ~Thesaurus() {}
virtual wxArrayString GetSuggestions(wxString word)=0; virtual void Lookup(wxString word,ThesaurusEntryArray &result)=0;
virtual wxArrayString GetLanguageList()=0; virtual wxArrayString GetLanguageList()=0;
virtual void SetLanguage(wxString language)=0; virtual void SetLanguage(wxString language)=0;
}; };

View File

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

View File

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