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*/) const
229 #ifdef USE_OSTREAM_ONLY
231 current_view->buffer()->GetLanguage();
232 int quoteind = quote_index[side][language];
235 if (lyxrc->fontenc == "T1") {
236 qstr = latex_quote_t1[times][quoteind];
238 else if (doclang == "default") {
239 qstr = latex_quote_ot1[times][quoteind];
241 else if (language == InsetQuotes::FrenchQ
242 && times == InsetQuotes::DoubleQ
243 && doclang == "frenchb") {
244 if (side == InsetQuotes::LeftQ)
250 qstr = latex_quote_babel[times][quoteind];
252 // protect against !` and ?` ligatures.
253 // Is it very bad of us to always protect against those ligatures?
255 if ((suffixIs(file, '?') || suffixIs(file, '!'))
259 // Always guard against unfortunate ligatures (!` ?`)
260 qstr.insert(0, "{}");
267 int res = Latex(quote, 0);
274 #ifndef USE_OSTREAM_ONLY
275 int InsetQuotes::Latex(string & file, signed char /*fragile*/) const
278 current_view->buffer()->GetLanguage();
279 int quoteind = quote_index[side][language];
282 if (lyxrc->fontenc == "T1") {
283 qstr = latex_quote_t1[times][quoteind];
285 else if (doclang == "default") {
286 qstr = latex_quote_ot1[times][quoteind];
288 else if (language == InsetQuotes::FrenchQ
289 && times == InsetQuotes::DoubleQ
290 && doclang == "frenchb") {
291 if (side == InsetQuotes::LeftQ)
297 qstr = latex_quote_babel[times][quoteind];
299 // protect against !` and ?` ligatures.
300 if ((suffixIs(file, '?') || suffixIs(file, '!'))
309 int InsetQuotes::Linuxdoc(string & file) const
317 int InsetQuotes::DocBook(string & file) const
319 if(times == InsetQuotes::DoubleQ) {
320 if (side == InsetQuotes::LeftQ)
325 if (side == InsetQuotes::LeftQ)
335 int InsetQuotes::Linuxdoc(ostream & os) const
342 int InsetQuotes::DocBook(ostream & os) const
344 if(times == InsetQuotes::DoubleQ) {
345 if (side == InsetQuotes::LeftQ)
350 if (side == InsetQuotes::LeftQ)
360 void InsetQuotes::Validate(LaTeXFeatures & features) const
362 char type = quote_char[quote_index[side][language]];
364 if (current_view->buffer()->GetLanguage() == "default"
365 && lyxrc->fontenc != "T1") {
366 if (times == InsetQuotes::SingleQ)
368 case ',': features.quotesinglbase = true; break;
369 case '<': features.guilsinglleft = true; break;
370 case '>': features.guilsinglright = true; break;
375 case ',': features.quotedblbase = true; break;
376 case '<': features.guillemotleft = true; break;
377 case '>': features.guillemotright = true; break;
384 Inset * InsetQuotes::Clone() const
386 return new InsetQuotes(language, side, times);
390 Inset::Code InsetQuotes::LyxCode() const
392 return Inset::QUOTE_CODE;