2001-07-29 10:42:11 +00:00
|
|
|
/**
|
|
|
|
* \file FormThesaurus.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-07-29 10:42:11 +00:00
|
|
|
*
|
2002-09-05 14:10:50 +00:00
|
|
|
* \author Edwin Leuven
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2001-07-29 10:42:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ControlThesaurus.h"
|
|
|
|
#include "FormThesaurus.h"
|
2002-06-13 13:43:51 +00:00
|
|
|
#include "forms/form_thesaurus.h"
|
|
|
|
#include "xformsBC.h"
|
|
|
|
#include "xforms_helpers.h"
|
2001-07-29 10:42:11 +00:00
|
|
|
#include "debug.h"
|
2002-06-13 13:43:51 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include FORMS_H_LOCATION
|
|
|
|
#include <cctype>
|
2001-07-29 10:42:11 +00:00
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
using std::isupper;
|
|
|
|
using std::islower;
|
|
|
|
#endif
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
2002-06-13 13:43:51 +00:00
|
|
|
typedef FormCB<ControlThesaurus, FormDB<FD_thesaurus> > base_class;
|
2001-07-29 10:42:11 +00:00
|
|
|
|
2002-03-11 22:47:41 +00:00
|
|
|
|
2002-08-12 14:28:43 +00:00
|
|
|
FormThesaurus::FormThesaurus()
|
|
|
|
: base_class(_("LyX: Thesaurus"), false),
|
2002-03-11 22:47:41 +00:00
|
|
|
clickline_(-1)
|
2001-07-29 10:42:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FormThesaurus::build()
|
|
|
|
{
|
2002-06-13 13:43:51 +00:00
|
|
|
dialog_.reset(build_thesaurus(this));
|
2002-03-11 18:24:31 +00:00
|
|
|
|
|
|
|
fl_set_input_return(dialog_->input_entry, FL_RETURN_CHANGED);
|
|
|
|
fl_set_input_return(dialog_->input_replace, FL_RETURN_CHANGED);
|
|
|
|
|
|
|
|
setPrehandler(dialog_->input_entry);
|
|
|
|
setPrehandler(dialog_->input_replace);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
// Manage the ok, apply and cancel/close buttons
|
|
|
|
bc().setCancel(dialog_->button_close);
|
|
|
|
bc().addReadOnly(dialog_->input_replace);
|
|
|
|
|
|
|
|
fl_set_input_return(dialog_->input_entry, FL_RETURN_END_CHANGED);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FormThesaurus::update()
|
|
|
|
{
|
|
|
|
if (!dialog_.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
string const & str_ = controller().text();
|
|
|
|
setEnabled(dialog_->button_replace, !str_.empty());
|
|
|
|
fl_set_input(dialog_->input_replace, "");
|
2002-01-13 01:46:33 +00:00
|
|
|
updateMeanings(str_);
|
2001-07-29 10:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
void FormThesaurus::updateMeanings(string const & str)
|
2001-07-29 10:42:11 +00:00
|
|
|
{
|
2002-01-13 01:46:33 +00:00
|
|
|
fl_clear_browser(dialog_->browser_meanings);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
fl_set_input(dialog_->input_entry, str.c_str());
|
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
fl_set_browser_topline(dialog_->browser_meanings, 1);
|
|
|
|
|
|
|
|
fl_freeze_form(form());
|
|
|
|
|
|
|
|
Thesaurus::Meanings meanings = controller().getMeanings(str);
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
Thesaurus::Meanings::const_iterator cit = meanings.begin();
|
|
|
|
Thesaurus::Meanings::const_iterator end = meanings.end();
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
fl_add_browser_line(dialog_->browser_meanings,
|
|
|
|
cit->first.c_str());
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
vector<string> const & tmpvec = cit->second;
|
|
|
|
vector<string>::const_iterator cit2 = tmpvec.begin();
|
|
|
|
vector<string>::const_iterator end2 = tmpvec.end();
|
|
|
|
for (; cit2 != end2; ++cit2) {
|
|
|
|
string ent = " ";
|
|
|
|
ent += *cit2;
|
|
|
|
fl_add_browser_line(dialog_->browser_meanings,
|
|
|
|
ent.c_str());
|
2002-01-13 01:46:33 +00:00
|
|
|
}
|
2002-02-16 15:59:55 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
fl_unfreeze_form(form());
|
|
|
|
fl_redraw_form(form());
|
2001-07-29 10:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FormThesaurus::setReplace(string const & templ, string const & nstr)
|
|
|
|
{
|
|
|
|
string str(nstr);
|
|
|
|
|
|
|
|
// the following mechanism makes sure we replace "House" with "Home",
|
|
|
|
// "HOUSE" with "HOME" etc.
|
|
|
|
|
|
|
|
bool all_lower = true;
|
|
|
|
bool all_upper = true;
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
string::const_iterator beg = templ.begin();
|
|
|
|
string::const_iterator end = templ.end();
|
|
|
|
string::const_iterator cit = beg;
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
if (isupper(*cit))
|
2001-07-29 10:42:11 +00:00
|
|
|
all_lower = false;
|
2002-02-16 15:59:55 +00:00
|
|
|
if (islower(*cit))
|
2001-07-29 10:42:11 +00:00
|
|
|
all_upper = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_lower) {
|
|
|
|
str = lowercase(nstr);
|
|
|
|
} else if (all_upper) {
|
|
|
|
str = uppercase(nstr);
|
|
|
|
} else if (templ.size() > 0 && isupper(templ[0])) {
|
|
|
|
bool rest_lower = true;
|
2002-02-16 15:59:55 +00:00
|
|
|
string::const_iterator cit2 = beg + 1;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
for (; cit2 != end; ++cit2) {
|
|
|
|
if (isupper(*cit2))
|
2001-07-29 10:42:11 +00:00
|
|
|
rest_lower = false;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
if (rest_lower) {
|
|
|
|
str = lowercase(nstr);
|
|
|
|
str[0] = uppercase(nstr[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fl_set_input(dialog_->input_replace, str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ButtonPolicy::SMInput FormThesaurus::input(FL_OBJECT * obj, long)
|
|
|
|
{
|
|
|
|
if (obj == dialog_->input_entry) {
|
2002-07-28 22:50:13 +00:00
|
|
|
string s = trim(fl_get_input(dialog_->input_entry));
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
updateMeanings(s);
|
|
|
|
|
|
|
|
if (s.empty()) {
|
2001-07-29 10:42:11 +00:00
|
|
|
fl_set_input(dialog_->input_replace, "");
|
|
|
|
return ButtonPolicy::SMI_APPLY;
|
|
|
|
}
|
2002-01-15 15:31:19 +00:00
|
|
|
return ButtonPolicy::SMI_NOOP;
|
|
|
|
|
2001-07-29 10:42:11 +00:00
|
|
|
} else if (obj == dialog_->button_replace) {
|
|
|
|
string rep(fl_get_input(dialog_->input_replace));
|
|
|
|
if (!rep.empty())
|
|
|
|
controller().replace(fl_get_input(dialog_->input_replace));
|
|
|
|
clickline_ = -1;
|
2002-01-13 01:46:33 +00:00
|
|
|
updateMeanings(rep);
|
2001-07-29 10:42:11 +00:00
|
|
|
return ButtonPolicy::SMI_APPLY;
|
2002-01-13 01:46:33 +00:00
|
|
|
} else if (obj != dialog_->browser_meanings) {
|
|
|
|
return ButtonPolicy::SMI_NOOP;
|
2001-07-29 10:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-01-15 15:31:19 +00:00
|
|
|
int const line = fl_get_browser(obj);
|
|
|
|
if (line > 0) {
|
|
|
|
setReplace(fl_get_input(dialog_->input_entry),
|
2002-07-28 22:50:13 +00:00
|
|
|
trim(fl_get_browser_line(obj, line)));
|
2002-01-15 15:31:19 +00:00
|
|
|
}
|
2001-07-29 10:42:11 +00:00
|
|
|
|
2002-01-13 01:46:33 +00:00
|
|
|
if (clickline_ == fl_get_browser(obj)) {
|
|
|
|
updateMeanings(fl_get_input(dialog_->input_replace));
|
|
|
|
clickline_ = -1;
|
|
|
|
} else {
|
|
|
|
clickline_ = fl_get_browser(obj);
|
2001-07-29 10:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ButtonPolicy::SMI_VALID;
|
|
|
|
}
|