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