]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
Collapse all those LFUN_XYZ_APPLY to a single LFUN_INSET_APPLY.
[lyx.git] / src / insets / insetquotes.C
1 /**
2  * \file insetquotes.C
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
14 #include "insetquotes.h"
15
16 #include "support/LAssert.h"
17 #include "support/lstrings.h"
18 #include "BufferView.h"
19 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
21 #include "buffer.h"
22 #include "debug.h"
23 #include "frontends/font_metrics.h"
24 #include "language.h"
25 #include "lyxfont.h"
26 #include "lyxrc.h"
27 #include "paragraph.h"
28 #include "lyxlex.h"
29
30 using std::ostream;
31 using std::endl;
32
33 // Quotes. Used for the various quotes. German, English, French,
34 // Danish, Polish, all either double or single.
35
36 namespace {
37
38 // codes used to read/write quotes to LyX files
39 char const * const language_char = "esgpfa";
40 char const * const side_char = "lr" ;
41 char const * const times_char = "sd";
42
43 // List of known quote chars
44 char const * const quote_char = ",'`<>";
45
46 // Index of chars used for the quote. Index is [side, language]
47 int quote_index[2][6] = {
48         { 2, 1, 0, 0, 3, 4 },    // "'',,<>"
49         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
50
51 // Corresponding LaTeX code, for double and single quotes.
52 char const * const latex_quote_t1[2][5] =
53 { { "\\quotesinglbase ",  "'", "`",
54     "\\guilsinglleft{}", "\\guilsinglright{}" },
55   { ",,", "''", "``", "<<", ">>" } };
56
57 char const * const latex_quote_ot1[2][5] =
58 { { "\\quotesinglbase ",  "'", "`",
59     "\\guilsinglleft{}", "\\guilsinglright{}" },
60   { "\\quotedblbase ", "''", "``",
61     "\\guillemotleft{}", "\\guillemotright{}" } };
62
63 char const * const latex_quote_babel[2][5] =
64 { { "\\glq ",  "'", "`", "\\flq{}", "\\frq{}" },
65   { "\\glqq ", "''", "``", "\\flqq{}", "\\frqq{}" } };
66
67 } // namespace anon
68
69
70 InsetQuotes::InsetQuotes(string const & str)
71 {
72         parseString(str);
73 }
74
75
76 InsetQuotes::InsetQuotes(quote_language l,
77                          quote_side s,
78                          quote_times t)
79         : language_(l), side_(s), times_(t)
80 {}
81
82
83 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
84         : language_(params.quotes_language), times_(params.quotes_times)
85 {
86         // Decide whether left or right
87         switch (c) {
88         case ' ': case '(':
89         case Paragraph::META_HFILL:
90         case Paragraph::META_NEWLINE:
91                 side_ = LeftQ;   // left quote
92                 break;
93         default:
94                 side_ = RightQ;  // right quote
95         }
96 }
97
98
99 void InsetQuotes::parseString(string const & s)
100 {
101         string str(s);
102         if (str.length() != 3) {
103                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
104                         " bad string length." << endl;
105                 str = "eld";
106         }
107
108         int i;
109
110         for (i = 0; i < 6; ++i) {
111                 if (str[0] == language_char[i]) {
112                         language_ = quote_language(i);
113                         break;
114                 }
115         }
116         if (i >= 6) {
117                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
118                         " bad language specification." << endl;
119                 language_ = EnglishQ;
120         }
121
122         for (i = 0; i < 2; ++i) {
123                 if (str[1] == side_char[i]) {
124                         side_ = quote_side(i);
125                         break;
126                 }
127         }
128         if (i >= 2) {
129                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
130                         " bad side specification." << endl;
131                 side_ = LeftQ;
132         }
133
134         for (i = 0; i < 2; ++i) {
135                 if (str[2] == times_char[i]) {
136                         times_ = quote_times(i);
137                         break;
138                 }
139         }
140         if (i >= 2) {
141                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
142                         " bad times specification." << endl;
143                 times_ = DoubleQ;
144         }
145 }
146
147
148 string const InsetQuotes::dispString(Language const * loclang) const
149 {
150         string disp;
151         disp += quote_char[quote_index[side_][language_]];
152         if (times_ == DoubleQ)
153                 disp += disp;
154
155         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
156             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
157             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
158                 if (disp == "<<")
159                         disp = '«';
160                 else if (disp == ">>")
161                         disp = '»';
162         }
163
164         // in french, spaces are added inside double quotes
165         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
166                 if (side_ == LeftQ)
167                         disp += ' ';
168                 else
169                         disp.insert(string::size_type(0), 1, ' ');
170         }
171
172         return disp;
173 }
174
175
176 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
177 {
178         return font_metrics::maxAscent(font);
179 }
180
181
182 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
183 {
184         return font_metrics::maxDescent(font);
185 }
186
187
188 int InsetQuotes::width(BufferView *, LyXFont const & font) const
189 {
190         string const text = dispString(font.language());
191         int w = 0;
192
193         for (string::size_type i = 0; i < text.length(); ++i) {
194                 if (text[i] == ' ')
195                         w += font_metrics::width('i', font);
196                 else if (i == 0 || text[i] != text[i - 1])
197                         w += font_metrics::width(text[i], font);
198                 else
199                         w += font_metrics::width(',', font);
200         }
201
202         return w;
203 }
204
205
206 #if 0
207 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
208 {
209 #if 1
210         return f;
211 #else
212         LyXFont font(f);
213         return font;
214 #endif
215 }
216 #endif
217
218
219 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
220                        int baseline, float & x, bool) const
221 {
222         string const text = dispString(font.language());
223
224         if (text.length() == 2 && text[0] == text[1]) {
225                 bv->painter().text(int(x), baseline, text[0], font);
226                 int x2 = int(x + font_metrics::width(',', font));
227                 bv->painter().text(x2, baseline, text[0], font);
228         } else
229                 bv->painter().text(int(x), baseline, text, font);
230         x += width(bv, font);
231 }
232
233
234 void InsetQuotes::write(Buffer const *, ostream & os) const
235 {
236         string text;
237         text += language_char[language_];
238         text += side_char[side_];
239         text += times_char[times_];
240         os << "Quotes " << text;
241 }
242
243
244 void InsetQuotes::read(Buffer const *, LyXLex & lex)
245 {
246         lex.nextToken();
247         parseString(lex.getString());
248         lex.next();
249         if (lex.getString() != "\\end_inset") {
250                 lex.printError("Missing \\end_inset at this point");
251         }
252 }
253
254
255 extern bool use_babel;
256
257 int InsetQuotes::latex(Buffer const * buf, ostream & os,
258                        bool /*fragile*/, bool /* free_spc */) const
259 {
260         // How do we get the local language here??
261         lyx::pos_type curr_pos = parOwner()->getPositionOfInset(this);
262         lyx::Assert(curr_pos != -1);
263         string const curr_lang =
264                 parOwner()->getFont(buf->params,
265                                     curr_pos).language()->babel();
266
267         const int quoteind = quote_index[side_][language_];
268         string qstr;
269
270         if (language_ == FrenchQ && times_ == DoubleQ
271             && curr_lang == "frenchb") {
272                 if (side_ == LeftQ)
273                         qstr = "\\og "; //the spaces are important here
274                 else
275                         qstr = " \\fg{}"; //and here
276         } else if (language_ == FrenchQ && times_ == DoubleQ
277                    && curr_lang == "french") {
278                 if (side_ == LeftQ)
279                         qstr = "<< "; //the spaces are important here
280                 else
281                         qstr = " >>"; //and here
282         } else if (lyxrc.fontenc == "T1") {
283                 qstr = latex_quote_t1[times_][quoteind];
284 #ifdef DO_USE_DEFAULT_LANGUAGE
285         } else if (doclang == "default") {
286 #else
287         } else if (!use_babel) {
288 #endif
289                 qstr = latex_quote_ot1[times_][quoteind];
290         } else {
291                 qstr = latex_quote_babel[times_][quoteind];
292         }
293
294         // Always guard against unfortunate ligatures (!` ?`)
295         if (prefixIs(qstr, "`"))
296                 qstr.insert(0, "{}");
297
298         os << qstr;
299         return 0;
300 }
301
302
303 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
304 {
305         os << '"';
306         return 0;
307 }
308
309
310 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
311 {
312         os << '"';
313         return 0;
314 }
315
316
317 int InsetQuotes::docbook(Buffer const *, ostream & os, bool) const
318 {
319         if (times_ == DoubleQ) {
320                 if (side_ == LeftQ)
321                         os << "&ldquo;";
322                 else
323                         os << "&rdquo;";
324         } else {
325                 if (side_ == LeftQ)
326                         os << "&lsquo;";
327                 else
328                         os << "&rsquo;";
329         }
330         return 0;
331 }
332
333
334 void InsetQuotes::validate(LaTeXFeatures & features) const
335 {
336         char type = quote_char[quote_index[side_][language_]];
337
338 #ifdef DO_USE_DEFAULT_LANGUAGE
339         if (features.bufferParams().language->lang() == "default"
340 #else
341         if (!use_babel
342 #endif
343             && lyxrc.fontenc != "T1") {
344                 if (times_ == SingleQ)
345                         switch (type) {
346                         case ',': features.require("quotesinglbase");  break;
347                         case '<': features.require("guilsinglleft");  break;
348                         case '>': features.require("guilsinglright"); break;
349                         default: break;
350                         }
351                 else
352                         switch (type) {
353                         case ',': features.require("quotedblbase");   break;
354                         case '<': features.require("guillemotleft");  break;
355                         case '>': features.require("guillemotright"); break;
356                         default: break;
357                         }
358         }
359 }
360
361
362 Inset * InsetQuotes::clone(Buffer const &, bool) const
363 {
364   return new InsetQuotes(language_, side_, times_);
365 }
366
367
368 Inset::Code InsetQuotes::lyxCode() const
369 {
370   return Inset::QUOTE_CODE;
371 }