]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
A bit of cxx warniong hunting; update to rpm spec file.
[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*/,
192                             bool free_space) const
193 {
194 #ifdef USE_OSTREAM_ONLY
195         switch (kind) {
196         case HYPHENATION:         os << "\\-";  break;
197         case END_OF_SENTENCE:     os << "\\@."; break;
198         case LDOTS:               os << "\\ldots{}";    break;
199         case MENU_SEPARATOR:      os << "\\lyxarrow{}"; break;
200         case PROTECTED_SEPARATOR: os << (free_space ? " " : "~"); break;
201         }
202         return 0;
203 #else
204         string command;
205         signed char dummy = 0;
206         Latex(command, dummy, free_space);
207         os << command;
208         return 0;
209 #endif
210 }
211
212
213 #ifndef USE_OSTREAM_ONLY
214 int InsetSpecialChar::Latex(string & file, signed char /*fragile*/,
215                             bool free_space) const
216 {
217         switch (kind) {
218         case HYPHENATION:       file += "\\-";  break;
219         case END_OF_SENTENCE:   file += "\\@."; break;
220         case LDOTS:             file += "\\ldots{}";    break;
221         case MENU_SEPARATOR:    file += "\\lyxarrow{}"; break;
222         case PROTECTED_SEPARATOR: file += (free_space ? " " : "~"); break;
223         }
224         return 0;
225 }
226
227
228 int InsetSpecialChar::Linuxdoc(string & file) const
229 {
230         switch (kind) {
231         case HYPHENATION:       file += "";     break;
232         case END_OF_SENTENCE:   file += "";     break;
233         case LDOTS:             file += "...";  break;
234         case MENU_SEPARATOR:    file += "->";   break;
235         case PROTECTED_SEPARATOR:    file += " ";   break;
236         }
237         return 0;
238 }
239
240
241 int InsetSpecialChar::DocBook(string & file) const
242 {
243         switch (kind) {
244         case HYPHENATION:       file += "";     break;
245         case END_OF_SENTENCE:   file += "";     break;
246         case LDOTS:             file += "...";  break;
247         case MENU_SEPARATOR:    file += "->";   break;
248         case PROTECTED_SEPARATOR:    file += " ";   break;
249         }
250         return 0;
251 }
252
253 #else
254
255 int InsetSpecialChar::Linuxdoc(ostream & os) const
256 {
257         switch (kind) {
258         case HYPHENATION:         os << "";     break;
259         case END_OF_SENTENCE:     os << "";     break;
260         case LDOTS:               os << "...";  break;
261         case MENU_SEPARATOR:      os << "->";   break;
262         case PROTECTED_SEPARATOR: os << " ";   break;
263         }
264         return 0;
265 }
266
267
268 int InsetSpecialChar::DocBook(ostream & os) const
269 {
270         switch (kind) {
271         case HYPHENATION:         os << "";     break;
272         case END_OF_SENTENCE:     os << "";     break;
273         case LDOTS:               os << "...";  break;
274         case MENU_SEPARATOR:      os << "->";   break;
275         case PROTECTED_SEPARATOR: os << " ";   break;
276         }
277         return 0;
278 }
279 #endif
280
281
282 Inset * InsetSpecialChar::Clone() const
283 {
284         return new InsetSpecialChar(kind);
285 }
286
287
288 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
289 {
290         if (kind == MENU_SEPARATOR) {
291                 features.lyxarrow = true;
292         }
293 }