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