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