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