]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
Removed all redundant using directives from the source.
[lyx.git] / src / insets / insetspecialchar.C
1 /**
2  * \file insetspecialchar.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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetspecialchar.h"
16
17 #include "debug.h"
18 #include "LaTeXFeatures.h"
19 #include "lyxlex.h"
20 #include "metricsinfo.h"
21
22 #include "frontends/font_metrics.h"
23 #include "frontends/Painter.h"
24
25 using std::auto_ptr;
26 using std::ostream;
27
28
29 InsetSpecialChar::InsetSpecialChar(Kind k)
30         : kind_(k)
31 {}
32
33
34 InsetSpecialChar::Kind InsetSpecialChar::kind() const
35 {
36         return kind_;
37 }
38
39
40 void InsetSpecialChar::metrics(MetricsInfo & mi, Dimension & dim) const
41 {
42         LyXFont & font = mi.base.font;
43         dim.asc = font_metrics::maxAscent(font);
44         dim.des = font_metrics::maxDescent(font);
45
46         string s;
47         switch (kind_) {
48                 case LIGATURE_BREAK:      s = "|";     break;
49                 case END_OF_SENTENCE:     s = ".";     break;
50                 case LDOTS:               s = ". . ."; break;
51                 case MENU_SEPARATOR:      s = " x ";   break;
52                 case HYPHENATION:      s = "-";   break;
53         }
54         dim.wid = font_metrics::width(s, font);
55         if (kind_ == HYPHENATION && dim.wid > 5)
56                 dim.wid -= 2; // to make it look shorter
57         dim_ = dim;
58 }
59
60
61 void InsetSpecialChar::draw(PainterInfo & pi, int x, int y) const
62 {
63         LyXFont font = pi.base.font;
64
65         switch (kind_) {
66         case HYPHENATION:
67         {
68                 font.setColor(LColor::special);
69                 pi.pain.text(x, y, '-', font);
70                 break;
71         }
72         case LIGATURE_BREAK:
73         {
74                 font.setColor(LColor::special);
75                 pi.pain.text(x, y, '|', font);
76                 break;
77         }
78         case END_OF_SENTENCE:
79         {
80                 font.setColor(LColor::special);
81                 pi.pain.text(x, y, '.', font);
82                 break;
83         }
84         case LDOTS:
85         {
86                 font.setColor(LColor::special);
87                 pi.pain.text(x, y, ". . .", font);
88                 break;
89         }
90         case MENU_SEPARATOR:
91         {
92                 // A triangle the width and height of an 'x'
93                 int w = font_metrics::width('x', font);
94                 int ox = font_metrics::width(' ', font) + x;
95                 int h = font_metrics::ascent('x', font);
96                 int xp[4], yp[4];
97
98                 xp[0] = ox;     yp[0] = y;
99                 xp[1] = ox;     yp[1] = y - h;
100                 xp[2] = ox + w; yp[2] = y - h/2;
101                 xp[3] = ox;     yp[3] = y;
102
103                 pi.pain.lines(xp, yp, 4, LColor::special);
104                 break;
105         }
106         }
107 }
108
109
110 // In lyxf3 this will be just LaTeX
111 void InsetSpecialChar::write(Buffer const &, ostream & os) const
112 {
113         string command;
114         switch (kind_) {
115         case HYPHENATION:
116                 command = "\\-";
117                 break;
118         case LIGATURE_BREAK:
119                 command = "\\textcompwordmark{}";
120                 break;
121         case END_OF_SENTENCE:
122                 command = "\\@.";
123                 break;
124         case LDOTS:
125                 command = "\\ldots{}";
126                 break;
127         case MENU_SEPARATOR:
128                 command = "\\menuseparator";
129                 break;
130         }
131         os << "\\SpecialChar " << command << "\n";
132 }
133
134
135 // This function will not be necessary when lyx3
136 void InsetSpecialChar::read(Buffer const &, LyXLex & lex)
137 {
138         lex.nextToken();
139         string const command = lex.getString();
140
141         if (command == "\\-")
142                 kind_ = HYPHENATION;
143         else if (command == "\\textcompwordmark{}")
144                 kind_ = LIGATURE_BREAK;
145         else if (command == "\\@.")
146                 kind_ = END_OF_SENTENCE;
147         else if (command == "\\ldots{}")
148                 kind_ = LDOTS;
149         else if (command == "\\menuseparator")
150                 kind_ = MENU_SEPARATOR;
151         else
152                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
153 }
154
155
156 int InsetSpecialChar::latex(Buffer const &, ostream & os,
157                             LatexRunParams const &) const
158 {
159         switch (kind_) {
160         case HYPHENATION:
161                 os << "\\-";
162                 break;
163         case LIGATURE_BREAK:
164                 os << "\\textcompwordmark{}";
165                 break;
166         case END_OF_SENTENCE:
167                 os << "\\@.";
168                 break;
169         case LDOTS:
170                 os << "\\ldots{}";
171                 break;
172         case MENU_SEPARATOR:
173                 os << "\\lyxarrow{}";
174                 break;
175         }
176         return 0;
177 }
178
179
180 int InsetSpecialChar::ascii(Buffer const &, ostream & os, int) const
181 {
182         switch (kind_) {
183         case HYPHENATION:
184         case LIGATURE_BREAK:
185                 break;
186         case END_OF_SENTENCE:
187                 os << '.';
188                 break;
189         case LDOTS:
190                 os << "...";
191                 break;
192         case MENU_SEPARATOR:
193                 os << "->";
194                 break;
195         }
196         return 0;
197 }
198
199
200 int InsetSpecialChar::linuxdoc(Buffer const &, ostream & os) const
201 {
202         switch (kind_) {
203         case HYPHENATION:
204         case LIGATURE_BREAK:
205                 break;
206         case END_OF_SENTENCE:
207                 os << '.';
208                 break;
209         case LDOTS:
210                 os << "...";
211                 break;
212         case MENU_SEPARATOR:
213                 os << "&lyxarrow;";
214                 break;
215         }
216         return 0;
217 }
218
219
220 int InsetSpecialChar::docbook(Buffer const &, ostream & os, bool) const
221 {
222         switch (kind_) {
223         case HYPHENATION:
224         case LIGATURE_BREAK:
225                 break;
226         case END_OF_SENTENCE:
227                 os << '.';
228                 break;
229         case LDOTS:
230                 os << "...";
231                 break;
232         case MENU_SEPARATOR:
233                 os << "&lyxarrow;";
234                 break;
235         }
236         return 0;
237 }
238
239
240 auto_ptr<InsetBase> InsetSpecialChar::clone() const
241 {
242         return auto_ptr<InsetBase>(new InsetSpecialChar(kind_));
243 }
244
245
246 void InsetSpecialChar::validate(LaTeXFeatures & features) const
247 {
248         if (kind_ == MENU_SEPARATOR) {
249                 features.require("lyxarrow");
250         }
251 }
252
253
254 bool InsetSpecialChar::isChar() const
255 {
256         return true;
257 }
258
259
260 bool InsetSpecialChar::isLetter() const
261 {
262         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
263 }
264
265
266 bool InsetSpecialChar::isSpace() const
267 {
268         return false;
269 }
270
271
272 bool InsetSpecialChar::isLineSeparator() const
273 {
274 #if 0
275         // this would be nice, but it does not work, since
276         // Paragraph::stripLeadingSpaces nukes the characters which
277         // have this property. I leave the code here, since it should
278         // eventually be made to work. (JMarc 20020327)
279         return kind_ == HYPHENATION || kind_ == MENU_SEPARATOR;
280 #else
281         return false;
282 #endif
283 }