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