]> git.lyx.org Git - lyx.git/blob - src/insets/insetspecialchar.C
f1889a2260a572e264ddf4fab58893a66a983c90
[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 "lyxdraw.h"
18 #include "debug.h"
19 #include "LaTeXFeatures.h"
20
21 InsetSpecialChar::InsetSpecialChar(Kind k)
22         : kind(k)
23 {}
24
25
26 int InsetSpecialChar::Ascent(LyXFont const & font) const
27 {
28         return font.maxAscent();
29 }
30
31
32 int InsetSpecialChar::Descent(LyXFont const & font) const
33 {
34         return font.maxDescent();
35 }
36
37
38 int InsetSpecialChar::Width(LyXFont const & font) const
39 {
40         LyXFont f = font;
41         switch (kind) {
42         case HYPHENATION:
43         {
44                 int w = f.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 f.textWidth(".", 1);
52         }
53         case LDOTS:
54         {
55                 return f.textWidth(". . .", 5);
56         }
57         case MENU_SEPARATOR: {
58                 return f.textWidth(" x ", 3);
59         }
60         }
61         return 1; // To shut up gcc
62 }
63
64
65 void InsetSpecialChar::Draw(LyXFont font, LyXScreen & scr,
66                             int baseline, float & x)
67 {
68         switch (kind) {
69         case HYPHENATION:
70         {
71                 font.setColor(LyXFont::MAGENTA);
72                 scr.drawText(font, "-", 1, baseline, int(x));
73                 x += Width(font);
74                 break;
75         }
76         case END_OF_SENTENCE:
77         {
78                 font.setColor(LyXFont::MAGENTA);
79                 scr.drawText(font, ".", 1, baseline, int(x));
80                 x += Width(font);
81                 break;
82         }
83         case LDOTS:
84         {
85                 font.setColor(LyXFont::MAGENTA);
86                 scr.drawText(font, ". . .", 5, baseline, int(x));
87                 x += Width(font);
88                 break;
89         }
90         case MENU_SEPARATOR:
91         {
92                 // A triangle the width and height of an 'x'
93                 int w = font.textWidth("x", 1);
94                 int ox = font.textWidth(" ", 1) + int(x);
95                 int h = font.ascent('x');
96                 XPoint p[4];
97                 p[0].x = ox;    p[0].y = baseline;
98                 p[1].x = ox;    p[1].y = baseline - h;
99                 p[2].x = ox + w;p[2].y = baseline - h/2;
100                 p[3].x = ox;    p[3].y = baseline;
101                 scr.drawLines(getGC(gc_copy), p, 4);
102                 x += Width(font);
103         }
104         }
105 }
106
107
108 // In lyxf3 this will be just LaTeX
109 void InsetSpecialChar::Write(FILE * file)
110 {
111         string command;
112         switch (kind) {
113         case HYPHENATION:       command = "\\-";        break;
114         case END_OF_SENTENCE:   command = "\\@.";       break;
115         case LDOTS:             command = "\\ldots{}";  break;
116         case MENU_SEPARATOR:    command = "\\menuseparator"; break;
117         }
118         fprintf(file, "\\SpecialChar %s\n", command.c_str());
119 }
120
121
122 // This function will not be necessary when lyx3
123 void InsetSpecialChar::Read(LyXLex & lex)
124 {    
125         lex.nextToken();
126         string command = lex.GetString();
127
128         if (command == "\\-")
129                 kind = HYPHENATION;
130         else if (command == "\\@.")
131                 kind = END_OF_SENTENCE;
132         else if (command == "\\ldots{}")
133                 kind = LDOTS;
134         else if (command == "\\menuseparator")
135                 kind = MENU_SEPARATOR;
136         else
137                 lex.printError("InsetSpecialChar: Unknown kind: `$$Token'");
138 }
139
140
141 int InsetSpecialChar::Latex(FILE * file, signed char /*fragile*/)
142 {
143         string command;
144         signed char dummy = 0;
145         Latex(command, dummy);
146         fprintf(file, "%s", command.c_str());
147         return 0;
148 }
149
150
151 int InsetSpecialChar::Latex(string & file, signed char /*fragile*/)
152 {
153         switch (kind) {
154         case HYPHENATION:       file += "\\-";  break;
155         case END_OF_SENTENCE:   file += "\\@."; break;
156         case LDOTS:             file += "\\ldots{}";    break;
157         case MENU_SEPARATOR:    file += "\\lyxarrow{}"; break;
158         }
159         return 0;
160 }
161
162
163 int InsetSpecialChar::Linuxdoc(string & file)
164 {
165         switch (kind) {
166         case HYPHENATION:       file += "";     break;
167         case END_OF_SENTENCE:   file += "";     break;
168         case LDOTS:             file += "...";  break;
169         case MENU_SEPARATOR:    file += "->";   break;
170         }
171         return 0;
172 }
173
174
175 int InsetSpecialChar::DocBook(string & file)
176 {
177         switch (kind) {
178         case HYPHENATION:       file += "";     break;
179         case END_OF_SENTENCE:   file += "";     break;
180         case LDOTS:             file += "...";  break;
181         case MENU_SEPARATOR:    file += "->";   break;
182         }
183         return 0;
184 }
185
186
187 Inset * InsetSpecialChar::Clone() const
188 {
189         return new InsetSpecialChar(kind);
190 }
191
192
193 void InsetSpecialChar::Validate(LaTeXFeatures & features) const
194 {
195         if (kind == MENU_SEPARATOR) {
196                 features.lyxarrow = true;
197         }
198 }