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.
7 * \author Jürgen Spitzmüller
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
16 #include "InGuiThread.h"
18 #include "frontends/Application.h"
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
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"
29 #include <QApplication>
31 #include <QMessageBox>
33 #include <QInputDialog>
34 #include <QPushButton>
41 // sync with GuiView.cpp
42 #define EXPORT_in_THREAD 1
46 using namespace lyx::support;
52 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
55 const char *argv[] = { "lyx", 0 };
57 QApplication app(argc, (char**)argv);
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;
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)
76 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
77 if (!use_gui || lyxerr.debugging()) {
78 lyxerr << title0 << '\n'
79 << "----------------------------------------\n"
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;
90 return default_button;
93 docstring const title = bformat(_("LyX: %1$s"), title0);
95 // For some reason, sometimes Qt uses a hourglass or watch cursor when
96 // displaying the alert. Hence, we ask for the standard cursor shape.
97 qApp->setOverrideCursor(Qt::ArrowCursor);
99 // FIXME replace that with guiApp->currentView()
100 //LYXERR0("FOCUS: " << qApp->focusWidget());
101 QPushButton * b[4] = { 0, 0, 0, 0 };
102 QMessageBox msg_box(QMessageBox::Information,
103 toqstr(title), toqstr(question),
104 QMessageBox::NoButton, qApp->focusWidget());
105 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
106 QMessageBox::ActionRole);
108 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
110 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
112 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
113 msg_box.setDefaultButton(b[default_button]);
114 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
115 int res = msg_box.exec();
117 qApp->restoreOverrideCursor();
119 // Qt bug: can return -1 on cancel or WM close, despite the docs.
125 int prompt(docstring const & title0, docstring const & question,
126 int default_button, int cancel_button,
127 docstring const & b1, docstring const & b2,
128 docstring const & b3, docstring const & b4)
130 #ifdef EXPORT_in_THREAD
131 return InGuiThread<int>().call(&doPrompt,
135 title0, question, default_button,
136 cancel_button, b1, b2, b3, b4);
139 void doWarning(docstring const & title0, docstring const & message,
140 bool const & askshowagain)
142 lyxerr << "Warning: " << title0 << '\n'
143 << "----------------------------------------\n"
149 docstring const title = bformat(_("LyX: %1$s"), title0);
152 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Warning);
156 // Don't use a hourglass cursor while displaying the alert
157 qApp->setOverrideCursor(Qt::ArrowCursor);
160 ProgressInterface::instance()->warning(
164 ProgressInterface::instance()->toggleWarning(
170 qApp->restoreOverrideCursor();
173 void warning(docstring const & title0, docstring const & message,
174 bool const & askshowagain)
176 #ifdef EXPORT_in_THREAD
177 InGuiThread<void>().call(&doWarning,
181 title0, message, askshowagain);
184 void doError(docstring const & title0, docstring const & message)
186 lyxerr << "Error: " << title0 << '\n'
187 << "----------------------------------------\n"
193 docstring const title = bformat(_("LyX: %1$s"), title0);
196 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Critical);
200 // Don't use a hourglass cursor while displaying the alert
201 qApp->setOverrideCursor(Qt::ArrowCursor);
203 ProgressInterface::instance()->error(
207 qApp->restoreOverrideCursor();
210 void error(docstring const & title0, docstring const & message)
212 #ifdef EXPORT_in_THREAD
213 InGuiThread<void>().call(&doError,
220 void doInformation(docstring const & title0, docstring const & message)
222 if (!use_gui || lyxerr.debugging())
223 lyxerr << title0 << '\n'
224 << "----------------------------------------\n"
230 docstring const title = bformat(_("LyX: %1$s"), title0);
233 noAppDialog(toqstr(title), toqstr(message), QMessageBox::Information);
237 // Don't use a hourglass cursor while displaying the alert
238 qApp->setOverrideCursor(Qt::ArrowCursor);
240 ProgressInterface::instance()->information(
244 qApp->restoreOverrideCursor();
247 void information(docstring const & title0, docstring const & message)
249 #ifdef EXPORT_in_THREAD
250 InGuiThread<void>().call(&doInformation,
257 bool doAskForText(docstring & response, docstring const & msg,
258 docstring const & dflt)
260 if (!use_gui || lyxerr.debugging()) {
261 lyxerr << "----------------------------------------\n"
263 << "Assuming answer is " << dflt << '\n'
264 << "----------------------------------------" << endl;
271 docstring const title = bformat(_("LyX: %1$s"), msg);
274 QString text = QInputDialog::getText(qApp->focusWidget(),
276 toqstr(char_type('&') + msg),
281 response = qstring_to_ucs4(text);
288 bool askForText(docstring & response, docstring const & msg,
289 docstring const & dflt)
291 #ifdef EXPORT_in_THREAD
292 return InGuiThread<bool>().call(&doAskForText,
296 response, msg, dflt);
300 } // namespace frontend