]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
parlist-24-a.diff
[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 "dimension.h"
20 #include "LaTeXFeatures.h"
21 #include "latexrunparams.h"
22 #include "BufferView.h"
23 #include "frontends/Painter.h"
24 #include "frontends/font_metrics.h"
25 #include "lyxlex.h"
26 #include "lyxfont.h"
27
28 using std::ostream;
29 using std::max;
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::dimension(BufferView *, LyXFont const & font,
48                            Dimension & dim) const
49 {
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 }
74
75
76 void InsetSpace::draw(BufferView * bv, LyXFont const & f,
77                             int baseline, float & x) const
78 {
79         Painter & pain = bv->painter();
80         LyXFont font(f);
81
82         float w = width(bv, font);
83         int h = font_metrics::ascent('x', font);
84         int xp[4], yp[4];
85
86         xp[0] = int(x); yp[0] = baseline - max(h / 4, 1);
87         if (kind_ == NORMAL) {
88                 xp[1] = int(x); yp[1] = baseline;
89                 xp[2] = int(x + w); yp[2] = baseline;
90         } else {
91                 xp[1] = int(x); yp[1] = baseline + max(h / 4, 1);
92                 xp[2] = int(x + w); yp[2] = baseline + max(h / 4, 1);
93         }
94         xp[3] = int(x + w); yp[3] = baseline - max(h / 4, 1);
95
96         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
97                 pain.lines(xp, yp, 4, LColor::latex);
98         else
99                 pain.lines(xp, yp, 4, LColor::special);
100         x += w;
101 }
102
103
104 void InsetSpace::write(Buffer const *, ostream & os) const
105 {
106         string command;
107         switch (kind_) {
108         case NORMAL:
109                 command = "\\space";
110                 break;
111         case PROTECTED:
112                 command = "~";
113                 break;
114         case THIN:
115                 command = "\\,";
116                 break;
117         case QUAD:
118                 command = "\\quad{}";
119                 break;
120         case QQUAD:
121                 command = "\\qquad{}";
122                 break;
123         case ENSPACE:
124                 command = "\\enspace{}";
125                 break;
126         case ENSKIP:
127                 command = "\\enskip{}";
128                 break;
129         case NEGTHIN:
130                 command = "\\negthinspace{}";
131                 break;
132         }
133         os << "\\InsetSpace " << command << "\n";
134 }
135
136
137 // This function will not be necessary when lyx3
138 void InsetSpace::read(Buffer const *, LyXLex & lex)
139 {
140         lex.nextToken();
141         string const command = lex.getString();
142
143         if (command == "\\space")
144                 kind_ = NORMAL;
145         else if (command == "~")
146                 kind_ = PROTECTED;
147         else if (command == "\\,")
148                 kind_ = THIN;
149         else if (command == "\\quad{}")
150                 kind_ = QUAD;
151         else if (command == "\\qquad{}")
152                 kind_ = QQUAD;
153         else if (command == "\\enspace{}")
154                 kind_ = ENSPACE;
155         else if (command == "\\enskip{}")
156                 kind_ = ENSKIP;
157         else if (command == "\\negthinspace{}")
158                 kind_ = NEGTHIN;
159         else
160                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
161 }
162
163
164 int InsetSpace::latex(Buffer const *, ostream & os,
165                       LatexRunParams const & runparams) const
166 {
167         switch (kind_) {
168         case NORMAL:
169                 os << (runparams.free_spacing ? " " : "\\ ");
170                 break;
171         case PROTECTED:
172                 os << (runparams.free_spacing ? ' ' : '~');
173                 break;
174         case THIN:
175                 os << (runparams.free_spacing ? " " : "\\,");
176                 break;
177         case QUAD:
178                 os << (runparams.free_spacing ? " " : "\\quad{}");
179                 break;
180         case QQUAD:
181                 os << (runparams.free_spacing ? " " : "\\qquad{}");
182                 break;
183         case ENSPACE:
184                 os << (runparams.free_spacing ? " " : "\\enspace{}");
185                 break;
186         case ENSKIP:
187                 os << (runparams.free_spacing ? " " : "\\enskip{}");
188                 break;
189         case NEGTHIN:
190                 os << (runparams.free_spacing ? " " : "\\negthinspace{}");
191                 break;
192         }
193         return 0;
194 }
195
196
197 int InsetSpace::ascii(Buffer const *, ostream & os, int) const
198 {
199         switch (kind_) {
200         case NORMAL:
201         case PROTECTED:
202         case THIN:
203         case QUAD:
204         case QQUAD:
205         case ENSPACE:
206         case ENSKIP:
207         case NEGTHIN:
208                 os << ' ';
209                 break;
210         }
211         return 0;
212 }
213
214
215 int InsetSpace::linuxdoc(Buffer const *, ostream & os) const
216 {
217         switch (kind_) {
218         // fixme: correct?
219         case NORMAL:
220         case QUAD:
221         case QQUAD:
222         case ENSKIP:
223                 os << " ";
224                 break;
225         case PROTECTED:
226         case ENSPACE:
227         case THIN:
228         case NEGTHIN:
229                 os << "&nbsp;";
230                 break;
231         }
232         return 0;
233 }
234
235
236 int InsetSpace::docbook(Buffer const *, ostream & os, bool) const
237 {
238         switch (kind_) {
239         // fixme: correct?
240         case NORMAL:
241         case QUAD:
242         case QQUAD:
243         case ENSKIP:
244                 os << " ";
245                 break;
246         case PROTECTED:
247         case ENSPACE:
248         case THIN:
249         case NEGTHIN:
250                 os << "&nbsp;";
251                 break;
252         }
253         return 0;
254 }
255
256
257 Inset * InsetSpace::clone(Buffer const &) const
258 {
259         return new InsetSpace(kind_);
260 }
261
262
263 // Inset * InsetSpace::clone(Buffer const &, bool) const
264 // {
265 //      return new InsetSpace(kind_);
266 // }
267
268
269 bool InsetSpace::isChar() const
270 {
271         return true;
272 }
273
274 bool InsetSpace::isLetter() const
275 {
276         return false;
277 }
278
279 bool InsetSpace::isSpace() const
280 {
281         return true;
282 }
283
284 bool InsetSpace::isLineSeparator() const
285 {
286         return false;
287 }