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