]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
Fix several warnings regarding unused variable/arguments.
[lyx.git] / src / insets / insetspecialchar.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1997 Asger Alstrup
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetspecialchar.h"
17 #include "debug.h"
18 #include "LaTeXFeatures.h"
19 #include "BufferView.h"
20 #include "Painter.h"
21 #include "font.h"
22 #include "lyxlex.h"
23
24 using std::ostream;
25 using std::max;
26
27 InsetSpecialChar::InsetSpecialChar(Kind k)
28         : kind_(k)
29 {}
30
31
32 InsetSpecialChar::Kind InsetSpecialChar::kind() const
33 {
34         return kind_;
35 }
36
37 int InsetSpecialChar::ascent(BufferView *, LyXFont const & font) const
38 {
39         return lyxfont::maxAscent(font);
40 }
41
42
43 int InsetSpecialChar::descent(BufferView *, LyXFont const & font) const
44 {
45         return lyxfont::maxDescent(font);
46 }
47
48
49 int InsetSpecialChar::width(BufferView *, LyXFont const & font) const
50 {
51         switch (kind_) {
52         case HYPHENATION:
53         {
54                 int w = lyxfont::width('-', font);
55                 if (w > 5) 
56                         w -= 2; // to make it look shorter
57                 return w;
58         }
59         case LIGATURE_BREAK:
60         {
61                 return lyxfont::width('|', font);
62         }
63         case END_OF_SENTENCE:
64         {
65                 return lyxfont::width('.', font);
66         }
67         case LDOTS:
68         {
69                 return lyxfont::width(". . .", font);
70         }
71         case MENU_SEPARATOR:
72         {
73                 return lyxfont::width(" x ", font);
74         }
75         case PROTECTED_SEPARATOR:
76         {
77                 return lyxfont::width('x', font);
78         }
79         
80         }
81         return 1; // To shut up gcc
82 }
83
84
85 void InsetSpecialChar::draw(BufferView * bv, LyXFont const & f,
86                             int baseline, float & x, bool) const
87 {
88         Painter & pain = bv->painter();
89         LyXFont font(f);
90
91         switch (kind_) {
92         case HYPHENATION:
93         {
94                 font.setColor(LColor::special);
95                 pain.text(int(x), baseline, "-", font);
96                 x += width(bv, font);
97                 break;
98         }
99         case LIGATURE_BREAK:
100         {
101                 font.setColor(LColor::special);
102                 pain.text(int(x), baseline, "|", font);
103                 x += width(bv, font);
104                 break;
105         }
106         case END_OF_SENTENCE:
107         {
108                 font.setColor(LColor::special);
109                 pain.text(int(x), baseline, ".", font);
110                 x += width(bv, font);
111                 break;
112         }
113         case LDOTS:
114         {
115                 font.setColor(LColor::special);
116                 pain.text(int(x), baseline, ". . .", font);
117                 x += width(bv, font);
118                 break;
119         }
120         case MENU_SEPARATOR:
121         {
122                 // A triangle the width and height of an 'x'
123                 int w = lyxfont::width('x', font);
124                 int ox = lyxfont::width(' ', font) + int(x);
125                 int h = lyxfont::ascent('x', font);
126                 int xp[4], yp[4];
127                 
128                 xp[0] = ox;     yp[0] = baseline;
129                 xp[1] = ox;     yp[1] = baseline - h;
130                 xp[2] = ox + w; yp[2] = baseline - h/2;
131                 xp[3] = ox;     yp[3] = baseline;
132                 
133                 pain.lines(xp, yp, 4, LColor::special);
134                 x += width(bv, font);
135                 break;
136         }
137         case PROTECTED_SEPARATOR:
138         {
139                 float w = width(bv, font);
140                 int h = lyxfont::ascent('x', font);
141                 int xp[4], yp[4];
142                 
143                 xp[0] = int(x);
144                 yp[0] = baseline - max(h / 4, 1);
145
146                 xp[1] = int(x);
147                 yp[1] = baseline;
148
149                 xp[2] = int(x + w);
150                 yp[2] = baseline;
151
152                 xp[3] = int(x + w);
153                 yp[3] = baseline - max(h / 4, 1);
154                 
155                 pain.lines(xp, yp, 4, LColor::special);
156                 x += w;
157                 break;
158         }
159         }
160 }
161
162
163 // In lyxf3 this will be just LaTeX
164 void InsetSpecialChar::write(Buffer const *, ostream & os) const
165 {
166         string command;
167         switch (kind_) {
168         case HYPHENATION:       
169                 command = "\\-";        
170                 break;
171         case LIGATURE_BREAK: 
172                 command = "\\textcompwordmark{}"; 
173                 break;
174         case END_OF_SENTENCE:   
175                 command = "\\@.";
176                 break;
177         case LDOTS:
178                 command = "\\ldots{}";  
179                 break;
180         case MENU_SEPARATOR:
181                 command = "\\menuseparator"; 
182                 break;
183         case PROTECTED_SEPARATOR:
184                 //command = "\\protected_separator";
185                 command = "~";
186                 break;
187         }
188         os << "\\SpecialChar " << command << "\n";
189 }
190
191
192 // This function will not be necessary when lyx3
193 void InsetSpecialChar::read(Buffer const *, LyXLex & lex)
194 {    
195         lex.nextToken();
196         string const command = lex.getString();
197
198         if (command == "\\-")
199                 kind_ = HYPHENATION;
200         else if (command == "\\textcompwordmark{}")
201                 kind_ = LIGATURE_BREAK;
202         else if (command == "\\@.")
203                 kind_ = END_OF_SENTENCE;
204         else if (command == "\\ldots{}")
205                 kind_ = LDOTS;
206         else if (command == "\\menuseparator")
207                 kind_ = MENU_SEPARATOR;
208         else if (command == "\\protected_separator"
209                  || command == "~")
210                 kind_ = PROTECTED_SEPARATOR;
211         else
212                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
213 }
214
215
216 int InsetSpecialChar::latex(Buffer const *, ostream & os, bool /*fragile*/,
217                             bool free_space) const
218 {
219         switch (kind_) {
220         case HYPHENATION:         
221                 os << "\\-";    
222                 break;
223         case LIGATURE_BREAK:
224                 os << "\\textcompwordmark{}";
225                 break;
226         case END_OF_SENTENCE:     
227                 os << "\\@.";   
228                 break;
229         case LDOTS:               
230                 os << "\\ldots{}";      
231                 break;
232         case MENU_SEPARATOR:      
233                 os << "\\lyxarrow{}"; 
234                 break;
235         case PROTECTED_SEPARATOR: 
236                 os << (free_space ? " " : "~"); 
237                 break;
238         }
239         return 0;
240 }
241
242
243 int InsetSpecialChar::ascii(Buffer const *, ostream & os, int) const
244 {
245         switch (kind_) {
246         case HYPHENATION:
247         case LIGATURE_BREAK:
248                 break;
249         case END_OF_SENTENCE:     
250                 os << ".";      
251                 break;
252         case LDOTS:               
253                 os << "...";    
254                 break;
255         case MENU_SEPARATOR:      
256                 os << "->";   
257                 break;
258         case PROTECTED_SEPARATOR: 
259                 os << " ";
260                 break;
261         }
262         return 0;
263 }
264
265
266 int InsetSpecialChar::linuxdoc(Buffer const *, ostream & os) const
267 {
268         switch (kind_) {
269         case HYPHENATION:
270         case LIGATURE_BREAK:
271                 break;
272         case END_OF_SENTENCE:
273                 os << ".";
274                 break;
275         case LDOTS:
276                 os << "...";    
277                 break;
278         case MENU_SEPARATOR:      
279                 os << "&lyxarrow;"; 
280                 break;
281         case PROTECTED_SEPARATOR:
282                 os << "&nbsp;";
283                 break;
284         }
285         return 0;
286 }
287
288
289 int InsetSpecialChar::docbook(Buffer const *, ostream & os) const
290 {
291         switch (kind_) {
292         case HYPHENATION:
293         case LIGATURE_BREAK:
294                 break;
295         case END_OF_SENTENCE:
296                 os << ".";
297                 break;
298         case LDOTS:
299                 os << "...";    
300                 break;
301         case MENU_SEPARATOR:
302                 os << "&lyxarrow;";
303                 break;
304         case PROTECTED_SEPARATOR:
305                 os << "&nbsp;";
306                 break;
307         }
308         return 0;
309 }
310
311
312 Inset * InsetSpecialChar::clone(Buffer const &, bool) const
313 {
314         return new InsetSpecialChar(kind_);
315 }
316
317
318 void InsetSpecialChar::validate(LaTeXFeatures & features) const
319 {
320         if (kind_ == MENU_SEPARATOR) {
321                 features.lyxarrow = true;
322         }
323 }
324
325
326 bool InsetSpecialChar::isLetter() const
327 {
328         return kind_ == HYPHENATION || kind_ == LIGATURE_BREAK;
329 }
330
331
332 bool InsetSpecialChar::isSpace() const
333 {
334         return kind_ == PROTECTED_SEPARATOR;
335 }