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"
26 // Quotes. Used for the various quotes. German, English, French,
27 // 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 case LyXParagraph::META_PROTECTED_SEPARATOR:
84 case LyXParagraph::META_NEWLINE:
85 side = InsetQuotes::LeftQ; // left quote
88 side = InsetQuotes::RightQ; // right quote
93 void InsetQuotes::ParseString(string const & s)
97 if (str.length() != 3) {
98 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
99 " bad string length." << endl;
103 for (i = 0; i < 6; ++i) {
104 if (str[0] == language_char[i]) {
105 language = (InsetQuotes::quote_language)i;
110 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
111 " bad language specification." << endl;
112 language = InsetQuotes::EnglishQ;
115 for (i = 0; i < 2; ++i) {
116 if (str[1] == side_char[i]) {
117 side = (InsetQuotes::quote_side)i;
122 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
123 " bad side specification." << endl;
124 side = InsetQuotes::LeftQ;
127 for (i = 0; i < 2; ++i) {
128 if (str[2] == times_char[i]) {
129 times = (InsetQuotes::quote_times)i;
134 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
135 " bad times specification." << endl;
136 times = InsetQuotes::DoubleQ;
141 string InsetQuotes::DispString() const
144 disp += quote_char[quote_index[side][language]];
145 if (times == InsetQuotes::DoubleQ)
148 if (lyxrc->font_norm == "iso8859-1")
151 else if (disp == ">>")
158 int InsetQuotes::Ascent(LyXFont const & font) const
160 return font.maxAscent();
164 int InsetQuotes::Descent(LyXFont const & font) const
166 return font.maxDescent();
170 int InsetQuotes::Width(LyXFont const & font) const
172 string text = DispString();
175 for (string::size_type i = 0; i < text.length(); ++i) {
177 w += font.width('i');
178 else if (i == 0 || text[i] != text[i-1])
179 w += font.width(text[i]);
181 w += font.width(',');
188 LyXFont InsetQuotes::ConvertFont(LyXFont font)
190 /* quotes-insets cannot be latex of any kind */
191 font.setLatex(LyXFont::OFF);
196 void InsetQuotes::Draw(LyXFont font, LyXScreen & scr,
197 int baseline, float & x)
199 string text = DispString();
201 scr.drawString(font, text, baseline, int(x));
206 void InsetQuotes::Write(ostream & os)
209 text += language_char[language];
210 text += side_char[side];
211 text += times_char[times];
212 os << "Quotes " << text;
216 void InsetQuotes::Read(LyXLex & lex)
219 ParseString(lex.GetString());
223 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/)
226 int res = Latex(quote, 0);
232 int InsetQuotes::Latex(string & file, signed char /*fragile*/)
235 current_view->buffer()->GetLanguage();
236 int quoteind = quote_index[side][language];
239 if (lyxrc->fontenc == "T1") {
240 qstr = latex_quote_t1[times][quoteind];
242 else if (doclang == "default") {
243 qstr = latex_quote_ot1[times][quoteind];
245 else if (language == InsetQuotes::FrenchQ
246 && times == InsetQuotes::DoubleQ
247 && doclang == "frenchb") {
248 if (side == InsetQuotes::LeftQ)
254 qstr = latex_quote_babel[times][quoteind];
256 // protect against !` and ?` ligatures.
257 if ((suffixIs(file, '?') || suffixIs(file, '!'))
266 int InsetQuotes::Linuxdoc(string & file)
274 int InsetQuotes::DocBook(string & file)
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;