]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiAlert.cpp
On Mac, moving down a paragraph should place the cursor at the end of the current...
[lyx.git] / src / frontends / qt4 / GuiAlert.cpp
1 /**
2  * \file qt4/GuiAlert.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 Jürgen Spitzmüller
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "alert.h"
16 #include "InGuiThread.h"
17
18 #include "frontends/Application.h"
19
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
22
23 #include "support/gettext.h"
24 #include "support/debug.h"
25 #include "support/docstring.h"
26 #include "support/lstrings.h"
27 #include "support/ProgressInterface.h"
28
29 #include <QApplication>
30 #include <QCheckBox>
31 #include <QMessageBox>
32 #include <QLineEdit>
33 #include <QInputDialog>
34 #include <QPushButton>
35 #include <QSettings>
36
37 #include <iomanip>
38 #include <iostream>
39
40
41 // sync with GuiView.cpp
42 #define EXPORT_in_THREAD 1
43
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49 namespace frontend {
50
51
52 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
53 {
54         int argc = 1;
55         const char *argv[] = { "lyx", 0 };
56
57         QApplication app(argc, (char**)argv);
58         switch (mode)
59         {
60                 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
61                 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
62                 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
63                 default: break;
64         }
65 }
66
67
68 namespace Alert {
69
70
71 int doPrompt(docstring const & title0, docstring const & question,
72                   int default_button, int cancel_button,
73                   docstring const & b1, docstring const & b2,
74                   docstring const & b3, docstring const & b4)
75 {
76         //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
77         if (!use_gui || lyxerr.debugging()) {
78                 lyxerr << title0 << '\n'
79                        << "----------------------------------------\n"
80                        << question << endl;
81
82                 lyxerr << "Assuming answer is ";
83                 switch (default_button) {
84                 case 0: lyxerr << b1 << endl;
85                 case 1: lyxerr << b2 << endl;
86                 case 2: lyxerr << b3 << endl;
87                 case 3: lyxerr << b4 << endl;
88                 }
89                 if (!use_gui)
90                         return default_button;
91         }
92
93         docstring const title = bformat(_("LyX: %1$s"), title0);
94
95         /// Long operation in progress prevents user from Ok-ing the error dialog
96         bool long_op = theApp()->longOperationStarted();
97         if (long_op)
98                 theApp()->stopLongOperation();
99
100         // For some reason, sometimes Qt uses a hourglass or watch cursor when
101         // displaying the alert. Hence, we ask for the standard cursor shape.
102         qApp->setOverrideCursor(Qt::ArrowCursor);
103
104         // FIXME replace that with guiApp->currentView()
105         //LYXERR0("FOCUS: " << qApp->focusWidget());
106         QPushButton * b[4] = { 0, 0, 0, 0 };
107         QMessageBox msg_box(QMessageBox::Information,
108                         toqstr(title), toqstr(question),
109                         QMessageBox::NoButton, qApp->focusWidget());
110         b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
111                                         QMessageBox::ActionRole);
112         if (!b2.empty())
113                 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
114         if (!b3.empty())
115                 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
116         if (!b4.empty())
117                 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
118         msg_box.setDefaultButton(b[default_button]);
119         msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
120         int res = msg_box.exec();
121
122         qApp->restoreOverrideCursor();
123
124         if (long_op)
125                 theApp()->startLongOperation();
126
127         // Qt bug: can return -1 on cancel or WM close, despite the docs.
128         if (res == -1)
129                 res = cancel_button;
130         return res;
131 }
132
133 int prompt(docstring const & title0, docstring const & question,
134                   int default_button, int cancel_button,
135                   docstring const & b1, docstring const & b2,
136                   docstring const & b3, docstring const & b4)
137 {
138 #ifdef EXPORT_in_THREAD
139         return InGuiThread<int>().call(&doPrompt,
140 #else
141         return doPrompt(
142 #endif
143                                 title0, question, default_button,
144                                 cancel_button, b1, b2, b3, b4);
145 }
146
147 void doWarning(docstring const & title0, docstring const & message,
148              bool const & askshowagain)
149 {
150         lyxerr << "Warning: " << title0 << '\n'
151                << "----------------------------------------\n"
152                << message << endl;
153
154         if (!use_gui)
155                 return;
156
157         docstring const title = bformat(_("LyX: %1$s"), title0);
158
159         if (theApp() == 0) {
160                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
161                 return;
162         }
163
164         // Don't use a hourglass cursor while displaying the alert
165         qApp->setOverrideCursor(Qt::ArrowCursor);
166
167         if (!askshowagain) {
168                 ProgressInterface::instance()->warning(
169                                 toqstr(title),
170                                 toqstr(message));
171         } else {
172                 ProgressInterface::instance()->toggleWarning(
173                                 toqstr(title),
174                                 toqstr(message),
175                                 toqstr(message));
176         }
177
178         qApp->restoreOverrideCursor();
179 }
180
181 void warning(docstring const & title0, docstring const & message,
182              bool const & askshowagain)
183 {
184 #ifdef EXPORT_in_THREAD 
185         InGuiThread<void>().call(&doWarning,
186 #else
187         doWarning(
188 #endif
189                                 title0, message, askshowagain);
190 }
191
192 void doError(docstring const & title0, docstring const & message)
193 {
194         lyxerr << "Error: " << title0 << '\n'
195                << "----------------------------------------\n"
196                << message << endl;
197
198         if (!use_gui)
199                 return;
200
201         docstring const title = bformat(_("LyX: %1$s"), title0);
202
203         if (theApp() == 0) {
204                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
205                 return;
206         }
207
208         /// Long operation in progress prevents user from Ok-ing the error dialog
209         bool long_op = theApp()->longOperationStarted();
210         if (long_op)
211                 theApp()->stopLongOperation();
212
213         // Don't use a hourglass cursor while displaying the alert
214         qApp->setOverrideCursor(Qt::ArrowCursor);
215
216         ProgressInterface::instance()->error(
217                 toqstr(title),
218                 toqstr(message));
219
220         qApp->restoreOverrideCursor();
221
222         if (long_op)
223                 theApp()->startLongOperation();
224 }
225
226 void error(docstring const & title0, docstring const & message)
227 {
228 #ifdef EXPORT_in_THREAD
229         InGuiThread<void>().call(&doError, 
230 #else
231         doError(
232 #endif
233                                 title0, message);
234 }
235
236 void doInformation(docstring const & title0, docstring const & message)
237 {
238         if (!use_gui || lyxerr.debugging())
239                 lyxerr << title0 << '\n'
240                        << "----------------------------------------\n"
241                        << message << endl;
242
243         if (!use_gui)
244                 return;
245
246         docstring const title = bformat(_("LyX: %1$s"), title0);
247
248         if (theApp() == 0) {
249                 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
250                 return;
251         }
252
253         // Don't use a hourglass cursor while displaying the alert
254         qApp->setOverrideCursor(Qt::ArrowCursor);
255
256         ProgressInterface::instance()->information(
257                 toqstr(title),
258                 toqstr(message));
259
260         qApp->restoreOverrideCursor();
261 }
262
263 void information(docstring const & title0, docstring const & message)
264 {
265 #ifdef EXPORT_in_THREAD
266         InGuiThread<void>().call(&doInformation,
267 #else
268         doInformation(
269 #endif
270                                 title0, message);
271 }
272
273 bool doAskForText(docstring & response, docstring const & msg,
274         docstring const & dflt)
275 {
276         if (!use_gui || lyxerr.debugging()) {
277                 lyxerr << "----------------------------------------\n"
278                        << msg << '\n'
279                        << "Assuming answer is " << dflt << '\n'
280                        << "----------------------------------------" << endl;
281                 if (!use_gui) {
282                         response = dflt;
283                         return true;
284                 }
285         }
286
287         docstring const title = bformat(_("LyX: %1$s"), msg);
288
289         bool ok;
290         QString text = QInputDialog::getText(qApp->focusWidget(),
291                 toqstr(title),
292                 toqstr(char_type('&') + msg),
293                 QLineEdit::Normal,
294                 toqstr(dflt), &ok);
295
296         if (ok) {
297                 response = qstring_to_ucs4(text);
298                 return true;
299         }
300         response.clear();
301         return false;
302 }
303
304 bool askForText(docstring & response, docstring const & msg,
305         docstring const & dflt)
306 {
307 #ifdef EXPORT_in_THREAD
308         return InGuiThread<bool>().call(&doAskForText,
309 #else
310         return doAskForText(
311 #endif
312                                 response, msg, dflt);
313 }
314
315 } // namespace Alert
316 } // namespace frontend
317 } // namespace lyx