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 Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
16 #include "frontends/Application.h"
18 #include "qt_helpers.h"
19 #include "support/debug.h"
20 #include "LyX.h" // for lyx::use_gui
21 #include "ui_AskForTextUi.h"
22 #include "support/gettext.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
27 #include <QApplication>
28 #include <QMessageBox>
30 #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) {
104 int prompt(docstring const & title0, docstring const & question,
105 int default_button, int cancel_button,
106 docstring const & b1, docstring const & b2, docstring const & b3)
108 if (!use_gui || lyxerr.debugging()) {
109 lyxerr << to_utf8(title0) << '\n'
110 << "----------------------------------------\n"
111 << to_utf8(question) << endl;
113 lyxerr << "Assuming answer is ";
114 switch (default_button) {
115 case 0: lyxerr << to_utf8(b1) << endl;
116 case 1: lyxerr << to_utf8(b2) << endl;
117 case 2: lyxerr << to_utf8(b3) << endl;
120 return default_button;
123 docstring const title = bformat(_("LyX: %1$s"), title0);
125 // For some reason, sometimes Qt uses an hourglass or watch cursor when
126 // displaying the alert. Hence, we ask for the standard cursor shape.
127 // This call has no effect if the cursor has not been overridden.
128 qApp->changeOverrideCursor(Qt::ArrowCursor);
130 // FIXME replace that with guiApp->currentView()
131 int res = QMessageBox::information(qApp->focusWidget(),
133 toqstr(formatted(question)),
136 b3.empty() ? QString::null : toqstr(b3),
137 default_button, cancel_button);
139 // Qt bug: can return -1 on cancel or WM close, despite the docs.
146 void warning(docstring const & title0, docstring const & message)
148 lyxerr << "Warning: " << to_utf8(title0) << '\n'
149 << "----------------------------------------\n"
150 << to_utf8(message) << endl;
155 docstring const title = bformat(_("LyX: %1$s"), title0);
160 QApplication app(argc, argv);
161 QMessageBox::warning(0,
163 toqstr(formatted(message)));
166 QMessageBox::warning(qApp->focusWidget(),
168 toqstr(formatted(message)));
172 void error(docstring const & title0, docstring const & message)
174 lyxerr << "Error: " << to_utf8(title0) << '\n'
175 << "----------------------------------------\n"
176 << to_utf8(message) << endl;
181 docstring const title = bformat(_("LyX: %1$s"), title0);
185 QApplication app(argc, argv);
186 QMessageBox::critical(0,
188 toqstr(formatted(message)));
191 QMessageBox::critical(qApp->focusWidget(),
193 toqstr(formatted(message)));
197 void information(docstring const & title0, docstring const & message)
199 if (!use_gui || lyxerr.debugging())
200 lyxerr << to_utf8(title0) << '\n'
201 << "----------------------------------------\n"
202 << to_utf8(message) << endl;
207 docstring const title = bformat(_("LyX: %1$s"), title0);
208 QMessageBox::information(qApp->focusWidget(),
210 toqstr(formatted(message)));
214 bool askForText(docstring & response, docstring const & msg,
215 docstring const & dflt)
217 if (!use_gui || lyxerr.debugging()) {
218 lyxerr << "----------------------------------------\n"
219 << to_utf8(msg) << '\n'
220 << "Assuming answer is " << to_utf8(dflt) << '\n'
221 << "----------------------------------------" << endl;
228 docstring const title = bformat(_("LyX: %1$s"), msg);
231 QString text = QInputDialog::getText(qApp->focusWidget(),
233 toqstr(char_type('&') + msg),
237 if (ok && !text.isEmpty()) {
238 response = qstring_to_ucs4(text);
247 } // namespace frontend