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"
29 // Quotes. Used for the various quotes. German, English, French,
30 // Danish, Polish, all either double or single.
32 extern BufferView * current_view;
34 // codes used to read/write quotes to LyX files
35 static char const * const language_char = "esgpfa";
36 static char const * const side_char = "lr" ;
37 static char const * const times_char = "sd";
39 // List of known quote chars
40 static char const * const quote_char = ",'`<>";
42 // Index of chars used for the quote. Index is [side, language]
43 int quote_index[2][6] = {
44 { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
45 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
47 // Corresponding LaTeX code, for double and single quotes.
48 static char const * const latex_quote_t1[2][5] =
49 { { "\\quotesinglbase{}", "'", "`",
50 "\\guilsinglleft{}", "\\guilsinglright{}" },
51 { ",,", "''", "``", "<<", ">>" } };
53 static char const * const latex_quote_ot1[2][5] =
54 { { "\\quotesinglbase{}", "'", "`",
55 "\\guilsinglleft{}", "\\guilsinglright{}" },
56 { "\\quotedblbase{}", "''", "``",
57 "\\guillemotleft{}", "\\guillemotright{}" } };
59 static char const * const latex_quote_babel[2][5] =
60 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
61 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
64 InsetQuotes::InsetQuotes(string const & str)
70 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
71 InsetQuotes::quote_side s,
72 InsetQuotes::quote_times t)
73 : language(l), side(s), times(t)
78 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
79 : language(params.quotes_language), times(params.quotes_times)
81 // Decide whether left or right
83 case ' ': case '(': case '{': case '[': case '-': case ':':
84 case LyXParagraph::META_HFILL:
85 #warning think about this
87 case LyXParagraph::META_PROTECTED_SEPARATOR:
89 case LyXParagraph::META_NEWLINE:
90 side = InsetQuotes::LeftQ; // left quote
93 side = InsetQuotes::RightQ; // right quote
98 void InsetQuotes::ParseString(string const & s)
101 if (str.length() != 3) {
102 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
103 " bad string length." << endl;
109 for (i = 0; i < 6; ++i) {
110 if (str[0] == language_char[i]) {
111 language = InsetQuotes::quote_language(i);
116 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
117 " bad language specification." << endl;
118 language = InsetQuotes::EnglishQ;
121 for (i = 0; i < 2; ++i) {
122 if (str[1] == side_char[i]) {
123 side = InsetQuotes::quote_side(i);
128 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129 " bad side specification." << endl;
130 side = InsetQuotes::LeftQ;
133 for (i = 0; i < 2; ++i) {
134 if (str[2] == times_char[i]) {
135 times = InsetQuotes::quote_times(i);
140 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141 " bad times specification." << endl;
142 times = InsetQuotes::DoubleQ;
147 string InsetQuotes::DispString() const
150 disp += quote_char[quote_index[side][language]];
151 if (times == InsetQuotes::DoubleQ)
154 if (lyxrc.font_norm == "iso8859-1")
157 else if (disp == ">>")
164 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
166 return font.maxAscent();
170 int InsetQuotes::descent(Painter &, LyXFont const & font) const
172 return font.maxDescent();
176 int InsetQuotes::width(Painter &, LyXFont const & font) const
178 string text = DispString();
181 for (string::size_type i = 0; i < text.length(); ++i) {
183 w += font.width('i');
184 else if (i == 0 || text[i] != text[i-1])
185 w += font.width(text[i]);
187 w += font.width(',');
194 LyXFont InsetQuotes::ConvertFont(LyXFont font)
196 /* quotes-insets cannot be latex of any kind */
197 font.setLatex(LyXFont::OFF);
202 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
203 int baseline, float & x) const
205 string text = DispString();
207 pain.text(int(x), baseline, text, font);
208 x += width(pain, font);
212 void InsetQuotes::Write(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(LyXLex & lex)
225 ParseString(lex.GetString());
229 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/, bool) const
232 current_view->buffer()->GetLanguage();
233 int quoteind = quote_index[side][language];
236 if (lyxrc.fontenc == "T1") {
237 qstr = latex_quote_t1[times][quoteind];
239 else if (doclang == "default") {
240 qstr = latex_quote_ot1[times][quoteind];
242 else if (language == InsetQuotes::FrenchQ
243 && times == InsetQuotes::DoubleQ
244 && doclang == "frenchb") {
245 if (side == InsetQuotes::LeftQ)
251 qstr = latex_quote_babel[times][quoteind];
253 // protect against !` and ?` ligatures.
254 // Is it very bad of us to always protect against those ligatures?
256 if ((suffixIs(file, '?') || suffixIs(file, '!'))
260 // Always guard against unfortunate ligatures (!` ?`)
261 qstr.insert(0, "{}");
269 int InsetQuotes::Linuxdoc(ostream & os) const
276 int InsetQuotes::DocBook(ostream & os) const
278 if(times == InsetQuotes::DoubleQ) {
279 if (side == InsetQuotes::LeftQ)
284 if (side == InsetQuotes::LeftQ)
293 void InsetQuotes::Validate(LaTeXFeatures & features) const
295 char type = quote_char[quote_index[side][language]];
297 if (current_view->buffer()->GetLanguage() == "default"
298 && lyxrc.fontenc != "T1") {
299 if (times == InsetQuotes::SingleQ)
301 case ',': features.quotesinglbase = true; break;
302 case '<': features.guilsinglleft = true; break;
303 case '>': features.guilsinglright = true; break;
308 case ',': features.quotedblbase = true; break;
309 case '<': features.guillemotleft = true; break;
310 case '>': features.guillemotright = true; break;
317 Inset * InsetQuotes::Clone() const
319 return new InsetQuotes(language, side, times);
323 Inset::Code InsetQuotes::LyxCode() const
325 return Inset::QUOTE_CODE;