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 "LyX.h" // for lyx::use_gui
20 #include "ui_AskForTextUi.h"
21 #include "support/gettext.h"
23 #include "support/debug.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
27 #include <QApplication>
28 #include <QMessageBox>
30 #include <QInputDialog>
36 using namespace lyx::support;
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 = 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 //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
109 if (!use_gui || lyxerr.debugging()) {
110 lyxerr << title0 << '\n'
111 << "----------------------------------------\n"
114 lyxerr << "Assuming answer is ";
115 switch (default_button) {
116 case 0: lyxerr << b1 << endl;
117 case 1: lyxerr << b2 << endl;
118 case 2: lyxerr << b3 << endl;
121 return default_button;
124 docstring const title = bformat(_("LyX: %1$s"), title0);
126 // For some reason, sometimes Qt uses an hourglass or watch cursor when
127 // displaying the alert. Hence, we ask for the standard cursor shape.
128 // This call has no effect if the cursor has not been overridden.
129 qApp->changeOverrideCursor(Qt::ArrowCursor);
131 // FIXME replace that with guiApp->currentView()
132 //LYXERR0("FOCUS: " << qApp->focusWidget());
133 int res = QMessageBox::information(qApp->focusWidget(),
135 toqstr(formatted(question)),
138 b3.empty() ? QString::null : toqstr(b3),
139 default_button, cancel_button);
141 // Qt bug: can return -1 on cancel or WM close, despite the docs.
148 void warning(docstring const & title0, docstring const & message)
150 lyxerr << "Warning: " << title0 << '\n'
151 << "----------------------------------------\n"
157 docstring const title = bformat(_("LyX: %1$s"), title0);
162 QApplication app(argc, argv);
163 QMessageBox::warning(0,
165 toqstr(formatted(message)));
168 QMessageBox::warning(qApp->focusWidget(),
170 toqstr(formatted(message)));
174 void error(docstring const & title0, docstring const & message)
176 lyxerr << "Error: " << title0 << '\n'
177 << "----------------------------------------\n"
183 docstring const title = bformat(_("LyX: %1$s"), title0);
187 QApplication app(argc, argv);
188 QMessageBox::critical(0,
190 toqstr(formatted(message)));
193 QMessageBox::critical(qApp->focusWidget(),
195 toqstr(formatted(message)));
199 void information(docstring const & title0, docstring const & message)
201 if (!use_gui || lyxerr.debugging())
202 lyxerr << title0 << '\n'
203 << "----------------------------------------\n"
209 docstring const title = bformat(_("LyX: %1$s"), title0);
210 QMessageBox::information(qApp->focusWidget(),
212 toqstr(formatted(message)));
216 bool askForText(docstring & response, docstring const & msg,
217 docstring const & dflt)
219 if (!use_gui || lyxerr.debugging()) {
220 lyxerr << "----------------------------------------\n"
222 << "Assuming answer is " << dflt << '\n'
223 << "----------------------------------------" << endl;
230 docstring const title = bformat(_("LyX: %1$s"), msg);
233 QString text = QInputDialog::getText(qApp->focusWidget(),
235 toqstr(char_type('&') + msg),
239 if (ok && !text.isEmpty()) {
240 response = qstring_to_ucs4(text);
249 } // namespace frontend