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"
24 InsetCommand::InsetCommand()
29 InsetCommand::InsetCommand(string const & cmd, string const & arg,
31 : command(cmd), options(opt), contents(arg)
36 int InsetCommand::ascent(Painter & pain, LyXFont const &) const
38 LyXFont font(LyXFont::ALL_SANE);
41 int width, ascent, descent;
42 string s = getScreenLabel();
45 pain.buttonText(0, 0, s, font,
46 false, width, ascent, descent);
48 pain.rectText(0, 0, s, font,
49 LColor::commandbg, LColor::commandframe,
50 false, width, ascent, descent);
56 int InsetCommand::descent(Painter & pain, LyXFont const &) const
58 LyXFont font(LyXFont::ALL_SANE);
61 int width, ascent, descent;
62 string s = getScreenLabel();
65 pain.buttonText(0, 0, s, font,
66 false, width, ascent, descent);
68 pain.rectText(0, 0, s, font,
69 LColor::commandbg, LColor::commandframe,
70 false, width, ascent, descent);
76 int InsetCommand::width(Painter & pain, LyXFont const &) const
78 LyXFont font(LyXFont::ALL_SANE);
81 int width, ascent, descent;
82 string s = getScreenLabel();
85 pain.buttonText(0, 0, s, font,
86 false, width, ascent, descent);
88 pain.rectText(0, 0, s, font,
89 LColor::commandbg, LColor::commandframe,
90 false, width, ascent, descent);
96 void InsetCommand::draw(Painter & pain, LyXFont const &,
97 int baseline, float & x) const
99 // Draw it as a box with the LaTeX text
100 LyXFont font(LyXFont::ALL_SANE);
101 font.setColor(LColor::command).decSize();
104 string s = getScreenLabel();
107 pain.buttonText(int(x)+2, baseline, s, font, true, width);
109 pain.rectText(int(x)+2, baseline, s, font,
110 LColor::commandbg, LColor::commandframe,
118 // In lyxf3 this will be just LaTeX
119 void InsetCommand::Write(ostream & os) const
121 os << "LatexCommand " << getCommand() << "\n";
125 void InsetCommand::scanCommand(string const & cmd)
127 string tcommand, toptions, tcontents;
129 if (cmd.empty()) return;
131 enum { WS, Command, Option, Content } state = WS;
133 // Used to handle things like \command[foo[bar]]{foo{bar}}
136 for (string::size_type i = 0; i < cmd.length(); ++i) {
138 if ((state == Command && c == ' ') ||
139 (state == Command && c == '[') ||
140 (state == Command && c == '{')) {
143 if ((state == Option && c == ']') ||
144 (state == Content && c == '}')) {
145 if (nestdepth == 0) {
151 if ((state == Option && c == '[') ||
152 (state == Content && c == '{')) {
156 case Command: tcommand += c; break;
157 case Option: toptions += c; break;
158 case Content: tcontents += c; break;
162 } else if (c == '[') {
164 nestdepth = 0; // Just to be sure
165 } else if (c == '{') {
167 nestdepth = 0; // Just to be sure
173 // Don't mess with this.
174 if (!tcommand.empty()) command = tcommand;
175 if (!toptions.empty()) options = toptions;
176 if (!tcontents.empty()) setContents(tcontents);
177 // setContents is overloaded in InsetInclude
179 if (lyxerr.debugging(Debug::PARSER))
180 lyxerr << "Command <" << cmd
181 << "> == <" << getCommand()
182 << "> == <" << getCmdName()
183 << '|' << getContents()
184 << '|' << getOptions() << '>' << endl;
188 // This function will not be necessary when lyx3
189 void InsetCommand::Read(LyXLex & lex)
192 string t = lex.GetString();
195 lex.printError("InsetCommand: Parse error: `$$Token'");
199 int InsetCommand::Latex(ostream & os, signed char /*fragile*/, bool/*fs*/) const
206 int InsetCommand::Linuxdoc(ostream &) const
212 int InsetCommand::DocBook(ostream &) const
218 Inset * InsetCommand::Clone() const
220 return new InsetCommand(command, contents, options);
224 string InsetCommand::getCommand() const
227 if (!command.empty()) s += "\\"+command;
228 if (!options.empty()) s += "["+options+']';
229 s += "{"+contents+'}';