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