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.drawFilledRectangle(int(x), baseline - Ascent(font) + 1,
70 // Width(font) - 2, Ascent(font) + Descent(font)-2,
72 scr.fillRectangle(gc_lighted,
73 int(x), baseline - Ascent(font) + 1,
75 Ascent(font) + Descent(font)-2);
76 // Tell whether this slows down the drawing (ale)
77 // lets draw editable and non-editable insets differently
79 int y = baseline - Ascent(font)+1, w = Width(font)-6,
80 h = (Ascent(font)+Descent(font)-2);
81 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
83 //scr.drawRectangle(int(x), baseline - Ascent(font)+1,
84 // Width(font)-2, Ascent(font)+Descent(font)-2,
86 scr.drawRectangle(gc_note_frame,
87 int(x), baseline - Ascent(font)+1,
89 Ascent(font)+Descent(font)-2);
91 string s = getScreenLabel();
94 f.setColor(LyXFont::NONE);
95 f.setLatex(LyXFont::OFF);
96 scr.drawString(f, s, baseline, int(x+2));
102 // In lyxf3 this will be just LaTeX
103 void InsetCommand::Write(FILE *file)
105 fprintf(file, "LatexCommand %s\n", getCommand().c_str());
109 void InsetCommand::scanCommand(string const &cmd)
111 string tcommand, toptions, tcontents;
113 if (cmd.empty()) return;
115 enum { WS, Command, Option, Content } state = WS;
117 // Used to handle things like \command[foo[bar]]{foo{bar}}
120 for (string::size_type i = 0; i < cmd.length(); ++i) {
122 if ((state==Command && c == ' ') ||
123 (state==Command && c == '[') ||
124 (state==Command && c == '{')) {
127 if ((state==Option && c == ']') ||
128 (state==Content && c == '}')) {
135 if ((state==Option && c == '[') ||
136 (state==Content && c == '{')) {
140 case Command: tcommand += c; break;
141 case Option: toptions += c; break;
142 case Content: tcontents += c; break;
146 } else if (c == '[') {
148 nestdepth = 0; // Just to be sure
149 } else if (c == '{') {
151 nestdepth = 0; // Just to be sure
157 // Don't mess with this.
158 if (!tcommand.empty()) command = tcommand;
159 if (!toptions.empty()) options = toptions;
160 if (!tcontents.empty()) setContents(tcontents);
161 // setContents is overloaded in InsetInclude
163 if (lyxerr.debugging(Error::LEX_PARSER))
164 lyxerr.print("Command <" + cmd
165 + "> == <" + getCommand()
166 + "> == <" + getCmdName()
167 + '|' + getContents()
168 + '|' + getOptions() +'>');
172 // This function will not be necessary when lyx3
173 void InsetCommand::Read(LyXLex &lex)
176 string t = lex.GetString();
179 lex.printError("InsetCommand: Parse error: `$$Token'");
183 int InsetCommand::Latex(FILE *file, signed char /*fragile*/)
185 fprintf(file, "%s", getCommand().c_str());
190 int InsetCommand::Latex(string &file, signed char /*fragile*/)
192 file += getCommand();
197 int InsetCommand::Linuxdoc(string &/*file*/)
203 int InsetCommand::DocBook(string &/*file*/)
209 Inset* InsetCommand::Clone()
211 InsetCommand *result = new InsetCommand(command, contents, options);
216 string InsetCommand::getCommand() const
219 if (!command.empty()) s += "\\"+command;
220 if (!options.empty()) s += "["+options+']';
221 s += "{"+contents+'}';