]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
7dfef058f2253d6e774df453bb70ec11bbdc57bd
[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 "error.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 LYX_META_HFILL: case LYX_META_PROTECTED_SEPARATOR:
82         case LYX_META_NEWLINE: 
83                 side = InsetQuotes::LeftQ;   // left quote 
84                 break;
85         default:
86                 side = InsetQuotes::RightQ;  // right quote
87         }
88 }
89
90
91 void InsetQuotes::ParseString(string str)
92 {
93         int i;
94         if (str.length() != 3) {
95                 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
96                               " bad string length.");
97                 str = "eld";
98         }
99
100         for (i=0;i<6;i++) {
101                 if (str[0] == language_char[i]) {
102                         language = (InsetQuotes::quote_language)i;
103                         break;
104                 }
105         }
106         if (i>=6) {
107                 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
108                               " bad language specification.");
109                 language = InsetQuotes::EnglishQ; 
110         }
111
112         for (i=0;i<2;i++) {
113                 if (str[1] == side_char[i]) {
114                         side = (InsetQuotes::quote_side)i;
115                         break;
116                 }
117         }
118         if (i>=2) {
119                 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
120                               " bad side specification.");
121                 side = InsetQuotes::LeftQ; 
122         }
123
124         for (i=0;i<2;i++) {
125                 if (str[2] == times_char[i]) {
126                         times = (InsetQuotes::quote_times)i;
127                         break;
128                 }
129         }
130         if (i>=2) {
131                 lyxerr.print("ERROR (InsetQuotes::InsetQuotes):"
132                               " bad times specification.");
133                 times = InsetQuotes::DoubleQ; 
134         }
135 }
136
137 string InsetQuotes::DispString() const
138 {
139         string disp;
140
141         disp += quote_char[quote_index[side][language]];
142                 
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         for (string::size_type i = 0; i < text.length(); ++i) {
200                 if (text[i] == ' ') 
201                         x += font.width('i');
202                 else if (i == text.length()-1 || text[i] != text[i+1]) {
203                         scr.drawString(font, &text[i], baseline, int(x));
204                         x += font.width(text[i]);
205                 } else {
206                         scr.drawString(font, &text[i+1], baseline, int(x));
207                         x += font.width(',');
208                 }
209         }
210         
211 }
212
213
214 void InsetQuotes::Write(FILE * file)
215 {
216         string text;
217         text += language_char[language];
218         text += side_char[side];
219         text += times_char[times]; 
220         fprintf(file, "Quotes %s", text.c_str());
221 }
222
223
224 void InsetQuotes::Read(LyXLex & lex)
225 {
226         lex.nextToken();
227         ParseString(lex.GetString());
228 }
229
230
231 int InsetQuotes::Latex(FILE * file, signed char /*fragile*/)
232 {
233         string quote;
234         int res = Latex(quote, 0);
235         fprintf(file, "%s", quote.c_str()); 
236         return res;
237 }
238
239 int InsetQuotes::Latex(string & file, signed char /*fragile*/)
240 {
241         string doclang =
242                 current_view->currentBuffer()->GetLanguage();
243         int quoteind = quote_index[side][language];
244         string qstr;
245         
246         if (lyxrc->fontenc == "T1") {
247                 qstr = latex_quote_t1[times][quoteind];
248         }
249         else if (doclang == "default") {
250                 qstr = latex_quote_ot1[times][quoteind];
251         } 
252         else if (language == InsetQuotes::FrenchQ 
253                  && times == InsetQuotes::DoubleQ
254                  && doclang == "frenchb") {
255                 if (side == InsetQuotes::LeftQ) 
256                         qstr = "\\og{}";
257                 else 
258                         qstr = " \\fg{}";
259         } 
260         else 
261                 qstr = latex_quote_babel[times][quoteind];
262
263         // protect against !` and ?` ligatures.
264         if ((suffixIs(file, '?') || suffixIs(file, '!'))
265             && qstr[0] == '`')
266                 qstr = "{}" + qstr;
267
268         file += qstr;
269         return 0;
270 }
271
272
273 int InsetQuotes::Linuxdoc(string & file)
274 {
275         file += "\"";
276
277         return 0;
278 }
279
280
281 int InsetQuotes::DocBook(string & file)
282 {
283         if(times == InsetQuotes::DoubleQ) {
284                 if (side == InsetQuotes::LeftQ)
285                         file += "&ldquo;";
286                 else
287                         file += "&rdquo;";
288         } else {
289                 if (side == InsetQuotes::LeftQ)
290                         file += "&lsquo;";
291                 else
292                         file += "&rsquo;";
293         }
294         return 0;
295 }
296
297
298 void InsetQuotes::Validate(LaTeXFeatures & features) const 
299 {
300         char type = quote_char[quote_index[side][language]];
301
302         if (current_view->currentBuffer()->GetLanguage() == "default" 
303             && lyxrc->fontenc != "T1") {
304                 if (times == InsetQuotes::SingleQ) 
305                         switch (type) {
306                         case ',': features.quotesinglbase = true; break;
307                         case '<': features.guilsinglleft = true; break;
308                         case '>': features.guilsinglright = true; break;
309                         default: break;
310                         }
311                 else 
312                         switch (type) {
313                         case ',': features.quotedblbase = true; break;
314                         case '<': features.guillemotleft = true; break;
315                         case '>': features.guillemotright = true; break;
316                         default: break;
317                         }
318         }
319 }
320
321 Inset* InsetQuotes::Clone()
322 {
323   return new InsetQuotes(language, side, times);
324 }
325
326
327 Inset::Code InsetQuotes::LyxCode() const
328 {
329   return Inset::QUOTE_CODE;
330 }