3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "funcrequest.h"
16 #include "FloatList.h"
18 #include "BufferView.h"
21 #include "insets/insetbibitem.h"
22 #include "insets/insetbibtex.h"
23 #include "insets/insetcaption.h"
24 #include "insets/insetcite.h"
25 #include "insets/insetenv.h"
26 #include "insets/insetert.h"
27 #include "insets/insetexternal.h"
28 #include "insets/insetfloat.h"
29 #include "insets/insetfloatlist.h"
30 #include "insets/insetfoot.h"
31 #include "insets/insetgraphics.h"
32 #include "insets/insethfill.h"
33 #include "insets/insetinclude.h"
34 #include "insets/insetindex.h"
35 #include "insets/insetlabel.h"
36 #include "insets/insetmarginal.h"
37 #include "insets/insetminipage.h"
38 #include "insets/insetnote.h"
39 #include "insets/insetbranch.h"
40 #include "insets/insetoptarg.h"
41 #include "insets/insetref.h"
42 #include "insets/insetspace.h"
43 #include "insets/insettabular.h"
44 #include "insets/insettoc.h"
45 #include "insets/inseturl.h"
46 #include "insets/insetwrap.h"
47 #include "mathed/formulamacro.h"
48 #include "mathed/formula.h"
50 #include "frontends/Dialogs.h"
51 #include "frontends/LyXView.h"
52 #include "support/lstrings.h"
55 using namespace lyx::support;
59 InsetOld * createInset(FuncRequest const & cmd)
61 BufferView * bv = cmd.view();
62 BufferParams const & params = bv->buffer()->params;
66 return new InsetHFill();
68 case LFUN_INSET_MINIPAGE:
69 return new InsetMinipage(params);
71 case LFUN_INSERT_NOTE:
73 string arg = cmd.getArg(0);
76 return new InsetNote(params, arg);
78 case LFUN_INSERT_BRANCH:
80 string arg = cmd.getArg(0);
83 return new InsetBranch(params, arg);
87 return new InsetERT(params);
89 case LFUN_INSET_FOOTNOTE:
90 return new InsetFoot(params);
92 case LFUN_INSET_MARGINAL:
93 return new InsetMarginal(params);
95 case LFUN_INSET_OPTARG:
96 return new InsetOptArg(params);
98 case LFUN_INSERT_BIBITEM:
99 return new InsetBibitem(InsetCommandParams("bibitem"));
101 case LFUN_INSET_FLOAT:
102 // check if the float type exists
103 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
104 return new InsetFloat(params, cmd.argument);
105 lyxerr << "Non-existent float type: " << cmd.argument << endl;
108 case LFUN_INSET_WIDE_FLOAT:
109 // check if the float type exists
110 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
111 InsetFloat * p = new InsetFloat(params, cmd.argument);
112 p->wide(true, params);
114 lyxerr << "Non-existent float type: " << cmd.argument << endl;
117 case LFUN_INSET_WRAP:
118 if (cmd.argument == "figure")
119 return new InsetWrap(params, cmd.argument);
120 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
123 case LFUN_INDEX_INSERT: {
124 // Try and generate a valid index entry.
125 InsetCommandParams icp("index");
126 string const contents = cmd.argument.empty() ?
127 bv->getLyXText()->getStringToIndex() :
129 icp.setContents(contents);
131 string data = InsetCommandMailer::params2string("index", icp);
132 LyXView * lv = bv->owner();
134 if (icp.getContents().empty()) {
135 lv->getDialogs().show("index", data, 0);
137 lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
142 case LFUN_TABULAR_INSERT:
143 if (!cmd.argument.empty()) {
146 ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
147 return new InsetTabular(*bv->buffer(), r, c);
149 bv->owner()->getDialogs().show("tabularcreate");
152 case LFUN_INSET_CAPTION:
153 if (bv->theLockingInset()) {
154 lyxerr << "Locking inset code: "
155 << static_cast<int>(bv->theLockingInset()->lyxCode());
156 InsetCaption * inset = new InsetCaption(params);
157 inset->setOwner(bv->theLockingInset());
158 inset->setAutoBreakRows(true);
159 inset->setDrawFrame(InsetText::LOCKED);
160 inset->setFrameColor(LColor::captionframe);
165 case LFUN_INDEX_PRINT:
166 return new InsetPrintIndex(InsetCommandParams("printindex"));
168 case LFUN_TOC_INSERT:
169 return new InsetTOC(InsetCommandParams("tableofcontents"));
171 case LFUN_ENVIRONMENT_INSERT:
172 return new InsetEnvironment(params, cmd.argument);
175 case LFUN_INSET_LIST:
176 return new InsetList;
178 case LFUN_INSET_THEOREM:
179 return new InsetTheorem;
182 case LFUN_INSET_INSERT: {
183 string const name = cmd.getArg(0);
185 if (name == "bibitem") {
186 InsetCommandParams icp;
187 InsetCommandMailer::string2params(cmd.argument, icp);
188 return new InsetBibitem(icp);
190 } else if (name == "bibtex") {
191 InsetCommandParams icp;
192 InsetCommandMailer::string2params(cmd.argument, icp);
193 return new InsetBibtex(icp);
195 } else if (name == "citation") {
196 InsetCommandParams icp;
197 InsetCommandMailer::string2params(cmd.argument, icp);
198 InsetCitation * inset = new InsetCitation(icp);
199 inset->setLoadingBuffer(*bv->buffer(), false);
202 } else if (name == "ert") {
203 InsetERT * inset = new InsetERT(params);
204 InsetERT::ERTStatus s;
205 InsetERTMailer::string2params(cmd.argument, s);
206 inset->status(bv, s);
209 } else if (name == "external") {
210 Buffer const & buffer = *cmd.view()->buffer();
211 InsetExternal::Params iep;
212 InsetExternalMailer::string2params(cmd.argument,
214 InsetExternal * inset = new InsetExternal;
215 inset->setParams(iep, buffer);
218 } else if (name == "graphics") {
219 Buffer const & buffer = *cmd.view()->buffer();
220 InsetGraphicsParams igp;
221 InsetGraphicsMailer::string2params(cmd.argument,
223 InsetGraphics * inset = new InsetGraphics;
224 inset->setParams(igp);
227 } else if (name == "include") {
228 InsetInclude::Params iip;
229 InsetIncludeMailer::string2params(cmd.argument, iip);
230 return new InsetInclude(iip);
232 } else if (name == "index") {
233 InsetCommandParams icp;
234 InsetCommandMailer::string2params(cmd.argument, icp);
235 return new InsetIndex(icp);
237 } else if (name == "label") {
238 InsetCommandParams icp;
239 InsetCommandMailer::string2params(cmd.argument, icp);
240 return new InsetLabel(icp);
242 } else if (name == "ref") {
243 InsetCommandParams icp;
244 InsetCommandMailer::string2params(cmd.argument, icp);
245 return new InsetRef(icp, *bv->buffer());
247 } else if (name == "toc") {
248 InsetCommandParams icp;
249 InsetCommandMailer::string2params(cmd.argument, icp);
250 return new InsetTOC(icp);
252 } else if (name == "url") {
253 InsetCommandParams icp;
254 InsetCommandMailer::string2params(cmd.argument, icp);
255 return new InsetUrl(icp);
259 case LFUN_SPACE_INSERT: {
260 string const name = cmd.argument;
261 if (name == "normal")
262 return new InsetSpace(InsetSpace::NORMAL);
263 else if (name == "protected")
264 return new InsetSpace(InsetSpace::PROTECTED);
265 else if (name == "thin")
266 return new InsetSpace(InsetSpace::THIN);
267 else if (name == "quad")
268 return new InsetSpace(InsetSpace::QUAD);
269 else if (name == "qquad")
270 return new InsetSpace(InsetSpace::QQUAD);
271 else if (name == "enspace")
272 return new InsetSpace(InsetSpace::ENSPACE);
273 else if (name == "enskip")
274 return new InsetSpace(InsetSpace::ENSKIP);
275 else if (name == "negthinspace")
276 return new InsetSpace(InsetSpace::NEGTHIN);
277 else if (name.empty())
278 lyxerr << "LyX function 'space' needs an argument." << endl;
280 lyxerr << "Wrong argument for LyX function 'space'." << endl;
293 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
296 if (lex.getString() != "\\begin_inset") {
297 lyxerr << "Buffer::readInset: Consistency check failed."
301 InsetOld * inset = 0;
304 string const tmptok = lex.getString();
306 // test the different insets
307 if (tmptok == "LatexCommand") {
308 InsetCommandParams inscmd;
311 string const cmdName = inscmd.getCmdName();
313 // This strange command allows LyX to recognize "natbib" style
314 // citations: citet, citep, Citet etc.
315 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
316 inset = new InsetCitation(inscmd);
317 } else if (cmdName == "bibitem") {
318 lex.printError("Wrong place for bibitem");
319 inset = new InsetBibitem(inscmd);
320 } else if (cmdName == "bibtex") {
321 inset = new InsetBibtex(inscmd);
322 } else if (cmdName == "index") {
323 inset = new InsetIndex(inscmd);
324 } else if (cmdName == "include") {
325 inset = new InsetInclude(inscmd, buf);
326 } else if (cmdName == "label") {
327 inset = new InsetLabel(inscmd);
328 } else if (cmdName == "url"
329 || cmdName == "htmlurl") {
330 inset = new InsetUrl(inscmd);
331 } else if (cmdName == "ref"
332 || cmdName == "eqref"
333 || cmdName == "pageref"
335 || cmdName == "vpageref"
336 || cmdName == "prettyref") {
337 if (!inscmd.getOptions().empty()
338 || !inscmd.getContents().empty()) {
339 inset = new InsetRef(inscmd, buf);
341 } else if (cmdName == "tableofcontents") {
342 inset = new InsetTOC(inscmd);
343 } else if (cmdName == "listofalgorithms") {
344 inset = new InsetFloatList("algorithm");
345 } else if (cmdName == "listoffigures") {
346 inset = new InsetFloatList("figure");
347 } else if (cmdName == "listoftables") {
348 inset = new InsetFloatList("table");
349 } else if (cmdName == "printindex") {
350 inset = new InsetPrintIndex(inscmd);
353 if (tmptok == "Quotes") {
354 inset = new InsetQuotes;
355 } else if (tmptok == "External") {
356 inset = new InsetExternal;
357 } else if (tmptok == "FormulaMacro") {
358 inset = new InsetFormulaMacro;
359 } else if (tmptok == "Formula") {
360 inset = new InsetFormula;
361 } else if (tmptok == "Graphics") {
362 inset = new InsetGraphics;
363 } else if (tmptok == "Note" || tmptok == "Comment"
364 || tmptok == "Greyedout") {
365 inset = new InsetNote(buf.params, tmptok);
366 } else if (tmptok == "Branch") {
367 inset = new InsetBranch(buf.params, tmptok);
368 } else if (tmptok == "Include") {
369 InsetCommandParams p("Include");
370 inset = new InsetInclude(p, buf);
371 } else if (tmptok == "Environment") {
373 inset = new InsetEnvironment(buf.params, lex.getString());
374 } else if (tmptok == "ERT") {
375 inset = new InsetERT(buf.params);
376 } else if (tmptok == "InsetSpace") {
377 inset = new InsetSpace;
378 } else if (tmptok == "Tabular") {
379 inset = new InsetTabular(buf);
380 } else if (tmptok == "Text") {
381 inset = new InsetText(buf.params);
382 } else if (tmptok == "Foot") {
383 inset = new InsetFoot(buf.params);
384 } else if (tmptok == "Marginal") {
385 inset = new InsetMarginal(buf.params);
386 } else if (tmptok == "OptArg") {
387 inset = new InsetOptArg(buf.params);
388 } else if (tmptok == "Minipage") {
389 inset = new InsetMinipage(buf.params);
390 } else if (tmptok == "Float") {
392 string tmptok = lex.getString();
393 inset = new InsetFloat(buf.params, tmptok);
394 } else if (tmptok == "Wrap") {
396 string tmptok = lex.getString();
397 inset = new InsetWrap(buf.params, tmptok);
399 } else if (tmptok == "List") {
400 inset = new InsetList;
401 } else if (tmptok == "Theorem") {
402 inset = new InsetList;
404 } else if (tmptok == "Caption") {
405 inset = new InsetCaption(buf.params);
406 } else if (tmptok == "FloatList") {
407 inset = new InsetFloatList;
411 inset->read(buf, lex);