1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 *======================================================*/
14 #pragma implementation
17 #include "insetquotes.h"
23 #include "LaTeXFeatures.h"
25 // $Id: insetquotes.C,v 1.1 1999/09/27 18:44:39 larsbj Exp $
27 #if !defined(lint) && !defined(WITH_WARNINGS)
28 static char vcid[] = "$Id: insetquotes.C,v 1.1 1999/09/27 18:44:39 larsbj Exp $";
31 // Quotes. Used for the various quotes. German, English, French,
32 // Danish, Polish, all either double or single.
35 extern BufferView *current_view;
37 // codes used to read/write quotes to LyX files
38 static char const *language_char = "esgpfa";
39 static char const *side_char = "lr" ;
40 static char const *times_char ="sd";
42 // List of known quote chars
43 static char const *quote_char = ",'`<>";
45 // Index of chars used for the quote. Index is [side,language]
46 int quote_index[2][6] =
47 { { 2, 1, 0, 0, 3, 4 }, // "'',,<>"
48 { 1, 1, 2, 1, 4, 3 } }; // "`'`'><"
50 // Corresponding LaTeX code, for double and single quotes.
51 static char const *latex_quote_t1[2][5] =
52 { { "\\quotesinglbase{}", "'", "`",
53 "\\guilsinglleft{}", "\\guilsinglright{}" },
54 { ",,", "''", "``", "<<", ">>" } };
56 static char const *latex_quote_ot1[2][5] =
57 { { "\\quotesinglbase{}", "'", "`",
58 "\\guilsinglleft{}", "\\guilsinglright{}" },
59 { "\\quotedblbase{}", "''", "``",
60 "\\guillemotleft{}", "\\guillemotright{}" } };
62 static char const *latex_quote_babel[2][5] =
63 { { "\\glq{}", "'", "`", "\\flq{}", "\\frq{}" },
64 { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
67 InsetQuotes::InsetQuotes(LString const &string)
72 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
73 InsetQuotes::quote_side s,
74 InsetQuotes::quote_times t)
75 : language(l), side(s), times(t)
80 InsetQuotes::InsetQuotes(char c, BufferParams const ¶ms)
81 : language(params.quotes_language), times(params.quotes_times)
83 // Decide whether left or right
85 case ' ': case '(': case '{': case '[': case '-': case ':':
86 case LYX_META_HFILL: case LYX_META_PROTECTED_SEPARATOR:
87 case LYX_META_NEWLINE:
88 side = InsetQuotes::LeftQ; // left quote
91 side = InsetQuotes::RightQ; // right quote
96 void InsetQuotes::ParseString(LString string)
99 if (string.length() != 3) {
100 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
101 " bad string length.");
106 if (string[0] == language_char[i]) {
107 language = (InsetQuotes::quote_language)i;
112 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
113 " bad language specification.");
114 language = InsetQuotes::EnglishQ;
118 if (string[1] == side_char[i]) {
119 side = (InsetQuotes::quote_side)i;
124 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
125 " bad side specification.");
126 side = InsetQuotes::LeftQ;
130 if (string[2] == times_char[i]) {
131 times = (InsetQuotes::quote_times)i;
136 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
137 " bad times specification.");
138 times = InsetQuotes::DoubleQ;
142 LString InsetQuotes::DispString() const
146 disp += quote_char[quote_index[side][language]];
148 if (times == InsetQuotes::DoubleQ)
151 if (lyxrc->font_norm == "iso8859-1")
154 else if (disp == ">>")
161 int InsetQuotes::Ascent(LyXFont const &font) const
163 return font.maxAscent();
167 int InsetQuotes::Descent(LyXFont const &font) const
169 return font.maxDescent();
173 int InsetQuotes::Width(LyXFont const &font) const
175 LString text = DispString();
178 for (int i = 0; i < text.length(); i++) {
180 w += font.width('i');
181 else if (i == 0 || text[i] != text[i-1])
182 w += font.width(text[i]);
184 w += font.width(',');
191 LyXFont InsetQuotes::ConvertFont(LyXFont font)
193 /* quotes-insets cannot be latex of any kind */
194 font.setLatex(LyXFont::OFF);
199 void InsetQuotes::Draw(LyXFont font, LyXScreen &scr,
200 int baseline, float &x)
202 LString text = DispString();
204 for (int i = 0; i < text.length(); i++) {
206 x += font.width('i');
207 else if (i == text.length()-1 || text[i] != text[i+1]) {
208 scr.drawString(font, &text[i], baseline, int(x));
209 x += font.width(text[i]);
211 scr.drawString(font, &text[i+1], baseline, int(x));
212 x += font.width(',');
219 void InsetQuotes::Write(FILE *file)
221 LString text = LString(language_char[language]) + side_char[side] +
223 fprintf(file, "Quotes %s", text.c_str());
227 void InsetQuotes::Read(LyXLex &lex)
230 ParseString(lex.GetString());
234 int InsetQuotes::Latex(FILE *file, signed char /*fragile*/)
237 int res = Latex(quote, 0);
238 fprintf(file, "%s", quote.c_str());
242 int InsetQuotes::Latex(LString &file, signed char /*fragile*/)
245 current_view->currentBuffer()->GetLanguage();
246 int quoteind = quote_index[side][language];
249 if (lyxrc->fontenc == "T1") {
250 qstr = latex_quote_t1[times][quoteind];
252 else if (doclang == "default") {
253 qstr = latex_quote_ot1[times][quoteind];
255 else if (language == InsetQuotes::FrenchQ
256 && times == InsetQuotes::DoubleQ
257 && doclang == "frenchb") {
258 if (side == InsetQuotes::LeftQ)
264 qstr = latex_quote_babel[times][quoteind];
266 // protect against !` and ?` ligatures.
267 if ((file.suffixIs('?') || file.suffixIs('!'))
276 int InsetQuotes::Linuxdoc(LString &file)
284 int InsetQuotes::DocBook(LString &file)
286 if(times == InsetQuotes::DoubleQ) {
287 if (side == InsetQuotes::LeftQ)
292 if (side == InsetQuotes::LeftQ)
301 void InsetQuotes::Validate(LaTeXFeatures &features) const
303 char type = quote_char[quote_index[side][language]];
305 if (current_view->currentBuffer()->GetLanguage() == "default"
306 && lyxrc->fontenc != "T1") {
307 if (times == InsetQuotes::SingleQ)
309 case ',': features.quotesinglbase = true; break;
310 case '<': features.guilsinglleft = true; break;
311 case '>': features.guilsinglright = true; break;
316 case ',': features.quotedblbase = true; break;
317 case '<': features.guillemotleft = true; break;
318 case '>': features.guillemotright = true; break;
324 Inset* InsetQuotes::Clone()
326 return new InsetQuotes(language, side, times);
330 Inset::Code InsetQuotes::LyxCode() const
332 return Inset::QUOTE_CODE;