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 InsetCommand::~InsetCommand()
38 int InsetCommand::Ascent(LyXFont const & font) const
42 return f.maxAscent() + 3;
46 int InsetCommand::Descent(LyXFont const & font) const
50 return f.maxDescent() + 3;
54 int InsetCommand::Width(LyXFont const & font) const
58 string s = getScreenLabel();
59 return 10 + f.stringWidth(s);
63 void InsetCommand::Draw(LyXFont font, LyXScreen & scr,
64 int baseline, float & x)
66 // Draw it as a box with the LaTeX text
69 scr.fillRectangle(gc_lighted,
70 int(x), baseline - Ascent(font) + 1,
72 Ascent(font) + Descent(font)-2);
73 // Tell whether this slows down the drawing (ale)
74 // lets draw editable and non-editable insets differently
76 int y = baseline - Ascent(font)+1, w = Width(font)-6,
77 h = (Ascent(font)+Descent(font)-2);
78 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
80 scr.drawRectangle(gc_note_frame,
81 int(x), baseline - Ascent(font)+1,
83 Ascent(font)+Descent(font)-2);
85 string s = getScreenLabel();
88 f.setColor(LyXFont::NONE);
89 f.setLatex(LyXFont::OFF);
90 scr.drawString(f, s, baseline, int(x+2));
96 // In lyxf3 this will be just LaTeX
97 void InsetCommand::Write(FILE * file)
99 fprintf(file, "LatexCommand %s\n", getCommand().c_str());
103 void InsetCommand::scanCommand(string const & cmd)
105 string tcommand, toptions, tcontents;
107 if (cmd.empty()) return;
109 enum { WS, Command, Option, Content } state = WS;
111 // Used to handle things like \command[foo[bar]]{foo{bar}}
114 for (string::size_type i = 0; i < cmd.length(); ++i) {
116 if ((state==Command && c == ' ') ||
117 (state==Command && c == '[') ||
118 (state==Command && c == '{')) {
121 if ((state==Option && c == ']') ||
122 (state==Content && c == '}')) {
129 if ((state==Option && c == '[') ||
130 (state==Content && c == '{')) {
134 case Command: tcommand += c; break;
135 case Option: toptions += c; break;
136 case Content: tcontents += c; break;
140 } else if (c == '[') {
142 nestdepth = 0; // Just to be sure
143 } else if (c == '{') {
145 nestdepth = 0; // Just to be sure
151 // Don't mess with this.
152 if (!tcommand.empty()) command = tcommand;
153 if (!toptions.empty()) options = toptions;
154 if (!tcontents.empty()) setContents(tcontents);
155 // setContents is overloaded in InsetInclude
157 if (lyxerr.debugging(Debug::PARSER))
158 lyxerr << "Command <" << cmd
159 << "> == <" << getCommand()
160 << "> == <" << getCmdName()
161 << '|' << getContents()
162 << '|' << getOptions() << '>' << endl;
166 // This function will not be necessary when lyx3
167 void InsetCommand::Read(LyXLex & lex)
170 string t = lex.GetString();
173 lex.printError("InsetCommand: Parse error: `$$Token'");
177 int InsetCommand::Latex(FILE * file, signed char /*fragile*/)
179 fprintf(file, "%s", getCommand().c_str());
184 int InsetCommand::Latex(string & file, signed char /*fragile*/)
186 file += getCommand();
191 int InsetCommand::Linuxdoc(string &/*file*/)
197 int InsetCommand::DocBook(string &/*file*/)
203 Inset * InsetCommand::Clone()
205 InsetCommand * result = new InsetCommand(command, contents, options);
210 string InsetCommand::getCommand() const
213 if (!command.empty()) s += "\\"+command;
214 if (!options.empty()) s += "["+options+']';
215 s += "{"+contents+'}';