]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
8444ee0aa9819e67dd4a2e56c209fb3bee75ff89
[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 #include "font.h"
27
28 using std::ostream;
29 using std::endl;
30
31 // Quotes. Used for the various quotes. German, English, French,
32 // Danish, Polish, all either double or single.
33
34 extern BufferView * current_view;
35
36 // codes used to read/write quotes to LyX files
37 static char const * const language_char = "esgpfa";
38 static char const * const side_char = "lr" ;
39 static char const * const times_char = "sd";
40
41 // List of known quote chars
42 static char const * const quote_char = ",'`<>";
43
44 // Index of chars used for the quote. Index is [side, language]
45 int quote_index[2][6] = {
46         { 2, 1, 0, 0, 3, 4 },    // "'',,<>" 
47         { 1, 1, 2, 1, 4, 3 } };  // "`'`'><"
48
49 // Corresponding LaTeX code, for double and single quotes.
50 static char const * const latex_quote_t1[2][5] = 
51 { { "\\quotesinglbase{}",  "'", "`", 
52     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
53   { ",,", "''", "``", "<<", ">>" } };
54
55 static char const * const latex_quote_ot1[2][5] = 
56 { { "\\quotesinglbase{}",  "'", "`", 
57     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
58   { "\\quotedblbase{}", "''", "``",
59     "\\guillemotleft{}", "\\guillemotright{}" } };
60
61 static char const * const latex_quote_babel[2][5] = 
62 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
63   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
64
65
66 InsetQuotes::InsetQuotes(string const & str)
67 {
68         ParseString(str);
69 }
70
71
72 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
73                          InsetQuotes::quote_side s,
74                          InsetQuotes::quote_times t)
75         : language(l), side(s), times(t)
76 {}
77
78
79 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
80         : language(params.quotes_language), times(params.quotes_times)
81 {
82         // Decide whether left or right 
83         switch(c) {
84         case ' ': case '(': case '{': case '[': case '-': case ':':
85         case LyXParagraph::META_HFILL:
86         case LyXParagraph::META_NEWLINE: 
87                 side = InsetQuotes::LeftQ;   // left quote 
88                 break;
89         default:
90                 side = InsetQuotes::RightQ;  // right quote
91         }
92 }
93
94
95 void InsetQuotes::ParseString(string const & s)
96 {
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         int i;
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 const 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(BufferView *, LyXFont const & font) const
162 {
163         return lyxfont::maxAscent(font);
164 }
165
166
167 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
168 {
169         return lyxfont::maxDescent(font);
170 }
171
172
173 int InsetQuotes::width(BufferView *, 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 += lyxfont::width('i', font);
181                 else if (i == 0 || text[i] != text[i-1])
182                         w += lyxfont::width(text[i], font);
183                 else
184                         w += lyxfont::width(',', font);
185         }
186
187         return w;
188 }
189
190
191 LyXFont const InsetQuotes::ConvertFont(LyXFont const & f) const
192 {
193         LyXFont font(f);
194         // quotes-insets cannot be latex of any kind
195         font.setLatex(LyXFont::OFF);
196         return font;
197 }
198
199
200 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
201                        int baseline, float & x, bool) const
202 {
203         string text = DispString();
204
205         bv->painter().text(int(x), baseline, text, font);
206         x += width(bv, font);
207 }
208
209
210 void InsetQuotes::Write(Buffer const *, ostream & os) const
211 {
212         string text;
213         text += language_char[language];
214         text += side_char[side];
215         text += times_char[times]; 
216         os << "Quotes " << text;
217 }
218
219
220 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
221 {
222         lex.nextToken();
223         ParseString(lex.GetString());
224         lex.next();
225         string tmp(lex.GetString());
226         if (tmp != "\\end_inset")
227                 lyxerr << "LyX Warning: Missing \\end_inset "
228                         "in InsetQuotes::Read." << endl;
229 }
230
231
232 int InsetQuotes::Latex(Buffer const *, ostream & os,
233                        bool /*fragile*/, bool) const
234 {
235         string doclang = 
236                 current_view->buffer()->GetLanguage();
237         int quoteind = quote_index[side][language];
238         string qstr;
239         
240         if (lyxrc.fontenc == "T1") {
241                 qstr = latex_quote_t1[times][quoteind];
242         } else if (doclang == "default") {
243                 qstr = latex_quote_ot1[times][quoteind];
244         } else if (language == InsetQuotes::FrenchQ 
245                  && times == InsetQuotes::DoubleQ
246                  && doclang == "frenchb") {
247                 if (side == InsetQuotes::LeftQ) 
248                         qstr = "\\og{}";
249                 else 
250                         qstr = " \\fg{}";
251         } else 
252                 qstr = latex_quote_babel[times][quoteind];
253
254         // Always guard against unfortunate ligatures (!` ?`)
255         qstr.insert(0, "{}");
256
257         os << qstr;
258         return 0;
259 }
260
261
262 int InsetQuotes::Ascii(Buffer const *, ostream & os, int) const
263 {
264         os << "\"";
265         return 0;
266 }
267
268
269 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
270 {
271         os << "\"";
272         return 0;
273 }
274
275
276 int InsetQuotes::DocBook(Buffer const *, 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 }