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 = std::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 if (!use_gui || lyxerr.debugging()) {
108 lyxerr << to_utf8(title0) << '\n'
109 << "----------------------------------------\n"
110 << to_utf8(question) << endl;
112 lyxerr << "Assuming answer is ";
113 switch (default_button) {
114 case 0: lyxerr << to_utf8(b1) << endl;
115 case 1: lyxerr << to_utf8(b2) << endl;
116 case 2: lyxerr << to_utf8(b3) << endl;
119 return default_button;
122 docstring const title = bformat(_("LyX: %1$s"), title0);
124 // For some reason, sometimes Qt uses an hourglass or watch cursor when
125 // displaying the alert. Hence, we ask for the standard cursor shape.
126 // This call has no effect if the cursor has not been overridden.
127 qApp->changeOverrideCursor(Qt::ArrowCursor);
129 // FIXME replace that with guiApp->currentView()
130 int res = QMessageBox::information(qApp->focusWidget(),
132 toqstr(formatted(question)),
135 b3.empty() ? QString::null : toqstr(b3),
136 default_button, cancel_button);
138 // Qt bug: can return -1 on cancel or WM close, despite the docs.
145 void warning(docstring const & title0, docstring const & message)
147 lyxerr << "Warning: " << to_utf8(title0) << '\n'
148 << "----------------------------------------\n"
149 << to_utf8(message) << endl;
154 docstring const title = bformat(_("LyX: %1$s"), title0);
159 QApplication app(argc, argv);
160 QMessageBox::warning(0,
162 toqstr(formatted(message)));
165 QMessageBox::warning(qApp->focusWidget(),
167 toqstr(formatted(message)));
171 void error(docstring const & title0, docstring const & message)
173 lyxerr << "Error: " << to_utf8(title0) << '\n'
174 << "----------------------------------------\n"
175 << to_utf8(message) << endl;
180 docstring const title = bformat(_("LyX: %1$s"), title0);
184 QApplication app(argc, argv);
185 QMessageBox::critical(0,
187 toqstr(formatted(message)));
190 QMessageBox::critical(qApp->focusWidget(),
192 toqstr(formatted(message)));
196 void information(docstring const & title0, docstring const & message)
198 if (!use_gui || lyxerr.debugging())
199 lyxerr << to_utf8(title0) << '\n'
200 << "----------------------------------------\n"
201 << to_utf8(message) << endl;
206 docstring const title = bformat(_("LyX: %1$s"), title0);
207 QMessageBox::information(qApp->focusWidget(),
209 toqstr(formatted(message)));
213 bool askForText(docstring & response, docstring const & msg,
214 docstring const & dflt)
216 if (!use_gui || lyxerr.debugging()) {
217 lyxerr << "----------------------------------------\n"
218 << to_utf8(msg) << '\n'
219 << "Assuming answer is " << to_utf8(dflt) << '\n'
220 << "----------------------------------------" << endl;
227 docstring const title = bformat(_("LyX: %1$s"), msg);
230 QString text = QInputDialog::getText(qApp->focusWidget(),
232 toqstr(char_type('&') + msg),
236 if (ok && !text.isEmpty()) {
237 response = qstring_to_ucs4(text);
246 } // namespace frontend