]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
2b3d4bd728b27e03372b05cc80acbb8c5a14cc19
[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         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 << (free_space ? " " : "~"); break;
200         }
201         return 0;
202 }
203
204
205 int InsetSpecialChar::Linuxdoc(ostream & os) const
206 {
207         switch (kind) {
208         case HYPHENATION:         os << "";     break;
209         case END_OF_SENTENCE:     os << "";     break;
210         case LDOTS:               os << "...";  break;
211         case MENU_SEPARATOR:      os << "->";   break;
212         case PROTECTED_SEPARATOR: os << " ";   break;
213         }
214         return 0;
215 }
216
217
218 int InsetSpecialChar::DocBook(ostream & os) const
219 {
220         switch (kind) {
221         case HYPHENATION:         os << "";     break;
222         case END_OF_SENTENCE:     os << "";     break;
223         case LDOTS:               os << "...";  break;
224         case MENU_SEPARATOR:      os << "->";   break;
225         case PROTECTED_SEPARATOR: os << " ";   break;
226         }
227         return 0;
228 }
229
230
231 Inset * InsetSpecialChar::Clone() const
232 {
233         return new InsetSpecialChar(kind);
234 }
235
236
237 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
238 {
239         if (kind == MENU_SEPARATOR) {
240                 features.lyxarrow = true;
241         }
242 }