]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/alert_pimpl.cpp
pimpl not needed here
[lyx.git] / src / frontends / qt4 / alert_pimpl.cpp
1 /**
2  * \file qt4/alert_pimpl.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_pimpl.h"
15 #include "alert.h"
16
17 #include "ui_AskForTextUi.h"
18 #include "qt_helpers.h"
19
20 #include "frontends/Application.h"
21
22 #include "gettext.h"
23
24 #include <QApplication>
25 #include <QMessageBox>
26 #include <QLabel>
27 #include <QLineEdit>
28 #include <QDialog>
29 #include <QInputDialog>
30
31 #include <algorithm>
32
33 using std::pair;
34 using std::make_pair;
35 using lyx::support::bformat;
36
37 namespace lyx {
38
39 static docstring const formatted(docstring const & text)
40 {
41         const int w = 80;
42         docstring sout;
43
44         if (text.empty())
45                 return sout;
46
47         docstring::size_type curpos = 0;
48         docstring line;
49
50         for (;;) {
51                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
52                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
53                 docstring::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
54
55                 docstring const word =
56                         nxtpos == docstring::npos ?
57                         text.substr(curpos) :
58                         text.substr(curpos, nxtpos - curpos);
59
60                 bool const newline = (nxtpos2 != docstring::npos &&
61                                       nxtpos2 < nxtpos1);
62
63                 docstring const line_plus_word =
64                         line.empty() ? word : line + char_type(' ') + word;
65
66                 // FIXME: make w be size_t
67                 if (int(line_plus_word.length()) >= w) {
68                         sout += line + char_type('\n');
69                         if (newline) {
70                                 sout += word + char_type('\n');
71                                 line.erase();
72                         } else {
73                                 line = word;
74                         }
75
76                 } else if (newline) {
77                         sout += line_plus_word + char_type('\n');
78                         line.erase();
79
80                 } else {
81                         if (!line.empty())
82                                 line += char_type(' ');
83                         line += word;
84                 }
85
86                 if (nxtpos == docstring::npos) {
87                         if (!line.empty())
88                                 sout += line;
89                         break;
90                 }
91
92                 curpos = nxtpos + 1;
93         }
94
95         return sout;
96 }
97
98
99 int prompt_pimpl(docstring const & tit, docstring const & question,
100                  int default_button, int cancel_button,
101                  docstring const & b1, docstring const & b2, docstring const & b3)
102 {
103         docstring const title = bformat(_("LyX: %1$s"), tit);
104
105         QMessageBox mb;
106
107         // For some reason, sometimes Qt uses an hourglass or watch cursor when
108         // displaying the alert. Hence, we ask for the standard cursor shape.
109         // This call has no effect if the cursor has not been overridden.
110         qApp->changeOverrideCursor(Qt::ArrowCursor);
111
112         // FIXME replace that with theApp->gui()->currentView()
113         int res = QMessageBox::information(qApp->focusWidget(),
114                                            toqstr(title),
115                                            toqstr(formatted(question)),
116                                            toqstr(b1),
117                                            toqstr(b2),
118                                            b3.empty() ? QString::null : toqstr(b3),
119                                            default_button, cancel_button);
120
121         // Qt bug: can return -1 on cancel or WM close, despite the docs.
122         if (res == -1)
123                 res = cancel_button;
124         return res;
125 }
126
127
128 void warning_pimpl(docstring const & tit, docstring const & message)
129 {
130         docstring const title = bformat(_("LyX: %1$s"), tit);
131
132         if (theApp() == 0) {
133                 int argc = 1;
134                 char * argv[1];
135                 QApplication app(argc, argv);
136                 QMessageBox::warning(0,
137                         toqstr(title),
138                         toqstr(formatted(message)));
139                 return;
140         }
141         QMessageBox::warning(qApp->focusWidget(),
142                              toqstr(title),
143                              toqstr(formatted(message)));
144 }
145
146
147 void error_pimpl(docstring const & tit, docstring const & message)
148 {
149         docstring const title = bformat(_("LyX: %1$s"), tit);
150         if (theApp() == 0) {
151                 int argc = 1;
152                 char * argv[1];
153                 QApplication app(argc, argv);
154                 QMessageBox::critical(0,
155                         toqstr(title),
156                         toqstr(formatted(message)));
157                 return;
158         }
159         QMessageBox::critical(qApp->focusWidget(),
160                               toqstr(title),
161                               toqstr(formatted(message)));
162 }
163
164
165 void information_pimpl(docstring const & tit, docstring const & message)
166 {
167         docstring const title = bformat(_("LyX: %1$s"), tit);
168         QMessageBox::information(qApp->focusWidget(),
169                                  toqstr(title),
170                                  toqstr(formatted(message)));
171 }
172
173
174 pair<bool, docstring> const
175 askForText_pimpl(docstring const & msg, docstring const & dflt)
176 {
177         docstring const title = bformat(_("LyX: %1$s"), msg);
178
179         bool ok;
180         QString text = QInputDialog::getText(qApp->focusWidget(),
181                 toqstr(title),
182                 toqstr(char_type('&') + msg),
183                 QLineEdit::Normal,
184                 toqstr(dflt), &ok);
185
186         if (ok && !text.isEmpty())
187                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
188         else
189                 return make_pair<bool, docstring>(false, docstring());
190 }
191
192
193 } // namespace lyx