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"
24 InsetCommand::InsetCommand()
29 InsetCommand::InsetCommand(string const & cmd, string const & arg,
31 : command(cmd), options(opt), contents(arg)
37 int InsetCommand::ascent(Painter &, LyXFont const & font) const
41 return f.maxAscent() + 3;
44 int InsetCommand::Ascent(LyXFont const & font) const
48 return f.maxAscent() + 3;
54 int InsetCommand::descent(Painter &, LyXFont const & font) const
58 return f.maxDescent() + 3;
61 int InsetCommand::Descent(LyXFont const & font) const
65 return f.maxDescent() + 3;
71 int InsetCommand::width(Painter &, LyXFont const & font) const
75 string s = getScreenLabel();
76 return 10 + f.stringWidth(s);
79 int InsetCommand::Width(LyXFont const & font) const
83 string s = getScreenLabel();
84 return 10 + f.stringWidth(s);
90 void InsetCommand::draw(Painter & pain, LyXFont const & font,
91 int baseline, float & x) const
93 // Draw it as a box with the LaTeX text
96 pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
97 width(pain, font) - 6,
98 ascent(pain, font) + descent(pain, font) - 2,
100 // Tell whether this slows down the drawing (ale)
101 // lets draw editable and non-editable insets differently
103 int y = baseline - ascent(pain, font) + 1;
104 int w = width(pain, font) - 6;
105 int h = ascent(pain, font) + descent(pain, font) - 2;
106 pain.rectangle(int(x), y, w, h, LColor::insetframe);
109 pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
110 width(pain, font) - 6,
111 ascent(pain, font) + descent(pain, font) - 2,
114 string s = getScreenLabel();
117 f.setColor(LColor::none);
118 f.setLatex(LyXFont::OFF);
119 pain.text(int(x + 2), baseline, s, f);
121 x += width(pain, font) - 3;
124 void InsetCommand::Draw(LyXFont font, LyXScreen & scr,
125 int baseline, float & x)
127 // Draw it as a box with the LaTeX text
130 scr.fillRectangle(gc_lighted,
131 int(x), baseline - Ascent(font) + 1,
133 Ascent(font) + Descent(font)-2);
134 // Tell whether this slows down the drawing (ale)
135 // lets draw editable and non-editable insets differently
137 int y = baseline - Ascent(font)+1, w = Width(font)-6,
138 h = (Ascent(font)+Descent(font)-2);
139 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
141 scr.drawRectangle(gc_note_frame,
142 int(x), baseline - Ascent(font)+1,
144 Ascent(font)+Descent(font)-2);
146 string s = getScreenLabel();
149 f.setColor(LyXFont::NONE);
150 f.setLatex(LyXFont::OFF);
151 scr.drawString(f, s, baseline, int(x+2));
153 x += Width(font) - 3;
158 // In lyxf3 this will be just LaTeX
159 void InsetCommand::Write(ostream & os)
161 os << "LatexCommand " << getCommand() << "\n";
165 void InsetCommand::scanCommand(string const & cmd)
167 string tcommand, toptions, tcontents;
169 if (cmd.empty()) return;
171 enum { WS, Command, Option, Content } state = WS;
173 // Used to handle things like \command[foo[bar]]{foo{bar}}
176 for (string::size_type i = 0; i < cmd.length(); ++i) {
178 if ((state == Command && c == ' ') ||
179 (state == Command && c == '[') ||
180 (state == Command && c == '{')) {
183 if ((state == Option && c == ']') ||
184 (state == Content && c == '}')) {
185 if (nestdepth == 0) {
191 if ((state == Option && c == '[') ||
192 (state == Content && c == '{')) {
196 case Command: tcommand += c; break;
197 case Option: toptions += c; break;
198 case Content: tcontents += c; break;
202 } else if (c == '[') {
204 nestdepth = 0; // Just to be sure
205 } else if (c == '{') {
207 nestdepth = 0; // Just to be sure
213 // Don't mess with this.
214 if (!tcommand.empty()) command = tcommand;
215 if (!toptions.empty()) options = toptions;
216 if (!tcontents.empty()) setContents(tcontents);
217 // setContents is overloaded in InsetInclude
219 if (lyxerr.debugging(Debug::PARSER))
220 lyxerr << "Command <" << cmd
221 << "> == <" << getCommand()
222 << "> == <" << getCmdName()
223 << '|' << getContents()
224 << '|' << getOptions() << '>' << endl;
228 // This function will not be necessary when lyx3
229 void InsetCommand::Read(LyXLex & lex)
232 string t = lex.GetString();
235 lex.printError("InsetCommand: Parse error: `$$Token'");
239 int InsetCommand::Latex(ostream & os, signed char /*fragile*/)
246 int InsetCommand::Latex(string & file, signed char /*fragile*/)
248 file += getCommand();
253 int InsetCommand::Linuxdoc(string &/*file*/)
259 int InsetCommand::DocBook(string &/*file*/)
265 Inset * InsetCommand::Clone() const
267 return new InsetCommand(command, contents, options);
271 string InsetCommand::getCommand() const
274 if (!command.empty()) s += "\\"+command;
275 if (!options.empty()) s += "["+options+']';
276 s += "{"+contents+'}';