1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 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"
27 // Quotes. Used for the various quotes. German, English, French,
28 // Danish, Polish, all either double or single.
30 extern BufferView * current_view;
32 // codes used to read/write quotes to LyX files
33 static char const * const language_char = "esgpfa";
34 static char const * const side_char = "lr" ;
35 static char const * const times_char = "sd";
37 // List of known quote chars
38 static char const * const quote_char = ",'`<>";
40 // Index of chars used for the quote. Index is [side, language]
41 int quote_index[2][6] = {
42 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
43 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
45 // Corresponding LaTeX code, for double and single quotes.
46 static char const * const latex_quote_t1[2][5] =
47 { { "\\quotesinglbase{}", "'", "`",
48 "\\guilsinglleft{}", "\\guilsinglright{}" },
49 { ",,", "''", "``", "<<", ">>" } };
51 static char const * const latex_quote_ot1[2][5] =
52 { { "\\quotesinglbase{}", "'", "`",
53 "\\guilsinglleft{}", "\\guilsinglright{}" },
54 { "\\quotedblbase{}", "''", "``",
55 "\\guillemotleft{}", "\\guillemotright{}" } };
57 static char const * const latex_quote_babel[2][5] =
58 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
59 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
62 InsetQuotes::InsetQuotes(string const & str)
68 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
69 InsetQuotes::quote_side s,
70 InsetQuotes::quote_times t)
71 : language(l), side(s), times(t)
76 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
77 : language(params.quotes_language), times(params.quotes_times)
79 // Decide whether left or right
81 case ' ': case '(': case '{': case '[': case '-': case ':':
82 case LyXParagraph::META_HFILL:
83 #warning think about this
85 case LyXParagraph::META_PROTECTED_SEPARATOR:
87 case LyXParagraph::META_NEWLINE:
88 side = InsetQuotes::LeftQ; // left quote
91 side = InsetQuotes::RightQ; // right quote
96 void InsetQuotes::ParseString(string const & s)
100 if (str.length() != 3) {
101 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
102 " 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(Painter &, LyXFont const & font) const
163 return font.maxAscent();
167 int InsetQuotes::descent(Painter &, LyXFont const & font) const
169 return font.maxDescent();
173 int InsetQuotes::width(Painter &, LyXFont const & font) const
175 string text = DispString();
178 for (string::size_type i = 0; i < text.length(); ++i) {
180 w += font.width('i');
181 else if (i == 0 || text[i] != text[i-1])
182 w += font.width(text[i]);
184 w += font.width(',');
191 LyXFont InsetQuotes::ConvertFont(LyXFont font)
193 /* quotes-insets cannot be latex of any kind */
194 font.setLatex(LyXFont::OFF);
199 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
200 int baseline, float & x) const
202 string text = DispString();
204 pain.text(int(x), baseline, text, font);
205 x += width(pain, font);
209 void InsetQuotes::Write(ostream & os) const
212 text += language_char[language];
213 text += side_char[side];
214 text += times_char[times];
215 os << "Quotes " << text;
219 void InsetQuotes::Read(LyXLex & lex)
222 ParseString(lex.GetString());
226 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/, bool) const
229 current_view->buffer()->GetLanguage();
230 int quoteind = quote_index[side][language];
233 if (lyxrc.fontenc == "T1") {
234 qstr = latex_quote_t1[times][quoteind];
236 else if (doclang == "default") {
237 qstr = latex_quote_ot1[times][quoteind];
239 else if (language == InsetQuotes::FrenchQ
240 && times == InsetQuotes::DoubleQ
241 && doclang == "frenchb") {
242 if (side == InsetQuotes::LeftQ)
248 qstr = latex_quote_babel[times][quoteind];
250 // protect against !` and ?` ligatures.
251 // Is it very bad of us to always protect against those ligatures?
253 if ((suffixIs(file, '?') || suffixIs(file, '!'))
257 // Always guard against unfortunate ligatures (!` ?`)
258 qstr.insert(0, "{}");
266 int InsetQuotes::Linuxdoc(ostream & os) const
273 int InsetQuotes::DocBook(ostream & os) const
275 if(times == InsetQuotes::DoubleQ) {
276 if (side == InsetQuotes::LeftQ)
281 if (side == InsetQuotes::LeftQ)
290 void InsetQuotes::Validate(LaTeXFeatures & features) const
292 char type = quote_char[quote_index[side][language]];
294 if (current_view->buffer()->GetLanguage() == "default"
295 && lyxrc.fontenc != "T1") {
296 if (times == InsetQuotes::SingleQ)
298 case ',': features.quotesinglbase = true; break;
299 case '<': features.guilsinglleft = true; break;
300 case '>': features.guilsinglright = true; break;
305 case ',': features.quotedblbase = true; break;
306 case '<': features.guillemotleft = true; break;
307 case '>': features.guillemotright = true; break;
314 Inset * InsetQuotes::Clone() const
316 return new InsetQuotes(language, side, times);
320 Inset::Code InsetQuotes::LyxCode() const
322 return Inset::QUOTE_CODE;