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