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"
21 InsetCommand::InsetCommand()
26 InsetCommand::InsetCommand(string const & cmd, string const & arg,
28 : command(cmd), options(opt), contents(arg)
33 int InsetCommand::ascent(Painter & pain, LyXFont const &) const
36 LyXFont font(LyXFont::ALL_SANE);
39 int width, ascent, descent;
40 string s = getScreenLabel();
43 pain.buttonText(0, 0, s, font,
44 false, width, ascent, descent);
46 pain.rectText(0, 0, s, font,
47 LColor::commandbg, LColor::commandframe,
48 false, width, ascent, descent);
54 return f.maxAscent() + 3;
59 int InsetCommand::descent(Painter & pain, LyXFont const &) const
62 LyXFont font(LyXFont::ALL_SANE);
65 int width, ascent, descent;
66 string s = getScreenLabel();
69 pain.buttonText(0, 0, s, font,
70 false, width, ascent, descent);
72 pain.rectText(0, 0, s, font,
73 LColor::commandbg, LColor::commandframe,
74 false, width, ascent, descent);
80 return f.maxDescent() + 3;
85 int InsetCommand::width(Painter & pain, LyXFont const &) const
88 LyXFont font(LyXFont::ALL_SANE);
91 int width, ascent, descent;
92 string s = getScreenLabel();
95 pain.buttonText(0, 0, s, font,
96 false, width, ascent, descent);
98 pain.rectText(0, 0, s, font,
99 LColor::commandbg, LColor::commandframe,
100 false, width, ascent, descent);
106 string s = getScreenLabel();
107 return 10 + f.stringWidth(s);
112 void InsetCommand::draw(Painter & pain, LyXFont const &,
113 int baseline, float & x) const
115 // Draw it as a box with the LaTeX text
117 LyXFont font(LyXFont::ALL_SANE);
118 font.setColor(LColor::command).decSize();
121 string s = getScreenLabel();
124 pain.buttonText(int(x), baseline, s, font, true, width);
126 pain.rectText(int(x), baseline, s, font,
127 LColor::commandbg, LColor::commandframe,
136 pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
137 width(pain, font) - 6,
138 ascent(pain, font) + descent(pain, font) - 2,
140 // Tell whether this slows down the drawing (ale)
141 // lets draw editable and non-editable insets differently
143 int y = baseline - ascent(pain, font) + 1;
144 int w = width(pain, font) - 6;
145 int h = ascent(pain, font) + descent(pain, font) - 2;
146 pain.rectangle(int(x), y, w, h, LColor::insetframe);
149 pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
150 width(pain, font) - 6,
151 ascent(pain, font) + descent(pain, font) - 2,
154 string s = getScreenLabel();
157 f.setColor(LColor::none);
158 f.setLatex(LyXFont::OFF);
159 pain.text(int(x + 2), baseline, s, f);
161 x += width(pain, font) - 3;
166 // In lyxf3 this will be just LaTeX
167 void InsetCommand::Write(ostream & os) const
169 os << "LatexCommand " << getCommand() << "\n";
173 void InsetCommand::scanCommand(string const & cmd)
175 string tcommand, toptions, tcontents;
177 if (cmd.empty()) return;
179 enum { WS, Command, Option, Content } state = WS;
181 // Used to handle things like \command[foo[bar]]{foo{bar}}
184 for (string::size_type i = 0; i < cmd.length(); ++i) {
186 if ((state == Command && c == ' ') ||
187 (state == Command && c == '[') ||
188 (state == Command && c == '{')) {
191 if ((state == Option && c == ']') ||
192 (state == Content && c == '}')) {
193 if (nestdepth == 0) {
199 if ((state == Option && c == '[') ||
200 (state == Content && c == '{')) {
204 case Command: tcommand += c; break;
205 case Option: toptions += c; break;
206 case Content: tcontents += c; break;
210 } else if (c == '[') {
212 nestdepth = 0; // Just to be sure
213 } else if (c == '{') {
215 nestdepth = 0; // Just to be sure
221 // Don't mess with this.
222 if (!tcommand.empty()) command = tcommand;
223 if (!toptions.empty()) options = toptions;
224 if (!tcontents.empty()) setContents(tcontents);
225 // setContents is overloaded in InsetInclude
227 if (lyxerr.debugging(Debug::PARSER))
228 lyxerr << "Command <" << cmd
229 << "> == <" << getCommand()
230 << "> == <" << getCmdName()
231 << '|' << getContents()
232 << '|' << getOptions() << '>' << endl;
236 // This function will not be necessary when lyx3
237 void InsetCommand::Read(LyXLex & lex)
240 string t = lex.GetString();
243 lex.printError("InsetCommand: Parse error: `$$Token'");
247 int InsetCommand::Latex(ostream & os, signed char /*fragile*/) const
254 int InsetCommand::Latex(string & file, signed char /*fragile*/) const
256 file += getCommand();
261 int InsetCommand::Linuxdoc(string &/*file*/) const
267 int InsetCommand::DocBook(string &/*file*/) const
273 Inset * InsetCommand::Clone() const
275 return new InsetCommand(command, contents, options);
279 string InsetCommand::getCommand() const
282 if (!command.empty()) s += "\\"+command;
283 if (!options.empty()) s += "["+options+']';
284 s += "{"+contents+'}';