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"
20 #include "LyX.h" // for lyx::use_gui
21 #include "ui_AskForTextUi.h"
24 #include "support/docstring.h"
25 #include "support/lstrings.h"
27 #include <QApplication>
28 #include <QMessageBox>
30 #include <QInputDialog>
38 using support::bformat;
40 static docstring const formatted(docstring const & text)
52 size_t const nxtpos1 = text.find(' ', curpos);
53 size_t const nxtpos2 = text.find('\n', curpos);
54 size_t const nxtpos = std::min(nxtpos1, nxtpos2);
56 docstring const word =
57 nxtpos == docstring::npos ?
59 text.substr(curpos, nxtpos - curpos);
61 bool const newline = (nxtpos2 != docstring::npos &&
64 docstring const line_plus_word =
65 line.empty() ? word : line + char_type(' ') + word;
67 // FIXME: make w be size_t
68 if (int(line_plus_word.length()) >= w) {
69 sout += line + char_type('\n');
71 sout += word + char_type('\n');
78 sout += line_plus_word + char_type('\n');
83 line += char_type(' ');
87 if (nxtpos == docstring::npos) {
102 int prompt(docstring const & title0, docstring const & question,
103 int default_button, int cancel_button,
104 docstring const & b1, docstring const & b2, docstring const & b3)
106 if (!use_gui || lyxerr.debugging()) {
107 lyxerr << to_utf8(title0) << '\n'
108 << "----------------------------------------\n"
109 << to_utf8(question) << endl;
111 lyxerr << "Assuming answer is ";
112 switch (default_button) {
113 case 0: lyxerr << to_utf8(b1) << endl;
114 case 1: lyxerr << to_utf8(b2) << endl;
115 case 2: lyxerr << to_utf8(b3) << endl;
118 return default_button;
121 docstring const title = bformat(_("LyX: %1$s"), title0);
123 // For some reason, sometimes Qt uses an hourglass or watch cursor when
124 // displaying the alert. Hence, we ask for the standard cursor shape.
125 // This call has no effect if the cursor has not been overridden.
126 qApp->changeOverrideCursor(Qt::ArrowCursor);
128 // FIXME replace that with guiApp->currentView()
129 int res = QMessageBox::information(qApp->focusWidget(),
131 toqstr(formatted(question)),
134 b3.empty() ? QString::null : toqstr(b3),
135 default_button, cancel_button);
137 // Qt bug: can return -1 on cancel or WM close, despite the docs.
144 void warning(docstring const & title0, docstring const & message)
146 lyxerr << "Warning: " << to_utf8(title0) << '\n'
147 << "----------------------------------------\n"
148 << to_utf8(message) << endl;
153 docstring const title = bformat(_("LyX: %1$s"), title0);
158 QApplication app(argc, argv);
159 QMessageBox::warning(0,
161 toqstr(formatted(message)));
164 QMessageBox::warning(qApp->focusWidget(),
166 toqstr(formatted(message)));
170 void error(docstring const & title0, docstring const & message)
172 lyxerr << "Error: " << to_utf8(title0) << '\n'
173 << "----------------------------------------\n"
174 << to_utf8(message) << endl;
179 docstring const title = bformat(_("LyX: %1$s"), title0);
183 QApplication app(argc, argv);
184 QMessageBox::critical(0,
186 toqstr(formatted(message)));
189 QMessageBox::critical(qApp->focusWidget(),
191 toqstr(formatted(message)));
195 void information(docstring const & title0, docstring const & message)
197 if (!use_gui || lyxerr.debugging())
198 lyxerr << to_utf8(title0) << '\n'
199 << "----------------------------------------\n"
200 << to_utf8(message) << endl;
205 docstring const title = bformat(_("LyX: %1$s"), title0);
206 QMessageBox::information(qApp->focusWidget(),
208 toqstr(formatted(message)));
212 bool askForText(docstring & response, docstring const & msg,
213 docstring const & dflt)
215 if (!use_gui || lyxerr.debugging()) {
216 lyxerr << "----------------------------------------\n"
217 << to_utf8(msg) << '\n'
218 << "Assuming answer is " << to_utf8(dflt) << '\n'
219 << "----------------------------------------" << endl;
226 docstring const title = bformat(_("LyX: %1$s"), msg);
229 QString text = QInputDialog::getText(qApp->focusWidget(),
231 toqstr(char_type('&') + msg),
235 if (ok && !text.isEmpty()) {
236 response = qstring_to_ucs4(text);
245 } // namespace frontend