]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
read changelog
[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 "Painter.h"
20 #ifndef USE_PAINTER
21 #include "lyxdraw.h"
22 #endif
23
24 InsetSpecialChar::InsetSpecialChar(Kind k)
25         : kind(k)
26 {}
27
28
29 #ifdef USE_PAINTER
30 int InsetSpecialChar::ascent(Painter &, LyXFont const & font) const
31 {
32         return font.maxAscent();
33 }
34 #else
35 int InsetSpecialChar::Ascent(LyXFont const & font) const
36 {
37         return font.maxAscent();
38 }
39 #endif
40
41
42 #ifdef USE_PAINTER
43 int InsetSpecialChar::descent(Painter &, LyXFont const & font) const
44 {
45         return font.maxDescent();
46 }
47 #else
48 int InsetSpecialChar::Descent(LyXFont const & font) const
49 {
50         return font.maxDescent();
51 }
52 #endif
53
54
55 #ifdef USE_PAINTER
56 int InsetSpecialChar::width(Painter &, LyXFont const & font) const
57 {
58         LyXFont f(font);
59         switch (kind) {
60         case HYPHENATION:
61         {
62                 int w = f.textWidth("-", 1);
63                 if (w > 5) 
64                         w -= 2; // to make it look shorter
65                 return w;
66         }
67         case END_OF_SENTENCE:
68         {
69                 return f.textWidth(".", 1);
70         }
71         case LDOTS:
72         {
73                 return f.textWidth(". . .", 5);
74         }
75         case MENU_SEPARATOR: {
76                 return f.textWidth(" x ", 3);
77         }
78         }
79         return 1; // To shut up gcc
80 }
81 #else
82 int InsetSpecialChar::Width(LyXFont const & font) const
83 {
84         LyXFont f = font;
85         switch (kind) {
86         case HYPHENATION:
87         {
88                 int w = f.textWidth("-", 1);
89                 if (w > 5) 
90                         w -= 2; // to make it look shorter
91                 return w;
92         }
93         case END_OF_SENTENCE:
94         {
95                 return f.textWidth(".", 1);
96         }
97         case LDOTS:
98         {
99                 return f.textWidth(". . .", 5);
100         }
101         case MENU_SEPARATOR: {
102                 return f.textWidth(" x ", 3);
103         }
104         }
105         return 1; // To shut up gcc
106 }
107 #endif
108
109
110 #ifdef USE_PAINTER
111 void InsetSpecialChar::draw(Painter & pain, LyXFont const & f,
112                             int baseline, float & x) const
113 {
114         LyXFont font(f);
115         switch (kind) {
116         case HYPHENATION:
117         {
118                 font.setColor(LColor::special);
119                 pain.text(int(x), baseline, "-", font);
120                 x += width(pain, font);
121                 break;
122         }
123         case END_OF_SENTENCE:
124         {
125                 font.setColor(LColor::special);
126                 pain.text(int(x), baseline, ".", font);
127                 x += width(pain, font);
128                 break;
129         }
130         case LDOTS:
131         {
132                 font.setColor(LColor::special);
133                 pain.text(int(x), baseline, ". . .", font);
134                 x += width(pain, font);
135                 break;
136         }
137         case MENU_SEPARATOR:
138         {
139                 // A triangle the width and height of an 'x'
140                 int w = font.textWidth("x", 1);
141                 int ox = font.textWidth(" ", 1) + int(x);
142                 int h = font.ascent('x');
143                 int xp[4], yp[4];
144                 
145                 xp[0] = ox;     yp[0] = baseline;
146                 xp[1] = ox;     yp[1] = baseline - h;
147                 xp[2] = ox + w; yp[2] = baseline - h/2;
148                 xp[3] = ox;     yp[3] = baseline;
149                 
150                 pain.lines(xp, yp, 4, LColor::special);
151                 x += width(pain, font);
152         }
153         }
154 }
155 #else
156 void InsetSpecialChar::Draw(LyXFont font, LyXScreen & scr,
157                             int baseline, float & x)
158 {
159         switch (kind) {
160         case HYPHENATION:
161         {
162                 font.setColor(LyXFont::BLUE);
163                 scr.drawText(font, "-", 1, baseline, int(x));
164                 x += Width(font);
165                 break;
166         }
167         case END_OF_SENTENCE:
168         {
169                 font.setColor(LyXFont::BLUE);
170                 scr.drawText(font, ".", 1, baseline, int(x));
171                 x += Width(font);
172                 break;
173         }
174         case LDOTS:
175         {
176                 font.setColor(LyXFont::BLUE);
177                 scr.drawText(font, ". . .", 5, baseline, int(x));
178                 x += Width(font);
179                 break;
180         }
181         case MENU_SEPARATOR:
182         {
183                 // A triangle the width and height of an 'x'
184                 int w = font.textWidth("x", 1);
185                 int ox = font.textWidth(" ", 1) + int(x);
186                 int h = font.ascent('x');
187                 XPoint p[4];
188                 p[0].x = ox;    p[0].y = baseline;
189                 p[1].x = ox;    p[1].y = baseline - h;
190                 p[2].x = ox + w;p[2].y = baseline - h/2;
191                 p[3].x = ox;    p[3].y = baseline;
192                 scr.drawLines(getGC(gc_copy), p, 4);
193                 x += Width(font);
194         }
195         }
196 }
197 #endif
198
199
200 // In lyxf3 this will be just LaTeX
201 void InsetSpecialChar::Write(ostream & os)
202 {
203         string command;
204         switch (kind) {
205         case HYPHENATION:       command = "\\-";        break;
206         case END_OF_SENTENCE:   command = "\\@.";       break;
207         case LDOTS:             command = "\\ldots{}";  break;
208         case MENU_SEPARATOR:    command = "\\menuseparator"; break;
209         }
210         os << "\\SpecialChar " << command << "\n";
211 }
212
213
214 // This function will not be necessary when lyx3
215 void InsetSpecialChar::Read(LyXLex & lex)
216 {    
217         lex.nextToken();
218         string command = lex.GetString();
219
220         if (command == "\\-")
221                 kind = HYPHENATION;
222         else if (command == "\\@.")
223                 kind = END_OF_SENTENCE;
224         else if (command == "\\ldots{}")
225                 kind = LDOTS;
226         else if (command == "\\menuseparator")
227                 kind = MENU_SEPARATOR;
228         else
229                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
230 }
231
232
233 int InsetSpecialChar::Latex(ostream & os, signed char /*fragile*/)
234 {
235         string command;
236         signed char dummy = 0;
237         Latex(command, dummy);
238         os << command;
239         return 0;
240 }
241
242
243 int InsetSpecialChar::Latex(string & file, signed char /*fragile*/)
244 {
245         switch (kind) {
246         case HYPHENATION:       file += "\\-";  break;
247         case END_OF_SENTENCE:   file += "\\@."; break;
248         case LDOTS:             file += "\\ldots{}";    break;
249         case MENU_SEPARATOR:    file += "\\lyxarrow{}"; break;
250         }
251         return 0;
252 }
253
254
255 int InsetSpecialChar::Linuxdoc(string & file)
256 {
257         switch (kind) {
258         case HYPHENATION:       file += "";     break;
259         case END_OF_SENTENCE:   file += "";     break;
260         case LDOTS:             file += "...";  break;
261         case MENU_SEPARATOR:    file += "->";   break;
262         }
263         return 0;
264 }
265
266
267 int InsetSpecialChar::DocBook(string & file)
268 {
269         switch (kind) {
270         case HYPHENATION:       file += "";     break;
271         case END_OF_SENTENCE:   file += "";     break;
272         case LDOTS:             file += "...";  break;
273         case MENU_SEPARATOR:    file += "->";   break;
274         }
275         return 0;
276 }
277
278
279 Inset * InsetSpecialChar::Clone() const
280 {
281         return new InsetSpecialChar(kind);
282 }
283
284
285 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
286 {
287         if (kind == MENU_SEPARATOR) {
288                 features.lyxarrow = true;
289         }
290 }