]> git.lyx.org Git - lyx.git/blob - src/insets/InsetQuotes.cpp
Further cleanup of collapsable insets. The layouts are now properly read and applied.
[lyx.git] / src / insets / InsetQuotes.cpp
1 /**
2  * \file InsetQuotes.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetQuotes.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "debug.h"
19 #include "Language.h"
20 #include "LaTeXFeatures.h"
21 #include "Lexer.h"
22 #include "LyXRC.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/docstring.h"
30 #include "support/lstrings.h"
31
32
33 namespace lyx {
34
35 using support::prefixIs;
36
37 using std::endl;
38 using std::string;
39 using std::ostream;
40
41
42 namespace {
43
44 /* codes used to read/write quotes to LyX files
45  * e    ``english''
46  * s    ''spanish''
47  * g    ,,german``
48  * p    ,,polish''
49  * f    <<french>>
50  * a    >>danish<<
51  */
52
53 char const * const language_char = "esgpfa";
54 char const * const side_char = "lr" ;
55 char const * const times_char = "sd";
56
57 // List of known quote chars
58 char const * const quote_char = ",'`<>";
59
60 // Index of chars used for the quote. Index is [side, language]
61 int quote_index[2][6] = {
62         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
63         { 1, 1, 2, 1, 4, 3 }     // "`'`'><"
64 };
65
66 // Corresponding LaTeX code, for double and single quotes.
67 char const * const latex_quote_t1[2][5] = {
68         { "\\quotesinglbase ",  "'", "`",
69     "\\guilsinglleft{}", "\\guilsinglright{}" },
70   { ",,", "''", "``", "<<", ">>" }
71 };
72
73 char const * const latex_quote_ot1[2][5] = {
74         { "\\quotesinglbase ",  "'", "`",
75     "\\guilsinglleft{}", "\\guilsinglright{}" },
76   { "\\quotedblbase ", "''", "``",
77     "\\guillemotleft{}", "\\guillemotright{}" }
78 };
79
80 char const * const latex_quote_babel[2][5] = {
81         { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
82   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" }
83 };
84
85 } // namespace anon
86
87
88 InsetQuotes::InsetQuotes(string const & str)
89 {
90         parseString(str);
91 }
92
93
94 InsetQuotes::InsetQuotes(quote_language l, quote_side s, quote_times t)
95         : language_(l), side_(s), times_(t)
96 {
97 }
98
99
100 InsetQuotes::InsetQuotes(char_type c, BufferParams const & params)
101         : language_(params.quotes_language), times_(params.quotes_times)
102 {
103         getPosition(c);
104 }
105
106
107 InsetQuotes::InsetQuotes(char_type c, quote_language l, quote_times t)
108         : language_(l), times_(t)
109 {
110         getPosition(c);
111 }
112
113
114 docstring InsetQuotes::name() const
115 {
116         return from_ascii("Quotes");
117 }
118
119
120 void InsetQuotes::getPosition(char_type c)
121 {
122         // Decide whether left or right
123         switch (c) {
124         case ' ':
125         case '(':
126         case '[':
127                 side_ = LeftQ;   // left quote
128                 break;
129         default:
130                 side_ = RightQ;  // right quote
131         }
132 }
133
134
135 void InsetQuotes::parseString(string const & s)
136 {
137         string str(s);
138         if (str.length() != 3) {
139                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140                         " bad string length." << endl;
141                 str = "eld";
142         }
143
144         int i;
145
146         for (i = 0; i < 6; ++i) {
147                 if (str[0] == language_char[i]) {
148                         language_ = quote_language(i);
149                         break;
150                 }
151         }
152         if (i >= 6) {
153                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
154                         " bad language specification." << endl;
155                 language_ = EnglishQ;
156         }
157
158         for (i = 0; i < 2; ++i) {
159                 if (str[1] == side_char[i]) {
160                         side_ = quote_side(i);
161                         break;
162                 }
163         }
164         if (i >= 2) {
165                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
166                         " bad side specification." << endl;
167                 side_ = LeftQ;
168         }
169
170         for (i = 0; i < 2; ++i) {
171                 if (str[2] == times_char[i]) {
172                         times_ = quote_times(i);
173                         break;
174                 }
175         }
176         if (i >= 2) {
177                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
178                         " bad times specification." << endl;
179                 times_ = DoubleQ;
180         }
181 }
182
183
184 docstring const InsetQuotes::dispString(Language const * loclang) const
185 {
186         string disp;
187         disp += quote_char[quote_index[side_][language_]];
188         if (times_ == DoubleQ)
189                 disp += disp;
190
191
192         docstring retdisp;
193         if (disp == "<<")
194                 retdisp = docstring(1, 0x00ab); //'«';
195         else if (disp == ">>")
196                 retdisp = docstring(1, 0x00bb); //'»';
197 #if 0
198         // The below are supposed to work, but something fails.
199         else if (disp == ",,")
200                 retdisp = docstring(1, 0x201e);
201         else if (disp == "''")
202                 retdisp == docstring(1, 0x201d);
203         else if (disp == "``")
204                 retdisp == docstring(1, 0x201c);
205         else if (disp == "<")
206                 retdisp = docstring(1, 0x2039);
207         else if (disp == ">")
208                 retdisp = docstring(1, 0x203a);
209         else if (disp == ",")
210                 retdisp = docstring(1, 0x201a);
211         else if (disp == "'")
212                 retdisp = docstring(1, 0x2019);
213         else if (disp == "`")
214                 retdisp = docstring(1, 0x2018);
215 #endif
216         else
217                 retdisp = from_ascii(disp);
218
219         // in french, spaces are added inside double quotes
220         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
221                 if (side_ == LeftQ)
222                         retdisp += ' ';
223                 else
224                         retdisp.insert(size_t(0), 1, ' ');
225         }
226
227         return retdisp;
228 }
229
230
231 void InsetQuotes::metrics(MetricsInfo & mi, Dimension & dim) const
232 {
233         FontInfo & font = mi.base.font;
234         frontend::FontMetrics const & fm =
235                 theFontMetrics(font);
236         dim.asc = fm.maxAscent();
237         dim.des = fm.maxDescent();
238         dim.wid = 0;
239
240         // FIXME: should we add a language or a font parameter member?
241         docstring const text = dispString(
242                 mi.base.bv->buffer().params().language);
243         for (string::size_type i = 0; i < text.length(); ++i) {
244                 if (text[i] == ' ')
245                         dim.wid += fm.width('i');
246                 else if (i == 0 || text[i] != text[i - 1])
247                         dim.wid += fm.width(text[i]);
248                 else
249                         dim.wid += fm.width(',');
250         }
251 }
252
253
254 void InsetQuotes::draw(PainterInfo & pi, int x, int y) const
255 {
256         // FIXME: should we add a language or a font parameter member?
257         docstring const text = dispString(
258                 pi.base.bv->buffer().params().language);
259
260         if (text.length() == 2 && text[0] == text[1]) {
261                 pi.pain.text(x, y, text[0], pi.base.font);
262                 int const t = theFontMetrics(pi.base.font)
263                         .width(',');
264                 pi.pain.text(x + t, y, text[0], pi.base.font);
265         } else {
266                 pi.pain.text(x, y, text, pi.base.font);
267         }
268 }
269
270
271 void InsetQuotes::write(Buffer const &, ostream & os) const
272 {
273         string text;
274         text += language_char[language_];
275         text += side_char[side_];
276         text += times_char[times_];
277         os << "Quotes " << text;
278 }
279
280
281 void InsetQuotes::read(Buffer const &, Lexer & lex)
282 {
283         lex.next();
284         parseString(lex.getString());
285         lex.next();
286         if (lex.getString() != "\\end_inset") {
287                 lex.printError("Missing \\end_inset at this point");
288         }
289 }
290
291
292 int InsetQuotes::latex(Buffer const &, odocstream & os,
293                        OutputParams const & runparams) const
294 {
295         const int quoteind = quote_index[side_][language_];
296         string qstr;
297
298         if (language_ == FrenchQ && times_ == DoubleQ
299             && prefixIs(runparams.local_font->language()->code(), "fr")) {
300                 if (side_ == LeftQ)
301                         qstr = "\\og "; //the spaces are important here
302                 else
303                         qstr = " \\fg{}"; //and here
304         } else if (lyxrc.fontenc == "T1") {
305                 qstr = latex_quote_t1[times_][quoteind];
306 #ifdef DO_USE_DEFAULT_LANGUAGE
307         } else if (doclang == "default") {
308 #else
309         } else if (!runparams.use_babel) {
310 #endif
311                 qstr = latex_quote_ot1[times_][quoteind];
312         } else {
313                 qstr = latex_quote_babel[times_][quoteind];
314         }
315
316         // Always guard against unfortunate ligatures (!` ?`)
317         if (prefixIs(qstr, "`"))
318                 qstr.insert(0, "{}");
319
320         os << from_ascii(qstr);
321         return 0;
322 }
323
324
325 int InsetQuotes::plaintext(Buffer const & buf, odocstream & os,
326                            OutputParams const &) const
327 {
328         docstring const str = dispString(buf.params().language);
329         os << str;
330         return str.size();
331 }
332
333
334 int InsetQuotes::docbook(Buffer const &, odocstream & os,
335                          OutputParams const &) const
336 {
337         if (times_ == DoubleQ) {
338                 if (side_ == LeftQ)
339                         os << "&ldquo;";
340                 else
341                         os << "&rdquo;";
342         } else {
343                 if (side_ == LeftQ)
344                         os << "&lsquo;";
345                 else
346                         os << "&rsquo;";
347         }
348         return 0;
349 }
350
351
352 void InsetQuotes::textString(Buffer const & buf, odocstream & os) const
353 {
354         os << dispString(buf.params().language);
355 }
356
357
358 void InsetQuotes::validate(LaTeXFeatures & features) const
359 {
360         bool const use_babel = features.useBabel();
361         char type = quote_char[quote_index[side_][language_]];
362
363 #ifdef DO_USE_DEFAULT_LANGUAGE
364         if (features.bufferParams().language->lang() == "default"
365 #else
366         if (!use_babel
367 #endif
368             && lyxrc.fontenc != "T1") {
369                 if (times_ == SingleQ)
370                         switch (type) {
371                         case ',': features.require("quotesinglbase"); break;
372                         case '<': features.require("guilsinglleft");  break;
373                         case '>': features.require("guilsinglright"); break;
374                         default: break;
375                         }
376                 else
377                         switch (type) {
378                         case ',': features.require("quotedblbase");   break;
379                         case '<': features.require("guillemotleft");  break;
380                         case '>': features.require("guillemotright"); break;
381                         default: break;
382                         }
383         }
384 }
385
386
387 Inset * InsetQuotes::clone() const
388 {
389         return new InsetQuotes(language_, side_, times_);
390 }
391
392
393 } // namespace lyx