1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetquotes.h"
18 #include "support/lyxlib.h"
23 #include "LaTeXFeatures.h"
24 #include "support/lstrings.h"
31 // Quotes. Used for the various quotes. German, English, French,
32 // Danish, Polish, all either double or single.
34 extern BufferView * current_view;
36 // codes used to read/write quotes to LyX files
37 static char const * const language_char = "esgpfa";
38 static char const * const side_char = "lr" ;
39 static char const * const times_char = "sd";
41 // List of known quote chars
42 static char const * const quote_char = ",'`<>";
44 // Index of chars used for the quote. Index is [side, language]
45 int quote_index[2][6] = {
46 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
47 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
49 // Corresponding LaTeX code, for double and single quotes.
50 static char const * const latex_quote_t1[2][5] =
51 { { "\\quotesinglbase{}", "'", "`",
52 "\\guilsinglleft{}", "\\guilsinglright{}" },
53 { ",,", "''", "``", "<<", ">>" } };
55 static char const * const latex_quote_ot1[2][5] =
56 { { "\\quotesinglbase{}", "'", "`",
57 "\\guilsinglleft{}", "\\guilsinglright{}" },
58 { "\\quotedblbase{}", "''", "``",
59 "\\guillemotleft{}", "\\guillemotright{}" } };
61 static char const * const latex_quote_babel[2][5] =
62 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
63 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
66 InsetQuotes::InsetQuotes(string const & str)
72 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
73 InsetQuotes::quote_side s,
74 InsetQuotes::quote_times t)
75 : language(l), side(s), times(t)
79 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
80 : language(params.quotes_language), times(params.quotes_times)
82 // Decide whether left or right
84 case ' ': case '(': case '{': case '[': case '-': case ':':
85 case LyXParagraph::META_HFILL:
86 case LyXParagraph::META_NEWLINE:
87 side = InsetQuotes::LeftQ; // left quote
90 side = InsetQuotes::RightQ; // right quote
95 void InsetQuotes::ParseString(string const & s)
98 if (str.length() != 3) {
99 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
100 " bad string length." << endl;
106 for (i = 0; i < 6; ++i) {
107 if (str[0] == language_char[i]) {
108 language = InsetQuotes::quote_language(i);
113 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
114 " bad language specification." << endl;
115 language = InsetQuotes::EnglishQ;
118 for (i = 0; i < 2; ++i) {
119 if (str[1] == side_char[i]) {
120 side = InsetQuotes::quote_side(i);
125 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
126 " bad side specification." << endl;
127 side = InsetQuotes::LeftQ;
130 for (i = 0; i < 2; ++i) {
131 if (str[2] == times_char[i]) {
132 times = InsetQuotes::quote_times(i);
137 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
138 " bad times specification." << endl;
139 times = InsetQuotes::DoubleQ;
144 string InsetQuotes::DispString() const
147 disp += quote_char[quote_index[side][language]];
148 if (times == InsetQuotes::DoubleQ)
151 if (lyxrc.font_norm == "iso8859-1")
154 else if (disp == ">>")
161 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
163 return lyxfont::maxAscent(font);
167 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
169 return lyxfont::maxDescent(font);
173 int InsetQuotes::width(BufferView *, LyXFont const & font) const
175 string text = DispString();
178 for (string::size_type i = 0; i < text.length(); ++i) {
180 w += lyxfont::width('i', font);
181 else if (i == 0 || text[i] != text[i-1])
182 w += lyxfont::width(text[i], font);
184 w += lyxfont::width(',', font);
191 //LyXFont InsetQuotes::ConvertFont(LyXFont font)
192 // I really belive this should be
193 LyXFont InsetQuotes::ConvertFont(LyXFont const & f)
196 // quotes-insets cannot be latex of any kind
197 font.setLatex(LyXFont::OFF);
202 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
203 int baseline, float & x, bool) const
205 string text = DispString();
207 bv->painter().text(int(x), baseline, text, font);
208 x += width(bv, font);
212 void InsetQuotes::Write(Buffer const *, ostream & os) const
215 text += language_char[language];
216 text += side_char[side];
217 text += times_char[times];
218 os << "Quotes " << text;
222 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
225 ParseString(lex.GetString());
227 string tmp(lex.GetString());
228 if (tmp != "\\end_inset")
229 lyxerr << "LyX Warning: Missing \\end_inset "
230 "in InsetQuotes::Read." << endl;
234 int InsetQuotes::Latex(Buffer const *, ostream & os,
235 bool /*fragile*/, bool) const
238 current_view->buffer()->GetLanguage();
239 int quoteind = quote_index[side][language];
242 if (lyxrc.fontenc == "T1") {
243 qstr = latex_quote_t1[times][quoteind];
244 } else if (doclang == "default") {
245 qstr = latex_quote_ot1[times][quoteind];
246 } else if (language == InsetQuotes::FrenchQ
247 && times == InsetQuotes::DoubleQ
248 && doclang == "frenchb") {
249 if (side == InsetQuotes::LeftQ)
254 qstr = latex_quote_babel[times][quoteind];
256 // Always guard against unfortunate ligatures (!` ?`)
257 qstr.insert(0, "{}");
264 int InsetQuotes::Ascii(Buffer const *, ostream & os) const
271 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
278 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
280 if(times == InsetQuotes::DoubleQ) {
281 if (side == InsetQuotes::LeftQ)
286 if (side == InsetQuotes::LeftQ)
295 void InsetQuotes::Validate(LaTeXFeatures & features) const
297 char type = quote_char[quote_index[side][language]];
299 if (current_view->buffer()->GetLanguage() == "default"
300 && lyxrc.fontenc != "T1") {
301 if (times == InsetQuotes::SingleQ)
303 case ',': features.quotesinglbase = true; break;
304 case '<': features.guilsinglleft = true; break;
305 case '>': features.guilsinglright = true; break;
310 case ',': features.quotedblbase = true; break;
311 case '<': features.guillemotleft = true; break;
312 case '>': features.guillemotright = true; break;
319 Inset * InsetQuotes::Clone() const
321 return new InsetQuotes(language, side, times);
325 Inset::Code InsetQuotes::LyxCode() const
327 return Inset::QUOTE_CODE;