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