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
13 #include "funcrequest.h"
14 #include "bufferparams.h"
16 #include "FloatList.h"
18 #include "BufferView.h"
22 #include "insets/insetbibitem.h"
23 #include "insets/insetbibtex.h"
24 #include "insets/insetcaption.h"
25 #include "insets/insetcite.h"
26 #include "insets/insetenv.h"
27 #include "insets/insetert.h"
28 #include "insets/insetexternal.h"
29 #include "insets/insetfloat.h"
30 #include "insets/insetfloatlist.h"
31 #include "insets/insetfoot.h"
32 #include "insets/insetgraphics.h"
33 #include "insets/insethfill.h"
34 #include "insets/insetinclude.h"
35 #include "insets/insetindex.h"
36 #include "insets/insetlabel.h"
37 #include "insets/insetmarginal.h"
38 #include "insets/insetminipage.h"
39 #include "insets/insetnote.h"
40 #include "insets/insetoptarg.h"
41 #include "insets/insetparent.h"
42 #include "insets/insetref.h"
43 #include "insets/insettabular.h"
44 #include "insets/insettext.h"
45 #include "insets/insettoc.h"
46 #include "insets/inseturl.h"
47 #include "insets/insetwrap.h"
48 #include "mathed/formulamacro.h"
49 #include "mathed/formula.h"
51 #include "frontends/Dialogs.h"
52 #include "frontends/LyXView.h"
53 #include "support/lstrings.h"
59 Inset * 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:
72 return new InsetNote(params);
75 return new InsetERT(params);
77 case LFUN_INSET_FOOTNOTE:
78 return new InsetFoot(params);
80 case LFUN_INSET_MARGINAL:
81 return new InsetMarginal(params);
83 case LFUN_INSET_OPTARG:
84 return new InsetOptArg(params);
86 case LFUN_INSERT_BIBITEM:
87 return new InsetBibitem(InsetCommandParams("bibitem"));
89 case LFUN_INSET_FLOAT:
90 // check if the float type exists
91 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
92 return new InsetFloat(params, cmd.argument);
93 lyxerr << "Non-existent float type: " << cmd.argument << endl;
96 case LFUN_INSET_WIDE_FLOAT:
97 // check if the float type exists
98 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
99 InsetFloat * p = new InsetFloat(params, cmd.argument);
100 p->wide(true, params);
102 lyxerr << "Non-existent float type: " << cmd.argument << endl;
105 case LFUN_INSET_WRAP:
106 if (cmd.argument == "figure")
107 return new InsetWrap(params, cmd.argument);
108 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
111 case LFUN_INDEX_INSERT: {
112 // Try and generate a valid index entry.
113 InsetCommandParams icp("index");
114 string const contents = cmd.argument.empty() ?
115 bv->getLyXText()->getStringToIndex() :
117 icp.setContents(contents);
119 string data = InsetCommandMailer::params2string("index", icp);
120 LyXView * lv = bv->owner();
122 if (icp.getContents().empty()) {
123 lv->getDialogs().show("index", data, 0);
125 FuncRequest fr(bv, LFUN_INSET_APPLY, data);
131 case LFUN_TABULAR_INSERT:
132 if (!cmd.argument.empty()) {
135 ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
136 return new InsetTabular(*bv->buffer(), r, c);
138 bv->owner()->getDialogs().show("tabularcreate");
141 case LFUN_INSET_CAPTION:
142 if (bv->theLockingInset()) {
143 lyxerr << "Locking inset code: "
144 << static_cast<int>(bv->theLockingInset()->lyxCode());
145 InsetCaption * inset = new InsetCaption(params);
146 inset->setOwner(bv->theLockingInset());
147 inset->setAutoBreakRows(true);
148 inset->setDrawFrame(0, InsetText::LOCKED);
149 inset->setFrameColor(0, LColor::captionframe);
154 case LFUN_INDEX_PRINT:
155 return new InsetPrintIndex(InsetCommandParams("printindex"));
157 case LFUN_TOC_INSERT:
158 return new InsetTOC(InsetCommandParams("tableofcontents"));
160 case LFUN_PARENTINSERT:
161 return new InsetParent(
162 InsetCommandParams("lyxparent", cmd.argument), *bv->buffer());
164 case LFUN_ENVIRONMENT_INSERT:
165 return new InsetEnvironment(params, cmd.argument);
168 case LFUN_INSET_LIST:
169 return new InsetList;
171 case LFUN_INSET_THEOREM:
172 return new InsetTheorem;
175 case LFUN_INSET_INSERT: {
176 string const name = cmd.getArg(0);
178 if (name == "bibitem") {
179 InsetCommandParams icp;
180 InsetCommandMailer::string2params(cmd.argument, icp);
181 return new InsetBibitem(icp);
183 } else if (name == "bibtex") {
184 InsetCommandParams icp;
185 InsetCommandMailer::string2params(cmd.argument, icp);
186 return new InsetBibtex(icp);
188 } else if (name == "citation") {
189 InsetCommandParams icp;
190 InsetCommandMailer::string2params(cmd.argument, icp);
191 InsetCitation * inset = new InsetCitation(icp);
192 inset->setLoadingBuffer(bv->buffer(), false);
195 } else if (name == "ert") {
196 InsetERT * inset = new InsetERT(params);
197 InsetERT::ERTStatus s;
198 InsetERTMailer::string2params(cmd.argument, s);
199 inset->status(bv, s);
202 } else if (name == "external") {
203 InsetExternal::Params iep;
204 InsetExternalMailer::string2params(cmd.argument, iep);
205 InsetExternal * inset = new InsetExternal;
206 inset->setFromParams(iep);
209 } else if (name == "graphics") {
210 InsetGraphicsParams igp;
211 InsetGraphicsMailer::string2params(cmd.argument, igp);
212 InsetGraphics * inset = new InsetGraphics;
213 string const fpath = cmd.view()->buffer()->filePath();
214 inset->setParams(igp, fpath);
217 } else if (name == "include") {
218 InsetInclude::Params iip;
219 InsetIncludeMailer::string2params(cmd.argument, iip);
220 return new InsetInclude(iip);
222 } else if (name == "index") {
223 InsetCommandParams icp;
224 InsetCommandMailer::string2params(cmd.argument, icp);
225 return new InsetIndex(icp);
227 } else if (name == "label") {
228 InsetCommandParams icp;
229 InsetCommandMailer::string2params(cmd.argument, icp);
230 return new InsetLabel(icp);
232 } else if (name == "ref") {
233 InsetCommandParams icp;
234 InsetCommandMailer::string2params(cmd.argument, icp);
235 return new InsetRef(icp, *bv->buffer());
237 } else if (name == "toc") {
238 InsetCommandParams icp;
239 InsetCommandMailer::string2params(cmd.argument, icp);
240 return new InsetTOC(icp);
242 } else if (name == "url") {
243 InsetCommandParams icp;
244 InsetCommandMailer::string2params(cmd.argument, icp);
245 return new InsetUrl(icp);
258 Inset * readInset(LyXLex & lex, Buffer const & buf)
261 if (lex.getString() != "\\begin_inset") {
262 lyxerr << "Buffer::readInset: Consistency check failed."
269 string const tmptok = lex.getString();
271 // test the different insets
272 if (tmptok == "LatexCommand") {
273 InsetCommandParams inscmd;
276 string const cmdName = inscmd.getCmdName();
278 // This strange command allows LyX to recognize "natbib" style
279 // citations: citet, citep, Citet etc.
280 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
281 inset = new InsetCitation(inscmd);
282 } else if (cmdName == "bibitem") {
283 lex.printError("Wrong place for bibitem");
284 inset = new InsetBibitem(inscmd);
285 } else if (cmdName == "BibTeX") {
286 inset = new InsetBibtex(inscmd);
287 } else if (cmdName == "index") {
288 inset = new InsetIndex(inscmd);
289 } else if (cmdName == "include") {
290 inset = new InsetInclude(inscmd, buf);
291 } else if (cmdName == "label") {
292 inset = new InsetLabel(inscmd);
293 } else if (cmdName == "url"
294 || cmdName == "htmlurl") {
295 inset = new InsetUrl(inscmd);
296 } else if (cmdName == "ref"
297 || cmdName == "eqref"
298 || cmdName == "pageref"
300 || cmdName == "vpageref"
301 || cmdName == "prettyref") {
302 if (!inscmd.getOptions().empty()
303 || !inscmd.getContents().empty()) {
304 inset = new InsetRef(inscmd, buf);
306 } else if (cmdName == "tableofcontents") {
307 inset = new InsetTOC(inscmd);
308 } else if (cmdName == "listofalgorithms") {
309 inset = new InsetFloatList("algorithm");
310 } else if (cmdName == "listoffigures") {
311 inset = new InsetFloatList("figure");
312 } else if (cmdName == "listoftables") {
313 inset = new InsetFloatList("table");
314 } else if (cmdName == "printindex") {
315 inset = new InsetPrintIndex(inscmd);
316 } else if (cmdName == "lyxparent") {
317 inset = new InsetParent(inscmd, buf);
320 if (tmptok == "Quotes") {
321 inset = new InsetQuotes;
322 } else if (tmptok == "External") {
323 inset = new InsetExternal;
324 } else if (tmptok == "FormulaMacro") {
325 inset = new InsetFormulaMacro;
326 } else if (tmptok == "Formula") {
327 inset = new InsetFormula;
328 } else if (tmptok == "Graphics") {
329 inset = new InsetGraphics;
330 } else if (tmptok == "Note") {
331 inset = new InsetNote(buf.params);
332 } else if (tmptok == "Include") {
333 InsetCommandParams p("Include");
334 inset = new InsetInclude(p, buf);
335 } else if (tmptok == "Environment") {
337 inset = new InsetEnvironment(buf.params, lex.getString());
338 } else if (tmptok == "ERT") {
339 inset = new InsetERT(buf.params);
340 } else if (tmptok == "Tabular") {
341 inset = new InsetTabular(buf);
342 } else if (tmptok == "Text") {
343 inset = new InsetText(buf.params);
344 } else if (tmptok == "Foot") {
345 inset = new InsetFoot(buf.params);
346 } else if (tmptok == "Marginal") {
347 inset = new InsetMarginal(buf.params);
348 } else if (tmptok == "OptArg") {
349 inset = new InsetOptArg(buf.params);
350 } else if (tmptok == "Minipage") {
351 inset = new InsetMinipage(buf.params);
352 } else if (tmptok == "Float") {
354 string tmptok = lex.getString();
355 inset = new InsetFloat(buf.params, tmptok);
356 } else if (tmptok == "Wrap") {
358 string tmptok = lex.getString();
359 inset = new InsetWrap(buf.params, tmptok);
361 } else if (tmptok == "List") {
362 inset = new InsetList;
363 } else if (tmptok == "Theorem") {
364 inset = new InsetList;
366 } else if (tmptok == "Caption") {
367 inset = new InsetCaption(buf.params);
368 } else if (tmptok == "FloatList") {
369 inset = new InsetFloatList;
373 inset->read(&buf, lex);