]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathPhantom.cpp
Fix various warnings issued by clang++.
[lyx.git] / src / mathed / InsetMathPhantom.cpp
1 /**
2  * \file InsetMathPhantom.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Georg Baum
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathPhantom.h"
14
15 #include "LaTeXFeatures.h"
16 #include "MathStream.h"
17 #include "frontends/Painter.h"
18
19 #include <ostream>
20
21 namespace lyx {
22
23
24 InsetMathPhantom::InsetMathPhantom(Buffer * buf, Kind k)
25         : InsetMathNest(buf, 1), kind_(k)
26 {}
27
28
29 Inset * InsetMathPhantom::clone() const
30 {
31         return new InsetMathPhantom(*this);
32 }
33
34
35 void InsetMathPhantom::metrics(MetricsInfo & mi, Dimension & dim) const
36 {
37         cell(0).metrics(mi, dim);
38         metricsMarkers(dim);
39 }
40
41
42 void InsetMathPhantom::draw(PainterInfo & pi, int x, int y) const
43 {
44         static int const arrow_size = 4;
45
46         // We first draw the text and then an arrow
47         ColorCode const origcol = pi.base.font.color();
48         if (visibleContents())
49                 pi.base.font.setColor(Color_special);
50         cell(0).draw(pi, x + 1, y);
51         if (visibleContents())
52                 pi.base.font.setColor(origcol);
53         Dimension const dim = dimension(*pi.base.bv);
54
55         if (kind_ == phantom || kind_ == vphantom) {
56                 // y1---------
57                 //           / \.
58                 // y2-----  / | \.
59                 //            |
60                 //            |
61                 // y3-----  \ | /
62                 //           \ /
63                 // y4---------
64                 //          | | |
65                 //         /  |  \.
66                 //        x1  x2 x3
67
68                 int const x2 = x + dim.wid / 2;
69                 int const x1 = x2 - arrow_size;
70                 int const x3 = x2 + arrow_size;
71
72                 int const y1 = y - dim.asc;
73                 int const y2 = y1 + arrow_size;
74                 int const y4 = y + dim.des;
75                 int const y3 = y4 - arrow_size;
76
77                 // top arrow
78                 pi.pain.line(x2, y1, x1, y2, Color_added_space);
79                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
80
81                 // bottom arrow
82                 pi.pain.line(x2, y4, x1, y3, Color_added_space);
83                 pi.pain.line(x2, y4, x3, y3, Color_added_space);
84
85                 // joining line
86                 pi.pain.line(x2, y1, x2, y4, Color_added_space);
87         }
88
89         if (kind_ == phantom || kind_ == hphantom) {
90                 // y1----  /            \.
91                 //        /              \.
92                 // y2--- <---------------->
93                 //        \              /
94                 // y3----  \            /
95                 //       |  |          |  |
96                 //      x1 x2         x3 x4
97
98                 int const x1 = x;
99                 int const x2 = x + arrow_size;
100                 int const x4 = x + dim.wid;
101                 int const x3 = x4 - arrow_size;
102
103                 int const y2 = y + (dim.des - dim.asc) / 2;
104                 int const y1 = y2 - arrow_size;
105                 int const y3 = y2 + arrow_size;
106
107                 // left arrow
108                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
109                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
110
111                 // right arrow
112                 pi.pain.line(x4, y2, x3, y3, Color_added_space);
113                 pi.pain.line(x4, y2, x3, y1, Color_added_space);
114
115                 // joining line
116                 pi.pain.line(x1, y2, x4, y2, Color_added_space);
117         }
118
119         else if (kind_ == mathclap) {
120                 // y1----      \     /
121                 //              \   /
122                 // y2--- -------->-<--------
123                 //              /   \.
124                 // y3----      /     \.
125                 //       |    |   |   |    |
126                 //      x1   x2  x3  x4   x5
127
128                 int const x1 = x;
129                 int const x5 = x + dim.wid;
130                 int const x3 = x + dim.wid / 2;
131                 int const x2 = std::max(x1, x3 - arrow_size);
132                 int const x4 = std::min(x5, x3 + arrow_size);
133
134                 int const y2 = y + (dim.des - dim.asc) / 2;
135                 int const y1 = y2 - arrow_size;
136                 int const y3 = y2 + arrow_size;
137
138                 // left arrow
139                 pi.pain.line(x2, y3, x3, y2, Color_added_space);
140                 pi.pain.line(x2, y1, x3, y2, Color_added_space);
141
142                 // right arrow
143                 pi.pain.line(x4, y3, x3, y2, Color_added_space);
144                 pi.pain.line(x4, y1, x3, y2, Color_added_space);
145
146                 // joining line
147                 pi.pain.line(x1, y2, x5, y2, Color_added_space);
148         }
149
150         else if (kind_ == mathllap) {
151                 // y1----                \.
152                 //                        \.
153                 // y2--- ------------------>
154                 //                        /
155                 // y3----                /
156                 //       |              |  |
157                 //      x1             x2 x3
158
159                 int const x1 = x;
160                 int const x3 = x + dim.wid;
161                 int const x2 = std::max(x1, x3 - arrow_size);
162
163                 int const y2 = y + (dim.des - dim.asc) / 2;
164                 int const y1 = y2 - arrow_size;
165                 int const y3 = y2 + arrow_size;
166
167                 // right arrow
168                 pi.pain.line(x3, y2, x2, y3, Color_added_space);
169                 pi.pain.line(x3, y2, x2, y1, Color_added_space);
170
171                 // joining line
172                 pi.pain.line(x1, y2, x3, y2, Color_added_space);
173         }
174
175         else if (kind_ == mathrlap) {
176                 // y1----  /
177                 //        /
178                 // y2--- <------------------
179                 //        \.
180                 // y3----  \.
181                 //       |  |              |
182                 //      x1 x2             x3
183
184                 int const x1 = x;
185                 int const x3 = x + dim.wid;
186                 int const x2 = std::min(x3, x + arrow_size);
187
188                 int const y2 = y + (dim.des - dim.asc) / 2;
189                 int const y1 = y2 - arrow_size;
190                 int const y3 = y2 + arrow_size;
191
192                 // left arrow
193                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
194                 pi.pain.line(x1, y2, x2, y1, Color_added_space);
195
196                 // joining line
197                 pi.pain.line(x1, y2, x3, y2, Color_added_space);
198         }
199
200         else if (kind_ == smash) {
201                 // y1---------
202                 //            |
203                 // y2-----  \ | /
204                 //           \ /
205                 // y3-------- |
206                 //           / \.
207                 // y4-----  / | \.
208                 //            |
209                 // y5---------
210                 //          | | |
211                 //         /  |  \.
212                 //        x1  x2 x3
213
214                 int const x2 = x + dim.wid / 2;
215                 int const x1 = x2 - arrow_size;
216                 int const x3 = x2 + arrow_size;
217
218                 int const y1 = y - dim.asc;
219                 int const y5 = y + dim.des;
220                 int const y3 = y;
221                 int const y2 = std::max(y1, y3 - arrow_size);
222                 int const y4 = std::min(y5, y3 + arrow_size);
223
224                 // top arrow
225                 pi.pain.line(x1, y2, x2, y3, Color_added_space);
226                 pi.pain.line(x3, y2, x2, y3, Color_added_space);
227
228                 // bottom arrow
229                 pi.pain.line(x1, y4, x2, y3, Color_added_space);
230                 pi.pain.line(x3, y4, x2, y3, Color_added_space);
231
232                 // joining line
233                 pi.pain.line(x2, y1, x2, y5, Color_added_space);
234         }
235
236         drawMarkers(pi, x, y);
237 }
238
239
240 void InsetMathPhantom::write(WriteStream & os) const
241 {
242         MathEnsurer ensurer(os);
243         if (os.fragile())
244                 os << "\\protect";
245         switch (kind_) {
246         case phantom:
247                 os << "\\phantom{";
248                 break;
249         case vphantom:
250                 os << "\\vphantom{";
251                 break;
252         case hphantom:
253                 os << "\\hphantom{";
254                 break;
255         case smash:
256                 os << "\\smash{";
257                 break;
258         case mathclap:
259                 os << "\\mathclap{";
260                 break;
261         case mathllap:
262                 os << "\\mathllap{";
263                 break;
264         case mathrlap:
265                 os << "\\mathrlap{";
266                 break;
267         }
268         os << cell(0) << '}';
269 }
270
271
272 void InsetMathPhantom::normalize(NormalStream & os) const
273 {
274         switch (kind_) {
275         case phantom:
276                 os << "[phantom ";
277                 break;
278         case vphantom:
279                 os << "[vphantom ";
280                 break;
281         case hphantom:
282                 os << "[hphantom ";
283                 break;
284         case smash:
285                 os << "[smash ";
286                 break;
287         case mathclap:
288                 os << "[mathclap ";
289                 break;
290         case mathllap:
291                 os << "[mathllap ";
292                 break;
293         case mathrlap:
294                 os << "[mathrlap ";
295                 break;
296         }
297         os << cell(0) << ']';
298 }
299
300
301 void InsetMathPhantom::infoize(odocstream & os) const
302 {
303         switch (kind_) {
304         case phantom:
305                 os << "Phantom";
306                 break;
307         case vphantom:
308                 os << "Vphantom";
309                 break;
310         case hphantom:
311                 os << "Hphantom";
312                 break;
313         case smash:
314                 os << "Smash";
315                 break;
316         case mathllap:
317                 os << "Mathllap";
318                 break;
319         case mathclap:
320                 os << "Mathclap";
321                 break;
322         case mathrlap:
323                 os << "Mathrlap";
324                 break;
325         }
326 }
327
328
329 void InsetMathPhantom::validate(LaTeXFeatures & features) const
330 {
331         InsetMathNest::validate(features);
332         switch (kind_) {
333         case phantom:
334         case vphantom:
335         case hphantom:
336         case smash:
337                 break;
338         case mathclap:
339         case mathllap:
340         case mathrlap:
341                 features.require("mathtools");
342                 break;
343         }
344 }
345
346
347 bool InsetMathPhantom::visibleContents() const 
348
349         return kind_ == phantom || kind_ == vphantom || kind_ == hphantom;
350 }
351
352
353 } // namespace lyx