]> git.lyx.org Git - lyx.git/blob - src/factory.C
ec992ba6059cca3643720a756c851f8714a0c451
[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 * inset = new InsetERT(params);
233                         InsetERT::ERTStatus s;
234                         InsetERTMailer::string2params(cmd.argument, s);
235                         inset->status(bv, s);
236                         return inset;
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         }
287
288         case LFUN_INSERT_LABEL: {
289                 InsetCommandParams icp;
290                 InsetCommandMailer::string2params(cmd.argument, icp);
291                 return new InsetLabel(icp);
292         }
293
294         case LFUN_URL:
295         case LFUN_HTMLURL: {
296                 InsetCommandParams icp;
297                 InsetCommandMailer::string2params(cmd.argument, icp);
298                 return new InsetUrl(icp);
299         }
300
301         case LFUN_SPACE_INSERT: {
302                 string const name = cmd.argument;
303                 if (name == "normal")
304                         return new InsetSpace(InsetSpace::NORMAL);
305                 else if (name == "protected")
306                         return new InsetSpace(InsetSpace::PROTECTED);
307                 else if (name == "thin")
308                         return new InsetSpace(InsetSpace::THIN);
309                 else if (name == "quad")
310                         return new InsetSpace(InsetSpace::QUAD);
311                 else if (name == "qquad")
312                         return new InsetSpace(InsetSpace::QQUAD);
313                 else if (name == "enspace")
314                         return new InsetSpace(InsetSpace::ENSPACE);
315                 else if (name == "enskip")
316                         return new InsetSpace(InsetSpace::ENSKIP);
317                 else if (name == "negthinspace")
318                         return new InsetSpace(InsetSpace::NEGTHIN);
319                 else if (name.empty())
320                         lyxerr << "LyX function 'space' needs an argument." << endl;
321                 else
322                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
323         }
324
325         break;
326
327         default:
328                 break;
329         }
330
331         return 0;
332 }
333
334
335 InsetOld * readInset(LyXLex & lex, Buffer const & buf)
336 {
337         // consistency check
338         if (lex.getString() != "\\begin_inset") {
339                 lyxerr << "Buffer::readInset: Consistency check failed."
340                        << endl;
341         }
342
343         auto_ptr<InsetOld> inset;
344
345         LyXTextClass tclass = buf.params().getLyXTextClass();
346                 
347         lex.next();
348         string tmptok = lex.getString();
349         CharStyles::iterator found_cs = tclass.charstyle(tmptok);
350         if (found_cs != tclass.charstyles().end())
351                 tmptok = "CharStyle";
352
353         // test the different insets
354         if (tmptok == "LatexCommand") {
355                 InsetCommandParams inscmd;
356                 inscmd.read(lex);
357
358                 string const cmdName = inscmd.getCmdName();
359
360                 // This strange command allows LyX to recognize "natbib" style
361                 // citations: citet, citep, Citet etc.
362                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
363                         inset.reset(new InsetCitation(inscmd));
364                 } else if (cmdName == "bibitem") {
365                         lex.printError("Wrong place for bibitem");
366                         inset.reset(new InsetBibitem(inscmd));
367                 } else if (cmdName == "bibtex") {
368                         inset.reset(new InsetBibtex(inscmd));
369                 } else if (cmdName == "index") {
370                         inset.reset(new InsetIndex(inscmd));
371                 } else if (cmdName == "include") {
372                         inset.reset(new InsetInclude(inscmd));
373                 } else if (cmdName == "label") {
374                         inset.reset(new InsetLabel(inscmd));
375                 } else if (cmdName == "url"
376                            || cmdName == "htmlurl") {
377                         inset.reset(new InsetUrl(inscmd));
378                 } else if (cmdName == "ref"
379                            || cmdName == "eqref"
380                            || cmdName == "pageref"
381                            || cmdName == "vref"
382                            || cmdName == "vpageref"
383                            || cmdName == "prettyref") {
384                         if (!inscmd.getOptions().empty()
385                             || !inscmd.getContents().empty()) {
386                                 inset.reset(new InsetRef(inscmd, buf));
387                         }
388                 } else if (cmdName == "tableofcontents") {
389                         inset.reset(new InsetTOC(inscmd));
390                 } else if (cmdName == "listofalgorithms") {
391                         inset.reset(new InsetFloatList("algorithm"));
392                 } else if (cmdName == "listoffigures") {
393                         inset.reset(new InsetFloatList("figure"));
394                 } else if (cmdName == "listoftables") {
395                         inset.reset(new InsetFloatList("table"));
396                 } else if (cmdName == "printindex") {
397                         inset.reset(new InsetPrintIndex(inscmd));
398                 } else {
399                         lyxerr << "unknown CommandInset '" << cmdName
400                                << "'" << std::endl;
401                         while (lex.isOK() && lex.getString() != "\\end_inset")
402                                 lex.next();
403                         return 0;
404                 }
405         } else {
406                 if (tmptok == "Quotes") {
407                         inset.reset(new InsetQuotes);
408                 } else if (tmptok == "External") {
409                         inset.reset(new InsetExternal);
410                 } else if (tmptok == "FormulaMacro") {
411                         inset.reset(new InsetFormulaMacro);
412                 } else if (tmptok == "Formula") {
413                         inset.reset(new InsetFormula);
414                 } else if (tmptok == "Graphics") {
415                         inset.reset(new InsetGraphics);
416                 } else if (tmptok == "Note"     || tmptok == "Comment"
417                                 || tmptok == "Greyedout") {
418                         inset.reset(new InsetNote(buf.params(), tmptok));
419                 } else if (tmptok == "Boxed" || tmptok == "ovalbox"
420                         || tmptok == "Shadowbox" || tmptok == "Doublebox"
421                         || tmptok == "Ovalbox" || tmptok == "Frameless") {
422                         inset.reset(new InsetBox(buf.params(), tmptok));
423                 } else if (tmptok == "CharStyle") {
424                         inset.reset(new InsetCharStyle(buf.params(), found_cs));
425                 } else if (tmptok == "Branch") {
426                         inset.reset(new InsetBranch(buf.params(), string()));
427                 } else if (tmptok == "Include") {
428                         InsetCommandParams p("Include");
429                         inset.reset(new InsetInclude(p));
430                 } else if (tmptok == "Environment") {
431                         lex.next();
432                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
433                 } else if (tmptok == "ERT") {
434                         inset.reset(new InsetERT(buf.params()));
435                 } else if (tmptok == "InsetSpace") {
436                         inset.reset(new InsetSpace);
437                 } else if (tmptok == "Tabular") {
438                         inset.reset(new InsetTabular(buf));
439                 } else if (tmptok == "Text") {
440                         inset.reset(new InsetText(buf.params()));
441                 } else if (tmptok == "Foot") {
442                         inset.reset(new InsetFoot(buf.params()));
443                 } else if (tmptok == "Marginal") {
444                         inset.reset(new InsetMarginal(buf.params()));
445                 } else if (tmptok == "OptArg") {
446                         inset.reset(new InsetOptArg(buf.params()));
447                 } else if (tmptok == "Minipage") {
448                         inset.reset(new InsetMinipage(buf.params()));
449                 } else if (tmptok == "Float") {
450                         lex.next();
451                         string tmptok = lex.getString();
452                         inset.reset(new InsetFloat(buf.params(), tmptok));
453                 } else if (tmptok == "Wrap") {
454                         lex.next();
455                         string tmptok = lex.getString();
456                         inset.reset(new InsetWrap(buf.params(), tmptok));
457 #if 0
458                 } else if (tmptok == "List") {
459                         inset.reset(new InsetList);
460                 } else if (tmptok == "Theorem") {
461                         inset.reset(new InsetList);
462 #endif
463                 } else if (tmptok == "Caption") {
464                         inset.reset(new InsetCaption(buf.params()));
465                 } else if (tmptok == "FloatList") {
466                         inset.reset(new InsetFloatList);
467                 } else {
468                         lyxerr << "unknown Inset type '" << tmptok
469                                << "'" << std::endl;
470                         while (lex.isOK() && lex.getString() != "\\end_inset")
471                                 lex.next();
472                         return 0;
473                 }
474
475                 inset->read(buf, lex);
476         }
477
478         return inset.release();
479 }