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