]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSearch.cpp
On Linux show in crash message box the backtrace
[lyx.git] / src / frontends / qt4 / GuiSearch.cpp
1 /**
2  * \file GuiSearch.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lyxfind.h"
16 #include "qt_helpers.h"
17 #include "FuncRequest.h"
18 #include "BufferView.h"
19 #include "Buffer.h"
20 #include "Cursor.h"
21 #include "GuiSearch.h"
22 #include "GuiView.h"
23
24 #include "support/gettext.h"
25 #include "frontends/alert.h"
26
27 #include <QLineEdit>
28 #include <QShowEvent>
29
30 using namespace std;
31
32 namespace lyx {
33 namespace frontend {
34
35 static void uniqueInsert(QComboBox * box, QString const & text)
36 {
37         for (int i = box->count(); --i >= 0; )
38                 if (box->itemText(i) == text)
39                         return;
40
41         box->insertItem(0, text);
42 }
43
44
45 GuiSearch::GuiSearch(GuiView & lv)
46         : GuiDialog(lv, "findreplace", qt_("Find and Replace"))
47 {
48         setupUi(this);
49
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()));
56
57         setFocusProxy(findCO);
58
59         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
60         bc().setCancel(closePB);
61         bc().addReadOnly(replaceCO);
62         bc().addReadOnly(replacePB);
63         bc().addReadOnly(replaceallPB);
64
65         replacePB->setEnabled(false);
66         replaceallPB->setEnabled(false);
67 }
68
69
70 void GuiSearch::showEvent(QShowEvent * e)
71 {
72         findPB->setFocus();
73         findCO->lineEdit()->selectAll();
74         GuiDialog::showEvent(e);
75 }
76
77
78 void GuiSearch::findChanged()
79 {
80         if (findCO->currentText().isEmpty()) {
81                 findPB->setEnabled(false);
82                 replacePB->setEnabled(false);
83                 replaceallPB->setEnabled(false);
84         } else {
85                 findPB->setEnabled(true);
86                 replacePB->setEnabled(!isBufferReadonly());
87                 replaceallPB->setEnabled(!isBufferReadonly());
88         }
89 }
90
91
92 void GuiSearch::findClicked()
93 {
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();
99 }
100
101
102 void GuiSearch::replaceClicked()
103 {
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());
110 }
111
112
113 void GuiSearch::replaceallClicked()
114 {
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());
120 }
121
122
123 void GuiSearch::wrap_dispatch(const FuncRequest & func, bool forward)
124 {
125         dispatch(func);
126
127         BufferView * bv = const_cast<BufferView *>(bufferview());
128
129         if (!bv->cursor().result().dispatched()) {
130                 GuiView & lv = *const_cast<GuiView *>(&lyxview());
131                 DocIterator cur_orig(bv->cursor());
132                 docstring q;
133                 if (forward)
134                         q = _("End of file reached while searching forward.\n"
135                           "Continue searching from the beginning?");
136                 else
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) {
142                         if (forward) {
143                                 bv->cursor().clear();
144                                 bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
145                         } else {
146                                 bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
147                                 bv->cursor().backwardPos();
148                         }
149                         bv->clearSelection();
150                         dispatch(func);
151                         if (bv->cursor().result().dispatched())
152                                 return;
153                 }
154                 bv->cursor().setCursor(cur_orig);
155                 lv.message(_("String not found."));
156         }
157 }
158
159
160 void GuiSearch::find(docstring const & search, bool casesensitive,
161                          bool matchword, bool forward)
162 {
163         docstring const data =
164                 find2string(search, casesensitive, matchword, forward);
165         wrap_dispatch(FuncRequest(LFUN_WORD_FIND, data), forward);
166 }
167
168
169 void GuiSearch::replace(docstring const & search, docstring const & replace,
170                             bool casesensitive, bool matchword,
171                             bool forward, bool all)
172 {
173         docstring const data =
174                 replace2string(replace, search, casesensitive,
175                                      matchword, all, forward);
176         wrap_dispatch(FuncRequest(LFUN_WORD_REPLACE, data), forward);
177 }
178
179
180 Dialog * createGuiSearch(GuiView & lv) { return new GuiSearch(lv); }
181
182
183 } // namespace frontend
184 } // namespace lyx
185
186
187 #include "moc_GuiSearch.cpp"