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