]> git.lyx.org Git - features.git/blob - src/insets/insetcommand.C
the runlatex merge
[features.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.fillRectangle(gc_lighted,
70                           int(x), baseline - Ascent(font) + 1,
71                           Width(font) - 6,
72                           Ascent(font) + Descent(font)-2);
73         // Tell whether this slows down the drawing  (ale)
74         // lets draw editable and non-editable insets differently
75         if (Editable()) {
76                 int y = baseline - Ascent(font)+1, w = Width(font)-6,
77                         h = (Ascent(font)+Descent(font)-2);
78                 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
79         } else {
80                 scr.drawRectangle(gc_note_frame,
81                                   int(x), baseline - Ascent(font)+1,
82                                   Width(font)-6,
83                                   Ascent(font)+Descent(font)-2); 
84         }
85         string s = getScreenLabel();
86         LyXFont f = font;
87         f.decSize();
88         f.setColor(LyXFont::NONE);
89         f.setLatex(LyXFont::OFF);
90         scr.drawString(f, s, baseline, int(x+2));
91
92         x +=  Width(font) - 3;
93 }
94
95
96 // In lyxf3 this will be just LaTeX
97 void InsetCommand::Write(FILE * file)
98 {
99         fprintf(file, "LatexCommand %s\n", getCommand().c_str());
100 }
101
102
103 void InsetCommand::scanCommand(string const & cmd)
104 {
105         string tcommand, toptions, tcontents;
106
107         if (cmd.empty()) return;
108
109         enum { WS, Command, Option, Content } state = WS;
110         
111         // Used to handle things like \command[foo[bar]]{foo{bar}}
112         int nestdepth = 0;
113
114         for (string::size_type i = 0; i < cmd.length(); ++i) {
115                 char c = cmd[i];
116                 if ((state==Command && c == ' ') ||
117                     (state==Command && c == '[') ||
118                     (state==Command && c == '{')) {
119                         state = WS;
120                 }
121                 if ((state==Option  && c == ']') ||
122                     (state==Content && c == '}')) {
123                         if (nestdepth==0) {
124                                 state = WS;
125                         } else {
126                                 nestdepth--;
127                         }
128                 }
129                 if ((state==Option  && c == '[') ||
130                     (state==Content && c == '{')) {
131                         nestdepth++;
132                 }
133                 switch (state) {
134                 case Command:   tcommand += c; break;
135                 case Option:    toptions += c; break;
136                 case Content:   tcontents += c; break;
137                 case WS:
138                         if (c == '\\') {
139                                 state = Command;
140                         } else if (c == '[') {
141                                 state = Option;
142                                 nestdepth = 0; // Just to be sure
143                         } else if (c == '{') {
144                                 state = Content;
145                                 nestdepth = 0; // Just to be sure
146                         }
147                         break;
148                 }
149         }
150
151         // Don't mess with this.
152         if (!tcommand.empty()) command = tcommand;
153         if (!toptions.empty()) options = toptions;
154         if (!tcontents.empty()) setContents(tcontents); 
155                         // setContents is overloaded in InsetInclude
156
157         if (lyxerr.debugging(Debug::PARSER))
158                 lyxerr << "Command <" <<  cmd
159                        << "> == <" << getCommand()
160                        << "> == <" << getCmdName()
161                        << '|' << getContents()
162                        << '|' << getOptions() << '>' << endl;
163 }
164
165
166 // This function will not be necessary when lyx3
167 void InsetCommand::Read(LyXLex & lex)
168 {    
169         if (lex.EatLine()) {
170                 string t = lex.GetString();
171                 scanCommand(t);
172         } else
173                 lex.printError("InsetCommand: Parse error: `$$Token'");
174 }
175
176
177 int InsetCommand::Latex(FILE * file, signed char /*fragile*/)
178 {
179         fprintf(file, "%s", getCommand().c_str());
180         return 0;
181 }
182
183
184 int InsetCommand::Latex(string & file, signed char /*fragile*/)
185 {
186         file += getCommand();
187         return 0;
188 }
189
190
191 int InsetCommand::Linuxdoc(string &/*file*/)
192 {
193         return 0;
194 }
195
196
197 int InsetCommand::DocBook(string &/*file*/)
198 {
199         return 0;
200 }
201
202
203 Inset * InsetCommand::Clone()
204 {
205         InsetCommand * result = new InsetCommand(command, contents, options);
206         return result;
207 }
208
209
210 string InsetCommand::getCommand() const
211 {       
212         string s;
213         if (!command.empty()) s += "\\"+command;
214         if (!options.empty()) s += "["+options+']';
215         s += "{"+contents+'}';
216         return s;
217 }