1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 *======================================================*/
14 #pragma implementation
17 #include "insetcommand.h"
21 // $Id: insetcommand.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $
23 #if !defined(lint) && !defined(WITH_WARNINGS)
24 static char vcid[] = "$Id: insetcommand.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $";
28 InsetCommand::InsetCommand()
33 InsetCommand::InsetCommand(LString const & cmd, LString const & arg,
35 : command(cmd), options(opt), contents(arg)
40 InsetCommand::~InsetCommand()
45 int InsetCommand::Ascent(LyXFont const&font) const
49 return f.maxAscent() + 3;
53 int InsetCommand::Descent(LyXFont const&font) const
57 return f.maxDescent() + 3;
61 int InsetCommand::Width(LyXFont const&font) const
65 LString s = getScreenLabel();
66 return 10 + f.stringWidth(s);
70 void InsetCommand::Draw(LyXFont font, LyXScreen &scr,
71 int baseline, float &x)
73 // Draw it as a box with the LaTeX text
76 //scr.drawFilledRectangle(int(x), baseline - Ascent(font) + 1,
77 // Width(font) - 2, Ascent(font) + Descent(font)-2,
79 scr.fillRectangle(gc_lighted,
80 int(x), baseline - Ascent(font) + 1,
82 Ascent(font) + Descent(font)-2);
83 // Tell whether this slows down the drawing (ale)
84 // lets draw editable and non-editable insets differently
86 int y = baseline - Ascent(font)+1, w = Width(font)-6,
87 h = (Ascent(font)+Descent(font)-2);
88 scr.drawFrame(FL_UP_FRAME, int(x), y, w, h, FL_BLACK, -1);
90 //scr.drawRectangle(int(x), baseline - Ascent(font)+1,
91 // Width(font)-2, Ascent(font)+Descent(font)-2,
93 scr.drawRectangle(gc_note_frame,
94 int(x), baseline - Ascent(font)+1,
96 Ascent(font)+Descent(font)-2);
98 LString s = getScreenLabel();
101 f.setColor(LyXFont::NONE);
102 f.setLatex(LyXFont::OFF);
103 scr.drawString(f, s, baseline, int(x+2));
105 x += Width(font) - 3;
109 // In lyxf3 this will be just LaTeX
110 void InsetCommand::Write(FILE *file)
112 fprintf(file, "LatexCommand %s\n", getCommand().c_str());
116 void InsetCommand::scanCommand(LString const &cmd)
118 LString tcommand, toptions, tcontents;
120 if (cmd.empty()) return;
122 enum { WS, Command, Option, Content } state = WS;
124 // Used to handle things like \command[foo[bar]]{foo{bar}}
127 for (int i=0; i<cmd.length(); i++) {
129 if ((state==Command && c == ' ') ||
130 (state==Command && c == '[') ||
131 (state==Command && c == '{')) {
134 if ((state==Option && c == ']') ||
135 (state==Content && c == '}')) {
142 if ((state==Option && c == '[') ||
143 (state==Content && c == '{')) {
147 case Command: tcommand += c; break;
148 case Option: toptions += c; break;
149 case Content: tcontents += c; break;
153 } else if (c == '[') {
155 nestdepth = 0; // Just to be sure
156 } else if (c == '{') {
158 nestdepth = 0; // Just to be sure
164 // Don't mess with this.
165 if (!tcommand.empty()) command = tcommand;
166 if (!toptions.empty()) options = toptions;
167 if (!tcontents.empty()) setContents(tcontents);
168 // setContents is overloaded in InsetInclude
170 if (lyxerr.debugging(Error::LEX_PARSER))
171 lyxerr.print("Command <" + cmd
172 + "> == <" + getCommand()
173 + "> == <" + getCmdName()
174 + '|' + getContents()
175 + '|' + getOptions() +'>');
179 // This function will not be necessary when lyx3
180 void InsetCommand::Read(LyXLex &lex)
183 LString t = lex.GetString();
186 lex.printError("InsetCommand: Parse error: `$$Token'");
190 int InsetCommand::Latex(FILE *file, signed char /*fragile*/)
192 fprintf(file, "%s", getCommand().c_str());
197 int InsetCommand::Latex(LString &file, signed char /*fragile*/)
199 file += getCommand();
204 int InsetCommand::Linuxdoc(LString &/*file*/)
210 int InsetCommand::DocBook(LString &/*file*/)
216 Inset* InsetCommand::Clone()
218 InsetCommand *result = new InsetCommand(command, contents, options);
223 LString InsetCommand::getCommand() const
226 if (!command.empty()) s += "\\"+command;
227 if (!options.empty()) s += "["+options+']';
228 s += "{"+contents+'}';