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