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