]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
eea2bc00bcfa810aecd5fc4dd667bd68e34ee3bb
[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         LyXFont f(font);
42         switch (kind) {
43         case HYPHENATION:
44         {
45                 int w = f.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 f.textWidth(".", 1);
53         }
54         case LDOTS:
55         {
56                 return f.textWidth(". . .", 5);
57         }
58         case MENU_SEPARATOR: {
59                 return f.textWidth(" x ", 3);
60         }
61         }
62         return 1; // To shut up gcc
63 }
64
65
66 void InsetSpecialChar::draw(Painter & pain, LyXFont const & f,
67                             int baseline, float & x) const
68 {
69         LyXFont font(f);
70         switch (kind) {
71         case HYPHENATION:
72         {
73                 font.setColor(LColor::special);
74                 pain.text(int(x), baseline, "-", font);
75                 x += width(pain, font);
76                 break;
77         }
78         case END_OF_SENTENCE:
79         {
80                 font.setColor(LColor::special);
81                 pain.text(int(x), baseline, ".", font);
82                 x += width(pain, font);
83                 break;
84         }
85         case LDOTS:
86         {
87                 font.setColor(LColor::special);
88                 pain.text(int(x), baseline, ". . .", font);
89                 x += width(pain, font);
90                 break;
91         }
92         case MENU_SEPARATOR:
93         {
94                 // A triangle the width and height of an 'x'
95                 int w = font.textWidth("x", 1);
96                 int ox = font.textWidth(" ", 1) + int(x);
97                 int h = font.ascent('x');
98                 int xp[4], yp[4];
99                 
100                 xp[0] = ox;     yp[0] = baseline;
101                 xp[1] = ox;     yp[1] = baseline - h;
102                 xp[2] = ox + w; yp[2] = baseline - h/2;
103                 xp[3] = ox;     yp[3] = baseline;
104                 
105                 pain.lines(xp, yp, 4, LColor::special);
106                 x += width(pain, font);
107         }
108         }
109 }
110
111
112 // In lyxf3 this will be just LaTeX
113 void InsetSpecialChar::Write(ostream & os) const
114 {
115         string command;
116         switch (kind) {
117         case HYPHENATION:       command = "\\-";        break;
118         case END_OF_SENTENCE:   command = "\\@.";       break;
119         case LDOTS:             command = "\\ldots{}";  break;
120         case MENU_SEPARATOR:    command = "\\menuseparator"; break;
121         }
122         os << "\\SpecialChar " << command << "\n";
123 }
124
125
126 // This function will not be necessary when lyx3
127 void InsetSpecialChar::Read(LyXLex & lex)
128 {    
129         lex.nextToken();
130         string command = lex.GetString();
131
132         if (command == "\\-")
133                 kind = HYPHENATION;
134         else if (command == "\\@.")
135                 kind = END_OF_SENTENCE;
136         else if (command == "\\ldots{}")
137                 kind = LDOTS;
138         else if (command == "\\menuseparator")
139                 kind = MENU_SEPARATOR;
140         else
141                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
142 }
143
144
145 int InsetSpecialChar::Latex(ostream & os, signed char /*fragile*/) const
146 {
147         string command;
148         signed char dummy = 0;
149         Latex(command, dummy);
150         os << command;
151         return 0;
152 }
153
154
155 int InsetSpecialChar::Latex(string & file, signed char /*fragile*/) const
156 {
157         switch (kind) {
158         case HYPHENATION:       file += "\\-";  break;
159         case END_OF_SENTENCE:   file += "\\@."; break;
160         case LDOTS:             file += "\\ldots{}";    break;
161         case MENU_SEPARATOR:    file += "\\lyxarrow{}"; break;
162         }
163         return 0;
164 }
165
166
167 int InsetSpecialChar::Linuxdoc(string & file) const
168 {
169         switch (kind) {
170         case HYPHENATION:       file += "";     break;
171         case END_OF_SENTENCE:   file += "";     break;
172         case LDOTS:             file += "...";  break;
173         case MENU_SEPARATOR:    file += "->";   break;
174         }
175         return 0;
176 }
177
178
179 int InsetSpecialChar::DocBook(string & file) const
180 {
181         switch (kind) {
182         case HYPHENATION:       file += "";     break;
183         case END_OF_SENTENCE:   file += "";     break;
184         case LDOTS:             file += "...";  break;
185         case MENU_SEPARATOR:    file += "->";   break;
186         }
187         return 0;
188 }
189
190
191 Inset * InsetSpecialChar::Clone() const
192 {
193         return new InsetSpecialChar(kind);
194 }
195
196
197 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
198 {
199         if (kind == MENU_SEPARATOR) {
200                 features.lyxarrow = true;
201         }
202 }