]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiAlert.cpp
Typos etc.
[lyx.git] / src / frontends / qt4 / GuiAlert.cpp
1 /**
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.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "alert.h"
16
17
18 #include "frontends/Application.h"
19
20 #include "qt_helpers.h"
21 #include "LyX.h" // for lyx::use_gui
22 #include "ui_AskForTextUi.h"
23 #include "support/gettext.h"
24
25 #include "support/debug.h"
26 #include "support/docstring.h"
27 #include "support/lstrings.h"
28 #include "support/ProgressInterface.h"
29
30 #include <QApplication>
31 #include <QCheckBox>
32 #include <QMessageBox>
33 #include <QLineEdit>
34 #include <QInputDialog>
35 #include <QSettings>
36
37 #include <iomanip>
38 #include <iostream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44 namespace frontend {
45
46
47
48
49 static docstring const formatted(docstring const & text)
50 {
51         const int w = 80;
52         docstring sout;
53
54         if (text.empty())
55                 return sout;
56
57         size_t curpos = 0;
58         docstring line;
59
60         while (true) {
61                 size_t const nxtpos1 = text.find(' ',  curpos);
62                 size_t const nxtpos2 = text.find('\n', curpos);
63                 size_t const nxtpos = min(nxtpos1, nxtpos2);
64
65                 docstring const word =
66                         nxtpos == docstring::npos ?
67                         text.substr(curpos) :
68                         text.substr(curpos, nxtpos - curpos);
69
70                 bool const newline = (nxtpos2 != docstring::npos &&
71                                       nxtpos2 < nxtpos1);
72
73                 docstring const line_plus_word =
74                         line.empty() ? word : line + char_type(' ') + word;
75
76                 // FIXME: make w be size_t
77                 if (int(line_plus_word.length()) >= w) {
78                         sout += line + char_type('\n');
79                         if (newline) {
80                                 sout += word + char_type('\n');
81                                 line.erase();
82                         } else {
83                                 line = word;
84                         }
85
86                 } else if (newline) {
87                         sout += line_plus_word + char_type('\n');
88                         line.erase();
89
90                 } else {
91                         if (!line.empty())
92                                 line += char_type(' ');
93                         line += word;
94                 }
95
96                 if (nxtpos == docstring::npos) {
97                         if (!line.empty())
98                                 sout += line;
99                         break;
100                 }
101
102                 curpos = nxtpos + 1;
103         }
104
105         return sout;
106 }
107
108
109 void noAppDialog(QString const & title, QString const & msg, QMessageBox::Icon mode)
110 {
111         int argc = 1;
112         char * argv[1];
113         QApplication app(argc, argv);
114         switch (mode)
115         {
116                 case QMessageBox::Information: QMessageBox::information(0, title, msg); break;
117                 case QMessageBox::Warning: QMessageBox::warning(0, title, msg); break;
118                 case QMessageBox::Critical: QMessageBox::critical(0, title, msg); break;
119                 default: break;
120         }
121 }
122
123
124 namespace Alert {
125
126 int prompt(docstring const & title0, docstring const & question,
127                   int default_button, int cancel_button,
128                   docstring const & b1, docstring const & b2, docstring const & b3)
129 {
130         //lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
131         if (!use_gui || lyxerr.debugging()) {
132                 lyxerr << title0 << '\n'
133                        << "----------------------------------------\n"
134                        << question << endl;
135
136                 lyxerr << "Assuming answer is ";
137                 switch (default_button) {
138                 case 0: lyxerr << b1 << endl;
139                 case 1: lyxerr << b2 << endl;
140                 case 2: lyxerr << b3 << endl;
141                 }
142                 if (!use_gui)
143                         return default_button;
144         }
145
146         docstring const title = bformat(_("LyX: %1$s"), title0);
147
148         // For some reason, sometimes Qt uses an hourglass or watch cursor when
149         // displaying the alert. Hence, we ask for the standard cursor shape.
150         // This call has no effect if the cursor has not been overridden.
151         qApp->changeOverrideCursor(Qt::ArrowCursor);
152
153         // FIXME replace that with guiApp->currentView()
154         //LYXERR0("FOCUS: " << qApp->focusWidget());
155         int res = QMessageBox::information(qApp->focusWidget(),
156                                            toqstr(title),
157                                            toqstr(formatted(question)),
158                                            toqstr(b1),
159                                            toqstr(b2),
160                                            b3.empty() ? QString::null : toqstr(b3),
161                                            default_button, cancel_button);
162
163         // Qt bug: can return -1 on cancel or WM close, despite the docs.
164         if (res == -1)
165                 res = cancel_button;
166         return res;
167 }
168
169
170 void warning(docstring const & title0, docstring const & message,
171              bool const & askshowagain)
172 {
173         lyxerr << "Warning: " << title0 << '\n'
174                << "----------------------------------------\n"
175                << message << endl;
176
177         if (!use_gui)
178                 return;
179
180         docstring const title = bformat(_("LyX: %1$s"), title0);
181
182         if (theApp() == 0) {
183                 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Warning);
184                 return;
185         }
186
187         if (!askshowagain) {
188                 ProgressInterface::instance()->warning(
189                                 toqstr(title),
190                                 toqstr(formatted(message)));
191         } else {
192                 ProgressInterface::instance()->toggleWarning(
193                                 toqstr(title),
194                                 toqstr(message),
195                                 toqstr(formatted(message)));
196         }
197 }
198
199
200 void error(docstring const & title0, docstring const & message)
201 {
202         lyxerr << "Error: " << title0 << '\n'
203                << "----------------------------------------\n"
204                << message << endl;
205
206         if (!use_gui)
207                 return;
208
209         docstring const title = bformat(_("LyX: %1$s"), title0);
210
211         if (theApp() == 0) {
212                 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Critical);
213                 return;
214         }
215
216         ProgressInterface::instance()->error(
217                 toqstr(title),
218                 toqstr(formatted(message)));
219 }
220
221
222 void information(docstring const & title0, docstring const & message)
223 {
224         if (!use_gui || lyxerr.debugging())
225                 lyxerr << title0 << '\n'
226                        << "----------------------------------------\n"
227                        << message << endl;
228
229         if (!use_gui)
230                 return;
231
232         docstring const title = bformat(_("LyX: %1$s"), title0);
233
234         if (theApp() == 0) {
235                 noAppDialog(toqstr(title), toqstr(formatted(message)), QMessageBox::Information);
236                 return;
237         }
238
239         ProgressInterface::instance()->information(
240                 toqstr(title),
241                 toqstr(formatted(message)));
242 }
243
244
245 bool askForText(docstring & response, docstring const & msg,
246         docstring const & dflt)
247 {
248         if (!use_gui || lyxerr.debugging()) {
249                 lyxerr << "----------------------------------------\n"
250                        << msg << '\n'
251                        << "Assuming answer is " << dflt << '\n'
252                        << "----------------------------------------" << endl;
253                 if (!use_gui) {
254                         response = dflt;
255                         return true;
256                 }
257         }
258
259         docstring const title = bformat(_("LyX: %1$s"), msg);
260
261         bool ok;
262         QString text = QInputDialog::getText(qApp->focusWidget(),
263                 toqstr(title),
264                 toqstr(char_type('&') + msg),
265                 QLineEdit::Normal,
266                 toqstr(dflt), &ok);
267
268         if (ok) {
269                 response = qstring_to_ucs4(text);
270                 return true;
271         }
272         response.clear();
273         return false;
274 }
275
276
277 } // namespace Alert
278 } // namespace frontend
279 } // namespace lyx