]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Add support for compilers which do not have cname headers
[lyx.git] / src / insets / insetcommand.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-1999 The LyX Team.
8  *
9  * ======================================================*/
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetcommand.h"
18 #include "lyxdraw.h"
19 #include "debug.h"
20
21 InsetCommand::InsetCommand()
22 {
23 }
24
25
26 InsetCommand::InsetCommand(string const & cmd, string const & arg, 
27                            string const & opt)
28         : command(cmd), options(opt), contents(arg)
29 {
30 }
31
32
33 InsetCommand::~InsetCommand()
34 {
35 }
36
37
38 int InsetCommand::Ascent(LyXFont const&font) const
39 {
40         LyXFont f = font;
41         f.decSize();
42         return f.maxAscent() + 3;
43 }
44
45
46 int InsetCommand::Descent(LyXFont const&font) const
47 {
48         LyXFont f = font;
49         f.decSize();
50         return f.maxDescent() + 3;
51 }
52
53
54 int InsetCommand::Width(LyXFont const&font) const
55 {
56         LyXFont f = font;
57         f.decSize();
58         string s = getScreenLabel();
59         return 10 + f.stringWidth(s);
60 }
61
62
63 void InsetCommand::Draw(LyXFont font, LyXScreen &scr,
64                       int baseline, float &x)
65 {
66         // Draw it as a box with the LaTeX text
67         x += 3;
68
69         //scr.drawFilledRectangle(int(x), baseline - Ascent(font) + 1,
70         //           Width(font) - 2, Ascent(font) + Descent(font)-2,
71         //           FL_GRAY80);
72         scr.fillRectangle(gc_lighted,
73                           int(x), baseline - Ascent(font) + 1,
74                           Width(font) - 6,
75                           Ascent(font) + Descent(font)-2);
76         // Tell whether this slows down the drawing  (ale)
77         // lets draw editable and non-editable insets differently
78         if (Editable()) {
79                 int y = baseline - Ascent(font)+1, w = Width(font)-6,
80                         h = (Ascent(font)+Descent(font)-2);
81                 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
82         } else {
83                 //scr.drawRectangle(int(x), baseline - Ascent(font)+1,
84                 //           Width(font)-2, Ascent(font)+Descent(font)-2,
85                 //           FL_BLACK);
86                 scr.drawRectangle(gc_note_frame,
87                                   int(x), baseline - Ascent(font)+1,
88                                   Width(font)-6,
89                                   Ascent(font)+Descent(font)-2); 
90         }
91         string s = getScreenLabel();
92         LyXFont f = font;
93         f.decSize();
94         f.setColor(LyXFont::NONE);
95         f.setLatex(LyXFont::OFF);
96         scr.drawString(f, s, baseline, int(x+2));
97
98         x +=  Width(font) - 3;
99 }
100
101
102 // In lyxf3 this will be just LaTeX
103 void InsetCommand::Write(FILE *file)
104 {
105         fprintf(file, "LatexCommand %s\n", getCommand().c_str());
106 }
107
108
109 void InsetCommand::scanCommand(string const &cmd)
110 {
111         string tcommand, toptions, tcontents;
112
113         if (cmd.empty()) return;
114
115         enum { WS, Command, Option, Content } state = WS;
116         
117         // Used to handle things like \command[foo[bar]]{foo{bar}}
118         int nestdepth = 0;
119
120         for (string::size_type i = 0; i < cmd.length(); ++i) {
121                 char c = cmd[i];
122                 if ((state==Command && c == ' ') ||
123                     (state==Command && c == '[') ||
124                     (state==Command && c == '{')) {
125                         state = WS;
126                 }
127                 if ((state==Option  && c == ']') ||
128                     (state==Content && c == '}')) {
129                         if (nestdepth==0) {
130                                 state = WS;
131                         } else {
132                                 nestdepth--;
133                         }
134                 }
135                 if ((state==Option  && c == '[') ||
136                     (state==Content && c == '{')) {
137                         nestdepth++;
138                 }
139                 switch (state) {
140                 case Command:   tcommand += c; break;
141                 case Option:    toptions += c; break;
142                 case Content:   tcontents += c; break;
143                 case WS:
144                         if (c == '\\') {
145                                 state = Command;
146                         } else if (c == '[') {
147                                 state = Option;
148                                 nestdepth = 0; // Just to be sure
149                         } else if (c == '{') {
150                                 state = Content;
151                                 nestdepth = 0; // Just to be sure
152                         }
153                         break;
154                 }
155         }
156
157         // Don't mess with this.
158         if (!tcommand.empty()) command = tcommand;
159         if (!toptions.empty()) options = toptions;
160         if (!tcontents.empty()) setContents(tcontents); 
161                         // setContents is overloaded in InsetInclude
162
163         if (lyxerr.debugging(Debug::PARSER))
164                 lyxerr << "Command <" <<  cmd
165                        << "> == <" << getCommand()
166                        << "> == <" << getCmdName()
167                        << '|' << getContents()
168                        << '|' << getOptions() << '>' << endl;
169 }
170
171
172 // This function will not be necessary when lyx3
173 void InsetCommand::Read(LyXLex &lex)
174 {    
175         if (lex.EatLine()) {
176                 string t = lex.GetString();
177                 scanCommand(t);
178         } else
179                 lex.printError("InsetCommand: Parse error: `$$Token'");
180 }
181
182
183 int InsetCommand::Latex(FILE *file, signed char /*fragile*/)
184 {
185         fprintf(file, "%s", getCommand().c_str());
186         return 0;
187 }
188
189
190 int InsetCommand::Latex(string &file, signed char /*fragile*/)
191 {
192         file += getCommand();
193         return 0;
194 }
195
196
197 int InsetCommand::Linuxdoc(string &/*file*/)
198 {
199         return 0;
200 }
201
202
203 int InsetCommand::DocBook(string &/*file*/)
204 {
205         return 0;
206 }
207
208
209 Inset* InsetCommand::Clone()
210 {
211         InsetCommand *result = new InsetCommand(command, contents, options);
212         return result;
213 }
214
215
216 string InsetCommand::getCommand() const
217 {       
218         string s;
219         if (!command.empty()) s += "\\"+command;
220         if (!options.empty()) s += "["+options+']';
221         s += "{"+contents+'}';
222         return s;
223 }