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