]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
Strip out redundant includes (193 of 'em).
[lyx.git] / src / insets / insetspace.C
1 /**
2  * \file insetspace.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Jean-Marc Lasgouttes
8  * \author Lars Gullik Bjønnes
9  * \author Juergen Spitzmueller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "insetspace.h"
17
18 #include "debug.h"
19 #include "latexrunparams.h"
20 #include "lyxlex.h"
21 #include "metricsinfo.h"
22
23 #include "frontends/font_metrics.h"
24 #include "frontends/Painter.h"
25
26 using std::max;
27 using std::auto_ptr;
28 using std::ostream;
29
30
31 InsetSpace::InsetSpace()
32 {}
33
34
35 InsetSpace::InsetSpace(Kind k)
36         : kind_(k)
37 {}
38
39
40 InsetSpace::Kind InsetSpace::kind() const
41 {
42         return kind_;
43 }
44
45
46 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
47 {
48         LyXFont & font = mi.base.font;
49         dim.asc = font_metrics::maxAscent(font);
50         dim.des = font_metrics::maxDescent(font);
51
52         switch (kind_) {
53                 case THIN:
54                 case NEGTHIN:
55                         dim.wid = font_metrics::width("x", font) / 3;
56                         break;
57                 case PROTECTED:
58                 case NORMAL:
59                         dim.wid = font_metrics::width("x", font);
60                         break;
61                 case QUAD:
62                         dim.wid = 20;
63                         break;
64                 case QQUAD:
65                         dim.wid = 40;
66                         break;
67                 case ENSPACE:
68                 case ENSKIP:
69                         dim.wid = 10;
70                         break;
71         }
72         dim_ = dim;
73 }
74
75
76 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
77 {
78         int const w = width();
79         int const h = font_metrics::ascent('x', pi.base.font);
80         int xp[4], yp[4];
81
82         xp[0] = x;
83         yp[0] = y - max(h / 4, 1);
84         if (kind_ == NORMAL) {
85                 xp[1] = x;     yp[1] = y;
86                 xp[2] = x + w; yp[2] = y;
87         } else {
88                 xp[1] = x;     yp[1] = y + max(h / 4, 1);
89                 xp[2] = x + w; yp[2] = y + max(h / 4, 1);
90         }
91         xp[3] = x + w;
92         yp[3] = y - max(h / 4, 1);
93
94         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
95                 pi.pain.lines(xp, yp, 4, LColor::latex);
96         else
97                 pi.pain.lines(xp, yp, 4, LColor::special);
98 }
99
100
101 void InsetSpace::write(Buffer const &, ostream & os) const
102 {
103         string command;
104         switch (kind_) {
105         case NORMAL:
106                 command = "\\space";
107                 break;
108         case PROTECTED:
109                 command = "~";
110                 break;
111         case THIN:
112                 command = "\\,";
113                 break;
114         case QUAD:
115                 command = "\\quad{}";
116                 break;
117         case QQUAD:
118                 command = "\\qquad{}";
119                 break;
120         case ENSPACE:
121                 command = "\\enspace{}";
122                 break;
123         case ENSKIP:
124                 command = "\\enskip{}";
125                 break;
126         case NEGTHIN:
127                 command = "\\negthinspace{}";
128                 break;
129         }
130         os << "\\InsetSpace " << command << "\n";
131 }
132
133
134 // This function will not be necessary when lyx3
135 void InsetSpace::read(Buffer const &, LyXLex & lex)
136 {
137         lex.nextToken();
138         string const command = lex.getString();
139
140         if (command == "\\space")
141                 kind_ = NORMAL;
142         else if (command == "~")
143                 kind_ = PROTECTED;
144         else if (command == "\\,")
145                 kind_ = THIN;
146         else if (command == "\\quad{}")
147                 kind_ = QUAD;
148         else if (command == "\\qquad{}")
149                 kind_ = QQUAD;
150         else if (command == "\\enspace{}")
151                 kind_ = ENSPACE;
152         else if (command == "\\enskip{}")
153                 kind_ = ENSKIP;
154         else if (command == "\\negthinspace{}")
155                 kind_ = NEGTHIN;
156         else
157                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
158 }
159
160
161 int InsetSpace::latex(Buffer const &, ostream & os,
162                       LatexRunParams const & runparams) const
163 {
164         switch (kind_) {
165         case NORMAL:
166                 os << (runparams.free_spacing ? " " : "\\ ");
167                 break;
168         case PROTECTED:
169                 os << (runparams.free_spacing ? ' ' : '~');
170                 break;
171         case THIN:
172                 os << (runparams.free_spacing ? " " : "\\,");
173                 break;
174         case QUAD:
175                 os << (runparams.free_spacing ? " " : "\\quad{}");
176                 break;
177         case QQUAD:
178                 os << (runparams.free_spacing ? " " : "\\qquad{}");
179                 break;
180         case ENSPACE:
181                 os << (runparams.free_spacing ? " " : "\\enspace{}");
182                 break;
183         case ENSKIP:
184                 os << (runparams.free_spacing ? " " : "\\enskip{}");
185                 break;
186         case NEGTHIN:
187                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
188                 break;
189         }
190         return 0;
191 }
192
193
194 int InsetSpace::ascii(Buffer const &, ostream & os, int) const
195 {
196         switch (kind_) {
197         case NORMAL:
198         case PROTECTED:
199         case THIN:
200         case QUAD:
201         case QQUAD:
202         case ENSPACE:
203         case ENSKIP:
204         case NEGTHIN:
205                 os << ' ';
206                 break;
207         }
208         return 0;
209 }
210
211
212 int InsetSpace::linuxdoc(Buffer const &, ostream & os) const
213 {
214         switch (kind_) {
215         case NORMAL:
216         case QUAD:
217         case QQUAD:
218         case ENSKIP:
219                 os << " ";
220                 break;
221         case PROTECTED:
222         case ENSPACE:
223         case THIN:
224         case NEGTHIN:
225                 os << "&nbsp;";
226                 break;
227         }
228         return 0;
229 }
230
231
232 int InsetSpace::docbook(Buffer const &, ostream & os, bool) const
233 {
234         switch (kind_) {
235         case NORMAL:
236         case QUAD:
237         case QQUAD:
238         case ENSKIP:
239                 os << " ";
240                 break;
241         case PROTECTED:
242         case ENSPACE:
243         case THIN:
244         case NEGTHIN:
245                 os << "&nbsp;";
246                 break;
247         }
248         return 0;
249 }
250
251
252 auto_ptr<InsetBase>  InsetSpace::clone() const
253 {
254         return auto_ptr<InsetBase>(new InsetSpace(kind_));
255 }
256
257
258 bool InsetSpace::isChar() const
259 {
260         return true;
261 }
262
263 bool InsetSpace::isLetter() const
264 {
265         return false;
266 }
267
268 bool InsetSpace::isSpace() const
269 {
270         return true;
271 }
272
273 bool InsetSpace::isLineSeparator() const
274 {
275         return false;
276 }