]> git.lyx.org Git - lyx.git/blob - src/factory.C
246e1c1afb7767317523af0c5839ec5d3bb7344c
[lyx.git] / src / factory.C
1 /**
2  * \file factory.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "factory.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "bufferparams.h"
18 #include "debug.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "LColor.h"
22 #include "lyxlex.h"
23
24 #include "insets/insetbibitem.h"
25 #include "insets/insetbibtex.h"
26 #include "insets/insetcaption.h"
27 #include "insets/insetcite.h"
28 #include "insets/insetenv.h"
29 #include "insets/insetert.h"
30 #include "insets/insetexternal.h"
31 #include "insets/insetfloat.h"
32 #include "insets/insetfloatlist.h"
33 #include "insets/insetfoot.h"
34 #include "insets/insetgraphics.h"
35 #include "insets/insethfill.h"
36 #include "insets/insetinclude.h"
37 #include "insets/insetindex.h"
38 #include "insets/insetlabel.h"
39 #include "insets/insetline.h"
40 #include "insets/insetmarginal.h"
41 #include "insets/insetminipage.h"
42 #include "insets/insetnote.h"
43 #include "insets/insetbox.h"
44 #include "insets/insetbranch.h"
45 #include "insets/insetoptarg.h"
46 #include "insets/insetpagebreak.h"
47 #include "insets/insetref.h"
48 #include "insets/insetspace.h"
49 #include "insets/insettabular.h"
50 #include "insets/insettoc.h"
51 #include "insets/inseturl.h"
52 #include "insets/insetwrap.h"
53 #include "mathed/formulamacro.h"
54 #include "mathed/formula.h"
55
56 #include "frontends/Dialogs.h"
57 #include "frontends/LyXView.h"
58 #include "support/lstrings.h"
59 #include "support/std_sstream.h"
60
61 #include <boost/assert.hpp>
62
63 using lyx::support::compare_ascii_no_case;
64
65 using std::endl;
66 using std::string;
67
68
69 InsetOld * createInset(FuncRequest const & cmd)
70 {
71         BufferView * bv = cmd.view();
72         BufferParams const & params = bv->buffer()->params();
73
74         switch (cmd.action) {
75         case LFUN_HFILL:
76                 return new InsetHFill;
77
78         case LFUN_INSERT_LINE:
79                 return new InsetLine;
80
81         case LFUN_INSERT_PAGEBREAK:
82                 return new InsetPagebreak;
83
84         case LFUN_INSET_MINIPAGE:
85                 return new InsetMinipage(params);
86
87         case LFUN_INSERT_NOTE: {
88                 string arg = cmd.getArg(0);
89                 if (arg.empty())
90                         arg = "Note";
91                 return new InsetNote(params, arg);
92         }
93
94         case LFUN_INSERT_BOX: {
95                 string arg = cmd.getArg(0);
96                 if (arg.empty())
97                         arg = "Boxed";
98                 return new InsetBox(params, arg);
99         }
100
101         case LFUN_INSERT_BRANCH: {
102                 string arg = cmd.getArg(0);
103                 if (arg.empty())
104                         arg = "none";
105                 return new InsetBranch(params, arg);
106         }
107
108         case LFUN_INSET_ERT:
109                 return new InsetERT(params);
110
111         case LFUN_INSET_FOOTNOTE:
112                 return new InsetFoot(params);
113
114         case LFUN_INSET_MARGINAL:
115                 return new InsetMarginal(params);
116
117         case LFUN_INSET_OPTARG:
118                 return new InsetOptArg(params);
119
120         case LFUN_INSERT_BIBITEM:
121                 return new InsetBibitem(InsetCommandParams("bibitem"));
122
123         case LFUN_INSET_FLOAT:
124                 // check if the float type exists
125                 if (params.getLyXTextClass().floats().typeExist(cmd.argument))
126                         return new InsetFloat(params, cmd.argument);
127                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
128                 return 0;
129
130         case LFUN_INSET_WIDE_FLOAT:
131                 // check if the float type exists
132                 if (params.getLyXTextClass().floats().typeExist(cmd.argument)) {
133                         InsetFloat * p = new InsetFloat(params, cmd.argument);
134                         p->wide(true, params);
135                 }
136                 lyxerr << "Non-existent float type: " << cmd.argument << endl;
137                 return 0;
138
139         case LFUN_INSET_WRAP:
140                 if (cmd.argument == "figure")
141                         return new InsetWrap(params, cmd.argument);
142                 lyxerr << "Non-existent floatflt type: " << cmd.argument << endl;
143                 return 0;
144
145         case LFUN_INDEX_INSERT: {
146                 // Try and generate a valid index entry.
147                 InsetCommandParams icp("index");
148                 string const contents = cmd.argument.empty() ?
149                         bv->getLyXText()->getStringToIndex() :
150                         cmd.argument;
151                 icp.setContents(contents);
152
153                 string data = InsetCommandMailer::params2string("index", icp);
154                 LyXView * lv = bv->owner();
155
156                 if (icp.getContents().empty()) {
157                         lv->getDialogs().show("index", data, 0);
158                 } else {
159                         lv->dispatch(FuncRequest(bv, LFUN_INSET_APPLY, data));
160                 }
161                 return 0;
162         }
163
164         case LFUN_TABULAR_INSERT:
165                 if (!cmd.argument.empty()) {
166                         std::istringstream ss(cmd.argument);
167                         int r, c;
168                         ss >> r >> c;
169                         if (r <= 0) r = 2;
170                         if (c <= 0) c = 2;
171                         return new InsetTabular(*bv->buffer(), r, c);
172                 }
173                 bv->owner()->getDialogs().show("tabularcreate");
174                 return 0;
175
176         case LFUN_INSET_CAPTION:
177                 if (bv->theLockingInset()) {
178                         lyxerr << "Locking inset code: "
179                                << static_cast<int>(bv->theLockingInset()->lyxCode());
180                         InsetCaption * inset = new InsetCaption(params);
181                         inset->setOwner(bv->theLockingInset());
182                         inset->setAutoBreakRows(true);
183                         inset->setDrawFrame(InsetText::LOCKED);
184                         inset->setFrameColor(LColor::captionframe);
185                         return inset;
186                 }
187                 return 0;
188
189         case LFUN_INDEX_PRINT:
190                 return new InsetPrintIndex(InsetCommandParams("printindex"));
191
192         case LFUN_TOC_INSERT:
193                 return new InsetTOC(InsetCommandParams("tableofcontents"));
194
195         case LFUN_ENVIRONMENT_INSERT:
196                 return new InsetEnvironment(params, cmd.argument);
197
198 #if 0
199         case LFUN_INSET_LIST:
200                 return new InsetList;
201
202         case LFUN_INSET_THEOREM:
203                 return new InsetTheorem;
204 #endif
205
206         case LFUN_INSET_INSERT: {
207                 string const name = cmd.getArg(0);
208
209                 if (name == "bibitem") {
210                         InsetCommandParams icp;
211                         InsetCommandMailer::string2params(cmd.argument, icp);
212                         return new InsetBibitem(icp);
213
214                 } else if (name == "bibtex") {
215                         InsetCommandParams icp;
216                         InsetCommandMailer::string2params(cmd.argument, icp);
217                         return new InsetBibtex(icp);
218
219                 } else if (name == "citation") {
220                         InsetCommandParams icp;
221                         InsetCommandMailer::string2params(cmd.argument, icp);
222                         return new InsetCitation(icp);
223
224                 } else if (name == "ert") {
225                         InsetERT * inset = new InsetERT(params);
226                         InsetERT::ERTStatus s;
227                         InsetERTMailer::string2params(cmd.argument, s);
228                         inset->status(bv, s);
229                         return inset;
230
231                 } else if (name == "external") {
232                         Buffer const & buffer = *cmd.view()->buffer();
233                         InsetExternalParams iep;
234                         InsetExternalMailer::string2params(cmd.argument,
235                                                            buffer, iep);
236                         InsetExternal * inset = new InsetExternal;
237                         inset->setParams(iep, buffer);
238                         return inset;
239
240                 } else if (name == "graphics") {
241                         Buffer const & buffer = *cmd.view()->buffer();
242                         InsetGraphicsParams igp;
243                         InsetGraphicsMailer::string2params(cmd.argument,
244                                                            buffer, igp);
245                         InsetGraphics * inset = new InsetGraphics;
246                         inset->setParams(igp);
247                         return inset;
248
249                 } else if (name == "include") {
250                         InsetCommandParams iip;
251                         InsetIncludeMailer::string2params(cmd.argument, iip);
252                         return new InsetInclude(iip);
253
254                 } else if (name == "index") {
255                         InsetCommandParams icp;
256                         InsetCommandMailer::string2params(cmd.argument, icp);
257                         return new InsetIndex(icp);
258
259                 } else if (name == "label") {
260                         InsetCommandParams icp;
261                         InsetCommandMailer::string2params(cmd.argument, icp);
262                         return new InsetLabel(icp);
263
264                 } else if (name == "ref") {
265                         InsetCommandParams icp;
266                         InsetCommandMailer::string2params(cmd.argument, icp);
267                         return new InsetRef(icp, *bv->buffer());
268
269                 } else if (name == "toc") {
270                         InsetCommandParams icp;
271                         InsetCommandMailer::string2params(cmd.argument, icp);
272                         return new InsetTOC(icp);
273
274                 } else if (name == "url") {
275                         InsetCommandParams icp;
276                         InsetCommandMailer::string2params(cmd.argument, icp);
277                         return new InsetUrl(icp);
278                 }
279         }
280
281         case LFUN_SPACE_INSERT: {
282                 string const name = cmd.argument;
283                 if (name == "normal")
284                         return new InsetSpace(InsetSpace::NORMAL);
285                 else if (name == "protected")
286                         return new InsetSpace(InsetSpace::PROTECTED);
287                 else if (name == "thin")
288                         return new InsetSpace(InsetSpace::THIN);
289                 else if (name == "quad")
290                         return new InsetSpace(InsetSpace::QUAD);
291                 else if (name == "qquad")
292                         return new InsetSpace(InsetSpace::QQUAD);
293                 else if (name == "enspace")
294                         return new InsetSpace(InsetSpace::ENSPACE);
295                 else if (name == "enskip")
296                         return new InsetSpace(InsetSpace::ENSKIP);
297                 else if (name == "negthinspace")
298                         return new InsetSpace(InsetSpace::NEGTHIN);
299                 else if (name.empty())
300                         lyxerr << "LyX function 'space' needs an argument." << endl;
301                 else
302                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
303         }
304
305         break;
306
307         default:
308                 break;
309         }
310
311         return 0;
312 }
313
314
315 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
316 {
317         // consistency check
318         if (lex.getString() != "\\begin_inset") {
319                 lyxerr << "Buffer::readInset: Consistency check failed."
320                        << endl;
321         }
322
323         InsetOld * inset = 0;
324
325         lex.next();
326         string const tmptok = lex.getString();
327
328         // test the different insets
329         if (tmptok == "LatexCommand") {
330                 InsetCommandParams inscmd;
331                 inscmd.read(lex);
332
333                 string const cmdName = inscmd.getCmdName();
334
335                 // This strange command allows LyX to recognize "natbib" style
336                 // citations: citet, citep, Citet etc.
337                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
338                         inset = new InsetCitation(inscmd);
339                 } else if (cmdName == "bibitem") {
340                         lex.printError("Wrong place for bibitem");
341                         inset = new InsetBibitem(inscmd);
342                 } else if (cmdName == "bibtex") {
343                         inset = new InsetBibtex(inscmd);
344                 } else if (cmdName == "index") {
345                         inset = new InsetIndex(inscmd);
346                 } else if (cmdName == "include") {
347                         inset = new InsetInclude(inscmd);
348                 } else if (cmdName == "label") {
349                         inset = new InsetLabel(inscmd);
350                 } else if (cmdName == "url"
351                            || cmdName == "htmlurl") {
352                         inset = new InsetUrl(inscmd);
353                 } else if (cmdName == "ref"
354                            || cmdName == "eqref"
355                            || cmdName == "pageref"
356                            || cmdName == "vref"
357                            || cmdName == "vpageref"
358                            || cmdName == "prettyref") {
359                         if (!inscmd.getOptions().empty()
360                             || !inscmd.getContents().empty()) {
361                                 inset = new InsetRef(inscmd, buf);
362                         }
363                 } else if (cmdName == "tableofcontents") {
364                         inset = new InsetTOC(inscmd);
365                 } else if (cmdName == "listofalgorithms") {
366                         inset = new InsetFloatList("algorithm");
367                 } else if (cmdName == "listoffigures") {
368                         inset = new InsetFloatList("figure");
369                 } else if (cmdName == "listoftables") {
370                         inset = new InsetFloatList("table");
371                 } else if (cmdName == "printindex") {
372                         inset = new InsetPrintIndex(inscmd);
373                 } else {
374                         lyxerr << "unknown CommandInset '" << cmdName
375                                << "'" << std::endl;
376                         while (lex.isOK() && lex.getString() != "\\end_inset")
377                                 lex.next();
378                         return 0;
379                 }
380         } else {
381                 if (tmptok == "Quotes") {
382                         inset = new InsetQuotes;
383                 } else if (tmptok == "External") {
384                         inset = new InsetExternal;
385                 } else if (tmptok == "FormulaMacro") {
386                         inset = new InsetFormulaMacro;
387                 } else if (tmptok == "Formula") {
388                         inset = new InsetFormula;
389                 } else if (tmptok == "Graphics") {
390                         inset = new InsetGraphics;
391                 } else if (tmptok == "Note"     || tmptok == "Comment"
392                                 || tmptok == "Greyedout") {
393                         inset = new InsetNote(buf.params(), tmptok);
394                 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
395                         || tmptok == "Shadowbox" || tmptok == "Doublebox"
396                         || tmptok == "Ovalbox" || tmptok == "Frameless") {
397                         inset = new InsetBox(buf.params(), tmptok);
398                 } else if (tmptok == "Branch") {
399                         inset = new InsetBranch(buf.params(), string());
400                 } else if (tmptok == "Include") {
401                         InsetCommandParams p("Include");
402                         inset = new InsetInclude(p);
403                 } else if (tmptok == "Environment") {
404                         lex.next();
405                         inset = new InsetEnvironment(buf.params(), lex.getString());
406                 } else if (tmptok == "ERT") {
407                         inset = new InsetERT(buf.params());
408                 } else if (tmptok == "InsetSpace") {
409                         inset = new InsetSpace;
410                 } else if (tmptok == "Tabular") {
411                         inset = new InsetTabular(buf);
412                 } else if (tmptok == "Text") {
413                         inset = new InsetText(buf.params());
414                 } else if (tmptok == "Foot") {
415                         inset = new InsetFoot(buf.params());
416                 } else if (tmptok == "Marginal") {
417                         inset = new InsetMarginal(buf.params());
418                 } else if (tmptok == "OptArg") {
419                         inset = new InsetOptArg(buf.params());
420                 } else if (tmptok == "Minipage") {
421                         inset = new InsetMinipage(buf.params());
422                 } else if (tmptok == "Float") {
423                         lex.next();
424                         string tmptok = lex.getString();
425                         inset = new InsetFloat(buf.params(), tmptok);
426                 } else if (tmptok == "Wrap") {
427                         lex.next();
428                         string tmptok = lex.getString();
429                         inset = new InsetWrap(buf.params(), tmptok);
430 #if 0
431                 } else if (tmptok == "List") {
432                         inset = new InsetList;
433                 } else if (tmptok == "Theorem") {
434                         inset = new InsetList;
435 #endif
436                 } else if (tmptok == "Caption") {
437                         inset = new InsetCaption(buf.params());
438                 } else if (tmptok == "FloatList") {
439                         inset = new InsetFloatList;
440                 } else {
441                         lyxerr << "unknown Inset type '" << tmptok
442                                << "'" << std::endl;
443                         while (lex.isOK() && lex.getString() != "\\end_inset")
444                                 lex.next();
445                         return 0;
446                 }
447
448                 inset->read(buf, lex);
449         }
450
451         return inset;
452 }