]> git.lyx.org Git - lyx.git/blob - src/factory.C
* Painter.h:
[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/insetnote.h"
44 #include "insets/insetbox.h"
45 #include "insets/insetbranch.h"
46 #include "insets/insetoptarg.h"
47 #include "insets/insetpagebreak.h"
48 #include "insets/insetref.h"
49 #include "insets/insetspace.h"
50 #include "insets/insettabular.h"
51 #include "insets/insettoc.h"
52 #include "insets/inseturl.h"
53 #include "insets/insetvspace.h"
54 #include "insets/insetwrap.h"
55
56 #include "mathed/MathMacroTemplate.h"
57 #include "mathed/InsetMathHull.h"
58
59 #include "support/lstrings.h"
60
61 #include <boost/assert.hpp>
62 #include <boost/current_function.hpp>
63
64 #include <sstream>
65
66
67 namespace lyx {
68
69 using support::compare_ascii_no_case;
70
71 using std::auto_ptr;
72 using std::endl;
73 using std::string;
74
75
76 InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
77 {
78         BufferParams const & params = bv->buffer()->params();
79
80         switch (cmd.action) {
81         case LFUN_HFILL_INSERT:
82                 return new InsetHFill;
83
84         case LFUN_LINE_INSERT:
85                 return new InsetLine;
86
87         case LFUN_PAGEBREAK_INSERT:
88                 return new InsetPagebreak;
89
90         case LFUN_CHARSTYLE_INSERT: {
91                 string s = cmd.getArg(0);
92                 LyXTextClass tclass = params.getLyXTextClass();
93                 CharStyles::iterator found_cs = tclass.charstyle(s);
94                 if (found_cs != tclass.charstyles().end())
95                         return new InsetCharStyle(params, found_cs);
96                 else
97                         return new InsetCharStyle(params, s);
98         }
99
100         case LFUN_NOTE_INSERT: {
101                 string arg = cmd.getArg(0);
102                 if (arg.empty())
103                         arg = "Note";
104                 return new InsetNote(params, arg);
105         }
106
107         case LFUN_BOX_INSERT: {
108                 string arg = cmd.getArg(0);
109                 if (arg.empty())
110                         arg = "Boxed";
111                 return new InsetBox(params, arg);
112         }
113
114         case LFUN_BRANCH_INSERT: {
115                 string arg = cmd.getArg(0);
116                 if (arg.empty())
117                         arg = "none";
118                 return new InsetBranch(params, InsetBranchParams(arg));
119         }
120
121         case LFUN_ERT_INSERT:
122                 return new InsetERT(params);
123
124         case LFUN_FOOTNOTE_INSERT:
125                 return new InsetFoot(params);
126
127         case LFUN_MARGINALNOTE_INSERT:
128                 return new InsetMarginal(params);
129
130         case LFUN_OPTIONAL_INSERT:
131                 return new InsetOptArg(params);
132
133         case LFUN_BIBITEM_INSERT:
134                 return new InsetBibitem(InsetCommandParams("bibitem"));
135
136         case LFUN_FLOAT_INSERT: {
137                 // check if the float type exists
138                 string const argument = to_utf8(cmd.argument());
139                 if (params.getLyXTextClass().floats().typeExist(argument))
140                         return new InsetFloat(params, argument);
141                 lyxerr << "Non-existent float type: " << argument << endl;
142                 return 0;
143         }
144
145         case LFUN_FLOAT_WIDE_INSERT: {
146                 // check if the float type exists
147                 string const argument = to_utf8(cmd.argument());
148                 if (params.getLyXTextClass().floats().typeExist(argument)) {
149                         auto_ptr<InsetFloat> p(new InsetFloat(params, argument));
150                         p->wide(true, params);
151                         return p.release();
152                 }
153                 lyxerr << "Non-existent float type: " << argument << endl;
154                 return 0;
155         }
156
157         case LFUN_WRAP_INSERT: {
158                 string const argument = to_utf8(cmd.argument());
159                 if (argument == "figure")
160                         return new InsetWrap(params, argument);
161                 lyxerr << "Non-existent floatflt type: " << argument << endl;
162                 return 0;
163         }
164
165         case LFUN_INDEX_INSERT: {
166                 // Try and generate a valid index entry.
167                 InsetCommandParams icp("index");
168                 icp["name"] = cmd.argument().empty() ?
169                         from_utf8(bv->getLyXText()->getStringToIndex(bv->cursor())) :
170                         cmd.argument();
171                 return new InsetIndex(icp);
172         }
173
174         case LFUN_TABULAR_INSERT: {
175                 if (cmd.argument().empty())
176                         return 0;
177                 std::istringstream ss(to_utf8(cmd.argument()));
178                 int r = 0, c = 0;
179                 ss >> r >> c;
180                 if (r <= 0)
181                         r = 2;
182                 if (c <= 0)
183                         c = 2;
184                 return new InsetTabular(*bv->buffer(), r, c);
185         }
186
187         case LFUN_CAPTION_INSERT: {
188                 auto_ptr<InsetCaption> inset(new InsetCaption(params));
189                 inset->setAutoBreakRows(true);
190                 inset->setDrawFrame(true);
191                 inset->setFrameColor(LColor::captionframe);
192                 return inset.release();
193         }
194
195         case LFUN_INDEX_PRINT:
196                 return new InsetPrintIndex(InsetCommandParams("printindex"));
197
198         case LFUN_TOC_INSERT:
199                 return new InsetTOC(InsetCommandParams("tableofcontents"));
200
201         case LFUN_ENVIRONMENT_INSERT:
202                 return new InsetEnvironment(params, to_utf8(cmd.argument()));
203
204 #if 0
205         case LFUN_LIST_INSERT:
206                 return new InsetList;
207
208         case LFUN_THEOREM_INSERT:
209                 return new InsetTheorem;
210 #endif
211
212         case LFUN_INSET_INSERT: {
213                 string const name = cmd.getArg(0);
214
215                 if (name == "bibitem") {
216                         InsetCommandParams icp(name);
217                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
218                                                           icp);
219                         return new InsetBibitem(icp);
220
221                 } else if (name == "bibtex") {
222                         InsetCommandParams icp(name);
223                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
224                                                           icp);
225                         return new InsetBibtex(icp);
226
227                 } else if (name == "citation") {
228                         InsetCommandParams icp("cite");
229                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
230                                                           icp);
231                         return new InsetCitation(icp);
232
233                 } else if (name == "ert") {
234                         InsetCollapsable::CollapseStatus st;
235                         InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
236                         return new InsetERT(params, st);
237
238                 } else if (name == "external") {
239                         Buffer const & buffer = *bv->buffer();
240                         InsetExternalParams iep;
241                         InsetExternalMailer::string2params(to_utf8(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 = *bv->buffer();
249                         InsetGraphicsParams igp;
250                         InsetGraphicsMailer::string2params(to_utf8(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(name);
258                         InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
259                         return new InsetInclude(iip);
260
261                 } else if (name == "index") {
262                         InsetCommandParams icp(name);
263                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
264                                                           icp);
265                         return new InsetIndex(icp);
266
267                 } else if (name == "label") {
268                         InsetCommandParams icp(name);
269                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
270                                                           icp);
271                         return new InsetLabel(icp);
272
273                 } else if (name == "ref") {
274                         InsetCommandParams icp(name);
275                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
276                                                           icp);
277                         return new InsetRef(icp, *bv->buffer());
278
279                 } else if (name == "toc") {
280                         InsetCommandParams icp("tableofcontents");
281                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
282                                                           icp);
283                         return new InsetTOC(icp);
284
285                 } else if (name == "url") {
286                         InsetCommandParams icp(name);
287                         InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
288                                                           icp);
289                         return new InsetUrl(icp);
290
291                 } else if (name == "vspace") {
292                         VSpace vspace;
293                         InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), vspace);
294                         return new InsetVSpace(vspace);
295                 }
296         }
297
298         case LFUN_SPACE_INSERT: {
299                 string const name = to_utf8(cmd.argument());
300                 if (name == "normal")
301                         return new InsetSpace(InsetSpace::NORMAL);
302                 else if (name == "protected")
303                         return new InsetSpace(InsetSpace::PROTECTED);
304                 else if (name == "thin")
305                         return new InsetSpace(InsetSpace::THIN);
306                 else if (name == "quad")
307                         return new InsetSpace(InsetSpace::QUAD);
308                 else if (name == "qquad")
309                         return new InsetSpace(InsetSpace::QQUAD);
310                 else if (name == "enspace")
311                         return new InsetSpace(InsetSpace::ENSPACE);
312                 else if (name == "enskip")
313                         return new InsetSpace(InsetSpace::ENSKIP);
314                 else if (name == "negthinspace")
315                         return new InsetSpace(InsetSpace::NEGTHIN);
316                 else if (name.empty())
317                         lyxerr << "LyX function 'space' needs an argument." << endl;
318                 else
319                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
320         }
321
322         break;
323
324         default:
325                 break;
326         }
327
328         return 0;
329 }
330
331
332 InsetBase * readInset(LyXLex & lex, Buffer const & buf)
333 {
334         // consistency check
335         if (lex.getString() != "\\begin_inset") {
336                 lyxerr << "Buffer::readInset: Consistency check failed."
337                        << endl;
338         }
339
340         auto_ptr<InsetBase> inset;
341
342         LyXTextClass tclass = buf.params().getLyXTextClass();
343
344         lex.next();
345         string tmptok = lex.getString();
346
347         // test the different insets
348         if (tmptok == "LatexCommand") {
349                 lex.next();
350                 string const cmdName = lex.getString();
351                 lex.pushToken(cmdName);
352
353                 InsetCommandParams inscmd(cmdName);
354                 inscmd.read(lex);
355
356                 // This strange command allows LyX to recognize "natbib" style
357                 // citations: citet, citep, Citet etc.
358                 // FIXME: We already have partial support for \\fullcite and
359                 // the various \\footcite commands. We should increase the
360                 // file format number and read these commands here, too.
361                 // Then we should use is_possible_cite_command() in
362                 // src/frontends/controllers/biblio.C to test for valid cite
363                 // commands.
364                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
365                         inset.reset(new InsetCitation(inscmd));
366                 } else if (cmdName == "bibitem") {
367                         inset.reset(new InsetBibitem(inscmd));
368                 } else if (cmdName == "bibtex") {
369                         inset.reset(new InsetBibtex(inscmd));
370                 } else if (cmdName == "index") {
371                         inset.reset(new InsetIndex(inscmd));
372                 } else if (cmdName == "include") {
373                         inset.reset(new InsetInclude(inscmd));
374                 } else if (cmdName == "label") {
375                         inset.reset(new InsetLabel(inscmd));
376                 } else if (cmdName == "url"
377                            || cmdName == "htmlurl") {
378                         inset.reset(new InsetUrl(inscmd));
379                 } else if (cmdName == "ref"
380                            || cmdName == "eqref"
381                            || cmdName == "pageref"
382                            || cmdName == "vref"
383                            || cmdName == "vpageref"
384                            || cmdName == "prettyref") {
385                         if (!inscmd["name"].empty()
386                             || !inscmd["reference"].empty()) {
387                                 inset.reset(new InsetRef(inscmd, buf));
388                         }
389                 } else if (cmdName == "tableofcontents") {
390                         inset.reset(new InsetTOC(inscmd));
391                 } else if (cmdName == "listofalgorithms") {
392                         inset.reset(new InsetFloatList("algorithm"));
393                 } else if (cmdName == "listoffigures") {
394                         inset.reset(new InsetFloatList("figure"));
395                 } else if (cmdName == "listoftables") {
396                         inset.reset(new InsetFloatList("table"));
397                 } else if (cmdName == "printindex") {
398                         inset.reset(new InsetPrintIndex(inscmd));
399                 } else {
400                         lyxerr << "unknown CommandInset '" << cmdName
401                                << "'" << std::endl;
402                         while (lex.isOK() && lex.getString() != "\\end_inset")
403                                 lex.next();
404                         return 0;
405                 }
406         } else {
407                 if (tmptok == "Quotes") {
408                         inset.reset(new InsetQuotes);
409                 } else if (tmptok == "External") {
410                         inset.reset(new InsetExternal);
411                 } else if (tmptok == "FormulaMacro") {
412                         inset.reset(new MathMacroTemplate);
413                 } else if (tmptok == "Formula") {
414                         inset.reset(new InsetMathHull);
415                 } else if (tmptok == "Graphics") {
416                         inset.reset(new InsetGraphics);
417                 } else if (tmptok == "Note") {
418                         inset.reset(new InsetNote(buf.params(), tmptok));
419                 } else if (tmptok == "Box") {
420                         inset.reset(new InsetBox(buf.params(), tmptok));
421                 } else if (tmptok == "CharStyle") {
422                         lex.next();
423                         string s = lex.getString();
424                         CharStyles::iterator found_cs = tclass.charstyle(s);
425                         if (found_cs != tclass.charstyles().end())
426                                 inset.reset(new InsetCharStyle(buf.params(), found_cs));
427                         else {
428                                 // "Undefined" inset
429                                 inset.reset(new InsetCharStyle(buf.params(), s));
430                         }
431                 } else if (tmptok == "Branch") {
432                         inset.reset(new InsetBranch(buf.params(),
433                                                     InsetBranchParams()));
434                 } else if (tmptok == "Include") {
435                         InsetCommandParams p("Include");
436                         inset.reset(new InsetInclude(p));
437                 } else if (tmptok == "Environment") {
438                         lex.next();
439                         inset.reset(new InsetEnvironment(buf.params(), lex.getString()));
440                 } else if (tmptok == "ERT") {
441                         inset.reset(new InsetERT(buf.params()));
442                 } else if (tmptok == "InsetSpace") {
443                         inset.reset(new InsetSpace);
444                 } else if (tmptok == "Tabular") {
445                         inset.reset(new InsetTabular(buf));
446                 } else if (tmptok == "Text") {
447                         inset.reset(new InsetText(buf.params()));
448                 } else if (tmptok == "VSpace") {
449                         inset.reset(new InsetVSpace);
450                 } else if (tmptok == "Foot") {
451                         inset.reset(new InsetFoot(buf.params()));
452                 } else if (tmptok == "Marginal") {
453                         inset.reset(new InsetMarginal(buf.params()));
454                 } else if (tmptok == "OptArg") {
455                         inset.reset(new InsetOptArg(buf.params()));
456                 } else if (tmptok == "Float") {
457                         lex.next();
458                         string tmptok = lex.getString();
459                         inset.reset(new InsetFloat(buf.params(), tmptok));
460                 } else if (tmptok == "Wrap") {
461                         lex.next();
462                         string tmptok = lex.getString();
463                         inset.reset(new InsetWrap(buf.params(), tmptok));
464 #if 0
465                 } else if (tmptok == "List") {
466                         inset.reset(new InsetList);
467                 } else if (tmptok == "Theorem") {
468                         inset.reset(new InsetList);
469 #endif
470                 } else if (tmptok == "Caption") {
471                         inset.reset(new InsetCaption(buf.params()));
472                 } else if (tmptok == "FloatList") {
473                         inset.reset(new InsetFloatList);
474                 } else {
475                         lyxerr << "unknown Inset type '" << tmptok
476                                << "'" << std::endl;
477                         while (lex.isOK() && lex.getString() != "\\end_inset")
478                                 lex.next();
479                         return 0;
480                 }
481
482                 inset->read(buf, lex);
483
484 #ifdef WITH_WARNINGS
485 #warning hack..
486 #endif
487                 if (inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
488                         MathMacroTemplate const * tmpl =
489                                 static_cast<MathMacroTemplate*>(inset.get());
490                         MacroTable::globalMacros().insert
491                                 (tmpl->name(), tmpl->asMacroData());
492                         lyxerr[Debug::DEBUG]
493                                 << BOOST_CURRENT_FUNCTION
494                                 << ": creating local macro " << to_utf8(tmpl->name())
495                                 << endl;
496                 }
497         }
498
499         return inset.release();
500 }
501
502
503 } // namespace lyx