]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
beb5887d0f86b3c6120552c7336b394aa93e9f9c
[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 #include "support/lyxlib.h"
19 #include "debug.h"
20 #include "lyxfont.h"
21 #include "lyxrc.h"
22 #include "buffer.h"
23 #include "LaTeXFeatures.h"
24 #include "support/lstrings.h"
25 #include "Painter.h"
26 #include "font.h"
27 #include "language.h"
28 #include "BufferView.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                 if (disp == "'")
158                         disp = "´";
159                 else if (disp == "''")
160                         disp = "´´";
161         }
162         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1
163             || lyxrc.font_norm_type == LyXRC::ISO_8859_9
164             || lyxrc.font_norm_type == LyXRC::ISO_8859_15) {
165                 if (disp == "<<")
166                         disp = '«';
167                 else if (disp == ">>")
168                         disp = '»';
169         }
170
171         // in french, spaces are added inside double quotes
172         if (times_ == DoubleQ && prefixIs(loclang->code(), "fr")) {
173                 if (side_ == LeftQ)
174                         disp += " ";
175                 else
176                         disp = " " + disp;
177         }
178         
179         return disp;
180 }
181
182
183 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
184 {
185         return lyxfont::maxAscent(font);
186 }
187
188
189 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
190 {
191         return lyxfont::maxDescent(font);
192 }
193
194
195 int InsetQuotes::width(BufferView *, LyXFont const & font) const
196 {
197         string const text = dispString(font.language());
198         int w = 0;
199
200         for (string::size_type i = 0; i < text.length(); ++i) {
201                 if (text[i] == ' ')
202                         w += lyxfont::width('i', font);
203                 else if (i == 0 || text[i] != text[i-1])
204                         w += lyxfont::width(text[i], font);
205                 else
206                         w += lyxfont::width(',', font);
207         }
208
209         return w;
210 }
211
212
213 #if 0
214 LyXFont const InsetQuotes::convertFont(LyXFont const & f) const
215 {
216 #if 1
217         return f;
218 #else
219         LyXFont font(f);
220         return font;
221 #endif
222 }
223 #endif
224
225
226 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
227                        int baseline, float & x, bool) const
228 {
229         string const text = dispString(font.language());
230
231         bv->painter().text(int(x), baseline, text, font);
232         x += width(bv, font);
233 }
234
235
236 void InsetQuotes::write(Buffer const *, ostream & os) const
237 {
238         string text;
239         text += language_char[language_];
240         text += side_char[side_];
241         text += times_char[times_]; 
242         os << "Quotes " << text;
243 }
244
245
246 void InsetQuotes::read(Buffer const *, LyXLex & lex)
247 {
248         lex.nextToken();
249         parseString(lex.getString());
250         lex.next();
251         if (lex.getString() != "\\end_inset") {
252                 lex.printError("Missing \\end_inset at this point");
253         }
254 }
255
256
257 extern bool use_babel;
258
259 int InsetQuotes::latex(Buffer const * buf, ostream & os,
260                        bool /*fragile*/, bool) const
261 {
262         // How do we get the local language here??
263
264         string const doclang = buf->getLanguage()->babel();
265         const int quoteind = quote_index[side_][language_];
266         string qstr;
267         
268         if (language_ == FrenchQ && times_ == DoubleQ
269             && doclang == "frenchb") {
270                 if (side_ == LeftQ) 
271                         qstr = "\\og "; //the spaces are important here
272                 else 
273                         qstr = " \\fg{}"; //and here
274         } else if (language_ == FrenchQ && times_ == DoubleQ
275                    && doclang == "french") {
276                 if (side_ == LeftQ) 
277                         qstr = "<< "; //the spaces are important here
278                 else 
279                         qstr = " >>"; //and here
280         } else if (lyxrc.fontenc == "T1") {
281                 qstr = latex_quote_t1[times_][quoteind];
282 #ifdef DO_USE_DEFAULT_LANGUAGE
283         } else if (doclang == "default") {
284 #else
285         } else if (!use_babel) {
286 #endif
287                 qstr = latex_quote_ot1[times_][quoteind];
288         } else { 
289                 qstr = latex_quote_babel[times_][quoteind];
290         }
291
292         // Always guard against unfortunate ligatures (!` ?`)
293         if (prefixIs(qstr, "`"))
294                 qstr.insert(0, "{}");
295
296         os << qstr;
297         return 0;
298 }
299
300
301 int InsetQuotes::ascii(Buffer const *, ostream & os, int) const
302 {
303         os << "\"";
304         return 0;
305 }
306
307
308 int InsetQuotes::linuxdoc(Buffer const *, ostream & os) const
309 {
310         os << "\"";
311         return 0;
312 }
313
314
315 int InsetQuotes::docbook(Buffer const *, ostream & os) const
316 {
317         if (times_ == DoubleQ) {
318                 if (side_ == LeftQ)
319                         os << "&ldquo;";
320                 else
321                         os << "&rdquo;";
322         } else {
323                 if (side_ == LeftQ)
324                         os << "&lsquo;";
325                 else
326                         os << "&rsquo;";
327         }
328         return 0;
329 }
330
331
332 void InsetQuotes::validate(LaTeXFeatures & features) const 
333 {
334         char type = quote_char[quote_index[side_][language_]];
335
336 #ifdef DO_USE_DEFAULT_LANGUAGE
337         if (features.bufferParams().language->lang() == "default" 
338 #else
339         if (!use_babel
340 #endif
341             && lyxrc.fontenc != "T1") {
342                 if (times_ == SingleQ) 
343                         switch (type) {
344                         case ',': features.quotesinglbase = true; break;
345                         case '<': features.guilsinglleft = true; break;
346                         case '>': features.guilsinglright = true; break;
347                         default: break;
348                         }
349                 else 
350                         switch (type) {
351                         case ',': features.quotedblbase = true; break;
352                         case '<': features.guillemotleft = true; break;
353                         case '>': features.guillemotright = true; break;
354                         default: break;
355                         }
356         }
357 }
358
359
360 Inset * InsetQuotes::clone(Buffer const &, bool) const
361 {
362   return new InsetQuotes(language_, side_, times_);
363 }
364
365
366 Inset::Code InsetQuotes::lyxCode() const
367 {
368   return Inset::QUOTE_CODE;
369 }