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