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>
40 using namespace lyx::support;
48 static docstring const formatted(docstring const & text)
60 size_t const nxtpos1 = text.find(' ', curpos);
61 size_t const nxtpos2 = text.find('\n', curpos);
62 size_t const nxtpos = min(nxtpos1, nxtpos2);
64 docstring const word =
65 nxtpos == docstring::npos ?
67 text.substr(curpos, nxtpos - curpos);
69 bool const newline = (nxtpos2 != docstring::npos &&
72 docstring const line_plus_word =
73 line.empty() ? word : line + char_type(' ') + word;
75 // FIXME: make w be size_t
76 if (int(line_plus_word.length()) >= w) {
77 sout += line + char_type('\n');
79 sout += word + char_type('\n');
86 sout += line_plus_word + char_type('\n');
91 line += char_type(' ');
95 if (nxtpos == docstring::npos) {
108 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
112 QApplication app(argc, argv);
115 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
116 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
117 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
125 int prompt(docstring const & title0, docstring const & question,
126 int default_button, int cancel_button,
127 docstring const & b1, docstring const & b2, docstring const & b3)
129 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
130 if (!use_gui || lyxerr.debugging()) {
131 lyxerr << title0 << '\n'
132 << "----------------------------------------\n"
135 lyxerr << "Assuming answer is ";
136 switch (default_button) {
137 case 0: lyxerr << b1 << endl;
138 case 1: lyxerr << b2 << endl;
139 case 2: lyxerr << b3 << endl;
142 return default_button;
145 docstring const title = bformat(_("LyX: %1$s"), title0);
147 // For some reason, sometimes Qt uses a hourglass or watch cursor when
148 // displaying the alert. Hence, we ask for the standard cursor shape.
149 qApp->setOverrideCursor(Qt::ArrowCursor);
151 // FIXME replace that with guiApp->currentView()
152 //LYXERR0("FOCUS: " << qApp->focusWidget());
153 int res = QMessageBox::information(qApp->focusWidget(),
155 toqstr(formatted(question)),
158 b3.empty() ? QString::null : toqstr(b3),
159 default_button, cancel_button);
161 qApp->restoreOverrideCursor();
163 // Qt bug: can return -1 on cancel or WM close, despite the docs.
170 void warning(docstring const & title0, docstring const & message,
171 bool const & askshowagain)
173 lyxerr << "Warning: " << title0 << '\n'
174 << "----------------------------------------\n"
180 docstring const title = bformat(_("LyX: %1$s"), title0);
183 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Warning);
187 // Don't use a hourglass cursor while displaying the alert
188 qApp->setOverrideCursor(Qt::ArrowCursor);
191 ProgressInterface::instance()->warning(
193 toqstr(formatted(message)));
195 ProgressInterface::instance()->toggleWarning(
198 toqstr(formatted(message)));
201 qApp->restoreOverrideCursor();
205 void error(docstring const & title0, docstring const & message)
207 lyxerr << "Error: " << title0 << '\n'
208 << "----------------------------------------\n"
214 docstring const title = bformat(_("LyX: %1$s"), title0);
217 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Critical);
221 // Don't use a hourglass cursor while displaying the alert
222 qApp->setOverrideCursor(Qt::ArrowCursor);
224 ProgressInterface::instance()->error(
226 toqstr(formatted(message)));
228 qApp->restoreOverrideCursor();
232 void information(docstring const & title0, docstring const & message)
234 if (!use_gui || lyxerr.debugging())
235 lyxerr << title0 << '\n'
236 << "----------------------------------------\n"
242 docstring const title = bformat(_("LyX: %1$s"), title0);
245 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Information);
249 // Don't use a hourglass cursor while displaying the alert
250 qApp->setOverrideCursor(Qt::ArrowCursor);
252 ProgressInterface::instance()->information(
254 toqstr(formatted(message)));
256 qApp->restoreOverrideCursor();
260 bool askForText(docstring & response, docstring const & msg,
261 docstring const & dflt)
263 if (!use_gui || lyxerr.debugging()) {
264 lyxerr << "----------------------------------------\n"
266 << "Assuming answer is " << dflt << '\n'
267 << "----------------------------------------" << endl;
274 docstring const title = bformat(_("LyX: %1$s"), msg);
277 QString text = QInputDialog::getText(qApp->focusWidget(),
279 toqstr(char_type('&') + msg),
284 response = qstring_to_ucs4(text);
293 } // namespace frontend