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