]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSearch.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiSearch.cpp
index 42a74587b4fb6f949ec4dd1ef011d1a65817a2c8..a8f10e7c63cf59b1169b0c5b59911e2cb495c9d8 100644 (file)
@@ -5,6 +5,7 @@
  *
  * \author John Levon
  * \author Edwin Leuven
+ * \author Angus Leeming
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "GuiSearch.h"
+
 #include "qt_helpers.h"
 
+#include "FuncRequest.h"
+#include "lyxfind.h"
+
 #include <QLineEdit>
-#include <QCloseEvent>
+#include <QShowEvent>
 
-using std::string;
+using namespace std;
 
 namespace lyx {
 namespace frontend {
 
-
-/////////////////////////////////////////////////////////////////////
-//
-// GuiSearchDialog
-//
-/////////////////////////////////////////////////////////////////////
-
-
 static void uniqueInsert(QComboBox * box, QString const & text)
 {
-       for (int i = 0; i < box->count(); ++i) {
+       for (int i = box->count(); --i >= 0; )
                if (box->itemText(i) == text)
                        return;
-       }
 
-       box->addItem(text);
+       box->insertItem(0, text);
 }
 
 
-GuiSearchDialog::GuiSearchDialog(GuiSearch * form)
-       : form_(form)
+GuiSearch::GuiSearch(GuiView & lv)
+       : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
 {
        setupUi(this);
 
-       connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
+       connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
        connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
        connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
        connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
-       connect(findCO, SIGNAL(editTextChanged(const QString &)),
+       connect(findCO, SIGNAL(editTextChanged(QString)),
                this, SLOT(findChanged()));
 
        setFocusProxy(findCO);
-}
 
+       bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
+       bc().setCancel(closePB);
+       bc().addReadOnly(replaceCO);
+       bc().addReadOnly(replacePB);
+       bc().addReadOnly(replaceallPB);
 
-void GuiSearchDialog::show()
-{
-       QDialog::show();
-       findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
+       replacePB->setEnabled(false);
+       replaceallPB->setEnabled(false);
 }
 
 
-void GuiSearchDialog::closeEvent(QCloseEvent * e)
+void GuiSearch::showEvent(QShowEvent * e)
 {
-       form_->slotWMHide();
-       e->accept();
+       findPB->setFocus();
+       findCO->lineEdit()->selectAll();
+       GuiDialog::showEvent(e);
 }
 
 
-void GuiSearchDialog::findChanged()
+void GuiSearch::findChanged()
 {
        if (findCO->currentText().isEmpty()) {
                findPB->setEnabled(false);
@@ -79,92 +78,67 @@ void GuiSearchDialog::findChanged()
                replaceallPB->setEnabled(false);
        } else {
                findPB->setEnabled(true);
-               replacePB->setEnabled(!form_->readOnly());
-               replaceallPB->setEnabled(!form_->readOnly());
+               replacePB->setEnabled(!isBufferReadonly());
+               replaceallPB->setEnabled(!isBufferReadonly());
        }
 }
 
 
-void GuiSearchDialog::findClicked()
+void GuiSearch::findClicked()
 {
-       docstring const find = qstring_to_ucs4(findCO->currentText());
-       form_->find(find,
-               caseCB->isChecked(),
-               wordsCB->isChecked(),
-               backwardsCB->isChecked());
+       docstring const needle = qstring_to_ucs4(findCO->currentText());
+       find(needle, caseCB->isChecked(), wordsCB->isChecked(),
+               !backwardsCB->isChecked());
        uniqueInsert(findCO, findCO->currentText());
-       findCO->lineEdit()->setSelection(0, findCO->lineEdit()->text().length());
+       findCO->lineEdit()->selectAll();
 }
 
 
-void GuiSearchDialog::replaceClicked()
+void GuiSearch::replaceClicked()
 {
-       docstring const find = qstring_to_ucs4(findCO->currentText());
-       docstring const replace = qstring_to_ucs4(replaceCO->currentText());
-       form_->replace(find, replace,
-               caseCB->isChecked(),
-               wordsCB->isChecked(),
-               backwardsCB->isChecked(), false);
+       docstring const needle = qstring_to_ucs4(findCO->currentText());
+       docstring const repl = qstring_to_ucs4(replaceCO->currentText());
+       replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
+               !backwardsCB->isChecked(), false);
        uniqueInsert(findCO, findCO->currentText());
        uniqueInsert(replaceCO, replaceCO->currentText());
 }
 
 
-void GuiSearchDialog::replaceallClicked()
+void GuiSearch::replaceallClicked()
 {
-       form_->replace(qstring_to_ucs4(findCO->currentText()),
+       replace(qstring_to_ucs4(findCO->currentText()),
                qstring_to_ucs4(replaceCO->currentText()),
-               caseCB->isChecked(),
-               wordsCB->isChecked(),
-               false, true);
+               caseCB->isChecked(), wordsCB->isChecked(), true, true);
        uniqueInsert(findCO, findCO->currentText());
        uniqueInsert(replaceCO, replaceCO->currentText());
 }
 
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiSearch
-//
-/////////////////////////////////////////////////////////////////////
-
-
-GuiSearch::GuiSearch(GuiDialog & parent)
-       : GuiView<GuiSearchDialog>(parent, _("Find and Replace"))
-{
-}
-
-
-void GuiSearch::build_dialog()
+void GuiSearch::find(docstring const & search, bool casesensitive,
+                        bool matchword, bool forward)
 {
-       dialog_.reset(new GuiSearchDialog(this));
-
-       bc().setCancel(dialog_->closePB);
-       bc().addReadOnly(dialog_->replaceCO);
-       bc().addReadOnly(dialog_->replacePB);
-       bc().addReadOnly(dialog_->replaceallPB);
-
-       dialog_->replacePB->setEnabled(false);
-       dialog_->replaceallPB->setEnabled(false);
+       docstring const data =
+               find2string(search, casesensitive, matchword, forward);
+       dispatch(FuncRequest(LFUN_WORD_FIND, data));
 }
 
 
-void GuiSearch::find(docstring const & str, bool casesens,
-                  bool words, bool backwards)
+void GuiSearch::replace(docstring const & search, docstring const & replace,
+                           bool casesensitive, bool matchword,
+                           bool forward, bool all)
 {
-       controller().find(str, casesens, words, !backwards);
+       docstring const data =
+               replace2string(replace, search, casesensitive,
+                                    matchword, all, forward);
+       dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
 }
 
+Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
 
-void GuiSearch::replace(docstring const & findstr, docstring const & replacestr,
-       bool casesens, bool words, bool backwards, bool all)
-{
-       controller().replace(findstr, replacestr, casesens, words,
-                            !backwards, all);
-}
 
 } // namespace frontend
 } // namespace lyx
 
 
-#include "GuiSearch_moc.cpp"
+#include "moc_GuiSearch.cpp"