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)));
177 void error(docstring const & title0, docstring const & message)
179 lyxerr << "Error: " << title0 << '\n'
180 << "----------------------------------------\n"
186 docstring const title = bformat(_("LyX: %1$s"), title0);
188 QApplication app(argc, argv);
189 QMessageBox::critical(0,
191 toqstr(formatted(message)));
194 QMessageBox::critical(qApp->focusWidget(),
196 toqstr(formatted(message)));
200 void information(docstring const & title0, docstring const & message)
202 if (!use_gui || lyxerr.debugging())
203 lyxerr << title0 << '\n'
204 << "----------------------------------------\n"
210 docstring const title = bformat(_("LyX: %1$s"), title0);
211 QMessageBox::information(qApp->focusWidget(),
213 toqstr(formatted(message)));
217 bool askForText(docstring & response, docstring const & msg,
218 docstring const & dflt)
220 if (!use_gui || lyxerr.debugging()) {
221 lyxerr << "----------------------------------------\n"
223 << "Assuming answer is " << dflt << '\n'
224 << "----------------------------------------" << endl;
231 docstring const title = bformat(_("LyX: %1$s"), msg);
234 QString text = QInputDialog::getText(qApp->focusWidget(),
236 toqstr(char_type('&') + msg),
241 response = qstring_to_ucs4(text);
250 } // namespace frontend