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