]> git.lyx.org Git - lyx.git/blob - src/insets/insetspace.C
Clean up InsetGraphics::Cache and rename as GraphicsInset.
[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 #include "metricsinfo.h"
28
29 using std::ostream;
30 using std::max;
31
32
33 InsetSpace::InsetSpace()
34 {}
35
36
37 InsetSpace::InsetSpace(Kind k)
38         : kind_(k)
39 {}
40
41
42 InsetSpace::Kind InsetSpace::kind() const
43 {
44         return kind_;
45 }
46
47
48 void InsetSpace::metrics(MetricsInfo & mi, Dimension & dim) const
49 {
50         LyXFont & font = mi.base.font;
51         dim.asc = font_metrics::maxAscent(font);
52         dim.des = font_metrics::maxDescent(font);
53
54         switch (kind_) {
55                 case THIN:
56                 case NEGTHIN:
57                         dim.wid = font_metrics::width("x", font) / 3;
58                         break;
59                 case PROTECTED:
60                 case NORMAL:
61                         dim.wid = font_metrics::width("x", font);
62                         break;
63                 case QUAD:
64                         dim.wid = 20;
65                         break;
66                 case QQUAD:
67                         dim.wid = 40;
68                         break;
69                 case ENSPACE:
70                 case ENSKIP:
71                         dim.wid = 10;
72                         break;
73         }
74 }
75
76
77 void InsetSpace::draw(PainterInfo & pi, int x, int y) const
78 {
79         int const w = width(pi.base.bv, pi.base.font);
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         // fixme: correct?
217         case NORMAL:
218         case QUAD:
219         case QQUAD:
220         case ENSKIP:
221                 os << " ";
222                 break;
223         case PROTECTED:
224         case ENSPACE:
225         case THIN:
226         case NEGTHIN:
227                 os << "&nbsp;";
228                 break;
229         }
230         return 0;
231 }
232
233
234 int InsetSpace::docbook(Buffer const *, ostream & os, bool) const
235 {
236         switch (kind_) {
237         // fixme: correct?
238         case NORMAL:
239         case QUAD:
240         case QQUAD:
241         case ENSKIP:
242                 os << " ";
243                 break;
244         case PROTECTED:
245         case ENSPACE:
246         case THIN:
247         case NEGTHIN:
248                 os << "&nbsp;";
249                 break;
250         }
251         return 0;
252 }
253
254
255 Inset * InsetSpace::clone() const
256 {
257         return new InsetSpace(kind_);
258 }
259
260
261 bool InsetSpace::isChar() const
262 {
263         return true;
264 }
265
266 bool InsetSpace::isLetter() const
267 {
268         return false;
269 }
270
271 bool InsetSpace::isSpace() const
272 {
273         return true;
274 }
275
276 bool InsetSpace::isLineSeparator() const
277 {
278         return false;
279 }