3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
16 #include "qt_helpers.h"
17 #include "FuncRequest.h"
18 #include "BufferView.h"
21 #include "GuiSearch.h"
24 #include "support/gettext.h"
25 #include "frontends/alert.h"
35 static void uniqueInsert(QComboBox * box, QString const & text)
37 for (int i = box->count(); --i >= 0; )
38 if (box->itemText(i) == text)
41 box->insertItem(0, text);
45 GuiSearch::GuiSearch(GuiView & lv)
46 : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
50 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
51 connect(findPB, SIGNAL(clicked()), this, SLOT(findClicked()));
52 connect(replacePB, SIGNAL(clicked()), this, SLOT(replaceClicked()));
53 connect(replaceallPB, SIGNAL(clicked()), this, SLOT(replaceallClicked()));
54 connect(findCO, SIGNAL(editTextChanged(QString)),
55 this, SLOT(findChanged()));
57 setFocusProxy(findCO);
59 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
60 bc().setCancel(closePB);
61 bc().addReadOnly(replaceCO);
62 bc().addReadOnly(replacePB);
63 bc().addReadOnly(replaceallPB);
65 replacePB->setEnabled(false);
66 replaceallPB->setEnabled(false);
70 void GuiSearch::showEvent(QShowEvent * e)
73 findCO->lineEdit()->selectAll();
74 GuiDialog::showEvent(e);
78 void GuiSearch::findChanged()
80 if (findCO->currentText().isEmpty()) {
81 findPB->setEnabled(false);
82 replacePB->setEnabled(false);
83 replaceallPB->setEnabled(false);
85 findPB->setEnabled(true);
86 replacePB->setEnabled(!isBufferReadonly());
87 replaceallPB->setEnabled(!isBufferReadonly());
92 void GuiSearch::findClicked()
94 docstring const needle = qstring_to_ucs4(findCO->currentText());
95 find(needle, caseCB->isChecked(), wordsCB->isChecked(),
96 !backwardsCB->isChecked());
97 uniqueInsert(findCO, findCO->currentText());
98 findCO->lineEdit()->selectAll();
102 void GuiSearch::replaceClicked()
104 docstring const needle = qstring_to_ucs4(findCO->currentText());
105 docstring const repl = qstring_to_ucs4(replaceCO->currentText());
106 replace(needle, repl, caseCB->isChecked(), wordsCB->isChecked(),
107 !backwardsCB->isChecked(), false);
108 uniqueInsert(findCO, findCO->currentText());
109 uniqueInsert(replaceCO, replaceCO->currentText());
113 void GuiSearch::replaceallClicked()
115 replace(qstring_to_ucs4(findCO->currentText()),
116 qstring_to_ucs4(replaceCO->currentText()),
117 caseCB->isChecked(), wordsCB->isChecked(), true, true);
118 uniqueInsert(findCO, findCO->currentText());
119 uniqueInsert(replaceCO, replaceCO->currentText());
123 void GuiSearch::find(docstring const & search, bool casesensitive,
124 bool matchword, bool forward)
126 docstring const data =
127 find2string(search, casesensitive, matchword, forward);
128 dispatch(FuncRequest(LFUN_WORD_FIND, data));
132 void GuiSearch::replace(docstring const & search, docstring const & replace,
133 bool casesensitive, bool matchword,
134 bool forward, bool all)
136 docstring const data =
137 replace2string(replace, search, casesensitive,
138 matchword, all, forward);
139 dispatch(FuncRequest(LFUN_WORD_REPLACE, data));
143 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
146 } // namespace frontend
150 #include "moc_GuiSearch.cpp"