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