2 * \file qt4/alert_pimpl.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
16 #include "frontends/Application.h"
18 #include "qt_helpers.h"
20 #include "LyX.h" // for lyx::use_gui
21 #include "ui_AskForTextUi.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
27 #include <QApplication>
28 #include <QMessageBox>
32 #include <QInputDialog>
40 using support::bformat;
42 static docstring const formatted(docstring const & text)
54 size_t const nxtpos1 = text.find(' ', curpos);
55 size_t const nxtpos2 = text.find('\n', curpos);
56 size_t const nxtpos = std::min(nxtpos1, nxtpos2);
58 docstring const word =
59 nxtpos == docstring::npos ?
61 text.substr(curpos, nxtpos - curpos);
63 bool const newline = (nxtpos2 != docstring::npos &&
66 docstring const line_plus_word =
67 line.empty() ? word : line + char_type(' ') + word;
69 // FIXME: make w be size_t
70 if (int(line_plus_word.length()) >= w) {
71 sout += line + char_type('\n');
73 sout += word + char_type('\n');
80 sout += line_plus_word + char_type('\n');
85 line += char_type(' ');
89 if (nxtpos == docstring::npos) {
102 int prompt_pimpl(docstring const & tit, docstring const & question,
103 int default_button, int cancel_button,
104 docstring const & b1, docstring const & b2, docstring const & b3)
106 docstring const title = bformat(_("LyX: %1$s"), tit);
110 // For some reason, sometimes Qt uses an hourglass or watch cursor when
111 // displaying the alert. Hence, we ask for the standard cursor shape.
112 // This call has no effect if the cursor has not been overridden.
113 qApp->changeOverrideCursor(Qt::ArrowCursor);
115 // FIXME replace that with theApp->gui()->currentView()
116 int res = QMessageBox::information(qApp->focusWidget(),
118 toqstr(formatted(question)),
121 b3.empty() ? QString::null : toqstr(b3),
122 default_button, cancel_button);
124 // Qt bug: can return -1 on cancel or WM close, despite the docs.
131 void warning_pimpl(docstring const & tit, docstring const & message)
133 docstring const title = bformat(_("LyX: %1$s"), tit);
138 QApplication app(argc, argv);
139 QMessageBox::warning(0,
141 toqstr(formatted(message)));
144 QMessageBox::warning(qApp->focusWidget(),
146 toqstr(formatted(message)));
150 void error_pimpl(docstring const & tit, docstring const & message)
152 docstring const title = bformat(_("LyX: %1$s"), tit);
156 QApplication app(argc, argv);
157 QMessageBox::critical(0,
159 toqstr(formatted(message)));
162 QMessageBox::critical(qApp->focusWidget(),
164 toqstr(formatted(message)));
168 void information_pimpl(docstring const & tit, docstring const & message)
170 docstring const title = bformat(_("LyX: %1$s"), tit);
171 QMessageBox::information(qApp->focusWidget(),
173 toqstr(formatted(message)));
177 bool askForText_pimpl(docstring & response, docstring const & msg,
178 docstring const & dflt)
180 docstring const title = bformat(_("LyX: %1$s"), msg);
183 QString text = QInputDialog::getText(qApp->focusWidget(),
185 toqstr(char_type('&') + msg),
189 if (ok && !text.isEmpty()) {
190 response = qstring_to_ucs4(text);
200 int prompt(docstring const & title, docstring const & question,
201 int default_button, int escape_button,
202 docstring const & b1, docstring const & b2, docstring const & b3)
204 if (!use_gui || lyxerr.debugging()) {
205 lyxerr << to_utf8(title) << '\n'
206 << "----------------------------------------\n"
207 << to_utf8(question) << endl;
209 lyxerr << "Assuming answer is ";
210 switch (default_button) {
211 case 0: lyxerr << to_utf8(b1) << endl;
212 case 1: lyxerr << to_utf8(b2) << endl;
213 case 2: lyxerr << to_utf8(b3) << endl;
216 return default_button;
219 return prompt_pimpl(title, question,
220 default_button, escape_button, b1, b2, b3);
225 void warning(docstring const & title, docstring const & message)
227 lyxerr << "Warning: " << to_utf8(title) << '\n'
228 << "----------------------------------------\n"
229 << to_utf8(message) << endl;
232 warning_pimpl(title, message);
236 void error(docstring const & title, docstring const & message)
238 lyxerr << "Error: " << to_utf8(title) << '\n'
239 << "----------------------------------------\n"
240 << to_utf8(message) << endl;
243 error_pimpl(title, message);
247 void information(docstring const & title, docstring const & message)
249 if (!use_gui || lyxerr.debugging())
250 lyxerr << to_utf8(title) << '\n'
251 << "----------------------------------------\n"
252 << to_utf8(message) << endl;
255 information_pimpl(title, message);
259 bool askForText(docstring & response, docstring const & msg,
260 docstring const & dflt)
262 if (!use_gui || lyxerr.debugging()) {
263 lyxerr << "----------------------------------------\n"
264 << to_utf8(msg) << '\n'
265 << "Assuming answer is " << to_utf8(dflt) << '\n'
266 << "----------------------------------------" << endl;
273 return askForText_pimpl(response, msg, dflt);