]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
update copyright year
[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
29 using std::ostream;
30 using std::endl;
31
32 // Quotes. Used for the various quotes. German, English, French,
33 // Danish, Polish, all either double or single.
34
35 namespace {
36
37 // codes used to read/write quotes to LyX files
38 char const * const language_char = "esgpfa";
39 char const * const side_char = "lr" ;
40 char const * const times_char = "sd";
41
42 // List of known quote chars
43 char const * const quote_char = ",'`<>";
44
45 // Index of chars used for the quote. Index is [side, language]
46 int quote_index[2][6] = {
47         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
48         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
49
50 // Corresponding LaTeX code, for double and single quotes.
51 char const * const latex_quote_t1[2][5] = 
52 { { "\\quotesinglbase{}",  "'", "`", 
53     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
54   { ",,", "''", "``", "<<", ">>" } };
55
56 char const * const latex_quote_ot1[2][5] = 
57 { { "\\quotesinglbase{}",  "'", "`", 
58     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
59   { "\\quotedblbase{}", "''", "``",
60     "\\guillemotleft{}", "\\guillemotright{}" } };
61
62 char const * const latex_quote_babel[2][5] = 
63 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
64   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
65
66 } // namespace anon
67
68
69 InsetQuotes::InsetQuotes(string const & str)
70 {
71         ParseString(str);
72 }
73
74
75 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
76                          InsetQuotes::quote_side s,
77                          InsetQuotes::quote_times t)
78         : language(l), side(s), times(t)
79 {}
80
81
82 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
83         : language(params.quotes_language), times(params.quotes_times)
84 {
85         // Decide whether left or right 
86         switch (c) {
87         case ' ': case '(': case '{': case '[': case '-': case ':':
88         case LyXParagraph::META_HFILL:
89         case LyXParagraph::META_NEWLINE: 
90                 side = InsetQuotes::LeftQ;   // left quote 
91                 break;
92         default:
93                 side = InsetQuotes::RightQ;  // right quote
94         }
95 }
96
97
98 void InsetQuotes::ParseString(string const & s)
99 {
100         string str(s);
101         if (str.length() != 3) {
102                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
103                         " bad string length." << endl;
104                 str = "eld";
105         }
106
107         int i;
108         
109         for (i = 0; i < 6; ++i) {
110                 if (str[0] == language_char[i]) {
111                         language = InsetQuotes::quote_language(i);
112                         break;
113                 }
114         }
115         if (i >= 6) {
116                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
117                         " bad language specification." << endl;
118                 language = InsetQuotes::EnglishQ; 
119         }
120
121         for (i = 0; i < 2; ++i) {
122                 if (str[1] == side_char[i]) {
123                         side = InsetQuotes::quote_side(i);
124                         break;
125                 }
126         }
127         if (i >= 2) {
128                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
129                         " bad side specification." << endl;
130                 side = InsetQuotes::LeftQ; 
131         }
132
133         for (i = 0; i < 2; ++i) {
134                 if (str[2] == times_char[i]) {
135                         times = InsetQuotes::quote_times(i);
136                         break;
137                 }
138         }
139         if (i >= 2) {
140                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
141                         " bad times specification." << endl;
142                 times = InsetQuotes::DoubleQ; 
143         }
144 }
145
146
147 string const InsetQuotes::DispString() const
148 {
149         string disp;
150         disp += quote_char[quote_index[side][language]];
151         if (times == InsetQuotes::DoubleQ)
152                 disp += disp;
153
154         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1)
155                 if (disp == "<<")
156                         disp = '«';
157                 else if (disp == ">>")
158                         disp = '»';
159         
160         return disp;
161 }
162
163
164 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
165 {
166         return lyxfont::maxAscent(font);
167 }
168
169
170 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
171 {
172         return lyxfont::maxDescent(font);
173 }
174
175
176 int InsetQuotes::width(BufferView *, LyXFont const & font) const
177 {
178         string text = DispString();
179         int w = 0;
180
181         for (string::size_type i = 0; i < text.length(); ++i) {
182                 if (text[i] == ' ')
183                         w += lyxfont::width('i', font);
184                 else if (i == 0 || text[i] != text[i-1])
185                         w += lyxfont::width(text[i], font);
186                 else
187                         w += lyxfont::width(',', font);
188         }
189
190         return w;
191 }
192
193
194 LyXFont const InsetQuotes::ConvertFont(LyXFont const & f) const
195 {
196         LyXFont font(f);
197         // quotes-insets cannot be latex of any kind
198         font.setLatex(LyXFont::OFF);
199         return font;
200 }
201
202
203 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
204                        int baseline, float & x, bool) const
205 {
206         string text = DispString();
207
208         bv->painter().text(int(x), baseline, text, font);
209         x += width(bv, font);
210 }
211
212
213 void InsetQuotes::Write(Buffer const *, ostream & os) const
214 {
215         string text;
216         text += language_char[language];
217         text += side_char[side];
218         text += times_char[times]; 
219         os << "Quotes " << text;
220 }
221
222
223 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
224 {
225         lex.nextToken();
226         ParseString(lex.GetString());
227         lex.next();
228         string tmp(lex.GetString());
229         if (tmp != "\\end_inset")
230                 lyxerr << "LyX Warning: Missing \\end_inset "
231                         "in InsetQuotes::Read." << endl;
232 }
233
234
235 extern bool use_babel;
236
237 int InsetQuotes::Latex(Buffer const * buf, ostream & os,
238                        bool /*fragile*/, bool) const
239 {
240         string const doclang = buf->GetLanguage()->lang();
241         int quoteind = quote_index[side][language];
242         string qstr;
243         
244         if (lyxrc.fontenc == "T1") {
245                 qstr = latex_quote_t1[times][quoteind];
246 #ifdef DO_USE_DEFAULT_LANGUAGE
247         } else if (doclang == "default") {
248 #else
249         } else if (!use_babel) {
250 #endif
251                 qstr = latex_quote_ot1[times][quoteind];
252         } else if (language == InsetQuotes::FrenchQ 
253                  && times == InsetQuotes::DoubleQ
254                  && doclang == "frenchb") {
255                 if (side == InsetQuotes::LeftQ) 
256                         qstr = "\\og{}";
257                 else 
258                         qstr = " \\fg{}";
259         } else 
260                 qstr = latex_quote_babel[times][quoteind];
261
262         // Always guard against unfortunate ligatures (!` ?`)
263         if (prefixIs(qstr, "`"))
264                 qstr.insert(0, "{}");
265
266         os << qstr;
267         return 0;
268 }
269
270
271 int InsetQuotes::Ascii(Buffer const *, ostream & os, int) const
272 {
273         os << "\"";
274         return 0;
275 }
276
277
278 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
279 {
280         os << "\"";
281         return 0;
282 }
283
284
285 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
286 {
287         if (times == InsetQuotes::DoubleQ) {
288                 if (side == InsetQuotes::LeftQ)
289                         os << "&ldquo;";
290                 else
291                         os << "&rdquo;";
292         } else {
293                 if (side == InsetQuotes::LeftQ)
294                         os << "&lsquo;";
295                 else
296                         os << "&rsquo;";
297         }
298         return 0;
299 }
300
301
302 void InsetQuotes::Validate(LaTeXFeatures & features) const 
303 {
304         char type = quote_char[quote_index[side][language]];
305
306         if (features.bufferParams().language->lang() == "default" 
307             && lyxrc.fontenc != "T1") {
308                 if (times == InsetQuotes::SingleQ) 
309                         switch (type) {
310                         case ',': features.quotesinglbase = true; break;
311                         case '<': features.guilsinglleft = true; break;
312                         case '>': features.guilsinglright = true; break;
313                         default: break;
314                         }
315                 else 
316                         switch (type) {
317                         case ',': features.quotedblbase = true; break;
318                         case '<': features.guillemotleft = true; break;
319                         case '>': features.guillemotright = true; break;
320                         default: break;
321                         }
322         }
323 }
324
325
326 Inset * InsetQuotes::Clone(Buffer const &) const
327 {
328   return new InsetQuotes(language, side, times);
329 }
330
331
332 Inset::Code InsetQuotes::LyxCode() const
333 {
334   return Inset::QUOTE_CODE;
335 }