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>
35 using namespace lyx::support;
41 static docstring const formatted(docstring const & text)
53 size_t const nxtpos1 = text.find(' ', curpos);
54 size_t const nxtpos2 = text.find('\n', curpos);
55 size_t const nxtpos = min(nxtpos1, nxtpos2);
57 docstring const word =
58 nxtpos == docstring::npos ?
60 text.substr(curpos, nxtpos - curpos);
62 bool const newline = (nxtpos2 != docstring::npos &&
65 docstring const line_plus_word =
66 line.empty() ? word : line + char_type(' ') + word;
68 // FIXME: make w be size_t
69 if (int(line_plus_word.length()) >= w) {
70 sout += line + char_type('\n');
72 sout += word + char_type('\n');
79 sout += line_plus_word + char_type('\n');
84 line += char_type(' ');
88 if (nxtpos == docstring::npos) {
103 int prompt(docstring const & title0, docstring const & question,
104 int default_button, int cancel_button,
105 docstring const & b1, docstring const & b2, docstring const & b3)
107 lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
108 if (!use_gui || lyxerr.debugging()) {
109 lyxerr << title0 << '\n'
110 << "----------------------------------------\n"
113 lyxerr << "Assuming answer is ";
114 switch (default_button) {
115 case 0: lyxerr << b1 << endl;
116 case 1: lyxerr << b2 << endl;
117 case 2: lyxerr << 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 LYXERR0("FOCUS: " << qApp->focusWidget());
132 int res = QMessageBox::information(qApp->focusWidget(),
134 toqstr(formatted(question)),
137 b3.empty() ? QString::null : toqstr(b3),
138 default_button, cancel_button);
140 // Qt bug: can return -1 on cancel or WM close, despite the docs.
147 void warning(docstring const & title0, docstring const & message)
149 lyxerr << "Warning: " << title0 << '\n'
150 << "----------------------------------------\n"
156 docstring const title = bformat(_("LyX: %1$s"), title0);
161 QApplication app(argc, argv);
162 QMessageBox::warning(0,
164 toqstr(formatted(message)));
167 QMessageBox::warning(qApp->focusWidget(),
169 toqstr(formatted(message)));
173 void error(docstring const & title0, docstring const & message)
175 lyxerr << "Error: " << title0 << '\n'
176 << "----------------------------------------\n"
182 docstring const title = bformat(_("LyX: %1$s"), title0);
186 QApplication app(argc, argv);
187 QMessageBox::critical(0,
189 toqstr(formatted(message)));
192 QMessageBox::critical(qApp->focusWidget(),
194 toqstr(formatted(message)));
198 void information(docstring const & title0, docstring const & message)
200 if (!use_gui || lyxerr.debugging())
201 lyxerr << title0 << '\n'
202 << "----------------------------------------\n"
208 docstring const title = bformat(_("LyX: %1$s"), title0);
209 QMessageBox::information(qApp->focusWidget(),
211 toqstr(formatted(message)));
215 bool askForText(docstring & response, docstring const & msg,
216 docstring const & dflt)
218 if (!use_gui || lyxerr.debugging()) {
219 lyxerr << "----------------------------------------\n"
221 << "Assuming answer is " << dflt << '\n'
222 << "----------------------------------------" << endl;
229 docstring const title = bformat(_("LyX: %1$s"), msg);
232 QString text = QInputDialog::getText(qApp->focusWidget(),
234 toqstr(char_type('&') + msg),
238 if (ok && !text.isEmpty()) {
239 response = qstring_to_ucs4(text);
248 } // namespace frontend