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