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"
19 #include "BufferView.h"
26 InsetSpecialChar::InsetSpecialChar(Kind k)
31 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
33 return lyxfont::maxAscent(font);
37 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
39 return lyxfont::maxDescent(font);
43 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
48 int w = lyxfont::width('-', font);
50 w -= 2; // to make it look shorter
55 return lyxfont::width('.', font);
59 return lyxfont::width(". . .", font);
63 return lyxfont::width(" x ", font);
65 case PROTECTED_SEPARATOR:
67 return lyxfont::width('x', font);
71 return 1; // To shut up gcc
75 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
76 int baseline, float & x, bool) const
78 Painter & pain = bv->painter();
84 font.setColor(LColor::special);
85 pain.text(int(x), baseline, "-", font);
91 font.setColor(LColor::special);
92 pain.text(int(x), baseline, ".", font);
98 font.setColor(LColor::special);
99 pain.text(int(x), baseline, ". . .", font);
100 x += width(bv, font);
105 // A triangle the width and height of an 'x'
106 int w = lyxfont::width('x', font);
107 int ox = lyxfont::width(' ', font) + int(x);
108 int h = lyxfont::ascent('x', font);
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(bv, font);
120 case PROTECTED_SEPARATOR:
122 float w = width(bv, font);
123 int h = lyxfont::ascent('x', font);
127 yp[0] = baseline - max(h / 4, 1);
136 yp[3] = baseline - max(h / 4, 1);
138 pain.lines(xp, yp, 4, LColor::special);
146 // In lyxf3 this will be just LaTeX
147 void InsetSpecialChar::Write(Buffer const *, ostream & os) const
151 case HYPHENATION: command = "\\-"; break;
152 case END_OF_SENTENCE: command = "\\@."; break;
153 case LDOTS: command = "\\ldots{}"; break;
154 case MENU_SEPARATOR: command = "\\menuseparator"; break;
155 case PROTECTED_SEPARATOR:
156 //command = "\\protected_separator";
160 os << "\\SpecialChar " << command << "\n";
164 // This function will not be necessary when lyx3
165 void InsetSpecialChar::Read(Buffer const *, LyXLex & lex)
168 string command = lex.GetString();
170 if (command == "\\-")
172 else if (command == "\\@.")
173 kind = END_OF_SENTENCE;
174 else if (command == "\\ldots{}")
176 else if (command == "\\menuseparator")
177 kind = MENU_SEPARATOR;
178 else if (command == "\\protected_separator"
180 kind = PROTECTED_SEPARATOR;
182 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
186 int InsetSpecialChar::Latex(Buffer const *, ostream & os, bool /*fragile*/,
187 bool free_space) const
190 case HYPHENATION: os << "\\-"; break;
191 case END_OF_SENTENCE: os << "\\@."; break;
192 case LDOTS: os << "\\ldots{}"; break;
193 case MENU_SEPARATOR: os << "\\lyxarrow{}"; break;
194 case PROTECTED_SEPARATOR: os << (free_space ? " " : "~"); break;
199 int InsetSpecialChar::Ascii(Buffer const *, ostream & os) const
202 case HYPHENATION: break;
203 case END_OF_SENTENCE: os << "."; break;
204 case LDOTS: os << "..."; break;
205 case MENU_SEPARATOR: os << "->"; break;
206 case PROTECTED_SEPARATOR: os << " "; break;
212 int InsetSpecialChar::Linuxdoc(Buffer const * buf, ostream & os) const
214 return Ascii(buf, os);
218 int InsetSpecialChar::DocBook(Buffer const * buf, ostream & os) const
220 return Ascii(buf, os);
224 Inset * InsetSpecialChar::Clone() const
226 return new InsetSpecialChar(kind);
230 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
232 if (kind == MENU_SEPARATOR) {
233 features.lyxarrow = true;