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