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