]> git.lyx.org Git - lyx.git/blob - src/factory.C
e02a76b10e00eebf6421d13f20ee529fc4c5f705
[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 Inset * 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                         InsetExternal::Params iep;
202                         InsetExternalMailer::string2params(cmd.argument, iep);
203                         InsetExternal * inset = new InsetExternal;
204                         string const fpath = cmd.view()->buffer()->filePath();
205                         inset->setParams(iep, fpath);
206                         return inset;
207
208                 } else if (name == "graphics") {
209                         InsetGraphicsParams igp;
210                         InsetGraphicsMailer::string2params(cmd.argument, igp);
211                         InsetGraphics * inset = new InsetGraphics;
212                         inset->setParams(igp);
213                         return inset;
214
215                 } else if (name == "include") {
216                         InsetInclude::Params iip;
217                         InsetIncludeMailer::string2params(cmd.argument, iip);
218                         return new InsetInclude(iip);
219
220                 } else if (name == "index") {
221                         InsetCommandParams icp;
222                         InsetCommandMailer::string2params(cmd.argument, icp);
223                         return new InsetIndex(icp);
224
225                 } else if (name == "label") {
226                         InsetCommandParams icp;
227                         InsetCommandMailer::string2params(cmd.argument, icp);
228                         return new InsetLabel(icp);
229
230                 } else if (name == "ref") {
231                         InsetCommandParams icp;
232                         InsetCommandMailer::string2params(cmd.argument, icp);
233                         return new InsetRef(icp, *bv->buffer());
234
235                 } else if (name == "toc") {
236                         InsetCommandParams icp;
237                         InsetCommandMailer::string2params(cmd.argument, icp);
238                         return new InsetTOC(icp);
239
240                 } else if (name == "url") {
241                         InsetCommandParams icp;
242                         InsetCommandMailer::string2params(cmd.argument, icp);
243                         return new InsetUrl(icp);
244                 }
245         }
246
247         case LFUN_SPACE_INSERT: {
248                 string const name = cmd.argument;
249                 if (name == "normal")
250                         return new InsetSpace(InsetSpace::NORMAL);
251                 else if (name == "protected")
252                         return new InsetSpace(InsetSpace::PROTECTED);
253                 else if (name == "thin")
254                         return new InsetSpace(InsetSpace::THIN);
255                 else if (name == "quad")
256                         return new InsetSpace(InsetSpace::QUAD);
257                 else if (name == "qquad")
258                         return new InsetSpace(InsetSpace::QQUAD);
259                 else if (name == "enspace")
260                         return new InsetSpace(InsetSpace::ENSPACE);
261                 else if (name == "enskip")
262                         return new InsetSpace(InsetSpace::ENSKIP);
263                 else if (name == "negthinspace")
264                         return new InsetSpace(InsetSpace::NEGTHIN);
265                 else if (name.empty())
266                         lyxerr << "LyX function 'space' needs an argument." << endl;
267                 else
268                         lyxerr << "Wrong argument for LyX function 'space'." << endl;
269         }
270
271         break;
272
273         default:
274                 break;
275         }
276
277         return 0;
278 }
279
280
281 Inset * readInset(LyXLex & lex, Buffer const & buf)
282 {
283         // consistency check
284         if (lex.getString() != "\\begin_inset") {
285                 lyxerr << "Buffer::readInset: Consistency check failed."
286                        << endl;
287         }
288
289         Inset * inset = 0;
290
291         lex.next();
292         string const tmptok = lex.getString();
293
294         // test the different insets
295         if (tmptok == "LatexCommand") {
296                 InsetCommandParams inscmd;
297                 inscmd.read(lex);
298
299                 string const cmdName = inscmd.getCmdName();
300
301                 // This strange command allows LyX to recognize "natbib" style
302                 // citations: citet, citep, Citet etc.
303                 if (compare_ascii_no_case(cmdName.substr(0,4), "cite") == 0) {
304                         inset = new InsetCitation(inscmd);
305                 } else if (cmdName == "bibitem") {
306                         lex.printError("Wrong place for bibitem");
307                         inset = new InsetBibitem(inscmd);
308                 } else if (cmdName == "bibtex") {
309                         inset = new InsetBibtex(inscmd);
310                 } else if (cmdName == "index") {
311                         inset = new InsetIndex(inscmd);
312                 } else if (cmdName == "include") {
313                         inset = new InsetInclude(inscmd, buf);
314                 } else if (cmdName == "label") {
315                         inset = new InsetLabel(inscmd);
316                 } else if (cmdName == "url"
317                            || cmdName == "htmlurl") {
318                         inset = new InsetUrl(inscmd);
319                 } else if (cmdName == "ref"
320                            || cmdName == "eqref"
321                            || cmdName == "pageref"
322                            || cmdName == "vref"
323                            || cmdName == "vpageref"
324                            || cmdName == "prettyref") {
325                         if (!inscmd.getOptions().empty()
326                             || !inscmd.getContents().empty()) {
327                                 inset = new InsetRef(inscmd, buf);
328                         }
329                 } else if (cmdName == "tableofcontents") {
330                         inset = new InsetTOC(inscmd);
331                 } else if (cmdName == "listofalgorithms") {
332                         inset = new InsetFloatList("algorithm");
333                 } else if (cmdName == "listoffigures") {
334                         inset = new InsetFloatList("figure");
335                 } else if (cmdName == "listoftables") {
336                         inset = new InsetFloatList("table");
337                 } else if (cmdName == "printindex") {
338                         inset = new InsetPrintIndex(inscmd);
339                 }
340         } else {
341                 if (tmptok == "Quotes") {
342                         inset = new InsetQuotes;
343                 } else if (tmptok == "External") {
344                         inset = new InsetExternal;
345                 } else if (tmptok == "FormulaMacro") {
346                         inset = new InsetFormulaMacro;
347                 } else if (tmptok == "Formula") {
348                         inset = new InsetFormula;
349                 } else if (tmptok == "Graphics") {
350                         inset = new InsetGraphics;
351                 } else if (tmptok == "Note"     || tmptok == "Comment" 
352                                 || tmptok == "Greyedout") {
353                         inset = new InsetNote(buf.params, tmptok); 
354                 } else if (tmptok == "Include") {
355                         InsetCommandParams p("Include");
356                         inset = new InsetInclude(p, buf);
357                 } else if (tmptok == "Environment") {
358                         lex.next();
359                         inset = new InsetEnvironment(buf.params, lex.getString());
360                 } else if (tmptok == "ERT") {
361                         inset = new InsetERT(buf.params);
362                 } else if (tmptok == "InsetSpace") {
363                         inset = new InsetSpace;
364                 } else if (tmptok == "Tabular") {
365                         inset = new InsetTabular(buf);
366                 } else if (tmptok == "Text") {
367                         inset = new InsetText(buf.params);
368                 } else if (tmptok == "Foot") {
369                         inset = new InsetFoot(buf.params);
370                 } else if (tmptok == "Marginal") {
371                         inset = new InsetMarginal(buf.params);
372                 } else if (tmptok == "OptArg") {
373                         inset = new InsetOptArg(buf.params);
374                 } else if (tmptok == "Minipage") {
375                         inset = new InsetMinipage(buf.params);
376                 } else if (tmptok == "Float") {
377                         lex.next();
378                         string tmptok = lex.getString();
379                         inset = new InsetFloat(buf.params, tmptok);
380                 } else if (tmptok == "Wrap") {
381                         lex.next();
382                         string tmptok = lex.getString();
383                         inset = new InsetWrap(buf.params, tmptok);
384 #if 0
385                 } else if (tmptok == "List") {
386                         inset = new InsetList;
387                 } else if (tmptok == "Theorem") {
388                         inset = new InsetList;
389 #endif
390                 } else if (tmptok == "Caption") {
391                         inset = new InsetCaption(buf.params);
392                 } else if (tmptok == "FloatList") {
393                         inset = new InsetFloatList;
394                 }
395
396                 if (inset)
397                         inset->read(&buf, lex);
398         }
399
400         return inset;
401 }