1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetcommand.h"
22 InsetCommand::InsetCommand()
27 InsetCommand::InsetCommand(string const & cmd, string const & arg,
29 : command(cmd), options(opt), contents(arg)
35 int InsetCommand::ascent(Painter &, LyXFont const & font) const
39 return f.maxAscent() + 3;
42 int InsetCommand::Ascent(LyXFont const & font) const
46 return f.maxAscent() + 3;
52 int InsetCommand::descent(Painter &, LyXFont const & font) const
56 return f.maxDescent() + 3;
59 int InsetCommand::Descent(LyXFont const & font) const
63 return f.maxDescent() + 3;
69 int InsetCommand::width(Painter &, LyXFont const & font) const
73 string s = getScreenLabel();
74 return 10 + f.stringWidth(s);
77 int InsetCommand::Width(LyXFont const & font) const
81 string s = getScreenLabel();
82 return 10 + f.stringWidth(s);
88 void InsetCommand::draw(Painter & pain, LyXFont const & font,
89 int baseline, float & x) const
91 // Draw it as a box with the LaTeX text
94 pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
95 width(pain, font) - 6,
96 ascent(pain, font) + descent(pain, font) - 2,
98 // Tell whether this slows down the drawing (ale)
99 // lets draw editable and non-editable insets differently
101 int y = baseline - ascent(pain, font) + 1;
102 int w = width(pain, font) - 6;
103 int h = ascent(pain, font) + descent(pain, font) - 2;
104 pain.rectangle(int(x), y, w, h, LColor::insetframe);
107 pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
108 width(pain, font) - 6,
109 ascent(pain, font) + descent(pain, font) - 2,
112 string s = getScreenLabel();
115 f.setColor(LColor::none);
116 f.setLatex(LyXFont::OFF);
117 pain.text(int(x + 2), baseline, s, f);
119 x += width(pain, font) - 3;
122 void InsetCommand::Draw(LyXFont font, LyXScreen & scr,
123 int baseline, float & x)
125 // Draw it as a box with the LaTeX text
128 scr.fillRectangle(gc_lighted,
129 int(x), baseline - Ascent(font) + 1,
131 Ascent(font) + Descent(font)-2);
132 // Tell whether this slows down the drawing (ale)
133 // lets draw editable and non-editable insets differently
135 int y = baseline - Ascent(font)+1, w = Width(font)-6,
136 h = (Ascent(font)+Descent(font)-2);
137 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
139 scr.drawRectangle(gc_note_frame,
140 int(x), baseline - Ascent(font)+1,
142 Ascent(font)+Descent(font)-2);
144 string s = getScreenLabel();
147 f.setColor(LyXFont::NONE);
148 f.setLatex(LyXFont::OFF);
149 scr.drawString(f, s, baseline, int(x+2));
151 x += Width(font) - 3;
156 // In lyxf3 this will be just LaTeX
157 void InsetCommand::Write(ostream & os)
159 os << "LatexCommand " << getCommand() << "\n";
163 void InsetCommand::scanCommand(string const & cmd)
165 string tcommand, toptions, tcontents;
167 if (cmd.empty()) return;
169 enum { WS, Command, Option, Content } state = WS;
171 // Used to handle things like \command[foo[bar]]{foo{bar}}
174 for (string::size_type i = 0; i < cmd.length(); ++i) {
176 if ((state == Command && c == ' ') ||
177 (state == Command && c == '[') ||
178 (state == Command && c == '{')) {
181 if ((state == Option && c == ']') ||
182 (state == Content && c == '}')) {
183 if (nestdepth == 0) {
189 if ((state == Option && c == '[') ||
190 (state == Content && c == '{')) {
194 case Command: tcommand += c; break;
195 case Option: toptions += c; break;
196 case Content: tcontents += c; break;
200 } else if (c == '[') {
202 nestdepth = 0; // Just to be sure
203 } else if (c == '{') {
205 nestdepth = 0; // Just to be sure
211 // Don't mess with this.
212 if (!tcommand.empty()) command = tcommand;
213 if (!toptions.empty()) options = toptions;
214 if (!tcontents.empty()) setContents(tcontents);
215 // setContents is overloaded in InsetInclude
217 if (lyxerr.debugging(Debug::PARSER))
218 lyxerr << "Command <" << cmd
219 << "> == <" << getCommand()
220 << "> == <" << getCmdName()
221 << '|' << getContents()
222 << '|' << getOptions() << '>' << endl;
226 // This function will not be necessary when lyx3
227 void InsetCommand::Read(LyXLex & lex)
230 string t = lex.GetString();
233 lex.printError("InsetCommand: Parse error: `$$Token'");
237 int InsetCommand::Latex(ostream & os, signed char /*fragile*/)
244 int InsetCommand::Latex(string & file, signed char /*fragile*/)
246 file += getCommand();
251 int InsetCommand::Linuxdoc(string &/*file*/)
257 int InsetCommand::DocBook(string &/*file*/)
263 Inset * InsetCommand::Clone() const
265 return new InsetCommand(command, contents, options);
269 string InsetCommand::getCommand() const
272 if (!command.empty()) s += "\\"+command;
273 if (!options.empty()) s += "["+options+']';
274 s += "{"+contents+'}';