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