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