]> git.lyx.org Git - lyx.git/blob - src/insets/insetquotes.C
reformatting and remove using delc
[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 namespace {
35
36 // codes used to read/write quotes to LyX files
37 char const * const language_char = "esgpfa";
38 char const * const side_char = "lr" ;
39 char const * const times_char = "sd";
40
41 // List of known quote chars
42 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 char const * const latex_quote_t1[2][5] = 
51 { { "\\quotesinglbase{}",  "'", "`", 
52     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
53   { ",,", "''", "``", "<<", ">>" } };
54
55 char const * const latex_quote_ot1[2][5] = 
56 { { "\\quotesinglbase{}",  "'", "`", 
57     "\\guilsinglleft{}", "\\guilsinglright{}" }, 
58   { "\\quotedblbase{}", "''", "``",
59     "\\guillemotleft{}", "\\guillemotright{}" } };
60
61 char const * const latex_quote_babel[2][5] = 
62 { { "\\glq{}",  "'", "`", "\\flq{}", "\\frq{}" },
63   { "\\glqq{}", "''", "``", "\\flqq{}", "\\frqq{}" } };
64
65 } // namespace anon
66
67
68 InsetQuotes::InsetQuotes(string const & str)
69 {
70         ParseString(str);
71 }
72
73
74 InsetQuotes::InsetQuotes(InsetQuotes::quote_language l,
75                          InsetQuotes::quote_side s,
76                          InsetQuotes::quote_times t)
77         : language(l), side(s), times(t)
78 {}
79
80
81 InsetQuotes::InsetQuotes(char c, BufferParams const & params)
82         : language(params.quotes_language), times(params.quotes_times)
83 {
84         // Decide whether left or right 
85         switch (c) {
86         case ' ': case '(': case '{': case '[': case '-': case ':':
87         case LyXParagraph::META_HFILL:
88         case LyXParagraph::META_NEWLINE: 
89                 side = InsetQuotes::LeftQ;   // left quote 
90                 break;
91         default:
92                 side = InsetQuotes::RightQ;  // right quote
93         }
94 }
95
96
97 void InsetQuotes::ParseString(string const & s)
98 {
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         int i;
107         
108         for (i = 0; i < 6; ++i) {
109                 if (str[0] == language_char[i]) {
110                         language = InsetQuotes::quote_language(i);
111                         break;
112                 }
113         }
114         if (i >= 6) {
115                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
116                         " bad language specification." << endl;
117                 language = InsetQuotes::EnglishQ; 
118         }
119
120         for (i = 0; i < 2; ++i) {
121                 if (str[1] == side_char[i]) {
122                         side = InsetQuotes::quote_side(i);
123                         break;
124                 }
125         }
126         if (i >= 2) {
127                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
128                         " bad side specification." << endl;
129                 side = InsetQuotes::LeftQ; 
130         }
131
132         for (i = 0; i < 2; ++i) {
133                 if (str[2] == times_char[i]) {
134                         times = InsetQuotes::quote_times(i);
135                         break;
136                 }
137         }
138         if (i >= 2) {
139                 lyxerr << "ERROR (InsetQuotes::InsetQuotes):"
140                         " bad times specification." << endl;
141                 times = InsetQuotes::DoubleQ; 
142         }
143 }
144
145
146 string const InsetQuotes::DispString() const
147 {
148         string disp;
149         disp += quote_char[quote_index[side][language]];
150         if (times == InsetQuotes::DoubleQ)
151                 disp += disp;
152
153         if (lyxrc.font_norm_type == LyXRC::ISO_8859_1)
154                 if (disp == "<<")
155                         disp = '«';
156                 else if (disp == ">>")
157                         disp = '»';
158         
159         return disp;
160 }
161
162
163 int InsetQuotes::ascent(BufferView *, LyXFont const & font) const
164 {
165         return lyxfont::maxAscent(font);
166 }
167
168
169 int InsetQuotes::descent(BufferView *, LyXFont const & font) const
170 {
171         return lyxfont::maxDescent(font);
172 }
173
174
175 int InsetQuotes::width(BufferView *, LyXFont const & font) const
176 {
177         string text = DispString();
178         int w = 0;
179
180         for (string::size_type i = 0; i < text.length(); ++i) {
181                 if (text[i] == ' ')
182                         w += lyxfont::width('i', font);
183                 else if (i == 0 || text[i] != text[i-1])
184                         w += lyxfont::width(text[i], font);
185                 else
186                         w += lyxfont::width(',', font);
187         }
188
189         return w;
190 }
191
192
193 LyXFont const InsetQuotes::ConvertFont(LyXFont const & f) const
194 {
195         LyXFont font(f);
196         // quotes-insets cannot be latex of any kind
197         font.setLatex(LyXFont::OFF);
198         return font;
199 }
200
201
202 void InsetQuotes::draw(BufferView * bv, LyXFont const & font,
203                        int baseline, float & x, bool) const
204 {
205         string text = DispString();
206
207         bv->painter().text(int(x), baseline, text, font);
208         x += width(bv, font);
209 }
210
211
212 void InsetQuotes::Write(Buffer const *, ostream & os) const
213 {
214         string text;
215         text += language_char[language];
216         text += side_char[side];
217         text += times_char[times]; 
218         os << "Quotes " << text;
219 }
220
221
222 void InsetQuotes::Read(Buffer const *, LyXLex & lex)
223 {
224         lex.nextToken();
225         ParseString(lex.GetString());
226         lex.next();
227         string tmp(lex.GetString());
228         if (tmp != "\\end_inset")
229                 lyxerr << "LyX Warning: Missing \\end_inset "
230                         "in InsetQuotes::Read." << endl;
231 }
232
233
234 extern bool use_babel;
235
236 int InsetQuotes::Latex(Buffer const * buf, ostream & os,
237                        bool /*fragile*/, bool) const
238 {
239         string const doclang = buf->GetLanguage()->lang();
240         int quoteind = quote_index[side][language];
241         string qstr;
242         
243         if (lyxrc.fontenc == "T1") {
244                 qstr = latex_quote_t1[times][quoteind];
245 #ifdef DO_USE_DEFAULT_LANGUAGE
246         } else if (doclang == "default") {
247 #else
248         } else if (!use_babel) {
249 #endif
250                 qstr = latex_quote_ot1[times][quoteind];
251         } else if (language == InsetQuotes::FrenchQ 
252                  && times == InsetQuotes::DoubleQ
253                  && doclang == "frenchb") {
254                 if (side == InsetQuotes::LeftQ) 
255                         qstr = "\\og{}";
256                 else 
257                         qstr = " \\fg{}";
258         } else 
259                 qstr = latex_quote_babel[times][quoteind];
260
261         // Always guard against unfortunate ligatures (!` ?`)
262         qstr.insert(0, "{}");
263
264         os << qstr;
265         return 0;
266 }
267
268
269 int InsetQuotes::Ascii(Buffer const *, ostream & os, int) const
270 {
271         os << "\"";
272         return 0;
273 }
274
275
276 int InsetQuotes::Linuxdoc(Buffer const *, ostream & os) const
277 {
278         os << "\"";
279         return 0;
280 }
281
282
283 int InsetQuotes::DocBook(Buffer const *, ostream & os) const
284 {
285         if (times == InsetQuotes::DoubleQ) {
286                 if (side == InsetQuotes::LeftQ)
287                         os << "&ldquo;";
288                 else
289                         os << "&rdquo;";
290         } else {
291                 if (side == InsetQuotes::LeftQ)
292                         os << "&lsquo;";
293                 else
294                         os << "&rsquo;";
295         }
296         return 0;
297 }
298
299
300 void InsetQuotes::Validate(LaTeXFeatures & features) const 
301 {
302         char type = quote_char[quote_index[side][language]];
303
304         if (features.bufferParams().language->lang() == "default" 
305             && lyxrc.fontenc != "T1") {
306                 if (times == InsetQuotes::SingleQ) 
307                         switch (type) {
308                         case ',': features.quotesinglbase = true; break;
309                         case '<': features.guilsinglleft = true; break;
310                         case '>': features.guilsinglright = true; break;
311                         default: break;
312                         }
313                 else 
314                         switch (type) {
315                         case ',': features.quotedblbase = true; break;
316                         case '<': features.guillemotleft = true; break;
317                         case '>': features.guillemotright = true; break;
318                         default: break;
319                         }
320         }
321 }
322
323
324 Inset * InsetQuotes::Clone(Buffer const &) const
325 {
326   return new InsetQuotes(language, side, times);
327 }
328
329
330 Inset::Code InsetQuotes::LyxCode() const
331 {
332   return Inset::QUOTE_CODE;
333 }