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(LyXFont const & font) const
37 return f.maxAscent() + 3;
41 int InsetCommand::Descent(LyXFont const & font) const
45 return f.maxDescent() + 3;
49 int InsetCommand::Width(LyXFont const & font) const
53 string s = getScreenLabel();
54 return 10 + f.stringWidth(s);
58 void InsetCommand::Draw(LyXFont font, LyXScreen & scr,
59 int baseline, float & x)
61 // Draw it as a box with the LaTeX text
64 scr.fillRectangle(gc_lighted,
65 int(x), baseline - Ascent(font) + 1,
67 Ascent(font) + Descent(font)-2);
68 // Tell whether this slows down the drawing (ale)
69 // lets draw editable and non-editable insets differently
71 int y = baseline - Ascent(font)+1, w = Width(font)-6,
72 h = (Ascent(font)+Descent(font)-2);
73 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
75 scr.drawRectangle(gc_note_frame,
76 int(x), baseline - Ascent(font)+1,
78 Ascent(font)+Descent(font)-2);
80 string s = getScreenLabel();
83 f.setColor(LyXFont::NONE);
84 f.setLatex(LyXFont::OFF);
85 scr.drawString(f, s, baseline, int(x+2));
91 // In lyxf3 this will be just LaTeX
92 void InsetCommand::Write(FILE * file)
94 fprintf(file, "LatexCommand %s\n", getCommand().c_str());
98 void InsetCommand::scanCommand(string const & cmd)
100 string tcommand, toptions, tcontents;
102 if (cmd.empty()) return;
104 enum { WS, Command, Option, Content } state = WS;
106 // Used to handle things like \command[foo[bar]]{foo{bar}}
109 for (string::size_type i = 0; i < cmd.length(); ++i) {
111 if ((state == Command && c == ' ') ||
112 (state == Command && c == '[') ||
113 (state == Command && c == '{')) {
116 if ((state == Option && c == ']') ||
117 (state == Content && c == '}')) {
118 if (nestdepth == 0) {
124 if ((state == Option && c == '[') ||
125 (state == Content && c == '{')) {
129 case Command: tcommand += c; break;
130 case Option: toptions += c; break;
131 case Content: tcontents += c; break;
135 } else if (c == '[') {
137 nestdepth = 0; // Just to be sure
138 } else if (c == '{') {
140 nestdepth = 0; // Just to be sure
146 // Don't mess with this.
147 if (!tcommand.empty()) command = tcommand;
148 if (!toptions.empty()) options = toptions;
149 if (!tcontents.empty()) setContents(tcontents);
150 // setContents is overloaded in InsetInclude
152 if (lyxerr.debugging(Debug::PARSER))
153 lyxerr << "Command <" << cmd
154 << "> == <" << getCommand()
155 << "> == <" << getCmdName()
156 << '|' << getContents()
157 << '|' << getOptions() << '>' << endl;
161 // This function will not be necessary when lyx3
162 void InsetCommand::Read(LyXLex & lex)
165 string t = lex.GetString();
168 lex.printError("InsetCommand: Parse error: `$$Token'");
172 int InsetCommand::Latex(FILE * file, signed char /*fragile*/)
174 fprintf(file, "%s", getCommand().c_str());
179 int InsetCommand::Latex(string & file, signed char /*fragile*/)
181 file += getCommand();
186 int InsetCommand::Linuxdoc(string &/*file*/)
192 int InsetCommand::DocBook(string &/*file*/)
198 Inset * InsetCommand::Clone() const
200 return new InsetCommand(command, contents, options);
204 string InsetCommand::getCommand() const
207 if (!command.empty()) s += "\\"+command;
208 if (!options.empty()) s += "["+options+']';
209 s += "{"+contents+'}';