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