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.
31 extern BufferView * current_view;
33 // codes used to read/write quotes to LyX files
34 static char const * const language_char = "esgpfa";
35 static char const * const side_char = "lr" ;
36 static char const * const times_char = "sd";
38 // List of known quote chars
39 static char const * const quote_char = ",'`<>";
41 // Index of chars used for the quote. Index is [side, language]
42 int quote_index[2][6] = {
43 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
44 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
46 // Corresponding LaTeX code, for double and single quotes.
47 static char const * const latex_quote_t1[2][5] =
48 { { "\\quotesinglbase{}", "'", "`",
49 "\\guilsinglleft{}", "\\guilsinglright{}" },
50 { ",,", "''", "``", "<<", ">>" } };
52 static char const * const latex_quote_ot1[2][5] =
53 { { "\\quotesinglbase{}", "'", "`",
54 "\\guilsinglleft{}", "\\guilsinglright{}" },
55 { "\\quotedblbase{}", "''", "``",
56 "\\guillemotleft{}", "\\guillemotright{}" } };
58 static char const * const latex_quote_babel[2][5] =
59 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
60 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
63 InsetQuotes::InsetQuotes(string const & str)
69 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
70 InsetQuotes::quote_side s,
71 InsetQuotes::quote_times t)
72 : language(l), side(s), times(t)
77 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
78 : language(params.quotes_language), times(params.quotes_times)
80 // Decide whether left or right
82 case ' ': case '(': case '{': case '[': case '-': case ':':
83 case LyXParagraph::META_HFILL:
84 case LyXParagraph::META_PROTECTED_SEPARATOR:
85 case LyXParagraph::META_NEWLINE:
86 side = InsetQuotes::LeftQ; // left quote
89 side = InsetQuotes::RightQ; // right quote
94 void InsetQuotes::ParseString(string const & s)
98 if (str.length() != 3) {
99 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
100 " bad string length." << endl;
104 for (i = 0; i < 6; ++i) {
105 if (str[0] == language_char[i]) {
106 language = InsetQuotes::quote_language(i);
111 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
112 " bad language specification." << endl;
113 language = InsetQuotes::EnglishQ;
116 for (i = 0; i < 2; ++i) {
117 if (str[1] == side_char[i]) {
118 side = InsetQuotes::quote_side(i);
123 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
124 " bad side specification." << endl;
125 side = InsetQuotes::LeftQ;
128 for (i = 0; i < 2; ++i) {
129 if (str[2] == times_char[i]) {
130 times = InsetQuotes::quote_times(i);
135 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
136 " bad times specification." << endl;
137 times = InsetQuotes::DoubleQ;
142 string InsetQuotes::DispString() const
145 disp += quote_char[quote_index[side][language]];
146 if (times == InsetQuotes::DoubleQ)
149 if (lyxrc->font_norm == "iso8859-1")
152 else if (disp == ">>")
159 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
161 return font.maxAscent();
165 int InsetQuotes::descent(Painter &, LyXFont const & font) const
167 return font.maxDescent();
171 int InsetQuotes::width(Painter &, LyXFont const & font) const
173 string text = DispString();
176 for (string::size_type i = 0; i < text.length(); ++i) {
178 w += font.width('i');
179 else if (i == 0 || text[i] != text[i-1])
180 w += font.width(text[i]);
182 w += font.width(',');
189 LyXFont InsetQuotes::ConvertFont(LyXFont font)
191 /* quotes-insets cannot be latex of any kind */
192 font.setLatex(LyXFont::OFF);
197 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
198 int baseline, float & x) const
200 string text = DispString();
202 pain.text(int(x), baseline, text, font);
203 x += width(pain, font);
207 void InsetQuotes::Write(ostream & os) const
210 text += language_char[language];
211 text += side_char[side];
212 text += times_char[times];
213 os << "Quotes " << text;
217 void InsetQuotes::Read(LyXLex & lex)
220 ParseString(lex.GetString());
224 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/) const
227 int res = Latex(quote, 0);
233 int InsetQuotes::Latex(string & file, signed char /*fragile*/) const
236 current_view->buffer()->GetLanguage();
237 int quoteind = quote_index[side][language];
240 if (lyxrc->fontenc == "T1") {
241 qstr = latex_quote_t1[times][quoteind];
243 else if (doclang == "default") {
244 qstr = latex_quote_ot1[times][quoteind];
246 else if (language == InsetQuotes::FrenchQ
247 && times == InsetQuotes::DoubleQ
248 && doclang == "frenchb") {
249 if (side == InsetQuotes::LeftQ)
255 qstr = latex_quote_babel[times][quoteind];
257 // protect against !` and ?` ligatures.
258 if ((suffixIs(file, '?') || suffixIs(file, '!'))
267 int InsetQuotes::Linuxdoc(string & file) const
275 int InsetQuotes::DocBook(string & file) const
277 if(times == InsetQuotes::DoubleQ) {
278 if (side == InsetQuotes::LeftQ)
283 if (side == InsetQuotes::LeftQ)
292 void InsetQuotes::Validate(LaTeXFeatures & features) const
294 char type = quote_char[quote_index[side][language]];
296 if (current_view->buffer()->GetLanguage() == "default"
297 && lyxrc->fontenc != "T1") {
298 if (times == InsetQuotes::SingleQ)
300 case ',': features.quotesinglbase = true; break;
301 case '<': features.guilsinglleft = true; break;
302 case '>': features.guilsinglright = true; break;
307 case ',': features.quotedblbase = true; break;
308 case '<': features.guillemotleft = true; break;
309 case '>': features.guillemotright = true; break;
316 Inset * InsetQuotes::Clone() const
318 return new InsetQuotes(language, side, times);
322 Inset::Code InsetQuotes::LyxCode() const
324 return Inset::QUOTE_CODE;