]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
(Juergen) the new space inset.
[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 "BufferView.h"
22 #include "frontends/Painter.h"
23 #include "frontends/font_metrics.h"
24 #include "lyxlex.h"
25 #include "lyxfont.h"
26
27 using std::ostream;
28 using std::max;
29
30
31 InsetSpace::InsetSpace(Kind k)
32         : kind_(k)
33 {}
34
35
36 InsetSpace::Kind InsetSpace::kind() const
37 {
38         return kind_;
39 }
40
41
42 void InsetSpace::dimension(BufferView *, LyXFont const & font,
43                            Dimension & dim) const
44 {
45         dim.a = font_metrics::maxAscent(font);
46         dim.d = font_metrics::maxDescent(font);
47
48         switch (kind_) {
49                 case THIN:
50                 case NEGTHIN:
51                         dim.w = font_metrics::width("x", font) / 3;
52                         break;
53                 case PROTECTED:
54                 case NORMAL:
55                         dim.w = font_metrics::width("x", font);
56                         break;
57                 case QUAD:
58                         dim.w = 20;
59                         break;
60                 case QQUAD:
61                         dim.w = 40;
62                         break;
63                 case ENSPACE:
64                 case ENSKIP:
65                         dim.w = 10;
66                         break;
67         }
68 }
69
70
71 void InsetSpace::draw(BufferView * bv, LyXFont const & f,
72                             int baseline, float & x) const
73 {
74         Painter & pain = bv->painter();
75         LyXFont font(f);
76
77         float w = width(bv, font);
78         int h = font_metrics::ascent('x', font);
79         int xp[4], yp[4];
80
81         xp[0] = int(x); yp[0] = baseline - max(h / 4, 1);
82         if (kind_ == NORMAL) {
83                 xp[1] = int(x); yp[1] = baseline;
84                 xp[2] = int(x + w); yp[2] = baseline;
85         } else {
86                 xp[1] = int(x); yp[1] = baseline + max(h / 4, 1);
87                 xp[2] = int(x + w); yp[2] = baseline + max(h / 4, 1);
88         }
89         xp[3] = int(x + w); yp[3] = baseline - max(h / 4, 1);
90
91         if (kind_ == PROTECTED || kind_ == ENSPACE || kind_ == NEGTHIN)
92                 pain.lines(xp, yp, 4, LColor::latex);
93         else
94                 pain.lines(xp, yp, 4, LColor::special);
95         x += w;
96 }
97
98
99 void InsetSpace::write(Buffer const *, ostream & os) const
100 {
101         string command;
102         switch (kind_) {
103         case NORMAL:
104                 command = "\\space";
105                 break;
106         case PROTECTED:
107                 command = "~";
108                 break;
109         case THIN:
110                 command = "\\,";
111                 break;
112         case QUAD:
113                 command = "\\quad{}";
114                 break;
115         case QQUAD:
116                 command = "\\qquad{}";
117                 break;
118         case ENSPACE:
119                 command = "\\enspace{}";
120                 break;
121         case ENSKIP:
122                 command = "\\enskip{}";
123                 break;
124         case NEGTHIN:
125                 command = "\\negthinspace{}";
126                 break;
127         }
128         os << "\\InsetSpace " << command << "\n";
129 }
130
131
132 // This function will not be necessary when lyx3
133 void InsetSpace::read(Buffer const *, LyXLex & lex)
134 {
135         lex.nextToken();
136         string const command = lex.getString();
137
138         if (command == "\\space")
139                 kind_ = NORMAL;
140         else if (command == "~")
141                 kind_ = PROTECTED;
142         else if (command == "\\,")
143                 kind_ = THIN;
144         else if (command == "\\quad{}")
145                 kind_ = QUAD;
146         else if (command == "\\qquad{}")
147                 kind_ = QQUAD;
148         else if (command == "\\enspace{}")
149                 kind_ = ENSPACE;
150         else if (command == "\\enskip{}")
151                 kind_ = ENSKIP;
152         else if (command == "\\negthinspace{}")
153                 kind_ = NEGTHIN;
154         else
155                 lex.printError("InsetSpace: Unknown kind: `$$Token'");
156 }
157
158
159 int InsetSpace::latex(Buffer const *, ostream & os, bool /*fragile*/,
160                             bool free_space) const
161 {
162         switch (kind_) {
163         case NORMAL:
164                 os << (free_space ? " " : "\\ ");
165                 break;
166         case PROTECTED:
167                 os << (free_space ? ' ' : '~');
168                 break;
169         case THIN:
170                 os << (free_space ? " " : "\\,");
171                 break;
172         case QUAD:
173                 os << (free_space ? " " : "\\quad{}");
174                 break;
175         case QQUAD:
176                 os << (free_space ? " " : "\\qquad{}");
177                 break;
178         case ENSPACE:
179                 os << (free_space ? " " : "\\enspace{}");
180                 break;
181         case ENSKIP:
182                 os << (free_space ? " " : "\\enskip{}");
183                 break;
184         case NEGTHIN:
185                 os << (free_space ? " " : "\\negthinspace{}");
186                 break;
187         }
188         return 0;
189 }
190
191
192 int InsetSpace::ascii(Buffer const *, ostream & os, int) const
193 {
194         switch (kind_) {
195         case NORMAL:
196         case PROTECTED:
197         case THIN:
198         case QUAD:
199         case QQUAD:
200         case ENSPACE:
201         case ENSKIP:
202         case NEGTHIN:
203                 os << ' ';
204                 break;
205         }
206         return 0;
207 }
208
209
210 int InsetSpace::linuxdoc(Buffer const *, ostream & os) const
211 {
212         switch (kind_) {
213         // fixme: correct?
214         case NORMAL:
215         case QUAD:
216         case QQUAD:
217         case ENSKIP:
218                 os << " ";
219                 break;
220         case PROTECTED:
221         case ENSPACE:
222         case THIN:
223         case NEGTHIN:
224                 os << "&nbsp;";
225                 break;
226         }
227         return 0;
228 }
229
230
231 int InsetSpace::docbook(Buffer const *, ostream & os, bool) const
232 {
233         switch (kind_) {
234         // fixme: correct?
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 Inset * InsetSpace::clone(Buffer const &, bool) const
253 {
254         return 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 }