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