]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/alert_pimpl.cpp
Introducing TextClassPtr.h to minimize header dependencies.
[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 "qt_helpers.h"
18
19 #include "ui_AskForTextUi.h"
20
21 #include "frontends/Application.h"
22
23 #include "gettext.h"
24
25 #include <QApplication>
26 #include <QMessageBox>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QDialog>
30 #include <QInputDialog>
31
32 #include <algorithm>
33
34 using std::pair;
35 using std::make_pair;
36 using lyx::support::bformat;
37
38 namespace lyx {
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         docstring::size_type curpos = 0;
49         docstring line;
50
51         for (;;) {
52                 docstring::size_type const nxtpos1 = text.find(' ',  curpos);
53                 docstring::size_type const nxtpos2 = text.find('\n', curpos);
54                 docstring::size_type 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 int prompt_pimpl(docstring const & tit, docstring const & question,
101                  int default_button, int cancel_button,
102                  docstring const & b1, docstring const & b2, docstring const & b3)
103 {
104         docstring const title = bformat(_("LyX: %1$s"), tit);
105
106         QMessageBox mb;
107
108         // For some reason, sometimes Qt uses an hourglass or watch cursor when
109         // displaying the alert. Hence, we ask for the standard cursor shape.
110         // This call has no effect if the cursor has not been overridden.
111         qApp->changeOverrideCursor(Qt::ArrowCursor);
112
113         // FIXME replace that with theApp->gui()->currentView()
114         int res = QMessageBox::information(qApp->focusWidget(),
115                                            toqstr(title),
116                                            toqstr(formatted(question)),
117                                            toqstr(b1),
118                                            toqstr(b2),
119                                            b3.empty() ? QString::null : toqstr(b3),
120                                            default_button, cancel_button);
121
122         // Qt bug: can return -1 on cancel or WM close, despite the docs.
123         if (res == -1)
124                 res = cancel_button;
125         return res;
126 }
127
128
129 void warning_pimpl(docstring const & tit, docstring const & message)
130 {
131         docstring const title = bformat(_("LyX: %1$s"), tit);
132
133         if (theApp() == 0) {
134                 int argc = 1;
135                 char * argv[1];
136                 QApplication app(argc, argv);
137                 QMessageBox::warning(0,
138                         toqstr(title),
139                         toqstr(formatted(message)));
140                 return;
141         }
142         QMessageBox::warning(qApp->focusWidget(),
143                              toqstr(title),
144                              toqstr(formatted(message)));
145 }
146
147
148 void error_pimpl(docstring const & tit, docstring const & message)
149 {
150         docstring const title = bformat(_("LyX: %1$s"), tit);
151         if (theApp() == 0) {
152                 int argc = 1;
153                 char * argv[1];
154                 QApplication app(argc, argv);
155                 QMessageBox::critical(0,
156                         toqstr(title),
157                         toqstr(formatted(message)));
158                 return;
159         }
160         QMessageBox::critical(qApp->focusWidget(),
161                               toqstr(title),
162                               toqstr(formatted(message)));
163 }
164
165
166 void information_pimpl(docstring const & tit, docstring const & message)
167 {
168         docstring const title = bformat(_("LyX: %1$s"), tit);
169         QMessageBox::information(qApp->focusWidget(),
170                                  toqstr(title),
171                                  toqstr(formatted(message)));
172 }
173
174
175 pair<bool, docstring> const
176 askForText_pimpl(docstring const & msg, docstring const & dflt)
177 {
178         docstring const title = bformat(_("LyX: %1$s"), msg);
179
180         bool ok;
181         QString text = QInputDialog::getText(qApp->focusWidget(),
182                 toqstr(title),
183                 toqstr(char_type('&') + msg),
184                 QLineEdit::Normal,
185                 toqstr(dflt), &ok);
186
187         if (ok && !text.isEmpty())
188                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
189         else
190                 return make_pair<bool, docstring>(false, docstring());
191 }
192
193
194 } // namespace lyx