]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
fix the outstanding painter drawing problems, now tablelines, mathframe, specialchars...
[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-1999 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 // Quotes. Used for the various quotes. German, English, French,
28 // Danish, Polish, all either double or single.
29
30 extern LyXRC * lyxrc;
31 extern BufferView * current_view;
32
33 // codes used to read/write quotes to LyX files
34 static char const * const language_char = "esgpfa";
35 static char const * const side_char = "lr" ;
36 static char const * const times_char = "sd";
37
38 // List of known quote chars
39 static char const * const quote_char = ",'`<>";
40
41 // Index of chars used for the quote. Index is [side, language]
42 int quote_index[2][6] = {
43         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
44         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
45
46 // Corresponding LaTeX code, for double and single quotes.
47 static char const * const latex_quote_t1[2][5] = 
48 { { "\\quotesinglbase{}",  "'", "`", 
49     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
50   { ",,", "''", "``", "<<", ">>" } };
51
52 static char const * const latex_quote_ot1[2][5] = 
53 { { "\\quotesinglbase{}",  "'", "`", 
54     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
55   { "\\quotedblbase{}", "''", "``",
56     "\\guillemotleft{}", "\\guillemotright{}" } };
57
58 static char const * const latex_quote_babel[2][5] = 
59 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
60   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
61
62
63 InsetQuotes::InsetQuotes(string const & str)
64 {
65         ParseString(str);
66 }
67
68
69 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
70                          InsetQuotes::quote_side s,
71                          InsetQuotes::quote_times t)
72         : language(l), side(s), times(t)
73 {
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_PROTECTED_SEPARATOR:
85         case LyXParagraph::META_NEWLINE: 
86                 side = InsetQuotes::LeftQ;   // left quote 
87                 break;
88         default:
89                 side = InsetQuotes::RightQ;  // right quote
90         }
91 }
92
93
94 void InsetQuotes::ParseString(string const & s)
95 {
96         int i;
97         string str(s);
98         if (str.length() != 3) {
99                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
100                         " bad string length." << endl;
101                 str = "eld";
102         }
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 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 == "iso8859-1") 
150                 if (disp == "<<")
151                         disp = '«';
152                 else if (disp == ">>")
153                         disp = '»';
154         
155         return disp;
156 }
157
158
159 #ifdef USE_PAINTER
160 int InsetQuotes::ascent(Painter &, LyXFont const & font) const
161 {
162         return font.maxAscent();
163 }
164 #else
165 int InsetQuotes::Ascent(LyXFont const & font) const
166 {
167         return font.maxAscent();
168 }
169 #endif
170
171
172 #ifdef USE_PAINTER
173 int InsetQuotes::descent(Painter &, LyXFont const & font) const
174 {
175         return font.maxDescent();
176 }
177 #else
178 int InsetQuotes::Descent(LyXFont const & font) const
179 {
180         return font.maxDescent();
181 }
182 #endif
183
184
185 #ifdef USE_PAINTER
186 int InsetQuotes::width(Painter &, LyXFont const & font) const
187 {
188         string text = DispString();
189         int w = 0;
190
191         for (string::size_type i = 0; i < text.length(); ++i) {
192                 if (text[i] == ' ')
193                         w += font.width('i');
194                 else if (i == 0 || text[i] != text[i-1])
195                         w += font.width(text[i]);
196                 else
197                         w += font.width(',');
198         }
199
200         return w;
201 }
202 #else
203 int InsetQuotes::Width(LyXFont const & font) const
204 {
205         string text = DispString();
206         int w = 0;
207
208         for (string::size_type i = 0; i < text.length(); ++i) {
209                 if (text[i] == ' ')
210                         w += font.width('i');
211                 else if (i == 0 || text[i] != text[i-1])
212                         w += font.width(text[i]);
213                 else
214                         w += font.width(',');
215         }
216
217         return w;
218 }
219 #endif
220
221
222 LyXFont InsetQuotes::ConvertFont(LyXFont font)
223 {
224         /* quotes-insets cannot be latex of any kind */
225         font.setLatex(LyXFont::OFF);
226         return font;
227 }
228
229
230 #ifdef USE_PAINTER
231 void InsetQuotes::draw(Painter & pain, LyXFont const & font,
232                        int baseline, float & x) const
233 {
234         string text = DispString();
235
236         pain.text(int(x), baseline, text, font);
237         x += width(pain, font);
238 }
239 #else
240 void InsetQuotes::Draw(LyXFont font, LyXScreen & scr,
241                        int baseline, float & x)
242 {
243         string text = DispString();
244
245         scr.drawString(font, text, baseline, int(x));
246         x += Width(font);
247 }
248 #endif
249
250
251 void InsetQuotes::Write(ostream & os)
252 {
253         string text;
254         text += language_char[language];
255         text += side_char[side];
256         text += times_char[times]; 
257         os << "Quotes " << text;
258 }
259
260
261 void InsetQuotes::Read(LyXLex & lex)
262 {
263         lex.nextToken();
264         ParseString(lex.GetString());
265 }
266
267
268 int InsetQuotes::Latex(ostream & os, signed char /*fragile*/)
269 {
270         string quote;
271         int res = Latex(quote, 0);
272         os << quote;
273         return res;
274 }
275
276
277 int InsetQuotes::Latex(string & file, signed char /*fragile*/)
278 {
279         string doclang = 
280                 current_view->buffer()->GetLanguage();
281         int quoteind = quote_index[side][language];
282         string qstr;
283         
284         if (lyxrc->fontenc == "T1") {
285                 qstr = latex_quote_t1[times][quoteind];
286         }
287         else if (doclang == "default") {
288                 qstr = latex_quote_ot1[times][quoteind];
289         } 
290         else if (language == InsetQuotes::FrenchQ 
291                  && times == InsetQuotes::DoubleQ
292                  && doclang == "frenchb") {
293                 if (side == InsetQuotes::LeftQ) 
294                         qstr = "\\og{}";
295                 else 
296                         qstr = " \\fg{}";
297         } 
298         else 
299                 qstr = latex_quote_babel[times][quoteind];
300
301         // protect against !` and ?` ligatures.
302         if ((suffixIs(file, '?') || suffixIs(file, '!'))
303             && qstr[0] == '`')
304                 qstr = "{}" + qstr;
305
306         file += qstr;
307         return 0;
308 }
309
310
311 int InsetQuotes::Linuxdoc(string & file)
312 {
313         file += "\"";
314
315         return 0;
316 }
317
318
319 int InsetQuotes::DocBook(string & file)
320 {
321         if(times == InsetQuotes::DoubleQ) {
322                 if (side == InsetQuotes::LeftQ)
323                         file += "&ldquo;";
324                 else
325                         file += "&rdquo;";
326         } else {
327                 if (side == InsetQuotes::LeftQ)
328                         file += "&lsquo;";
329                 else
330                         file += "&rsquo;";
331         }
332         return 0;
333 }
334
335
336 void InsetQuotes::Validate(LaTeXFeatures & features) const 
337 {
338         char type = quote_char[quote_index[side][language]];
339
340         if (current_view->buffer()->GetLanguage() == "default" 
341             && lyxrc->fontenc != "T1") {
342                 if (times == InsetQuotes::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() 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 }