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