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.
18 #include "frontends/Application.h"
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
22 #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 using namespace lyx::support;
49 static docstring const formatted(docstring const & text)
61 size_t const nxtpos1 = text.find(' ', curpos);
62 size_t const nxtpos2 = text.find('\n', curpos);
63 size_t const nxtpos = min(nxtpos1, nxtpos2);
65 docstring const word =
66 nxtpos == docstring::npos ?
68 text.substr(curpos, nxtpos - curpos);
70 bool const newline = (nxtpos2 != docstring::npos &&
73 docstring const line_plus_word =
74 line.empty() ? word : line + char_type(' ') + word;
76 // FIXME: make w be size_t
77 if (int(line_plus_word.length()) >= w) {
78 sout += line + char_type('\n');
80 sout += word + char_type('\n');
87 sout += line_plus_word + char_type('\n');
92 line += char_type(' ');
96 if (nxtpos == docstring::npos) {
109 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
112 const char *argv[] = { "lyx", 0 };
114 QApplication app(argc, (char**)argv);
117 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
118 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
119 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
127 int prompt(docstring const & title0, docstring const & question,
128 int default_button, int cancel_button,
129 docstring const & b1, docstring const & b2,
130 docstring const & b3, docstring const & b4)
132 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
133 if (!use_gui || lyxerr.debugging()) {
134 lyxerr << title0 << '\n'
135 << "----------------------------------------\n"
138 lyxerr << "Assuming answer is ";
139 switch (default_button) {
140 case 0: lyxerr << b1 << endl;
141 case 1: lyxerr << b2 << endl;
142 case 2: lyxerr << b3 << endl;
143 case 3: lyxerr << b4 << endl;
146 return default_button;
149 docstring const title = bformat(_("LyX: %1$s"), title0);
151 // For some reason, sometimes Qt uses a hourglass or watch cursor when
152 // displaying the alert. Hence, we ask for the standard cursor shape.
153 qApp->setOverrideCursor(Qt::ArrowCursor);
155 // FIXME replace that with guiApp->currentView()
156 //LYXERR0("FOCUS: " << qApp->focusWidget());
157 QPushButton * b[4] = { 0, 0, 0, 0 };
158 QMessageBox msg_box(QMessageBox::Information,
159 toqstr(title), toqstr(formatted(question)),
160 QMessageBox::NoButton, qApp->focusWidget());
161 b[0] = msg_box.addButton(b1.empty() ? "OK" : toqstr(b1),
162 QMessageBox::ActionRole);
164 b[1] = msg_box.addButton(toqstr(b2), QMessageBox::ActionRole);
166 b[2] = msg_box.addButton(toqstr(b3), QMessageBox::ActionRole);
168 b[3] = msg_box.addButton(toqstr(b4), QMessageBox::ActionRole);
169 msg_box.setDefaultButton(b[default_button]);
170 msg_box.setEscapeButton(static_cast<QAbstractButton *>(b[cancel_button]));
171 int res = msg_box.exec();
173 qApp->restoreOverrideCursor();
175 // Qt bug: can return -1 on cancel or WM close, despite the docs.
182 void warning(docstring const & title0, docstring const & message,
183 bool const & askshowagain)
185 lyxerr << "Warning: " << title0 << '\n'
186 << "----------------------------------------\n"
192 docstring const title = bformat(_("LyX: %1$s"), title0);
195 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Warning);
199 // Don't use a hourglass cursor while displaying the alert
200 qApp->setOverrideCursor(Qt::ArrowCursor);
203 ProgressInterface::instance()->warning(
205 toqstr(formatted(message)));
207 ProgressInterface::instance()->toggleWarning(
210 toqstr(formatted(message)));
213 qApp->restoreOverrideCursor();
217 void error(docstring const & title0, docstring const & message)
219 lyxerr << "Error: " << title0 << '\n'
220 << "----------------------------------------\n"
226 docstring const title = bformat(_("LyX: %1$s"), title0);
229 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Critical);
233 // Don't use a hourglass cursor while displaying the alert
234 qApp->setOverrideCursor(Qt::ArrowCursor);
236 ProgressInterface::instance()->error(
238 toqstr(formatted(message)));
240 qApp->restoreOverrideCursor();
244 void information(docstring const & title0, docstring const & message)
246 if (!use_gui || lyxerr.debugging())
247 lyxerr << title0 << '\n'
248 << "----------------------------------------\n"
254 docstring const title = bformat(_("LyX: %1$s"), title0);
257 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Information);
261 // Don't use a hourglass cursor while displaying the alert
262 qApp->setOverrideCursor(Qt::ArrowCursor);
264 ProgressInterface::instance()->information(
266 toqstr(formatted(message)));
268 qApp->restoreOverrideCursor();
272 bool askForText(docstring & response, docstring const & msg,
273 docstring const & dflt)
275 if (!use_gui || lyxerr.debugging()) {
276 lyxerr << "----------------------------------------\n"
278 << "Assuming answer is " << dflt << '\n'
279 << "----------------------------------------" << endl;
286 docstring const title = bformat(_("LyX: %1$s"), msg);
289 QString text = QInputDialog::getText(qApp->focusWidget(),
291 toqstr(char_type('&') + msg),
296 response = qstring_to_ucs4(text);
305 } // namespace frontend