]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
364524f655a379da6bbc684291870a5553c85fda
[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
27 using std::endl;
28
29 // Quotes. Used for the various quotes. German, English, French,
30 // Danish, Polish, all either double or single.
31
32 extern BufferView * current_view;
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
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 '(': case '{': case '[': case '-': case ':':
84         case LyXParagraph::META_HFILL:
85 #warning think about this
86 #if 0
87         case LyXParagraph::META_PROTECTED_SEPARATOR:
88 #endif
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 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 == "iso8859-1") 
155                 if (disp == "<<")
156                         disp = '«';
157                 else if (disp == ">>")
158                         disp = '»';
159         
160         return disp;
161 }
162
163
164 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
165 {
166         return font.maxAscent();
167 }
168
169
170 int InsetQuotes::descent(Painter &, LyXFont const & font) const
171 {
172         return font.maxDescent();
173 }
174
175
176 int InsetQuotes::width(Painter &, 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 += font.width('i');
184                 else if (i == 0 || text[i] != text[i-1])
185                         w += font.width(text[i]);
186                 else
187                         w += font.width(',');
188         }
189
190         return w;
191 }
192
193
194 LyXFont InsetQuotes::ConvertFont(LyXFont font)
195 {
196         /* quotes-insets cannot be latex of any kind */
197         font.setLatex(LyXFont::OFF);
198         return font;
199 }
200
201
202 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
203                        int baseline, float & x) const
204 {
205         string text = DispString();
206
207         pain.text(int(x), baseline, text, font);
208         x += width(pain, font);
209 }
210
211
212 void InsetQuotes::Write(ostream & os) const
213 {
214         string text;
215         text += language_char[language];
216         text += side_char[side];
217         text += times_char[times]; 
218         os << "Quotes " << text;
219 }
220
221
222 void InsetQuotes::Read(LyXLex & lex)
223 {
224         lex.nextToken();
225         ParseString(lex.GetString());
226 }
227
228
229 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/, bool) const
230 {
231         string doclang = 
232                 current_view->buffer()->GetLanguage();
233         int quoteind = quote_index[side][language];
234         string qstr;
235         
236         if (lyxrc.fontenc == "T1") {
237                 qstr = latex_quote_t1[times][quoteind];
238         }
239         else if (doclang == "default") {
240                 qstr = latex_quote_ot1[times][quoteind];
241         } 
242         else if (language == InsetQuotes::FrenchQ 
243                  && times == InsetQuotes::DoubleQ
244                  && doclang == "frenchb") {
245                 if (side == InsetQuotes::LeftQ) 
246                         qstr = "\\og{}";
247                 else 
248                         qstr = " \\fg{}";
249         } 
250         else 
251                 qstr = latex_quote_babel[times][quoteind];
252
253         // protect against !` and ?` ligatures.
254         // Is it very bad of us to always protect against those ligatures?
255 #if 0
256         if ((suffixIs(file, '?') || suffixIs(file, '!'))
257             && qstr[0] == '`')
258                 qstr = "{}" + qstr;
259 #else
260         // Always guard against unfortunate ligatures (!` ?`)
261         qstr.insert(0, "{}");
262 #endif
263
264         os << qstr;
265         return 0;
266 }
267
268
269 int InsetQuotes::Linuxdoc(ostream & os) const
270 {
271         os << "\"";
272         return 0;
273 }
274
275
276 int InsetQuotes::DocBook(ostream & os) const
277 {
278         if(times == InsetQuotes::DoubleQ) {
279                 if (side == InsetQuotes::LeftQ)
280                         os << "&ldquo;";
281                 else
282                         os << "&rdquo;";
283         } else {
284                 if (side == InsetQuotes::LeftQ)
285                         os << "&lsquo;";
286                 else
287                         os << "&rsquo;";
288         }
289         return 0;
290 }
291
292
293 void InsetQuotes::Validate(LaTeXFeatures & features) const 
294 {
295         char type = quote_char[quote_index[side][language]];
296
297         if (current_view->buffer()->GetLanguage() == "default" 
298             && lyxrc.fontenc != "T1") {
299                 if (times == InsetQuotes::SingleQ) 
300                         switch (type) {
301                         case ',': features.quotesinglbase = true; break;
302                         case '<': features.guilsinglleft = true; break;
303                         case '>': features.guilsinglright = true; break;
304                         default: break;
305                         }
306                 else 
307                         switch (type) {
308                         case ',': features.quotedblbase = true; break;
309                         case '<': features.guillemotleft = true; break;
310                         case '>': features.guillemotright = true; break;
311                         default: break;
312                         }
313         }
314 }
315
316
317 Inset * InsetQuotes::Clone() const
318 {
319   return new InsetQuotes(language, side, times);
320 }
321
322
323 Inset::Code InsetQuotes::LyxCode() const
324 {
325   return Inset::QUOTE_CODE;
326 }