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::wrap_dispatch(const FuncRequest & func, bool forward)
127 BufferView * bv = const_cast<BufferView *>(bufferview());
129 if (!bv->cursor().result().dispatched()) {
130 GuiView & lv = *const_cast<GuiView *>(&lyxview());
131 DocIterator cur_orig(bv->cursor());
134 q = _("End of file reached while searching forward.\n"
135 "Continue searching from the beginning?");
137 q = _("Beginning of file reached while searching backward.\n"
138 "Continue searching from the end?");
139 int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
140 q, 0, 1, _("&Yes"), _("&No"));
141 if (wrap_answer == 0) {
143 bv->cursor().clear();
144 bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
146 bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
147 bv->cursor().backwardPos();
149 bv->clearSelection();
151 if (bv->cursor().result().dispatched())
154 bv->cursor().setCursor(cur_orig);
155 lv.message(_("String not found."));
160 void GuiSearch::find(docstring const & search, bool casesensitive,
161 bool matchword, bool forward)
163 docstring const data =
164 find2string(search, casesensitive, matchword, forward);
165 wrap_dispatch(FuncRequest(LFUN_WORD_FIND, data), forward);
169 void GuiSearch::replace(docstring const & search, docstring const & replace,
170 bool casesensitive, bool matchword,
171 bool forward, bool all)
173 docstring const data =
174 replace2string(replace, search, casesensitive,
175 matchword, all, forward);
176 wrap_dispatch(FuncRequest(LFUN_WORD_REPLACE, data), forward);
180 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
183 } // namespace frontend
187 #include "moc_GuiSearch.cpp"