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