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)
80 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
81 : language(params.quotes_language), times(params.quotes_times)
83 // Decide whether left or right
85 case ' ': case '(': case '{': case '[': case '-': case ':':
86 case LyXParagraph::META_HFILL:
87 case LyXParagraph::META_NEWLINE:
88 side = InsetQuotes::LeftQ; // left quote
91 side = InsetQuotes::RightQ; // right quote
96 void InsetQuotes::ParseString(string const & s)
99 if (str.length() != 3) {
100 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
101 " bad string length." << endl;
107 for (i = 0; i < 6; ++i) {
108 if (str[0] == language_char[i]) {
109 language = InsetQuotes::quote_language(i);
114 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
115 " bad language specification." << endl;
116 language = InsetQuotes::EnglishQ;
119 for (i = 0; i < 2; ++i) {
120 if (str[1] == side_char[i]) {
121 side = InsetQuotes::quote_side(i);
126 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
127 " bad side specification." << endl;
128 side = InsetQuotes::LeftQ;
131 for (i = 0; i < 2; ++i) {
132 if (str[2] == times_char[i]) {
133 times = InsetQuotes::quote_times(i);
138 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
139 " bad times specification." << endl;
140 times = InsetQuotes::DoubleQ;
145 string InsetQuotes::DispString() const
148 disp += quote_char[quote_index[side][language]];
149 if (times == InsetQuotes::DoubleQ)
152 if (lyxrc.font_norm == "iso8859-1")
155 else if (disp == ">>")
162 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
164 return lyxfont::maxAscent(font);
168 int InsetQuotes::descent(Painter &, LyXFont const & font) const
170 return lyxfont::maxDescent(font);
174 int InsetQuotes::width(Painter &, LyXFont const & font) const
176 string text = DispString();
179 for (string::size_type i = 0; i < text.length(); ++i) {
181 w += lyxfont::width('i', font);
182 else if (i == 0 || text[i] != text[i-1])
183 w += lyxfont::width(text[i], font);
185 w += lyxfont::width(',', font);
192 LyXFont InsetQuotes::ConvertFont(LyXFont font)
194 /* quotes-insets cannot be latex of any kind */
195 font.setLatex(LyXFont::OFF);
200 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
201 int baseline, float & x) const
203 string text = DispString();
205 bv->painter().text(int(x), baseline, text, font);
206 x += width(bv->painter(), font);
210 void InsetQuotes::Write(Buffer const *, ostream & os) const
213 text += language_char[language];
214 text += side_char[side];
215 text += times_char[times];
216 os << "Quotes " << text;
220 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
223 ParseString(lex.GetString());
227 int InsetQuotes::Latex(Buffer const *, ostream & os, bool /*fragile*/, bool) const
230 current_view->buffer()->GetLanguage();
231 int quoteind = quote_index[side][language];
234 if (lyxrc.fontenc == "T1") {
235 qstr = latex_quote_t1[times][quoteind];
237 else if (doclang == "default") {
238 qstr = latex_quote_ot1[times][quoteind];
240 else if (language == InsetQuotes::FrenchQ
241 && times == InsetQuotes::DoubleQ
242 && doclang == "frenchb") {
243 if (side == InsetQuotes::LeftQ)
248 qstr = latex_quote_babel[times][quoteind];
250 // Always guard against unfortunate ligatures (!` ?`)
251 qstr.insert(0, "{}");
258 int InsetQuotes::Ascii(Buffer const *, ostream & os) const
265 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
272 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
274 if(times == InsetQuotes::DoubleQ) {
275 if (side == InsetQuotes::LeftQ)
280 if (side == InsetQuotes::LeftQ)
289 void InsetQuotes::Validate(LaTeXFeatures & features) const
291 char type = quote_char[quote_index[side][language]];
293 if (current_view->buffer()->GetLanguage() == "default"
294 && lyxrc.fontenc != "T1") {
295 if (times == InsetQuotes::SingleQ)
297 case ',': features.quotesinglbase = true; break;
298 case '<': features.guilsinglleft = true; break;
299 case '>': features.guilsinglright = true; break;
304 case ',': features.quotedblbase = true; break;
305 case '<': features.guillemotleft = true; break;
306 case '>': features.guillemotright = true; break;
313 Inset * InsetQuotes::Clone() const
315 return new InsetQuotes(language, side, times);
319 Inset::Code InsetQuotes::LyxCode() const
321 return Inset::QUOTE_CODE;