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