]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiAlert.cpp
We do not need to update just because the class has been changed.
[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 "support/debug.h"
20 #include "LyX.h" // for lyx::use_gui
21 #include "ui_AskForTextUi.h"
22 #include "support/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 namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38 namespace frontend {
39
40
41 static docstring const formatted(docstring const & text)
42 {
43         const int w = 80;
44         docstring sout;
45
46         if (text.empty())
47                 return sout;
48
49         size_t curpos = 0;
50         docstring line;
51
52         while (true) {
53                 size_t const nxtpos1 = text.find(' ',  curpos);
54                 size_t const nxtpos2 = text.find('\n', curpos);
55                 size_t const nxtpos = min(nxtpos1, nxtpos2);
56
57                 docstring const word =
58                         nxtpos == docstring::npos ?
59                         text.substr(curpos) :
60                         text.substr(curpos, nxtpos - curpos);
61
62                 bool const newline = (nxtpos2 != docstring::npos &&
63                                       nxtpos2 < nxtpos1);
64
65                 docstring const line_plus_word =
66                         line.empty() ? word : line + char_type(' ') + word;
67
68                 // FIXME: make w be size_t
69                 if (int(line_plus_word.length()) >= w) {
70                         sout += line + char_type('\n');
71                         if (newline) {
72                                 sout += word + char_type('\n');
73                                 line.erase();
74                         } else {
75                                 line = word;
76                         }
77
78                 } else if (newline) {
79                         sout += line_plus_word + char_type('\n');
80                         line.erase();
81
82                 } else {
83                         if (!line.empty())
84                                 line += char_type(' ');
85                         line += word;
86                 }
87
88                 if (nxtpos == docstring::npos) {
89                         if (!line.empty())
90                                 sout += line;
91                         break;
92                 }
93
94                 curpos = nxtpos + 1;
95         }
96
97         return sout;
98 }
99
100
101 namespace Alert {
102
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)
106 {
107         lyxerr << "PROMPT" << title0 << "FOCUS: " << qApp->focusWidget() << endl;
108         if (!use_gui || lyxerr.debugging()) {
109                 lyxerr << title0 << '\n'
110                        << "----------------------------------------\n"
111                        << question << endl;
112
113                 lyxerr << "Assuming answer is ";
114                 switch (default_button) {
115                 case 0: lyxerr << b1 << endl;
116                 case 1: lyxerr << b2 << endl;
117                 case 2: lyxerr << 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         LYXERR0("FOCUS: " << qApp->focusWidget());
132         int res = QMessageBox::information(qApp->focusWidget(),
133                                            toqstr(title),
134                                            toqstr(formatted(question)),
135                                            toqstr(b1),
136                                            toqstr(b2),
137                                            b3.empty() ? QString::null : toqstr(b3),
138                                            default_button, cancel_button);
139
140         // Qt bug: can return -1 on cancel or WM close, despite the docs.
141         if (res == -1)
142                 res = cancel_button;
143         return res;
144 }
145
146
147 void warning(docstring const & title0, docstring const & message)
148 {
149         lyxerr << "Warning: " << title0 << '\n'
150                << "----------------------------------------\n"
151                << message << endl;
152
153         if (!use_gui)
154                 return;
155
156         docstring const title = bformat(_("LyX: %1$s"), title0);
157
158         if (theApp() == 0) {
159                 int argc = 1;
160                 char * argv[1];
161                 QApplication app(argc, argv);
162                 QMessageBox::warning(0,
163                         toqstr(title),
164                         toqstr(formatted(message)));
165                 return;
166         }
167         QMessageBox::warning(qApp->focusWidget(),
168                              toqstr(title),
169                              toqstr(formatted(message)));
170 }
171
172
173 void error(docstring const & title0, docstring const & message)
174 {
175         lyxerr << "Error: " << title0 << '\n'
176                << "----------------------------------------\n"
177                << message << endl;
178
179         if (!use_gui)
180                 return;
181
182         docstring const title = bformat(_("LyX: %1$s"), title0);
183         if (theApp() == 0) {
184                 int argc = 1;
185                 char * argv[1];
186                 QApplication app(argc, argv);
187                 QMessageBox::critical(0,
188                         toqstr(title),
189                         toqstr(formatted(message)));
190                 return;
191         }
192         QMessageBox::critical(qApp->focusWidget(),
193                               toqstr(title),
194                               toqstr(formatted(message)));
195 }
196
197
198 void information(docstring const & title0, docstring const & message)
199 {
200         if (!use_gui || lyxerr.debugging())
201                 lyxerr << title0 << '\n'
202                        << "----------------------------------------\n"
203                        << message << endl;
204
205         if (!use_gui)
206                 return;
207
208         docstring const title = bformat(_("LyX: %1$s"), title0);
209         QMessageBox::information(qApp->focusWidget(),
210                                  toqstr(title),
211                                  toqstr(formatted(message)));
212 }
213
214
215 bool askForText(docstring & response, docstring const & msg,
216         docstring const & dflt)
217 {
218         if (!use_gui || lyxerr.debugging()) {
219                 lyxerr << "----------------------------------------\n"
220                        << msg << '\n'
221                        << "Assuming answer is " << dflt << '\n'
222                        << "----------------------------------------" << endl;
223                 if (!use_gui) {
224                         response = dflt;
225                         return true;
226                 }
227         }
228
229         docstring const title = bformat(_("LyX: %1$s"), msg);
230
231         bool ok;
232         QString text = QInputDialog::getText(qApp->focusWidget(),
233                 toqstr(title),
234                 toqstr(char_type('&') + msg),
235                 QLineEdit::Normal,
236                 toqstr(dflt), &ok);
237
238         if (ok && !text.isEmpty()) {
239                 response = qstring_to_ucs4(text);
240                 return true;
241         }
242         response.clear();
243         return false;
244 }
245
246
247 } // namespace Alert
248 } // namespace frontend
249 } // namespace lyx