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