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 #warning think about this
86 case LyXParagraph::META_PROTECTED_SEPARATOR:
88 case LyXParagraph::META_NEWLINE:
89 side = InsetQuotes::LeftQ; // left quote
92 side = InsetQuotes::RightQ; // right quote
97 void InsetQuotes::ParseString(string const & s)
101 if (str.length() != 3) {
102 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
103 " 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 font.maxAscent();
168 int InsetQuotes::descent(Painter &, LyXFont const & font) const
170 return font.maxDescent();
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 += font.width('i');
182 else if (i == 0 || text[i] != text[i-1])
183 w += font.width(text[i]);
185 w += font.width(',');
192 LyXFont InsetQuotes::ConvertFont(LyXFont font)
194 /* quotes-insets cannot be latex of any kind */
195 font.setLatex(LyXFont::OFF);
200 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
201 int baseline, float & x) const
203 string text = DispString();
205 pain.text(int(x), baseline, text, font);
206 x += width(pain, font);
210 void InsetQuotes::Write(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(LyXLex & lex)
223 ParseString(lex.GetString());
227 int InsetQuotes::Latex(ostream & os, signed char /*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)
249 qstr = latex_quote_babel[times][quoteind];
251 // protect against !` and ?` ligatures.
252 // Is it very bad of us to always protect against those ligatures?
254 if ((suffixIs(file, '?') || suffixIs(file, '!'))
258 // Always guard against unfortunate ligatures (!` ?`)
259 qstr.insert(0, "{}");
267 int InsetQuotes::Linuxdoc(ostream & os) const
274 int InsetQuotes::DocBook(ostream & os) const
276 if(times == InsetQuotes::DoubleQ) {
277 if (side == InsetQuotes::LeftQ)
282 if (side == InsetQuotes::LeftQ)
291 void InsetQuotes::Validate(LaTeXFeatures & features) const
293 char type = quote_char[quote_index[side][language]];
295 if (current_view->buffer()->GetLanguage() == "default"
296 && lyxrc->fontenc != "T1") {
297 if (times == InsetQuotes::SingleQ)
299 case ',': features.quotesinglbase = true; break;
300 case '<': features.guilsinglleft = true; break;
301 case '>': features.guilsinglright = true; break;
306 case ',': features.quotedblbase = true; break;
307 case '<': features.guillemotleft = true; break;
308 case '>': features.guillemotright = true; break;
315 Inset * InsetQuotes::Clone() const
317 return new InsetQuotes(language, side, times);
321 Inset::Code InsetQuotes::LyxCode() const
323 return Inset::QUOTE_CODE;