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/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/insetoptarg.h"
40 #include "insets/insetparent.h"
41 #include "insets/insetref.h"
42 #include "insets/insettabular.h"
43 #include "insets/insettext.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"
58 Inset * createInset(FuncRequest const & cmd)
60 BufferView * bv = cmd.view();
61 BufferParams const & params = bv->buffer()->params;
65 return new InsetHFill();
67 case LFUN_INSET_MINIPAGE:
68 return new InsetMinipage(params);
70 case LFUN_INSERT_NOTE:
71 return new InsetNote(params);
74 return new InsetERT(params);
76 case LFUN_INSET_FOOTNOTE:
77 return new InsetFoot(params);
79 case LFUN_INSET_MARGINAL:
80 return new InsetMarginal(params);
82 case LFUN_INSET_OPTARG:
83 return new InsetOptArg(params);
85 case LFUN_INSERT_BIBITEM:
86 return new InsetBibitem(InsetCommandParams("bibitem"));
88 case LFUN_INSET_FLOAT:
89 // check if the float type exists
90 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
91 return new InsetFloat(params, cmd.argument);
92 lyxerr << "Non-existent float type: " << cmd.argument << endl;
95 case LFUN_INSET_WIDE_FLOAT:
96 // check if the float type exists
97 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
98 InsetFloat * p = new InsetFloat(params, cmd.argument);
99 p->wide(true, params);
101 lyxerr << "Non-existent float type: " << cmd.argument << endl;
104 case LFUN_INSET_WRAP:
105 if (cmd.argument == "figure")
106 return new InsetWrap(params, cmd.argument);
107 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
110 case LFUN_INDEX_INSERT: {
111 // Try and generate a valid index entry.
112 InsetCommandParams icp("index");
113 string const contents = cmd.argument.empty() ?
114 bv->getLyXText()->getStringToIndex(bv) :
116 icp.setContents(contents);
118 string data = InsetCommandMailer::params2string("index", icp);
119 LyXView * lv = bv->owner();
121 if (icp.getContents().empty()) {
122 lv->getDialogs().show("index", data, 0);
124 FuncRequest fr(bv, LFUN_INSET_APPLY, data);
130 case LFUN_TABULAR_INSERT:
131 if (!cmd.argument.empty()) {
134 ::sscanf(cmd.argument.c_str(),"%d%d", &r, &c);
135 return new InsetTabular(*bv->buffer(), r, c);
137 bv->owner()->getDialogs().show("tabularcreate");
140 case LFUN_INSET_CAPTION:
141 if (bv->theLockingInset()) {
142 lyxerr << "Locking inset code: "
143 << static_cast<int>(bv->theLockingInset()->lyxCode());
144 InsetCaption * inset = new InsetCaption(params);
145 inset->setOwner(bv->theLockingInset());
146 inset->setAutoBreakRows(true);
147 inset->setDrawFrame(0, InsetText::LOCKED);
148 inset->setFrameColor(0, LColor::captionframe);
153 case LFUN_INDEX_PRINT:
154 return new InsetPrintIndex(InsetCommandParams("printindex"));
156 case LFUN_TOC_INSERT:
157 return new InsetTOC(InsetCommandParams("tableofcontents"));
159 case LFUN_PARENTINSERT:
160 return new InsetParent(
161 InsetCommandParams("lyxparent", cmd.argument), *bv->buffer());
164 case LFUN_INSET_LIST:
165 return new InsetList;
167 case LFUN_INSET_THEOREM:
168 return new InsetTheorem;
171 case LFUN_INSET_APPLY: {
172 string const name = cmd.getArg(0);
174 if (name == "bibitem") {
175 InsetCommandParams icp;
176 InsetCommandMailer::string2params(cmd.argument, icp);
177 return new InsetBibitem(icp);
179 } else if (name == "bibtex") {
180 InsetCommandParams icp;
181 InsetCommandMailer::string2params(cmd.argument, icp);
182 return new InsetBibtex(icp);
184 } else if (name == "citation") {
185 InsetCommandParams icp;
186 InsetCommandMailer::string2params(cmd.argument, icp);
187 InsetCitation * inset = new InsetCitation(icp);
188 inset->setLoadingBuffer(bv->buffer(), false);
191 } else if (name == "ert") {
192 InsetERT * inset = new InsetERT(params);
193 InsetERT::ERTStatus s;
194 InsetERTMailer::string2params(cmd.argument, s);
195 inset->status(bv, s);
198 } else if (name == "external") {
199 InsetExternal::Params iep;
200 InsetExternalMailer::string2params(cmd.argument, iep);
201 InsetExternal * inset = new InsetExternal;
202 inset->setFromParams(iep);
205 } else if (name == "graphics") {
206 InsetGraphicsParams igp;
207 InsetGraphicsMailer::string2params(cmd.argument, igp);
208 InsetGraphics * inset = new InsetGraphics;
209 string const fpath = cmd.view()->buffer()->filePath();
210 inset->setParams(igp, fpath);
213 } else if (name == "include") {
214 InsetInclude::Params iip;
215 InsetIncludeMailer::string2params(cmd.argument, iip);
216 return new InsetInclude(iip);
218 } else if (name == "index") {
219 InsetCommandParams icp;
220 InsetCommandMailer::string2params(cmd.argument, icp);
221 return new InsetIndex(icp);
223 } else if (name == "label") {
224 InsetCommandParams icp;
225 InsetCommandMailer::string2params(cmd.argument, icp);
226 return new InsetLabel(icp);
228 } else if (name == "ref") {
229 InsetCommandParams icp;
230 InsetCommandMailer::string2params(cmd.argument, icp);
231 return new InsetRef(icp, *bv->buffer());
233 } else if (name == "toc") {
234 InsetCommandParams icp;
235 InsetCommandMailer::string2params(cmd.argument, icp);
236 return new InsetTOC(icp);
238 } else if (name == "url") {
239 InsetCommandParams icp;
240 InsetCommandMailer::string2params(cmd.argument, icp);
241 return new InsetUrl(icp);
254 Inset * readInset(LyXLex & lex, Buffer const & buf)
257 if (lex.getString() != "\\begin_inset") {
258 lyxerr << "Buffer::readInset: Consistency check failed."
265 string const tmptok = lex.getString();
267 // test the different insets
268 if (tmptok == "LatexCommand") {
269 InsetCommandParams inscmd;
272 string const cmdName = inscmd.getCmdName();
274 // This strange command allows LyX to recognize "natbib" style
275 // citations: citet, citep, Citet etc.
276 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
277 inset = new InsetCitation(inscmd);
278 } else if (cmdName == "bibitem") {
279 lex.printError("Wrong place for bibitem");
280 inset = new InsetBibitem(inscmd);
281 } else if (cmdName == "BibTeX") {
282 inset = new InsetBibtex(inscmd);
283 } else if (cmdName == "index") {
284 inset = new InsetIndex(inscmd);
285 } else if (cmdName == "include") {
286 inset = new InsetInclude(inscmd, buf);
287 } else if (cmdName == "label") {
288 inset = new InsetLabel(inscmd);
289 } else if (cmdName == "url"
290 || cmdName == "htmlurl") {
291 inset = new InsetUrl(inscmd);
292 } else if (cmdName == "ref"
293 || cmdName == "pageref"
295 || cmdName == "vpageref"
296 || cmdName == "prettyref") {
297 if (!inscmd.getOptions().empty()
298 || !inscmd.getContents().empty()) {
299 inset = new InsetRef(inscmd, buf);
301 } else if (cmdName == "tableofcontents") {
302 inset = new InsetTOC(inscmd);
303 } else if (cmdName == "listofalgorithms") {
304 inset = new InsetFloatList("algorithm");
305 } else if (cmdName == "listoffigures") {
306 inset = new InsetFloatList("figure");
307 } else if (cmdName == "listoftables") {
308 inset = new InsetFloatList("table");
309 } else if (cmdName == "printindex") {
310 inset = new InsetPrintIndex(inscmd);
311 } else if (cmdName == "lyxparent") {
312 inset = new InsetParent(inscmd, buf);
315 if (tmptok == "Quotes") {
316 inset = new InsetQuotes;
317 } else if (tmptok == "External") {
318 inset = new InsetExternal;
319 } else if (tmptok == "FormulaMacro") {
320 inset = new InsetFormulaMacro;
321 } else if (tmptok == "Formula") {
322 inset = new InsetFormula;
323 } else if (tmptok == "Graphics") {
324 inset = new InsetGraphics;
325 } else if (tmptok == "Note") {
326 inset = new InsetNote(buf.params);
327 } else if (tmptok == "Include") {
328 InsetCommandParams p("Include");
329 inset = new InsetInclude(p, buf);
330 } else if (tmptok == "ERT") {
331 inset = new InsetERT(buf.params);
332 } else if (tmptok == "Tabular") {
333 inset = new InsetTabular(buf);
334 } else if (tmptok == "Text") {
335 inset = new InsetText(buf.params);
336 } else if (tmptok == "Foot") {
337 inset = new InsetFoot(buf.params);
338 } else if (tmptok == "Marginal") {
339 inset = new InsetMarginal(buf.params);
340 } else if (tmptok == "OptArg") {
341 inset = new InsetOptArg(buf.params);
342 } else if (tmptok == "Minipage") {
343 inset = new InsetMinipage(buf.params);
344 } else if (tmptok == "Float") {
346 string tmptok = lex.getString();
347 inset = new InsetFloat(buf.params, tmptok);
348 } else if (tmptok == "Wrap") {
350 string tmptok = lex.getString();
351 inset = new InsetWrap(buf.params, tmptok);
353 } else if (tmptok == "List") {
354 inset = new InsetList;
355 } else if (tmptok == "Theorem") {
356 inset = new InsetList;
358 } else if (tmptok == "Caption") {
359 inset = new InsetCaption(buf.params);
360 } else if (tmptok == "FloatList") {
361 inset = new InsetFloatList;
365 inset->read(&buf, lex);