1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetcommand.h"
25 InsetCommand::InsetCommand()
30 InsetCommand::InsetCommand(string const & cmd, string const & arg,
32 : command(cmd), options(opt), contents(arg)
37 int InsetCommand::ascent(Painter & pain, LyXFont const &) const
39 LyXFont font(LyXFont::ALL_SANE);
42 int width, ascent, descent;
43 string s = getScreenLabel();
46 pain.buttonText(0, 0, s, font,
47 false, width, ascent, descent);
49 pain.rectText(0, 0, s, font,
50 LColor::commandbg, LColor::commandframe,
51 false, width, ascent, descent);
57 int InsetCommand::descent(Painter & pain, LyXFont const &) const
59 LyXFont font(LyXFont::ALL_SANE);
62 int width, ascent, descent;
63 string s = getScreenLabel();
66 pain.buttonText(0, 0, s, font,
67 false, width, ascent, descent);
69 pain.rectText(0, 0, s, font,
70 LColor::commandbg, LColor::commandframe,
71 false, width, ascent, descent);
77 int InsetCommand::width(Painter & pain, LyXFont const &) const
79 LyXFont font(LyXFont::ALL_SANE);
82 int width, ascent, descent;
83 string s = getScreenLabel();
86 pain.buttonText(0, 0, s, font,
87 false, width, ascent, descent);
89 pain.rectText(0, 0, s, font,
90 LColor::commandbg, LColor::commandframe,
91 false, width, ascent, descent);
97 void InsetCommand::draw(Painter & pain, LyXFont const &,
98 int baseline, float & x) const
100 // Draw it as a box with the LaTeX text
101 LyXFont font(LyXFont::ALL_SANE);
102 font.setColor(LColor::command).decSize();
105 string s = getScreenLabel();
108 pain.buttonText(int(x)+2, baseline, s, font, true, width);
110 pain.rectText(int(x)+2, baseline, s, font,
111 LColor::commandbg, LColor::commandframe,
119 // In lyxf3 this will be just LaTeX
120 void InsetCommand::Write(ostream & os) const
122 os << "LatexCommand " << getCommand() << "\n";
126 void InsetCommand::scanCommand(string const & cmd)
128 string tcommand, toptions, tcontents;
130 if (cmd.empty()) return;
132 enum { WS, Command, Option, Content } state = WS;
134 // Used to handle things like \command[foo[bar]]{foo{bar}}
137 for (string::size_type i = 0; i < cmd.length(); ++i) {
139 if ((state == Command && c == ' ') ||
140 (state == Command && c == '[') ||
141 (state == Command && c == '{')) {
144 if ((state == Option && c == ']') ||
145 (state == Content && c == '}')) {
146 if (nestdepth == 0) {
152 if ((state == Option && c == '[') ||
153 (state == Content && c == '{')) {
157 case Command: tcommand += c; break;
158 case Option: toptions += c; break;
159 case Content: tcontents += c; break;
163 } else if (c == '[') {
165 nestdepth = 0; // Just to be sure
166 } else if (c == '{') {
168 nestdepth = 0; // Just to be sure
174 // Don't mess with this.
175 if (!tcommand.empty()) command = tcommand;
176 if (!toptions.empty()) options = toptions;
177 if (!tcontents.empty()) setContents(tcontents);
178 // setContents is overloaded in InsetInclude
180 if (lyxerr.debugging(Debug::PARSER))
181 lyxerr << "Command <" << cmd
182 << "> == <" << getCommand()
183 << "> == <" << getCmdName()
184 << '|' << getContents()
185 << '|' << getOptions() << '>' << endl;
189 // This function will not be necessary when lyx3
190 void InsetCommand::Read(LyXLex & lex)
193 string t = lex.GetString();
196 lex.printError("InsetCommand: Parse error: `$$Token'");
200 int InsetCommand::Latex(ostream & os, signed char /*fragile*/, bool/*fs*/) const
207 int InsetCommand::Linuxdoc(ostream &) const
213 int InsetCommand::DocBook(ostream &) const
219 Inset * InsetCommand::Clone() const
221 return new InsetCommand(command, contents, options);
225 string InsetCommand::getCommand() const
228 if (!command.empty()) s += "\\"+command;
229 if (!options.empty()) s += "["+options+']';
230 s += "{"+contents+'}';