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