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