1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1997 Asger Alstrup
8 * ====================================================== */
13 #pragma implementation
16 #include "insetspecialchar.h"
18 #include "LaTeXFeatures.h"
23 InsetSpecialChar::InsetSpecialChar(Kind k)
28 int InsetSpecialChar::ascent(Painter &, LyXFont const & font) const
30 return font.maxAscent();
34 int InsetSpecialChar::descent(Painter &, LyXFont const & font) const
36 return font.maxDescent();
40 int InsetSpecialChar::width(Painter &, LyXFont const & font) const
45 int w = font.textWidth("-", 1);
47 w -= 2; // to make it look shorter
52 return font.textWidth(".", 1);
56 return font.textWidth(". . .", 5);
60 return font.textWidth(" x ", 3);
67 case PROTECTED_SEPARATOR:
69 return font.textWidth("x", 1);
73 return 1; // To shut up gcc
77 void InsetSpecialChar::draw(Painter & pain, LyXFont const & f,
78 int baseline, float & x) const
84 font.setColor(LColor::special);
85 pain.text(int(x), baseline, "-", font);
86 x += width(pain, font);
91 font.setColor(LColor::special);
92 pain.text(int(x), baseline, ".", font);
93 x += width(pain, font);
98 font.setColor(LColor::special);
99 pain.text(int(x), baseline, ". . .", font);
100 x += width(pain, font);
105 // A triangle the width and height of an 'x'
106 int w = font.textWidth("x", 1);
107 int ox = font.textWidth(" ", 1) + int(x);
108 int h = font.ascent('x');
111 xp[0] = ox; yp[0] = baseline;
112 xp[1] = ox; yp[1] = baseline - h;
113 xp[2] = ox + w; yp[2] = baseline - h/2;
114 xp[3] = ox; yp[3] = baseline;
116 pain.lines(xp, yp, 4, LColor::special);
117 x += width(pain, font);
125 case PROTECTED_SEPARATOR:
127 float w = width(pain, font);
128 int h = font.ascent('x');
132 yp[0] = baseline - max(h / 4, 1);
141 yp[3] = baseline - max(h / 4, 1);
143 pain.lines(xp, yp, 4, LColor::special);
151 // In lyxf3 this will be just LaTeX
152 void InsetSpecialChar::Write(ostream & os) const
156 case HYPHENATION: command = "\\-"; break;
157 case END_OF_SENTENCE: command = "\\@."; break;
158 case LDOTS: command = "\\ldots{}"; break;
159 case MENU_SEPARATOR: command = "\\menuseparator"; break;
161 case NEWLINE: command = "\\newline"; break;
163 case PROTECTED_SEPARATOR:
164 command = "\\protected_separator"; break;
166 os << "\\SpecialChar " << command << "\n";
170 // This function will not be necessary when lyx3
171 void InsetSpecialChar::Read(LyXLex & lex)
174 string command = lex.GetString();
176 if (command == "\\-")
178 else if (command == "\\@.")
179 kind = END_OF_SENTENCE;
180 else if (command == "\\ldots{}")
182 else if (command == "\\menuseparator")
183 kind = MENU_SEPARATOR;
184 else if (command == "\\protected_separator")
185 kind = PROTECTED_SEPARATOR;
187 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
191 int InsetSpecialChar::Latex(ostream & os, signed char /*fragile*/,
192 bool free_space) const
195 case HYPHENATION: os << "\\-"; break;
196 case END_OF_SENTENCE: os << "\\@."; break;
197 case LDOTS: os << "\\ldots{}"; break;
198 case MENU_SEPARATOR: os << "\\lyxarrow{}"; break;
199 case PROTECTED_SEPARATOR: os << (free_space ? " " : "~"); break;
205 int InsetSpecialChar::Linuxdoc(ostream & os) const
208 case HYPHENATION: os << ""; break;
209 case END_OF_SENTENCE: os << ""; break;
210 case LDOTS: os << "..."; break;
211 case MENU_SEPARATOR: os << "->"; break;
212 case PROTECTED_SEPARATOR: os << " "; break;
218 int InsetSpecialChar::DocBook(ostream & os) const
221 case HYPHENATION: os << ""; break;
222 case END_OF_SENTENCE: os << ""; break;
223 case LDOTS: os << "..."; break;
224 case MENU_SEPARATOR: os << "->"; break;
225 case PROTECTED_SEPARATOR: os << " "; break;
231 Inset * InsetSpecialChar::Clone() const
233 return new InsetSpecialChar(kind);
237 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
239 if (kind == MENU_SEPARATOR) {
240 features.lyxarrow = true;