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