]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiAlert.cpp
cosmetics
[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 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 <QLineEdit>
30 #include <QInputDialog>
31
32 #include <iomanip>
33
34 using std::endl;
35
36
37 namespace lyx {
38 namespace frontend {
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 namespace Alert {
103
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)
107 {
108         if (!use_gui || lyxerr.debugging()) {
109                 lyxerr << to_utf8(title0) << '\n'
110                        << "----------------------------------------\n"
111                        << to_utf8(question) << endl;
112
113                 lyxerr << "Assuming answer is ";
114                 switch (default_button) {
115                 case 0: lyxerr << to_utf8(b1) << endl;
116                 case 1: lyxerr << to_utf8(b2) << endl;
117                 case 2: lyxerr << to_utf8(b3) << endl;
118                 }
119                 if (!use_gui)
120                         return default_button;
121         }
122
123         docstring const title = bformat(_("LyX: %1$s"), title0);
124
125         // For some reason, sometimes Qt uses an hourglass or watch cursor when
126         // displaying the alert. Hence, we ask for the standard cursor shape.
127         // This call has no effect if the cursor has not been overridden.
128         qApp->changeOverrideCursor(Qt::ArrowCursor);
129
130         // FIXME replace that with guiApp->currentView()
131         int res = QMessageBox::information(qApp->focusWidget(),
132                                            toqstr(title),
133                                            toqstr(formatted(question)),
134                                            toqstr(b1),
135                                            toqstr(b2),
136                                            b3.empty() ? QString::null : toqstr(b3),
137                                            default_button, cancel_button);
138
139         // Qt bug: can return -1 on cancel or WM close, despite the docs.
140         if (res == -1)
141                 res = cancel_button;
142         return res;
143 }
144
145
146 void warning(docstring const & title0, docstring const & message)
147 {
148         lyxerr << "Warning: " << to_utf8(title0) << '\n'
149                << "----------------------------------------\n"
150                << to_utf8(message) << endl;
151
152         if (!use_gui)
153                 return;
154
155         docstring const title = bformat(_("LyX: %1$s"), title0);
156
157         if (theApp() == 0) {
158                 int argc = 1;
159                 char * argv[1];
160                 QApplication app(argc, argv);
161                 QMessageBox::warning(0,
162                         toqstr(title),
163                         toqstr(formatted(message)));
164                 return;
165         }
166         QMessageBox::warning(qApp->focusWidget(),
167                              toqstr(title),
168                              toqstr(formatted(message)));
169 }
170
171
172 void error(docstring const & title0, docstring const & message)
173 {
174         lyxerr << "Error: " << to_utf8(title0) << '\n'
175                << "----------------------------------------\n"
176                << to_utf8(message) << endl;
177
178         if (!use_gui)
179                 return;
180
181         docstring const title = bformat(_("LyX: %1$s"), title0);
182         if (theApp() == 0) {
183                 int argc = 1;
184                 char * argv[1];
185                 QApplication app(argc, argv);
186                 QMessageBox::critical(0,
187                         toqstr(title),
188                         toqstr(formatted(message)));
189                 return;
190         }
191         QMessageBox::critical(qApp->focusWidget(),
192                               toqstr(title),
193                               toqstr(formatted(message)));
194 }
195
196
197 void information(docstring const & title0, docstring const & message)
198 {
199         if (!use_gui || lyxerr.debugging())
200                 lyxerr << to_utf8(title0) << '\n'
201                        << "----------------------------------------\n"
202                        << to_utf8(message) << endl;
203
204         if (!use_gui)
205                 return;
206
207         docstring const title = bformat(_("LyX: %1$s"), title0);
208         QMessageBox::information(qApp->focusWidget(),
209                                  toqstr(title),
210                                  toqstr(formatted(message)));
211 }
212
213
214 bool askForText(docstring & response, docstring const & msg,
215         docstring const & dflt)
216 {
217         if (!use_gui || lyxerr.debugging()) {
218                 lyxerr << "----------------------------------------\n"
219                        << to_utf8(msg) << '\n'
220                        << "Assuming answer is " << to_utf8(dflt) << '\n'
221                        << "----------------------------------------" << endl;
222                 if (!use_gui) {
223                         response = dflt;
224                         return true;
225                 }
226         }
227
228         docstring const title = bformat(_("LyX: %1$s"), msg);
229
230         bool ok;
231         QString text = QInputDialog::getText(qApp->focusWidget(),
232                 toqstr(title),
233                 toqstr(char_type('&') + msg),
234                 QLineEdit::Normal,
235                 toqstr(dflt), &ok);
236
237         if (ok && !text.isEmpty()) {
238                 response = qstring_to_ucs4(text);
239                 return true;
240         }
241         response.clear();
242         return false;
243 }
244
245
246 } // namespace Alert
247 } // namespace frontend
248 } // namespace lyx