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 "ui_AskForTextUi.h"
23 #include "support/gettext.h"
25 #include "support/debug.h"
26 #include "support/docstring.h"
27 #include "support/lstrings.h"
28 #include "support/ProgressInterface.h"
30 #include <QApplication>
32 #include <QMessageBox>
34 #include <QInputDialog>
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)
113 QApplication app(argc, argv);
116 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
117 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
118 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
126 int prompt(docstring const & title0, docstring const & question,
127 int default_button, int cancel_button,
128 docstring const & b1, docstring const & b2, docstring const & b3)
130 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
131 if (!use_gui || lyxerr.debugging()) {
132 lyxerr << title0 << '\n'
133 << "----------------------------------------\n"
136 lyxerr << "Assuming answer is ";
137 switch (default_button) {
138 case 0: lyxerr << b1 << endl;
139 case 1: lyxerr << b2 << endl;
140 case 2: lyxerr << b3 << endl;
143 return default_button;
146 docstring const title = bformat(_("LyX: %1$s"), title0);
148 // For some reason, sometimes Qt uses an hourglass or watch cursor when
149 // displaying the alert. Hence, we ask for the standard cursor shape.
150 // This call has no effect if the cursor has not been overridden.
151 qApp->changeOverrideCursor(Qt::ArrowCursor);
153 // FIXME replace that with guiApp->currentView()
154 //LYXERR0("FOCUS: " << qApp->focusWidget());
155 int res = QMessageBox::information(qApp->focusWidget(),
157 toqstr(formatted(question)),
160 b3.empty() ? QString::null : toqstr(b3),
161 default_button, cancel_button);
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);
188 ProgressInterface::instance()->warning(
190 toqstr(formatted(message)));
192 ProgressInterface::instance()->toggleWarning(
195 toqstr(formatted(message)));
200 void error(docstring const & title0, docstring const & message)
202 lyxerr << "Error: " << title0 << '\n'
203 << "----------------------------------------\n"
209 docstring const title = bformat(_("LyX: %1$s"), title0);
212 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Critical);
216 ProgressInterface::instance()->error(
218 toqstr(formatted(message)));
222 void information(docstring const & title0, docstring const & message)
224 if (!use_gui || lyxerr.debugging())
225 lyxerr << title0 << '\n'
226 << "----------------------------------------\n"
232 docstring const title = bformat(_("LyX: %1$s"), title0);
235 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Information);
239 ProgressInterface::instance()->information(
241 toqstr(formatted(message)));
245 bool askForText(docstring & response, docstring const & msg,
246 docstring const & dflt)
248 if (!use_gui || lyxerr.debugging()) {
249 lyxerr << "----------------------------------------\n"
251 << "Assuming answer is " << dflt << '\n'
252 << "----------------------------------------" << endl;
259 docstring const title = bformat(_("LyX: %1$s"), msg);
262 QString text = QInputDialog::getText(qApp->focusWidget(),
264 toqstr(char_type('&') + msg),
269 response = qstring_to_ucs4(text);
278 } // namespace frontend